turning a /proc/$pid/stat startTime into "seconds since this process started"

87 views
Skip to first unread message

andrew.geo...@gmail.com

unread,
Jan 18, 2018, 10:19:55 PM1/18/18
to golang-nuts
I want to find out how long a process has been running.

I'm grabbing the starttime out of /proc/$pid/stat for a process. This is in "in clock ticks (divide by sysconf(_SC_CLK_TCK))." according to http://man7.org/linux/man-pages/man5/proc.5.html

I also have Sysinfo.Uptime (seconds since boot). My plan was to subtract proc starttime from uptime, but I need to first divide out the clock tick / second to get my units right. I haven't been able to find a way to get this value.

I suspect that my approach is wrong and that there is probably a more elegant way to do this.

A

Caleb Spare

unread,
Jan 18, 2018, 10:45:14 PM1/18/18
to andrew.geo...@gmail.com, golang-nuts
You can do it with cgo. I have some code that does this:

// #include <unistd.h>
import "C"

func init() {
clockTicksPerSec = float64(C.sysconf(C._SC_CLK_TCK))
if clockTicksPerSec != 100 {
log.Println("Unusual _SC_CLK_TCK value:", clockTicksPerSec)
}
}

var clockTicksPerSec float64

// later it normalizes things by clickTicksPerSec
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andrew Hammond

unread,
Jan 19, 2018, 4:02:51 PM1/19/18
to golang-nuts
I punted and just did clockTicksPerSecond := float64(100) since I couldn't get this to build correctly. It's supposed to be a statically linked binary in a docker scratch container. Close enough, I guess.
Reply all
Reply to author
Forward
0 new messages