HPCToolkit
cct.h
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // A variable degree-tree for storing call stack samples. Each node
54 // may have zero or more children and each node contains a single
55 // instruction pointer value. Call stack samples are represented
56 // implicitly by a path from some node x (where x may or may not be
57 // a leaf node) to the tree root (with the root being the bottom of
58 // the call stack).
59 //
60 // The basic tree functionality is based on NonUniformDegreeTree.h/C
61 // from HPCView/HPCTools.
62 //
63 // Description:
64 // [The set of functions, macros, etc. defined in the file]
65 //
66 //***************************************************************************
67 
68 #ifndef cct_h
69 #define cct_h
70 
71 //************************* System Include Files ****************************
72 
73 #include <stdio.h>
74 #include <stddef.h>
75 #include <stdbool.h>
76 #include <ucontext.h>
77 
78 #include <assert.h>
79 
80 //*************************** User Include Files ****************************
81 
82 #include <hpcrun/metrics.h>
83 
85 
87 
88 #include <lib/prof-lean/hpcio.h>
89 #include <lib/prof-lean/hpcfmt.h>
91 
92 #include "cct_addr.h"
93 
94 //*************************** Macros ****************************
95 
96 //
97 // Readability Macros (to facilitate coding initialization operations)
98 //
99 #define CCT_ROOT HPCRUN_FMT_LMId_NULL, HPCRUN_FMT_LMIp_NULL
100 #define PARTIAL_ROOT HPCRUN_FMT_LMId_NULL, HPCRUN_FMT_LMIp_Flag1
101 #define ADDR_I(L) NON_LUSH_ADDR_INI(L)
102 #define ADDR(L) (cct_addr_t) NON_LUSH_ADDR_INI(L)
103 #define ADDR2_I(id, ip) NON_LUSH_ADDR_INI(id, ip)
104 #define ADDR2(id, ip) (cct_addr_t) ADDR2_I(id, ip)
105 //***************************************************************************
106 // Calling context tree node (abstract data type)
107 //***************************************************************************
108 
109 typedef struct cct_node_t cct_node_t;
110 //
111 // In order to associate data with a given calling context,
112 // cct nodes need an id type (abstract)
113 //
114 
116 
117 //
118 // Interface procedures
119 //
120 
121 //
122 // Constructors
123 //
124 extern cct_node_t* hpcrun_cct_new(void);
125 extern cct_node_t* hpcrun_cct_new_partial(void);
126 extern cct_node_t* hpcrun_cct_new_special(void* addr);
127 extern cct_node_t* hpcrun_cct_top_new(uint16_t lmid, uintptr_t lmip);
128 //
129 // Accessor functions
130 //
131 
133 extern int32_t hpcrun_cct_persistent_id(cct_node_t* node);
135 extern bool hpcrun_cct_is_leaf(cct_node_t* node);
136 //
137 // NOTE: having no children is not exactly the same as being a leaf
138 // A leaf represents a full path. There might be full paths
139 // that are a prefix of other full paths. So, a "leaf" can have children
140 //
142 extern bool hpcrun_cct_is_root(cct_node_t* node);
143 
144 //
145 // Mutator functions: modify a given cct
146 //
147 
148 //
149 // Fundamental mutation operation: insert a given addr into the
150 // set of children of a given cct node. Return the cct_node corresponding
151 // to the inserted addr [NB: if the addr is already in the node children, then
152 // the already present node is returned. Otherwise, a new node is created, linked in,
153 // and returned]
154 //
156 
157 //
158 // 2nd fundamental mutator: mark a node as "terminal". That is,
159 // it is the last node of a path
160 //
162 //
163 // Special purpose mutator:
164 // This operation is somewhat akin to concatenation.
165 // An already constructed cct ('src') is inserted as a
166 // child of the 'target' cct. The addr field of the src
167 // cct is ASSUMED TO BE DIFFERENT FROM ANY ADDR IN target's
168 // child set. [Otherwise something recursive has to happen]
169 //
170 //
172 
173 extern void hpcrun_cct_insert_path(cct_node_t ** root, cct_node_t* path);
174 
175 // mark a node for retention as the leaf of a traced call path.
176 extern void hpcrun_cct_retain(cct_node_t* x);
177 
178 // check if a node was marked for retention as the leaf of a traced
179 // call path.
180 extern int hpcrun_cct_retained(cct_node_t* x);
181 
182 
183 // Walking functions section:
184 //
185 // typedefs for walking functions
186 typedef void* cct_op_arg_t;
187 typedef void (*cct_op_t)(cct_node_t* cct, cct_op_arg_t arg, size_t level);
188 
189 //
190 // general walking functions: (client may select starting level)
191 // visits every node in the cct, calling op(node, arg, level)
192 // level has property that node at level n ==> children at level n+1
193 // there are 2 different walking strategies:
194 // 1) walk the children, then walk the node
195 // 2) walk the node, then walk the children
196 // there is no implied children ordering
197 //
198 
199 //
200 // visting order: children first, then node
201 //
203  cct_op_t op,
204  cct_op_arg_t arg, size_t level);
205 
206 //
207 // visting order: node first, then children
208 //
210  cct_op_t op,
211  cct_op_arg_t arg, size_t level);
212 //
213 // Top level walking routines:
214 // frequently, a walk will start with level = 0
215 // the static inline routines below implement this utility
216 //
217 
218 static inline
220  cct_op_t op, cct_op_arg_t arg)
221 {
222  hpcrun_cct_walk_child_1st_w_level(cct, op, arg, 0);
223 }
224 
225 static inline
227  cct_op_t op, cct_op_arg_t arg)
228 {
229  hpcrun_cct_walk_node_1st_w_level(cct, op, arg, 0);
230 }
231 
232 
233 //
234 // Special routine to walk a path represented by a cct node.
235 // The actual path represented by a node is list reversal of the nodes
236 // linked by the parent link. So walking a path involves touching the
237 // path nodes in list reverse order
238 //
239 extern void hpcrun_walk_path(cct_node_t* node, cct_op_t op, cct_op_arg_t arg);
240 
241 //
242 // utility walker for cct sets (part of the substructure of a cct)
243 //
244 extern void hpcrun_cct_walkset(cct_node_t* cct, cct_op_t fn, cct_op_arg_t arg);
245 //
246 // Writing operation
247 //
248 // cct2metrics_t is defined in cct2metrics.h but we cannot include this header
249 // beceause cct2metrics.h includes this file (cct.h)
250 //
251 // TODO: need to declare cct2metrics_t here to avoid to cyclic inclusion
253 
254 int hpcrun_cct_fwrite(cct2metrics_t* cct2metrics_map,
255  cct_node_t* cct, FILE* fs, epoch_flags_t flags);
256 //
257 // Utilities
258 //
259 extern size_t hpcrun_cct_num_nodes(cct_node_t* cct);
260 //
261 // look up addr in the set of cct's children
262 // return the found node or NULL
263 //
265 //
266 // Merging operation: Given 2 ccts : CCT_A, CCT_B,
267 // merge means add all paths in CCT_B that are NOT in CCT_A
268 // to CCT_A. For paths that are common, perform the merge operation on
269 // each common node, using auxiliary arg merge_arg
270 //
271 // NOTE: this merge operation presumes
272 // cct_addr_data(CCT_A) == cct_addr_data(CCT_B)
273 //
274 typedef void* merge_op_arg_t;
275 typedef void (*merge_op_t)(cct_node_t* a, cct_node_t*b, merge_op_arg_t arg);
276 
277 extern void hpcrun_cct_merge(cct_node_t* cct_a, cct_node_t* cct_b,
278  merge_op_t merge, merge_op_arg_t arg);
279 
280 
281 cct_node_t*
283 
284 cct_node_t*
286 
287 cct_node_t *
289 
290 
291 // --------------------------------------------------------------
292 // variable address
293 // --------------------------------------------------------------
294 
295 void
296 hpcrun_cct_var_add(cct_node_t *node_source, void *start, cct_node_t *node_target);
297 
298 bool
300 
301 
302 // --------------------------------------------------------------
303 // Node types get and set
304 // --------------------------------------------------------------
305 
306 void
308 
309 bool
311 
312 void
314 
315 bool
317 
318 void
320 
321 bool
323 
324 void
326 
327 bool
329 
330 #endif // cct_h
cct_node_t * hpcrun_cct_new(void)
Definition: cct.c:326
int32_t hpcrun_cct_persistent_id(cct_node_t *node)
Definition: cct.c:363
bool hpcrun_cct_no_children(cct_node_t *node)
Definition: cct.c:381
bool hpcrun_cct_is_node_allocation(cct_node_t *node)
Definition: cct.c:474
void hpcrun_cct_set_node_variable(cct_node_t *node)
Definition: cct.c:480
cct_node_t * hpcrun_cct_new_partial(void)
Definition: cct.c:332
void hpcrun_walk_path(cct_node_t *node, cct_op_t op, cct_op_arg_t arg)
Definition: cct.c:636
int hpcrun_cct_retained(cct_node_t *x)
Definition: cct.c:576
void hpcrun_cct_set_node_memaccess(cct_node_t *node)
Definition: cct.c:495
cct_node_t * hpcrun_cct_insert_addr(cct_node_t *cct, cct_addr_t *addr)
Definition: cct.c:405
void hpcrun_cct_set_node_root(cct_node_t *root)
Definition: cct.c:513
bool hpcrun_cct_is_node_root(cct_node_t *node)
Definition: cct.c:520
bool hpcrun_cct_var_static(cct_node_t *node)
bool hpcrun_cct_is_node_memaccess(cct_node_t *node)
Definition: cct.c:504
cct_addr_t * hpcrun_cct_addr(cct_node_t *node)
Definition: cct.c:369
cct_node_t * node
Definition: cct.c:128
bool hpcrun_cct_is_leaf(cct_node_t *node)
Definition: cct.c:458
cct_node_t * cct_node_id_t
Definition: cct.h:115
void hpcrun_cct_retain(cct_node_t *x)
Definition: cct.c:567
cct_node_t * hpcrun_cct_find_addr(cct_node_t *cct, cct_addr_t *addr)
Definition: cct.c:719
void hpcrun_cct_walk_child_1st_w_level(cct_node_t *cct, cct_op_t op, cct_op_arg_t arg, size_t level)
Definition: cct.c:599
cct_node_t * hpcrun_cct_insert_path_return_leaf(cct_node_t *path, cct_node_t *root)
Definition: cct.c:871
void * cct_op_arg_t
Definition: cct.h:186
size_t hpcrun_cct_num_nodes(cct_node_t *cct)
Definition: cct.c:707
cct_node_t * hpcrun_cct_new_special(void *addr)
Definition: cct.c:338
void hpcrun_cct_set_node_allocation(cct_node_t *node)
Definition: cct.c:468
void hpcrun_cct_walkset(cct_node_t *cct, cct_op_t fn, cct_op_arg_t arg)
Definition: cct.c:623
cct_node_t * hpcrun_cct_parent(cct_node_t *node)
Definition: cct.c:357
cct_node_t * hpcrun_cct_insert_node(cct_node_t *target, cct_node_t *src)
Definition: cct.c:537
bool hpcrun_cct_is_node_variable(cct_node_t *node)
Definition: cct.c:486
void * merge_op_arg_t
Definition: cct.h:274
cct_node_t * hpcrun_cct_top_new(uint16_t lmid, uintptr_t lmip)
Definition: cct.c:348
void hpcrun_cct_walk_node_1st_w_level(cct_node_t *cct, cct_op_t op, cct_op_arg_t arg, size_t level)
Definition: cct.c:611
void(* merge_op_t)(cct_node_t *a, cct_node_t *b, merge_op_arg_t arg)
Definition: cct.h:275
void hpcrun_cct_merge(cct_node_t *cct_a, cct_node_t *cct_b, merge_op_t merge, merge_op_arg_t arg)
Definition: cct.c:769
cct_node_t * hpcrun_cct_get_root(cct_node_t *node)
Definition: cct.c:882
static void hpcrun_cct_walk_child_1st(cct_node_t *cct, cct_op_t op, cct_op_arg_t arg)
Definition: cct.h:219
void hpcrun_cct_insert_path(cct_node_t **root, cct_node_t *path)
Definition: cct.c:662
void hpcrun_cct_terminate_path(cct_node_t *node)
Definition: cct.c:452
Definition: cct.c:96
cct_addr_t * addr
Definition: cct.c:130
cct_node_t * hpcrun_insert_special_node(cct_node_t *root, void *addr)
Definition: cct.c:861
bool hpcrun_cct_is_root(cct_node_t *node)
Definition: cct.c:387
void hpcrun_cct_var_add(cct_node_t *node_source, void *start, cct_node_t *node_target)
void(* cct_op_t)(cct_node_t *cct, cct_op_arg_t arg, size_t level)
Definition: cct.h:187
int hpcrun_cct_fwrite(cct2metrics_t *cct2metrics_map, cct_node_t *cct, FILE *fs, epoch_flags_t flags)
Definition: cct.c:672
static void hpcrun_cct_walk_node_1st(cct_node_t *cct, cct_op_t op, cct_op_arg_t arg)
Definition: cct.h:226