-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
102 lines (85 loc) · 2.62 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
/*
#include <unistd.h>
*/
import "C"
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func PageSize() int {
return int(C.sysconf(C._SC_PAGE_SIZE))
}
type PassiveObserver interface {
SetFlags()
Initialize(t *Tracker, r Reader)
TimerEvent()
}
type ActiveObserver interface {
SetFlags()
Initialize(t *Tracker, r Reader, c chan bool)
}
func main() {
var blockSizeInMb = flag.Int("blockSize", 128, "block size for every allocation (in Mb), 0 to disable periodical allocator")
var initialBlockSizeInMb = flag.Int("initialSize", 0, "size to allocate before test start (in Mb), 0 to disable initial allocation")
var allocatePeriodInS = flag.Int("allocInterval", 1, "time delay between allocations (in seconds)")
var printPeriodInS = flag.Int("printInterval", 5, "time delay between current status updates (in seconds)")
var maximumLimitInMb = flag.Int("limit", 0, "maximum allocated memory size (in Mb), 0 to disable the limit")
// observer-specific flags are handled by observers in their own SetFlags() funcs
r := FileReader{}
t := Tracker{}
t.prepare()
var passiveObservers []PassiveObserver
passiveObservers = append(passiveObservers, &MeminfoObserver{})
passiveObservers = append(passiveObservers, &SwapObserver{})
passiveObservers = append(passiveObservers, &PsiObserver{})
for _, element := range passiveObservers {
element.SetFlags()
}
notifySink := make(chan bool)
var activeObservers []ActiveObserver
activeObservers = append(activeObservers, &CgroupsObserver{})
activeObservers = append(activeObservers, &PsiTrigObserver{})
for _, element := range activeObservers {
element.SetFlags()
}
flag.Parse()
for _, element := range passiveObservers {
element.Initialize(&t, r)
}
for _, element := range activeObservers {
element.Initialize(&t, r, notifySink)
}
if *blockSizeInMb == 0 {
log.Printf("Working in a passive mode, will not allocate memory during the test")
}
if *blockSizeInMb > 0 || *initialBlockSizeInMb > 0 {
a := Allocator{}
a.initialize(&t, *initialBlockSizeInMb)
if *blockSizeInMb > 0 {
log.Printf("Will allocate %v Mb every %v seconds", *blockSizeInMb, *allocatePeriodInS)
go a.startMemoryFilling(*blockSizeInMb, time.Duration(*allocatePeriodInS)*time.Second, *maximumLimitInMb)
}
}
t.prepareAndPrintHeader()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
ticker := time.NewTicker(time.Duration(*printPeriodInS) * time.Second)
for {
select {
case <-sig:
os.Exit(0)
case <-notifySink:
case <-ticker.C:
}
for _, element := range passiveObservers {
element.TimerEvent()
}
t.saveTime()
t.printData()
}
}