// Copyright (C) 2014 The Syncthing Authors. // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. //+build !windows,!solaris package main import ( "syscall" "time" ) func init() { go trackCPUUsage() } func trackCPUUsage() { var prevUsage int64 var prevTime = time.Now().UnixNano() var rusage syscall.Rusage for range time.NewTicker(time.Second).C { syscall.Getrusage(syscall.RUSAGE_SELF, &rusage) curTime := time.Now().UnixNano() timeDiff := curTime - prevTime curUsage := rusage.Utime.Nano() + rusage.Stime.Nano() usageDiff := curUsage - prevUsage cpuUsageLock.Lock() copy(cpuUsagePercent[1:], cpuUsagePercent[0:]) cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff) cpuUsageLock.Unlock() prevTime = curTime prevUsage = curUsage } }