aboutsummaryrefslogtreecommitdiff
path: root/gprofng/src/BaseMetricTreeNode.cc
blob: c8d76a60e4c28ee2aeefc092598b1a2e0880f3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Copyright (C) 2021-2024 Free Software Foundation, Inc.
   Contributed by Oracle.

   This file is part of GNU Binutils.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */

#include "config.h"
#include <stdio.h>
#include <strings.h>
#include <limits.h>
#include <sys/param.h>

#include "hwcentry.h"
#include "DbeSession.h"
#include "Experiment.h"
#include "Expression.h"
#include "Metric.h"
#include "Table.h"
#include "i18n.h"
#include "debug.h"

BaseMetricTreeNode::BaseMetricTreeNode ()
{
  init_vars ();
  build_basic_tree ();
}

BaseMetricTreeNode::BaseMetricTreeNode (BaseMetric *item)
{
  init_vars ();
  bm = item;
  name = dbe_strdup (bm->get_cmd ());
  uname = dbe_strdup (bm->get_username ());
  unit = NULL; //YXXX populate from base_metric (requires updating base_metric)
  unit_uname = NULL;
}

BaseMetricTreeNode::BaseMetricTreeNode (const char *_name, const char *_uname,
				    const char *_unit, const char *_unit_uname)
{
  init_vars ();
  name = dbe_strdup (_name);
  uname = dbe_strdup (_uname);
  unit = dbe_strdup (_unit);
  unit_uname = dbe_strdup (_unit_uname);
}

void
BaseMetricTreeNode::init_vars ()
{
  name = NULL;
  uname = NULL;
  unit = NULL;
  unit_uname = NULL;
  root = this;
  parent = NULL;
  children = new Vector<BaseMetricTreeNode*>;
  isCompositeMetric = false;
  bm = NULL;
  registered = false;
  num_registered_descendents = 0;
}

BaseMetricTreeNode::~BaseMetricTreeNode ()
{
  children->destroy ();
  delete children;
  free (name);
  free (uname);
  free (unit);
  free (unit_uname);
}

BaseMetricTreeNode *
BaseMetricTreeNode::register_metric (BaseMetric *item)
{
  BaseMetricTreeNode *found = root->find (item->get_cmd ());
  if (!found)
    {
      switch (item->get_type ())
	{
	case BaseMetric::CP_TOTAL:
	  found = root->find (L_CP_TOTAL);
	  break;
	case BaseMetric::CP_TOTAL_CPU:
	  found = root->find (L_CP_TOTAL_CPU);
	  break;
	}
      if (found && found->bm == NULL)
	found->bm = item;
    }
  if (!found)
    {
      switch (item->get_type ())
	{
	case BaseMetric::HEAP_ALLOC_BYTES:
	case BaseMetric::HEAP_ALLOC_CNT:
	case BaseMetric::HEAP_LEAK_BYTES:
	case BaseMetric::HEAP_LEAK_CNT:
	  found = root->find (get_prof_data_type_name (DATA_HEAP));
	  break;
	case BaseMetric::CP_KERNEL_CPU:
	case BaseMetric::CP_TOTAL:
	  found = root->find (get_prof_data_type_name (DATA_CLOCK));
	  break;
	case BaseMetric::CP_LMS_DFAULT:
	case BaseMetric::CP_LMS_TFAULT:
	case BaseMetric::CP_LMS_KFAULT:
	case BaseMetric::CP_LMS_STOPPED:
	case BaseMetric::CP_LMS_WAIT_CPU:
	case BaseMetric::CP_LMS_SLEEP:
	case BaseMetric::CP_LMS_USER_LOCK:
	case BaseMetric::CP_TOTAL_CPU:
	  found = root->find (L_CP_TOTAL);
	  break;
	case BaseMetric::CP_LMS_USER:
	case BaseMetric::CP_LMS_SYSTEM:
	case BaseMetric::CP_LMS_TRAP:
	  found = root->find (L_CP_TOTAL_CPU);
	  break;
	case BaseMetric::HWCNTR:
	  found = root->find ((item->get_flavors () & BaseMetric::DATASPACE) != 0 ?
			      L2_HWC_DSPACE : L2_HWC_GENERAL);
	  break;
	case BaseMetric::SYNC_WAIT_TIME:
	case BaseMetric::SYNC_WAIT_COUNT:
	  found = root->find (get_prof_data_type_name (DATA_SYNCH));
	  break;
	case BaseMetric::OMP_WORK:
	case BaseMetric::OMP_WAIT:
	case BaseMetric::OMP_OVHD:
	  found = root->find (get_prof_data_type_name (DATA_OMP));
	  break;
	case BaseMetric::IO_READ_TIME:
	case BaseMetric::IO_READ_BYTES:
	case BaseMetric::IO_READ_CNT:
	case BaseMetric::IO_WRITE_TIME:
	case BaseMetric::IO_WRITE_BYTES:
	case BaseMetric::IO_WRITE_CNT:
	case BaseMetric::IO_OTHER_TIME:
	case BaseMetric::IO_OTHER_CNT:
	case BaseMetric::IO_ERROR_TIME:
	case BaseMetric::IO_ERROR_CNT:
	  found = root->find (get_prof_data_type_name (DATA_IOTRACE));
	  break;
	case BaseMetric::ONAME:
	case BaseMetric::SIZES:
	case BaseMetric::ADDRESS:
	  found = root->find (L1_STATIC);
	  break;
	default:
	  found = root->find (L1_OTHER);
	  break;
	}
      assert (found != NULL);
      switch (item->get_type ())
	{
	case BaseMetric::CP_TOTAL:
	case BaseMetric::CP_TOTAL_CPU:
	  found->isCompositeMetric = true;
	  break;
	}
      found = found->add_child (item);
    }
  register_node (found);
  return found;
}

