github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/pprof/rusage_test.go (about) 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 unix 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 }