func captureRusageStats(r metrics.Registry, d time.Duration) { for { ru := getRusage() time.Sleep(d) ru2 := getRusage() if ru == nil || ru2 == nil { continue } rVals := map[string]int64{ "UserTime": syscall.TimevalToNsec(ru2.Utime) - syscall.TimevalToNsec(ru.Utime), "SystemTime": syscall.TimevalToNsec(ru2.Stime) - syscall.TimevalToNsec(ru.Stime), "MaxRss": ru.Maxrss, "InBlock": ru2.Inblock - ru.Inblock, "OuBlock": ru2.Oublock - ru.Oublock, } for metric, val := range rVals { if g := getGauge(r, "rusage."+metric); g != nil { g.(metrics.Gauge).Update(val) } } } }
// CPUtime returns the current CPU usage (user time + system time). func CPUtime() time.Duration { var ustruct syscall.Rusage rx.CkErr(syscall.Getrusage(0, &ustruct)) user := time.Duration(syscall.TimevalToNsec(ustruct.Utime)) sys := time.Duration(syscall.TimevalToNsec(ustruct.Stime)) return user + sys return 0 }
func (i *Instrumentation) sampleRusage(ru, ru2 *syscall.Rusage) { if ru == nil || ru2 == nil { return } i.Gauge(1.0, "rusage.userTime", int(syscall.TimevalToNsec(ru2.Utime)-syscall.TimevalToNsec(ru.Utime))) i.Gauge(1.0, "rusage.systemTime", int(syscall.TimevalToNsec(ru2.Stime)-syscall.TimevalToNsec(ru.Stime))) i.Gauge(1.0, "rusage.maxRss", int(ru.Maxrss)) i.Gauge(1.0, "rusage.inBlock", int(ru2.Inblock-ru.Inblock)) i.Gauge(1.0, "rusage.outBlock", int(ru2.Oublock-ru.Oublock)) }
func (ct cpuTimeMetric) Collect(ch chan<- prometheus.Metric) { var rusage syscall.Rusage if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err == nil { m := ct.counter.WithLabelValues("user") m.Set(float64(syscall.TimevalToNsec(rusage.Utime))) ch <- m m = ct.counter.WithLabelValues("system") m.Set(float64(syscall.TimevalToNsec(rusage.Stime))) ch <- m } }
func TimevalToDuration(tv syscall.Timeval) time.Duration { ns := syscall.TimevalToNsec(tv) return time.Duration(ns) * time.Nanosecond }