I am trying to query the process statistics against the runtime in Windows so I can monitor it's utilisation.
First, I get the process handle for the runtime:
// GetCurrentProcess will return our process handle with the appropriate information.
func GetCurrentProcess() (syscall.Handle, error) {
myProcess, err := syscall.GetCurrentProcess()
if err != nil {
return syscall.Handle(0), err
}
return syscall.Handle(myHandle), err
}
Then, I sleep for one second to make sure the process has been running for at least one second, so no counters will be 0. After that, I call `syscall.GetProcessTimes()`:
func (p *ProcInfo) GetProcessTimes() error {
lpCreationTime := new(syscall.Filetime)
lpExitTime := new(syscall.Filetime)
lpKernelTime := new(syscall.Filetime)
lpUserTime := new(syscall.Filetime)
if err := syscall.GetProcessTimes(p.handle, lpCreationTime, lpExitTime, lpKernelTime, lpUserTime); err != nil {
return err
}
p.CreationTime = lpCreationTime
p.ExitTime = lpExitTime
p.KernelTime = lpKernelTime
p.UserTime = lpUserTime
return nil
}
Once that is done, I print out the values:
func main() {
me, err := GetCurrentProcess()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
proc := &ProcInfo{handle: me}
time.Sleep(time.Duration(time.Second * 1))
err = proc.GetProcessTimes()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%#v\n", proc)
fmt.Printf("kernel low time: %d\n", proc.KernelTime.LowDateTime)
fmt.Printf("kernel high time: %d\n", proc.KernelTime.HighDateTime)
fmt.Printf("user low time: %d\n", proc.UserTime.LowDateTime)
fmt.Printf("user high time: %d\n", proc.UserTime.HighDateTime)
}
I've been reading through the
Windows API documentation and it should return the value in 100-nanosecond units the process has spent in user and/or kernel space. However, even thought I'm sleeping my little test program for 1 second, all the counters from `syscall.GetProcessTimes()` are 0. They should at least be in the millions since the program slept for 1 second.
Here is the full code:
https://play.golang.org/p/_Z3o4H2WwRIs this a bug or am I not using it correctly?