summaryrefslogtreecommitdiff
path: root/gstat/main.go
blob: 312e066924548e2b3ddb5f928f1dc6c592e833ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main

import (
	"fmt"
	"github.com/buetow/gstat/diskstats"
	"github.com/buetow/gstat/process"
	"time"
)

type twoP struct {
	first  process.Process
	second process.Process
}
type processMap map[string]twoP

func timedGather(timerChan <-chan bool, dRxChan chan<- diskstats.Diskstats, pRxChan chan<- process.Process) {
	for {
		switch <-timerChan {
		case false:
			{
				break
			}
		case true:
			{
				go diskstats.Gather(dRxChan)
				go process.Gather(pRxChan)
			}
		}
	}
	close(dRxChan)
	close(pRxChan)
}

func receiveD(dRxChan <-chan diskstats.Diskstats) {
	for d := range dRxChan {
		//diskstats.Print()
		// Implemented later
		_ = d
	}
}

func compareP(lastP processMap) {
	for id, val := range lastP {
		first := val.first.Count["syscr"]
		second := val.second.Count["syscr"]
		diff := first - second
		if diff < 0 {
			diff = -diff
		}
		fmt.Printf("%d %s\n", diff, id)
	}
}

func receiveP(pRxChan <-chan process.Process) {
	lastP := make(processMap)
	flag := false

	for p := range pRxChan {
		if p.Last {
			if flag {
				compareP(lastP)
			}
			flag = !flag
		} else {
			if val, ok := lastP[p.Id]; ok {
				if flag {
					lastP[p.Id] = twoP{first: val.first, second: p}
				} else {
					lastP[p.Id] = twoP{first: p, second: val.second}
				}
			} else {
				lastP[p.Id] = twoP{first: p}
			}
		}
	}
}

func main() {
	timerChan := make(chan bool)
	dRxChan := make(chan diskstats.Diskstats)
	pRxChan := make(chan process.Process)

	go timedGather(timerChan, dRxChan, pRxChan)
	go receiveD(dRxChan)
	go receiveP(pRxChan)

	for counter := 0; counter < 5; counter++ {
		timerChan <- true
		time.Sleep(time.Second * 2)
	}
	timerChan <- false

	fmt.Println("Good bye")
}