Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * call-path.h: Manipulate a tree data structure containing function call paths
0004  * Copyright (c) 2014, Intel Corporation.
0005  */
0006 
0007 #include <linux/rbtree.h>
0008 #include <linux/list.h>
0009 #include <linux/zalloc.h>
0010 #include <stdlib.h>
0011 
0012 #include "call-path.h"
0013 
0014 static void call_path__init(struct call_path *cp, struct call_path *parent,
0015                 struct symbol *sym, u64 ip, bool in_kernel)
0016 {
0017     cp->parent = parent;
0018     cp->sym = sym;
0019     cp->ip = sym ? 0 : ip;
0020     cp->db_id = 0;
0021     cp->in_kernel = in_kernel;
0022     RB_CLEAR_NODE(&cp->rb_node);
0023     cp->children = RB_ROOT;
0024 }
0025 
0026 struct call_path_root *call_path_root__new(void)
0027 {
0028     struct call_path_root *cpr;
0029 
0030     cpr = zalloc(sizeof(struct call_path_root));
0031     if (!cpr)
0032         return NULL;
0033     call_path__init(&cpr->call_path, NULL, NULL, 0, false);
0034     INIT_LIST_HEAD(&cpr->blocks);
0035     return cpr;
0036 }
0037 
0038 void call_path_root__free(struct call_path_root *cpr)
0039 {
0040     struct call_path_block *pos, *n;
0041 
0042     list_for_each_entry_safe(pos, n, &cpr->blocks, node) {
0043         list_del_init(&pos->node);
0044         free(pos);
0045     }
0046     free(cpr);
0047 }
0048 
0049 static struct call_path *call_path__new(struct call_path_root *cpr,
0050                     struct call_path *parent,
0051                     struct symbol *sym, u64 ip,
0052                     bool in_kernel)
0053 {
0054     struct call_path_block *cpb;
0055     struct call_path *cp;
0056     size_t n;
0057 
0058     if (cpr->next < cpr->sz) {
0059         cpb = list_last_entry(&cpr->blocks, struct call_path_block,
0060                       node);
0061     } else {
0062         cpb = zalloc(sizeof(struct call_path_block));
0063         if (!cpb)
0064             return NULL;
0065         list_add_tail(&cpb->node, &cpr->blocks);
0066         cpr->sz += CALL_PATH_BLOCK_SIZE;
0067     }
0068 
0069     n = cpr->next++ & CALL_PATH_BLOCK_MASK;
0070     cp = &cpb->cp[n];
0071 
0072     call_path__init(cp, parent, sym, ip, in_kernel);
0073 
0074     return cp;
0075 }
0076 
0077 struct call_path *call_path__findnew(struct call_path_root *cpr,
0078                      struct call_path *parent,
0079                      struct symbol *sym, u64 ip, u64 ks)
0080 {
0081     struct rb_node **p;
0082     struct rb_node *node_parent = NULL;
0083     struct call_path *cp;
0084     bool in_kernel = ip >= ks;
0085 
0086     if (sym)
0087         ip = 0;
0088 
0089     if (!parent)
0090         return call_path__new(cpr, parent, sym, ip, in_kernel);
0091 
0092     p = &parent->children.rb_node;
0093     while (*p != NULL) {
0094         node_parent = *p;
0095         cp = rb_entry(node_parent, struct call_path, rb_node);
0096 
0097         if (cp->sym == sym && cp->ip == ip)
0098             return cp;
0099 
0100         if (sym < cp->sym || (sym == cp->sym && ip < cp->ip))
0101             p = &(*p)->rb_left;
0102         else
0103             p = &(*p)->rb_right;
0104     }
0105 
0106     cp = call_path__new(cpr, parent, sym, ip, in_kernel);
0107     if (!cp)
0108         return NULL;
0109 
0110     rb_link_node(&cp->rb_node, node_parent, p);
0111     rb_insert_color(&cp->rb_node, &parent->children);
0112 
0113     return cp;
0114 }