-
Notifications
You must be signed in to change notification settings - Fork 8
/
Top.go
199 lines (183 loc) · 5.21 KB
/
Top.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import "io/ioutil"
import "strconv"
import "regexp"
import "errors"
import "time"
import "os"
import "log"
//process info sructure
type ProcessItem struct {
Pid int
Name string
User string
Cpu float32
Memory int
}
type Timeval struct {
Sec int32
Usec int32
}
type Top struct {
processItems []ProcessItem
collectInfoJob BatchJob
lastRequestTime time.Time
}
func (top *Top) StartCollectInfo() error {
top.collectInfoJob.Job = top.collectInfo
top.lastRequestTime = time.Now()
err := top.collectInfoJob.Start()
if err != nil {
stErr := "error: Can't start collect info job"
log.Println(stErr)
return errors.New(stErr)
}
return nil
}
func (top *Top) StopCollectInfo() error {
err := top.collectInfoJob.Stop()
if err != nil {
stErr := "error: Can't stop collect info job"
log.Println(stErr)
return errors.New(stErr)
}
return nil
}
func (top *Top) getTicksbyPid(pid int) (int64, error) {
statFileData, err := ioutil.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat")
if err != nil {
stErr := "info: problem with read stat file with pid " + strconv.Itoa(pid)
log.Println(stErr)
return 0, errors.New(stErr)
}
statFileStr := string(statFileData)
cpuTimeReg := regexp.MustCompile("\\d+")
cpuStatField := cpuTimeReg.FindAllString(statFileStr, -1)
utime, _ := strconv.Atoi(cpuStatField[11])
stime, _ := strconv.Atoi(cpuStatField[12])
cutime, _ := strconv.Atoi(cpuStatField[13])
cstime, _ := strconv.Atoi(cpuStatField[14])
sumTime := int64(utime + stime + cutime + cstime)
return sumTime, nil
}
func (top *Top) getTicksProcessor() (int64, error) {
statFileData, err := ioutil.ReadFile("/proc/stat")
if err != nil {
stErr := "error: problem with read proc filesystem"
log.Println(stErr)
return 0, errors.New(stErr)
}
statFileStr := string(statFileData)
cpuTimeReg := regexp.MustCompile("\\d+")
cpuStatField := cpuTimeReg.FindAllString(statFileStr, -1)[:9]
var sumTime int64
for _, cpuField := range cpuStatField {
tim, _ := strconv.Atoi(cpuField)
sumTime += int64(tim)
}
return sumTime, nil
}
func (top *Top) getTicksMap(pids []int) map[int]int64 {
ticksMap := make(map[int]int64)
for _, element := range pids {
ticks, err := top.getTicksbyPid(element)
if err != nil {
ticksMap[element] = 0
}
ticksMap[element] = ticks
}
return ticksMap
}
func (top *Top) collectInfo() {
for {
pids, err := top.getAllPids()
if err != nil {
stErr := "error: problem with read proc filesystem"
log.Println(stErr)
return //errors.New("error: problem with read proc filesystem")
}
StartTicks := top.getTicksMap(pids)
sumOldTick, _ := top.getTicksProcessor()
time.Sleep(1000 * time.Millisecond)
pids, _ = top.getAllPids()
EndTicks := top.getTicksMap(pids)
sumNewTick, _ := top.getTicksProcessor()
top.processItems = top.fillProcessInfo(StartTicks, EndTicks, sumNewTick-sumOldTick)
secondLastRequest := time.Now().Sub(top.lastRequestTime)
if secondLastRequest.Seconds() > 5 {
log.Println("info: sleep collect top info job")
top.collectInfoJob.Stop()
}
}
return
}
//
func (top *Top) getAllPids() ([]int, error) {
dirContent, err := ioutil.ReadDir("/proc/")
if err != nil {
return nil, err
}
var process []int
for _, element := range dirContent {
validPID := regexp.MustCompile(`^[0-9]+$`)
if element.IsDir() && validPID.MatchString(element.Name()) {
pid, _ := strconv.Atoi(element.Name())
process = append(process, pid)
}
}
return process, nil
}
func (top *Top) fillProcessInfo(oldTicks map[int]int64, newTicks map[int]int64, sumTicks int64) []ProcessItem {
processItems := []ProcessItem{}
for i, newTickVal := range newTicks {
statFileData, err := ioutil.ReadFile("/proc/" + strconv.Itoa(i) + "/status")
if err == nil {
processItem := ProcessItem{}
processItem.Pid = i
statFileStr := string(statFileData)
regName := regexp.MustCompile("Name:\t(.*)\n")
processItem.Name = regName.FindAllStringSubmatch(statFileStr, -1)[0][1]
regUid := regexp.MustCompile("Uid:\t(\\w+)")
processItem.User = regUid.FindAllStringSubmatch(statFileStr, -1)[0][1]
regMem := regexp.MustCompile("VmRSS:\\s+(\\d+)")
if regMem.MatchString(statFileStr) {
intMem, _ := strconv.Atoi(regMem.FindAllStringSubmatch(statFileStr, -1)[0][1])
processItem.Memory = intMem
}
oldTickVal, exist := oldTicks[i]
if exist {
processItem.Cpu = float32(float64(newTickVal-oldTickVal) / float64(sumTicks))
} else {
stErr := "info: can't messure cpu info for process"
log.Println(stErr, processItem.Name, processItem.Pid)
processItem.Cpu = 0
}
processItems = append(processItems, processItem)
}
}
return processItems
}
//
func (top *Top) GetProcessList() ([]ProcessItem, error) {
if !top.collectInfoJob.IsRunning() {
log.Println("info: start collect top info job")
top.collectInfoJob.Start()
}
top.lastRequestTime = time.Now()
return top.processItems, nil
}
func (top *Top) KillProcess(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
stErr := "info: can't find process with pid " + strconv.Itoa(pid)
log.Println(stErr)
return errors.New(stErr)
}
err = process.Kill()
if err != nil {
stErr := "info: can't kill process with pid " + strconv.Itoa(pid)
log.Println(stErr)
return errors.New(stErr)
}
return nil
}