mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-12 16:26:37 +00:00
More up to date CPU usage indicator
This commit is contained in:
parent
9d535b13cf
commit
fb162ff529
@ -155,7 +155,7 @@ func restGetNeed(m *Model, w http.ResponseWriter, params martini.Params) {
|
|||||||
json.NewEncoder(w).Encode(gfs)
|
json.NewEncoder(w).Encode(gfs)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cpuUsagePercent float64
|
var cpuUsagePercent [10]float64 // The last ten seconds
|
||||||
var cpuUsageLock sync.RWMutex
|
var cpuUsageLock sync.RWMutex
|
||||||
|
|
||||||
func restGetSystem(w http.ResponseWriter) {
|
func restGetSystem(w http.ResponseWriter) {
|
||||||
@ -168,8 +168,12 @@ func restGetSystem(w http.ResponseWriter) {
|
|||||||
res["alloc"] = m.Alloc
|
res["alloc"] = m.Alloc
|
||||||
res["sys"] = m.Sys
|
res["sys"] = m.Sys
|
||||||
cpuUsageLock.RLock()
|
cpuUsageLock.RLock()
|
||||||
res["cpuPercent"] = cpuUsagePercent
|
var cpusum float64
|
||||||
|
for _, p := range cpuUsagePercent {
|
||||||
|
cpusum += p
|
||||||
|
}
|
||||||
cpuUsageLock.RUnlock()
|
cpuUsageLock.RUnlock()
|
||||||
|
res["cpuPercent"] = cpusum / 10
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(res)
|
json.NewEncoder(w).Encode(res)
|
||||||
|
@ -72,8 +72,7 @@ func trackCPUUsage() {
|
|||||||
var prevTime = time.Now().UnixNano()
|
var prevTime = time.Now().UnixNano()
|
||||||
var rusage prusage_t
|
var rusage prusage_t
|
||||||
var pid = os.Getpid()
|
var pid = os.Getpid()
|
||||||
for {
|
for _ = range time.NewTicker(time.Second).C {
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
err := solarisPrusage(pid, &rusage)
|
err := solarisPrusage(pid, &rusage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
warnln(err)
|
warnln(err)
|
||||||
@ -84,7 +83,8 @@ func trackCPUUsage() {
|
|||||||
curUsage := rusage.Pr_utime.Nano() + rusage.Pr_stime.Nano()
|
curUsage := rusage.Pr_utime.Nano() + rusage.Pr_stime.Nano()
|
||||||
usageDiff := curUsage - prevUsage
|
usageDiff := curUsage - prevUsage
|
||||||
cpuUsageLock.Lock()
|
cpuUsageLock.Lock()
|
||||||
cpuUsagePercent = 100 * float64(usageDiff) / float64(timeDiff)
|
copy(cpuUsagePercent[1:], cpuUsagePercent[0:])
|
||||||
|
cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff)
|
||||||
cpuUsageLock.Unlock()
|
cpuUsageLock.Unlock()
|
||||||
prevTime = curTime
|
prevTime = curTime
|
||||||
prevUsage = curUsage
|
prevUsage = curUsage
|
||||||
|
@ -15,15 +15,15 @@ func trackCPUUsage() {
|
|||||||
var prevUsage int64
|
var prevUsage int64
|
||||||
var prevTime = time.Now().UnixNano()
|
var prevTime = time.Now().UnixNano()
|
||||||
var rusage syscall.Rusage
|
var rusage syscall.Rusage
|
||||||
for {
|
for _ = range time.NewTicker(time.Second).C {
|
||||||
time.Sleep(10 * time.Second)
|
|
||||||
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
|
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
|
||||||
curTime := time.Now().UnixNano()
|
curTime := time.Now().UnixNano()
|
||||||
timeDiff := curTime - prevTime
|
timeDiff := curTime - prevTime
|
||||||
curUsage := rusage.Utime.Nano() + rusage.Stime.Nano()
|
curUsage := rusage.Utime.Nano() + rusage.Stime.Nano()
|
||||||
usageDiff := curUsage - prevUsage
|
usageDiff := curUsage - prevUsage
|
||||||
cpuUsageLock.Lock()
|
cpuUsageLock.Lock()
|
||||||
cpuUsagePercent = 100 * float64(usageDiff) / float64(timeDiff)
|
copy(cpuUsagePercent[1:], cpuUsagePercent[0:])
|
||||||
|
cpuUsagePercent[0] = 100 * float64(usageDiff) / float64(timeDiff)
|
||||||
cpuUsageLock.Unlock()
|
cpuUsageLock.Unlock()
|
||||||
prevTime = curTime
|
prevTime = curTime
|
||||||
prevUsage = curUsage
|
prevUsage = curUsage
|
||||||
|
Loading…
Reference in New Issue
Block a user