cg_print.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /* cg_print.c - Print routines for displaying call graphs.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "gprof.h"
  17. #include "libiberty.h"
  18. #include "filenames.h"
  19. #include "search_list.h"
  20. #include "source.h"
  21. #include "symtab.h"
  22. #include "cg_arcs.h"
  23. #include "cg_print.h"
  24. #include "hist.h"
  25. #include "utils.h"
  26. #include "corefile.h"
  27. /* Return value of comparison functions used to sort tables. */
  28. #define LESSTHAN -1
  29. #define EQUALTO 0
  30. #define GREATERTHAN 1
  31. static void print_header (void);
  32. static void print_cycle (Sym *);
  33. static int cmp_member (Sym *, Sym *);
  34. static void sort_members (Sym *);
  35. static void print_members (Sym *);
  36. static int cmp_arc (Arc *, Arc *);
  37. static void sort_parents (Sym *);
  38. static void print_parents (Sym *);
  39. static void sort_children (Sym *);
  40. static void print_children (Sym *);
  41. static void print_line (Sym *);
  42. static int cmp_name (const PTR, const PTR);
  43. static int cmp_arc_count (const PTR, const PTR);
  44. static int cmp_fun_nuses (const PTR, const PTR);
  45. static void order_and_dump_functions_by_arcs
  46. (Arc **, unsigned long, int, Arc **, unsigned long *);
  47. /* Declarations of automatically generated functions to output blurbs. */
  48. extern void bsd_callg_blurb (FILE * fp);
  49. extern void fsf_callg_blurb (FILE * fp);
  50. double print_time = 0.0;
  51. static void
  52. print_header (void)
  53. {
  54. if (first_output)
  55. first_output = false;
  56. else
  57. printf ("\f\n");
  58. if (!bsd_style_output)
  59. {
  60. if (print_descriptions)
  61. printf (_("\t\t Call graph (explanation follows)\n\n"));
  62. else
  63. printf (_("\t\t\tCall graph\n\n"));
  64. }
  65. printf (_("\ngranularity: each sample hit covers %ld byte(s)"),
  66. (long) hist_scale * (long) sizeof (UNIT));
  67. if (print_time > 0.0)
  68. printf (_(" for %.2f%% of %.2f seconds\n\n"),
  69. 100.0 / print_time, print_time / hz);
  70. else
  71. {
  72. printf (_(" no time propagated\n\n"));
  73. /* This doesn't hurt, since all the numerators will be 0.0. */
  74. print_time = 1.0;
  75. }
  76. if (bsd_style_output)
  77. {
  78. printf ("%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n",
  79. "", "", "", "", _("called"), _("total"), _("parents"));
  80. printf ("%-6.6s %5.5s %7.7s %11.11s %7.7s+%-7.7s %-8.8s\t%5.5s\n",
  81. _("index"),
  82. /* xgettext:no-c-format */
  83. _("%time"),
  84. _("self"), _("descendants"), _("called"), _("self"),
  85. _("name"), _("index"));
  86. printf ("%6.6s %5.5s %7.7s %11.11s %7.7s/%-7.7s %-8.8s\n",
  87. "", "", "", "", _("called"), _("total"), _("children"));
  88. printf ("\n");
  89. }
  90. else
  91. {
  92. printf (_("index %% time self children called name\n"));
  93. }
  94. }
  95. /* Print a cycle header. */
  96. static void
  97. print_cycle (Sym *cyc)
  98. {
  99. char buf[BUFSIZ];
  100. sprintf (buf, "[%d]", cyc->cg.index);
  101. printf (bsd_style_output
  102. ? "%-6.6s %5.1f %7.2f %11.2f %7lu"
  103. : "%-6.6s %5.1f %7.2f %7.2f %7lu", buf,
  104. 100 * (cyc->cg.prop.self + cyc->cg.prop.child) / print_time,
  105. cyc->cg.prop.self / hz, cyc->cg.prop.child / hz, cyc->ncalls);
  106. if (cyc->cg.self_calls != 0)
  107. printf ("+%-7lu", cyc->cg.self_calls);
  108. else
  109. printf (" %7.7s", "");
  110. printf (_(" <cycle %d as a whole> [%d]\n"), cyc->cg.cyc.num, cyc->cg.index);
  111. }
  112. /* Compare LEFT and RIGHT membmer. Major comparison key is
  113. CG.PROP.SELF+CG.PROP.CHILD, secondary key is NCALLS+CG.SELF_CALLS. */
  114. static int
  115. cmp_member (Sym *left, Sym *right)
  116. {
  117. double left_time = left->cg.prop.self + left->cg.prop.child;
  118. double right_time = right->cg.prop.self + right->cg.prop.child;
  119. unsigned long left_calls = left->ncalls + left->cg.self_calls;
  120. unsigned long right_calls = right->ncalls + right->cg.self_calls;
  121. if (left_time > right_time)
  122. return GREATERTHAN;
  123. if (left_time < right_time)
  124. return LESSTHAN;
  125. if (left_calls > right_calls)
  126. return GREATERTHAN;
  127. if (left_calls < right_calls)
  128. return LESSTHAN;
  129. return EQUALTO;
  130. }
  131. /* Sort members of a cycle. */
  132. static void
  133. sort_members (Sym *cyc)
  134. {
  135. Sym *todo, *doing, *prev;
  136. /* Detach cycle members from cyclehead,
  137. and insertion sort them back on. */
  138. todo = cyc->cg.cyc.next;
  139. cyc->cg.cyc.next = 0;
  140. for (doing = todo; doing != NULL; doing = todo)
  141. {
  142. todo = doing->cg.cyc.next;
  143. for (prev = cyc; prev->cg.cyc.next; prev = prev->cg.cyc.next)
  144. {
  145. if (cmp_member (doing, prev->cg.cyc.next) == GREATERTHAN)
  146. break;
  147. }
  148. doing->cg.cyc.next = prev->cg.cyc.next;
  149. prev->cg.cyc.next = doing;
  150. }
  151. }
  152. /* Print the members of a cycle. */
  153. static void
  154. print_members (Sym *cyc)
  155. {
  156. Sym *member;
  157. sort_members (cyc);
  158. for (member = cyc->cg.cyc.next; member; member = member->cg.cyc.next)
  159. {
  160. printf (bsd_style_output
  161. ? "%6.6s %5.5s %7.2f %11.2f %7lu"
  162. : "%6.6s %5.5s %7.2f %7.2f %7lu",
  163. "", "", member->cg.prop.self / hz, member->cg.prop.child / hz,
  164. member->ncalls);
  165. if (member->cg.self_calls != 0)
  166. printf ("+%-7lu", member->cg.self_calls);
  167. else
  168. printf (" %7.7s", "");
  169. printf (" ");
  170. print_name (member);
  171. printf ("\n");
  172. }
  173. }
  174. /* Compare two arcs to/from the same child/parent.
  175. - if one arc is a self arc, it's least.
  176. - if one arc is within a cycle, it's less than.
  177. - if both arcs are within a cycle, compare arc counts.
  178. - if neither arc is within a cycle, compare with
  179. time + child_time as major key
  180. arc count as minor key. */
  181. static int
  182. cmp_arc (Arc *left, Arc *right)
  183. {
  184. Sym *left_parent = left->parent;
  185. Sym *left_child = left->child;
  186. Sym *right_parent = right->parent;
  187. Sym *right_child = right->child;
  188. double left_time, right_time;
  189. DBG (TIMEDEBUG,
  190. printf ("[cmp_arc] ");
  191. print_name (left_parent);
  192. printf (" calls ");
  193. print_name (left_child);
  194. printf (" %f + %f %lu/%lu\n", left->time, left->child_time,
  195. left->count, left_child->ncalls);
  196. printf ("[cmp_arc] ");
  197. print_name (right_parent);
  198. printf (" calls ");
  199. print_name (right_child);
  200. printf (" %f + %f %lu/%lu\n", right->time, right->child_time,
  201. right->count, right_child->ncalls);
  202. printf ("\n");
  203. );
  204. if (left_parent == left_child)
  205. return LESSTHAN; /* Left is a self call. */
  206. if (right_parent == right_child)
  207. return GREATERTHAN; /* Right is a self call. */
  208. if (left_parent->cg.cyc.num != 0 && left_child->cg.cyc.num != 0
  209. && left_parent->cg.cyc.num == left_child->cg.cyc.num)
  210. {
  211. /* Left is a call within a cycle. */
  212. if (right_parent->cg.cyc.num != 0 && right_child->cg.cyc.num != 0
  213. && right_parent->cg.cyc.num == right_child->cg.cyc.num)
  214. {
  215. /* Right is a call within the cycle, too. */
  216. if (left->count < right->count)
  217. return LESSTHAN;
  218. if (left->count > right->count)
  219. return GREATERTHAN;
  220. return EQUALTO;
  221. }
  222. else
  223. {
  224. /* Right isn't a call within the cycle. */
  225. return LESSTHAN;
  226. }
  227. }
  228. else
  229. {
  230. /* Left isn't a call within a cycle. */
  231. if (right_parent->cg.cyc.num != 0 && right_child->cg.cyc.num != 0
  232. && right_parent->cg.cyc.num == right_child->cg.cyc.num)
  233. {
  234. /* Right is a call within a cycle. */
  235. return GREATERTHAN;
  236. }
  237. else
  238. {
  239. /* Neither is a call within a cycle. */
  240. left_time = left->time + left->child_time;
  241. right_time = right->time + right->child_time;
  242. if (left_time < right_time)
  243. return LESSTHAN;
  244. if (left_time > right_time)
  245. return GREATERTHAN;
  246. if (left->count < right->count)
  247. return LESSTHAN;
  248. if (left->count > right->count)
  249. return GREATERTHAN;
  250. return EQUALTO;
  251. }
  252. }
  253. }
  254. static void
  255. sort_parents (Sym * child)
  256. {
  257. Arc *arc, *detached, sorted, *prev;
  258. /* Unlink parents from child, then insertion sort back on to
  259. sorted's parents.
  260. *arc the arc you have detached and are inserting.
  261. *detached the rest of the arcs to be sorted.
  262. sorted arc list onto which you insertion sort.
  263. *prev arc before the arc you are comparing. */
  264. sorted.next_parent = 0;
  265. for (arc = child->cg.parents; arc; arc = detached)
  266. {
  267. detached = arc->next_parent;
  268. /* Consider *arc as disconnected; insert it into sorted. */
  269. for (prev = &sorted; prev->next_parent; prev = prev->next_parent)
  270. {
  271. if (cmp_arc (arc, prev->next_parent) != GREATERTHAN)
  272. break;
  273. }
  274. arc->next_parent = prev->next_parent;
  275. prev->next_parent = arc;
  276. }
  277. /* Reattach sorted arcs to child. */
  278. child->cg.parents = sorted.next_parent;
  279. }
  280. static void
  281. print_parents (Sym *child)
  282. {
  283. Sym *parent;
  284. Arc *arc;
  285. Sym *cycle_head;
  286. if (child->cg.cyc.head != 0)
  287. cycle_head = child->cg.cyc.head;
  288. else
  289. cycle_head = child;
  290. if (!child->cg.parents)
  291. {
  292. printf (bsd_style_output
  293. ? _("%6.6s %5.5s %7.7s %11.11s %7.7s %7.7s <spontaneous>\n")
  294. : _("%6.6s %5.5s %7.7s %7.7s %7.7s %7.7s <spontaneous>\n"),
  295. "", "", "", "", "", "");
  296. return;
  297. }
  298. sort_parents (child);
  299. for (arc = child->cg.parents; arc; arc = arc->next_parent)
  300. {
  301. parent = arc->parent;
  302. if (child == parent || (child->cg.cyc.num != 0
  303. && parent->cg.cyc.num == child->cg.cyc.num))
  304. {
  305. /* Selfcall or call among siblings. */
  306. printf (bsd_style_output
  307. ? "%6.6s %5.5s %7.7s %11.11s %7lu %7.7s "
  308. : "%6.6s %5.5s %7.7s %7.7s %7lu %7.7s ",
  309. "", "", "", "",
  310. arc->count, "");
  311. print_name (parent);
  312. printf ("\n");
  313. }
  314. else
  315. {
  316. /* Regular parent of child. */
  317. printf (bsd_style_output
  318. ? "%6.6s %5.5s %7.2f %11.2f %7lu/%-7lu "
  319. : "%6.6s %5.5s %7.2f %7.2f %7lu/%-7lu ",
  320. "", "",
  321. arc->time / hz, arc->child_time / hz,
  322. arc->count, cycle_head->ncalls);
  323. print_name (parent);
  324. printf ("\n");
  325. }
  326. }
  327. }
  328. static void
  329. sort_children (Sym *parent)
  330. {
  331. Arc *arc, *detached, sorted, *prev;
  332. /* Unlink children from parent, then insertion sort back on to
  333. sorted's children.
  334. *arc the arc you have detached and are inserting.
  335. *detached the rest of the arcs to be sorted.
  336. sorted arc list onto which you insertion sort.
  337. *prev arc before the arc you are comparing. */
  338. sorted.next_child = 0;
  339. for (arc = parent->cg.children; arc; arc = detached)
  340. {
  341. detached = arc->next_child;
  342. /* Consider *arc as disconnected; insert it into sorted. */
  343. for (prev = &sorted; prev->next_child; prev = prev->next_child)
  344. {
  345. if (cmp_arc (arc, prev->next_child) != LESSTHAN)
  346. break;
  347. }
  348. arc->next_child = prev->next_child;
  349. prev->next_child = arc;
  350. }
  351. /* Reattach sorted children to parent. */
  352. parent->cg.children = sorted.next_child;
  353. }
  354. static void
  355. print_children (Sym *parent)
  356. {
  357. Sym *child;
  358. Arc *arc;
  359. sort_children (parent);
  360. arc = parent->cg.children;
  361. for (arc = parent->cg.children; arc; arc = arc->next_child)
  362. {
  363. child = arc->child;
  364. if (child == parent || (child->cg.cyc.num != 0
  365. && child->cg.cyc.num == parent->cg.cyc.num))
  366. {
  367. /* Self call or call to sibling. */
  368. printf (bsd_style_output
  369. ? "%6.6s %5.5s %7.7s %11.11s %7lu %7.7s "
  370. : "%6.6s %5.5s %7.7s %7.7s %7lu %7.7s ",
  371. "", "", "", "", arc->count, "");
  372. print_name (child);
  373. printf ("\n");
  374. }
  375. else
  376. {
  377. /* Regular child of parent. */
  378. printf (bsd_style_output
  379. ? "%6.6s %5.5s %7.2f %11.2f %7lu/%-7lu "
  380. : "%6.6s %5.5s %7.2f %7.2f %7lu/%-7lu ",
  381. "", "",
  382. arc->time / hz, arc->child_time / hz,
  383. arc->count, child->cg.cyc.head->ncalls);
  384. print_name (child);
  385. printf ("\n");
  386. }
  387. }
  388. }
  389. static void
  390. print_line (Sym *np)
  391. {
  392. char buf[BUFSIZ];
  393. sprintf (buf, "[%d]", np->cg.index);
  394. printf (bsd_style_output
  395. ? "%-6.6s %5.1f %7.2f %11.2f"
  396. : "%-6.6s %5.1f %7.2f %7.2f", buf,
  397. 100 * (np->cg.prop.self + np->cg.prop.child) / print_time,
  398. np->cg.prop.self / hz, np->cg.prop.child / hz);
  399. if ((np->ncalls + np->cg.self_calls) != 0)
  400. {
  401. printf (" %7lu", np->ncalls);
  402. if (np->cg.self_calls != 0)
  403. printf ("+%-7lu ", np->cg.self_calls);
  404. else
  405. printf (" %7.7s ", "");
  406. }
  407. else
  408. {
  409. printf (" %7.7s %7.7s ", "", "");
  410. }
  411. print_name (np);
  412. printf ("\n");
  413. }
  414. /* Print dynamic call graph. */
  415. void
  416. cg_print (Sym ** timesortsym)
  417. {
  418. unsigned int sym_index;
  419. Sym *parent;
  420. if (print_descriptions && bsd_style_output)
  421. bsd_callg_blurb (stdout);
  422. print_header ();
  423. for (sym_index = 0; sym_index < symtab.len + num_cycles; ++sym_index)
  424. {
  425. parent = timesortsym[sym_index];
  426. if ((ignore_zeros && parent->ncalls == 0
  427. && parent->cg.self_calls == 0 && parent->cg.prop.self == 0
  428. && parent->cg.prop.child == 0)
  429. || !parent->cg.print_flag
  430. || (line_granularity && ! parent->is_func))
  431. continue;
  432. if (!parent->name && parent->cg.cyc.num != 0)
  433. {
  434. /* Cycle header. */
  435. print_cycle (parent);
  436. print_members (parent);
  437. }
  438. else
  439. {
  440. print_parents (parent);
  441. print_line (parent);
  442. print_children (parent);
  443. }
  444. if (bsd_style_output)
  445. printf ("\n");
  446. printf ("-----------------------------------------------\n");
  447. if (bsd_style_output)
  448. printf ("\n");
  449. }
  450. free (timesortsym);
  451. if (print_descriptions && !bsd_style_output)
  452. fsf_callg_blurb (stdout);
  453. }
  454. static int
  455. cmp_name (const PTR left, const PTR right)
  456. {
  457. const Sym **npp1 = (const Sym **) left;
  458. const Sym **npp2 = (const Sym **) right;
  459. return strcmp ((*npp1)->name, (*npp2)->name);
  460. }
  461. void
  462. cg_print_index (void)
  463. {
  464. unsigned int sym_index;
  465. unsigned int nnames, todo, i, j;
  466. int col, starting_col;
  467. Sym **name_sorted_syms, *sym;
  468. const char *filename;
  469. char buf[20];
  470. int column_width = (output_width - 1) / 3; /* Don't write in last col! */
  471. /* Now, sort regular function name
  472. alphabetically to create an index. */
  473. name_sorted_syms = (Sym **) xmalloc ((symtab.len + num_cycles) * sizeof (Sym *));
  474. for (sym_index = 0, nnames = 0; sym_index < symtab.len; sym_index++)
  475. {
  476. if (ignore_zeros && symtab.base[sym_index].ncalls == 0
  477. && symtab.base[sym_index].hist.time == 0)
  478. continue;
  479. name_sorted_syms[nnames++] = &symtab.base[sym_index];
  480. }
  481. qsort (name_sorted_syms, nnames, sizeof (Sym *), cmp_name);
  482. for (sym_index = 1, todo = nnames; sym_index <= num_cycles; sym_index++)
  483. name_sorted_syms[todo++] = &cycle_header[sym_index];
  484. printf ("\f\n");
  485. printf (_("Index by function name\n\n"));
  486. sym_index = (todo + 2) / 3;
  487. for (i = 0; i < sym_index; i++)
  488. {
  489. col = 0;
  490. starting_col = 0;
  491. for (j = i; j < todo; j += sym_index)
  492. {
  493. sym = name_sorted_syms[j];
  494. if (sym->cg.print_flag)
  495. sprintf (buf, "[%d]", sym->cg.index);
  496. else
  497. sprintf (buf, "(%d)", sym->cg.index);
  498. if (j < nnames)
  499. {
  500. if (bsd_style_output)
  501. {
  502. printf ("%6.6s %-19.19s", buf, sym->name);
  503. }
  504. else
  505. {
  506. col += strlen (buf);
  507. for (; col < starting_col + 5; ++col)
  508. putchar (' ');
  509. printf (" %s ", buf);
  510. col += print_name_only (sym);
  511. if (!line_granularity && sym->is_static && sym->file)
  512. {
  513. filename = sym->file->name;
  514. if (!print_path)
  515. {
  516. filename = strrchr (filename, '/');
  517. if (filename)
  518. ++filename;
  519. else
  520. filename = sym->file->name;
  521. }
  522. printf (" (%s)", filename);
  523. col += strlen (filename) + 3;
  524. }
  525. }
  526. }
  527. else
  528. {
  529. if (bsd_style_output)
  530. {
  531. printf ("%6.6s ", buf);
  532. sprintf (buf, _("<cycle %d>"), sym->cg.cyc.num);
  533. printf ("%-19.19s", buf);
  534. }
  535. else
  536. {
  537. col += strlen (buf);
  538. for (; col < starting_col + 5; ++col)
  539. putchar (' ');
  540. printf (" %s ", buf);
  541. sprintf (buf, _("<cycle %d>"), sym->cg.cyc.num);
  542. printf ("%s", buf);
  543. col += strlen (buf);
  544. }
  545. }
  546. starting_col += column_width;
  547. }
  548. printf ("\n");
  549. }
  550. free (name_sorted_syms);
  551. }
  552. /* Compare two arcs based on their usage counts.
  553. We want to sort in descending order. */
  554. static int
  555. cmp_arc_count (const PTR left, const PTR right)
  556. {
  557. const Arc **npp1 = (const Arc **) left;
  558. const Arc **npp2 = (const Arc **) right;
  559. if ((*npp1)->count > (*npp2)->count)
  560. return -1;
  561. else if ((*npp1)->count < (*npp2)->count)
  562. return 1;
  563. else
  564. return 0;
  565. }
  566. /* Compare two funtions based on their usage counts.
  567. We want to sort in descending order. */
  568. static int
  569. cmp_fun_nuses (const PTR left, const PTR right)
  570. {
  571. const Sym **npp1 = (const Sym **) left;
  572. const Sym **npp2 = (const Sym **) right;
  573. if ((*npp1)->nuses > (*npp2)->nuses)
  574. return -1;
  575. else if ((*npp1)->nuses < (*npp2)->nuses)
  576. return 1;
  577. else
  578. return 0;
  579. }
  580. /* Print a suggested function ordering based on the profiling data.
  581. We perform 4 major steps when ordering functions:
  582. * Group unused functions together and place them at the
  583. end of the function order.
  584. * Search the highest use arcs (those which account for 90% of
  585. the total arc count) for functions which have several parents.
  586. Group those with the most call sites together (currently the
  587. top 1.25% which have at least five different call sites).
  588. These are emitted at the start of the function order.
  589. * Use a greedy placement algorithm to place functions which
  590. occur in the top 99% of the arcs in the profile. Some provisions
  591. are made to handle high usage arcs where the parent and/or
  592. child has already been placed.
  593. * Run the same greedy placement algorithm on the remaining
  594. arcs to place the leftover functions.
  595. The various "magic numbers" should (one day) be tuneable by command
  596. line options. They were arrived at by benchmarking a few applications
  597. with various values to see which values produced better overall function
  598. orderings.
  599. Of course, profiling errors, machine limitations (PA long calls), and
  600. poor cutoff values for the placement algorithm may limit the usefullness
  601. of the resulting function order. Improvements would be greatly appreciated.
  602. Suggestions:
  603. * Place the functions with many callers near the middle of the
  604. list to reduce long calls.
  605. * Propagate arc usage changes as functions are placed. Ie if
  606. func1 and func2 are placed together, arcs to/from those arcs
  607. to the same parent/child should be combined, then resort the
  608. arcs to choose the next one.
  609. * Implement some global positioning algorithm to place the
  610. chains made by the greedy local positioning algorithm. Probably
  611. by examining arcs which haven't been placed yet to tie two
  612. chains together.
  613. * Take a function's size and time into account in the algorithm;
  614. size in particular is important on the PA (long calls). Placing
  615. many small functions onto their own page may be wise.
  616. * Use better profiling information; many published algorithms
  617. are based on call sequences through time, rather than just
  618. arc counts.
  619. * Prodecure cloning could improve performance when a small number
  620. of arcs account for most of the calls to a particular function.
  621. * Use relocation information to avoid moving unused functions
  622. completely out of the code stream; this would avoid severe lossage
  623. when the profile data bears little resemblance to actual runs.
  624. * Propagation of arc usages should also improve .o link line
  625. ordering which shares the same arc placement algorithm with
  626. the function ordering code (in fact it is a degenerate case
  627. of function ordering). */
  628. void
  629. cg_print_function_ordering (void)
  630. {
  631. unsigned long sym_index;
  632. unsigned long arc_index;
  633. unsigned long used, unused, scratch_index;
  634. unsigned long unplaced_arc_count, high_arc_count, scratch_arc_count;
  635. #ifdef __GNUC__
  636. unsigned long long total_arcs, tmp_arcs_count;
  637. #else
  638. unsigned long total_arcs, tmp_arcs_count;
  639. #endif
  640. Sym **unused_syms, **used_syms, **scratch_syms;
  641. Arc **unplaced_arcs, **high_arcs, **scratch_arcs;
  642. sym_index = 0;
  643. used = 0;
  644. unused = 0;
  645. scratch_index = 0;
  646. unplaced_arc_count = 0;
  647. high_arc_count = 0;
  648. scratch_arc_count = 0;
  649. /* First group all the unused functions together. */
  650. unused_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  651. used_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  652. scratch_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  653. high_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  654. scratch_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  655. unplaced_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  656. /* Walk through all the functions; mark those which are never
  657. called as placed (we'll emit them as a group later). */
  658. for (sym_index = 0, used = 0, unused = 0; sym_index < symtab.len; sym_index++)
  659. {
  660. if (symtab.base[sym_index].ncalls == 0)
  661. {
  662. unused_syms[unused++] = &symtab.base[sym_index];
  663. symtab.base[sym_index].has_been_placed = 1;
  664. }
  665. else
  666. {
  667. used_syms[used++] = &symtab.base[sym_index];
  668. symtab.base[sym_index].has_been_placed = 0;
  669. symtab.base[sym_index].next = 0;
  670. symtab.base[sym_index].prev = 0;
  671. symtab.base[sym_index].nuses = 0;
  672. }
  673. }
  674. /* Sort the arcs from most used to least used. */
  675. qsort (arcs, numarcs, sizeof (Arc *), cmp_arc_count);
  676. /* Compute the total arc count. Also mark arcs as unplaced.
  677. Note we don't compensate for overflow if that happens!
  678. Overflow is much less likely when this file is compiled
  679. with GCC as it can double-wide integers via long long. */
  680. total_arcs = 0;
  681. for (arc_index = 0; arc_index < numarcs; arc_index++)
  682. {
  683. total_arcs += arcs[arc_index]->count;
  684. arcs[arc_index]->has_been_placed = 0;
  685. }
  686. /* We want to pull out those functions which are referenced
  687. by many highly used arcs and emit them as a group. This
  688. could probably use some tuning. */
  689. tmp_arcs_count = 0;
  690. for (arc_index = 0; arc_index < numarcs; arc_index++)
  691. {
  692. tmp_arcs_count += arcs[arc_index]->count;
  693. /* Count how many times each parent and child are used up
  694. to our threshold of arcs (90%). */
  695. if ((double)tmp_arcs_count / (double)total_arcs > 0.90)
  696. break;
  697. arcs[arc_index]->child->nuses++;
  698. }
  699. /* Now sort a temporary symbol table based on the number of
  700. times each function was used in the highest used arcs. */
  701. memcpy (scratch_syms, used_syms, used * sizeof (Sym *));
  702. qsort (scratch_syms, used, sizeof (Sym *), cmp_fun_nuses);
  703. /* Now pick out those symbols we're going to emit as
  704. a group. We take up to 1.25% of the used symbols. */
  705. for (sym_index = 0; sym_index < used / 80; sym_index++)
  706. {
  707. Sym *sym = scratch_syms[sym_index];
  708. Arc *arc;
  709. /* If we hit symbols that aren't used from many call sites,
  710. then we can quit. We choose five as the low limit for
  711. no particular reason. */
  712. if (sym->nuses == 5)
  713. break;
  714. /* We're going to need the arcs between these functions.
  715. Unfortunately, we don't know all these functions
  716. until we're done. So we keep track of all the arcs
  717. to the functions we care about, then prune out those
  718. which are uninteresting.
  719. An interesting variation would be to quit when we found
  720. multi-call site functions which account for some percentage
  721. of the arcs. */
  722. arc = sym->cg.children;
  723. while (arc)
  724. {
  725. if (arc->parent != arc->child)
  726. scratch_arcs[scratch_arc_count++] = arc;
  727. arc->has_been_placed = 1;
  728. arc = arc->next_child;
  729. }
  730. arc = sym->cg.parents;
  731. while (arc)
  732. {
  733. if (arc->parent != arc->child)
  734. scratch_arcs[scratch_arc_count++] = arc;
  735. arc->has_been_placed = 1;
  736. arc = arc->next_parent;
  737. }
  738. /* Keep track of how many symbols we're going to place. */
  739. scratch_index = sym_index;
  740. /* A lie, but it makes identifying
  741. these functions easier later. */
  742. sym->has_been_placed = 1;
  743. }
  744. /* Now walk through the temporary arcs and copy
  745. those we care about into the high arcs array. */
  746. for (arc_index = 0; arc_index < scratch_arc_count; arc_index++)
  747. {
  748. Arc *arc = scratch_arcs[arc_index];
  749. /* If this arc refers to highly used functions, then
  750. then we want to keep it. */
  751. if (arc->child->has_been_placed
  752. && arc->parent->has_been_placed)
  753. {
  754. high_arcs[high_arc_count++] = scratch_arcs[arc_index];
  755. /* We need to turn of has_been_placed since we're going to
  756. use the main arc placement algorithm on these arcs. */
  757. arc->child->has_been_placed = 0;
  758. arc->parent->has_been_placed = 0;
  759. }
  760. }
  761. /* Dump the multi-site high usage functions which are not
  762. going to be ordered by the main ordering algorithm. */
  763. for (sym_index = 0; sym_index < scratch_index; sym_index++)
  764. {
  765. if (scratch_syms[sym_index]->has_been_placed)
  766. printf ("%s\n", scratch_syms[sym_index]->name);
  767. }
  768. /* Now we can order the multi-site high use
  769. functions based on the arcs between them. */
  770. qsort (high_arcs, high_arc_count, sizeof (Arc *), cmp_arc_count);
  771. order_and_dump_functions_by_arcs (high_arcs, high_arc_count, 1,
  772. unplaced_arcs, &unplaced_arc_count);
  773. /* Order and dump the high use functions left,
  774. these typically have only a few call sites. */
  775. order_and_dump_functions_by_arcs (arcs, numarcs, 0,
  776. unplaced_arcs, &unplaced_arc_count);
  777. /* Now place the rarely used functions. */
  778. order_and_dump_functions_by_arcs (unplaced_arcs, unplaced_arc_count, 1,
  779. scratch_arcs, &scratch_arc_count);
  780. /* Output any functions not emitted by the order_and_dump calls. */
  781. for (sym_index = 0; sym_index < used; sym_index++)
  782. if (used_syms[sym_index]->has_been_placed == 0)
  783. printf("%s\n", used_syms[sym_index]->name);
  784. /* Output the unused functions. */
  785. for (sym_index = 0; sym_index < unused; sym_index++)
  786. printf("%s\n", unused_syms[sym_index]->name);
  787. unused_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  788. used_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  789. scratch_syms = (Sym **) xmalloc (symtab.len * sizeof (Sym *));
  790. high_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  791. scratch_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  792. unplaced_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  793. free (unused_syms);
  794. free (used_syms);
  795. free (scratch_syms);
  796. free (high_arcs);
  797. free (scratch_arcs);
  798. free (unplaced_arcs);
  799. }
  800. /* Place functions based on the arcs in THE_ARCS with ARC_COUNT entries;
  801. place unused arcs into UNPLACED_ARCS/UNPLACED_ARC_COUNT.
  802. If ALL is nonzero, then place all functions referenced by THE_ARCS,
  803. else only place those referenced in the top 99% of the arcs in THE_ARCS. */
  804. #define MOST 0.99
  805. static void
  806. order_and_dump_functions_by_arcs (Arc **the_arcs, unsigned long arc_count,
  807. int all, Arc **unplaced_arcs,
  808. unsigned long *unplaced_arc_count)
  809. {
  810. #ifdef __GNUC__
  811. unsigned long long tmp_arcs, total_arcs;
  812. #else
  813. unsigned long tmp_arcs, total_arcs;
  814. #endif
  815. unsigned int arc_index;
  816. /* If needed, compute the total arc count.
  817. Note we don't compensate for overflow if that happens! */
  818. if (! all)
  819. {
  820. total_arcs = 0;
  821. for (arc_index = 0; arc_index < arc_count; arc_index++)
  822. total_arcs += the_arcs[arc_index]->count;
  823. }
  824. else
  825. total_arcs = 0;
  826. tmp_arcs = 0;
  827. for (arc_index = 0; arc_index < arc_count; arc_index++)
  828. {
  829. Sym *sym1, *sym2;
  830. Sym *child, *parent;
  831. tmp_arcs += the_arcs[arc_index]->count;
  832. /* Ignore this arc if it's already been placed. */
  833. if (the_arcs[arc_index]->has_been_placed)
  834. continue;
  835. child = the_arcs[arc_index]->child;
  836. parent = the_arcs[arc_index]->parent;
  837. /* If we're not using all arcs, and this is a rarely used
  838. arc, then put it on the unplaced_arc list. Similarly
  839. if both the parent and child of this arc have been placed. */
  840. if ((! all && (double)tmp_arcs / (double)total_arcs > MOST)
  841. || child->has_been_placed || parent->has_been_placed)
  842. {
  843. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  844. continue;
  845. }
  846. /* If all slots in the parent and child are full, then there isn't
  847. anything we can do right now. We'll place this arc on the
  848. unplaced arc list in the hope that a global positioning
  849. algorithm can use it to place function chains. */
  850. if (parent->next && parent->prev && child->next && child->prev)
  851. {
  852. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  853. continue;
  854. }
  855. /* If the parent is unattached, then find the closest
  856. place to attach it onto child's chain. Similarly
  857. for the opposite case. */
  858. if (!parent->next && !parent->prev)
  859. {
  860. int next_count = 0;
  861. int prev_count = 0;
  862. Sym *prev = child;
  863. Sym *next = child;
  864. /* Walk to the beginning and end of the child's chain. */
  865. while (next->next)
  866. {
  867. next = next->next;
  868. next_count++;
  869. }
  870. while (prev->prev)
  871. {
  872. prev = prev->prev;
  873. prev_count++;
  874. }
  875. /* Choose the closest. */
  876. child = next_count < prev_count ? next : prev;
  877. }
  878. else if (! child->next && !child->prev)
  879. {
  880. int next_count = 0;
  881. int prev_count = 0;
  882. Sym *prev = parent;
  883. Sym *next = parent;
  884. while (next->next)
  885. {
  886. next = next->next;
  887. next_count++;
  888. }
  889. while (prev->prev)
  890. {
  891. prev = prev->prev;
  892. prev_count++;
  893. }
  894. parent = prev_count < next_count ? prev : next;
  895. }
  896. else
  897. {
  898. /* Couldn't find anywhere to attach the functions,
  899. put the arc on the unplaced arc list. */
  900. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  901. continue;
  902. }
  903. /* Make sure we don't tie two ends together. */
  904. sym1 = parent;
  905. if (sym1->next)
  906. while (sym1->next)
  907. sym1 = sym1->next;
  908. else
  909. while (sym1->prev)
  910. sym1 = sym1->prev;
  911. sym2 = child;
  912. if (sym2->next)
  913. while (sym2->next)
  914. sym2 = sym2->next;
  915. else
  916. while (sym2->prev)
  917. sym2 = sym2->prev;
  918. if (sym1 == child
  919. && sym2 == parent)
  920. {
  921. /* This would tie two ends together. */
  922. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  923. continue;
  924. }
  925. if (parent->next)
  926. {
  927. /* Must attach to the parent's prev field. */
  928. if (! child->next)
  929. {
  930. /* parent-prev and child-next */
  931. parent->prev = child;
  932. child->next = parent;
  933. the_arcs[arc_index]->has_been_placed = 1;
  934. }
  935. }
  936. else if (parent->prev)
  937. {
  938. /* Must attach to the parent's next field. */
  939. if (! child->prev)
  940. {
  941. /* parent-next and child-prev */
  942. parent->next = child;
  943. child->prev = parent;
  944. the_arcs[arc_index]->has_been_placed = 1;
  945. }
  946. }
  947. else
  948. {
  949. /* Can attach to either field in the parent, depends
  950. on where we've got space in the child. */
  951. if (child->prev)
  952. {
  953. /* parent-prev and child-next. */
  954. parent->prev = child;
  955. child->next = parent;
  956. the_arcs[arc_index]->has_been_placed = 1;
  957. }
  958. else
  959. {
  960. /* parent-next and child-prev. */
  961. parent->next = child;
  962. child->prev = parent;
  963. the_arcs[arc_index]->has_been_placed = 1;
  964. }
  965. }
  966. }
  967. /* Dump the chains of functions we've made. */
  968. for (arc_index = 0; arc_index < arc_count; arc_index++)
  969. {
  970. Sym *sym;
  971. if (the_arcs[arc_index]->parent->has_been_placed
  972. || the_arcs[arc_index]->child->has_been_placed)
  973. continue;
  974. sym = the_arcs[arc_index]->parent;
  975. /* If this symbol isn't attached to any other
  976. symbols, then we've got a rarely used arc.
  977. Skip it for now, we'll deal with them later. */
  978. if (sym->next == NULL
  979. && sym->prev == NULL)
  980. continue;
  981. /* Get to the start of this chain. */
  982. while (sym->prev)
  983. sym = sym->prev;
  984. while (sym)
  985. {
  986. /* Mark it as placed. */
  987. sym->has_been_placed = 1;
  988. printf ("%s\n", sym->name);
  989. sym = sym->next;
  990. }
  991. }
  992. /* If we want to place all the arcs, then output
  993. those which weren't placed by the main algorithm. */
  994. if (all)
  995. for (arc_index = 0; arc_index < arc_count; arc_index++)
  996. {
  997. Sym *sym;
  998. if (the_arcs[arc_index]->parent->has_been_placed
  999. || the_arcs[arc_index]->child->has_been_placed)
  1000. continue;
  1001. sym = the_arcs[arc_index]->parent;
  1002. sym->has_been_placed = 1;
  1003. printf ("%s\n", sym->name);
  1004. }
  1005. }
  1006. /* Compare two function_map structs based on file name.
  1007. We want to sort in ascending order. */
  1008. static int
  1009. cmp_symbol_map (const void * l, const void * r)
  1010. {
  1011. return filename_cmp (((struct function_map *) l)->file_name,
  1012. ((struct function_map *) r)->file_name);
  1013. }
  1014. /* Print a suggested .o ordering for files on a link line based
  1015. on profiling information. This uses the function placement
  1016. code for the bulk of its work. */
  1017. void
  1018. cg_print_file_ordering (void)
  1019. {
  1020. unsigned long scratch_arc_count;
  1021. unsigned long arc_index;
  1022. unsigned long sym_index;
  1023. Arc **scratch_arcs;
  1024. char *last;
  1025. scratch_arc_count = 0;
  1026. scratch_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  1027. for (arc_index = 0; arc_index < numarcs; arc_index++)
  1028. {
  1029. if (! arcs[arc_index]->parent->mapped
  1030. || ! arcs[arc_index]->child->mapped)
  1031. arcs[arc_index]->has_been_placed = 1;
  1032. }
  1033. order_and_dump_functions_by_arcs (arcs, numarcs, 0,
  1034. scratch_arcs, &scratch_arc_count);
  1035. /* Output .o's not handled by the main placement algorithm. */
  1036. for (sym_index = 0; sym_index < symtab.len; sym_index++)
  1037. {
  1038. if (symtab.base[sym_index].mapped
  1039. && ! symtab.base[sym_index].has_been_placed)
  1040. printf ("%s\n", symtab.base[sym_index].name);
  1041. }
  1042. qsort (symbol_map, symbol_map_count, sizeof (struct function_map), cmp_symbol_map);
  1043. /* Now output any .o's that didn't have any text symbols. */
  1044. last = NULL;
  1045. for (sym_index = 0; sym_index < symbol_map_count; sym_index++)
  1046. {
  1047. unsigned int index2;
  1048. /* Don't bother searching if this symbol
  1049. is the same as the previous one. */
  1050. if (last && !filename_cmp (last, symbol_map[sym_index].file_name))
  1051. continue;
  1052. for (index2 = 0; index2 < symtab.len; index2++)
  1053. {
  1054. if (! symtab.base[index2].mapped)
  1055. continue;
  1056. if (!filename_cmp (symtab.base[index2].name,
  1057. symbol_map[sym_index].file_name))
  1058. break;
  1059. }
  1060. /* If we didn't find it in the symbol table, then it must
  1061. be a .o with no text symbols. Output it last. */
  1062. if (index2 == symtab.len)
  1063. printf ("%s\n", symbol_map[sym_index].file_name);
  1064. last = symbol_map[sym_index].file_name;
  1065. }
  1066. }