summaryrefslogtreecommitdiff
path: root/gprofng/src/Hist_data.h
blob: a178e90a4b01486dd15f7291df8e4031f2cdfc36 (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
/* Copyright (C) 2021-2023 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.  */

#ifndef _HIST_DATA_H
#define _HIST_DATA_H

// A Hist_data object is used to obtain data used for constructing
// a histogram display.

#include <sys/types.h>

#include <vec.h>
#include <Map.h>
#include <HashMap.h>

#include "dbe_structs.h"
#include "Histable.h"
#include "DerivedMetrics.h"

class DbeLine;
class MetricList;

class Hist_data
{
public:
  friend class DbeView;
  friend class er_print_histogram;
  friend class PathTree;
  friend class DataSpace;
  friend class MemorySpace;
  friend class IOActivity;
  friend class HeapActivity;

  // HistItem contains all the data about a single histogram item.
  struct HistItem
  {
    HistItem (long n);
    ~HistItem ();
    Histable *obj;  // info on the object
    int type;       // annotated src/dis: type
    TValue *value;  // numeric values
    long size;
  };

  enum Hist_status
  {
    SUCCESS = 0,
    NO_DATA
  };

  enum Mode
  {
    ALL,
    CALLERS,
    CALLEES,
    SELF,
    MODL,
    LAYOUT,
    DETAIL
  };

  enum Sort_order
  {
    ASCEND,
    DESCEND
  };

  enum Sort_type
  {
    ALPHA,
    VALUE,
    AUX
  };

  Hist_data (MetricList *, Histable::Type, Mode, bool _viewowned = false);

  virtual ~Hist_data ();
  void dump (char *msg, FILE *f);

  Hist_status
  get_status (void)
  {
    return status;
  }

  // Return the view ownership flag
  bool
  isViewOwned (void)
  {
    return viewowned;
  }

  // Return the total number of items
  long size (void);

  // Append a new HistItem for the specified Histable
  HistItem *append_hist_item (Histable *obj);
  void append_hist_item (HistItem *hi);
  TValue *get_real_value (TValue *res, int met_index, int row);
  TValue *get_value (TValue *res, int met_index, int row);
  TValue *get_value (TValue *res, int met_index, HistItem *hi);
  void print_row(StringBuilder *sb, int row, Metric::HistMetric *hist_metric,
		 const char *mark);
  void print_content (FILE *out_file, Metric::HistMetric *hist_metric, int limit);
  int print_label (FILE *out_file, Metric::HistMetric *hist_metric, int space);
  void update_total (Hist_data::HistItem *new_total);
  void update_max (Metric::HistMetric *hm_tmp);
  void update_legend_width (Metric::HistMetric *hm_tmp);

  // Find an existing HistItem
  HistItem *find_hist_item (Histable *obj);

  // sort the data
  void sort (long ind, bool reverse);

  // resort the data, if metric sort or direction has changed
  void resort (MetricList *mlist);

  // compute minima and maxima
  void compute_minmax (void);

  // fetch() takes a hist item index and returns a ptr to the item
  HistItem *fetch (long index);

  HistItem *
  get_maximums (void)
  {
    return maximum;
  }

  HistItem *
  get_maximums_inc (void)
  {
    return maximum_inc;
  }

  HistItem *
  get_minimums (void)
  {
    return minimum;
  }

  HistItem *
  get_totals (void)
  {
    return total;
  }

  Vector<HistItem*> *
  get_hist_items (void)
  {
    return hist_items;
  }

  void
  set_status (Hist_status st)
  {
    status = st;
  }

  MetricList *
  get_metric_list (void)
  {
    return metrics;
  }

  Map<Histable*, int> *
  get_callsite_mark ()
  {
    return callsite_mark;
  }

  Metric::HistMetric *get_histmetrics ();
  void set_threshold (double proportion);
  bool above_threshold (HistItem *hi);
  double get_percentage (double value, int mindex);
  size_t value_maxlen (int mindex); // Return the drawing length
  size_t time_len (TValue *value, int clock);
  size_t time_maxlen (int mindex, int clock);
  size_t name_len (HistItem *item);
  size_t name_maxlen ();
  HistItem *new_hist_item (Histable *obj, int itype, TValue *value);
  HistItem *update_hist_item (HistItem *hi, TValue *value);
  Vector<uint64_t> *get_object_indices (Vector<int> *selections);

private:

  Metric::HistMetric *hist_metrics;
  Vector<HistItem*> *hist_items;        // Actual histogram values
  HashMap<Histable*, HistItem*>*hi_map; // map: Histable -> HistItem
  Map<Histable*, int>*callsite_mark;
  Hist_status status;
  int nmetrics;             // number of metrics
  MetricList *metrics;
  Histable::Type type;
  Sort_order sort_order;
  Sort_type sort_type;
  int sort_ind;
  bool rev_sort;            // true if sort is reversed

  Mode mode;
  HistItem *gprof_item;     // used for gprof-style info
  Histable *spontaneous;

  // Private state variables
  HistItem *maximum;
  HistItem *minimum;
  HistItem *maximum_inc;
  HistItem *total;
  HistItem *threshold;

  // Perform the sort operation with this function
  static int sort_compare_all (const void *a, const void *b, const void *arg);
  static int sort_compare_dlayout (const void *a, const void *b, const void *arg);
  static int sort_compare (HistItem *hi_1, HistItem *hi_2, Sort_type stype,
			   long ind, Hist_data *hdata);

  // Allocate a new structure of dynamic size
  HistItem *new_hist_item (Histable *obj);

  // Flag indicating whether or not the Hist_data structure
  //	is owned by a DbeView, which has responsibility for
  //	deleting it, or not, in which case the last user deletes it.
  //	XXX this is very ugly, and arises from the inconsistent handling
  //	XXX of the Hist_data structure in various bits of code.
  bool viewowned;
};

// This structure is destined to merge with Hist_data.
// We currently use it to present callstack data such as
// leak and allocation lists.

class DbeInstr;

struct CStack_data
{

  struct CStack_item
  {
    CStack_item (long n);
    ~CStack_item ();
    long count;
    int64_t val;
    Vector<DbeInstr*> *stack;
    TValue *value;      // numeric values
  };

  Vector<CStack_item*> *cstack_items;
  CStack_item *total;

  CStack_item *new_cstack_item ();
  CStack_data (MetricList *);

  long
  size ()
  {
    return cstack_items->size ();
  }

  CStack_item *
  fetch (long i)
  {
    return cstack_items->fetch (i);
  }

  ~CStack_data ()
  {
    cstack_items->destroy ();
    delete cstack_items;
    delete total;
  }

  MetricList *metrics;
};

#endif /* _HIST_DATA_H */