Commit | Line | Data |
---|---|---|
ba77c9e1 LZ |
1 | #include "builtin.h" |
2 | #include "perf.h" | |
3 | ||
0f7d2f1b | 4 | #include "util/evlist.h" |
fcf65bf1 | 5 | #include "util/evsel.h" |
ba77c9e1 LZ |
6 | #include "util/util.h" |
7 | #include "util/cache.h" | |
8 | #include "util/symbol.h" | |
9 | #include "util/thread.h" | |
10 | #include "util/header.h" | |
94c744b6 | 11 | #include "util/session.h" |
45694aa7 | 12 | #include "util/tool.h" |
ba77c9e1 LZ |
13 | |
14 | #include "util/parse-options.h" | |
15 | #include "util/trace-event.h" | |
16 | ||
17 | #include "util/debug.h" | |
ba77c9e1 LZ |
18 | |
19 | #include <linux/rbtree.h> | |
20 | ||
21 | struct alloc_stat; | |
22 | typedef int (*sort_fn_t)(struct alloc_stat *, struct alloc_stat *); | |
23 | ||
efad1415 | 24 | static const char *input_name; |
ba77c9e1 | 25 | |
ba77c9e1 LZ |
26 | static int alloc_flag; |
27 | static int caller_flag; | |
28 | ||
ba77c9e1 LZ |
29 | static int alloc_lines = -1; |
30 | static int caller_lines = -1; | |
31 | ||
7707b6b6 LZ |
32 | static bool raw_ip; |
33 | ||
29b3e152 LZ |
34 | static char default_sort_order[] = "frag,hit,bytes"; |
35 | ||
7d0d3945 LZ |
36 | static int *cpunode_map; |
37 | static int max_cpu_num; | |
38 | ||
ba77c9e1 | 39 | struct alloc_stat { |
079d3f65 LZ |
40 | u64 call_site; |
41 | u64 ptr; | |
ba77c9e1 LZ |
42 | u64 bytes_req; |
43 | u64 bytes_alloc; | |
44 | u32 hit; | |
079d3f65 LZ |
45 | u32 pingpong; |
46 | ||
47 | short alloc_cpu; | |
ba77c9e1 LZ |
48 | |
49 | struct rb_node node; | |
50 | }; | |
51 | ||
52 | static struct rb_root root_alloc_stat; | |
53 | static struct rb_root root_alloc_sorted; | |
54 | static struct rb_root root_caller_stat; | |
55 | static struct rb_root root_caller_sorted; | |
56 | ||
57 | static unsigned long total_requested, total_allocated; | |
7d0d3945 | 58 | static unsigned long nr_allocs, nr_cross_allocs; |
ba77c9e1 | 59 | |
7d0d3945 LZ |
60 | #define PATH_SYS_NODE "/sys/devices/system/node" |
61 | ||
2814eb05 | 62 | static int init_cpunode_map(void) |
7d0d3945 LZ |
63 | { |
64 | FILE *fp; | |
2814eb05 | 65 | int i, err = -1; |
7d0d3945 LZ |
66 | |
67 | fp = fopen("/sys/devices/system/cpu/kernel_max", "r"); | |
68 | if (!fp) { | |
69 | max_cpu_num = 4096; | |
2814eb05 ACM |
70 | return 0; |
71 | } | |
72 | ||
73 | if (fscanf(fp, "%d", &max_cpu_num) < 1) { | |
74 | pr_err("Failed to read 'kernel_max' from sysfs"); | |
75 | goto out_close; | |
7d0d3945 LZ |
76 | } |
77 | ||
7d0d3945 LZ |
78 | max_cpu_num++; |
79 | ||
80 | cpunode_map = calloc(max_cpu_num, sizeof(int)); | |
2814eb05 ACM |
81 | if (!cpunode_map) { |
82 | pr_err("%s: calloc failed\n", __func__); | |
83 | goto out_close; | |
84 | } | |
85 | ||
7d0d3945 LZ |
86 | for (i = 0; i < max_cpu_num; i++) |
87 | cpunode_map[i] = -1; | |
2814eb05 ACM |
88 | |
89 | err = 0; | |
90 | out_close: | |
7d0d3945 | 91 | fclose(fp); |
2814eb05 | 92 | return err; |
7d0d3945 LZ |
93 | } |
94 | ||
2814eb05 | 95 | static int setup_cpunode_map(void) |
7d0d3945 LZ |
96 | { |
97 | struct dirent *dent1, *dent2; | |
98 | DIR *dir1, *dir2; | |
99 | unsigned int cpu, mem; | |
100 | char buf[PATH_MAX]; | |
101 | ||
2814eb05 ACM |
102 | if (init_cpunode_map()) |
103 | return -1; | |
7d0d3945 LZ |
104 | |
105 | dir1 = opendir(PATH_SYS_NODE); | |
106 | if (!dir1) | |
2814eb05 | 107 | return -1; |
7d0d3945 | 108 | |
659d8cfb UD |
109 | while ((dent1 = readdir(dir1)) != NULL) { |
110 | if (dent1->d_type != DT_DIR || | |
111 | sscanf(dent1->d_name, "node%u", &mem) < 1) | |
7d0d3945 LZ |
112 | continue; |
113 | ||
114 | snprintf(buf, PATH_MAX, "%s/%s", PATH_SYS_NODE, dent1->d_name); | |
115 | dir2 = opendir(buf); | |
116 | if (!dir2) | |
117 | continue; | |
659d8cfb UD |
118 | while ((dent2 = readdir(dir2)) != NULL) { |
119 | if (dent2->d_type != DT_LNK || | |
120 | sscanf(dent2->d_name, "cpu%u", &cpu) < 1) | |
7d0d3945 LZ |
121 | continue; |
122 | cpunode_map[cpu] = mem; | |
123 | } | |
8442da1d | 124 | closedir(dir2); |
7d0d3945 | 125 | } |
8442da1d | 126 | closedir(dir1); |
2814eb05 | 127 | return 0; |
7d0d3945 LZ |
128 | } |
129 | ||
2814eb05 ACM |
130 | static int insert_alloc_stat(unsigned long call_site, unsigned long ptr, |
131 | int bytes_req, int bytes_alloc, int cpu) | |
ba77c9e1 LZ |
132 | { |
133 | struct rb_node **node = &root_alloc_stat.rb_node; | |
134 | struct rb_node *parent = NULL; | |
135 | struct alloc_stat *data = NULL; | |
136 | ||
ba77c9e1 LZ |
137 | while (*node) { |
138 | parent = *node; | |
139 | data = rb_entry(*node, struct alloc_stat, node); | |
140 | ||
141 | if (ptr > data->ptr) | |
142 | node = &(*node)->rb_right; | |
143 | else if (ptr < data->ptr) | |
144 | node = &(*node)->rb_left; | |
145 | else | |
146 | break; | |
147 | } | |
148 | ||
149 | if (data && data->ptr == ptr) { | |
150 | data->hit++; | |
151 | data->bytes_req += bytes_req; | |
4efb5290 | 152 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
153 | } else { |
154 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
155 | if (!data) { |
156 | pr_err("%s: malloc failed\n", __func__); | |
157 | return -1; | |
158 | } | |
ba77c9e1 | 159 | data->ptr = ptr; |
079d3f65 | 160 | data->pingpong = 0; |
ba77c9e1 LZ |
161 | data->hit = 1; |
162 | data->bytes_req = bytes_req; | |
163 | data->bytes_alloc = bytes_alloc; | |
164 | ||
165 | rb_link_node(&data->node, parent, node); | |
166 | rb_insert_color(&data->node, &root_alloc_stat); | |
167 | } | |
079d3f65 LZ |
168 | data->call_site = call_site; |
169 | data->alloc_cpu = cpu; | |
2814eb05 | 170 | return 0; |
ba77c9e1 LZ |
171 | } |
172 | ||
2814eb05 | 173 | static int insert_caller_stat(unsigned long call_site, |
ba77c9e1 LZ |
174 | int bytes_req, int bytes_alloc) |
175 | { | |
176 | struct rb_node **node = &root_caller_stat.rb_node; | |
177 | struct rb_node *parent = NULL; | |
178 | struct alloc_stat *data = NULL; | |
179 | ||
ba77c9e1 LZ |
180 | while (*node) { |
181 | parent = *node; | |
182 | data = rb_entry(*node, struct alloc_stat, node); | |
183 | ||
184 | if (call_site > data->call_site) | |
185 | node = &(*node)->rb_right; | |
186 | else if (call_site < data->call_site) | |
187 | node = &(*node)->rb_left; | |
188 | else | |
189 | break; | |
190 | } | |
191 | ||
192 | if (data && data->call_site == call_site) { | |
193 | data->hit++; | |
194 | data->bytes_req += bytes_req; | |
4efb5290 | 195 | data->bytes_alloc += bytes_alloc; |
ba77c9e1 LZ |
196 | } else { |
197 | data = malloc(sizeof(*data)); | |
2814eb05 ACM |
198 | if (!data) { |
199 | pr_err("%s: malloc failed\n", __func__); | |
200 | return -1; | |
201 | } | |
ba77c9e1 | 202 | data->call_site = call_site; |
079d3f65 | 203 | data->pingpong = 0; |
ba77c9e1 LZ |
204 | data->hit = 1; |
205 | data->bytes_req = bytes_req; | |
206 | data->bytes_alloc = bytes_alloc; | |
207 | ||
208 | rb_link_node(&data->node, parent, node); | |
209 | rb_insert_color(&data->node, &root_caller_stat); | |
210 | } | |
2814eb05 ACM |
211 | |
212 | return 0; | |
ba77c9e1 LZ |
213 | } |
214 | ||
2814eb05 | 215 | static int perf_evsel__process_alloc_event(struct perf_evsel *evsel, |
0f7d2f1b | 216 | struct perf_sample *sample) |
ba77c9e1 | 217 | { |
0f7d2f1b ACM |
218 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"), |
219 | call_site = perf_evsel__intval(evsel, sample, "call_site"); | |
220 | int bytes_req = perf_evsel__intval(evsel, sample, "bytes_req"), | |
221 | bytes_alloc = perf_evsel__intval(evsel, sample, "bytes_alloc"); | |
222 | ||
223 | if (insert_alloc_stat(call_site, ptr, bytes_req, bytes_alloc, sample->cpu) || | |
2814eb05 ACM |
224 | insert_caller_stat(call_site, bytes_req, bytes_alloc)) |
225 | return -1; | |
ba77c9e1 LZ |
226 | |
227 | total_requested += bytes_req; | |
228 | total_allocated += bytes_alloc; | |
7d0d3945 | 229 | |
0f7d2f1b ACM |
230 | nr_allocs++; |
231 | return 0; | |
232 | } | |
233 | ||
234 | static int perf_evsel__process_alloc_node_event(struct perf_evsel *evsel, | |
235 | struct perf_sample *sample) | |
236 | { | |
237 | int ret = perf_evsel__process_alloc_event(evsel, sample); | |
238 | ||
239 | if (!ret) { | |
240 | int node1 = cpunode_map[sample->cpu], | |
241 | node2 = perf_evsel__intval(evsel, sample, "node"); | |
242 | ||
7d0d3945 LZ |
243 | if (node1 != node2) |
244 | nr_cross_allocs++; | |
245 | } | |
0f7d2f1b ACM |
246 | |
247 | return ret; | |
ba77c9e1 LZ |
248 | } |
249 | ||
079d3f65 LZ |
250 | static int ptr_cmp(struct alloc_stat *, struct alloc_stat *); |
251 | static int callsite_cmp(struct alloc_stat *, struct alloc_stat *); | |
252 | ||
253 | static struct alloc_stat *search_alloc_stat(unsigned long ptr, | |
254 | unsigned long call_site, | |
255 | struct rb_root *root, | |
256 | sort_fn_t sort_fn) | |
257 | { | |
258 | struct rb_node *node = root->rb_node; | |
259 | struct alloc_stat key = { .ptr = ptr, .call_site = call_site }; | |
260 | ||
261 | while (node) { | |
262 | struct alloc_stat *data; | |
263 | int cmp; | |
264 | ||
265 | data = rb_entry(node, struct alloc_stat, node); | |
266 | ||
267 | cmp = sort_fn(&key, data); | |
268 | if (cmp < 0) | |
269 | node = node->rb_left; | |
270 | else if (cmp > 0) | |
271 | node = node->rb_right; | |
272 | else | |
273 | return data; | |
274 | } | |
275 | return NULL; | |
276 | } | |
277 | ||
2814eb05 ACM |
278 | static int perf_evsel__process_free_event(struct perf_evsel *evsel, |
279 | struct perf_sample *sample) | |
ba77c9e1 | 280 | { |
0f7d2f1b | 281 | unsigned long ptr = perf_evsel__intval(evsel, sample, "ptr"); |
079d3f65 LZ |
282 | struct alloc_stat *s_alloc, *s_caller; |
283 | ||
079d3f65 LZ |
284 | s_alloc = search_alloc_stat(ptr, 0, &root_alloc_stat, ptr_cmp); |
285 | if (!s_alloc) | |
2814eb05 | 286 | return 0; |
079d3f65 | 287 | |
22ad798c | 288 | if ((short)sample->cpu != s_alloc->alloc_cpu) { |
079d3f65 LZ |
289 | s_alloc->pingpong++; |
290 | ||
291 | s_caller = search_alloc_stat(0, s_alloc->call_site, | |
292 | &root_caller_stat, callsite_cmp); | |
2814eb05 ACM |
293 | if (!s_caller) |
294 | return -1; | |
079d3f65 LZ |
295 | s_caller->pingpong++; |
296 | } | |
297 | s_alloc->alloc_cpu = -1; | |
2814eb05 ACM |
298 | |
299 | return 0; | |
ba77c9e1 LZ |
300 | } |
301 | ||
0f7d2f1b ACM |
302 | typedef int (*tracepoint_handler)(struct perf_evsel *evsel, |
303 | struct perf_sample *sample); | |
ba77c9e1 | 304 | |
1d037ca1 | 305 | static int process_sample_event(struct perf_tool *tool __maybe_unused, |
d20deb64 | 306 | union perf_event *event, |
8115d60c | 307 | struct perf_sample *sample, |
fcf65bf1 | 308 | struct perf_evsel *evsel, |
743eb868 | 309 | struct machine *machine) |
ba77c9e1 | 310 | { |
743eb868 | 311 | struct thread *thread = machine__findnew_thread(machine, event->ip.pid); |
ba77c9e1 | 312 | |
ba77c9e1 LZ |
313 | if (thread == NULL) { |
314 | pr_debug("problem processing %d event, skipping it.\n", | |
315 | event->header.type); | |
316 | return -1; | |
317 | } | |
318 | ||
319 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | |
320 | ||
0f7d2f1b ACM |
321 | if (evsel->handler.func != NULL) { |
322 | tracepoint_handler f = evsel->handler.func; | |
323 | return f(evsel, sample); | |
324 | } | |
325 | ||
326 | return 0; | |
ba77c9e1 LZ |
327 | } |
328 | ||
fcf65bf1 ACM |
329 | static struct perf_tool perf_kmem = { |
330 | .sample = process_sample_event, | |
331 | .comm = perf_event__process_comm, | |
332 | .ordered_samples = true, | |
ba77c9e1 LZ |
333 | }; |
334 | ||
ba77c9e1 LZ |
335 | static double fragmentation(unsigned long n_req, unsigned long n_alloc) |
336 | { | |
337 | if (n_alloc == 0) | |
338 | return 0.0; | |
339 | else | |
340 | return 100.0 - (100.0 * n_req / n_alloc); | |
341 | } | |
342 | ||
4aa65636 ACM |
343 | static void __print_result(struct rb_root *root, struct perf_session *session, |
344 | int n_lines, int is_caller) | |
ba77c9e1 LZ |
345 | { |
346 | struct rb_node *next; | |
23346f21 | 347 | struct machine *machine; |
ba77c9e1 | 348 | |
079d3f65 LZ |
349 | printf("%.102s\n", graph_dotted_line); |
350 | printf(" %-34s |", is_caller ? "Callsite": "Alloc Ptr"); | |
47103277 | 351 | printf(" Total_alloc/Per | Total_req/Per | Hit | Ping-pong | Frag\n"); |
079d3f65 | 352 | printf("%.102s\n", graph_dotted_line); |
ba77c9e1 LZ |
353 | |
354 | next = rb_first(root); | |
355 | ||
23346f21 ACM |
356 | machine = perf_session__find_host_machine(session); |
357 | if (!machine) { | |
a1645ce1 ZY |
358 | pr_err("__print_result: couldn't find kernel information\n"); |
359 | return; | |
360 | } | |
ba77c9e1 | 361 | while (next && n_lines--) { |
1b145ae5 ACM |
362 | struct alloc_stat *data = rb_entry(next, struct alloc_stat, |
363 | node); | |
364 | struct symbol *sym = NULL; | |
71cf8b8f | 365 | struct map *map; |
079d3f65 | 366 | char buf[BUFSIZ]; |
1b145ae5 ACM |
367 | u64 addr; |
368 | ||
369 | if (is_caller) { | |
370 | addr = data->call_site; | |
7707b6b6 | 371 | if (!raw_ip) |
5c0541d5 | 372 | sym = machine__find_kernel_function(machine, addr, &map, NULL); |
1b145ae5 ACM |
373 | } else |
374 | addr = data->ptr; | |
375 | ||
376 | if (sym != NULL) | |
9486aa38 | 377 | snprintf(buf, sizeof(buf), "%s+%" PRIx64 "", sym->name, |
71cf8b8f | 378 | addr - map->unmap_ip(map, sym->start)); |
1b145ae5 | 379 | else |
9486aa38 | 380 | snprintf(buf, sizeof(buf), "%#" PRIx64 "", addr); |
079d3f65 | 381 | printf(" %-34s |", buf); |
ba77c9e1 | 382 | |
47103277 | 383 | printf(" %9llu/%-5lu | %9llu/%-5lu | %8lu | %8lu | %6.3f%%\n", |
079d3f65 | 384 | (unsigned long long)data->bytes_alloc, |
ba77c9e1 LZ |
385 | (unsigned long)data->bytes_alloc / data->hit, |
386 | (unsigned long long)data->bytes_req, | |
387 | (unsigned long)data->bytes_req / data->hit, | |
388 | (unsigned long)data->hit, | |
079d3f65 | 389 | (unsigned long)data->pingpong, |
ba77c9e1 LZ |
390 | fragmentation(data->bytes_req, data->bytes_alloc)); |
391 | ||
392 | next = rb_next(next); | |
393 | } | |
394 | ||
395 | if (n_lines == -1) | |
079d3f65 | 396 | printf(" ... | ... | ... | ... | ... | ... \n"); |
ba77c9e1 | 397 | |
079d3f65 | 398 | printf("%.102s\n", graph_dotted_line); |
ba77c9e1 LZ |
399 | } |
400 | ||
401 | static void print_summary(void) | |
402 | { | |
403 | printf("\nSUMMARY\n=======\n"); | |
404 | printf("Total bytes requested: %lu\n", total_requested); | |
405 | printf("Total bytes allocated: %lu\n", total_allocated); | |
406 | printf("Total bytes wasted on internal fragmentation: %lu\n", | |
407 | total_allocated - total_requested); | |
408 | printf("Internal fragmentation: %f%%\n", | |
409 | fragmentation(total_requested, total_allocated)); | |
7d0d3945 | 410 | printf("Cross CPU allocations: %lu/%lu\n", nr_cross_allocs, nr_allocs); |
ba77c9e1 LZ |
411 | } |
412 | ||
4aa65636 | 413 | static void print_result(struct perf_session *session) |
ba77c9e1 LZ |
414 | { |
415 | if (caller_flag) | |
4aa65636 | 416 | __print_result(&root_caller_sorted, session, caller_lines, 1); |
ba77c9e1 | 417 | if (alloc_flag) |
4aa65636 | 418 | __print_result(&root_alloc_sorted, session, alloc_lines, 0); |
ba77c9e1 LZ |
419 | print_summary(); |
420 | } | |
421 | ||
29b3e152 LZ |
422 | struct sort_dimension { |
423 | const char name[20]; | |
424 | sort_fn_t cmp; | |
425 | struct list_head list; | |
426 | }; | |
427 | ||
428 | static LIST_HEAD(caller_sort); | |
429 | static LIST_HEAD(alloc_sort); | |
430 | ||
ba77c9e1 | 431 | static void sort_insert(struct rb_root *root, struct alloc_stat *data, |
29b3e152 | 432 | struct list_head *sort_list) |
ba77c9e1 LZ |
433 | { |
434 | struct rb_node **new = &(root->rb_node); | |
435 | struct rb_node *parent = NULL; | |
29b3e152 | 436 | struct sort_dimension *sort; |
ba77c9e1 LZ |
437 | |
438 | while (*new) { | |
439 | struct alloc_stat *this; | |
29b3e152 | 440 | int cmp = 0; |
ba77c9e1 LZ |
441 | |
442 | this = rb_entry(*new, struct alloc_stat, node); | |
443 | parent = *new; | |
444 | ||
29b3e152 LZ |
445 | list_for_each_entry(sort, sort_list, list) { |
446 | cmp = sort->cmp(data, this); | |
447 | if (cmp) | |
448 | break; | |
449 | } | |
ba77c9e1 LZ |
450 | |
451 | if (cmp > 0) | |
452 | new = &((*new)->rb_left); | |
453 | else | |
454 | new = &((*new)->rb_right); | |
455 | } | |
456 | ||
457 | rb_link_node(&data->node, parent, new); | |
458 | rb_insert_color(&data->node, root); | |
459 | } | |
460 | ||
461 | static void __sort_result(struct rb_root *root, struct rb_root *root_sorted, | |
29b3e152 | 462 | struct list_head *sort_list) |
ba77c9e1 LZ |
463 | { |
464 | struct rb_node *node; | |
465 | struct alloc_stat *data; | |
466 | ||
467 | for (;;) { | |
468 | node = rb_first(root); | |
469 | if (!node) | |
470 | break; | |
471 | ||
472 | rb_erase(node, root); | |
473 | data = rb_entry(node, struct alloc_stat, node); | |
29b3e152 | 474 | sort_insert(root_sorted, data, sort_list); |
ba77c9e1 LZ |
475 | } |
476 | } | |
477 | ||
478 | static void sort_result(void) | |
479 | { | |
29b3e152 LZ |
480 | __sort_result(&root_alloc_stat, &root_alloc_sorted, &alloc_sort); |
481 | __sort_result(&root_caller_stat, &root_caller_sorted, &caller_sort); | |
ba77c9e1 LZ |
482 | } |
483 | ||
484 | static int __cmd_kmem(void) | |
485 | { | |
d549c769 | 486 | int err = -EINVAL; |
da378962 | 487 | struct perf_session *session; |
0f7d2f1b ACM |
488 | const struct perf_evsel_str_handler kmem_tracepoints[] = { |
489 | { "kmem:kmalloc", perf_evsel__process_alloc_event, }, | |
490 | { "kmem:kmem_cache_alloc", perf_evsel__process_alloc_event, }, | |
491 | { "kmem:kmalloc_node", perf_evsel__process_alloc_node_event, }, | |
492 | { "kmem:kmem_cache_alloc_node", perf_evsel__process_alloc_node_event, }, | |
493 | { "kmem:kfree", perf_evsel__process_free_event, }, | |
494 | { "kmem:kmem_cache_free", perf_evsel__process_free_event, }, | |
495 | }; | |
da378962 | 496 | |
fcf65bf1 | 497 | session = perf_session__new(input_name, O_RDONLY, 0, false, &perf_kmem); |
4aa65636 ACM |
498 | if (session == NULL) |
499 | return -ENOMEM; | |
500 | ||
e727ca73 ACM |
501 | if (perf_session__create_kernel_maps(session) < 0) |
502 | goto out_delete; | |
503 | ||
d549c769 ACM |
504 | if (!perf_session__has_traces(session, "kmem record")) |
505 | goto out_delete; | |
506 | ||
0f7d2f1b ACM |
507 | if (perf_session__set_tracepoints_handlers(session, kmem_tracepoints)) { |
508 | pr_err("Initializing perf session tracepoint handlers failed\n"); | |
509 | return -1; | |
510 | } | |
511 | ||
ba77c9e1 | 512 | setup_pager(); |
fcf65bf1 | 513 | err = perf_session__process_events(session, &perf_kmem); |
4aa65636 ACM |
514 | if (err != 0) |
515 | goto out_delete; | |
ba77c9e1 | 516 | sort_result(); |
4aa65636 ACM |
517 | print_result(session); |
518 | out_delete: | |
519 | perf_session__delete(session); | |
520 | return err; | |
ba77c9e1 LZ |
521 | } |
522 | ||
523 | static const char * const kmem_usage[] = { | |
90b86a9f | 524 | "perf kmem [<options>] {record|stat}", |
ba77c9e1 LZ |
525 | NULL |
526 | }; | |
527 | ||
ba77c9e1 LZ |
528 | static int ptr_cmp(struct alloc_stat *l, struct alloc_stat *r) |
529 | { | |
530 | if (l->ptr < r->ptr) | |
531 | return -1; | |
532 | else if (l->ptr > r->ptr) | |
533 | return 1; | |
534 | return 0; | |
535 | } | |
536 | ||
29b3e152 LZ |
537 | static struct sort_dimension ptr_sort_dimension = { |
538 | .name = "ptr", | |
539 | .cmp = ptr_cmp, | |
540 | }; | |
541 | ||
ba77c9e1 LZ |
542 | static int callsite_cmp(struct alloc_stat *l, struct alloc_stat *r) |
543 | { | |
544 | if (l->call_site < r->call_site) | |
545 | return -1; | |
546 | else if (l->call_site > r->call_site) | |
547 | return 1; | |
548 | return 0; | |
549 | } | |
550 | ||
29b3e152 LZ |
551 | static struct sort_dimension callsite_sort_dimension = { |
552 | .name = "callsite", | |
553 | .cmp = callsite_cmp, | |
554 | }; | |
555 | ||
f3ced7cd PE |
556 | static int hit_cmp(struct alloc_stat *l, struct alloc_stat *r) |
557 | { | |
558 | if (l->hit < r->hit) | |
559 | return -1; | |
560 | else if (l->hit > r->hit) | |
561 | return 1; | |
562 | return 0; | |
563 | } | |
564 | ||
29b3e152 LZ |
565 | static struct sort_dimension hit_sort_dimension = { |
566 | .name = "hit", | |
567 | .cmp = hit_cmp, | |
568 | }; | |
569 | ||
ba77c9e1 LZ |
570 | static int bytes_cmp(struct alloc_stat *l, struct alloc_stat *r) |
571 | { | |
572 | if (l->bytes_alloc < r->bytes_alloc) | |
573 | return -1; | |
574 | else if (l->bytes_alloc > r->bytes_alloc) | |
575 | return 1; | |
576 | return 0; | |
577 | } | |
578 | ||
29b3e152 LZ |
579 | static struct sort_dimension bytes_sort_dimension = { |
580 | .name = "bytes", | |
581 | .cmp = bytes_cmp, | |
582 | }; | |
583 | ||
f3ced7cd PE |
584 | static int frag_cmp(struct alloc_stat *l, struct alloc_stat *r) |
585 | { | |
586 | double x, y; | |
587 | ||
588 | x = fragmentation(l->bytes_req, l->bytes_alloc); | |
589 | y = fragmentation(r->bytes_req, r->bytes_alloc); | |
590 | ||
591 | if (x < y) | |
592 | return -1; | |
593 | else if (x > y) | |
594 | return 1; | |
595 | return 0; | |
596 | } | |
597 | ||
29b3e152 LZ |
598 | static struct sort_dimension frag_sort_dimension = { |
599 | .name = "frag", | |
600 | .cmp = frag_cmp, | |
601 | }; | |
602 | ||
079d3f65 LZ |
603 | static int pingpong_cmp(struct alloc_stat *l, struct alloc_stat *r) |
604 | { | |
605 | if (l->pingpong < r->pingpong) | |
606 | return -1; | |
607 | else if (l->pingpong > r->pingpong) | |
608 | return 1; | |
609 | return 0; | |
610 | } | |
611 | ||
612 | static struct sort_dimension pingpong_sort_dimension = { | |
613 | .name = "pingpong", | |
614 | .cmp = pingpong_cmp, | |
615 | }; | |
616 | ||
29b3e152 LZ |
617 | static struct sort_dimension *avail_sorts[] = { |
618 | &ptr_sort_dimension, | |
619 | &callsite_sort_dimension, | |
620 | &hit_sort_dimension, | |
621 | &bytes_sort_dimension, | |
622 | &frag_sort_dimension, | |
079d3f65 | 623 | &pingpong_sort_dimension, |
29b3e152 LZ |
624 | }; |
625 | ||
626 | #define NUM_AVAIL_SORTS \ | |
627 | (int)(sizeof(avail_sorts) / sizeof(struct sort_dimension *)) | |
628 | ||
629 | static int sort_dimension__add(const char *tok, struct list_head *list) | |
630 | { | |
631 | struct sort_dimension *sort; | |
632 | int i; | |
633 | ||
634 | for (i = 0; i < NUM_AVAIL_SORTS; i++) { | |
635 | if (!strcmp(avail_sorts[i]->name, tok)) { | |
636 | sort = malloc(sizeof(*sort)); | |
2814eb05 ACM |
637 | if (!sort) { |
638 | pr_err("%s: malloc failed\n", __func__); | |
639 | return -1; | |
640 | } | |
29b3e152 LZ |
641 | memcpy(sort, avail_sorts[i], sizeof(*sort)); |
642 | list_add_tail(&sort->list, list); | |
643 | return 0; | |
644 | } | |
645 | } | |
646 | ||
647 | return -1; | |
648 | } | |
649 | ||
650 | static int setup_sorting(struct list_head *sort_list, const char *arg) | |
651 | { | |
652 | char *tok; | |
653 | char *str = strdup(arg); | |
654 | ||
2814eb05 ACM |
655 | if (!str) { |
656 | pr_err("%s: strdup failed\n", __func__); | |
657 | return -1; | |
658 | } | |
29b3e152 LZ |
659 | |
660 | while (true) { | |
661 | tok = strsep(&str, ","); | |
662 | if (!tok) | |
663 | break; | |
664 | if (sort_dimension__add(tok, sort_list) < 0) { | |
665 | error("Unknown --sort key: '%s'", tok); | |
1b22859d | 666 | free(str); |
29b3e152 LZ |
667 | return -1; |
668 | } | |
669 | } | |
670 | ||
671 | free(str); | |
672 | return 0; | |
673 | } | |
674 | ||
1d037ca1 IT |
675 | static int parse_sort_opt(const struct option *opt __maybe_unused, |
676 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 | 677 | { |
ba77c9e1 LZ |
678 | if (!arg) |
679 | return -1; | |
680 | ||
ba77c9e1 | 681 | if (caller_flag > alloc_flag) |
29b3e152 | 682 | return setup_sorting(&caller_sort, arg); |
ba77c9e1 | 683 | else |
29b3e152 | 684 | return setup_sorting(&alloc_sort, arg); |
ba77c9e1 LZ |
685 | |
686 | return 0; | |
687 | } | |
688 | ||
1d037ca1 IT |
689 | static int parse_caller_opt(const struct option *opt __maybe_unused, |
690 | const char *arg __maybe_unused, | |
691 | int unset __maybe_unused) | |
ba77c9e1 | 692 | { |
90b86a9f LZ |
693 | caller_flag = (alloc_flag + 1); |
694 | return 0; | |
695 | } | |
ba77c9e1 | 696 | |
1d037ca1 IT |
697 | static int parse_alloc_opt(const struct option *opt __maybe_unused, |
698 | const char *arg __maybe_unused, | |
699 | int unset __maybe_unused) | |
90b86a9f LZ |
700 | { |
701 | alloc_flag = (caller_flag + 1); | |
ba77c9e1 LZ |
702 | return 0; |
703 | } | |
704 | ||
1d037ca1 IT |
705 | static int parse_line_opt(const struct option *opt __maybe_unused, |
706 | const char *arg, int unset __maybe_unused) | |
ba77c9e1 LZ |
707 | { |
708 | int lines; | |
709 | ||
710 | if (!arg) | |
711 | return -1; | |
712 | ||
713 | lines = strtoul(arg, NULL, 10); | |
714 | ||
715 | if (caller_flag > alloc_flag) | |
716 | caller_lines = lines; | |
717 | else | |
718 | alloc_lines = lines; | |
719 | ||
720 | return 0; | |
721 | } | |
722 | ||
723 | static const struct option kmem_options[] = { | |
724 | OPT_STRING('i', "input", &input_name, "file", | |
725 | "input file name"), | |
90b86a9f LZ |
726 | OPT_CALLBACK_NOOPT(0, "caller", NULL, NULL, |
727 | "show per-callsite statistics", | |
728 | parse_caller_opt), | |
729 | OPT_CALLBACK_NOOPT(0, "alloc", NULL, NULL, | |
730 | "show per-allocation statistics", | |
731 | parse_alloc_opt), | |
29b3e152 | 732 | OPT_CALLBACK('s', "sort", NULL, "key[,key2...]", |
079d3f65 | 733 | "sort by keys: ptr, call_site, bytes, hit, pingpong, frag", |
ba77c9e1 LZ |
734 | parse_sort_opt), |
735 | OPT_CALLBACK('l', "line", NULL, "num", | |
90b86a9f | 736 | "show n lines", |
ba77c9e1 | 737 | parse_line_opt), |
7707b6b6 | 738 | OPT_BOOLEAN(0, "raw-ip", &raw_ip, "show raw ip instead of symbol"), |
ba77c9e1 LZ |
739 | OPT_END() |
740 | }; | |
741 | ||
742 | static const char *record_args[] = { | |
743 | "record", | |
744 | "-a", | |
745 | "-R", | |
ba77c9e1 LZ |
746 | "-f", |
747 | "-c", "1", | |
748 | "-e", "kmem:kmalloc", | |
749 | "-e", "kmem:kmalloc_node", | |
750 | "-e", "kmem:kfree", | |
751 | "-e", "kmem:kmem_cache_alloc", | |
752 | "-e", "kmem:kmem_cache_alloc_node", | |
753 | "-e", "kmem:kmem_cache_free", | |
754 | }; | |
755 | ||
756 | static int __cmd_record(int argc, const char **argv) | |
757 | { | |
758 | unsigned int rec_argc, i, j; | |
759 | const char **rec_argv; | |
760 | ||
761 | rec_argc = ARRAY_SIZE(record_args) + argc - 1; | |
762 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); | |
763 | ||
ce47dc56 CS |
764 | if (rec_argv == NULL) |
765 | return -ENOMEM; | |
766 | ||
ba77c9e1 LZ |
767 | for (i = 0; i < ARRAY_SIZE(record_args); i++) |
768 | rec_argv[i] = strdup(record_args[i]); | |
769 | ||
770 | for (j = 1; j < (unsigned int)argc; j++, i++) | |
771 | rec_argv[i] = argv[j]; | |
772 | ||
773 | return cmd_record(i, rec_argv, NULL); | |
774 | } | |
775 | ||
1d037ca1 | 776 | int cmd_kmem(int argc, const char **argv, const char *prefix __maybe_unused) |
ba77c9e1 | 777 | { |
ba77c9e1 LZ |
778 | argc = parse_options(argc, argv, kmem_options, kmem_usage, 0); |
779 | ||
90b86a9f | 780 | if (!argc) |
ba77c9e1 LZ |
781 | usage_with_options(kmem_usage, kmem_options); |
782 | ||
655000e7 ACM |
783 | symbol__init(); |
784 | ||
90b86a9f LZ |
785 | if (!strncmp(argv[0], "rec", 3)) { |
786 | return __cmd_record(argc, argv); | |
787 | } else if (!strcmp(argv[0], "stat")) { | |
2814eb05 ACM |
788 | if (setup_cpunode_map()) |
789 | return -1; | |
90b86a9f LZ |
790 | |
791 | if (list_empty(&caller_sort)) | |
792 | setup_sorting(&caller_sort, default_sort_order); | |
793 | if (list_empty(&alloc_sort)) | |
794 | setup_sorting(&alloc_sort, default_sort_order); | |
ba77c9e1 | 795 | |
90b86a9f | 796 | return __cmd_kmem(); |
b00eca8c PE |
797 | } else |
798 | usage_with_options(kmem_usage, kmem_options); | |
7d0d3945 | 799 | |
90b86a9f | 800 | return 0; |
ba77c9e1 LZ |
801 | } |
802 |