New Romanian translation for ld
[binutils-gdb.git] / gprof / cg_arcs.c
blobcfffb09ff87b68cae7ec5310e71a49c74ec6cc2e
1 /*
2 * Copyright (c) 1983, 1993, 2001
3 * The Regents of the University of California. All rights reserved.
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.
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.
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"
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 *);
50 Sym *cycle_header;
51 unsigned int num_cycles;
52 Arc **arcs;
53 unsigned int numarcs;
56 * Return TRUE iff PARENT has an arc to covers the address
57 * range covered by CHILD.
59 Arc *
60 arc_lookup (Sym *parent, Sym *child)
62 Arc *arc;
64 if (!parent || !child)
66 printf ("[arc_lookup] parent == 0 || child == 0\n");
67 return 0;
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)
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)
78 return arc;
81 return 0;
86 * Add (or just increment) an arc:
88 void
89 arc_add (Sym *parent, Sym *child, unsigned long count)
91 static unsigned int maxarcs = 0;
92 Arc *arc, **newarcs;
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)
100 * A hit: just increment the count.
102 DBG (TALLYDEBUG, printf ("[tally] hit %lu += %lu\n",
103 arc->count, count));
104 arc->count += count;
105 return;
107 arc = (Arc *) xmalloc (sizeof (*arc));
108 memset (arc, 0, sizeof (*arc));
109 arc->parent = parent;
110 arc->child = child;
111 arc->count = count;
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)
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)
122 /* Determine how much space we want to allocate. */
123 if (maxarcs == 0)
124 maxarcs = 1;
125 maxarcs *= 2;
127 /* Allocate the new array. */
128 newarcs = (Arc **)xmalloc(sizeof (Arc *) * maxarcs);
130 /* Copy the old array's contents into the new array. */
131 memcpy (newarcs, arcs, numarcs * sizeof (Arc *));
133 /* Free up the old array. */
134 free (arcs);
136 /* And make the new array be the current array. */
137 arcs = newarcs;
140 /* Place this arc in the arc array. */
141 arcs[numarcs++] = arc;
144 /* prepend this child to the children of this parent: */
145 arc->next_child = parent->cg.children;
146 parent->cg.children = arc;
148 /* prepend this parent to the parents of this child: */
149 arc->next_parent = child->cg.parents;
150 child->cg.parents = arc;
154 static int
155 cmp_topo (const void *lp, const void *rp)
157 const Sym *left = *(const Sym **) lp;
158 const Sym *right = *(const Sym **) rp;
160 return left->cg.top_order - right->cg.top_order;
164 static void
165 propagate_time (Sym *parent)
167 Arc *arc;
168 Sym *child;
169 double share, prop_share;
171 if (parent->cg.prop.fract == 0.0)
173 return;
176 /* gather time from children of this parent: */
178 for (arc = parent->cg.children; arc; arc = arc->next_child)
180 child = arc->child;
181 if (arc->count == 0 || child == parent || child->cg.prop.fract == 0)
183 continue;
185 if (child->cg.cyc.head != child)
187 if (parent->cg.cyc.num == child->cg.cyc.num)
189 continue;
191 if (parent->cg.top_order <= child->cg.top_order)
193 fprintf (stderr, "[propagate] toporder botches\n");
195 child = child->cg.cyc.head;
197 else
199 if (parent->cg.top_order <= child->cg.top_order)
201 fprintf (stderr, "[propagate] toporder botches\n");
202 continue;
205 if (child->ncalls == 0)
207 continue;
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;
218 /* (1 - cg.prop.fract) gets lost along the way: */
219 prop_share = parent->cg.prop.fract * share;
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;
226 /* add this share to the parent's cycle header, if any: */
227 if (parent->cg.cyc.head != parent)
229 parent->cg.cyc.head->cg.child_time += share;
230 parent->cg.cyc.head->cg.prop.child += prop_share;
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));
245 * Compute the time of a cycle as the sum of the times of all
246 * its members.
248 static void
249 cycle_time (void)
251 Sym *member, *cyc;
253 for (cyc = &cycle_header[1]; cyc <= &cycle_header[num_cycles]; ++cyc)
255 for (member = cyc->cg.cyc.next; member; member = member->cg.cyc.next)
257 if (member->cg.prop.fract == 0.0)
260 * All members have the same propfraction except those
261 * that were excluded with -E.
263 continue;
265 cyc->hist.time += member->hist.time;
267 cyc->cg.prop.self = cyc->cg.prop.fract * cyc->hist.time;
272 static void
273 cycle_link (void)
275 Sym *sym, *cyc, *member;
276 Arc *arc;
277 int num;
279 /* count the number of cycles, and initialize the cycle lists: */
281 num_cycles = 0;
282 for (sym = symtab.base; sym < symtab.limit; ++sym)
284 /* this is how you find unattached cycles: */
285 if (sym->cg.cyc.head == sym && sym->cg.cyc.next)
287 ++num_cycles;
292 * cycle_header is indexed by cycle number: i.e. it is origin 1,
293 * not origin 0.
295 cycle_header = (Sym *) xmalloc ((num_cycles + 1) * sizeof (Sym));
298 * Now link cycles to true cycle-heads, number them, accumulate
299 * the data for the cycle.
301 num = 0;
302 cyc = cycle_header;
303 for (sym = symtab.base; sym < symtab.limit; ++sym)
305 if (!(sym->cg.cyc.head == sym && sym->cg.cyc.next != 0))
307 continue;
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));
321 /* link members to cycle header: */
322 for (member = sym; member; member = member->cg.cyc.next)
324 member->cg.cyc.num = num;
325 member->cg.cyc.head = cyc;
329 * Count calls from outside the cycle and those among cycle
330 * members:
332 for (member = sym; member; member = member->cg.cyc.next)
334 for (arc = member->cg.parents; arc; arc = arc->next_parent)
336 if (arc->parent == member)
338 continue;
340 if (arc->parent->cg.cyc.num == num)
342 cyc->cg.self_calls += arc->count;
344 else
346 cyc->ncalls += arc->count;
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.
360 static void
361 inherit_flags (Sym *child)
363 Sym *head, *parent, *member;
364 Arc *arc;
366 head = child->cg.cyc.head;
367 if (child == head)
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)
374 parent = arc->parent;
375 if (child == parent)
377 continue;
379 child->cg.print_flag |= parent->cg.print_flag;
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.
385 if (child->ncalls != 0)
387 child->cg.prop.fract += parent->cg.prop.fract
388 * (((double) arc->count) / ((double) child->ncalls));
392 else
395 * Its a member of a cycle, look at all parents from outside
396 * the cycle.
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)
402 for (arc = member->cg.parents; arc; arc = arc->next_parent)
404 if (arc->parent->cg.cyc.head == head)
406 continue;
408 parent = arc->parent;
409 head->cg.print_flag |= parent->cg.print_flag;
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.
415 if (head->ncalls != 0)
417 head->cg.prop.fract += parent->cg.prop.fract
418 * (((double) arc->count) / ((double) head->ncalls));
422 for (member = head; member; member = member->cg.cyc.next)
424 member->cg.print_flag = head->cg.print_flag;
425 member->cg.prop.fract = head->cg.prop.fract;
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.
438 static void
439 propagate_flags (Sym **symbols)
441 int sym_index;
442 Sym *old_head, *child;
444 old_head = 0;
445 for (sym_index = symtab.len - 1; sym_index >= 0; --sym_index)
447 child = symbols[sym_index];
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.
454 if (child->cg.cyc.head != old_head)
456 old_head = child->cg.cyc.head;
457 inherit_flags (child);
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)
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.
471 if (sym_lookup (&syms[INCL_GRAPH], child->addr)
472 || (syms[INCL_GRAPH].len == 0
473 && !sym_lookup (&syms[EXCL_GRAPH], child->addr)))
475 child->cg.print_flag = true;
478 else
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.)
485 if (!sym_lookup (&syms[INCL_GRAPH], child->addr)
486 && sym_lookup (&syms[EXCL_GRAPH], child->addr))
488 child->cg.print_flag = false;
491 if (child->cg.prop.fract == 0.0)
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.
498 if (sym_lookup (&syms[INCL_TIME], child->addr)
499 || (syms[INCL_TIME].len == 0
500 && !sym_lookup (&syms[EXCL_TIME], child->addr)))
502 child->cg.prop.fract = 1.0;
505 else
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.)
513 if (!sym_lookup (&syms[INCL_TIME], child->addr)
514 && sym_lookup (&syms[EXCL_TIME], child->addr))
516 child->cg.prop.fract = 0.0;
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));
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.
538 static int
539 cmp_total (const void *lp, const void *rp)
541 const Sym *left = *(const Sym **) lp;
542 const Sym *right = *(const Sym **) rp;
543 double diff;
545 diff = (left->cg.prop.self + left->cg.prop.child)
546 - (right->cg.prop.self + right->cg.prop.child);
547 if (diff < 0.0)
549 return 1;
551 if (diff > 0.0)
553 return -1;
555 if (!left->name && left->cg.cyc.num != 0)
557 return -1;
559 if (!right->name && right->cg.cyc.num != 0)
561 return 1;
563 if (!left->name)
565 return -1;
567 if (!right->name)
569 return 1;
571 if (left->name[0] != '_' && right->name[0] == '_')
573 return -1;
575 if (left->name[0] == '_' && right->name[0] != '_')
577 return 1;
579 if (left->ncalls > right->ncalls)
581 return -1;
583 if (left->ncalls < right->ncalls)
585 return 1;
587 return strcmp (left->name, right->name);
591 /* Topologically sort the graph (collapsing cycles), and propagates
592 time bottom up and flags top down. */
594 Sym **
595 cg_assemble (void)
597 Sym *parent, **time_sorted_syms, **top_sorted_syms;
598 unsigned int sym_index;
599 Arc *arc;
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++)
607 parent->cg.child_time = 0.0;
608 arc = arc_lookup (parent, parent);
609 if (arc && parent == arc->child)
611 parent->ncalls -= arc->count;
612 parent->cg.self_calls = arc->count;
614 else
616 parent->cg.self_calls = 0;
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);
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++)
638 if (parent->cg.top_order == DFN_NAN)
639 cg_dfn (parent);
642 /* Link together nodes on the same cycle. */
643 cycle_link ();
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];
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)
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");
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);
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]);
674 free (top_sorted_syms);
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];
682 for (sym_index = 1; sym_index <= num_cycles; sym_index++)
683 time_sorted_syms[symtab.len + sym_index - 1] = &cycle_header[sym_index];
685 qsort (time_sorted_syms, symtab.len + num_cycles, sizeof (Sym *),
686 cmp_total);
688 for (sym_index = 0; sym_index < symtab.len + num_cycles; sym_index++)
689 time_sorted_syms[sym_index]->cg.index = sym_index + 1;
691 return time_sorted_syms;