HPCToolkit
cct2metrics.c
Go to the documentation of this file.
1 //
2 // cct_node -> metrics map
3 //
4 #include <stdbool.h>
5 #include <string.h>
6 #include <stdlib.h>
7 
8 #include <messages/messages.h>
9 #include <memory/hpcrun-malloc.h>
10 #include <hpcrun/metrics.h>
11 #include <cct/cct.h>
12 #include <hpcrun/cct2metrics.h>
13 #include <hpcrun/thread_data.h>
15 
16 
17 //
18 // ***** The splay tree node *****
19 //
20 struct cct2metrics_t {
23  //
24  // left and right pointers for splay tree of siblings
25  //
28 };
29 
30 
31 
32 //
33 // ******** Local Data ***********
34 //
35 // There is 1 cct2metric map per thread. The public
36 // interface functions implicitly reference this map
37 //
38 
39 #define THREAD_LOCAL_MAP() TD_GET(core_profile_trace_data.cct2metrics_map)
40 
41 //
42 // ******** initialization
43 //
44 // The cct2metrics map gets explicit reference
45 // only at initialization time.
46 //
47 // Thereafter, the interface ops refer to it
48 // implicitly.
49 // [Singleton pattern for GangOf4 enthusiasts]
50 
51 void
53 {
54  TMSG(CCT2METRICS, "Init, map = %p", *map);
55  *map = NULL;
56 }
57 //
58 // ******* Internal operations: **********
59 // mapping implemented as a splay tree
60 //
61 
62 void
64 {
65  if (! tree) return;
66 
67  help_splay_tree_dump(tree->right, indent+2);
68 
69  char buf[300];
70  for (int i=0; i<indent; i++) buf[i] = ' ';
71  buf[indent] = '\0';
72  snprintf(buf+indent, sizeof(buf)-(indent+1), "%p", tree->node);
73  TMSG(CCT2METRICS, "%s", buf);
74 
75  help_splay_tree_dump(tree->left, indent+2);
76 }
77 
78 void
80 {
81  TMSG(CCT2METRICS, "Splay tree %p appears below", map);
82  help_splay_tree_dump(map, 0);
83 }
84 
85 
86 static cct2metrics_t*
88 {
89  TMSG(CCT2METRICS, "splay map = %p, node = %p", map, node);
90  REGULAR_SPLAY_TREE(cct2metrics_t, map, node, node, left, right);
91  TMSG(CCT2METRICS, "new map = %p, top node = %p", map, map->node);
92  return map;
93 }
94 
95 static cct2metrics_t*
97 {
99  rv->node = node;
100  rv->metrics = metrics;
101  rv->left = rv->right = NULL;
102  TMSG(CCT2METRICS, "Node: %p, Metrics: %p", rv->node, rv->metrics);
103  return rv;
104 }
105 // ******** Interface operations **********
106 //
107 // for a given cct node, return the metric set
108 // associated with it.
109 //
110 // if there are no metrics for the node, then
111 // create a metric set, and return it.
112 //
113 
116 {
117  TMSG(CCT2METRICS, "REIFY: %p", cct_id);
118  metric_set_t* rv = hpcrun_get_metric_set(cct_id);
119  TMSG(CCT2METRICS, " -- Metric set found = %p", rv);
120 
121  if (rv) return rv;
122 
123  TMSG(CCT2METRICS, " -- Metric set was null, allocating new metric set");
125  TMSG(CCT2METRICS, "REIFY returns %p", rv);
126  return rv;
127 }
128 
129 //
130 // get metric set for a node (NULL return value means no metrics associated).
131 // this function requires map between cct and metrics. If the map is null
132 // it uses the default local map.
133 //
136 {
137  cct2metrics_t *current_map = map ? *map : THREAD_LOCAL_MAP();
138 
139  TMSG(CCT2METRICS, "GET_METRIC_SET for %p, using map %p", cct_id, map);
140  if (! current_map) return NULL;
141 
142  current_map = splay(current_map, cct_id);
143 
144  if (map)
145  *map = current_map;
146  else
147  THREAD_LOCAL_MAP() = current_map;
148 
149  TMSG(CCT2METRICS, " -- After Splay map = %p", cct_id, map);
150 
151  if (current_map->node == cct_id) {
152  TMSG(CCT2METRICS, " -- found %p, returning metrics", current_map->node);
153  return current_map->metrics;
154  }
155  TMSG(CCT2METRICS, " -- cct_id NOT, found. Return NULL");
156  return NULL;
157 }
158 
159 //
160 // get metric set for a node (NULL return value means no metrics associated).
161 //
164 {
165  return hpcrun_get_metric_set_specific(NULL, cct_id);
166 }
167 
168 //
169 // check to see if node already has metrics
170 //
171 bool
173 {
174  return (hpcrun_get_metric_set(cct_id) != NULL);
175 }
176 
177 //
178 // associate a metric set with a cct node
179 //
180 void
182 {
184  TMSG(CCT2METRICS, "CCT2METRICS_ASSOC for %p, using map %p", node, map);
185  if (! map) {
186  map = cct2metrics_new(node, metrics);
187  TMSG(CCT2METRICS, " -- new map created: %p", map);
188  }
189  else {
190  cct2metrics_t* new = cct2metrics_new(node, metrics);
191  map = splay(map, node);
192  TMSG(CCT2METRICS, " -- map after splay = %p,"
193  " node sought = %p, mapnode = %p", map, node, map->node);
194  if (map->node == node) {
195  EMSG("CCT2METRICS map assoc invariant violated");
196  }
197  else {
198  if (map->node < node) {
199  TMSG(CCT2METRICS, " -- less-than insert %p < %p", map->node, node);
200  new->left = map;
201  new->right = map->right;
202  map->right = NULL;
203  }
204  else {
205  TMSG(CCT2METRICS, " -- greater-than insert %p > %p", map->node, node);
206  new->left = map->left;
207  new->right = map;
208  map->left = NULL;
209  }
210  map = new;
211  TMSG(CCT2METRICS, " -- new map after insertion %p.(%p, %p)", map->node, map->left, map->right);
212  }
213  }
214  THREAD_LOCAL_MAP() = map;
215  TMSG(CCT2METRICS, "METRICS_ASSOC final, THREAD_LOCAL_MAP = %p", THREAD_LOCAL_MAP());
216  if (ENABLED(CCT2METRICS)) splay_tree_dump(THREAD_LOCAL_MAP());
217 }
void hpcrun_cct2metrics_init(cct2metrics_t **map)
Definition: cct2metrics.c:52
metric_set_t * hpcrun_reify_metric_set(cct_node_id_t cct_id)
Definition: cct2metrics.c:115
bool hpcrun_has_metric_set(cct_node_id_t cct_id)
Definition: cct2metrics.c:172
static cct2metrics_t * splay(cct2metrics_t *map, cct_node_id_t node)
Definition: cct2metrics.c:87
Definition: fmt.c:108
struct cct2metrics_t * right
Definition: cct2metrics.c:26
void splay_tree_dump(cct2metrics_t *map)
Definition: cct2metrics.c:79
#define THREAD_LOCAL_MAP()
Definition: cct2metrics.c:39
#define REGULAR_SPLAY_TREE(type, root, key, value, left, right)
Definition: splay-macros.h:172
void cct2metrics_assoc(cct_node_id_t node, metric_set_t *metrics)
Definition: cct2metrics.c:181
#define EMSG
Definition: messages.h:70
static cct2metrics_t * cct2metrics_new(cct_node_id_t node, metric_set_t *metrics)
Definition: cct2metrics.c:96
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
metric_set_t * hpcrun_get_metric_set_specific(cct2metrics_t **map, cct_node_id_t cct_id)
Definition: cct2metrics.c:135
metric_set_t * metrics
Definition: cct2metrics.c:22
#define TMSG(f,...)
Definition: messages.h:93
metric_set_t * hpcrun_get_metric_set(cct_node_id_t cct_id)
Definition: cct2metrics.c:163
#define NULL
Definition: ElfHelper.cpp:85
Definition: cct.c:96
metric_set_t * hpcrun_metric_set_new(void)
Definition: metrics.c:456
cct_node_id_t node
Definition: cct2metrics.c:21
void help_splay_tree_dump(cct2metrics_t *tree, int indent)
Definition: cct2metrics.c:63
bitree_uwi_t * tree
#define ENABLED(f)
Definition: debug-flag.h:76
struct cct2metrics_t * left
Definition: cct2metrics.c:27