1
2
3
4
5
6
7 package pprof
8
9 import (
10 "syscall"
11 "time"
12 )
13
14 func init() {
15 diffCPUTimeImpl = diffCPUTimeRUsage
16 }
17
18 func diffCPUTimeRUsage(f func()) time.Duration {
19 ok := true
20 var before, after syscall.Rusage
21
22 err := syscall.Getrusage(syscall.RUSAGE_SELF, &before)
23 if err != nil {
24 ok = false
25 }
26
27 f()
28
29 err = syscall.Getrusage(syscall.RUSAGE_SELF, &after)
30 if err != nil {
31 ok = false
32 }
33
34 if !ok {
35 return 0
36 }
37
38 return time.Duration((after.Utime.Nano() + after.Stime.Nano()) - (before.Utime.Nano() + before.Stime.Nano()))
39 }
40
View as plain text