cg_print.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /* cg_print.c - Print routines for displaying call graphs.
  2. Copyright (C) 2000-2015 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 ()
  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 ()
  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 threshhold 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 (the_arcs, arc_count, all,
  807. unplaced_arcs, unplaced_arc_count)
  808. Arc **the_arcs;
  809. unsigned long arc_count;
  810. int all;
  811. Arc **unplaced_arcs;
  812. unsigned long *unplaced_arc_count;
  813. {
  814. #ifdef __GNUC__
  815. unsigned long long tmp_arcs, total_arcs;
  816. #else
  817. unsigned long tmp_arcs, total_arcs;
  818. #endif
  819. unsigned int arc_index;
  820. /* If needed, compute the total arc count.
  821. Note we don't compensate for overflow if that happens! */
  822. if (! all)
  823. {
  824. total_arcs = 0;
  825. for (arc_index = 0; arc_index < arc_count; arc_index++)
  826. total_arcs += the_arcs[arc_index]->count;
  827. }
  828. else
  829. total_arcs = 0;
  830. tmp_arcs = 0;
  831. for (arc_index = 0; arc_index < arc_count; arc_index++)
  832. {
  833. Sym *sym1, *sym2;
  834. Sym *child, *parent;
  835. tmp_arcs += the_arcs[arc_index]->count;
  836. /* Ignore this arc if it's already been placed. */
  837. if (the_arcs[arc_index]->has_been_placed)
  838. continue;
  839. child = the_arcs[arc_index]->child;
  840. parent = the_arcs[arc_index]->parent;
  841. /* If we're not using all arcs, and this is a rarely used
  842. arc, then put it on the unplaced_arc list. Similarly
  843. if both the parent and child of this arc have been placed. */
  844. if ((! all && (double)tmp_arcs / (double)total_arcs > MOST)
  845. || child->has_been_placed || parent->has_been_placed)
  846. {
  847. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  848. continue;
  849. }
  850. /* If all slots in the parent and child are full, then there isn't
  851. anything we can do right now. We'll place this arc on the
  852. unplaced arc list in the hope that a global positioning
  853. algorithm can use it to place function chains. */
  854. if (parent->next && parent->prev && child->next && child->prev)
  855. {
  856. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  857. continue;
  858. }
  859. /* If the parent is unattached, then find the closest
  860. place to attach it onto child's chain. Similarly
  861. for the opposite case. */
  862. if (!parent->next && !parent->prev)
  863. {
  864. int next_count = 0;
  865. int prev_count = 0;
  866. Sym *prev = child;
  867. Sym *next = child;
  868. /* Walk to the beginning and end of the child's chain. */
  869. while (next->next)
  870. {
  871. next = next->next;
  872. next_count++;
  873. }
  874. while (prev->prev)
  875. {
  876. prev = prev->prev;
  877. prev_count++;
  878. }
  879. /* Choose the closest. */
  880. child = next_count < prev_count ? next : prev;
  881. }
  882. else if (! child->next && !child->prev)
  883. {
  884. int next_count = 0;
  885. int prev_count = 0;
  886. Sym *prev = parent;
  887. Sym *next = parent;
  888. while (next->next)
  889. {
  890. next = next->next;
  891. next_count++;
  892. }
  893. while (prev->prev)
  894. {
  895. prev = prev->prev;
  896. prev_count++;
  897. }
  898. parent = prev_count < next_count ? prev : next;
  899. }
  900. else
  901. {
  902. /* Couldn't find anywhere to attach the functions,
  903. put the arc on the unplaced arc list. */
  904. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  905. continue;
  906. }
  907. /* Make sure we don't tie two ends together. */
  908. sym1 = parent;
  909. if (sym1->next)
  910. while (sym1->next)
  911. sym1 = sym1->next;
  912. else
  913. while (sym1->prev)
  914. sym1 = sym1->prev;
  915. sym2 = child;
  916. if (sym2->next)
  917. while (sym2->next)
  918. sym2 = sym2->next;
  919. else
  920. while (sym2->prev)
  921. sym2 = sym2->prev;
  922. if (sym1 == child
  923. && sym2 == parent)
  924. {
  925. /* This would tie two ends together. */
  926. unplaced_arcs[(*unplaced_arc_count)++] = the_arcs[arc_index];
  927. continue;
  928. }
  929. if (parent->next)
  930. {
  931. /* Must attach to the parent's prev field. */
  932. if (! child->next)
  933. {
  934. /* parent-prev and child-next */
  935. parent->prev = child;
  936. child->next = parent;
  937. the_arcs[arc_index]->has_been_placed = 1;
  938. }
  939. }
  940. else if (parent->prev)
  941. {
  942. /* Must attach to the parent's next field. */
  943. if (! child->prev)
  944. {
  945. /* parent-next and child-prev */
  946. parent->next = child;
  947. child->prev = parent;
  948. the_arcs[arc_index]->has_been_placed = 1;
  949. }
  950. }
  951. else
  952. {
  953. /* Can attach to either field in the parent, depends
  954. on where we've got space in the child. */
  955. if (child->prev)
  956. {
  957. /* parent-prev and child-next. */
  958. parent->prev = child;
  959. child->next = parent;
  960. the_arcs[arc_index]->has_been_placed = 1;
  961. }
  962. else
  963. {
  964. /* parent-next and child-prev. */
  965. parent->next = child;
  966. child->prev = parent;
  967. the_arcs[arc_index]->has_been_placed = 1;
  968. }
  969. }
  970. }
  971. /* Dump the chains of functions we've made. */
  972. for (arc_index = 0; arc_index < arc_count; arc_index++)
  973. {
  974. Sym *sym;
  975. if (the_arcs[arc_index]->parent->has_been_placed
  976. || the_arcs[arc_index]->child->has_been_placed)
  977. continue;
  978. sym = the_arcs[arc_index]->parent;
  979. /* If this symbol isn't attached to any other
  980. symbols, then we've got a rarely used arc.
  981. Skip it for now, we'll deal with them later. */
  982. if (sym->next == NULL
  983. && sym->prev == NULL)
  984. continue;
  985. /* Get to the start of this chain. */
  986. while (sym->prev)
  987. sym = sym->prev;
  988. while (sym)
  989. {
  990. /* Mark it as placed. */
  991. sym->has_been_placed = 1;
  992. printf ("%s\n", sym->name);
  993. sym = sym->next;
  994. }
  995. }
  996. /* If we want to place all the arcs, then output
  997. those which weren't placed by the main algorithm. */
  998. if (all)
  999. for (arc_index = 0; arc_index < arc_count; arc_index++)
  1000. {
  1001. Sym *sym;
  1002. if (the_arcs[arc_index]->parent->has_been_placed
  1003. || the_arcs[arc_index]->child->has_been_placed)
  1004. continue;
  1005. sym = the_arcs[arc_index]->parent;
  1006. sym->has_been_placed = 1;
  1007. printf ("%s\n", sym->name);
  1008. }
  1009. }
  1010. /* Compare two function_map structs based on file name.
  1011. We want to sort in ascending order. */
  1012. static int
  1013. cmp_symbol_map (const void * l, const void * r)
  1014. {
  1015. return filename_cmp (((struct function_map *) l)->file_name,
  1016. ((struct function_map *) r)->file_name);
  1017. }
  1018. /* Print a suggested .o ordering for files on a link line based
  1019. on profiling information. This uses the function placement
  1020. code for the bulk of its work. */
  1021. void
  1022. cg_print_file_ordering (void)
  1023. {
  1024. unsigned long scratch_arc_count;
  1025. unsigned long arc_index;
  1026. unsigned long sym_index;
  1027. Arc **scratch_arcs;
  1028. char *last;
  1029. scratch_arc_count = 0;
  1030. scratch_arcs = (Arc **) xmalloc (numarcs * sizeof (Arc *));
  1031. for (arc_index = 0; arc_index < numarcs; arc_index++)
  1032. {
  1033. if (! arcs[arc_index]->parent->mapped
  1034. || ! arcs[arc_index]->child->mapped)
  1035. arcs[arc_index]->has_been_placed = 1;
  1036. }
  1037. order_and_dump_functions_by_arcs (arcs, numarcs, 0,
  1038. scratch_arcs, &scratch_arc_count);
  1039. /* Output .o's not handled by the main placement algorithm. */
  1040. for (sym_index = 0; sym_index < symtab.len; sym_index++)
  1041. {
  1042. if (symtab.base[sym_index].mapped
  1043. && ! symtab.base[sym_index].has_been_placed)
  1044. printf ("%s\n", symtab.base[sym_index].name);
  1045. }
  1046. qsort (symbol_map, symbol_map_count, sizeof (struct function_map), cmp_symbol_map);
  1047. /* Now output any .o's that didn't have any text symbols. */
  1048. last = NULL;
  1049. for (sym_index = 0; sym_index < symbol_map_count; sym_index++)
  1050. {
  1051. unsigned int index2;
  1052. /* Don't bother searching if this symbol
  1053. is the same as the previous one. */
  1054. if (last && !filename_cmp (last, symbol_map[sym_index].file_name))
  1055. continue;
  1056. for (index2 = 0; index2 < symtab.len; index2++)
  1057. {
  1058. if (! symtab.base[index2].mapped)
  1059. continue;
  1060. if (!filename_cmp (symtab.base[index2].name,
  1061. symbol_map[sym_index].file_name))
  1062. break;
  1063. }
  1064. /* If we didn't find it in the symbol table, then it must
  1065. be a .o with no text symbols. Output it last. */
  1066. if (index2 == symtab.len)
  1067. printf ("%s\n", symbol_map[sym_index].file_name);
  1068. last = symbol_map[sym_index].file_name;
  1069. }
  1070. }