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()) (user, system 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, 0
36 }
37
38 user = time.Duration(after.Utime.Nano() - before.Utime.Nano())
39 system = time.Duration(after.Stime.Nano() - before.Stime.Nano())
40 return user, system
41 }
42
View as plain text