Skip to content

Commit

Permalink
Merge pull request #9 from dballard/historyLock
Browse files Browse the repository at this point in the history
make writes to shared global hashmap threadsafe
  • Loading branch information
struCoder authored Nov 22, 2018
2 parents f39c7cf + 0b9266b commit 7e73b8e
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pidusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"
)

// SysInfo will record cpu and memory data
Expand All @@ -33,6 +34,7 @@ type fn func(int) (*SysInfo, error)
var fnMap map[string]fn
var platform string
var history map[int]Stat
var historyLock sync.Mutex
var eol string

func wrapper(statType string) func(pid int) (*SysInfo, error) {
Expand Down Expand Up @@ -139,7 +141,9 @@ func stat(pid int, statType string) (*SysInfo, error) {
seconds = 1
}

historyLock.Lock()
history[pid] = *stat
historyLock.Unlock()
sysInfo.CPU = (total / seconds) * 100
sysInfo.Memory = stat.rss * pageSize
}
Expand Down

0 comments on commit 7e73b8e

Please sign in to comment.