func loadFromAll(generators []generator, datacentre string, logger *log.Logger) (*mdb.Mdb, error) { machineMap := make(map[string]mdb.Machine) startTime := time.Now() var rusageStart, rusageStop syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart) for _, gen := range generators { mdb, err := gen.Generate(datacentre, logger) if err != nil { return nil, err } for _, machine := range mdb.Machines { if oldMachine, ok := machineMap[machine.Hostname]; ok { oldMachine.UpdateFrom(machine) machineMap[machine.Hostname] = oldMachine } else { machineMap[machine.Hostname] = machine } } } var newMdb mdb.Mdb for _, machine := range machineMap { newMdb.Machines = append(newMdb.Machines, machine) } syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop) loadTimeDistribution.Add(time.Since(startTime)) loadCpuTimeDistribution.Add(time.Duration( rusageStop.Utime.Sec)*time.Second + time.Duration(rusageStop.Utime.Usec)*time.Microsecond - time.Duration(rusageStart.Utime.Sec)*time.Second - time.Duration(rusageStart.Utime.Usec)*time.Microsecond) return &newMdb, nil }
func main() { lim := &syscall.Rlimit{} syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim) lim.Cur = 15 syscall.Setrlimit(syscall.RLIMIT_NOFILE, lim) fmt.Println(lim) var s []*os.File for i := 0; i < int(lim.Max+10); i++ { f, err := os.Open("/tmp/whj.txt") fmt.Println(f, err) s = append(s, f) err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, lim) fmt.Println(lim, err) } err := syscall.Getrlimit(syscall.RLIMIT_CORE, lim) fmt.Println(lim, err) rusage := &syscall.Rusage{} err = syscall.Getrusage(0, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(1, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(2, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(3, rusage) fmt.Println(rusage, err) err = syscall.Getrusage(4, rusage) fmt.Println(rusage, err) }
func (imageObjectServers *imageObjectServersType) WriteHtml(writer io.Writer) { // TODO(rgooch): These statistics should be cached and the cache invalidated // when images and objects are added/deleted. var rusageStart, rusageStop syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart) objectsMap := imageObjectServers.objSrv.ListObjectSizes() var totalBytes uint64 for _, bytes := range objectsMap { totalBytes += bytes } numObjects := len(objectsMap) fmt.Fprintf(writer, "Number of objects: %d, consumimg %s<br>\n", numObjects, format.FormatBytes(totalBytes)) for _, imageName := range imageObjectServers.imdb.ListImages() { image := imageObjectServers.imdb.GetImage(imageName) if image == nil { continue } for _, inode := range image.FileSystem.InodeTable { if inode, ok := inode.(*filesystem.RegularInode); ok { delete(objectsMap, inode.Hash) } } } var unreferencedBytes uint64 for _, bytes := range objectsMap { unreferencedBytes += bytes } unreferencedObjectsPercent := 0.0 if numObjects > 0 { unreferencedObjectsPercent = 100.0 * float64(len(objectsMap)) / float64(numObjects) } unreferencedBytesPercent := 0.0 if totalBytes > 0 { unreferencedBytesPercent = 100.0 * float64(unreferencedBytes) / float64(totalBytes) } syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop) statisticsComputeCpuTimeDistribution.Add(time.Duration( rusageStop.Utime.Sec)*time.Second + time.Duration(rusageStop.Utime.Usec)*time.Microsecond - time.Duration(rusageStart.Utime.Sec)*time.Second - time.Duration(rusageStart.Utime.Usec)*time.Microsecond) fmt.Fprintf(writer, "Number of unreferenced objects: %d (%.1f%%), "+ "consuming %s (%.1f%%)<br>\n", len(objectsMap), unreferencedObjectsPercent, format.FormatBytes(unreferencedBytes), unreferencedBytesPercent) }
// Returns true if no update needs to be performed. func (sub *Sub) buildUpdateRequest(request *subproto.UpdateRequest) bool { var state state state.subFS = &sub.fileSystem.FileSystem requiredImage := sub.herd.getImage(sub.requiredImage) state.requiredFS = requiredImage.FileSystem filter := requiredImage.Filter request.Triggers = requiredImage.Triggers state.requiredInodeToSubInode = make(map[uint64]uint64) state.inodesChanged = make(map[uint64]bool) state.inodesCreated = make(map[uint64]string) state.subObjectCacheUsage = make(map[hash.Hash]uint64, len(sub.fileSystem.ObjectCache)) var rusageStart, rusageStop syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart) // Populate subObjectCacheUsage. for _, hash := range sub.fileSystem.ObjectCache { state.subObjectCacheUsage[hash] = 0 } compareDirectories(request, &state, &state.subFS.DirectoryInode, &state.requiredFS.DirectoryInode, "/", filter) // Look for multiply used objects and tell the sub. for obj, useCount := range state.subObjectCacheUsage { if useCount > 1 { if request.MultiplyUsedObjects == nil { request.MultiplyUsedObjects = make(map[hash.Hash]uint64) } request.MultiplyUsedObjects[obj] = useCount } } syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop) sub.lastComputeUpdateCpuDuration = time.Duration( rusageStop.Utime.Sec)*time.Second + time.Duration(rusageStop.Utime.Usec)*time.Microsecond - time.Duration(rusageStart.Utime.Sec)*time.Second - time.Duration(rusageStart.Utime.Usec)*time.Microsecond computeCpuTimeDistribution.Add(sub.lastComputeUpdateCpuDuration) if len(request.FilesToCopyToCache) > 0 || len(request.InodesToMake) > 0 || len(request.HardlinksToMake) > 0 || len(request.PathsToDelete) > 0 || len(request.DirectoriesToMake) > 0 || len(request.InodesToChange) > 0 { sub.herd.logger.Printf( "buildUpdateRequest(%s) took: %s user CPU time\n", sub.hostname, sub.lastComputeUpdateCpuDuration) return false } return true }
func RunSysStats(startedAt time.Time, interval time.Duration) { const nsInMs uint64 = 1000 * 1000 ticker := time.NewTicker(interval) defer func() { ticker.Stop() }() var ( ms = new(runtime.MemStats) rusage = &syscall.Rusage{} lastUserTime int64 lastSysTime int64 userTime int64 sysTime int64 userCpuUtil float64 sysCpuUtil float64 ) for _ = range ticker.C { runtime.ReadMemStats(ms) syscall.Getrusage(syscall.RUSAGE_SELF, rusage) syscall.Getrusage(syscall.RUSAGE_SELF, rusage) userTime = rusage.Utime.Sec*1000000000 + int64(rusage.Utime.Usec) sysTime = rusage.Stime.Sec*1000000000 + int64(rusage.Stime.Usec) userCpuUtil = float64(userTime-lastUserTime) * 100 / float64(interval) sysCpuUtil = float64(sysTime-lastSysTime) * 100 / float64(interval) lastUserTime = userTime lastSysTime = sysTime log.Info("ver:%s, since:%s, go:%d, gc:%dms/%d=%d, heap:{%s, %s, %s, %s %s} cpu:{%3.2f%%us, %3.2f%%sy}", BuildId, time.Since(startedAt), runtime.NumGoroutine(), ms.PauseTotalNs/nsInMs, ms.NumGC, ms.PauseTotalNs/(nsInMs*uint64(ms.NumGC))+1, gofmt.ByteSize(ms.HeapSys), // bytes it has asked the operating system for gofmt.ByteSize(ms.HeapAlloc), // bytes currently allocated in the heap gofmt.ByteSize(ms.HeapIdle), // bytes in the heap that are unused gofmt.ByteSize(ms.HeapReleased), // bytes returned to the operating system, 5m for scavenger gofmt.Comma(int64(ms.HeapObjects)), userCpuUtil, sysCpuUtil) } }
func getrusage(who int, rusage *Rusage) error { switch who { case RUSAGE_CHILDREN: who = syscall.RUSAGE_CHILDREN case RUSAGE_SELF: who = syscall.RUSAGE_SELF default: return syscall.ENOTSUP } var syscallRusage syscall.Rusage if err := syscall.Getrusage(who, &syscallRusage); err != nil { return err } rusage.Utime.Sec = int64(syscallRusage.Utime.Sec) rusage.Utime.Usec = int64(syscallRusage.Utime.Usec) rusage.Stime.Sec = int64(syscallRusage.Stime.Sec) rusage.Stime.Usec = int64(syscallRusage.Stime.Usec) rusage.Maxrss = int64(syscallRusage.Maxrss) >> 10 rusage.Ixrss = int64(syscallRusage.Ixrss) >> 10 rusage.Idrss = int64(syscallRusage.Idrss) >> 10 rusage.Minflt = int64(syscallRusage.Minflt) rusage.Majflt = int64(syscallRusage.Majflt) rusage.Nswap = int64(syscallRusage.Nswap) rusage.Inblock = int64(syscallRusage.Inblock) rusage.Oublock = int64(syscallRusage.Oublock) rusage.Msgsnd = int64(syscallRusage.Msgsnd) rusage.Msgrcv = int64(syscallRusage.Msgrcv) rusage.Nsignals = int64(syscallRusage.Nsignals) rusage.Nvcsw = int64(syscallRusage.Nvcsw) rusage.Nivcsw = int64(syscallRusage.Nivcsw) return nil }
func (s *Stats) Stats() map[string]int64 { st := make(map[string]int64) st["cmd_get"] = s.cmd_get st["cmd_set"] = s.cmd_set st["cmd_delete"] = s.cmd_delete st["get_hits"] = s.get_hits st["get_misses"] = s.get_misses st["curr_connections"] = s.curr_connections st["total_connections"] = s.total_connections st["bytes_read"] = s.bytes_read st["bytes_written"] = s.bytes_written for k, v := range s.stat { st[k] = v } t := time.Now() st["time"] = int64(t.Second()) st["uptime"] = int64(t.Sub(s.start).Seconds()) st["pid"] = int64(os.Getpid()) st["threads"] = int64(runtime.NumGoroutine()) rusage := syscall.Rusage{} syscall.Getrusage(0, &rusage) st["rusage_user"] = int64(rusage.Utime.Sec) st["rusage_system"] = int64(rusage.Stime.Sec) var memstat runtime.MemStats runtime.ReadMemStats(&memstat) st["rusage_maxrss"] = int64(memstat.HeapSys/1024) + cmem.Alloced()/1024 return st }
func CommonGmetrics(gmetric MetricSender) { gmetric("goroutines", fmt.Sprintf("%d", runtime.NumGoroutine()), Uint, "num", false) var mem runtime.MemStats runtime.ReadMemStats(&mem) gmetric("mem_alloc", fmt.Sprintf("%d", mem.Alloc), Uint, "bytes", false) gmetric("mem_sys", fmt.Sprintf("%d", mem.Sys), Uint, "bytes", false) gmetric("mem_gc_pause_last", fmt.Sprintf("%.6f", float64(mem.PauseNs[(mem.NumGC+255)%256])/1e6), Float, "ms", false) var gcPauseMax uint64 for _, v := range mem.PauseNs { if v > gcPauseMax { gcPauseMax = v } } gmetric("mem_gc_pause_max", fmt.Sprintf("%.6f", float64(gcPauseMax)/1e6), Float, "ms", false) gmetric("mem_gc_pause_total", fmt.Sprintf("%.6f", float64(mem.PauseTotalNs)/1e6), Float, "ms", true) since := time.Now().Sub(time.Unix(0, int64(mem.LastGC))).Seconds() gmetric("mem_gc_pause_since", fmt.Sprintf("%.6f", since), Float, "sec", false) var r syscall.Rusage if syscall.Getrusage(syscall.RUSAGE_SELF, &r) == nil { gmetric("rusage_utime", fmt.Sprintf("%.6f", float64(r.Utime.Nano())/1e9), Float, "cpusecs", true) gmetric("rusage_stime", fmt.Sprintf("%.6f", float64(r.Stime.Nano())/1e9), Float, "cpusecs", true) gmetric("cpu_pct", fmt.Sprintf("%.4f", 100*float64((r.Utime.Nano()+r.Stime.Nano()))/1e9), Float, "%", true) } }
func GetSystemStats() SystemStats { stats := SystemStats{} stats.NumGoRoutines = runtime.NumGoroutine() var r syscall.Rusage if syscall.Getrusage(syscall.RUSAGE_SELF, &r) == nil { stats.UserTime = float64(r.Utime.Nano()) / 1e9 stats.SystemTime = float64(r.Stime.Nano()) / 1e9 } var mem runtime.MemStats runtime.ReadMemStats(&mem) stats.BytesAlloc = mem.Alloc stats.BytesFromSystem = mem.Sys stats.GCPauseTimeLast = float64(mem.PauseNs[(mem.NumGC+255)%256]) / 1e6 var gcPauseMax uint64 for _, v := range mem.PauseNs { if v > gcPauseMax { gcPauseMax = v } } stats.GCPauseTimeMax = float64(gcPauseMax) / 1e6 stats.GCPauseTimeTotal = float64(mem.PauseTotalNs) / 1e6 stats.GCPauseSince = time.Now().Sub(time.Unix(0, int64(mem.LastGC))).Seconds() return stats }
func savePerfStats(file string) { fd, err := os.Create(file) if err != nil { panic(err) } var prevUsage int64 var prevTime int64 var rusage syscall.Rusage var memstats runtime.MemStats t0 := time.Now() for t := range time.NewTicker(250 * time.Millisecond).C { syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) curTime := time.Now().UnixNano() timeDiff := curTime - prevTime curUsage := rusage.Utime.Nano() + rusage.Stime.Nano() usageDiff := curUsage - prevUsage cpuUsagePercent := 100 * float64(usageDiff) / float64(timeDiff) prevTime = curTime prevUsage = curUsage runtime.ReadMemStats(&memstats) startms := int(t.Sub(t0).Seconds() * 1000) fmt.Fprintf(fd, "%d\t%f\t%d\t%d\n", startms, cpuUsagePercent, memstats.Alloc, memstats.Sys-memstats.HeapReleased) } }
func registerPlatformEnvironment(group *MonitorGroup) { group.Chain("rusage", MonitorFunc( func(cb func(name string, val float64)) { var rusage syscall.Rusage err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) if err != nil { logger.Errorf("failed getting rusage data: %s", err) return } MonitorStruct(&rusage, cb) })) group.Chain("proc.stat", MonitorFunc( func(cb func(name string, val float64)) { var stat procSelfStat err := readProcSelfStat(&stat) if err != nil { logger.Errorf("failed getting /proc/self/stat data: %s", err) return } MonitorStruct(&stat, cb) })) group.Chain("proc.statm", MonitorFunc( func(cb func(name string, val float64)) { var stat procSelfStatm err := readProcSelfStatm(&stat) if err != nil { logger.Errorf("failed getting /proc/self/statm data: %s", err) return } MonitorStruct(&stat, cb) })) }
func clock(l *State) int { var rusage syscall.Rusage _ = syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) // ignore errors l.PushNumber(float64(rusage.Utime.Sec+rusage.Stime.Sec) + float64(rusage.Utime.Usec+rusage.Stime.Usec)/1000000.0) return 1 }
func (server *server) statistics() serverStats { stats := *server.stats stats.Uptime = time.Since(server.startedAt).Seconds() stats.CurrentTubes = len(server.tubes) stats.TotalJobs = len(server.jobs) for _, tube := range server.tubes { stats.CurrentJobsBuried += tube.buried.Len() stats.CurrentJobsDelayed += tube.delayed.Len() stats.CurrentJobsReady += tube.ready.Len() stats.CurrentJobsReserved += tube.reserved.Len() } var duration time.Duration usage := new(syscall.Rusage) err := syscall.Getrusage(syscall.RUSAGE_SELF, usage) if err == nil { s, ns := usage.Utime.Unix() duration, err = time.ParseDuration(fmt.Sprintf("%d.%ds", s, ns)) stats.RusageStime = duration.Seconds() s, ns = usage.Stime.Unix() duration, err = time.ParseDuration(fmt.Sprintf("%d.%ds", s, ns)) stats.RusageUtime = duration.Seconds() } else { pf("failed to get rusage : %v", err) } return stats }
func writeHeader(writer io.Writer) { fmt.Fprintf(writer, "Start time: %s<br>\n", startTime.Format(timeFormat)) uptime := time.Since(startTime) + time.Millisecond*50 uptime = (uptime / time.Millisecond / 100) * time.Millisecond * 100 fmt.Fprintf(writer, "Uptime: %s<br>\n", uptime) var rusage syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) userCpuTime := time.Duration(rusage.Utime.Sec)*time.Second + time.Duration(rusage.Utime.Usec)*time.Microsecond sysCpuTime := time.Duration(rusage.Stime.Sec)*time.Second + time.Duration(rusage.Stime.Usec)*time.Microsecond cpuTime := rusage.Utime.Sec + rusage.Stime.Sec fmt.Fprintf(writer, "CPU Time: %.1f%% (User: %s Sys: %s)<br>\n", float64(cpuTime*100)/float64(uptime.Seconds()), userCpuTime, sysCpuTime) var memStatsBeforeGC, memStatsAfterGC runtime.MemStats runtime.ReadMemStats(&memStatsBeforeGC) runtime.GC() runtime.ReadMemStats(&memStatsAfterGC) fmt.Fprintf(writer, "Allocated memory: %s (%s after GC)<br>\n", format.FormatBytes(memStatsBeforeGC.Alloc), format.FormatBytes(memStatsAfterGC.Alloc)) fmt.Fprintf(writer, "System memory: %s (%s after GC)<br>\n", format.FormatBytes(memStatsBeforeGC.Sys), format.FormatBytes(memStatsAfterGC.Sys)) fmt.Fprintln(writer, "Raw <a href=\"metrics\">metrics</a>") }
func showSystemStat(interval time.Duration, count int) { usage1 := &syscall.Rusage{} var lastUtime int64 var lastStime int64 counter := 0 for { //http://man7.org/linux/man-pages/man3/vtimes.3.html syscall.Getrusage(syscall.RUSAGE_SELF, usage1) utime := usage1.Utime.Nano() stime := usage1.Stime.Nano() userCPUUtil := float64(utime-lastUtime) * 100 / float64(interval) sysCPUUtil := float64(stime-lastStime) * 100 / float64(interval) memUtil := usage1.Maxrss * 1024 lastUtime = utime lastStime = stime if counter > 0 { fmt.Printf("cpu: %3.2f%% us %3.2f%% sy, mem:%s \n", userCPUUtil, sysCPUUtil, toH(uint64(memUtil))) } counter += 1 if count >= 1 && count < counter { return } time.Sleep(interval) } }
func loadImageDataBase(baseDir string, objSrv objectserver.FullObjectServer, cleanupUnreferencedObjects bool, logger *log.Logger) (*ImageDataBase, error) { fi, err := os.Stat(baseDir) if err != nil { return nil, errors.New( fmt.Sprintf("Cannot stat: %s: %s\n", baseDir, err)) } if !fi.IsDir() { return nil, errors.New(fmt.Sprintf("%s is not a directory\n", baseDir)) } imdb := &ImageDataBase{ baseDir: baseDir, directoryMap: make(map[string]image.DirectoryMetadata), imageMap: make(map[string]*image.Image), addNotifiers: make(notifiers), deleteNotifiers: make(notifiers), mkdirNotifiers: make(makeDirectoryNotifiers), objectServer: objSrv, cleanupUnreferencedObjects: cleanupUnreferencedObjects, logger: logger, } state := concurrent.NewState(0) startTime := time.Now() var rusageStart, rusageStop syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart) if err := imdb.scanDirectory(".", state, logger); err != nil { return nil, err } if err := state.Reap(); err != nil { return nil, err } if logger != nil { plural := "" if imdb.CountImages() != 1 { plural = "s" } syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop) userTime := time.Duration(rusageStop.Utime.Sec)*time.Second + time.Duration(rusageStop.Utime.Usec)*time.Microsecond - time.Duration(rusageStart.Utime.Sec)*time.Second - time.Duration(rusageStart.Utime.Usec)*time.Microsecond logger.Printf("Loaded %d image%s in %s (%s user CPUtime)\n", imdb.CountImages(), plural, time.Since(startTime), userTime) } return imdb, nil }
func (this *Engine) handleHttpQuery(w http.ResponseWriter, req *http.Request, params map[string]interface{}) (interface{}, error) { var ( vars = mux.Vars(req) cmd = vars["cmd"] rpcServer = this.rpcServer.(*TFunServer) output = make(map[string]interface{}) ) switch cmd { case "ping": output["status"] = "ok" case "debug": stack := make([]byte, 1<<20) stackSize := runtime.Stack(stack, true) output["result"] = "go to global logger to see result" log.Info(string(stack[:stackSize])) case "stop": this.rpcServer.Stop() output["status"] = "stopped" case "stat", "stats": rusage := syscall.Rusage{} syscall.Getrusage(0, &rusage) output["rusage"] = rusage output["started"] = this.StartedAt output["elapsed"] = time.Since(this.StartedAt).String() output["pid"] = this.pid output["hostname"] = this.hostname output["ver"] = server.Version output["build_id"] = server.BuildId output["rpc"] = rpcServer.Runtime() case "runtime": output["runtime"] = rpcServer.stats.Runtime() case "conf": output["engine"] = *config.Engine output["rpc"] = *config.Engine.Rpc output["servants"] = *config.Engine.Servants case "guide", "help", "h": output["uris"] = []string{ "/engine/ping", "/engine/debug", "/engine/stop", "/engine/stat", "/engine/runtime", "/engine/conf", } default: return nil, server.ErrHttp404 } return output, nil }
func main() { // Test that NaNs in maps don't go quadratic. t := func(n int) time.Duration { var u0 syscall.Rusage if err := syscall.Getrusage(0, &u0); err != nil { panic(err) } m := map[float64]int{} nan := math.NaN() for i := 0; i < n; i++ { m[nan] = 1 } if len(m) != n { panic("wrong size map after nan insertion") } var u1 syscall.Rusage if err := syscall.Getrusage(0, &u1); err != nil { panic(err) } return time.Duration(u1.Utime.Nano() - u0.Utime.Nano()) } // Depending on the machine and OS, this test might be too fast // to measure with accurate enough granularity. On failure, // make it run longer, hoping that the timing granularity // is eventually sufficient. n := 30000 // ~8ms user time on a Mid 2011 MacBook Air (1.8 GHz Core i7) fails := 0 for { t1 := t(n) t2 := t(2 * n) // should be 2x (linear); allow up to 3x if t2 < 3*t1 { return } fails++ if fails == 6 { panic(fmt.Sprintf("too slow: %d inserts: %v; %d inserts: %v\n", n, t1, 2*n, t2)) } if fails < 4 { n *= 2 } } }
// 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 getRSS() uint64 { rusage := &syscall.Rusage{} ret := syscall.Getrusage(0, rusage) if ret == nil && rusage.Maxrss > 0 { return uint64(rusage.Maxrss) } return 0 }
// Returns the sytem and the user time so far. func getRTime() (tSys, tUsr float64) { rusage := &syscall.Rusage{} if err := syscall.Getrusage(syscall.RUSAGE_SELF, rusage); err != nil { log.Error("Couldn't get rusage time:", err) } s, u := rusage.Stime, rusage.Utime return iiToF(int64(s.Sec), int64(s.Usec)), iiToF(int64(u.Sec), int64(u.Usec)) }
func update_rusage() { rusage_mutex.Lock() defer rusage_mutex.Unlock() err := syscall.Getrusage(syscall.RUSAGE_SELF, rusage) if err != nil { panic(err) } }
// SampleEnvironment queries the runtime system for various interesting metrics, // storing the resulting values in the set of metric gauges maintained by // RuntimeStatSampler. This makes runtime statistics more convenient for // consumption by the time series and status systems. // // This method should be called periodically by a higher level system in order // to keep runtime statistics current. func (rsr *RuntimeStatSampler) SampleEnvironment() { // Record memory and call stats from the runtime package. // TODO(mrtracy): memory statistics will not include usage from RocksDB. // Determine an appropriate way to compute total memory usage. numCgoCall := runtime.NumCgoCall() numGoroutine := runtime.NumGoroutine() // It might be useful to call ReadMemStats() more often, but it stops the // world while collecting stats so shouldn't be called too often. ms := runtime.MemStats{} runtime.ReadMemStats(&ms) // Record CPU statistics using syscall package. ru := syscall.Rusage{} if err := syscall.Getrusage(syscall.RUSAGE_SELF, &ru); err != nil { log.Errorf("Getrusage failed: %v", err) } // Time statistics can be compared to the total elapsed time to create a // useful percentage of total CPU usage, which would be somewhat less accurate // if calculated later using downsampled time series data. now := rsr.clock.PhysicalNow() dur := float64(now - rsr.lastNow) newUtime := ru.Utime.Nano() newStime := ru.Stime.Nano() uPerc := float64(newUtime-rsr.lastUtime) / dur sPerc := float64(newStime-rsr.lastStime) / dur pausePerc := float64(ms.PauseTotalNs-rsr.lastPauseTime) / dur rsr.lastNow = now rsr.lastUtime = newUtime rsr.lastStime = newStime rsr.lastPauseTime = ms.PauseTotalNs // Log summary of statistics to console, if requested. activeMiB := float64(ms.Alloc) / (1 << 20) cgoRate := float64((numCgoCall-rsr.lastCgoCall)*int64(time.Second)) / dur log.Infof("runtime stats: %d goroutines, %.2fMiB active, %.2fcgo/sec, %.2f/%.2f %%(u/s)time, %.2f %%gc (%dx)", numGoroutine, activeMiB, cgoRate, uPerc, sPerc, pausePerc, ms.NumGC-rsr.lastNumGC) if log.V(2) { log.Infof("memstats: %+v", ms) } if logOSStats != nil { logOSStats() } rsr.lastCgoCall = numCgoCall rsr.lastNumGC = ms.NumGC rsr.cgoCalls.Update(numCgoCall) rsr.goroutines.Update(int64(numGoroutine)) rsr.allocBytes.Update(int64(ms.Alloc)) rsr.gcCount.Update(int64(ms.NumGC)) rsr.gcPauseNS.Update(int64(ms.PauseTotalNs)) rsr.gcPausePercent.Update(pausePerc) rsr.cpuUserNS.Update(newUtime) rsr.cpuUserPercent.Update(uPerc) rsr.cpuSysNS.Update(newStime) rsr.cpuSysPercent.Update(sPerc) }
// Rusage returns a StatSource that provides as many statistics as possible // gathered from the Rusage syscall. Not expected to be called directly, as // this StatSource is added by Register. func Rusage() monkit.StatSource { return monkit.StatSourceFunc(func(cb func(name string, val float64)) { var rusage syscall.Rusage err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) if err == nil { monkit.StatSourceFromStruct(&rusage).Stats(cb) } }) }
func getRusage() *syscall.Rusage { var r syscall.Rusage err := syscall.Getrusage(0, &r) if err != nil { log.Errorf("[Server] error getting rusage %v", err) return nil } return &r }
func (ctx *FsRateContext) SetSpeedPercent(percent uint) { if percent > 100 { percent = 100 } ctx.speedPercent = uint64(percent) var rusage syscall.Rusage syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) ctx.blocksSinceLastPause = uint64(rusage.Inblock) ctx.timeOfLastPause = time.Now() }
// ProcessTimes returns CPU usage time of the process func ProcessTimes() (user, system float64, size uint64) { var usage syscall.Rusage if err := syscall.Getrusage(syscall.RUSAGE_SELF, &usage); err != nil { fmt.Printf("Error: unable to gather resource usage data: %v\n", err) } user = float64(usage.Utime.Sec) + float64(usage.Utime.Usec)/1e6 system = float64(usage.Stime.Sec) + float64(usage.Stime.Usec)/1e6 size = uint64(uint32(usage.Maxrss)) return }
func memUnix() int64 { var ru syscall.Rusage syscall.Getrusage(0, &ru) if runtime.GOOS == "linux" { // in KB return int64(ru.Maxrss) << 10 } // In bytes: return int64(ru.Maxrss) }
// GetTimeSeriesData returns a slice of TimeSeriesData updates based on current // runtime statistics. // // Calling this method will query various system packages for runtime statistics // and convert the information to time series data. This is currently done in // one method because it is convenient; however, in the future querying and // recording can be easily separated, similar to the way that NodeStatus is // separated into a monitor and a recorder. func (rsr *RuntimeStatRecorder) GetTimeSeriesData() []ts.TimeSeriesData { data := make([]ts.TimeSeriesData, 0, rsr.lastDataCount) // Record memory and call stats from the runtime package. // TODO(mrtracy): memory statistics will not include usage from RocksDB. // Determine an appropriate way to compute total memory usage. numCgoCall := runtime.NumCgoCall() numGoroutine := runtime.NumGoroutine() ms := runtime.MemStats{} runtime.ReadMemStats(&ms) // Record CPU statistics using syscall package. ru := syscall.Rusage{} if err := syscall.Getrusage(syscall.RUSAGE_SELF, &ru); err != nil { log.Errorf("Getrusage failed: %v", err) } // Time statistics can be compared to the total elapsed time to create a // useful percentage of total CPU usage, which would be somewhat less accurate // if calculated later using downsampled time series data. now := rsr.clock.PhysicalNow() dur := float64(now - rsr.lastNow) newUtime := ru.Utime.Nano() newStime := ru.Stime.Nano() uPerc := float64(newUtime-rsr.lastUtime) / dur sPerc := float64(newStime-rsr.lastStime) / dur pausePerc := float64(ms.PauseTotalNs-rsr.lastPauseTime) / dur rsr.lastNow = now rsr.lastUtime = newUtime rsr.lastStime = newStime rsr.lastPauseTime = ms.PauseTotalNs // Log summary of statistics to console, if requested. if log.V(1) { activeMiB := float64(ms.Alloc) / (1 << 20) cgoRate := float64((numCgoCall-rsr.lastCgoCall)*int64(time.Second)) / dur log.Infof("runtime stats: %d goroutines, %.2fMiB active, %.2fcgo/sec, %.2f/%.2f %%(u/s)time, %.2f %%gc (%dx)", numGoroutine, activeMiB, cgoRate, uPerc, sPerc, pausePerc, ms.NumGC-rsr.lastNumGC) rsr.lastCgoCall = numCgoCall rsr.lastNumGC = ms.NumGC } data = append(data, rsr.record(now, "cgocalls", float64(numCgoCall))) data = append(data, rsr.record(now, "goroutines", float64(numGoroutine))) data = append(data, rsr.record(now, "allocbytes", float64(ms.Alloc))) data = append(data, rsr.record(now, "gc.count", float64(ms.NumGC))) data = append(data, rsr.record(now, "gc.pause.ns", float64(ms.PauseTotalNs))) data = append(data, rsr.record(now, "gc.pause.percent", pausePerc)) data = append(data, rsr.record(now, "cpu.user.ns", float64(newUtime))) data = append(data, rsr.record(now, "cpu.user.percent", uPerc)) data = append(data, rsr.record(now, "cpu.sys.ns", float64(newStime))) data = append(data, rsr.record(now, "cpu.sys.percent", sPerc)) rsr.lastDataCount = len(data) return data }
// Compute the maximum read speed of a block device, given a file within a // file-system mounted on the block device. // If I/O accounting is enabled, blocksPerSecond will be non-zero. func GetReadSpeed(name string) (bytesPerSecond, blocksPerSecond uint64, err error) { bytesPerSecond = 0 blocksPerSecond = 0 var devpath string devpath, err = getDevnodeForFile(name) if err != nil { return } var file *os.File file, err = openDirect(devpath, os.O_RDONLY, 0) if err != nil { return } var tread uint = 0 buffer := make([]byte, BUFLEN) var rusage_start, rusage_stop syscall.Rusage err = syscall.Getrusage(syscall.RUSAGE_SELF, &rusage_start) if err != nil { return } time_start := time.Now() for tread < MAX_TO_READ { var nread int nread, err = file.Read(buffer) if err != nil { return } tread += uint(nread) } elapsed := time.Since(time_start) bytesPerSecond = uint64(float64(tread) / elapsed.Seconds()) err = syscall.Getrusage(syscall.RUSAGE_SELF, &rusage_stop) if err != nil { return } if rusage_stop.Inblock > rusage_start.Inblock { blocksPerSecond = uint64(float64(rusage_stop.Inblock- rusage_start.Inblock) / elapsed.Seconds()) } return }