-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpage_reclaim.py
executable file
·182 lines (137 loc) · 4.34 KB
/
page_reclaim.py
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
#!/usr/bin/env python
#
# page_reclaim Track memory page reclaim by postgres processes,
# globally or per cgroup/namespace
#
# usage: page_reclaim [-d] [-c CONTAINER_ID] [-n NAMESPACE] [-i INTERVAL]
from __future__ import print_function
from time import sleep
import argparse
import ctypes as ct
import signal
from bcc import BPF
import utils
text = """
#include <linux/ptrace.h>
#define HASH_SIZE 2^14
struct key_t {
int pid;
u64 namespace;
STRUCT_NR_PAGES
char name[TASK_COMM_LEN];
};
BPF_PERF_OUTPUT(events);
BPF_HASH(reclaim, struct key_t, long);
static inline __attribute__((always_inline)) void get_key(struct key_t* key) {
key->pid = bpf_get_current_pid_tgid();
bpf_get_current_comm(&(key->name), sizeof(key->name));
}
int probe_try_to_free_mem_cgroup_pages(struct pt_regs *ctx)
{
struct key_t key = {};
get_key(&key);
SAVE_NAMESPACE
CHECK_NAMESPACE
STORE_NR_PAGES
SUBMIT_EVENT
unsigned long zero = 0, *val;
val = reclaim.lookup_or_init(&key, &zero);
(*val) += (unsigned long) PT_REGS_RC(ctx);
return 0;
}
"""
PAGE_SIZE = 4 * 1024
def pre_process(bpf_text, args):
bpf_text = utils.replace_namespace(bpf_text, args)
if args.debug:
bpf_text = bpf_text.replace(
"STRUCT_NR_PAGES",
"unsigned long nr_pages;"
)
bpf_text = bpf_text.replace(
"STORE_NR_PAGES",
"key.nr_pages = (unsigned long) PT_REGS_RC(ctx);"
)
bpf_text = bpf_text.replace(
"SUBMIT_EVENT",
"events.perf_submit(ctx, &key, sizeof(key));"
)
else:
bpf_text = bpf_text.replace("STRUCT_NR_PAGES", "")
bpf_text = bpf_text.replace("STORE_NR_PAGES", "")
bpf_text = bpf_text.replace("SUBMIT_EVENT", "")
return bpf_text
def attach(bpf, args):
bpf.attach_kretprobe(
event="try_to_free_mem_cgroup_pages",
fn_name="probe_try_to_free_mem_cgroup_pages"
)
# signal handler
def signal_ignore(sig, frame):
print()
class Data(ct.Structure):
_fields_ = [("pid", ct.c_int),
("namespace", ct.c_ulonglong),
("nr_pages", ct.c_ulong),
("name", ct.c_char * 16)]
def output(bpf, fmt="plain"):
if fmt == "plain":
print()
for (k, v) in bpf.get_table('reclaim').items():
name = k.name.decode("ascii")
if not name.startswith("postgres"):
return
size = utils.size(v.value * PAGE_SIZE)
print("[{}] {}: {}".format(k.pid, k.name, size))
bpf.get_table('reclaim').clear()
def run(args):
print("Attaching...")
debug = 4 if args.debug else 0
bpf = BPF(text=pre_process(text, args), debug=debug)
attach(bpf, args)
exiting = False
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
name = event.name.decode("ascii")
if not name.startswith("postgres"):
return
print("Event: pid {} name {} namespace {} reclaimed pages {}".format(
event.pid, name, event.namespace, event.nr_pages))
if args.debug:
bpf["events"].open_perf_buffer(print_event)
print("Listening...")
while True:
try:
sleep(args.interval)
output(bpf)
if args.debug:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exiting = True
# as cleanup can take many seconds, trap Ctrl-C:
signal.signal(signal.SIGINT, signal_ignore)
if exiting:
print()
print("Detaching...")
print()
break
output(bpf)
def parse_args():
parser = argparse.ArgumentParser(
description="Track memory page reclaim by postgres processes",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"-c", "--container", type=str,
help="trace this container only")
parser.add_argument(
"-n", "--namespace", type=int,
help="trace this namespace only")
parser.add_argument(
"-i", "--interval", type=int, default=5,
help="after how many seconds output the result")
parser.add_argument(
"-d", "--debug", action='store_true', default=False,
help="debug mode")
return parser.parse_args()
if __name__ == "__main__":
run(parse_args())