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