Fix non portability spotted by Florian:
[valgrind.git] / coregrind / m_sbprofile.c
blobf64260fc22144d3495e0011373156b0edbef3373
2 /*--------------------------------------------------------------------*/
3 /*--- For printing superblock profiles m_sbprofile.c ---*/
4 /*--------------------------------------------------------------------*/
6 /*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
10 Copyright (C) 2012-2013 Mozilla Foundation
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
27 The GNU General Public License is contained in the file COPYING.
30 /* Contributed by Julian Seward <jseward@acm.org> */
32 #include "pub_core_basics.h"
33 #include "pub_core_transtab.h"
34 #include "pub_core_libcbase.h"
35 #include "pub_core_libcprint.h"
36 #include "pub_core_libcassert.h"
37 #include "pub_core_debuginfo.h"
38 #include "pub_core_translate.h"
39 #include "pub_core_options.h"
40 #include "pub_core_sbprofile.h" // self
42 /*====================================================================*/
43 /*=== SB profiling ===*/
44 /*====================================================================*/
46 static UInt n_profiles = 0;
48 static
49 void show_SB_profile ( const SBProfEntry tops[], UInt n_tops,
50 ULong score_total, ULong ecs_done )
52 ULong score_cumul, score_cumul_saved, score_here;
53 Int r; /* must be signed */
55 HChar ecs_txt[50]; // large enough
56 if (ecs_done > 0) {
57 VG_(sprintf)(ecs_txt, "%'llu ecs done", ecs_done);
58 } else {
59 VG_(strcpy)(ecs_txt, "for the entire run");
62 vg_assert(VG_(clo_profyle_sbs));
64 VG_(printf)("\n");
65 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
66 "<<<---<<<---<<<---<<<---<<<---<<<\n");
67 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
68 "<<<---<<<---<<<---<<<---<<<---<<<\n");
69 VG_(printf)("\n");
70 VG_(printf)("<<< BEGIN SB Profile #%u (%s)\n",
71 ++n_profiles, ecs_txt);
72 VG_(printf)("<<<\n");
73 VG_(printf)("\n");
75 VG_(printf)("Total score = %'lld\n\n", score_total);
77 /* Print an initial per-block summary. */
78 VG_(printf)("rank ---cumulative--- -----self-----\n");
79 score_cumul = 0;
80 for (r = 0; r < n_tops; r++) {
81 if (tops[r].addr == 0)
82 continue;
83 if (tops[r].score == 0)
84 continue;
86 const HChar *name;
87 VG_(get_fnname_w_offset)(tops[r].addr, &name);
89 score_here = tops[r].score;
90 score_cumul += score_here;
92 /* Careful: do not divide by zero. score_total == 0 implies
93 score_cumul == 0 and also score_here == 0. */
94 Double percent_cumul =
95 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
96 Double percent_here =
97 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
99 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
101 score_cumul, percent_cumul,
102 score_here, percent_here, tops[r].addr, name);
104 score_cumul_saved = score_cumul;
106 if (VG_(clo_profyle_flags) > 0) {
108 /* Show the details, if requested. */
109 VG_(printf)("\n");
110 VG_(printf)("-----------------------------"
111 "------------------------------\n");
112 VG_(printf)("--- SB Profile (SB details) "
113 " ---\n");
114 VG_(printf)("-----------------------------"
115 "------------------------------\n");
116 VG_(printf)("\n");
118 score_cumul = 0;
119 for (r = 0; r < n_tops; r++) {
120 if (tops[r].addr == 0)
121 continue;
122 if (tops[r].score == 0)
123 continue;
125 const HChar *name;
126 VG_(get_fnname_w_offset)(tops[r].addr, &name);
128 score_here = tops[r].score;
129 score_cumul += score_here;
131 /* Careful: do not divide by zero. score_total == 0 implies
132 score_cumul == 0 and also score_here == 0. */
133 Double percent_cumul =
134 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
135 Double percent_here =
136 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
138 VG_(printf)("\n");
139 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin SB rank %d "
140 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
141 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
143 score_cumul, percent_cumul,
144 score_here, percent_here, tops[r].addr, name );
145 VG_(printf)("\n");
146 VG_(discard_translations)(tops[r].addr, 1, "bb profile");
147 VG_(translate)(0, tops[r].addr, True, VG_(clo_profyle_flags), 0, True);
148 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= end SB rank %d "
149 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
152 /* Print a final per-block summary, in reverse order, for the
153 convenience of people reading up from the end. */
154 score_cumul = score_cumul_saved;
155 for (r = n_tops-1; r >= 0; r--) {
156 if (tops[r].addr == 0)
157 continue;
158 if (tops[r].score == 0)
159 continue;
161 const HChar *name;
162 VG_(get_fnname_w_offset)(tops[r].addr, &name);
164 score_here = tops[r].score;
166 /* Careful: do not divide by zero. score_total == 0 implies
167 score_cumul == 0 and also score_here == 0. */
168 Double percent_cumul =
169 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
170 Double percent_here =
171 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
173 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
175 score_cumul, percent_cumul,
176 score_here, percent_here, tops[r].addr, name );
177 score_cumul -= score_here;
179 VG_(printf)("rank ---cumulative--- -----self-----\n");
183 VG_(printf)("\n");
184 VG_(printf)(">>>\n");
185 VG_(printf)(">>> END SB Profile #%u (%s)\n",
186 n_profiles, ecs_txt);
187 VG_(printf)(">>>\n");
188 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
189 ">>>--->>>--->>>--->>>--->>>--->>>\n");
190 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
191 ">>>--->>>--->>>--->>>--->>>--->>>\n");
192 VG_(printf)("\n");
196 /* Get and print a profile. Also, zero out the counters so that if we
197 call it again later, the second call will only show new work done
198 since the first call. ecs_done == 0 is taken to mean this is a
199 run-end profile. */
200 void VG_(get_and_show_SB_profile) ( ULong ecs_done )
202 /* The number of blocks to show for a end-of-run profile */
203 # define N_MAX_END 200
204 /* The number of blocks to show for a mid-run profile. */
205 # define N_MAX_INTERVAL 20
206 vg_assert(N_MAX_INTERVAL <= N_MAX_END);
207 SBProfEntry tops[N_MAX_END];
208 Int nToShow = ecs_done == 0 ? N_MAX_END : N_MAX_INTERVAL;
209 ULong score_total = VG_(get_SB_profile)(tops, nToShow);
210 show_SB_profile(tops, nToShow, score_total, ecs_done);
211 # undef N_MAX_END
212 # undef N_MAX_INTERVAL
216 /*--------------------------------------------------------------------*/
217 /*--- end m_sbprofile.c ---*/
218 /*--------------------------------------------------------------------*/