2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 21:43:32 +02:00
|
|
|
//
|
2015-03-07 21:36:35 +01:00
|
|
|
// 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,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 22:50:14 +02:00
|
|
|
|
2014-03-02 23:58:14 +01:00
|
|
|
//+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
|
2016-08-15 08:37:32 +02:00
|
|
|
for range time.NewTicker(time.Second).C {
|
2014-03-02 23:58:14 +01:00
|
|
|
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()
|
2014-04-14 12:02:40 +02:00
|
|
|
copy(cpuUsagePercent[1:], cpuUsagePercent[0:])
|
|
|
|
cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff)
|
2014-03-02 23:58:14 +01:00
|
|
|
cpuUsageLock.Unlock()
|
|
|
|
prevTime = curTime
|
|
|
|
prevUsage = curUsage
|
|
|
|
}
|
|
|
|
}
|