void
BaseMetricTreeNode::register_node (BaseMetricTreeNode *node)
{
  if (!node->registered)
    {
      node->registered = true;
      BaseMetricTreeNode *tmp = node->parent;
      while (tmp)
	{
	  tmp->num_registered_descendents++;
	  tmp = tmp->parent;
	}
    }
}

BaseMetricTreeNode *
BaseMetricTreeNode::find (const char *_name)
{
  BaseMetricTreeNode *found = NULL;
  if (dbe_strcmp (get_name (), _name) == 0)
    return this;
  if (bm && dbe_strcmp (bm->get_cmd (), _name) == 0)
    return this;
  BaseMetricTreeNode *child;
  int index;

  Vec_loop (BaseMetricTreeNode*, children, index, child)
  {
    found = child->find (_name);
    if (found)
      return found;
  }
  return NULL;
}

static void
int_get_registered_descendents (BaseMetricTreeNode* curr,
			  Vector<BaseMetricTreeNode*> *dest, bool nearest_only)
{
  if (!curr)
    return;
  if (curr->is_registered ())
    {
      dest->append (curr);
      if (nearest_only)
	return; // soon as we hit a live node, stop following branch
    }
  int index;
  BaseMetricTreeNode *child;

  Vec_loop (BaseMetricTreeNode*, curr->get_children (), index, child)
  {
    int_get_registered_descendents (child, dest, nearest_only);
  }
}

void
BaseMetricTreeNode::get_nearest_registered_descendents (Vector<BaseMetricTreeNode*> *dest)
{
  if (!dest || dest->size () != 0)
    abort ();
  bool nearest_only = true;
  int_get_registered_descendents (this, dest, nearest_only);
}

void
BaseMetricTreeNode::get_all_registered_descendents (Vector<BaseMetricTreeNode*> *dest)
{
  if (!dest || dest->size () != 0)
    abort ();
  bool nearest_only = false;
  int_get_registered_descendents (this, dest, nearest_only);
}

char *
BaseMetricTreeNode::get_description ()
{
  if (bm)
    {
      Hwcentry* hw_ctr = bm->get_hw_ctr ();
      if (hw_ctr)
	return hw_ctr->short_desc;
    }
  return NULL;
}

void
BaseMetricTreeNode::build_basic_tree ()
{
#define TREE_INSERT_DATA_TYPE(t) add_child(get_prof_data_type_name (t), get_prof_data_type_uname (t))
  BaseMetricTreeNode *level1, *level2;
  // register L1_DURATION here because it has a value but is not a true metric
  register_node (add_child (L1_DURATION, L1_DURATION_UNAME, UNIT_SECONDS,
			    UNIT_SECONDS_UNAME));
  register_node (add_child (L1_GCDURATION, L1_GCDURATION_UNAME, UNIT_SECONDS,
			    UNIT_SECONDS_UNAME));
  TREE_INSERT_DATA_TYPE (DATA_HEAP);
  level1 = TREE_INSERT_DATA_TYPE (DATA_CLOCK);
  level1 = level1->add_child (L_CP_TOTAL, GTXT ("XXX Total Thread Time"));
  level1->isCompositeMetric = true;
  level2 = level1->add_child (L_CP_TOTAL_CPU, GTXT ("XXX Total CPU Time"));
  level2->isCompositeMetric = true;

  add_child (L1_OTHER, L1_OTHER_UNAME);
  level1 = TREE_INSERT_DATA_TYPE (DATA_HWC);
  level1->add_child (L2_HWC_DSPACE, L2_HWC_DSPACE_UNAME);
  level1->add_child (L2_HWC_GENERAL, L2_HWC_GENERAL_UNAME);
  TREE_INSERT_DATA_TYPE (DATA_SYNCH);
  TREE_INSERT_DATA_TYPE (DATA_OMP);
  TREE_INSERT_DATA_TYPE (DATA_IOTRACE);
  add_child (L1_STATIC, L1_STATIC_UNAME);
}

BaseMetricTreeNode *
BaseMetricTreeNode::add_child (BaseMetric *item)
{
  return add_child (new BaseMetricTreeNode (item));
}

BaseMetricTreeNode *
BaseMetricTreeNode::add_child (const char * _name, const char *_uname,
			       const char * _unit, const char * _unit_uname)
{
  return add_child (new BaseMetricTreeNode (_name, _uname, _unit, _unit_uname));
}

BaseMetricTreeNode *
BaseMetricTreeNode::add_child (BaseMetricTreeNode *new_node)
{
  new_node->parent = this;
  new_node->root = root;
  children->append (new_node);
  return new_node;
}

char *
BaseMetricTreeNode::dump ()
{
  int len = 4;
  char *s = bm ? bm->dump () : dbe_strdup ("<no base metric>");
  char *msg = dbe_sprintf ("%s\n%*c %*c unit='%s' unit_uname='%s' uname='%s' name='%s'\n",
			   STR (s), len, ' ', len, ' ',
			   STR (get_unit_uname ()), STR (get_unit ()),
			   STR (get_user_name ()), STR (get_name ()));
  free (s);
  return msg;
}