vgdb: Handle EAGAIN in read_buf
[valgrind.git] / coregrind / m_sbprofile.c
blob84563206ce5dc978643cd9aeadc1afecc19675d4
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-2017 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, see <http://www.gnu.org/licenses/>.
25 The GNU General Public License is contained in the file COPYING.
28 /* Contributed by Julian Seward <jseward@acm.org> */
30 #include "pub_core_basics.h"
31 #include "pub_core_transtab.h"
32 #include "pub_core_libcbase.h"
33 #include "pub_core_libcprint.h"
34 #include "pub_core_libcassert.h"
35 #include "pub_core_debuginfo.h"
36 #include "pub_core_translate.h"
37 #include "pub_core_options.h"
38 #include "pub_core_sbprofile.h" // self
40 /*====================================================================*/
41 /*=== SB profiling ===*/
42 /*====================================================================*/
44 static UInt n_profiles = 0;
46 static
47 void show_SB_profile ( const SBProfEntry tops[], UInt n_tops,
48 ULong score_total, ULong ecs_done )
50 ULong score_cumul, score_cumul_saved, score_here;
51 Int r; /* must be signed */
53 HChar ecs_txt[50]; // large enough
54 if (ecs_done > 0) {
55 VG_(sprintf)(ecs_txt, "%'llu ecs done", ecs_done);
56 } else {
57 VG_(strcpy)(ecs_txt, "for the entire run");
60 vg_assert(VG_(clo_profyle_sbs));
62 VG_(printf)("\n");
63 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
64 "<<<---<<<---<<<---<<<---<<<---<<<\n");
65 VG_(printf)("<<<---<<<---<<<---<<<---<<<---<<<---<<<---"
66 "<<<---<<<---<<<---<<<---<<<---<<<\n");
67 VG_(printf)("\n");
68 VG_(printf)("<<< BEGIN SB Profile #%u (%s)\n",
69 ++n_profiles, ecs_txt);
70 VG_(printf)("<<<\n");
71 VG_(printf)("\n");
73 VG_(printf)("Total score = %'llu\n\n", score_total);
75 // FIXME JRS EPOCH 28 July 2017: this is probably not right in general
76 DiEpoch cur_ep = VG_(current_DiEpoch)();
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)(cur_ep, tops[r].addr, &name);
90 score_here = tops[r].score;
91 score_cumul += score_here;
93 /* Careful: do not divide by zero. score_total == 0 implies
94 score_cumul == 0 and also score_here == 0. */
95 Double percent_cumul =
96 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
97 Double percent_here =
98 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
100 VG_(printf)("%3d: (%9llu %5.2f%%) %9llu %5.2f%% 0x%lx %s\n",
102 score_cumul, percent_cumul,
103 score_here, percent_here, tops[r].addr, name);
105 score_cumul_saved = score_cumul;
107 if (VG_(clo_profyle_flags) > 0) {
109 /* Show the details, if requested. */
110 VG_(printf)("\n");
111 VG_(printf)("-----------------------------"
112 "------------------------------\n");
113 VG_(printf)("--- SB Profile (SB details) "
114 " ---\n");
115 VG_(printf)("-----------------------------"
116 "------------------------------\n");
117 VG_(printf)("\n");
119 score_cumul = 0;
120 for (r = 0; r < n_tops; r++) {
121 if (tops[r].addr == 0)
122 continue;
123 if (tops[r].score == 0)
124 continue;
126 const HChar *name;
127 VG_(get_fnname_w_offset)(cur_ep, tops[r].addr, &name);
129 score_here = tops[r].score;
130 score_cumul += score_here;
132 /* Careful: do not divide by zero. score_total == 0 implies
133 score_cumul == 0 and also score_here == 0. */
134 Double percent_cumul =
135 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
136 Double percent_here =
137 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
139 VG_(printf)("\n");
140 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= begin SB rank %d "
141 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
142 VG_(printf)("%3d: (%9llu %5.2f%%) %9llu %5.2f%% 0x%lx %s\n",
144 score_cumul, percent_cumul,
145 score_here, percent_here, tops[r].addr, name );
146 VG_(printf)("\n");
147 VG_(discard_translations)(tops[r].addr, 1, "bb profile");
148 VG_(translate)(0, tops[r].addr, True, VG_(clo_profyle_flags), 0, True);
149 VG_(printf)("=-=-=-=-=-=-=-=-=-=-=-=-=-= end SB rank %d "
150 "=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n", r);
153 /* Print a final per-block summary, in reverse order, for the
154 convenience of people reading up from the end. */
155 score_cumul = score_cumul_saved;
156 for (r = n_tops-1; r >= 0; r--) {
157 if (tops[r].addr == 0)
158 continue;
159 if (tops[r].score == 0)
160 continue;
162 const HChar *name;
163 VG_(get_fnname_w_offset)(cur_ep, tops[r].addr, &name);
165 score_here = tops[r].score;
167 /* Careful: do not divide by zero. score_total == 0 implies
168 score_cumul == 0 and also score_here == 0. */
169 Double percent_cumul =
170 score_total == 0 ? 100.0 : score_cumul * 100.0 / score_total;
171 Double percent_here =
172 score_total == 0 ? 100.0 : score_here * 100.0 / score_total;
174 VG_(printf)("%3d: (%9llu %5.2f%%) %9llu %5.2f%% 0x%lx %s\n",
176 score_cumul, percent_cumul,
177 score_here, percent_here, tops[r].addr, name );
178 score_cumul -= score_here;
180 VG_(printf)("rank ---cumulative--- -----self-----\n");
184 VG_(printf)("\n");
185 VG_(printf)(">>>\n");
186 VG_(printf)(">>> END SB Profile #%u (%s)\n",
187 n_profiles, ecs_txt);
188 VG_(printf)(">>>\n");
189 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
190 ">>>--->>>--->>>--->>>--->>>--->>>\n");
191 VG_(printf)(">>>--->>>--->>>--->>>--->>>--->>>--->>>---"
192 ">>>--->>>--->>>--->>>--->>>--->>>\n");
193 VG_(printf)("\n");
197 /* Get and print a profile. Also, zero out the counters so that if we
198 call it again later, the second call will only show new work done
199 since the first call. ecs_done == 0 is taken to mean this is a
200 run-end profile. */
201 void VG_(get_and_show_SB_profile) ( ULong ecs_done )
203 /* The number of blocks to show for a end-of-run profile */
204 # define N_MAX_END 200
205 /* The number of blocks to show for a mid-run profile. */
206 # define N_MAX_INTERVAL 20
207 vg_assert(N_MAX_INTERVAL <= N_MAX_END);
208 SBProfEntry tops[N_MAX_END];
209 Int nToShow = ecs_done == 0 ? N_MAX_END : N_MAX_INTERVAL;
210 ULong score_total = VG_(get_SB_profile)(tops, nToShow);
211 show_SB_profile(tops, nToShow, score_total, ecs_done);
212 # undef N_MAX_END
213 # undef N_MAX_INTERVAL
217 /*--------------------------------------------------------------------*/
218 /*--- end m_sbprofile.c ---*/
219 /*--------------------------------------------------------------------*/