-
Notifications
You must be signed in to change notification settings - Fork 159
/
testsuite_c_wrappers.go
126 lines (106 loc) · 2.68 KB
/
testsuite_c_wrappers.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
package earlyoom_testsuite
import (
"fmt"
"strings"
)
// #cgo CFLAGS: -std=gnu99 -DCGO
// #include "meminfo.h"
// #include "kill.h"
// #include "msg.h"
// #include "globals.h"
// #include "proc_pid.h"
import "C"
func init() {
C.enable_debug = 1
}
func enable_debug(state bool) (oldState bool) {
if C.enable_debug == 1 {
oldState = true
}
if state {
C.enable_debug = 1
} else {
C.enable_debug = 0
}
return
}
func parse_term_kill_tuple(optarg string, upper_limit int) (error, float64, float64) {
cs := C.CString(optarg)
tuple := C.parse_term_kill_tuple(cs, C.longlong(upper_limit))
errmsg := C.GoString(&(tuple.err[0]))
if len(errmsg) > 0 {
return fmt.Errorf(errmsg), 0, 0
}
return nil, float64(tuple.term), float64(tuple.kill)
}
func is_alive(pid int) bool {
res := C.is_alive(C.int(pid))
return bool(res)
}
func fix_truncated_utf8(str string) string {
cstr := C.CString(str)
C.fix_truncated_utf8(cstr)
return C.GoString(cstr)
}
func parse_meminfo() C.meminfo_t {
return C.parse_meminfo()
}
// Wrapper so _test.go code can create a poll_loop_args_t
// struct. _test.go code cannot use C.
func poll_loop_args_t(sort_by_rss bool) (args C.poll_loop_args_t) {
args.sort_by_rss = C.bool(sort_by_rss)
return
}
func procinfo_t() C.procinfo_t {
return C.procinfo_t{}
}
func is_larger(args *C.poll_loop_args_t, victim mockProcProcess, cur mockProcProcess) bool {
cVictim := victim.toProcinfo_t()
cCur := cur.toProcinfo_t()
return bool(C.is_larger(args, &cVictim, &cCur))
}
func find_largest_process() {
var args C.poll_loop_args_t
C.find_largest_process(&args)
}
func kill_process() {
var args C.poll_loop_args_t
var victim C.procinfo_t
victim.pid = 1
C.kill_process(&args, 0, &victim)
}
func get_oom_score(pid int) int {
return int(C.get_oom_score(C.int(pid)))
}
func get_oom_score_adj(pid int, out *int) int {
var out2 C.int
res := C.get_oom_score_adj(C.int(pid), &out2)
*out = int(out2)
return int(res)
}
func get_comm(pid int) (int, string) {
cstr := C.CString(strings.Repeat("\000", 256))
res := C.get_comm(C.int(pid), cstr, 256)
return int(res), C.GoString(cstr)
}
func get_cmdline(pid int) (int, string) {
cstr := C.CString(strings.Repeat("\000", 256))
res := C.get_cmdline(C.int(pid), cstr, 256)
return int(res), C.GoString(cstr)
}
func procdir_path(str string) string {
if str != "" {
cstr := C.CString(str)
C.procdir_path = cstr
}
return C.GoString(C.procdir_path)
}
func parse_proc_pid_stat_buf(buf string) (res bool, out C.pid_stat_t) {
cbuf := C.CString(buf)
res = bool(C.parse_proc_pid_stat_buf(&out, cbuf))
return res, out
}
func parse_proc_pid_stat(pid int) (res bool, out C.pid_stat_t) {
res = bool(C.parse_proc_pid_stat(&out, C.int(pid)))
return res, out
}