make writes to shared global hashmap threadsafe
This commit is contained in:
parent
f39c7cf5e2
commit
0b9266ba83
|
@ -9,6 +9,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SysInfo will record cpu and memory data
|
// SysInfo will record cpu and memory data
|
||||||
|
@ -33,6 +34,7 @@ type fn func(int) (*SysInfo, error)
|
||||||
var fnMap map[string]fn
|
var fnMap map[string]fn
|
||||||
var platform string
|
var platform string
|
||||||
var history map[int]Stat
|
var history map[int]Stat
|
||||||
|
var historyLock sync.Mutex
|
||||||
var eol string
|
var eol string
|
||||||
|
|
||||||
func wrapper(statType string) func(pid int) (*SysInfo, error) {
|
func wrapper(statType string) func(pid int) (*SysInfo, error) {
|
||||||
|
@ -139,7 +141,9 @@ func stat(pid int, statType string) (*SysInfo, error) {
|
||||||
seconds = 1
|
seconds = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
historyLock.Lock()
|
||||||
history[pid] = *stat
|
history[pid] = *stat
|
||||||
|
historyLock.Unlock()
|
||||||
sysInfo.CPU = (total / seconds) * 100
|
sysInfo.CPU = (total / seconds) * 100
|
||||||
sysInfo.Memory = stat.rss * pageSize
|
sysInfo.Memory = stat.rss * pageSize
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue