Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Refactor code to move call path handling out of thread-stack

Move the call path handling code out of thread-stack.c and
thread-stack.h to allow other components that are not part of
thread-stack to create call paths.

Summary:

- Create call-path.c and call-path.h and add them to the build.

- Move all call path related code out of thread-stack.c and thread-stack.h
and into call-path.c and call-path.h.

- A small subset of structures and functions are now visible through
call-path.h, which is required for thread-stack.c to continue to
compile.

This change is a prerequisite for subsequent patches in this change set
and by itself contains no user-visible changes.

Signed-off-by: Chris Phlipot <cphlipot0@gmail.com>
Acked-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1461831551-12213-3-git-send-email-cphlipot0@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Chris Phlipot and committed by
Arnaldo Carvalho de Melo
451db126 9919a65e

+204 -149
+1
tools/perf/util/Build
··· 74 74 libperf-y += data.o 75 75 libperf-y += tsc.o 76 76 libperf-y += cloexec.o 77 + libperf-y += call-path.o 77 78 libperf-y += thread-stack.o 78 79 libperf-$(CONFIG_AUXTRACE) += auxtrace.o 79 80 libperf-$(CONFIG_AUXTRACE) += intel-pt-decoder/
+122
tools/perf/util/call-path.c
··· 1 + /* 2 + * call-path.h: Manipulate a tree data structure containing function call paths 3 + * Copyright (c) 2014, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + * 14 + */ 15 + 16 + #include <linux/rbtree.h> 17 + #include <linux/list.h> 18 + 19 + #include "util.h" 20 + #include "call-path.h" 21 + 22 + static void call_path__init(struct call_path *cp, struct call_path *parent, 23 + struct symbol *sym, u64 ip, bool in_kernel) 24 + { 25 + cp->parent = parent; 26 + cp->sym = sym; 27 + cp->ip = sym ? 0 : ip; 28 + cp->db_id = 0; 29 + cp->in_kernel = in_kernel; 30 + RB_CLEAR_NODE(&cp->rb_node); 31 + cp->children = RB_ROOT; 32 + } 33 + 34 + struct call_path_root *call_path_root__new(void) 35 + { 36 + struct call_path_root *cpr; 37 + 38 + cpr = zalloc(sizeof(struct call_path_root)); 39 + if (!cpr) 40 + return NULL; 41 + call_path__init(&cpr->call_path, NULL, NULL, 0, false); 42 + INIT_LIST_HEAD(&cpr->blocks); 43 + return cpr; 44 + } 45 + 46 + void call_path_root__free(struct call_path_root *cpr) 47 + { 48 + struct call_path_block *pos, *n; 49 + 50 + list_for_each_entry_safe(pos, n, &cpr->blocks, node) { 51 + list_del(&pos->node); 52 + free(pos); 53 + } 54 + free(cpr); 55 + } 56 + 57 + static struct call_path *call_path__new(struct call_path_root *cpr, 58 + struct call_path *parent, 59 + struct symbol *sym, u64 ip, 60 + bool in_kernel) 61 + { 62 + struct call_path_block *cpb; 63 + struct call_path *cp; 64 + size_t n; 65 + 66 + if (cpr->next < cpr->sz) { 67 + cpb = list_last_entry(&cpr->blocks, struct call_path_block, 68 + node); 69 + } else { 70 + cpb = zalloc(sizeof(struct call_path_block)); 71 + if (!cpb) 72 + return NULL; 73 + list_add_tail(&cpb->node, &cpr->blocks); 74 + cpr->sz += CALL_PATH_BLOCK_SIZE; 75 + } 76 + 77 + n = cpr->next++ & CALL_PATH_BLOCK_MASK; 78 + cp = &cpb->cp[n]; 79 + 80 + call_path__init(cp, parent, sym, ip, in_kernel); 81 + 82 + return cp; 83 + } 84 + 85 + struct call_path *call_path__findnew(struct call_path_root *cpr, 86 + struct call_path *parent, 87 + struct symbol *sym, u64 ip, u64 ks) 88 + { 89 + struct rb_node **p; 90 + struct rb_node *node_parent = NULL; 91 + struct call_path *cp; 92 + bool in_kernel = ip >= ks; 93 + 94 + if (sym) 95 + ip = 0; 96 + 97 + if (!parent) 98 + return call_path__new(cpr, parent, sym, ip, in_kernel); 99 + 100 + p = &parent->children.rb_node; 101 + while (*p != NULL) { 102 + node_parent = *p; 103 + cp = rb_entry(node_parent, struct call_path, rb_node); 104 + 105 + if (cp->sym == sym && cp->ip == ip) 106 + return cp; 107 + 108 + if (sym < cp->sym || (sym == cp->sym && ip < cp->ip)) 109 + p = &(*p)->rb_left; 110 + else 111 + p = &(*p)->rb_right; 112 + } 113 + 114 + cp = call_path__new(cpr, parent, sym, ip, in_kernel); 115 + if (!cp) 116 + return NULL; 117 + 118 + rb_link_node(&cp->rb_node, node_parent, p); 119 + rb_insert_color(&cp->rb_node, &parent->children); 120 + 121 + return cp; 122 + }
+77
tools/perf/util/call-path.h
··· 1 + /* 2 + * call-path.h: Manipulate a tree data structure containing function call paths 3 + * Copyright (c) 2014, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + * 14 + */ 15 + 16 + #ifndef __PERF_CALL_PATH_H 17 + #define __PERF_CALL_PATH_H 18 + 19 + #include <sys/types.h> 20 + 21 + #include <linux/types.h> 22 + #include <linux/rbtree.h> 23 + 24 + /** 25 + * struct call_path - node in list of calls leading to a function call. 26 + * @parent: call path to the parent function call 27 + * @sym: symbol of function called 28 + * @ip: only if sym is null, the ip of the function 29 + * @db_id: id used for db-export 30 + * @in_kernel: whether function is a in the kernel 31 + * @rb_node: node in parent's tree of called functions 32 + * @children: tree of call paths of functions called 33 + * 34 + * In combination with the call_return structure, the call_path structure 35 + * defines a context-sensitve call-graph. 36 + */ 37 + struct call_path { 38 + struct call_path *parent; 39 + struct symbol *sym; 40 + u64 ip; 41 + u64 db_id; 42 + bool in_kernel; 43 + struct rb_node rb_node; 44 + struct rb_root children; 45 + }; 46 + 47 + #define CALL_PATH_BLOCK_SHIFT 8 48 + #define CALL_PATH_BLOCK_SIZE (1 << CALL_PATH_BLOCK_SHIFT) 49 + #define CALL_PATH_BLOCK_MASK (CALL_PATH_BLOCK_SIZE - 1) 50 + 51 + struct call_path_block { 52 + struct call_path cp[CALL_PATH_BLOCK_SIZE]; 53 + struct list_head node; 54 + }; 55 + 56 + /** 57 + * struct call_path_root - root of all call paths. 58 + * @call_path: root call path 59 + * @blocks: list of blocks to store call paths 60 + * @next: next free space 61 + * @sz: number of spaces 62 + */ 63 + struct call_path_root { 64 + struct call_path call_path; 65 + struct list_head blocks; 66 + size_t next; 67 + size_t sz; 68 + }; 69 + 70 + struct call_path_root *call_path_root__new(void); 71 + void call_path_root__free(struct call_path_root *cpr); 72 + 73 + struct call_path *call_path__findnew(struct call_path_root *cpr, 74 + struct call_path *parent, 75 + struct symbol *sym, u64 ip, u64 ks); 76 + 77 + #endif
+1
tools/perf/util/db-export.c
··· 23 23 #include "event.h" 24 24 #include "util.h" 25 25 #include "thread-stack.h" 26 + #include "call-path.h" 26 27 #include "db-export.h" 27 28 28 29 struct deferred_export {
+1
tools/perf/util/scripting-engines/trace-event-python.c
··· 41 41 #include "../thread-stack.h" 42 42 #include "../trace-event.h" 43 43 #include "../machine.h" 44 + #include "../call-path.h" 44 45 #include "thread_map.h" 45 46 #include "cpumap.h" 46 47 #include "stat.h"
+1 -125
tools/perf/util/thread-stack.c
··· 22 22 #include "debug.h" 23 23 #include "symbol.h" 24 24 #include "comm.h" 25 + #include "call-path.h" 25 26 #include "thread-stack.h" 26 - 27 - #define CALL_PATH_BLOCK_SHIFT 8 28 - #define CALL_PATH_BLOCK_SIZE (1 << CALL_PATH_BLOCK_SHIFT) 29 - #define CALL_PATH_BLOCK_MASK (CALL_PATH_BLOCK_SIZE - 1) 30 - 31 - struct call_path_block { 32 - struct call_path cp[CALL_PATH_BLOCK_SIZE]; 33 - struct list_head node; 34 - }; 35 - 36 - /** 37 - * struct call_path_root - root of all call paths. 38 - * @call_path: root call path 39 - * @blocks: list of blocks to store call paths 40 - * @next: next free space 41 - * @sz: number of spaces 42 - */ 43 - struct call_path_root { 44 - struct call_path call_path; 45 - struct list_head blocks; 46 - size_t next; 47 - size_t sz; 48 - }; 49 27 50 28 /** 51 29 * struct call_return_processor - provides a call-back to consume call-return ··· 311 333 312 334 for (i = 1; i < chain->nr; i++) 313 335 chain->ips[i] = thread->ts->stack[thread->ts->cnt - i].ret_addr; 314 - } 315 - 316 - static void call_path__init(struct call_path *cp, struct call_path *parent, 317 - struct symbol *sym, u64 ip, bool in_kernel) 318 - { 319 - cp->parent = parent; 320 - cp->sym = sym; 321 - cp->ip = sym ? 0 : ip; 322 - cp->db_id = 0; 323 - cp->in_kernel = in_kernel; 324 - RB_CLEAR_NODE(&cp->rb_node); 325 - cp->children = RB_ROOT; 326 - } 327 - 328 - static struct call_path_root *call_path_root__new(void) 329 - { 330 - struct call_path_root *cpr; 331 - 332 - cpr = zalloc(sizeof(struct call_path_root)); 333 - if (!cpr) 334 - return NULL; 335 - call_path__init(&cpr->call_path, NULL, NULL, 0, false); 336 - INIT_LIST_HEAD(&cpr->blocks); 337 - return cpr; 338 - } 339 - 340 - static void call_path_root__free(struct call_path_root *cpr) 341 - { 342 - struct call_path_block *pos, *n; 343 - 344 - list_for_each_entry_safe(pos, n, &cpr->blocks, node) { 345 - list_del(&pos->node); 346 - free(pos); 347 - } 348 - free(cpr); 349 - } 350 - 351 - static struct call_path *call_path__new(struct call_path_root *cpr, 352 - struct call_path *parent, 353 - struct symbol *sym, u64 ip, 354 - bool in_kernel) 355 - { 356 - struct call_path_block *cpb; 357 - struct call_path *cp; 358 - size_t n; 359 - 360 - if (cpr->next < cpr->sz) { 361 - cpb = list_last_entry(&cpr->blocks, struct call_path_block, 362 - node); 363 - } else { 364 - cpb = zalloc(sizeof(struct call_path_block)); 365 - if (!cpb) 366 - return NULL; 367 - list_add_tail(&cpb->node, &cpr->blocks); 368 - cpr->sz += CALL_PATH_BLOCK_SIZE; 369 - } 370 - 371 - n = cpr->next++ & CALL_PATH_BLOCK_MASK; 372 - cp = &cpb->cp[n]; 373 - 374 - call_path__init(cp, parent, sym, ip, in_kernel); 375 - 376 - return cp; 377 - } 378 - 379 - static struct call_path *call_path__findnew(struct call_path_root *cpr, 380 - struct call_path *parent, 381 - struct symbol *sym, u64 ip, u64 ks) 382 - { 383 - struct rb_node **p; 384 - struct rb_node *node_parent = NULL; 385 - struct call_path *cp; 386 - bool in_kernel = ip >= ks; 387 - 388 - if (sym) 389 - ip = 0; 390 - 391 - if (!parent) 392 - return call_path__new(cpr, parent, sym, ip, in_kernel); 393 - 394 - p = &parent->children.rb_node; 395 - while (*p != NULL) { 396 - node_parent = *p; 397 - cp = rb_entry(node_parent, struct call_path, rb_node); 398 - 399 - if (cp->sym == sym && cp->ip == ip) 400 - return cp; 401 - 402 - if (sym < cp->sym || (sym == cp->sym && ip < cp->ip)) 403 - p = &(*p)->rb_left; 404 - else 405 - p = &(*p)->rb_right; 406 - } 407 - 408 - cp = call_path__new(cpr, parent, sym, ip, in_kernel); 409 - if (!cp) 410 - return NULL; 411 - 412 - rb_link_node(&cp->rb_node, node_parent, p); 413 - rb_insert_color(&cp->rb_node, &parent->children); 414 - 415 - return cp; 416 336 } 417 337 418 338 struct call_return_processor *
+1 -24
tools/perf/util/thread-stack.h
··· 19 19 #include <sys/types.h> 20 20 21 21 #include <linux/types.h> 22 - #include <linux/rbtree.h> 23 22 24 23 struct thread; 25 24 struct comm; ··· 29 30 struct comm; 30 31 struct perf_sample; 31 32 struct addr_location; 33 + struct call_path; 32 34 33 35 /* 34 36 * Call/Return flags. ··· 66 66 u64 return_ref; 67 67 u64 db_id; 68 68 u32 flags; 69 - }; 70 - 71 - /** 72 - * struct call_path - node in list of calls leading to a function call. 73 - * @parent: call path to the parent function call 74 - * @sym: symbol of function called 75 - * @ip: only if sym is null, the ip of the function 76 - * @db_id: id used for db-export 77 - * @in_kernel: whether function is a in the kernel 78 - * @rb_node: node in parent's tree of called functions 79 - * @children: tree of call paths of functions called 80 - * 81 - * In combination with the call_return structure, the call_path structure 82 - * defines a context-sensitve call-graph. 83 - */ 84 - struct call_path { 85 - struct call_path *parent; 86 - struct symbol *sym; 87 - u64 ip; 88 - u64 db_id; 89 - bool in_kernel; 90 - struct rb_node rb_node; 91 - struct rb_root children; 92 69 }; 93 70 94 71 int thread_stack__event(struct thread *thread, u32 flags, u64 from_ip,