-
Notifications
You must be signed in to change notification settings - Fork 12
/
comm.c
404 lines (344 loc) · 10.7 KB
/
comm.c
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/refcount.h>
#include <linux/rblist.h>
#include <linux/time64.h>
#include <monitor.h>
#include <tp_struct.h>
#define TASK_COMM_LEN 16
struct pid_comm_node {
struct rb_node rbnode;
u64 update_time;
bool flush;
int pid;
char comm[TASK_COMM_LEN];
struct rb_node exit_node;
};
struct comm_ctx {
struct prof_dev *comm_dev;
refcount_t ref;
struct rblist pid_comm;
struct rblist exited;
int task_newtask, task_rename;
int sched_process_free;
};
static struct comm_ctx *global_comm_ctx = NULL;
static struct list_head global_comm_notify_list = LIST_HEAD_INIT(global_comm_notify_list);
static int pid_comm_node_cmp(struct rb_node *rbn, const void *entry)
{
struct pid_comm_node *b = container_of(rbn, struct pid_comm_node, rbnode);
const struct pid_comm_node *e = entry;
return b->pid - e->pid;
}
static struct rb_node *pid_comm_node_new(struct rblist *rlist, const void *new_entry)
{
struct pid_comm_node *e = (void *)new_entry;
struct pid_comm_node *b = malloc(sizeof(*b));
if (b) {
b->pid = e->pid;
b->flush = false;
b->update_time = 0;
RB_CLEAR_NODE(&b->rbnode);
RB_CLEAR_NODE(&b->exit_node);
return &b->rbnode;
} else
return NULL;
}
static void pid_comm_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct pid_comm_node *b = container_of(rb_node, struct pid_comm_node, rbnode);
free(b);
}
static int pid_comm_exit_cmp(struct rb_node *rbn, const void *entry)
{
struct pid_comm_node *b = container_of(rbn, struct pid_comm_node, exit_node);
const struct pid_comm_node *e = entry;
if (b->update_time > e->update_time)
return 1;
else if (b->update_time < e->update_time)
return -1;
else
return b->pid - e->pid;
}
static struct rb_node *pid_comm_exit_new(struct rblist *rlist, const void *new_entry)
{
struct pid_comm_node *e = (void *)new_entry;
RB_CLEAR_NODE(&e->exit_node);
return &e->exit_node;
}
static void pid_comm_exit_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct pid_comm_node *b = container_of(rb_node, struct pid_comm_node, exit_node);
// notify
if (!list_empty(&global_comm_notify_list)) {
struct comm_notify *node;
list_for_each_entry(node, &global_comm_notify_list, link) {
node->notify(node, b->pid, NOTIFY_COMM_DELETE, b->update_time);
}
}
}
static void comm_deinit(struct prof_dev *dev);
static int comm_init(struct prof_dev *dev)
{
struct perf_evlist *evlist = dev->evlist;
struct comm_ctx *ctx;
struct perf_event_attr attr = {
.type = PERF_TYPE_TRACEPOINT,
.config = 0,
.size = sizeof(struct perf_event_attr),
.sample_period = 1,
.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_RAW,
.pinned = 1,
.disabled = 1,
.watermark = 1,
};
struct perf_evsel *evsel;
ctx = zalloc(sizeof(*ctx));
if (!ctx) return -1;
dev->private = ctx;
dev->type = PROF_DEV_TYPE_SERVICE;
dev->silent = true;
global_comm_ctx = ctx;
ctx->comm_dev = dev;
refcount_set(&ctx->ref, 1);
rblist__init(&ctx->pid_comm);
ctx->pid_comm.node_cmp = pid_comm_node_cmp;
ctx->pid_comm.node_new = pid_comm_node_new;
ctx->pid_comm.node_delete = pid_comm_node_delete;
rblist__init(&ctx->exited);
ctx->exited.node_cmp = pid_comm_exit_cmp;
ctx->exited.node_new = pid_comm_exit_new;
ctx->exited.node_delete = pid_comm_exit_delete;
reduce_wakeup_times(dev, &attr);
tep__ref();
ctx->task_newtask = tep__event_id("task", "task_newtask");
if (ctx->task_newtask < 0) goto failed;
attr.config = ctx->task_newtask;
evsel = perf_evsel__new(&attr);
if (!evsel) goto failed;
perf_evlist__add(evlist, evsel);
ctx->task_rename = tep__event_id("task", "task_rename");
if (ctx->task_rename < 0) goto failed;
attr.config = ctx->task_rename;
evsel = perf_evsel__new(&attr);
if (!evsel) goto failed;
perf_evlist__add(evlist, evsel);
ctx->sched_process_free = tep__event_id("sched", "sched_process_free");
if (ctx->sched_process_free < 0) goto failed;
attr.config = ctx->sched_process_free;
evsel = perf_evsel__new(&attr);
if (!evsel) goto failed;
perf_evlist__add(evlist, evsel);
tep__unref();
return 0;
failed:
tep__unref();
comm_deinit(dev);
return -1;
}
static void comm_gc(struct prof_dev *dev, u64 time_before)
{
struct comm_ctx *ctx = dev->private;
while (1) {
struct rb_node *rbn = rb_first_cached(&ctx->exited.entries);
struct pid_comm_node *node = rb_entry_safe(rbn, struct pid_comm_node, exit_node);
if (!node || node->update_time >= time_before)
break;
rblist__remove_node(&ctx->exited, rbn);
rblist__remove_node(&ctx->pid_comm, &node->rbnode);
}
}
static void comm_update(struct prof_dev *dev, u64 time, bool exit, int pid, char *comm)
{
struct comm_ctx *ctx = dev->private;
struct pid_comm_node new, *node;
struct rb_node *rbn;
new.pid = pid;
rbn = rblist__findnew(&ctx->pid_comm, &new);
node = rb_entry_safe(rbn, struct pid_comm_node, rbnode);
if (node) {
if (node->update_time < time) {
if (unlikely(!RB_EMPTY_NODE(&node->exit_node))) {
rblist__remove_node(&ctx->exited, &node->exit_node);
RB_CLEAR_NODE(&node->exit_node);
}
node->update_time = time;
node->flush = false;
*(u64 *)(node->comm) = *(u64 *)(comm);
*(u64 *)(node->comm+8) = *(u64 *)(comm+8);
}
if (exit) {
rblist__add_node(&ctx->exited, node);
}
}
}
/* Skip "." and ".." directories */
static int filter(const struct dirent *dir)
{
if (dir->d_name[0] == '.')
return 0;
else
return 1;
}
static char *get_comm(int pid, char *comm)
{
char path[64];
int fd, len;
snprintf(path, sizeof(path), "/proc/%d/comm", pid);
fd = open(path, O_RDONLY);
if (fd < 0) return NULL;
len = (int)read(fd, comm, TASK_COMM_LEN);
close(fd);
if (len <= 0) return NULL;
len--;
if (comm[len] == '\n' || len == TASK_COMM_LEN-1)
comm[len] = '\0';
return comm;
}
static void comm_enabled(struct prof_dev *dev)
{
DIR *proc;
int items, i;
char path[NAME_MAX + 1 + 6];
struct dirent *dirent, **namelist = NULL;
char buff[TASK_COMM_LEN] = {0};
// pid 0, swapper
strcpy(buff, "swapper");
comm_update(dev, 1, false, 0, buff);
proc = opendir("/proc");
if (proc == NULL)
return;
while ((dirent = readdir(proc)) != NULL) {
char *end;
pid_t pid = strtol(dirent->d_name, &end, 10);
if (*end) /* only interested in proper numerical dirents */
continue;
snprintf(path, sizeof(path), "/proc/%d/task", pid);
items = scandir(path, &namelist, filter, NULL);
if (items <= 0)
continue;
for (i = 0; i < items; i++) {
int tid = atoi(namelist[i]->d_name);
char *comm = get_comm(tid, buff);
if (comm)
comm_update(dev, 1, false, tid, comm);
}
for (i = 0; i < items; i++)
zfree(&namelist[i]);
free(namelist);
}
closedir(proc);
return;
}
static void comm_deinit(struct prof_dev *dev)
{
struct comm_ctx *ctx = dev->private;
global_comm_ctx = NULL;
rblist__exit(&ctx->exited);
rblist__exit(&ctx->pid_comm);
free(ctx);
}
static void comm_interval(struct prof_dev *dev)
{
u64 time_before = prof_dev_list_minevtime();
comm_gc(dev, time_before);
}
static void comm_sample(struct prof_dev *dev, union perf_event *event, int instance)
{
struct comm_ctx *ctx = dev->private;
// PERF_SAMPLE_TIME | PERF_SAMPLE_RAW
struct sample_type_header {
__u64 time;
struct {
__u32 size;
union {
__u8 data[0];
unsigned short common_type;
struct task_newtask task_newtask;
struct task_rename task_rename;
struct sched_process_free process_free;
};
} __packed raw;
} *data = (void *)event->sample.array;
unsigned short common_type = data->raw.common_type;
if (common_type == ctx->task_newtask)
comm_update(dev, data->time, false, data->raw.task_newtask.pid, data->raw.task_newtask.comm);
else if (common_type == ctx->task_rename)
comm_update(dev, data->time, false, data->raw.task_rename.pid, data->raw.task_rename.newcomm);
else if (common_type == ctx->sched_process_free)
comm_update(dev, data->time, true, data->raw.process_free.pid, data->raw.process_free.comm);
}
static profiler comm = {
.name = "comm",
.pages = 16,
.init = comm_init,
.enabled = comm_enabled,
.deinit = comm_deinit,
.interval = comm_interval,
.sample = comm_sample,
};
int global_comm_ref(void)
{
if (global_comm_ctx == NULL) {
struct env *env = zalloc(sizeof(*env));
if (!env)
return -1;
env->interval = 1000;
if (!prof_dev_open(&comm, env))
return -1;
} else
refcount_inc(&global_comm_ctx->ref);
return 0;
}
void global_comm_unref(void)
{
if (!global_comm_ctx)
return;
if (refcount_dec_and_test(&global_comm_ctx->ref))
prof_dev_close(global_comm_ctx->comm_dev);
}
char *global_comm_get(int pid)
{
struct pid_comm_node find, *node;
struct rb_node *rbn;
if (!global_comm_ctx)
return NULL;
find.pid = pid;
rbn = rblist__find(&global_comm_ctx->pid_comm, &find);
node = rb_entry_safe(rbn, struct pid_comm_node, rbnode);
if (!node || node->flush) {
prof_dev_flush(global_comm_ctx->comm_dev, PROF_DEV_FLUSH_NORMAL);
rbn = rblist__find(&global_comm_ctx->pid_comm, &find);
node = rb_entry_safe(rbn, struct pid_comm_node, rbnode);
}
return node ? node->comm : NULL;
}
void global_comm_flush(int pid)
{
struct pid_comm_node find, *node;
struct rb_node *rbn;
if (!global_comm_ctx)
return;
if (pid == 0)
return;
find.pid = pid;
rbn = rblist__find(&global_comm_ctx->pid_comm, &find);
node = rb_entry_safe(rbn, struct pid_comm_node, rbnode);
if (node)
node->flush = true;
}
void global_comm_register_notify(struct comm_notify *node)
{
INIT_LIST_HEAD(&node->link);
list_add(&node->link, &global_comm_notify_list);
}
void global_comm_unregister_notify(struct comm_notify *node)
{
list_del_init(&node->link);
}