-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster-top-node.go
74 lines (55 loc) · 1.46 KB
/
cluster-top-node.go
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
package main
import (
"fmt"
"github.com/patwie/cluster-top/proc"
"github.com/pebbe/zmq4"
"github.com/vmihailenco/msgpack"
"log"
"time"
)
// hash map of all processes
var work_processes map[int]*proc.Process
var clus Cluster
func main() {
work_processes = make(map[int]*proc.Process)
// load ports and ip-address
cfg := LoadConfig()
cfg.Print()
// sending messages (PUSH-PULL)
SocketAddr := "tcp://" + cfg.RouterIp + ":" + cfg.Ports.Nodes
log.Println("Now pushing to", SocketAddr)
socket, err := zmq4.NewSocket(zmq4.PUSH)
if err != nil {
panic(err)
}
defer socket.Close()
socket.Connect(SocketAddr)
node := Node{}
InitNode(&node)
clus.Nodes = append(clus.Nodes, node)
cpu_tick_prev := int64(0)
cpu_tick_cur := int64(0)
cores := proc.NumberCPUCores()
fmt.Printf("Found %v cores\n", cores)
for {
// reset most processes
proc.MarkDirtyProcessList(work_processes)
cpu_tick_cur = proc.CpuTick()
proc.UpdateProcessList(work_processes)
clus.Nodes[0].Cpu.Update()
factor := float32(cpu_tick_cur-cpu_tick_prev) / float32(cores) / 100.
clus.Nodes[0].Processes = GetProcesses(work_processes, factor, cfg.MaxDisplay)
FetchMemory(&clus.Nodes[0].Memory)
clus.Nodes[0].Time = time.Now()
// encode data
msg, err := msgpack.Marshal(&clus.Nodes[0])
if err != nil {
log.Fatal("encode error:", err)
panic(err)
}
// send data
socket.SendBytes(msg, 0)
cpu_tick_prev = cpu_tick_cur
time.Sleep(time.Duration(cfg.Tick) * time.Second)
}
}