gopsutil icon indicating copy to clipboard operation
gopsutil copied to clipboard

get boot time each time cost too much cpu

Open hz-kelpie opened this issue 3 years ago • 5 comments

i use process.NewProcess(pid) 1000 processes per second but common.BootTimeWithContext() runs every time,it depends on syscall.Syscall, it's blocked maybe there's some way I don't know to cache the Process.starttime? thx~

hz-kelpie avatar May 10 '21 07:05 hz-kelpie

We can't cache host.BootTime as it can jump if time gets adjusted (like with ntpd)

Tested on a Raspberry Pi

pi@raspberrypi:~ $ grep btime /proc/stat
btime 1621271964
pi@raspberrypi:~ $ sudo systemctl disable systemd-timesyncd
pi@raspberrypi:~ $ sudo systemctl stop systemd-timesyncd
pi@raspberrypi:~ $ sudo systemctl disable fake-hwclock.service
pi@raspberrypi:~ $ sudo systemctl stop fake-hwclock.service
pi@raspberrypi:~ $ sudo reboot
# after reboot
pi@raspberrypi:~ $ grep btime /proc/stat
btime 1550139115
pi@raspberrypi:~ $ sudo systemctl enable systemd-timesyncd
pi@raspberrypi:~ $ sudo systemctl start systemd-timesyncd
pi@raspberrypi:~ $ grep btime /proc/stat
btime 1621272415
# btime jumped

See also #857/#837

Lomanic avatar May 17 '21 17:05 Lomanic

Thanks for your response, I think you are right can i skip the step getting boot time , It's bit of a waste of time

hz-kelpie avatar May 18 '21 03:05 hz-kelpie

Because this method has a lock I can't reduce the CPU even with concurrency

hz-kelpie avatar May 18 '21 03:05 hz-kelpie

Meet the same question. "process.NewProcessWithContext()" blocked in my situation when using pprof to monitor go routine stacks. And I found another problem, "process.NewProcessWithContext()" seems not using context (1th param) as a timeout control threshold. Then block may happen. image

ellocano avatar Dec 20 '21 08:12 ellocano