Debian Codegraph @

libgo/go/runtime/pprof/rusage_test.go

raw
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build darwin || freebsd || linux || netbsd || openbsd
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 }