github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/sysinfo/usage_windows.go (about)

     1  package sysinfo
     2  
     3  import (
     4  	"syscall"
     5  	"time"
     6  )
     7  
     8  func filetimeToDuration(ft *syscall.Filetime) time.Duration {
     9  	ns := ft.Nanoseconds()
    10  	return time.Duration(ns)
    11  }
    12  
    13  // GetUsage gathers process times.
    14  func GetUsage() (Usage, error) {
    15  	var creationTime syscall.Filetime
    16  	var exitTime syscall.Filetime
    17  	var kernelTime syscall.Filetime
    18  	var userTime syscall.Filetime
    19  
    20  	handle, err := syscall.GetCurrentProcess()
    21  	if err != nil {
    22  		return Usage{}, err
    23  	}
    24  
    25  	err = syscall.GetProcessTimes(handle, &creationTime, &exitTime, &kernelTime, &userTime)
    26  	if err != nil {
    27  		return Usage{}, err
    28  	}
    29  
    30  	return Usage{
    31  		System: filetimeToDuration(&kernelTime),
    32  		User:   filetimeToDuration(&userTime),
    33  	}, nil
    34  }