xref: /netbsd-src/external/gpl3/binutils/dist/gprof/cg_arcs.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
1 /*
2  * Copyright (c) 1983, 1993, 2001
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include "gprof.h"
30 #include "libiberty.h"
31 #include "search_list.h"
32 #include "source.h"
33 #include "symtab.h"
34 #include "call_graph.h"
35 #include "cg_arcs.h"
36 #include "cg_dfn.h"
37 #include "cg_print.h"
38 #include "utils.h"
39 #include "sym_ids.h"
40 #include "corefile.h"
41 
42 static int cmp_topo (const void *, const void *);
43 static void propagate_time (Sym *);
44 static void cycle_time (void);
45 static void cycle_link (void);
46 static void inherit_flags (Sym *);
47 static void propagate_flags (Sym **);
48 static int cmp_total (const void *, const void *);
49 
50 Sym *cycle_header;
51 unsigned int num_cycles;
52 Arc **arcs;
53 unsigned int numarcs;
54 
55 /*
56  * Return TRUE iff PARENT has an arc to covers the address
57  * range covered by CHILD.
58  */
59 Arc *
arc_lookup(Sym * parent,Sym * child)60 arc_lookup (Sym *parent, Sym *child)
61 {
62   Arc *arc;
63 
64   if (!parent || !child)
65     {
66       printf ("[arc_lookup] parent == 0 || child == 0\n");
67       return 0;
68     }
69   DBG (LOOKUPDEBUG, printf ("[arc_lookup] parent %s child %s\n",
70 			    parent->name, child->name));
71   for (arc = parent->cg.children; arc; arc = arc->next_child)
72     {
73       DBG (LOOKUPDEBUG, printf ("[arc_lookup]\t parent %s child %s\n",
74 				arc->parent->name, arc->child->name));
75       if (child->addr >= arc->child->addr
76 	  && child->end_addr <= arc->child->end_addr)
77 	{
78 	  return arc;
79 	}
80     }
81   return 0;
82 }
83 
84 
85 /*
86  * Add (or just increment) an arc:
87  */
88 void
arc_add(Sym * parent,Sym * child,unsigned long count)89 arc_add (Sym *parent, Sym *child, unsigned long count)
90 {
91   static unsigned int maxarcs = 0;
92   Arc *arc, **newarcs;
93 
94   DBG (TALLYDEBUG, printf ("[arc_add] %lu arcs from %s to %s\n",
95 			   count, parent->name, child->name));
96   arc = arc_lookup (parent, child);
97   if (arc)
98     {
99       /*
100        * A hit: just increment the count.
101        */
102       DBG (TALLYDEBUG, printf ("[tally] hit %lu += %lu\n",
103 			       arc->count, count));
104       arc->count += count;
105       return;
106     }
107   arc = (Arc *) xmalloc (sizeof (*arc));
108   memset (arc, 0, sizeof (*arc));
109   arc->parent = parent;
110   arc->child = child;
111   arc->count = count;
112 
113   /* If this isn't an arc for a recursive call to parent, then add it
114      to the array of arcs.  */
115   if (parent != child)
116     {
117       /* If we've exhausted space in our current array, get a new one
118 	 and copy the contents.   We might want to throttle the doubling
119 	 factor one day.  */
120       if (numarcs == maxarcs)
121 	{
122 	  /* Determine how much space we want to allocate.  */
123 	  if (maxarcs == 0)
124 	    maxarcs = 1;
125 	  maxarcs *= 2;
126 
127 	  /* Allocate the new array.  */
128 	  newarcs = (Arc **)xmalloc(sizeof (Arc *) * maxarcs);
129 
130 	  /* Copy the old array's contents into the new array.  */
131 	  memcpy (newarcs, arcs, numarcs * sizeof (Arc *));
132 
133 	  /* Free up the old array.  */
134 	  free (arcs);
135 
136 	  /* And make the new array be the current array.  */
137 	  arcs = newarcs;
138 	}
139 
140       /* Place this arc in the arc array.  */
141       arcs[numarcs++] = arc;
142     }
143 
144   /* prepend this child to the children of this parent: */
145   arc->next_child = parent->cg.children;
146   parent->cg.children = arc;
147 
148   /* prepend this parent to the parents of this child: */
149   arc->next_parent = child->cg.parents;
150   child->cg.parents = arc;
151 }
152 
153 
154 static int
cmp_topo(const void * lp,const void * rp)155 cmp_topo (const void *lp, const void *rp)
156 {
157   const Sym *left = *(const Sym **) lp;
158   const Sym *right = *(const Sym **) rp;
159 
160   return left->cg.top_order - right->cg.top_order;
161 }
162 
163 
164 static void
propagate_time(Sym * parent)165 propagate_time (Sym *parent)
166 {
167   Arc *arc;
168   Sym *child;
169   double share, prop_share;
170 
171   if (parent->cg.prop.fract == 0.0)
172     {
173       return;
174     }
175 
176   /* gather time from children of this parent: */
177 
178   for (arc = parent->cg.children; arc; arc = arc->next_child)
179     {
180       child = arc->child;
181       if (arc->count == 0 || child == parent || child->cg.prop.fract == 0)
182 	{
183 	  continue;
184 	}
185       if (child->cg.cyc.head != child)
186 	{
187 	  if (parent->cg.cyc.num == child->cg.cyc.num)
188 	    {
189 	      continue;
190 	    }
191 	  if (parent->cg.top_order <= child->cg.top_order)
192 	    {
193 	      fprintf (stderr, "[propagate] toporder botches\n");
194 	    }
195 	  child = child->cg.cyc.head;
196 	}
197       else
198 	{
199 	  if (parent->cg.top_order <= child->cg.top_order)
200 	    {
201 	      fprintf (stderr, "[propagate] toporder botches\n");
202 	      continue;
203 	    }
204 	}
205       if (child->ncalls == 0)
206 	{
207 	  continue;
208 	}
209 
210       /* distribute time for this arc: */
211       arc->time = child->hist.time * (((double) arc->count)
212 				      / ((double) child->ncalls));
213       arc->child_time = child->cg.child_time
214 	* (((double) arc->count) / ((double) child->ncalls));
215       share = arc->time + arc->child_time;
216       parent->cg.child_time += share;
217 
218       /* (1 - cg.prop.fract) gets lost along the way: */
219       prop_share = parent->cg.prop.fract * share;
220 
221       /* fix things for printing: */
222       parent->cg.prop.child += prop_share;
223       arc->time *= parent->cg.prop.fract;
224       arc->child_time *= parent->cg.prop.fract;
225 
226       /* add this share to the parent's cycle header, if any: */
227       if (parent->cg.cyc.head != parent)
228 	{
229 	  parent->cg.cyc.head->cg.child_time += share;
230 	  parent->cg.cyc.head->cg.prop.child += prop_share;
231 	}
232       DBG (PROPDEBUG,
233 	   printf ("[prop_time] child \t");
234 	   print_name (child);
235 	   printf (" with %f %f %lu/%lu\n", child->hist.time,
236 		   child->cg.child_time, arc->count, child->ncalls);
237 	   printf ("[prop_time] parent\t");
238 	   print_name (parent);
239 	   printf ("\n[prop_time] share %f\n", share));
240     }
241 }
242 
243 
244 /*
245  * Compute the time of a cycle as the sum of the times of all
246  * its members.
247  */
248 static void
cycle_time(void)249 cycle_time (void)
250 {
251   Sym *member, *cyc;
252 
253   for (cyc = &cycle_header[1]; cyc <= &cycle_header[num_cycles]; ++cyc)
254     {
255       for (member = cyc->cg.cyc.next; member; member = member->cg.cyc.next)
256 	{
257 	  if (member->cg.prop.fract == 0.0)
258 	    {
259 	      /*
260 	       * All members have the same propfraction except those
261 	       * that were excluded with -E.
262 	       */
263 	      continue;
264 	    }
265 	  cyc->hist.time += member->hist.time;
266 	}
267       cyc->cg.prop.self = cyc->cg.prop.fract * cyc->hist.time;
268     }
269 }
270 
271 
272 static void
cycle_link(void)273 cycle_link (void)
274 {
275   Sym *sym, *cyc, *member;
276   Arc *arc;
277   int num;
278 
279   /* count the number of cycles, and initialize the cycle lists: */
280 
281   num_cycles = 0;
282   for (sym = symtab.base; sym < symtab.limit; ++sym)
283     {
284       /* this is how you find unattached cycles: */
285       if (sym->cg.cyc.head == sym && sym->cg.cyc.next)
286 	{
287 	  ++num_cycles;
288 	}
289     }
290 
291   /*
292    * cycle_header is indexed by cycle number: i.e. it is origin 1,
293    * not origin 0.
294    */
295   cycle_header = (Sym *) xmalloc ((num_cycles + 1) * sizeof (Sym));
296 
297   /*
298    * Now link cycles to true cycle-heads, number them, accumulate
299    * the data for the cycle.
300    */
301   num = 0;
302   cyc = cycle_header;
303   for (sym = symtab.base; sym < symtab.limit; ++sym)
304     {
305       if (!(sym->cg.cyc.head == sym && sym->cg.cyc.next != 0))
306 	{
307 	  continue;
308 	}
309       ++num;
310       ++cyc;
311       sym_init (cyc);
312       cyc->cg.print_flag = true;	/* should this be printed? */
313       cyc->cg.top_order = DFN_NAN;	/* graph call chain top-sort order */
314       cyc->cg.cyc.num = num;	/* internal number of cycle on */
315       cyc->cg.cyc.head = cyc;	/* pointer to head of cycle */
316       cyc->cg.cyc.next = sym;	/* pointer to next member of cycle */
317       DBG (CYCLEDEBUG, printf ("[cycle_link] ");
318 	   print_name (sym);
319 	   printf (" is the head of cycle %d\n", num));
320 
321       /* link members to cycle header: */
322       for (member = sym; member; member = member->cg.cyc.next)
323 	{
324 	  member->cg.cyc.num = num;
325 	  member->cg.cyc.head = cyc;
326 	}
327 
328       /*
329        * Count calls from outside the cycle and those among cycle
330        * members:
331        */
332       for (member = sym; member; member = member->cg.cyc.next)
333 	{
334 	  for (arc = member->cg.parents; arc; arc = arc->next_parent)
335 	    {
336 	      if (arc->parent == member)
337 		{
338 		  continue;
339 		}
340 	      if (arc->parent->cg.cyc.num == num)
341 		{
342 		  cyc->cg.self_calls += arc->count;
343 		}
344 	      else
345 		{
346 		  cyc->ncalls += arc->count;
347 		}
348 	    }
349 	}
350     }
351 }
352 
353 
354 /*
355  * Check if any parent of this child (or outside parents of this
356  * cycle) have their print flags on and set the print flag of the
357  * child (cycle) appropriately.  Similarly, deal with propagation
358  * fractions from parents.
359  */
360 static void
inherit_flags(Sym * child)361 inherit_flags (Sym *child)
362 {
363   Sym *head, *parent, *member;
364   Arc *arc;
365 
366   head = child->cg.cyc.head;
367   if (child == head)
368     {
369       /* just a regular child, check its parents: */
370       child->cg.print_flag = false;
371       child->cg.prop.fract = 0.0;
372       for (arc = child->cg.parents; arc; arc = arc->next_parent)
373 	{
374 	  parent = arc->parent;
375 	  if (child == parent)
376 	    {
377 	      continue;
378 	    }
379 	  child->cg.print_flag |= parent->cg.print_flag;
380 	  /*
381 	   * If the child was never actually called (e.g., this arc
382 	   * is static (and all others are, too)) no time propagates
383 	   * along this arc.
384 	   */
385 	  if (child->ncalls != 0)
386 	    {
387 	      child->cg.prop.fract += parent->cg.prop.fract
388 		* (((double) arc->count) / ((double) child->ncalls));
389 	    }
390 	}
391     }
392   else
393     {
394       /*
395        * Its a member of a cycle, look at all parents from outside
396        * the cycle.
397        */
398       head->cg.print_flag = false;
399       head->cg.prop.fract = 0.0;
400       for (member = head->cg.cyc.next; member; member = member->cg.cyc.next)
401 	{
402 	  for (arc = member->cg.parents; arc; arc = arc->next_parent)
403 	    {
404 	      if (arc->parent->cg.cyc.head == head)
405 		{
406 		  continue;
407 		}
408 	      parent = arc->parent;
409 	      head->cg.print_flag |= parent->cg.print_flag;
410 	      /*
411 	       * If the cycle was never actually called (e.g. this
412 	       * arc is static (and all others are, too)) no time
413 	       * propagates along this arc.
414 	       */
415 	      if (head->ncalls != 0)
416 		{
417 		  head->cg.prop.fract += parent->cg.prop.fract
418 		    * (((double) arc->count) / ((double) head->ncalls));
419 		}
420 	    }
421 	}
422       for (member = head; member; member = member->cg.cyc.next)
423 	{
424 	  member->cg.print_flag = head->cg.print_flag;
425 	  member->cg.prop.fract = head->cg.prop.fract;
426 	}
427     }
428 }
429 
430 
431 /*
432  * In one top-to-bottom pass over the topologically sorted symbols
433  * propagate:
434  *      cg.print_flag as the union of parents' print_flags
435  *      propfraction as the sum of fractional parents' propfractions
436  * and while we're here, sum time for functions.
437  */
438 static void
propagate_flags(Sym ** symbols)439 propagate_flags (Sym **symbols)
440 {
441   int sym_index;
442   Sym *old_head, *child;
443 
444   old_head = 0;
445   for (sym_index = symtab.len - 1; sym_index >= 0; --sym_index)
446     {
447       child = symbols[sym_index];
448       /*
449        * If we haven't done this function or cycle, inherit things
450        * from parent.  This way, we are linear in the number of arcs
451        * since we do all members of a cycle (and the cycle itself)
452        * as we hit the first member of the cycle.
453        */
454       if (child->cg.cyc.head != old_head)
455 	{
456 	  old_head = child->cg.cyc.head;
457 	  inherit_flags (child);
458 	}
459       DBG (PROPDEBUG,
460 	   printf ("[prop_flags] ");
461 	   print_name (child);
462 	   printf ("inherits print-flag %d and prop-fract %f\n",
463 		   child->cg.print_flag, child->cg.prop.fract));
464       if (!child->cg.print_flag)
465 	{
466 	  /*
467 	   * Printflag is off. It gets turned on by being in the
468 	   * INCL_GRAPH table, or there being an empty INCL_GRAPH
469 	   * table and not being in the EXCL_GRAPH table.
470 	   */
471 	  if (sym_lookup (&syms[INCL_GRAPH], child->addr)
472 	      || (syms[INCL_GRAPH].len == 0
473 		  && !sym_lookup (&syms[EXCL_GRAPH], child->addr)))
474 	    {
475 	      child->cg.print_flag = true;
476 	    }
477 	}
478       else
479 	{
480 	  /*
481 	   * This function has printing parents: maybe someone wants
482 	   * to shut it up by putting it in the EXCL_GRAPH table.
483 	   * (But favor INCL_GRAPH over EXCL_GRAPH.)
484 	   */
485 	  if (!sym_lookup (&syms[INCL_GRAPH], child->addr)
486 	      && sym_lookup (&syms[EXCL_GRAPH], child->addr))
487 	    {
488 	      child->cg.print_flag = false;
489 	    }
490 	}
491       if (child->cg.prop.fract == 0.0)
492 	{
493 	  /*
494 	   * No parents to pass time to.  Collect time from children
495 	   * if its in the INCL_TIME table, or there is an empty
496 	   * INCL_TIME table and its not in the EXCL_TIME table.
497 	   */
498 	  if (sym_lookup (&syms[INCL_TIME], child->addr)
499 	      || (syms[INCL_TIME].len == 0
500 		  && !sym_lookup (&syms[EXCL_TIME], child->addr)))
501 	    {
502 	      child->cg.prop.fract = 1.0;
503 	    }
504 	}
505       else
506 	{
507 	  /*
508 	   * It has parents to pass time to, but maybe someone wants
509 	   * to shut it up by puttting it in the EXCL_TIME table.
510 	   * (But favor being in INCL_TIME tabe over being in
511 	   * EXCL_TIME table.)
512 	   */
513 	  if (!sym_lookup (&syms[INCL_TIME], child->addr)
514 	      && sym_lookup (&syms[EXCL_TIME], child->addr))
515 	    {
516 	      child->cg.prop.fract = 0.0;
517 	    }
518 	}
519       child->cg.prop.self = child->hist.time * child->cg.prop.fract;
520       print_time += child->cg.prop.self;
521       DBG (PROPDEBUG,
522 	   printf ("[prop_flags] ");
523 	   print_name (child);
524 	   printf (" ends up with printflag %d and prop-fract %f\n",
525 		   child->cg.print_flag, child->cg.prop.fract);
526 	   printf ("[prop_flags] time %f propself %f print_time %f\n",
527 		   child->hist.time, child->cg.prop.self, print_time));
528     }
529 }
530 
531 
532 /*
533  * Compare by decreasing propagated time.  If times are equal, but one
534  * is a cycle header, say that's first (e.g. less, i.e. -1).  If one's
535  * name doesn't have an underscore and the other does, say that one is
536  * first.  All else being equal, compare by names.
537  */
538 static int
cmp_total(const void * lp,const void * rp)539 cmp_total (const void *lp, const void *rp)
540 {
541   const Sym *left = *(const Sym **) lp;
542   const Sym *right = *(const Sym **) rp;
543   double diff;
544 
545   diff = (left->cg.prop.self + left->cg.prop.child)
546     - (right->cg.prop.self + right->cg.prop.child);
547   if (diff < 0.0)
548     {
549       return 1;
550     }
551   if (diff > 0.0)
552     {
553       return -1;
554     }
555   if (!left->name && left->cg.cyc.num != 0)
556     {
557       return -1;
558     }
559   if (!right->name && right->cg.cyc.num != 0)
560     {
561       return 1;
562     }
563   if (!left->name)
564     {
565       return -1;
566     }
567   if (!right->name)
568     {
569       return 1;
570     }
571   if (left->name[0] != '_' && right->name[0] == '_')
572     {
573       return -1;
574     }
575   if (left->name[0] == '_' && right->name[0] != '_')
576     {
577       return 1;
578     }
579   if (left->ncalls > right->ncalls)
580     {
581       return -1;
582     }
583   if (left->ncalls < right->ncalls)
584     {
585       return 1;
586     }
587   return strcmp (left->name, right->name);
588 }
589 
590 
591 /* Topologically sort the graph (collapsing cycles), and propagates
592    time bottom up and flags top down.  */
593 
594 Sym **
cg_assemble(void)595 cg_assemble (void)
596 {
597   Sym *parent, **time_sorted_syms, **top_sorted_syms;
598   unsigned int sym_index;
599   Arc *arc;
600 
601   /* Initialize various things:
602        Zero out child times.
603        Count self-recursive calls.
604        Indicate that nothing is on cycles.  */
605   for (parent = symtab.base; parent < symtab.limit; parent++)
606     {
607       parent->cg.child_time = 0.0;
608       arc = arc_lookup (parent, parent);
609       if (arc && parent == arc->child)
610 	{
611 	  parent->ncalls -= arc->count;
612 	  parent->cg.self_calls = arc->count;
613 	}
614       else
615 	{
616 	  parent->cg.self_calls = 0;
617 	}
618       parent->cg.prop.fract = 0.0;
619       parent->cg.prop.self = 0.0;
620       parent->cg.prop.child = 0.0;
621       parent->cg.print_flag = false;
622       parent->cg.top_order = DFN_NAN;
623       parent->cg.cyc.num = 0;
624       parent->cg.cyc.head = parent;
625       parent->cg.cyc.next = 0;
626       if (ignore_direct_calls
627 	  && parent->addr >= core_text_sect->vma
628 	  && parent->addr < core_text_sect->vma +  core_text_sect->size
629 	  && (parent + 1)->addr >= core_text_sect->vma
630 	  && (parent + 1)->addr <= core_text_sect->vma +  core_text_sect->size)
631 	find_call (parent, parent->addr, (parent + 1)->addr);
632     }
633 
634   /* Topologically order things.  If any node is unnumbered, number
635      it and any of its descendents.  */
636   for (parent = symtab.base; parent < symtab.limit; parent++)
637     {
638       if (parent->cg.top_order == DFN_NAN)
639 	cg_dfn (parent);
640     }
641 
642   /* Link together nodes on the same cycle.  */
643   cycle_link ();
644 
645   /* Sort the symbol table in reverse topological order.  */
646   top_sorted_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
647   for (sym_index = 0; sym_index < symtab.len; ++sym_index)
648     top_sorted_syms[sym_index] = &symtab.base[sym_index];
649 
650   qsort (top_sorted_syms, symtab.len, sizeof (Sym *), cmp_topo);
651   DBG (DFNDEBUG,
652        printf ("[cg_assemble] topological sort listing\n");
653        for (sym_index = 0; sym_index < symtab.len; ++sym_index)
654 	 {
655 	   printf ("[cg_assemble] ");
656 	   printf ("%d:", top_sorted_syms[sym_index]->cg.top_order);
657 	   print_name (top_sorted_syms[sym_index]);
658 	   printf ("\n");
659 	 }
660   );
661 
662   /* Starting from the topological top, propagate print flags to
663      children.  also, calculate propagation fractions.  this happens
664      before time propagation since time propagation uses the
665      fractions.  */
666   propagate_flags (top_sorted_syms);
667 
668   /* Starting from the topological bottom, propagate children times
669      up to parents.  */
670   cycle_time ();
671   for (sym_index = 0; sym_index < symtab.len; ++sym_index)
672     propagate_time (top_sorted_syms[sym_index]);
673 
674   free (top_sorted_syms);
675 
676   /* Now, sort by CG.PROP.SELF + CG.PROP.CHILD.  Sorting both the regular
677      function names and cycle headers.  */
678   time_sorted_syms = (Sym **) xmalloc ((symtab.len + num_cycles) * sizeof (Sym *));
679   for (sym_index = 0; sym_index < symtab.len; sym_index++)
680     time_sorted_syms[sym_index] = &symtab.base[sym_index];
681 
682   for (sym_index = 1; sym_index <= num_cycles; sym_index++)
683     time_sorted_syms[symtab.len + sym_index - 1] = &cycle_header[sym_index];
684 
685   qsort (time_sorted_syms, symtab.len + num_cycles, sizeof (Sym *),
686 	 cmp_total);
687 
688   for (sym_index = 0; sym_index < symtab.len + num_cycles; sym_index++)
689     time_sorted_syms[sym_index]->cg.index = sym_index + 1;
690 
691   return time_sorted_syms;
692 }
693