call-path.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <linux/rbtree.h>
  16. #include <linux/list.h>
  17. #include "util.h"
  18. #include "call-path.h"
  19. static void call_path__init(struct call_path *cp, struct call_path *parent,
  20. struct symbol *sym, u64 ip, bool in_kernel)
  21. {
  22. cp->parent = parent;
  23. cp->sym = sym;
  24. cp->ip = sym ? 0 : ip;
  25. cp->db_id = 0;
  26. cp->in_kernel = in_kernel;
  27. RB_CLEAR_NODE(&cp->rb_node);
  28. cp->children = RB_ROOT;
  29. }
  30. struct call_path_root *call_path_root__new(void)
  31. {
  32. struct call_path_root *cpr;
  33. cpr = zalloc(sizeof(struct call_path_root));
  34. if (!cpr)
  35. return NULL;
  36. call_path__init(&cpr->call_path, NULL, NULL, 0, false);
  37. INIT_LIST_HEAD(&cpr->blocks);
  38. return cpr;
  39. }
  40. void call_path_root__free(struct call_path_root *cpr)
  41. {
  42. struct call_path_block *pos, *n;
  43. list_for_each_entry_safe(pos, n, &cpr->blocks, node) {
  44. list_del(&pos->node);
  45. free(pos);
  46. }
  47. free(cpr);
  48. }
  49. static struct call_path *call_path__new(struct call_path_root *cpr,
  50. struct call_path *parent,
  51. struct symbol *sym, u64 ip,
  52. bool in_kernel)
  53. {
  54. struct call_path_block *cpb;
  55. struct call_path *cp;
  56. size_t n;
  57. if (cpr->next < cpr->sz) {
  58. cpb = list_last_entry(&cpr->blocks, struct call_path_block,
  59. node);
  60. } else {
  61. cpb = zalloc(sizeof(struct call_path_block));
  62. if (!cpb)
  63. return NULL;
  64. list_add_tail(&cpb->node, &cpr->blocks);
  65. cpr->sz += CALL_PATH_BLOCK_SIZE;
  66. }
  67. n = cpr->next++ & CALL_PATH_BLOCK_MASK;
  68. cp = &cpb->cp[n];
  69. call_path__init(cp, parent, sym, ip, in_kernel);
  70. return cp;
  71. }
  72. struct call_path *call_path__findnew(struct call_path_root *cpr,
  73. struct call_path *parent,
  74. struct symbol *sym, u64 ip, u64 ks)
  75. {
  76. struct rb_node **p;
  77. struct rb_node *node_parent = NULL;
  78. struct call_path *cp;
  79. bool in_kernel = ip >= ks;
  80. if (sym)
  81. ip = 0;
  82. if (!parent)
  83. return call_path__new(cpr, parent, sym, ip, in_kernel);
  84. p = &parent->children.rb_node;
  85. while (*p != NULL) {
  86. node_parent = *p;
  87. cp = rb_entry(node_parent, struct call_path, rb_node);
  88. if (cp->sym == sym && cp->ip == ip)
  89. return cp;
  90. if (sym < cp->sym || (sym == cp->sym && ip < cp->ip))
  91. p = &(*p)->rb_left;
  92. else
  93. p = &(*p)->rb_right;
  94. }
  95. cp = call_path__new(cpr, parent, sym, ip, in_kernel);
  96. if (!cp)
  97. return NULL;
  98. rb_link_node(&cp->rb_node, node_parent, p);
  99. rb_insert_color(&cp->rb_node, &parent->children);
  100. return cp;
  101. }