blob: f64260fc22144d3495e0011373156b0edbef3373 [file] [log] [blame]
sewardj17c5e2e2012-12-28 09:12:14 +00001
2/*--------------------------------------------------------------------*/
3/*--- For printing superblock profiles m_sbprofile.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj0f157dd2013-10-18 14:27:36 +000010 Copyright (C) 2012-2013 Mozilla Foundation
sewardj17c5e2e2012-12-28 09:12:14 +000011
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.
16
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.
21
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.
26
27 The GNU General Public License is contained in the file COPYING.
28*/
29
30/* Contributed by Julian Seward <jseward@acm.org> */
31
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
41
42/*====================================================================*/
43/*=== SB profiling ===*/
44/*====================================================================*/
45
46static UInt n_profiles = 0;
47
48static
florian518850b2014-10-22 22:25:30 +000049void show_SB_profile ( const SBProfEntry tops[], UInt n_tops,
sewardj17c5e2e2012-12-28 09:12:14 +000050 ULong score_total, ULong ecs_done )
51{
52 ULong score_cumul, score_cumul_saved, score_here;
sewardj17c5e2e2012-12-28 09:12:14 +000053 Int r; /* must be signed */
54
florianf44ff622014-12-20 16:52:08 +000055 HChar ecs_txt[50]; // large enough
sewardj17c5e2e2012-12-28 09:12:14 +000056 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");
60 }
61
62 vg_assert(VG_(clo_profyle_sbs));
63
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");
74
75 VG_(printf)("Total score = %'lld\n\n", score_total);
76
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;
florian46cc0452014-10-25 19:20:38 +000085
86 const HChar *name;
87 VG_(get_fnname_w_offset)(tops[r].addr, &name);
88
sewardj17c5e2e2012-12-28 09:12:14 +000089 score_here = tops[r].score;
90 score_cumul += score_here;
florian227a1ec2014-12-12 19:32:10 +000091
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;
98
florianddd61ff2015-01-04 17:20:45 +000099 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
sewardj17c5e2e2012-12-28 09:12:14 +0000100 r,
florian227a1ec2014-12-12 19:32:10 +0000101 score_cumul, percent_cumul,
102 score_here, percent_here, tops[r].addr, name);
sewardj17c5e2e2012-12-28 09:12:14 +0000103 }
104 score_cumul_saved = score_cumul;
105
106 if (VG_(clo_profyle_flags) > 0) {
107
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");
117
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;
florian46cc0452014-10-25 19:20:38 +0000124
125 const HChar *name;
126 VG_(get_fnname_w_offset)(tops[r].addr, &name);
127
sewardj17c5e2e2012-12-28 09:12:14 +0000128 score_here = tops[r].score;
129 score_cumul += score_here;
florian227a1ec2014-12-12 19:32:10 +0000130
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;
137
sewardj17c5e2e2012-12-28 09:12:14 +0000138 VG_(printf)("\n");
139 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin SB rank %d "
140 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
florianddd61ff2015-01-04 17:20:45 +0000141 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
sewardj17c5e2e2012-12-28 09:12:14 +0000142 r,
florian227a1ec2014-12-12 19:32:10 +0000143 score_cumul, percent_cumul,
144 score_here, percent_here, tops[r].addr, name );
sewardj17c5e2e2012-12-28 09:12:14 +0000145 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);
150 }
151
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;
florian46cc0452014-10-25 19:20:38 +0000160
161 const HChar *name;
162 VG_(get_fnname_w_offset)(tops[r].addr, &name);
163
sewardj17c5e2e2012-12-28 09:12:14 +0000164 score_here = tops[r].score;
florian227a1ec2014-12-12 19:32:10 +0000165
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;
172
florianddd61ff2015-01-04 17:20:45 +0000173 VG_(printf)("%3d: (%9lld %5.2f%%) %9lld %5.2f%% 0x%lx %s\n",
sewardj17c5e2e2012-12-28 09:12:14 +0000174 r,
florian227a1ec2014-12-12 19:32:10 +0000175 score_cumul, percent_cumul,
176 score_here, percent_here, tops[r].addr, name );
sewardj17c5e2e2012-12-28 09:12:14 +0000177 score_cumul -= score_here;
178 }
179 VG_(printf)("rank ---cumulative--- -----self-----\n");
180
181 }
182
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");
193}
194
195
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. */
200void VG_(get_and_show_SB_profile) ( ULong ecs_done )
201{
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
213}
214
215
216/*--------------------------------------------------------------------*/
217/*--- end m_sbprofile.c ---*/
218/*--------------------------------------------------------------------*/