drd: Add a consistency check
[valgrind.git] / coregrind / m_sbprofile.c
blob0be07dd9cf225754971d8e85cf8a19da319bd487
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 HChar buf_cumul[10], buf_here[10];
54 Int r; /* must be signed */
56 HChar ecs_txt[50];
57 if (ecs_done > 0) {
58 VG_(sprintf)(ecs_txt, "%'llu ecs done", ecs_done);
59 } else {
60 VG_(strcpy)(ecs_txt, "for the entire run");
63 vg_assert(VG_(clo_profyle_sbs));
65 VG_(printf)("\n");
66 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
67 "<<<---<<<---<<<---<<<---<<<---<<<\n");
68 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
69 "<<<---<<<---<<<---<<<---<<<---<<<\n");
70 VG_(printf)("\n");
71 VG_(printf)("<<< BEGIN SB Profile #%u (%s)\n",
72 ++n_profiles, ecs_txt);
73 VG_(printf)("<<<\n");
74 VG_(printf)("\n");
76 VG_(printf)("Total score = %'lld\n\n", score_total);
78 /* Print an initial per-block summary. */
79 VG_(printf)("rank ---cumulative--- -----self-----\n");
80 score_cumul = 0;
81 for (r = 0; r < n_tops; r++) {
82 if (tops[r].addr == 0)
83 continue;
84 if (tops[r].score == 0)
85 continue;
87 const HChar *name;
88 VG_(get_fnname_w_offset)(tops[r].addr, &name);
90 score_here = tops[r].score;
91 score_cumul += score_here;
92 VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul);
93 VG_(percentify)(score_here, score_total, 2, 6, buf_here);
94 VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n",
96 score_cumul, buf_cumul,
97 score_here, buf_here, tops[r].addr, name );
99 score_cumul_saved = score_cumul;
101 if (VG_(clo_profyle_flags) > 0) {
103 /* Show the details, if requested. */
104 VG_(printf)("\n");
105 VG_(printf)("-----------------------------"
106 "------------------------------\n");
107 VG_(printf)("--- SB Profile (SB details) "
108 " ---\n");
109 VG_(printf)("-----------------------------"
110 "------------------------------\n");
111 VG_(printf)("\n");
113 score_cumul = 0;
114 for (r = 0; r < n_tops; r++) {
115 if (tops[r].addr == 0)
116 continue;
117 if (tops[r].score == 0)
118 continue;
120 const HChar *name;
121 VG_(get_fnname_w_offset)(tops[r].addr, &name);
123 score_here = tops[r].score;
124 score_cumul += score_here;
125 VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul);
126 VG_(percentify)(score_here, score_total, 2, 6, buf_here);
127 VG_(printf)("\n");
128 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin SB rank %d "
129 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
130 VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n",
132 score_cumul, buf_cumul,
133 score_here, buf_here, tops[r].addr, name );
134 VG_(printf)("\n");
135 VG_(discard_translations)(tops[r].addr, 1, "bb profile");
136 VG_(translate)(0, tops[r].addr, True, VG_(clo_profyle_flags), 0, True);
137 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= end SB rank %d "
138 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
141 /* Print a final per-block summary, in reverse order, for the
142 convenience of people reading up from the end. */
143 score_cumul = score_cumul_saved;
144 for (r = n_tops-1; r >= 0; r--) {
145 if (tops[r].addr == 0)
146 continue;
147 if (tops[r].score == 0)
148 continue;
150 const HChar *name;
151 VG_(get_fnname_w_offset)(tops[r].addr, &name);
153 score_here = tops[r].score;
154 VG_(percentify)(score_cumul, score_total, 2, 6, buf_cumul);
155 VG_(percentify)(score_here, score_total, 2, 6, buf_here);
156 VG_(printf)("%3d: (%9lld %s) %9lld %s 0x%llx %s\n",
158 score_cumul, buf_cumul,
159 score_here, buf_here, tops[r].addr, name );
160 score_cumul -= score_here;
162 VG_(printf)("rank ---cumulative--- -----self-----\n");
166 VG_(printf)("\n");
167 VG_(printf)(">>>\n");
168 VG_(printf)(">>> END SB Profile #%u (%s)\n",
169 n_profiles, ecs_txt);
170 VG_(printf)(">>>\n");
171 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
172 ">>>--->>>--->>>--->>>--->>>--->>>\n");
173 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
174 ">>>--->>>--->>>--->>>--->>>--->>>\n");
175 VG_(printf)("\n");
179 /* Get and print a profile. Also, zero out the counters so that if we
180 call it again later, the second call will only show new work done
181 since the first call. ecs_done == 0 is taken to mean this is a
182 run-end profile. */
183 void VG_(get_and_show_SB_profile) ( ULong ecs_done )
185 /* The number of blocks to show for a end-of-run profile */
186 # define N_MAX_END 200
187 /* The number of blocks to show for a mid-run profile. */
188 # define N_MAX_INTERVAL 20
189 vg_assert(N_MAX_INTERVAL <= N_MAX_END);
190 SBProfEntry tops[N_MAX_END];
191 Int nToShow = ecs_done == 0 ? N_MAX_END : N_MAX_INTERVAL;
192 ULong score_total = VG_(get_SB_profile)(tops, nToShow);
193 show_SB_profile(tops, nToShow, score_total, ecs_done);
194 # undef N_MAX_END
195 # undef N_MAX_INTERVAL
199 /*--------------------------------------------------------------------*/
200 /*--- end m_sbprofile.c ---*/
201 /*--------------------------------------------------------------------*/