call-path.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * call-path.h: Manipulate a tree data structure containing function call paths
  4. * Copyright (c) 2014, Intel Corporation.
  5. */
  6. #include <linux/rbtree.h>
  7. #include <linux/list.h>
  8. #include <linux/zalloc.h>
  9. #include <stdlib.h>
  10. #include "call-path.h"
  11. static void call_path__init(struct call_path *cp, struct call_path *parent,
  12. struct symbol *sym, u64 ip, bool in_kernel)
  13. {
  14. cp->parent = parent;
  15. cp->sym = sym;
  16. cp->ip = sym ? 0 : ip;
  17. cp->db_id = 0;
  18. cp->in_kernel = in_kernel;
  19. RB_CLEAR_NODE(&cp->rb_node);
  20. cp->children = RB_ROOT;
  21. }
  22. struct call_path_root *call_path_root__new(void)
  23. {
  24. struct call_path_root *cpr;
  25. cpr = zalloc(sizeof(struct call_path_root));
  26. if (!cpr)
  27. return NULL;
  28. call_path__init(&cpr->call_path, NULL, NULL, 0, false);
  29. INIT_LIST_HEAD(&cpr->blocks);
  30. return cpr;
  31. }
  32. void call_path_root__free(struct call_path_root *cpr)
  33. {
  34. struct call_path_block *pos, *n;
  35. list_for_each_entry_safe(pos, n, &cpr->blocks, node) {
  36. list_del_init(&pos->node);
  37. free(pos);
  38. }
  39. free(cpr);
  40. }
  41. static struct call_path *call_path__new(struct call_path_root *cpr,
  42. struct call_path *parent,
  43. struct symbol *sym, u64 ip,
  44. bool in_kernel)
  45. {
  46. struct call_path_block *cpb;
  47. struct call_path *cp;
  48. size_t n;
  49. if (cpr->next < cpr->sz) {
  50. cpb = list_last_entry(&cpr->blocks, struct call_path_block,
  51. node);
  52. } else {
  53. cpb = zalloc(sizeof(struct call_path_block));
  54. if (!cpb)
  55. return NULL;
  56. list_add_tail(&cpb->node, &cpr->blocks);
  57. cpr->sz += CALL_PATH_BLOCK_SIZE;
  58. }
  59. n = cpr->next++ & CALL_PATH_BLOCK_MASK;
  60. cp = &cpb->cp[n];
  61. call_path__init(cp, parent, sym, ip, in_kernel);
  62. return cp;
  63. }
  64. struct call_path *call_path__findnew(struct call_path_root *cpr,
  65. struct call_path *parent,
  66. struct symbol *sym, u64 ip, u64 ks)
  67. {
  68. struct rb_node **p;
  69. struct rb_node *node_parent = NULL;
  70. struct call_path *cp;
  71. bool in_kernel = ip >= ks;
  72. if (sym)
  73. ip = 0;
  74. if (!parent)
  75. return call_path__new(cpr, parent, sym, ip, in_kernel);
  76. p = &parent->children.rb_node;
  77. while (*p != NULL) {
  78. node_parent = *p;
  79. cp = rb_entry(node_parent, struct call_path, rb_node);
  80. if (cp->sym == sym && cp->ip == ip)
  81. return cp;
  82. if (sym < cp->sym || (sym == cp->sym && ip < cp->ip))
  83. p = &(*p)->rb_left;
  84. else
  85. p = &(*p)->rb_right;
  86. }
  87. cp = call_path__new(cpr, parent, sym, ip, in_kernel);
  88. if (!cp)
  89. return NULL;
  90. rb_link_node(&cp->rb_node, node_parent, p);
  91. rb_insert_color(&cp->rb_node, &parent->children);
  92. return cp;
  93. }