1 /* Copyright (C) 2021 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
26 #include "DefaultMap.h"
29 #include "DbeSession.h"
30 #include "Application.h"
31 #include "CallStack.h"
33 #include "Experiment.h"
34 #include "Expression.h"
37 #include "IndexObject.h"
38 #include "MetricList.h"
43 #include "LoadObject.h"
45 #include "StringBuilder.h"
48 // Define counts, rate for error warnings for statistical profiles
49 #define MIN_PROF_CNT 100
50 #define MAX_PROF_RATE 1000.
52 #define NUM_DESCENDANTS(nd) ((nd)->descendants ? (nd)->descendants->size() : 0)
53 #define IS_LEAF(nd) ((nd)->descendants == NULL)
56 #define DBG(__func) __func
62 PathTree::construct (DbeView *_dbev, int _indxtype, PathTreeType _pathTreeType)
66 pathTreeType = _pathTreeType;
70 nodes = 1; // don't use node 0
84 ptree_internal = NULL;
85 ftree_internal = NULL;
86 ftree_needs_update = false;
91 PathTree::~PathTree ()
94 for (long i = 0; i < nchunks; i++)
102 fn_map = new DefaultMap<Function*, NodeIdx>;
103 stack_prop = PROP_NONE;
104 desc_htable_size = 511;
105 desc_htable_nelem = 0;
106 descHT = new hash_node_t*[desc_htable_size];
107 for (int i = 0; i < desc_htable_size; i++)
109 pathMap = new CacheMap<uint64_t, NodeIdx>;
110 statsq = new Emsgqueue (NTXT ("statsq"));
111 warningq = new Emsgqueue (NTXT ("warningq"));
114 Function *ftotal = dbeSession->get_Total_Function ();
115 if (pathTreeType == PATHTREE_INTERNAL_FUNCTREE)
118 total_obj = ftotal->find_dbeinstr (0, 0);
119 VMode view_mode = dbev->get_view_mode ();
120 if (view_mode == VMODE_MACHINE)
121 stack_prop = PROP_MSTACK;
122 else if (view_mode == VMODE_EXPERT)
123 stack_prop = PROP_XSTACK;
124 else if (view_mode == VMODE_USER)
126 stack_prop = PROP_USTACK;
127 if (dbeSession->is_omp_available ()
128 && pathTreeType == PATHTREE_INTERNAL_OMP)
129 stack_prop = PROP_XSTACK;
134 total_obj = new IndexObject (indxtype, (uint64_t) - 2);
135 total_obj->set_name (dbe_strdup (NTXT ("<Total>")));
136 char *idxname = dbeSession->getIndexSpaceName (indxtype);
137 if (streq (idxname, NTXT ("OMP_preg")))
138 stack_prop = PROP_CPRID;
139 else if (streq (idxname, NTXT ("OMP_task")))
140 stack_prop = PROP_TSKID;
142 indx_expr = dbeSession->getIndexSpaceExpr (indxtype);
144 root_idx = new_Node (0, total_obj, false);
145 root = NODE_IDX (root_idx);
151 // For each node free its descendants vector
152 // and reset the node list of its function
153 for (long i = 1; i < nodes; i++)
155 Node *node = NODE_IDX (i);
156 if (node->descendants)
157 delete node->descendants;
159 nodes = 1; // don't use node 0
161 for (int i = 0; i < nslots; i++)
163 int **tmp = slots[i].mvals;
164 for (long j = 0; j < nchunks; j++)
180 for (int i = 0; i < desc_htable_size; i++)
182 hash_node_t *p = descHT[i];
203 if (pathTreeType == PATHTREE_INTERNAL_FUNCTREE)
204 return NORMAL; // never process reset for ftree_internal.
206 if (dbeSession->is_omp_available () && dbev->get_view_mode () == VMODE_USER
207 && pathTreeType == PATHTREE_MAIN && ptree_internal == NULL)
208 ptree_internal = new PathTree (dbev, indxtype, PATHTREE_INTERNAL_OMP);
210 if (phaseIdx != dbev->getPhaseIdx ())
214 phaseIdx = dbev->getPhaseIdx ();
215 ftree_needs_update = true;
217 for (; nexps < dbeSession->nexps (); nexps++)
219 ftree_needs_update = true;
220 if (add_experiment (nexps) == CANCELED)
224 // LIBRARY_VISIBILITY
225 if (dbev->isNewViewMode ())
226 dbev->resetNewViewMode ();
227 if (dbev->isShowHideChanged ())
228 dbev->resetShowHideChanged ();
233 PathTree::allocate_slot (int id, ValueTag vtype)
237 int slot_idx = find_slot (id);
240 DBG (assert (slots[slot_idx].vtype == vtype));
245 Slot *old_slots = slots;
246 slots = new Slot[nslots];
247 for (i = 0; i < slot_idx; i++)
248 slots[i] = old_slots[i];
251 slots[slot_idx].id = id;
252 slots[slot_idx].vtype = vtype;
253 int **ip = new int*[nchunks];
254 for (i = 0; i < nchunks; i++)
256 slots[slot_idx].mvals = ip;
262 PathTree::allocate_slots (Slot *new_slots, int new_nslots)
264 // duplicates new_slots
266 // if previously had more slots than currently requested, delete the data from those slots.
267 for (int i = new_nslots; i < nslots; i++)
269 int **tmp = slots[i].mvals;
270 for (long j = 0; j < nchunks; j++)
282 Slot *old_slots = slots;
283 slots = new Slot[new_nslots];
284 for (int i = 0; i < new_nslots; i++)
286 slots[i] = new_slots[i]; // pick up id and vtype
288 slots[i].mvals = old_slots[i].mvals;
292 slots[i].mvals = NULL;
295 int **ip = new int*[nchunks];
296 for (long j = 0; j < nchunks; j++)
307 PathTree::find_slot (int id)
309 for (int i = 0; i < nslots; i++)
310 if (slots[i].id == id)
316 PathTree::new_Node (NodeIdx anc, Histable *instr, bool leaf)
318 if (nodes >= nchunks * CHUNKSZ)
320 long idx = nchunks++;
322 // Reallocate Node chunk array
323 Node **old_chunks = chunks;
324 chunks = new Node*[nchunks];
325 for (long k = 0; k < idx; k++)
326 chunks[k] = old_chunks[k];
329 // Reallocate metric value chunk arrays.
330 for (int i = 0; i < nslots; i++)
332 int **mvals = new int*[nchunks];
333 for (long k = 0; k < idx; k++)
335 mvals[k] = slots[i].mvals[k];
337 delete[] slots[i].mvals;
338 slots[i].mvals = mvals;
339 slots[i].mvals[idx] = NULL;
342 // Allocate new chunk for nodes.
343 // Note that we don't need to allocate new chunks
344 // for metric values at this point as we rely on
347 allocate_chunk (chunks, idx);
349 NodeIdx node_idx = nodes++;
350 Node *node = NODE_IDX (node_idx);
351 node->ancestor = anc;
352 node->descendants = leaf ? (Vector<NodeIdx>*)NULL : new Vector<NodeIdx>(2);
354 Function *func = (Function*) (instr->convertto (Histable::FUNCTION));
355 node->funclist = fn_map->get (func);
356 fn_map->put (func, node_idx);
361 PathTree::find_path (Experiment *exp, DataView *dview, long recIdx)
363 if (indx_expr != NULL)
365 Expression::Context ctx (dbev, exp, dview, recIdx);
366 uint64_t idx = indx_expr->eval (&ctx);
367 Histable *cur_obj = dbeSession->createIndexObject (indxtype, idx);
368 cur_obj->set_name_from_context (&ctx);
369 NodeIdx dsc_idx = find_in_desc_htable (root_idx, cur_obj, true);
374 bool showAll = dbev->isShowAll ();
375 int t_stack_prop = stack_prop;
376 void *stackId = dview->getObjValue (t_stack_prop, recIdx);
380 // pathMap does not work with NULL key
381 node_idx = pathMap->get ((uint64_t) stackId);
385 Vector<Histable*> *stack = (Vector<Histable*>*)CallStack::getStackPCs (stackId, !showAll);
386 int stack_size = stack->size ();
393 for (int i = stack_size - 1; i >= 0; i--)
395 bool leaf = (i == 0);
396 Histable *cur_addr = stack->fetch (i);
398 // bail out of loop if load object API-only is set
399 // and this is not the top frame
400 // This is now done in HSTACK if hide is set
402 Function *func = (Function*) cur_addr->convertto (Histable::FUNCTION);
405 Module *mod = func->module;
406 LoadObject *lo = mod->loadobject;
407 int segx = lo->seg_idx;
408 if (showAll && dbev->get_lo_expand (segx) == LIBEX_API
409 && i != stack_size - 1)
413 NodeIdx dsc_idx = find_desc_node (node_idx, cur_addr, leaf);
417 // LIBEX_API processing might have set leaf to true
421 if (thisdepth > depth)
424 pathMap->put ((uint64_t) stackId, node_idx);
429 desc_node_comp (const void *s1, const void *s2, const void *ptree)
431 PathTree::NodeIdx t1, t2;
432 t1 = *(PathTree::NodeIdx *)s1;
433 t2 = *(PathTree::NodeIdx *)s2;
434 PathTree* Ptree = (PathTree *) ptree;
435 PathTree::Node *n1 = Ptree->NODE_IDX (t1);
436 PathTree::Node *n2 = Ptree->NODE_IDX (t2);
437 Histable *d1 = n1->instr;
438 Histable *d2 = n2->instr;
441 else if (d1->id > d2->id)
448 PathTree::find_in_desc_htable (NodeIdx node_idx, Histable *instr, bool leaf)
450 unsigned int hash_code = (unsigned int) instr->id % desc_htable_size;
451 Node *node = NODE_IDX (node_idx);
452 hash_node_t *p = NULL;
453 for (p = descHT[hash_code]; p; p = p->next)
455 Node *dsc = NODE_IDX (p->nd);
456 Histable *dinstr = dsc->instr;
457 if (dinstr->id == instr->id && leaf == IS_LEAF (dsc))
461 NodeIdx dsc_idx = new_Node (node_idx, instr, leaf);
462 node->descendants->append (dsc_idx);
463 p = new hash_node_t ();
465 p->next = descHT[hash_code];
466 descHT[hash_code] = p;
470 if (desc_htable_nelem == desc_htable_size)
472 int old_htable_size = desc_htable_size;
473 desc_htable_size = old_htable_size * 2 + 1;
474 hash_node_t **old_htable = descHT;
475 descHT = new hash_node_t*[desc_htable_size];
476 for (int i = 0; i < desc_htable_size; i++)
479 for (int i = 0; i < old_htable_size; i++)
480 if (old_htable[i] != NULL)
483 hash_node_t *hash_p = old_htable[i];
484 while (hash_p != NULL)
486 hash_node_t *new_p = new hash_node_t ();
487 new_p->nd = hash_p->nd;
488 Node *dnode = NODE_IDX (hash_p->nd);
489 Histable *dnode_instr = dnode->instr;
490 hash_code = (unsigned int) dnode_instr->id % desc_htable_size;
491 new_p->next = descHT[hash_code];
492 descHT[hash_code] = new_p;
494 hash_p = hash_p->next;
504 PathTree::find_desc_node (NodeIdx node_idx, Histable *instr, bool leaf)
506 // Binary search. All nodes are ordered by Histable::id.
508 // We have a special case when two nodes with the same
509 // id value may co-exist: one representing a leaf node and
510 // another one representing a call site.
511 Node *node = NODE_IDX (node_idx);
513 int right = NUM_DESCENDANTS (node) - 1;
514 while (left <= right)
516 int index = (left + right) / 2;
517 NodeIdx dsc_idx = node->descendants->fetch (index);
518 Node *dsc = NODE_IDX (dsc_idx);
519 Histable *dinstr = dsc->instr;
520 if (instr->id < dinstr->id)
522 else if (instr->id > dinstr->id)
524 else if (leaf == IS_LEAF (dsc))
532 // None was found. Create one.
533 NodeIdx dsc_idx = new_Node (node_idx, instr, leaf);
534 node->descendants->insert (left, dsc_idx);
539 PathTree::process_packets (Experiment *exp, DataView *packets, int data_type)
541 Expression::Context ctx (dbev, exp);
542 char *progress_bar_msg = NULL;
543 int progress_bar_percent = -1;
545 Vector<BaseMetric*> *mlist = dbev->get_all_reg_metrics ();
546 Vector<BaseMetric*> mlist2;
548 for (int midx = 0, mlist_sz = mlist->size (); midx < mlist_sz; ++midx)
550 BaseMetric *mtr = mlist->fetch (midx);
551 if (mtr->get_packet_type () == data_type &&
552 (mtr->get_expr () == NULL || mtr->get_expr ()->passes (&ctx)))
554 Hwcentry *hwc = mtr->get_hw_ctr ();
558 // XXX this should be done at metric registration
559 Collection_params *col_params = exp->get_params ();
560 for (int i = 0; i < MAX_HWCOUNT; i++)
562 // We may have duplicate counters in col_params,
563 // check for all (see 5081284).
564 if (dbe_strcmp (hwc->name, col_params->hw_aux_name[i]) == 0)
566 if (stb.length () != 0)
567 stb.append (NTXT ("||"));
568 stb.append (NTXT ("HWCTAG=="));
572 if (stb.length () == 0)
574 stb.append (NTXT ("&& ((HWCINT & "));
575 stb.append ((long long) HWCVAL_ERR_FLAG);
576 stb.append (NTXT (")==0)"));
577 char *s = stb.toString ();
578 mtr->set_cond_spec (s);
581 ValueTag vtype = mtr->get_vtype ();
587 break; // nothing to do
589 vtype = VT_ULLONG; // ym: not sure when this would happen
592 allocate_slot (mtr->get_id (), vtype);
597 Slot **mslots = new Slot*[mlist2.size ()];
598 for (int midx = 0, mlist_sz = mlist2.size (); midx < mlist_sz; ++midx)
600 BaseMetric *mtr = mlist2.fetch (midx);
601 int id = mtr->get_id ();
602 int slot_ind = find_slot (id);
603 mslots[midx] = SLOT_IDX (slot_ind);
606 for (long i = 0, packets_sz = packets->getSize (); i < packets_sz; ++i)
608 if (dbeSession->is_interactive ())
610 if (NULL == progress_bar_msg)
611 progress_bar_msg = dbe_sprintf (GTXT ("Processing Experiment: %s"),
612 get_basename (exp->get_expt_name ()));
613 int val = (int) (100 * i / packets_sz);
614 if (val > progress_bar_percent)
616 progress_bar_percent += 10;
617 if (theApplication->set_progress (val, progress_bar_msg)
626 NodeIdx path_idx = 0;
627 ctx.put (packets, i);
629 for (int midx = 0, mlist_sz = mlist2.size (); midx < mlist_sz; ++midx)
631 BaseMetric *mtr = mlist2.fetch (midx);
632 if (mtr->get_cond () != NULL && !mtr->get_cond ()->passes (&ctx))
635 int64_t mval = mtr->get_val ()->eval (&ctx);
639 path_idx = find_path (exp, packets, i);
640 NodeIdx node_idx = path_idx;
641 Slot *mslot = mslots[midx];
644 INCREMENT_METRIC (mslot, node_idx, mval);
645 node_idx = NODE_IDX (node_idx)->ancestor;
649 if (dbeSession->is_interactive ())
650 free (progress_bar_msg);
652 if (indx_expr != NULL)
653 root->descendants->sort ((CompareFunc) desc_node_comp, this);
658 PathTree::get_filtered_events (int exp_index, int data_type)
660 if (indx_expr != NULL)
662 IndexObjType_t *indexObj = dbeSession->getIndexSpace (indxtype);
663 if (indexObj->memObj && data_type != DATA_HWC)
666 return dbev->get_filtered_events (exp_index, data_type);
670 PathTree::add_experiment (int exp_index)
676 Experiment *experiment = dbeSession->get_exp (exp_index);
677 if (experiment->broken != 0)
680 expt_name = experiment->get_expt_name ();
681 base_name = get_basename (expt_name);
683 hrtime_t starttime = gethrtime ();
684 hrtime_t startvtime = gethrvtime ();
686 // Experiment::getEndTime was initially implemented as
687 // returning exp->last_event. To preserve the semantics
688 // new Experiment::getLastEvent() is used here.
689 hrtime_t tot_time = experiment->getLastEvent () - experiment->getStartTime ();
691 if (!dbev->isShowAll () && (dbev->isShowHideChanged ()
692 || dbev->isNewViewMode ()))
693 experiment->resetShowHideStack ();
695 // To report experiment index to the user,
696 // start numeration from 1, not 0
697 sb.sprintf (GTXT ("PathTree processing experiment %d (`%s'); duration %lld.%06lld"),
698 exp_index + 1, base_name,
699 tot_time / NANOSEC, (tot_time % NANOSEC / 1000));
700 m = new Emsg (CMSG_COMMENT, sb);
703 DataView *prof_packet = get_filtered_events (exp_index, DATA_CLOCK);
704 if (prof_packet && prof_packet->getSize () > 0)
706 if (process_packets (experiment, prof_packet, DATA_CLOCK) == CANCELED)
708 long clock_cnt = prof_packet->getSize ();
711 clock_rate = (double) clock_cnt / (double) tot_time * (double) NANOSEC;
713 clock_rate = (double) 0.;
714 if (experiment->timelineavail)
715 sb.sprintf (GTXT (" Processed %ld clock-profile events (%3.2f/sec.)"),
716 clock_cnt, clock_rate);
718 sb.sprintf (GTXT (" Processed %ld clock-profile events"), clock_cnt);
719 m = new Emsg (CMSG_COMMENT, sb);
722 // check for statistical validity
723 if ((experiment->timelineavail == true)
724 && !dbev->get_filter_active () && (clock_cnt < MIN_PROF_CNT))
726 sb.sprintf (GTXT ("WARNING: too few clock-profile events (%ld) in experiment %d (`%s') for statistical validity"),
727 clock_cnt, exp_index + 1, base_name);
728 m = new Emsg (CMSG_COMMENT, sb);
733 DataView *sync_packet = get_filtered_events (exp_index, DATA_SYNCH);
734 if (sync_packet && sync_packet->getSize () > 0)
736 if (process_packets (experiment, sync_packet, DATA_SYNCH) == CANCELED)
738 long sync_cnt = sync_packet->getSize ();
739 sb.sprintf (GTXT (" Processed %ld synctrace events"), sync_cnt);
740 m = new Emsg (CMSG_COMMENT, sb);
744 DataView *iotrace_packet = get_filtered_events (exp_index, DATA_IOTRACE);
745 if (iotrace_packet && iotrace_packet->getSize () > 0)
747 if (process_packets (experiment, iotrace_packet, DATA_IOTRACE) == CANCELED)
749 long iotrace_cnt = iotrace_packet->getSize ();
750 sb.sprintf (GTXT (" Processed %ld IO trace events"), iotrace_cnt);
751 m = new Emsg (CMSG_COMMENT, sb);
755 DataView *hwc_packet = get_filtered_events (exp_index, DATA_HWC);
756 if (hwc_packet && hwc_packet->getSize () > 0)
758 if (process_packets (experiment, hwc_packet, DATA_HWC) == CANCELED)
760 long hwc_cnt = hwc_packet->getSize ();
761 double hwc_rate = (double) hwc_cnt / (double) tot_time * (double) NANOSEC;
762 if (experiment->timelineavail)
763 sb.sprintf (GTXT (" Processed %ld hwc-profile events (%3.2f/sec.)"),
766 sb.sprintf (GTXT (" Processed %ld hwc-profile events"), hwc_cnt);
767 m = new Emsg (CMSG_COMMENT, sb);
770 // check for statistical validity
771 if (experiment->timelineavail && !dbev->get_filter_active () && (hwc_cnt < MIN_PROF_CNT))
773 sb.sprintf (GTXT ("WARNING: too few HW counter profile events (%ld) in experiment %d (`%s') for statistical validity"),
774 hwc_cnt, exp_index + 1, base_name);
775 m = new Emsg (CMSG_COMMENT, sb);
780 DataView *heap_packet = get_filtered_events (exp_index, DATA_HEAP);
781 if (heap_packet && heap_packet->getSize () > 0)
783 if (process_packets (experiment, heap_packet, DATA_HEAP) == CANCELED)
785 long heap_cnt = heap_packet->getSize ();
786 sb.sprintf (GTXT (" Processed %ld heaptrace events"), heap_cnt);
787 m = new Emsg (CMSG_COMMENT, sb);
791 DataView *race_packet = get_filtered_events (exp_index, DATA_RACE);
792 if (race_packet && race_packet->getSize () > 0)
794 if (process_packets (experiment, race_packet, DATA_RACE) == CANCELED)
796 long race_cnt = race_packet->getSize ();
797 sb.sprintf (GTXT (" Processed %ld race access events"), race_cnt);
798 m = new Emsg (CMSG_COMMENT, sb);
802 DataView *deadlock_packet = get_filtered_events (exp_index, DATA_DLCK);
803 if (deadlock_packet && deadlock_packet->getSize () > 0)
805 if (process_packets (experiment, deadlock_packet, DATA_DLCK) == CANCELED)
807 long race_cnt = deadlock_packet->getSize ();
808 sb.sprintf (GTXT (" Processed %ld race access events"), race_cnt);
809 m = new Emsg (CMSG_COMMENT, sb);
813 hrtime_t pathtime = gethrtime () - starttime;
814 hrtime_t pathvtime = gethrvtime () - startvtime;
815 sb.sprintf (GTXT ("PathTree time = %lld.%06lld CPU-time %lld.%06lld\n"),
816 pathtime / NANOSEC, (pathtime % NANOSEC) / 1000,
817 pathvtime / NANOSEC, (pathvtime % NANOSEC) / 1000);
818 m = new Emsg (CMSG_COMMENT, sb);
824 PathTree::compute_metrics (MetricList *mlist, Histable::Type type,
825 Hist_data::Mode mode, Vector<Histable*> *objs,
826 Histable *context, Vector<Histable*> *sel_objs,
827 PtreeComputeOption computeOpt)
829 VMode view_mode = dbev->get_view_mode ();
831 // For displaying disassembly correctly in user mode with openmp
832 if (ptree_internal != NULL &&
833 (view_mode == VMODE_EXPERT ||
834 (view_mode == VMODE_USER && (type == Histable::INSTR
835 || (dbev->isOmpDisMode ()
836 && type == Histable::FUNCTION
837 && mode == Hist_data::CALLEES
838 && computeOpt == COMPUTEOPT_OMP_CALLEE))
840 return ptree_internal->compute_metrics (mlist, type, mode, objs, context,
843 PtreePhaseStatus resetStatus = reset ();
845 hist_data = new Hist_data (mlist, type, mode);
846 int nmetrics = mlist->get_items ()->size ();
848 Hist_data::HistItem *hi;
851 if (status != 0 || resetStatus == CANCELED)
854 hist_data->set_status (Hist_data::SUCCESS);
855 if (dbeSession->is_interactive () && mode != Hist_data::CALLEES)
856 theApplication->set_progress (0, GTXT ("Constructing Metrics"));
858 xlate = new int[nmetrics];
859 for (int mind = 0; mind < nmetrics; mind++)
861 Metric *mtr = mlist->get (mind);
862 xlate[mind] = find_slot (mtr->get_id ());
865 // Compute dynamic metrics
866 obj_list = new Histable*[depth];
867 if ((type == Histable::LINE || type == Histable::INSTR)
868 && mode == Hist_data::CALLERS)
869 node_list = new Node*[depth];
872 if (mode == Hist_data::MODL)
874 Histable *obj = objs && objs->size () > 0 ? objs->fetch (0) : NULL;
877 switch (obj->get_type ())
879 case Histable::FUNCTION:
881 Vector<Function*> *funclist = new Vector<Function*>;
882 funclist->append ((Function*) obj);
883 get_metrics (funclist, context);
887 case Histable::MODULE:
889 Vector<Histable*> *comparableModules = obj->get_comparable_objs ();
890 if (comparableModules != NULL)
892 Vector<Function*> *functions = new Vector<Function*>;
893 for (int i = 0; i < comparableModules->size (); i++)
895 Module *mod = (Module*) comparableModules->fetch (i);
899 for (int i1 = 0; i1 < i; i1++)
901 if (mod == comparableModules->fetch (i1))
908 functions->addAll (mod->functions);
911 get_metrics (functions, context);
915 get_metrics (((Module*) obj)->functions, context);
918 case Histable::SOURCEFILE:
919 get_metrics (((SourceFile *) obj)->get_functions (), context);
926 else if (mode == Hist_data::CALLERS)
928 if (objs && objs->size () > 0)
929 get_clr_metrics (objs);
931 else if (mode == Hist_data::CALLEES)
933 if (objs && objs->size () > 0)
934 get_cle_metrics (objs);
935 else // Special case: get root
936 get_cle_metrics (NULL);
938 else if (mode == Hist_data::SELF)
940 if (objs->size () == 1)
942 Histable *obj = objs->fetch (0);
945 if (obj->get_type () == Histable::LINE)
947 Vector<Function*> *funclist = new Vector<Function*>;
948 for (DbeLine *dl = (DbeLine*) obj->convertto (Histable::LINE);
949 dl; dl = dl->dbeline_func_next)
951 funclist->append (dl->func);
953 get_self_metrics (obj, funclist, sel_objs);
956 else if (obj->get_type () == Histable::FUNCTION
957 || obj->get_type () == Histable::INSTR)
959 // Use shortcut for functions and oth.
962 Vector<Function*> *funclist = NULL;
963 if (context->get_type () == Histable::MODULE)
964 funclist = ((Module*) context)->functions->copy ();
967 funclist = new Vector<Function*>;
968 funclist->append ((Function*) context);
970 get_self_metrics (obj, funclist, sel_objs);
974 get_self_metrics (objs);
977 get_self_metrics (objs);
981 get_self_metrics (objs);
983 else // Hist_data::ALL
984 get_metrics (root_idx, 0);
987 if ((type == Histable::LINE || type == Histable::INSTR)
988 && mode == Hist_data::CALLERS)
991 // Postprocess; find total
992 for (long mind = 0, sz = mlist->get_items ()->size (); mind < sz; mind++)
994 Metric *mtr = mlist->get_items ()->get (mind);
995 Metric::SubType subtype = mtr->get_subtype ();
996 ValueTag vtype = mtr->get_vtype ();
997 hist_data->total->value[mind].tag = vtype;
1001 // ignoring the following cases (why?)
1011 // Calculate total as the sum of all values in hist_data for
1012 // ATTRIBUTED metrics only. For all others, use root node values.
1014 if ((mode == Hist_data::CALLERS || mode == Hist_data::CALLEES)
1015 && subtype == Metric::ATTRIBUTED)
1017 hist_data->total->value[mind].i = 0;
1018 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1020 hist_data->total->value[mind].i += hi->value[mind].i;
1022 if (mode == Hist_data::CALLEES)
1023 hist_data->total->value[mind].i += hist_data->gprof_item->value[mind].i;
1025 else if (xlate[mind] != -1)
1026 ASN_METRIC_VAL (hist_data->total->value[mind], slots[xlate[mind]],
1031 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1033 hi->value[mind].tag = vtype;
1036 if ((mode == Hist_data::CALLERS || mode == Hist_data::CALLEES)
1037 && subtype == Metric::ATTRIBUTED)
1039 hist_data->total->value[mind].ll = 0;
1040 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1042 hist_data->total->value[mind].ll += hi->value[mind].ll;
1044 if (mode == Hist_data::CALLEES)
1045 hist_data->total->value[mind].ll += hist_data->gprof_item->value[mind].ll;
1047 else if (xlate[mind] != -1)
1048 ASN_METRIC_VAL (hist_data->total->value[mind], slots[xlate[mind]], root_idx);
1052 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1054 hi->value[mind].tag = vtype;
1056 if ((mode == Hist_data::CALLERS || mode == Hist_data::CALLEES)
1057 && subtype == Metric::ATTRIBUTED)
1059 hist_data->total->value[mind].ull = 0;
1060 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1062 hist_data->total->value[mind].ull += hi->value[mind].ull;
1064 if (mode == Hist_data::CALLEES)
1065 hist_data->total->value[mind].ull += hist_data->gprof_item->value[mind].ull;
1067 else if (xlate[mind] != -1)
1068 ASN_METRIC_VAL (hist_data->total->value[mind], slots[xlate[mind]], root_idx);
1072 double prec = mtr->get_precision ();
1073 ValueTag vt = (xlate[mind] != -1) ? slots[xlate[mind]].vtype : VT_INT;
1074 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1076 double val = (vt == VT_LLONG ? hi->value[mind].ll :
1077 (vt == VT_ULLONG ? hi->value[mind].ull
1078 : hi->value[mind].i));
1079 hi->value[mind].tag = vtype;
1080 hi->value[mind].d = val / prec;
1083 if ((mode == Hist_data::CALLERS || mode == Hist_data::CALLEES)
1084 && subtype == Metric::ATTRIBUTED)
1086 hist_data->total->value[mind].d = 0.0;
1087 Vec_loop (Hist_data::HistItem*, hist_data->hist_items, index, hi)
1089 hist_data->total->value[mind].d += hi->value[mind].d;
1091 if (mode == Hist_data::CALLEES)
1092 hist_data->total->value[mind].d +=
1093 (double) (vt == VT_LLONG ? hist_data->gprof_item->value[mind].ll :
1094 (vt == VT_ULLONG ? hist_data->gprof_item->value[mind].ull :
1095 hist_data->gprof_item->value[mind].i)) / prec;
1097 else if (xlate[mind] != -1)
1099 TValue& total = hist_data->total->value[mind];
1100 ASN_METRIC_VAL (total, slots[xlate[mind]], root_idx);
1101 double val = (vt == VT_LLONG ? total.ll :
1102 (vt == VT_ULLONG ? total.ll : total.i));
1103 total.d = val / prec;
1110 // Determine by which metric to sort if any
1111 bool rev_sort = mlist->get_sort_rev ();
1112 for (long mind = 0, sz = mlist->get_items ()->size (); mind < sz; mind++)
1114 Metric *mtr = mlist->get_items ()->get (mind);
1115 if (mlist->get_sort_ref_index () == mind)
1118 switch (mtr->get_type ())
1120 case BaseMetric::SIZES:
1121 Vec_loop (Hist_data::HistItem *, hist_data->hist_items, index, hi)
1123 Histable *h = mtr->get_comparable_obj (hi->obj);
1124 hi->value[mind].tag = VT_LLONG;
1125 hi->value[mind].ll = h ? h->get_size () : 0;
1128 case BaseMetric::ADDRESS:
1129 Vec_loop (Hist_data::HistItem *, hist_data->hist_items, index, hi)
1131 Histable *h = mtr->get_comparable_obj (hi->obj);
1132 hi->value[mind].tag = VT_ADDRESS;
1133 hi->value[mind].ll = h ? h->get_addr () : 0;
1136 case BaseMetric::DERIVED:
1138 Definition *def = mtr->get_definition ();
1139 long *map = def->get_map ();
1140 for (long i1 = 0, sz1 = hist_data->hist_items->size (); i1 < sz1; i1++)
1142 /* Hist_data::HistItem * */hi = hist_data->hist_items->get (i1);
1143 hi->value[mind].tag = VT_DOUBLE;
1144 hi->value[mind].d = def->eval (map, hi->value);
1146 hist_data->total->value[mind].tag = VT_DOUBLE;
1147 hist_data->total->value[mind].d = def->eval (map, hist_data->total->value);
1155 hist_data->sort (sort_ind, rev_sort);
1156 hist_data->compute_minmax ();
1157 if (dbeSession->is_interactive () && mode != Hist_data::CALLERS)
1158 theApplication->set_progress (0, GTXT (""));
1161 if (ftree_hist_data)
1163 bool matches = ftree_debug_match_hist_data (hist_data, ftree_hist_data);
1167 hist_data = ftree_hist_data; // return the debug version
1175 PathTree::ftree_debug_match_hist_data (Hist_data *data /* ref */,
1176 Hist_data *data_tmp)
1178 if (data->get_status () != Hist_data::SUCCESS)
1180 DBG (assert (false));
1183 if (data == NULL && data != data_tmp)
1185 DBG (assert (false));
1190 mlist = data->get_metric_list ();
1191 MetricList *mlist_tmp;
1192 mlist_tmp = data_tmp->get_metric_list ();
1193 if (mlist->size () != mlist_tmp->size ())
1195 DBG (assert (false));
1199 // Get table size: count visible metrics
1200 int nitems = data->size ();
1201 if (data->size () != data_tmp->size ())
1203 DBG (assert (false));
1207 for (int i = 0; i < nitems; ++i)
1209 Hist_data::HistItem *item = data->fetch (i);
1210 Hist_data::HistItem *item_tmp = data_tmp->fetch (i);
1211 if (item->obj->id != item_tmp->obj->id)
1213 DBG (assert (false));
1218 for (long i = 0, sz = mlist->size (); i < sz; i++)
1221 Metric *mitem = mlist->get (i);
1222 Metric *mitem_tmp = mlist_tmp->get (i);
1224 if (mitem->get_id () != mitem_tmp->get_id ())
1226 DBG (assert (false));
1229 if (mitem->get_visbits () != mitem_tmp->get_visbits ())
1231 DBG (assert (false));
1234 if (mitem->get_vtype () != mitem_tmp->get_vtype ())
1236 DBG (assert (false));
1240 if (!mitem->is_visible () && !mitem->is_tvisible ()
1241 && !mitem->is_pvisible ())
1243 // table->append(dbeGetTableDataOneColumn(data, i));
1244 for (long row = 0, sz_row = data->size (); row < sz_row; row++)
1249 TValue *v = data->get_value (&res, met_ind, row);
1250 TValue *v_tmp = data_tmp->get_value (&res_tmp, met_ind, row);
1251 if ((m->get_visbits () & VAL_RATIO) != 0)
1253 if (v->tag != VT_LABEL)
1255 if (v->to_double () != v_tmp->to_double ())
1257 DBG (assert (false));
1263 switch (m->get_vtype ())
1267 double diff = v->d - v_tmp->d;
1268 if (diff < 0) diff = -diff;
1271 DBG (assert (false));
1275 DBG (assert (true));
1279 if (v->i != v_tmp->i)
1281 DBG (assert (false));
1288 if (v->ll != v_tmp->ll)
1290 DBG (assert (false));
1296 if (dbe_strcmp (v->l, v_tmp->l))
1298 DBG (assert (false));
1303 DBG (assert (false));
1313 PathTree::get_hist_func_obj (Node *node)
1317 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1318 // LIBRARY VISIBILITY
1319 lo = func->module->loadobject;
1320 if (dbev->get_lo_expand (lo->seg_idx) == LIBEX_HIDE)
1321 return lo->get_hide_function ();
1322 return get_compare_obj (func);
1326 PathTree::get_hist_obj (Node *node, Histable* context)
1330 switch (hist_data->type)
1332 case Histable::INSTR:
1333 if (hist_data->mode == Hist_data::MODL)
1335 if (node->instr->get_type () != Histable::INSTR)
1340 // LIBRARY VISIBILITY
1341 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1342 lo = func->module->loadobject;
1343 if (dbev->get_lo_expand (lo->seg_idx) == LIBEX_HIDE)
1344 return lo->get_hide_function ();
1348 case Histable::LINE:
1349 if (hist_data->mode != Hist_data::MODL)
1351 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1352 lo = func->module->loadobject;
1353 // LIBRARY VISIBILITY
1354 if (dbev->get_lo_expand (lo->seg_idx) == LIBEX_HIDE)
1355 return lo->get_hide_function ();
1357 // For openmp user mode - the stack is already made with dbelines,
1358 // no need to convert it
1359 if (node->instr->get_type () == Histable::LINE)
1361 return node->instr->convertto (Histable::LINE, context);
1363 case Histable::FUNCTION:
1364 if (pathTreeType == PATHTREE_INTERNAL_FUNCTREE && node->ancestor != 0)
1365 func = (Function*) node->instr;
1367 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1368 lo = func->module->loadobject;
1369 // LIBRARY VISIBILITY
1370 if (dbev->get_lo_expand (lo->seg_idx) == LIBEX_HIDE)
1371 return lo->get_hide_function ();
1372 return get_compare_obj (func);
1373 case Histable::MODULE:
1374 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1375 return func->module;
1376 case Histable::LOADOBJECT:
1377 func = (Function*) (node->instr->convertto (Histable::FUNCTION));
1378 return func->module->loadobject;
1379 case Histable::INDEXOBJ:
1380 case Histable::MEMOBJ:
1389 PathTree::get_compare_obj (Histable *obj)
1391 if (obj && dbev->comparingExperiments ())
1392 obj = dbev->get_compare_obj (obj);
1397 PathTree::get_metrics (NodeIdx node_idx, int dpth)
1399 Node *node = NODE_IDX (node_idx);
1400 Histable *cur_obj = get_hist_obj (node);
1401 obj_list[dpth] = cur_obj;
1403 // Check for recursion (inclusive metrics)
1405 for (int i = dpth - 1; i >= 0; i--)
1406 if (cur_obj == obj_list[i])
1412 // Check for leaf nodes (exclusive metrics)
1414 if (IS_LEAF (node) || node == NODE_IDX (root_idx))
1417 // We shouldn't eliminate empty subtrees here because
1418 // we create the list of hist items dynamically and want
1419 // one for each object in the tree.
1420 cur_obj = get_compare_obj (cur_obj);
1421 Hist_data::HistItem *hi = hist_data->append_hist_item (cur_obj);
1422 DBG (assert (hi != NULL));
1424 MetricList *mlist = hist_data->get_metric_list ();
1425 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
1427 if (xlate[ind] == -1)
1429 Metric *mtr = mlist->get (ind);
1430 Metric::SubType subtype = mtr->get_subtype ();
1431 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
1436 case Metric::INCLUSIVE:
1438 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1440 case Metric::EXCLUSIVE:
1442 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1444 // ignoring the following cases (why?)
1445 case Metric::STATIC:
1446 case Metric::ATTRIBUTED:
1448 case Metric::DATASPACE:
1450 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1455 if (dbeSession->is_interactive ())
1458 int new_percent = 95 * ndone / nodes;
1459 if (new_percent > percent)
1461 percent = new_percent;
1462 theApplication->set_progress (percent, NULL);
1466 // Recursively process all descendants
1468 int dsize = NUM_DESCENDANTS (node);
1469 for (index = 0; index < dsize; index++)
1470 get_metrics (node->descendants->fetch (index), dpth + 1);
1474 PathTree::get_clr_metrics (Vector<Histable*> *objs, NodeIdx node_idx,
1475 int pmatch, int dpth)
1477 Node *node = NODE_IDX (node_idx);
1479 if (hist_data->type == Histable::LINE || hist_data->type == Histable::INSTR)
1481 cur_obj = get_hist_func_obj (node);
1482 node_list[dpth] = node;
1485 cur_obj = get_hist_obj (node);
1486 obj_list[dpth] = cur_obj;
1489 int nobj = objs->size ();
1490 if (dpth + 1 >= nobj)
1493 for (int i = 0; i < nobj; ++i)
1495 if (objs->fetch (i) != obj_list[dpth - nobj + 1 + i])
1503 Hist_data::HistItem *hi = NULL;
1504 Hist_data::HistItem *hi_adj = NULL;
1505 if (match && dpth >= nobj)
1507 if (hist_data->type == Histable::LINE
1508 || hist_data->type == Histable::INSTR)
1509 hi = hist_data->append_hist_item (get_hist_obj (node_list[dpth - nobj]));
1511 hi = hist_data->append_hist_item (obj_list[dpth - nobj]);
1513 if (pmatch >= 0 && pmatch >= nobj)
1515 if (hist_data->type == Histable::LINE
1516 || hist_data->type == Histable::INSTR)
1517 hi_adj = hist_data->append_hist_item (get_hist_obj (
1518 node_list[pmatch - nobj]));
1520 hi_adj = hist_data->append_hist_item (obj_list[pmatch - nobj]);
1526 MetricList *mlist = hist_data->get_metric_list ();
1527 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
1529 if (xlate[ind] == -1)
1531 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
1533 Metric *mtr = mlist->get (ind);
1534 Metric::SubType subtype = mtr->get_subtype ();
1538 case Metric::ATTRIBUTED:
1540 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1542 SUB_METRIC_VAL (hi_adj->value[ind], slots[xlate[ind]], node_idx);
1544 case Metric::STATIC:
1545 case Metric::EXCLUSIVE:
1546 case Metric::INCLUSIVE:
1547 case Metric::DATASPACE:
1553 // Recursively process all descendants
1554 int dsize = NUM_DESCENDANTS (node);
1555 for (int index = 0; index < dsize; index++)
1556 get_clr_metrics (objs, node->descendants->fetch (index),
1557 match ? dpth : pmatch, dpth + 1);
1561 PathTree::get_clr_metrics (Vector<Histable*> *objs)
1563 get_clr_metrics (objs, root_idx, -1, 0);
1567 PathTree::get_cle_metrics (Vector<Histable*> *objs, NodeIdx node_idx, int pcle,
1568 int pmatch, int dpth)
1570 Node *node = NODE_IDX (node_idx);
1571 Histable *cur_obj = get_hist_obj (node);
1572 obj_list[dpth] = cur_obj;
1575 int nobj = objs->size ();
1576 if (dpth + 1 >= nobj)
1579 for (int i = 0; i < nobj; ++i)
1580 if (objs->fetch (i) != obj_list[dpth - nobj + 1 + i])
1587 Hist_data::HistItem *hi = NULL;
1588 Hist_data::HistItem *hi_adj = NULL;
1589 if (pmatch >= 0 && dpth == pmatch + 1)
1590 hi = hist_data->append_hist_item (cur_obj);
1591 if (match && IS_LEAF (node))
1592 hi = hist_data->gprof_item;
1594 hi_adj = hist_data->append_hist_item (obj_list[pcle]);
1598 MetricList *mlist = hist_data->get_metric_list ();
1599 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
1601 if (xlate[ind] == -1)
1603 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
1605 Metric *mtr = mlist->get (ind);
1606 Metric::SubType subtype = mtr->get_subtype ();
1607 if (subtype == Metric::ATTRIBUTED)
1609 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1611 SUB_METRIC_VAL (hi_adj->value[ind], slots[xlate[ind]], node_idx);
1616 // Recursively process all descendants
1617 int dsize = NUM_DESCENDANTS (node);
1618 for (int index = 0; index < dsize; index++)
1619 get_cle_metrics (objs, node->descendants->fetch (index),
1620 pmatch >= 0 && dpth == pmatch + 1 ? dpth : pcle,
1621 match ? dpth : pmatch, dpth + 1);
1625 PathTree::get_cle_metrics (Vector<Histable*> *objs, NodeIdx node_idx, int dpth)
1627 Node *node = NODE_IDX (node_idx);
1628 Histable *cur_obj = get_hist_obj (node);
1629 Hist_data::HistItem *hi = NULL;
1630 if (NULL == objs) // Special case: get root
1631 hi = hist_data->append_hist_item (cur_obj);
1634 if (dpth == objs->size ())
1635 hi = hist_data->append_hist_item (cur_obj);
1636 else if (cur_obj == objs->fetch (dpth))
1638 // Recursively process all descendants
1639 int dsize = NUM_DESCENDANTS (node);
1640 for (int index = 0; index < dsize; index++)
1641 get_cle_metrics (objs, node->descendants->fetch (index), dpth + 1);
1642 if (dpth == objs->size () - 1 && dsize == 0)
1643 hi = hist_data->gprof_item;
1649 MetricList *mlist = hist_data->get_metric_list ();
1650 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
1652 if (xlate[ind] == -1)
1654 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
1656 Metric *mtr = mlist->get (ind);
1657 Metric::SubType subtype = mtr->get_subtype ();
1658 if (subtype == Metric::ATTRIBUTED)
1659 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
1665 PathTree::ftree_reset ()
1667 if (pathTreeType == PATHTREE_MAIN && indxtype < 0)
1670 if (ftree_needs_update)
1672 if (ftree_internal == NULL)
1674 ftree_internal = new PathTree (dbev, indxtype,
1675 PATHTREE_INTERNAL_FUNCTREE);
1676 if (ftree_internal == NULL)
1679 ftree_internal->ftree_build (this);
1680 ftree_needs_update = false;
1686 PathTree::ftree_build (PathTree * mstr)
1690 allocate_slots (mstr->slots, mstr->nslots);
1691 ftree_build (mstr, mstr->root_idx, root_idx);
1692 depth = mstr->depth;
1696 #if DEBUG_FTREE // possibly TBR
1698 PathTree::ftree_dump ()
1700 hrtime_t starttime, endtime;
1702 // int nmetrics = nslots;
1703 for (int kk = 0; kk < nmetrics; kk++)
1705 int id = slots[kk].id;
1706 starttime = gethrtime ();
1708 for (int ii = 0; ii < depth; ii++)
1710 Vector<Vector<void*>*> *tmp = (Vector<Vector<void*>*>*)get_ftree_level
1714 long sz = tmp->get (0)->size ();
1717 // fprintf(stderr, "... finished (%ld nodes)\n", sz);
1719 Vector<NodeIdx> *nodeIdxList = (Vector<NodeIdx> *)tmp->get (0);
1720 Vector<NodeIdx> *ancestorNodeIdxList = (Vector<NodeIdx> *)tmp->get (1);
1721 Vector<uint64_t> *idList = (Vector<uint64_t> *)tmp->get (2);
1722 Vector<uint64_t> *vals = (Vector<uint64_t> *)tmp->get (3);
1723 for (int jj = 0; jj < sz; jj++)
1724 fprintf (stderr, " ...%d:%d node=%ld, anc=%ld, id=%llu, val=%llu\n",
1725 sz, jj, nodeIdxList->get (jj),
1726 ancestorNodeIdxList->get (jj),
1727 idList->get (jj), vals->get (jj));
1731 endtime = gethrtime ();
1732 fprintf (stderr, "====================== %ld nodes time=%llu\n",
1733 nodecnt, (endtime - starttime) / 1000 / 1000);
1738 // ftree: translate mstr Histable::INSTR to Histable::FUNCTION
1740 PathTree::ftree_build (PathTree *mstr, NodeIdx mstr_node_idx,
1741 NodeIdx local_node_idx)
1743 // requires: slots, nslots
1744 Node *mstr_node = mstr->NODE_IDX (mstr_node_idx);
1745 int dsize = NUM_DESCENDANTS (mstr_node);
1748 for (int i = 0; i < nslots; i++)
1750 if (i >= mstr->nslots)
1752 if (slots[i].vtype != mstr->slots[i].vtype)
1756 mstr->ASN_METRIC_VAL (val, mstr->slots[i], mstr_node_idx);
1758 switch (slots[i].vtype)
1773 Slot * mslot = SLOT_IDX (i);
1775 INCREMENT_METRIC (mslot, local_node_idx, mval);
1779 // Recursively process all descendants
1780 for (int index = 0; index < dsize; index++)
1782 NodeIdx mstr_desc_node_idx = mstr_node->descendants->fetch (index);
1783 Node *mstr_desc_node = mstr->NODE_IDX (mstr_desc_node_idx);
1784 Function *func = (Function*) mstr_desc_node->instr->convertto (Histable::FUNCTION);
1785 int mstr_desc_dsize = NUM_DESCENDANTS (mstr_desc_node);
1786 bool leaf = (mstr_desc_dsize == 0);
1787 NodeIdx local_desc_node_idx = find_desc_node (local_node_idx, func, leaf);
1788 ftree_build (mstr, mstr_desc_node_idx, local_desc_node_idx);
1793 PathTree::depth_map_build ()
1795 destroy (depth_map);
1796 depth_map = new Vector<Vector<NodeIdx>*>(depth);
1799 depth_map->put (depth - 1, 0); // fill vector with nulls
1800 depth_map_build (root_idx, 0);
1805 PathTree::depth_map_build (NodeIdx node_idx, int dpth)
1807 Node *node = NODE_IDX (node_idx);
1809 Vector<NodeIdx> *node_idxs = depth_map->get (dpth);
1810 if (node_idxs == NULL)
1812 node_idxs = new Vector<NodeIdx>();
1813 depth_map->store (dpth, node_idxs);
1815 node_idxs->append (node_idx);
1817 // Recursively process all descendants
1818 int dsize = NUM_DESCENDANTS (node);
1819 for (int index = 0; index < dsize; index++)
1821 NodeIdx desc_node_idx = node->descendants->fetch (index);
1822 depth_map_build (desc_node_idx, dpth + 1);
1827 PathTree::get_ftree_depth ()
1828 { // external use only
1830 if (!ftree_internal)
1832 return ftree_internal->get_depth ();
1836 PathTree::get_ftree_funcs ()
1837 { // external use only
1839 if (!ftree_internal)
1841 return ftree_internal->get_funcs ();
1845 PathTree::get_funcs ()
1847 // get unique functions
1850 return fn_map->keySet ();
1854 PathTree::get_ftree_level (BaseMetric *bm, int dpth)
1855 { // external use only
1857 if (!ftree_internal)
1859 return ftree_internal->get_level (bm, dpth);
1863 PathTree::get_level (BaseMetric *bm, int dpth)
1865 // Nodes at tree depth dpth
1866 if (dpth < 0 || dpth >= depth)
1868 if (depth_map == NULL)
1870 Vector<NodeIdx> *node_idxs = depth_map->get (dpth);
1871 return get_nodes (bm, node_idxs);
1875 PathTree::get_ftree_node_children (BaseMetric *bm, NodeIdx node_idx)
1876 { // external use only
1878 if (!ftree_internal)
1880 return ftree_internal->get_node_children (bm, node_idx);
1884 PathTree::get_node_children (BaseMetric *bm, NodeIdx node_idx)
1886 // Nodes that are children of node_idx
1887 if (depth_map == NULL)
1889 if (node_idx == 0) // special case for root
1890 return get_nodes (bm, depth_map->get (0));
1891 if (node_idx < 0 || node_idx >= nodes)
1893 Node *node = NODE_IDX (node_idx);
1896 Vector<NodeIdx> *node_idxs = node->descendants;
1897 return get_nodes (bm, node_idxs);
1901 PathTree::get_nodes (BaseMetric *bm, Vector<NodeIdx> *node_idxs)
1903 // capture info for node_idxs:
1905 // node->ancestor idx
1907 // mind metric value // in the future, could instead accept vector of mind
1908 if (node_idxs == NULL)
1910 long sz = node_idxs->size ();
1914 bool calculate_metric = false;
1920 int mind = bm->get_id ();
1921 slot_idx = find_slot (mind); // may be -1 (CPI and IPC)
1922 prec = bm->get_precision ();
1923 vtype = bm->get_vtype ();
1939 if (slots[slot_idx].vtype == vtype)
1940 calculate_metric = true;
1942 DBG (assert (false));
1945 calculate_metric = true;
1952 Vector<void*> *results = new Vector<void*>(4);
1953 if (!calculate_metric)
1954 results->store (3, NULL);
1957 // Code below cribbed from Dbe.cc:dbeGetTableDataV2Data.
1958 // TBD: possibly create an intermediate HistData and instead call that routine
1964 Vector<long long> *vals = new Vector<long long>(sz);
1965 for (long i = 0; i < sz; i++)
1967 NodeIdx node_idx = node_idxs->get (i);
1970 ASN_METRIC_VAL (val, slots[slot_idx], node_idx);
1971 vals->append (val.ll);
1973 results->store (3, vals);
1978 Vector<double> *vals = new Vector<double>(sz);
1980 val.tag = slots[slot_idx].vtype; // required for to_double();
1981 for (long i = 0; i < sz; i++)
1983 NodeIdx node_idx = node_idxs->get (i);
1985 ASN_METRIC_VAL (val, slots[slot_idx], node_idx);
1986 double dval = val.to_double ();
1988 vals->append (dval);
1990 results->store (3, vals);
1995 Vector<int> *vals = new Vector<int>(sz);
1996 for (long i = 0; i < sz; i++)
1998 NodeIdx node_idx = node_idxs->get (i);
2001 ASN_METRIC_VAL (val, slots[slot_idx], node_idx);
2002 vals->append (val.i);
2004 results->store (3, vals);
2008 results->store (3, NULL);
2013 Vector<int> *nodeIdxList = new Vector<int>(sz);
2014 Vector<int> *ancestorNodeIdxList = new Vector<int>(sz);
2015 Vector<uint64_t> *idList = new Vector<uint64_t>(sz);
2016 for (long i = 0; i < sz; i++)
2018 NodeIdx node_idx = node_idxs->get (i);
2019 Node *node = NODE_IDX (node_idx);
2020 NodeIdx ancestor_idx = node->ancestor;
2021 Histable *func = node->instr;
2022 nodeIdxList->append (node_idx);
2023 ancestorNodeIdxList->append (ancestor_idx);
2024 idList->append (func->id);
2027 results->store (0, nodeIdxList);
2028 results->store (1, ancestorNodeIdxList);
2029 results->store (2, idList);
2034 PathTree::get_cle_metrics (Vector<Histable*> *objs)
2036 if (NULL == objs || objs->fetch (0) == get_hist_obj (NODE_IDX (root_idx)))
2037 // Call Tree optimization
2038 get_cle_metrics (objs, root_idx, 0);
2041 get_cle_metrics (objs, root_idx, -1, -1, 0);
2045 PathTree::get_metrics (Vector<Function*> *functions, Histable *context)
2048 int excl_ok, incl_ok;
2053 Vec_loop (Function*, functions, index, fitem)
2055 node_idx = fn_map->get (fitem);
2056 for (; node_idx; node_idx = node->funclist)
2058 node = NODE_IDX (node_idx);
2059 Histable *h_obj = get_hist_obj (node, context);
2063 // Check for recursion (inclusive metrics)
2065 for (anc = NODE_IDX (node->ancestor); anc;
2066 anc = NODE_IDX (anc->ancestor))
2068 if (h_obj == get_hist_obj (anc, context))
2075 // Check for leaf nodes (exclusive metrics)
2080 h_obj = get_compare_obj (h_obj);
2081 Hist_data::HistItem *hi = hist_data->append_hist_item (h_obj);
2084 hist_data->get_callsite_mark ()->put (h_obj, 1);
2085 MetricList *mlist = hist_data->get_metric_list ();
2086 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
2088 if (xlate[ind] == -1)
2090 Metric *mtr = mlist->get (ind);
2091 Metric::SubType subtype = mtr->get_subtype ();
2092 if (subtype == Metric::INCLUSIVE && !incl_ok)
2094 if (subtype == Metric::EXCLUSIVE && !excl_ok)
2096 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
2098 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
2105 PathTree::get_self_metrics (Vector<Histable*> *objs, NodeIdx node_idx,
2106 bool seen, int dpth)
2108 Node *node = NODE_IDX (node_idx);
2109 Histable *cur_obj = get_hist_obj (node);
2110 obj_list[dpth] = cur_obj;
2113 int nobj = objs->size ();
2114 if (dpth + 1 >= nobj)
2117 for (int i = 0; i < nobj; ++i)
2119 if (objs->fetch (i) != obj_list[dpth - nobj + 1 + i])
2129 Hist_data::HistItem *hi = hist_data->append_hist_item (cur_obj);
2130 int incl_ok = !seen;
2132 if (IS_LEAF (node) || node == NODE_IDX (root_idx))
2134 MetricList *mlist = hist_data->get_metric_list ();
2135 for (long ind = 0, sz = mlist->size (); ind < sz; ind++)
2137 if (xlate[ind] == -1)
2139 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
2141 Metric *mtr = mlist->get (ind);
2142 Metric::SubType subtype = mtr->get_subtype ();
2145 case Metric::INCLUSIVE:
2147 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
2149 case Metric::EXCLUSIVE:
2150 case Metric::ATTRIBUTED:
2152 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
2154 case Metric::DATASPACE:
2156 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
2158 // ignoring the following cases (why?)
2159 case Metric::STATIC:
2165 if (dbeSession->is_interactive ())
2168 int new_percent = 95 * ndone / nodes;
2169 if (new_percent > percent)
2171 percent = new_percent;
2172 theApplication->set_progress (percent, NULL);
2176 // Recursively process all descendants
2178 int dsize = NUM_DESCENDANTS (node);
2179 for (index = 0; index < dsize; index++)
2180 get_self_metrics (objs, node->descendants->fetch (index),
2181 seen || match, dpth + 1);
2185 PathTree::get_self_metrics (Vector<Histable*> *objs)
2187 get_self_metrics (objs, root_idx, false, 0);
2191 PathTree::get_self_metrics (Histable *obj, Vector<Function*> *funclist,
2192 Vector<Histable*>* sel_objs)
2194 int excl_ok, incl_ok;
2201 SourceFile *src = NULL;
2202 if (obj && obj->get_type () == Histable::LINE)
2204 DbeLine *dbeline = (DbeLine*) obj;
2205 src = dbeline->sourceFile;
2208 Hist_data::HistItem *hi = hist_data->append_hist_item (obj);
2209 for (int i = 0, sz = funclist ? funclist->size () : 0; i < sz; i++)
2211 Function *fitem = (Function*) get_compare_obj (funclist->fetch (i));
2212 node_idx = fn_map->get (fitem);
2213 for (; node_idx; node_idx = node->funclist)
2215 node = NODE_IDX (node_idx);
2216 if (obj && obj->get_type () == Histable::LINE)
2218 Histable *h = get_hist_obj (node, src);
2221 if (h->convertto (Histable::LINE) != obj->convertto (Histable::LINE))
2224 else if (get_hist_obj (node, src) != obj)
2227 // Check for recursion (inclusive metrics)
2229 for (anc = NODE_IDX (node->ancestor); anc;
2230 anc = NODE_IDX (anc->ancestor))
2232 if (get_hist_obj (anc, src) == obj)
2237 if (sel_objs != NULL)
2238 for (int k = 0; k < sel_objs->size (); k++)
2239 if (sel_objs->fetch (k) == get_hist_obj (anc, src))
2246 // Check for leaf nodes (exclusive metrics)
2248 if (IS_LEAF (node) || node == NODE_IDX (root_idx))
2251 MetricList *mlist = hist_data->get_metric_list ();
2252 for (long ind = 0, ind_sz = mlist->size (); ind < ind_sz; ind++)
2254 if (xlate[ind] == -1)
2256 Metric *mtr = mlist->get (ind);
2257 Metric::SubType subtype = mtr->get_subtype ();
2258 if (subtype == Metric::INCLUSIVE && !incl_ok)
2260 if (subtype == Metric::EXCLUSIVE && !excl_ok)
2262 if (subtype == Metric::ATTRIBUTED && !excl_ok)
2264 if (IS_MVAL_ZERO (slots[xlate[ind]], node_idx))
2266 ADD_METRIC_VAL (hi->value[ind], slots[xlate[ind]], node_idx);
2273 PathTree::get_clr_instr (Histable * func)
2275 Vector<Histable*> * instrs = NULL;
2276 if (func->get_type () != Histable::FUNCTION)
2278 NodeIdx node_idx = fn_map->get ((Function*) func);
2279 Node *node = NODE_IDX (node_idx);
2281 return new Vector<Histable*>();
2283 for (; node; node = NODE_IDX (node->funclist))
2285 instrs = new Vector<Histable*>(instr_num);
2286 node = NODE_IDX (node_idx);
2287 Histable *instr = NODE_IDX (node->ancestor)->instr;
2289 instrs->store (instr_num, instr);
2290 node = NODE_IDX (node->funclist);
2291 for (; node; node = NODE_IDX (node->funclist))
2293 instr = NODE_IDX (node->ancestor)->instr;
2295 instrs->store (instr_num, instr);
2301 PathTree::get_cle_instr (Histable * func, Vector<Histable*>*&instrs)
2303 if (func->get_type () != Histable::FUNCTION)
2305 NodeIdx node_idx = fn_map->get ((Function*) func);
2306 Node *node = NODE_IDX (node_idx);
2309 instrs = new Vector<Histable*>();
2310 return new Vector<void*>();
2313 for (; node; node = NODE_IDX (node->funclist))
2315 instrs = new Vector<Histable*>(instr_num);
2316 Vector<void*> *callee_info = new Vector<void*>(instr_num);
2317 node = NODE_IDX (node_idx);
2318 Histable *instr = node->instr;
2320 instrs->store (instr_num, instr);
2322 NodeIdx dec_idx = 0;
2323 if (NUM_DESCENDANTS (node) > 0)
2325 Vector<Histable*> * callee_instrs = new Vector<Histable*>(node->descendants->size ());
2326 Vec_loop (NodeIdx, node->descendants, dec_num, dec_idx)
2328 Node * dec_node = NODE_IDX (dec_idx);
2329 //XXXX Note: there can be more than one instrs in one leaf function
2330 callee_instrs->store (dec_num, dec_node->instr);
2332 callee_info->store (instr_num, callee_instrs);
2335 callee_info->store (instr_num, NULL);
2336 node = NODE_IDX (node->funclist);
2337 for (; node; node = NODE_IDX (node->funclist))
2339 instr = node->instr;
2341 instrs->store (instr_num, instr);
2342 if (NUM_DESCENDANTS (node) > 0)
2344 Vector<Histable*> * callee_instrs = new Vector<Histable*>(node->descendants->size ());
2345 Vec_loop (NodeIdx, node->descendants, dec_num, dec_idx)
2347 Node * dec_node = NODE_IDX (dec_idx);
2348 //XXXX Note: there can be more than one instrs in one leaf function
2349 callee_instrs->store (dec_num, dec_node->instr);
2351 callee_info->store (instr_num, callee_instrs);
2354 callee_info->store (instr_num, NULL);
2361 // The following methods are used for debugging purpose only.
2364 static int maxdepth;
2365 static int maxwidth;
2368 PathTree::print (FILE *fd)
2371 fprintf (fd, NTXT ("n = %lld, dn = %lld, MD = %lld\n\n"),
2372 (long long) nodes, (long long) dnodes, (long long) depth);
2375 print (fd, root, 0);
2376 fprintf (fd, NTXT ("md = %lld, mw = %lld\n"),
2377 (long long) maxdepth, (long long) maxwidth);
2381 PathTree::print (FILE *fd, PathTree::Node *node, int lvl)
2385 if (lvl + 1 > maxdepth)
2387 for (int i = 0; i < lvl; i++)
2388 fprintf (fd, NTXT ("-"));
2389 Histable *instr = node->instr;
2390 if (instr->get_type () == Histable::LINE)
2393 n = ((DbeLine *) instr)->func->get_name ();
2395 else if (instr->get_type () == Histable::INSTR)
2398 n = ((DbeInstr *) instr)->func->get_name ();
2403 n = instr->get_name ();
2405 long long addr = (long long) instr->get_addr ();
2406 fprintf (fd, NTXT ("%s %s (0x%08llx) -- ndesc = %lld\n"),
2407 t, n, addr, (long long) (NUM_DESCENDANTS (node)));
2409 // Recursively process all descendants
2410 int dsize = NUM_DESCENDANTS (node);
2411 if (dsize > maxwidth)
2413 for (int index = 0; index < dsize; index++)
2414 print (fd, NODE_IDX (node->descendants->fetch (index)), lvl + 1);
2418 PathTree::printn (FILE *fd)
2420 int n = dbg_nodes (root);
2421 fprintf (fd, GTXT ("Number of nodes: %d, total size: %d\n"), n, (int) (n * sizeof (Node)));
2425 PathTree::dumpNodes (FILE *fd, Histable *obj)
2429 NodeIdx node_idx = fn_map->get ((Function*) obj);
2430 Node *node = NODE_IDX (node_idx);
2433 fprintf (fd, GTXT ("No nodes associated with %s\n"), obj->get_name ());
2436 Histable *instr = node->instr;
2437 for (; node; node = NODE_IDX (node->funclist))
2439 instr = node->instr;
2440 if (instr->get_type () == Histable::LINE)
2443 n = ((DbeLine *) instr)->func->get_name ();
2445 else if (instr->get_type () == Histable::INSTR)
2448 n = ((DbeInstr *) instr)->func->get_name ();
2453 n = instr->get_name ();
2455 long long addr = (long long) instr->get_addr ();
2456 if (addr <= 0xFFFFFFFFU)
2457 fprintf (fd, NTXT ("0x%08x -- %s %s\n"), (uint32_t) addr, t, n);
2459 fprintf (fd, NTXT ("0x%016llX -- %s %s\n"), addr, t, n);
2464 PathTree::dbg_nodes (PathTree::Node *node)
2467 int dsize = NUM_DESCENDANTS (node);
2468 for (int index = 0; index < dsize; index++)
2469 res += dbg_nodes (NODE_IDX (node->descendants->fetch (index)));
2476 leak_alloc_comp (const void *s1, const void *s2)
2478 // See Hist_data::sort_compare() for duplicate code
2480 CStack_data::CStack_item *t1, *t2;
2481 t1 = *(CStack_data::CStack_item **)s1;
2482 t2 = *(CStack_data::CStack_item **)s2;
2484 switch (t1->value[mind_g].tag)
2487 if (t1->value[mind_g].i < t2->value[mind_g].i)
2489 else if (t1->value[mind_g].i > t2->value[mind_g].i)
2495 if (t1->value[mind_g].ll < t2->value[mind_g].ll)
2497 else if (t1->value[mind_g].ll > t2->value[mind_g].ll)
2503 if (t1->value[mind_g].ull < t2->value[mind_g].ull)
2505 else if (t1->value[mind_g].ull > t2->value[mind_g].ull)
2510 // ignoring the following cases (why?)
2520 // Sort in descending order
2525 PathTree::get_cstack_data (MetricList *mlist)
2528 CStack_data *lam = new CStack_data (mlist);
2529 int nmetrics = mlist->get_items ()->size ();
2531 xlate = new int[nmetrics];
2532 for (int mind = 0; mind < nmetrics; mind++)
2535 Metric *mtr = mlist->get_items ()->fetch (mind);
2536 if (mlist->get_sort_ref_index () == mind)
2538 xlate[mind] = find_slot (mtr->get_id ());
2541 // now fill in the actual data
2542 obj_list = new Histable*[depth];
2543 get_cstack_list (lam, root_idx, 0);
2547 lam->cstack_items->sort (leak_alloc_comp);
2553 PathTree::get_cstack_list (CStack_data *lam, NodeIdx node_idx, int dpth)
2556 Node *node = NODE_IDX (node_idx);
2557 obj_list[dpth] = node->instr;
2559 CStack_data::CStack_item *item = NULL;
2561 item = lam->new_cstack_item ();
2562 int nmetrics = lam->metrics->get_items ()->size ();
2563 bool subtree_empty = true;
2565 for (int mind = 0; mind < nmetrics; mind++)
2567 if (xlate[mind] == -1)
2569 if (IS_MVAL_ZERO (slots[xlate[mind]], node_idx))
2572 subtree_empty = false;
2575 ADD_METRIC_VAL (item->value[mind], slots[xlate[mind]], node_idx);
2576 ADD_METRIC_VAL (lam->total->value[mind], slots[xlate[mind]], node_idx);
2588 // Finish processing a leaf node
2589 item->stack = new Vector<DbeInstr*>(dpth);
2590 for (int i = 1; i <= dpth; i++)
2591 item->stack->append ((DbeInstr*) obj_list[i]);
2592 lam->cstack_items->append (item);
2596 // Recursively process all descendants
2597 int dsize = NUM_DESCENDANTS (node);
2598 for (int index = 0; index < dsize; index++)
2599 get_cstack_list (lam, node->descendants->fetch (index), dpth + 1);
2604 PathTree::fetch_stats ()
2608 return statsq->fetch ();
2612 PathTree::delete_stats ()
2617 statsq = new Emsgqueue (NTXT ("statsq"));
2622 PathTree::fetch_warnings ()
2624 if (warningq == NULL)
2626 return warningq->fetch ();
2630 PathTree::delete_warnings ()
2632 if (warningq != NULL)
2635 warningq = new Emsgqueue (NTXT ("warningq"));