diff options
| author | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 21:57:13 +0100 |
|---|---|---|
| committer | Paul Buetow (europa) <paul@buetow.org> | 2015-05-25 21:57:13 +0100 |
| commit | b41d48ef0817025251ce080578168fd85cab15f9 (patch) | |
| tree | 6c9f11e599d4c1f02274efe16f6bdc378ad20ae5 | |
| parent | 10074fefc76404a1be7bddcc2b9d18bc0ee44056 (diff) | |
store prev results into a process map
| -rw-r--r-- | gstat/main.go | 8 | ||||
| -rw-r--r-- | process/process.go | 16 |
2 files changed, 18 insertions, 6 deletions
diff --git a/gstat/main.go b/gstat/main.go index 254edeb..9aefa60 100644 --- a/gstat/main.go +++ b/gstat/main.go @@ -7,6 +7,10 @@ import ( "time" ) +type processMap map[string]process.Process + +var lastP processMap + func gather(timer <-chan bool, d chan<- diskstats.Diskstats, p chan<- process.Process) { for { switch <-timer { @@ -32,7 +36,9 @@ func receive1(diskstats <-chan diskstats.Diskstats) { } func receive2(processes <-chan process.Process) { + lastP = make(processMap) for process := range processes { + lastP[process.Id] = process process.Print() } } @@ -48,7 +54,7 @@ func main() { for counter := 0; counter < 3; counter++ { timer <- true - time.Sleep(time.Second) + time.Sleep(time.Second * 2) fmt.Printf("Next... %d\n", counter) } diff --git a/process/process.go b/process/process.go index 5e8366f..dade966 100644 --- a/process/process.go +++ b/process/process.go @@ -8,13 +8,16 @@ import ( "regexp" "strconv" "strings" + "time" ) type Process struct { - Pid int - Cmdline string - Count map[string]int - debug string + Id string + Timestamp int32 + Pid int + Cmdline string + Count map[string]int + debug string } func new(pidstr string) (Process, error) { @@ -23,7 +26,8 @@ func new(pidstr string) (Process, error) { return Process{}, err } - p := Process{Pid: pid} + timestamp := int32(time.Now().Unix()) + p := Process{Pid: pid, Timestamp: timestamp} var rawIo string if err = utils.Slurp(&rawIo, fmt.Sprintf("/proc/%d/io", pid)); err != nil { @@ -35,6 +39,7 @@ func new(pidstr string) (Process, error) { } err = utils.Slurp(&p.Cmdline, fmt.Sprintf("/proc/%d/cmdline", pid)) + p.Id = pidstr + " " + p.Cmdline return p, err } @@ -69,6 +74,7 @@ func (self *Process) String() string { str += fmt.Sprintf("PID: %d\n", self.Pid) str += fmt.Sprintf("Cmdline: %s\n", self.Cmdline) + str += fmt.Sprintf("Timestamp: %s\n", self.Timestamp) for key, val := range self.Count { str += fmt.Sprintf("%s=%d\n", key, val) } |
