-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (121 loc) · 2.75 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
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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
)
const (
checkInterval = 2 * time.Second
bytesInMB = 1024 * 1024
)
func main() {
totalMemoryBytes, err := totalMemory()
if err != nil {
fmt.Println("get total memory: %w", err)
return
}
maxMemoryBytes := maxMemory()
fmt.Printf("total memory:\t%10.dMB\n", totalMemoryBytes/bytesInMB)
fmt.Printf("max memory:\t%10.dMB\n", maxMemoryBytes/bytesInMB)
termSigC := waitForTermination()
ticker := time.NewTicker(checkInterval)
defer ticker.Stop()
for {
select {
case <-termSigC:
fmt.Print("\nmemkill terminated")
return
case <-ticker.C:
pids, err := findProcessesOverLimit(maxMemoryBytes, totalMemoryBytes)
if err != nil {
fmt.Println("Error finding processes:", err)
continue
}
if len(pids) == 0 {
fmt.Print(".")
continue
}
fmt.Printf("\nterminating pids: %v\n", pids)
for _, pid := range pids {
err := terminateProcess(pid)
if err != nil {
fmt.Printf("Error terminating process %d: %v\n", pid, err)
}
}
}
}
}
func findProcessesOverLimit(limit, totalMemoryBytes int64) ([]int, error) {
cmd := exec.Command("ps", "-eo", "pid,%mem")
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
var pids []int
lines := string(output)
for _, line := range strings.Split(lines, "\n")[1:] {
fields := strings.Fields(line)
if len(fields) != 2 {
continue
}
pid, err := strconv.Atoi(fields[0])
if err != nil {
return nil, err
}
memoryPercentage, err := strconv.ParseFloat(fields[1], 64)
if err != nil {
return nil, err
}
if memoryPercentage < 1 {
continue
}
memoryPercentage /= 100
memoryBytes := int64(memoryPercentage * float64(totalMemoryBytes))
if memoryBytes >= limit {
pids = append(pids, pid)
}
}
return pids, nil
}
func terminateProcess(pid int) error {
return errors.Join(
syscall.Kill(pid, syscall.SIGTERM),
syscall.Kill(pid, syscall.SIGINT),
syscall.Kill(pid, syscall.SIGKILL),
)
}
func waitForTermination() <-chan struct{} {
sig := make(chan os.Signal, 1)
done := make(chan struct{})
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sig
close(done)
}()
return done
}
func totalMemory() (int64, error) {
sysInfo := new(syscall.Sysinfo_t)
if err := syscall.Sysinfo(sysInfo); err != nil {
return 0, err
}
return int64(sysInfo.Totalram) * int64(sysInfo.Unit), nil
}
func maxMemory() int64 {
if len(os.Args) != 2 {
fmt.Println("Usage: program_name <max_memory_usage_in_megabytes>")
os.Exit(1)
}
maxMemoryUsage, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
fmt.Println("Invalid max_memory_usage argument:", err)
os.Exit(1)
}
return maxMemoryUsage * bytesInMB
}