blob: 6c4e1e4712dbcd8138bcf69bc60119aad8442a92 [file] [log] [blame]
sewardj07133bf2002-06-13 10:25:56 +00001
njn4f9c9342002-04-29 16:03:24 +00002/*--------------------------------------------------------------------*/
njn101e5722005-04-21 02:37:54 +00003/*--- Cachegrind: everything but the simulation itself. ---*/
njn25cac76cb2002-09-23 11:21:57 +00004/*--- cg_main.c ---*/
njn4f9c9342002-04-29 16:03:24 +00005/*--------------------------------------------------------------------*/
6
7/*
nethercote137bc552003-11-14 17:47:54 +00008 This file is part of Cachegrind, a Valgrind tool for cache
njnc9539842002-10-02 13:26:35 +00009 profiling programs.
njn4f9c9342002-04-29 16:03:24 +000010
njn9f207462009-03-10 22:02:09 +000011 Copyright (C) 2002-2009 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000012 njn@valgrind.org
njn4f9c9342002-04-29 16:03:24 +000013
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
njn4f9c9342002-04-29 16:03:24 +000030*/
31
njnc7561b92005-06-19 01:24:32 +000032#include "pub_tool_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +000033#include "pub_tool_vki.h"
njnea27e462005-05-31 02:38:09 +000034#include "pub_tool_debuginfo.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_tool_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_tool_libcassert.h"
njneb8896b2005-06-04 20:03:55 +000037#include "pub_tool_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_tool_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +000039#include "pub_tool_libcproc.h"
njnf536bbb2005-06-13 04:21:38 +000040#include "pub_tool_machine.h"
njn717cde52005-05-10 02:47:21 +000041#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +000042#include "pub_tool_options.h"
njnd3bef4f2005-10-15 17:46:18 +000043#include "pub_tool_oset.h"
njn43b9a8a2005-05-10 04:37:01 +000044#include "pub_tool_tooliface.h"
sewardj14c7cc52007-02-25 15:08:24 +000045#include "pub_tool_xarray.h"
sewardj45f4e7c2005-09-27 19:20:21 +000046#include "pub_tool_clientstate.h"
sewardj5bb86822005-12-23 12:47:42 +000047#include "pub_tool_machine.h" // VG_(fnptr_to_fnentry)
njn25e49d8e72002-09-23 09:36:25 +000048
nethercoteb35a8b92004-09-11 16:45:27 +000049#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000050#include "cg_sim.c"
sewardj8badbaa2007-05-08 09:20:25 +000051#include "cg_branchpred.c"
njn4f9c9342002-04-29 16:03:24 +000052
njn25e49d8e72002-09-23 09:36:25 +000053/*------------------------------------------------------------*/
54/*--- Constants ---*/
55/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000056
sewardj5155dec2005-10-12 10:09:23 +000057/* Set to 1 for very verbose debugging */
58#define DEBUG_CG 0
59
nethercote9313ac42004-07-06 21:54:20 +000060#define MIN_LINE_SIZE 16
njnd3bef4f2005-10-15 17:46:18 +000061#define FILE_LEN VKI_PATH_MAX
nethercote9313ac42004-07-06 21:54:20 +000062#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000063
64/*------------------------------------------------------------*/
sewardj8badbaa2007-05-08 09:20:25 +000065/*--- Options ---*/
66/*------------------------------------------------------------*/
67
njn374a36d2007-11-23 01:41:32 +000068static Bool clo_cache_sim = True; /* do cache simulation? */
69static Bool clo_branch_sim = False; /* do branch simulation? */
70static Char* clo_cachegrind_out_file = "cachegrind.out.%p";
sewardj8badbaa2007-05-08 09:20:25 +000071
72/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000073/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000074/*------------------------------------------------------------*/
75
sewardj8badbaa2007-05-08 09:20:25 +000076typedef
77 struct {
78 ULong a; /* total # memory accesses of this kind */
79 ULong m1; /* misses in the first level cache */
80 ULong m2; /* misses in the second level cache */
81 }
82 CacheCC;
83
84typedef
85 struct {
86 ULong b; /* total # branches of this kind */
87 ULong mp; /* number of branches mispredicted */
88 }
89 BranchCC;
njn4f9c9342002-04-29 16:03:24 +000090
nethercote9313ac42004-07-06 21:54:20 +000091//------------------------------------------------------------
92// Primary data structure #1: CC table
93// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
njnd3bef4f2005-10-15 17:46:18 +000094// - an ordered set of CCs. CC indexing done by file/function/line (as
95// determined from the instrAddr).
nethercote9313ac42004-07-06 21:54:20 +000096// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000097
njnd3bef4f2005-10-15 17:46:18 +000098typedef struct {
99 Char* file;
100 Char* fn;
101 Int line;
102}
103CodeLoc;
njn4f9c9342002-04-29 16:03:24 +0000104
sewardj8badbaa2007-05-08 09:20:25 +0000105typedef struct {
106 CodeLoc loc; /* Source location that these counts pertain to */
107 CacheCC Ir; /* Insn read counts */
108 CacheCC Dr; /* Data read counts */
109 CacheCC Dw; /* Data write/modify counts */
110 BranchCC Bc; /* Conditional branch counts */
111 BranchCC Bi; /* Indirect branch counts */
112} LineCC;
njn4f9c9342002-04-29 16:03:24 +0000113
njnd3bef4f2005-10-15 17:46:18 +0000114// First compare file, then fn, then line.
tom5a835d52007-12-30 12:28:26 +0000115static Word cmp_CodeLoc_LineCC(const void *vloc, const void *vcc)
njnd3bef4f2005-10-15 17:46:18 +0000116{
njnafa12262005-12-24 03:10:56 +0000117 Word res;
njnd3bef4f2005-10-15 17:46:18 +0000118 CodeLoc* a = (CodeLoc*)vloc;
119 CodeLoc* b = &(((LineCC*)vcc)->loc);
njn4f9c9342002-04-29 16:03:24 +0000120
njnd3bef4f2005-10-15 17:46:18 +0000121 res = VG_(strcmp)(a->file, b->file);
122 if (0 != res)
123 return res;
njn4f9c9342002-04-29 16:03:24 +0000124
njnd3bef4f2005-10-15 17:46:18 +0000125 res = VG_(strcmp)(a->fn, b->fn);
126 if (0 != res)
127 return res;
128
129 return a->line - b->line;
130}
131
132static OSet* CC_table;
njn4f9c9342002-04-29 16:03:24 +0000133
nethercote9313ac42004-07-06 21:54:20 +0000134//------------------------------------------------------------
njnd3bef4f2005-10-15 17:46:18 +0000135// Primary data structure #2: InstrInfo table
nethercote9313ac42004-07-06 21:54:20 +0000136// - Holds the cached info about each instr that is used for simulation.
sewardj0b9d74a2006-12-24 02:24:11 +0000137// - table(SB_start_addr, list(InstrInfo))
138// - For each SB, each InstrInfo in the list holds info about the
njnd3bef4f2005-10-15 17:46:18 +0000139// instruction (instrLen, instrAddr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000140// CC. This node is what's passed to the simulation function.
sewardj0b9d74a2006-12-24 02:24:11 +0000141// - When SBs are discarded the relevant list(instr_details) is freed.
nethercote9313ac42004-07-06 21:54:20 +0000142
njnd3bef4f2005-10-15 17:46:18 +0000143typedef struct _InstrInfo InstrInfo;
144struct _InstrInfo {
nethercoteca1f2dc2004-07-21 08:49:02 +0000145 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000146 UChar instr_len;
njnd3bef4f2005-10-15 17:46:18 +0000147 LineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000148};
149
sewardj0b9d74a2006-12-24 02:24:11 +0000150typedef struct _SB_info SB_info;
151struct _SB_info {
152 Addr SB_addr; // key; MUST BE FIRST
njnd3bef4f2005-10-15 17:46:18 +0000153 Int n_instrs;
154 InstrInfo instrs[0];
nethercote9313ac42004-07-06 21:54:20 +0000155};
156
njnd3bef4f2005-10-15 17:46:18 +0000157static OSet* instrInfoTable;
158
159//------------------------------------------------------------
160// Secondary data structure: string table
161// - holds strings, avoiding dups
162// - used for filenames and function names, each of which will be
163// pointed to by one or more CCs.
164// - it also allows equality checks just by pointer comparison, which
165// is good when printing the output file at the end.
166
167static OSet* stringTable;
nethercote9313ac42004-07-06 21:54:20 +0000168
169//------------------------------------------------------------
170// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000171static Int distinct_files = 0;
172static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000173static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000174static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000175
njnd3bef4f2005-10-15 17:46:18 +0000176static Int full_debugs = 0;
177static Int file_line_debugs = 0;
178static Int fn_debugs = 0;
179static Int no_debugs = 0;
njn4f9c9342002-04-29 16:03:24 +0000180
nethercote9313ac42004-07-06 21:54:20 +0000181/*------------------------------------------------------------*/
njnd3bef4f2005-10-15 17:46:18 +0000182/*--- String table operations ---*/
183/*------------------------------------------------------------*/
184
tom5a835d52007-12-30 12:28:26 +0000185static Word stringCmp( const void* key, const void* elem )
njnd3bef4f2005-10-15 17:46:18 +0000186{
187 return VG_(strcmp)(*(Char**)key, *(Char**)elem);
188}
189
190// Get a permanent string; either pull it out of the string table if it's
191// been encountered before, or dup it and put it into the string table.
192static Char* get_perm_string(Char* s)
193{
njne2a9ad32007-09-17 05:30:48 +0000194 Char** s_ptr = VG_(OSetGen_Lookup)(stringTable, &s);
njnd3bef4f2005-10-15 17:46:18 +0000195 if (s_ptr) {
196 return *s_ptr;
197 } else {
njne2a9ad32007-09-17 05:30:48 +0000198 Char** s_node = VG_(OSetGen_AllocNode)(stringTable, sizeof(Char*));
sewardj9c606bd2008-09-18 18:12:50 +0000199 *s_node = VG_(strdup)("cg.main.gps.1", s);
njne2a9ad32007-09-17 05:30:48 +0000200 VG_(OSetGen_Insert)(stringTable, s_node);
njnd3bef4f2005-10-15 17:46:18 +0000201 return *s_node;
202 }
203}
204
205/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000206/*--- CC table operations ---*/
207/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000208
nethercote9313ac42004-07-06 21:54:20 +0000209static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
210 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000211{
njnf3b61d62007-09-17 00:41:07 +0000212 Char dir[FILE_LEN];
213 Bool found_dirname;
sewardj7cee6f92005-06-13 17:39:06 +0000214 Bool found_file_line = VG_(get_filename_linenum)(
215 instr_addr,
216 file, FILE_LEN,
njnf3b61d62007-09-17 00:41:07 +0000217 dir, FILE_LEN, &found_dirname,
sewardj7cee6f92005-06-13 17:39:06 +0000218 line
219 );
nethercote9313ac42004-07-06 21:54:20 +0000220 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000221
nethercote9313ac42004-07-06 21:54:20 +0000222 if (!found_file_line) {
223 VG_(strcpy)(file, "???");
224 *line = 0;
225 }
226 if (!found_fn) {
227 VG_(strcpy)(fn, "???");
228 }
njnf3b61d62007-09-17 00:41:07 +0000229
230 if (found_dirname) {
231 // +1 for the '/'.
232 tl_assert(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILE_LEN);
233 VG_(strcat)(dir, "/"); // Append '/'
234 VG_(strcat)(dir, file); // Append file to dir
235 VG_(strcpy)(file, dir); // Move dir+file to file
236 }
237
nethercote9313ac42004-07-06 21:54:20 +0000238 if (found_file_line) {
njnd3bef4f2005-10-15 17:46:18 +0000239 if (found_fn) full_debugs++;
240 else file_line_debugs++;
nethercote9313ac42004-07-06 21:54:20 +0000241 } else {
njnd3bef4f2005-10-15 17:46:18 +0000242 if (found_fn) fn_debugs++;
243 else no_debugs++;
njn4f9c9342002-04-29 16:03:24 +0000244 }
245}
246
nethercote9313ac42004-07-06 21:54:20 +0000247// Do a three step traversal: by file, then fn, then line.
njnd3bef4f2005-10-15 17:46:18 +0000248// Returns a pointer to the line CC, creates a new one if necessary.
249static LineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000250{
nethercote9313ac42004-07-06 21:54:20 +0000251 Char file[FILE_LEN], fn[FN_LEN];
252 Int line;
njnd3bef4f2005-10-15 17:46:18 +0000253 CodeLoc loc;
254 LineCC* lineCC;
nethercote9313ac42004-07-06 21:54:20 +0000255
njn6a3009b2005-03-20 00:20:06 +0000256 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000257
njnd3bef4f2005-10-15 17:46:18 +0000258 loc.file = file;
259 loc.fn = fn;
260 loc.line = line;
njn4f9c9342002-04-29 16:03:24 +0000261
njne2a9ad32007-09-17 05:30:48 +0000262 lineCC = VG_(OSetGen_Lookup)(CC_table, &loc);
njnd3bef4f2005-10-15 17:46:18 +0000263 if (!lineCC) {
264 // Allocate and zero a new node.
njne2a9ad32007-09-17 05:30:48 +0000265 lineCC = VG_(OSetGen_AllocNode)(CC_table, sizeof(LineCC));
njnd3bef4f2005-10-15 17:46:18 +0000266 lineCC->loc.file = get_perm_string(loc.file);
267 lineCC->loc.fn = get_perm_string(loc.fn);
268 lineCC->loc.line = loc.line;
njn0a8db5c2007-04-02 03:11:41 +0000269 lineCC->Ir.a = 0;
270 lineCC->Ir.m1 = 0;
271 lineCC->Ir.m2 = 0;
272 lineCC->Dr.a = 0;
273 lineCC->Dr.m1 = 0;
274 lineCC->Dr.m2 = 0;
275 lineCC->Dw.a = 0;
276 lineCC->Dw.m1 = 0;
277 lineCC->Dw.m2 = 0;
sewardj8badbaa2007-05-08 09:20:25 +0000278 lineCC->Bc.b = 0;
279 lineCC->Bc.mp = 0;
280 lineCC->Bi.b = 0;
281 lineCC->Bi.mp = 0;
njne2a9ad32007-09-17 05:30:48 +0000282 VG_(OSetGen_Insert)(CC_table, lineCC);
njn4f9c9342002-04-29 16:03:24 +0000283 }
nethercote9313ac42004-07-06 21:54:20 +0000284
njnd3bef4f2005-10-15 17:46:18 +0000285 return lineCC;
njn4f9c9342002-04-29 16:03:24 +0000286}
287
288/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000289/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000290/*------------------------------------------------------------*/
291
njnaf839f52005-06-23 03:27:57 +0000292static VG_REGPARM(1)
njnd3bef4f2005-10-15 17:46:18 +0000293void log_1I_0D_cache_access(InstrInfo* n)
njn25e49d8e72002-09-23 09:36:25 +0000294{
sewardj5155dec2005-10-12 10:09:23 +0000295 //VG_(printf)("1I_0D : CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n",
296 // n, n->instr_addr, n->instr_len);
njn6a3009b2005-03-20 00:20:06 +0000297 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000298 &n->parent->Ir.m1, &n->parent->Ir.m2);
299 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000300}
301
njnaf839f52005-06-23 03:27:57 +0000302static VG_REGPARM(2)
njnd3bef4f2005-10-15 17:46:18 +0000303void log_2I_0D_cache_access(InstrInfo* n, InstrInfo* n2)
njn25e49d8e72002-09-23 09:36:25 +0000304{
sewardj5155dec2005-10-12 10:09:23 +0000305 //VG_(printf)("2I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n"
306 // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n",
307 // n, n->instr_addr, n->instr_len,
308 // n2, n2->instr_addr, n2->instr_len);
sewardj5155dec2005-10-12 10:09:23 +0000309 cachesim_I1_doref(n->instr_addr, n->instr_len,
310 &n->parent->Ir.m1, &n->parent->Ir.m2);
311 n->parent->Ir.a++;
312 cachesim_I1_doref(n2->instr_addr, n2->instr_len,
313 &n2->parent->Ir.m1, &n2->parent->Ir.m2);
314 n2->parent->Ir.a++;
sewardj5155dec2005-10-12 10:09:23 +0000315}
316
317static VG_REGPARM(3)
njnd3bef4f2005-10-15 17:46:18 +0000318void log_3I_0D_cache_access(InstrInfo* n, InstrInfo* n2, InstrInfo* n3)
sewardj5155dec2005-10-12 10:09:23 +0000319{
320 //VG_(printf)("3I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n"
321 // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n"
322 // " CC3addr=0x%010lx, i3addr=0x%010lx, i3size=%lu\n",
323 // n, n->instr_addr, n->instr_len,
324 // n2, n2->instr_addr, n2->instr_len,
325 // n3, n3->instr_addr, n3->instr_len);
sewardj5155dec2005-10-12 10:09:23 +0000326 cachesim_I1_doref(n->instr_addr, n->instr_len,
327 &n->parent->Ir.m1, &n->parent->Ir.m2);
328 n->parent->Ir.a++;
329 cachesim_I1_doref(n2->instr_addr, n2->instr_len,
330 &n2->parent->Ir.m1, &n2->parent->Ir.m2);
331 n2->parent->Ir.a++;
332 cachesim_I1_doref(n3->instr_addr, n3->instr_len,
333 &n3->parent->Ir.m1, &n3->parent->Ir.m2);
334 n3->parent->Ir.a++;
sewardj5155dec2005-10-12 10:09:23 +0000335}
336
337static VG_REGPARM(3)
njnd3bef4f2005-10-15 17:46:18 +0000338void log_1I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size)
sewardj5155dec2005-10-12 10:09:23 +0000339{
340 //VG_(printf)("1I_1Dr: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n"
341 // " daddr=0x%010lx, dsize=%lu\n",
342 // n, n->instr_addr, n->instr_len, data_addr, data_size);
njn6a3009b2005-03-20 00:20:06 +0000343 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000344 &n->parent->Ir.m1, &n->parent->Ir.m2);
345 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000346
sewardj5155dec2005-10-12 10:09:23 +0000347 cachesim_D1_doref(data_addr, data_size,
nethercote9313ac42004-07-06 21:54:20 +0000348 &n->parent->Dr.m1, &n->parent->Dr.m2);
349 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000350}
351
sewardj5155dec2005-10-12 10:09:23 +0000352static VG_REGPARM(3)
njnd3bef4f2005-10-15 17:46:18 +0000353void log_1I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size)
njn25e49d8e72002-09-23 09:36:25 +0000354{
sewardj5155dec2005-10-12 10:09:23 +0000355 //VG_(printf)("1I_1Dw: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n"
356 // " daddr=0x%010lx, dsize=%lu\n",
357 // n, n->instr_addr, n->instr_len, data_addr, data_size);
njn6a3009b2005-03-20 00:20:06 +0000358 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000359 &n->parent->Ir.m1, &n->parent->Ir.m2);
360 n->parent->Ir.a++;
361
sewardj5155dec2005-10-12 10:09:23 +0000362 cachesim_D1_doref(data_addr, data_size,
nethercote9313ac42004-07-06 21:54:20 +0000363 &n->parent->Dw.m1, &n->parent->Dw.m2);
364 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000365}
366
njnaf839f52005-06-23 03:27:57 +0000367static VG_REGPARM(3)
njnd3bef4f2005-10-15 17:46:18 +0000368void log_0I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size)
njn25e49d8e72002-09-23 09:36:25 +0000369{
sewardj5155dec2005-10-12 10:09:23 +0000370 //VG_(printf)("0I_1Dr: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n",
371 // n, data_addr, data_size);
sewardj5155dec2005-10-12 10:09:23 +0000372 cachesim_D1_doref(data_addr, data_size,
nethercote9313ac42004-07-06 21:54:20 +0000373 &n->parent->Dr.m1, &n->parent->Dr.m2);
374 n->parent->Dr.a++;
sewardj5155dec2005-10-12 10:09:23 +0000375}
376
377static VG_REGPARM(3)
njnd3bef4f2005-10-15 17:46:18 +0000378void log_0I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size)
sewardj5155dec2005-10-12 10:09:23 +0000379{
380 //VG_(printf)("0I_1Dw: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n",
381 // n, data_addr, data_size);
sewardj5155dec2005-10-12 10:09:23 +0000382 cachesim_D1_doref(data_addr, data_size,
nethercote9313ac42004-07-06 21:54:20 +0000383 &n->parent->Dw.m1, &n->parent->Dw.m2);
384 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000385}
386
sewardj8badbaa2007-05-08 09:20:25 +0000387/* For branches, we consult two different predictors, one which
388 predicts taken/untaken for conditional branches, and the other
389 which predicts the branch target address for indirect branches
390 (jump-to-register style ones). */
391
392static VG_REGPARM(2)
393void log_cond_branch(InstrInfo* n, Word taken)
394{
395 //VG_(printf)("cbrnch: CCaddr=0x%010lx, taken=0x%010lx\n",
396 // n, taken);
397 n->parent->Bc.b++;
398 n->parent->Bc.mp
399 += (1 & do_cond_branch_predict(n->instr_addr, taken));
400}
401
402static VG_REGPARM(2)
403void log_ind_branch(InstrInfo* n, UWord actual_dst)
404{
405 //VG_(printf)("ibrnch: CCaddr=0x%010lx, dst=0x%010lx\n",
406 // n, actual_dst);
407 n->parent->Bi.b++;
408 n->parent->Bi.mp
409 += (1 & do_ind_branch_predict(n->instr_addr, actual_dst));
410}
411
412
nethercote9313ac42004-07-06 21:54:20 +0000413/*------------------------------------------------------------*/
sewardj5155dec2005-10-12 10:09:23 +0000414/*--- Instrumentation types and structures ---*/
415/*------------------------------------------------------------*/
416
417/* Maintain an ordered list of memory events which are outstanding, in
418 the sense that no IR has yet been generated to do the relevant
419 helper calls. The BB is scanned top to bottom and memory events
420 are added to the end of the list, merging with the most recent
421 notified event where possible (Dw immediately following Dr and
422 having the same size and EA can be merged).
423
424 This merging is done so that for architectures which have
425 load-op-store instructions (x86, amd64), the insn is treated as if
426 it makes just one memory reference (a modify), rather than two (a
427 read followed by a write at the same address).
428
429 At various points the list will need to be flushed, that is, IR
430 generated from it. That must happen before any possible exit from
431 the block (the end, or an IRStmt_Exit). Flushing also takes place
432 when there is no space to add a new event.
433
434 If we require the simulation statistics to be up to date with
435 respect to possible memory exceptions, then the list would have to
436 be flushed before each memory reference. That would however lose
437 performance by inhibiting event-merging during flushing.
438
439 Flushing the list consists of walking it start to end and emitting
440 instrumentation IR for each event, in the order in which they
441 appear. It may be possible to emit a single call for two adjacent
442 events in order to reduce the number of helper function calls made.
443 For example, it could well be profitable to handle two adjacent Ir
444 events with a single helper call. */
445
446typedef
447 IRExpr
448 IRAtom;
449
450typedef
sewardj8badbaa2007-05-08 09:20:25 +0000451 enum {
452 Ev_Ir, // Instruction read
453 Ev_Dr, // Data read
454 Ev_Dw, // Data write
455 Ev_Dm, // Data modify (read then write)
456 Ev_Bc, // branch conditional
457 Ev_Bi // branch indirect (to unknown destination)
458 }
459 EventTag;
sewardj5155dec2005-10-12 10:09:23 +0000460
461typedef
462 struct {
sewardj8badbaa2007-05-08 09:20:25 +0000463 EventTag tag;
464 InstrInfo* inode;
465 union {
466 struct {
467 } Ir;
468 struct {
469 IRAtom* ea;
470 Int szB;
471 } Dr;
472 struct {
473 IRAtom* ea;
474 Int szB;
475 } Dw;
476 struct {
477 IRAtom* ea;
478 Int szB;
479 } Dm;
480 struct {
481 IRAtom* taken; /* :: Ity_I1 */
482 } Bc;
483 struct {
484 IRAtom* dst;
485 } Bi;
486 } Ev;
sewardj5155dec2005-10-12 10:09:23 +0000487 }
488 Event;
489
sewardj8badbaa2007-05-08 09:20:25 +0000490static void init_Event ( Event* ev ) {
491 VG_(memset)(ev, 0, sizeof(Event));
492}
493
494static IRAtom* get_Event_dea ( Event* ev ) {
495 switch (ev->tag) {
496 case Ev_Dr: return ev->Ev.Dr.ea;
497 case Ev_Dw: return ev->Ev.Dw.ea;
498 case Ev_Dm: return ev->Ev.Dm.ea;
499 default: tl_assert(0);
500 }
501}
502
503static Int get_Event_dszB ( Event* ev ) {
504 switch (ev->tag) {
505 case Ev_Dr: return ev->Ev.Dr.szB;
506 case Ev_Dw: return ev->Ev.Dw.szB;
507 case Ev_Dm: return ev->Ev.Dm.szB;
508 default: tl_assert(0);
509 }
510}
511
512
sewardj5155dec2005-10-12 10:09:23 +0000513/* Up to this many unnotified events are allowed. Number is
514 arbitrary. Larger numbers allow more event merging to occur, but
515 potentially induce more spilling due to extending live ranges of
516 address temporaries. */
517#define N_EVENTS 16
518
519
520/* A struct which holds all the running state during instrumentation.
521 Mostly to avoid passing loads of parameters everywhere. */
522typedef
523 struct {
524 /* The current outstanding-memory-event list. */
525 Event events[N_EVENTS];
526 Int events_used;
527
njnd3bef4f2005-10-15 17:46:18 +0000528 /* The array of InstrInfo bins for the BB. */
sewardj0b9d74a2006-12-24 02:24:11 +0000529 SB_info* sbInfo;
sewardj5155dec2005-10-12 10:09:23 +0000530
njnd3bef4f2005-10-15 17:46:18 +0000531 /* Number InstrInfo bins 'used' so far. */
sewardj0b9d74a2006-12-24 02:24:11 +0000532 Int sbInfo_i;
sewardj5155dec2005-10-12 10:09:23 +0000533
sewardj0b9d74a2006-12-24 02:24:11 +0000534 /* The output SB being constructed. */
535 IRSB* sbOut;
sewardj5155dec2005-10-12 10:09:23 +0000536 }
537 CgState;
538
539
sewardj5155dec2005-10-12 10:09:23 +0000540/*------------------------------------------------------------*/
541/*--- Instrumentation main ---*/
nethercote9313ac42004-07-06 21:54:20 +0000542/*------------------------------------------------------------*/
543
sewardj4ba057c2005-10-18 12:04:18 +0000544// Note that origAddr is the real origAddr, not the address of the first
545// instruction in the block (they can be different due to redirection).
nethercote564b2b02004-08-07 15:54:53 +0000546static
sewardj0b9d74a2006-12-24 02:24:11 +0000547SB_info* get_SB_info(IRSB* sbIn, Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000548{
njn4bd67b52005-08-11 00:47:10 +0000549 Int i, n_instrs;
550 IRStmt* st;
sewardj0b9d74a2006-12-24 02:24:11 +0000551 SB_info* sbInfo;
njnd3bef4f2005-10-15 17:46:18 +0000552
sewardj0b9d74a2006-12-24 02:24:11 +0000553 // Count number of original instrs in SB
njn6a3009b2005-03-20 00:20:06 +0000554 n_instrs = 0;
sewardj0b9d74a2006-12-24 02:24:11 +0000555 for (i = 0; i < sbIn->stmts_used; i++) {
556 st = sbIn->stmts[i];
njn6a3009b2005-03-20 00:20:06 +0000557 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000558 }
559
njnf7d26092005-10-12 16:45:17 +0000560 // Check that we don't have an entry for this BB in the instr-info table.
561 // If this assertion fails, there has been some screwup: some
562 // translations must have been discarded but Cachegrind hasn't discarded
563 // the corresponding entries in the instr-info table.
njne2a9ad32007-09-17 05:30:48 +0000564 sbInfo = VG_(OSetGen_Lookup)(instrInfoTable, &origAddr);
sewardj0b9d74a2006-12-24 02:24:11 +0000565 tl_assert(NULL == sbInfo);
sewardja3a29a52005-10-12 16:16:03 +0000566
njnd3bef4f2005-10-15 17:46:18 +0000567 // BB never translated before (at this address, at least; could have
568 // been unloaded and then reloaded elsewhere in memory)
njne2a9ad32007-09-17 05:30:48 +0000569 sbInfo = VG_(OSetGen_AllocNode)(instrInfoTable,
sewardj0b9d74a2006-12-24 02:24:11 +0000570 sizeof(SB_info) + n_instrs*sizeof(InstrInfo));
571 sbInfo->SB_addr = origAddr;
572 sbInfo->n_instrs = n_instrs;
njne2a9ad32007-09-17 05:30:48 +0000573 VG_(OSetGen_Insert)( instrInfoTable, sbInfo );
sewardja3a29a52005-10-12 16:16:03 +0000574 distinct_instrs++;
575
sewardj0b9d74a2006-12-24 02:24:11 +0000576 return sbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000577}
njn6a3009b2005-03-20 00:20:06 +0000578
nethercote9313ac42004-07-06 21:54:20 +0000579
sewardj5155dec2005-10-12 10:09:23 +0000580static void showEvent ( Event* ev )
nethercote9313ac42004-07-06 21:54:20 +0000581{
sewardj8badbaa2007-05-08 09:20:25 +0000582 switch (ev->tag) {
583 case Ev_Ir:
njnfd9f6222005-10-16 00:17:37 +0000584 VG_(printf)("Ir %p\n", ev->inode);
sewardj5155dec2005-10-12 10:09:23 +0000585 break;
sewardj8badbaa2007-05-08 09:20:25 +0000586 case Ev_Dr:
587 VG_(printf)("Dr %p %d EA=", ev->inode, ev->Ev.Dr.szB);
588 ppIRExpr(ev->Ev.Dr.ea);
sewardj5155dec2005-10-12 10:09:23 +0000589 VG_(printf)("\n");
590 break;
sewardj8badbaa2007-05-08 09:20:25 +0000591 case Ev_Dw:
592 VG_(printf)("Dw %p %d EA=", ev->inode, ev->Ev.Dw.szB);
593 ppIRExpr(ev->Ev.Dw.ea);
sewardj5155dec2005-10-12 10:09:23 +0000594 VG_(printf)("\n");
595 break;
sewardj8badbaa2007-05-08 09:20:25 +0000596 case Ev_Dm:
597 VG_(printf)("Dm %p %d EA=", ev->inode, ev->Ev.Dm.szB);
598 ppIRExpr(ev->Ev.Dm.ea);
599 VG_(printf)("\n");
600 break;
601 case Ev_Bc:
602 VG_(printf)("Bc %p GA=", ev->inode);
603 ppIRExpr(ev->Ev.Bc.taken);
604 VG_(printf)("\n");
605 break;
606 case Ev_Bi:
607 VG_(printf)("Bi %p DST=", ev->inode);
608 ppIRExpr(ev->Ev.Bi.dst);
sewardj5155dec2005-10-12 10:09:23 +0000609 VG_(printf)("\n");
610 break;
611 default:
612 tl_assert(0);
613 break;
614 }
njn6a3009b2005-03-20 00:20:06 +0000615}
616
njnfd9f6222005-10-16 00:17:37 +0000617// Reserve and initialise an InstrInfo for the first mention of a new insn.
618static
619InstrInfo* setup_InstrInfo ( CgState* cgs, Addr instr_addr, UInt instr_len )
njn6a3009b2005-03-20 00:20:06 +0000620{
njnd3bef4f2005-10-15 17:46:18 +0000621 InstrInfo* i_node;
sewardj0b9d74a2006-12-24 02:24:11 +0000622 tl_assert(cgs->sbInfo_i >= 0);
623 tl_assert(cgs->sbInfo_i < cgs->sbInfo->n_instrs);
624 i_node = &cgs->sbInfo->instrs[ cgs->sbInfo_i ];
njnfd9f6222005-10-16 00:17:37 +0000625 i_node->instr_addr = instr_addr;
626 i_node->instr_len = instr_len;
627 i_node->parent = get_lineCC(instr_addr);
sewardj0b9d74a2006-12-24 02:24:11 +0000628 cgs->sbInfo_i++;
sewardj5155dec2005-10-12 10:09:23 +0000629 return i_node;
630}
sewardj17a56bf2005-03-21 01:35:02 +0000631
sewardj17a56bf2005-03-21 01:35:02 +0000632
sewardj5155dec2005-10-12 10:09:23 +0000633/* Generate code for all outstanding memory events, and mark the queue
634 empty. Code is generated into cgs->bbOut, and this activity
sewardj0b9d74a2006-12-24 02:24:11 +0000635 'consumes' slots in cgs->sbInfo. */
njn6a3009b2005-03-20 00:20:06 +0000636
sewardj5155dec2005-10-12 10:09:23 +0000637static void flushEvents ( CgState* cgs )
638{
njnd3bef4f2005-10-15 17:46:18 +0000639 Int i, regparms;
640 Char* helperName;
641 void* helperAddr;
642 IRExpr** argv;
643 IRExpr* i_node_expr;
njnd3bef4f2005-10-15 17:46:18 +0000644 IRDirty* di;
njnc285dca2005-10-15 22:07:28 +0000645 Event* ev;
646 Event* ev2;
647 Event* ev3;
njn6a3009b2005-03-20 00:20:06 +0000648
sewardj5155dec2005-10-12 10:09:23 +0000649 i = 0;
650 while (i < cgs->events_used) {
njn6a3009b2005-03-20 00:20:06 +0000651
sewardj5155dec2005-10-12 10:09:23 +0000652 helperName = NULL;
653 helperAddr = NULL;
654 argv = NULL;
655 regparms = 0;
656
657 /* generate IR to notify event i and possibly the ones
658 immediately following it. */
659 tl_assert(i >= 0 && i < cgs->events_used);
njnc285dca2005-10-15 22:07:28 +0000660
661 ev = &cgs->events[i];
662 ev2 = ( i < cgs->events_used-1 ? &cgs->events[i+1] : NULL );
663 ev3 = ( i < cgs->events_used-2 ? &cgs->events[i+2] : NULL );
664
sewardj5155dec2005-10-12 10:09:23 +0000665 if (DEBUG_CG) {
666 VG_(printf)(" flush ");
njnc285dca2005-10-15 22:07:28 +0000667 showEvent( ev );
njn4f9c9342002-04-29 16:03:24 +0000668 }
sewardj5155dec2005-10-12 10:09:23 +0000669
njnfd9f6222005-10-16 00:17:37 +0000670 i_node_expr = mkIRExpr_HWord( (HWord)ev->inode );
sewardj5155dec2005-10-12 10:09:23 +0000671
672 /* Decide on helper fn to call and args to pass it, and advance
673 i appropriately. */
sewardj8badbaa2007-05-08 09:20:25 +0000674 switch (ev->tag) {
675 case Ev_Ir:
676 /* Merge an Ir with a following Dr/Dm. */
677 if (ev2 && (ev2->tag == Ev_Dr || ev2->tag == Ev_Dm)) {
678 /* Why is this true? It's because we're merging an Ir
679 with a following Dr or Dm. The Ir derives from the
680 instruction's IMark and the Dr/Dm from data
681 references which follow it. In short it holds
682 because each insn starts with an IMark, hence an
683 Ev_Ir, and so these Dr/Dm must pertain to the
684 immediately preceding Ir. Same applies to analogous
685 assertions in the subsequent cases. */
njnfd9f6222005-10-16 00:17:37 +0000686 tl_assert(ev2->inode == ev->inode);
sewardj5155dec2005-10-12 10:09:23 +0000687 helperName = "log_1I_1Dr_cache_access";
688 helperAddr = &log_1I_1Dr_cache_access;
689 argv = mkIRExprVec_3( i_node_expr,
sewardj8badbaa2007-05-08 09:20:25 +0000690 get_Event_dea(ev2),
691 mkIRExpr_HWord( get_Event_dszB(ev2) ) );
sewardj5155dec2005-10-12 10:09:23 +0000692 regparms = 3;
693 i += 2;
694 }
sewardj8badbaa2007-05-08 09:20:25 +0000695 /* Merge an Ir with a following Dw. */
sewardj5155dec2005-10-12 10:09:23 +0000696 else
sewardj8badbaa2007-05-08 09:20:25 +0000697 if (ev2 && ev2->tag == Ev_Dw) {
njnfd9f6222005-10-16 00:17:37 +0000698 tl_assert(ev2->inode == ev->inode);
sewardj5155dec2005-10-12 10:09:23 +0000699 helperName = "log_1I_1Dw_cache_access";
700 helperAddr = &log_1I_1Dw_cache_access;
701 argv = mkIRExprVec_3( i_node_expr,
sewardj8badbaa2007-05-08 09:20:25 +0000702 get_Event_dea(ev2),
703 mkIRExpr_HWord( get_Event_dszB(ev2) ) );
sewardj5155dec2005-10-12 10:09:23 +0000704 regparms = 3;
705 i += 2;
706 }
sewardj8badbaa2007-05-08 09:20:25 +0000707 /* Merge an Ir with two following Irs. */
sewardj5155dec2005-10-12 10:09:23 +0000708 else
sewardj8badbaa2007-05-08 09:20:25 +0000709 if (ev2 && ev3 && ev2->tag == Ev_Ir && ev3->tag == Ev_Ir)
njnc285dca2005-10-15 22:07:28 +0000710 {
sewardj5155dec2005-10-12 10:09:23 +0000711 helperName = "log_3I_0D_cache_access";
712 helperAddr = &log_3I_0D_cache_access;
njnfd9f6222005-10-16 00:17:37 +0000713 argv = mkIRExprVec_3( i_node_expr,
714 mkIRExpr_HWord( (HWord)ev2->inode ),
715 mkIRExpr_HWord( (HWord)ev3->inode ) );
sewardj5155dec2005-10-12 10:09:23 +0000716 regparms = 3;
717 i += 3;
718 }
sewardj8badbaa2007-05-08 09:20:25 +0000719 /* Merge an Ir with one following Ir. */
sewardj5155dec2005-10-12 10:09:23 +0000720 else
sewardj8badbaa2007-05-08 09:20:25 +0000721 if (ev2 && ev2->tag == Ev_Ir) {
sewardj5155dec2005-10-12 10:09:23 +0000722 helperName = "log_2I_0D_cache_access";
723 helperAddr = &log_2I_0D_cache_access;
njnfd9f6222005-10-16 00:17:37 +0000724 argv = mkIRExprVec_2( i_node_expr,
725 mkIRExpr_HWord( (HWord)ev2->inode ) );
sewardj5155dec2005-10-12 10:09:23 +0000726 regparms = 2;
727 i += 2;
728 }
729 /* No merging possible; emit as-is. */
730 else {
731 helperName = "log_1I_0D_cache_access";
732 helperAddr = &log_1I_0D_cache_access;
733 argv = mkIRExprVec_1( i_node_expr );
734 regparms = 1;
735 i++;
736 }
737 break;
sewardj8badbaa2007-05-08 09:20:25 +0000738 case Ev_Dr:
739 case Ev_Dm:
740 /* Data read or modify */
sewardj5155dec2005-10-12 10:09:23 +0000741 helperName = "log_0I_1Dr_cache_access";
742 helperAddr = &log_0I_1Dr_cache_access;
743 argv = mkIRExprVec_3( i_node_expr,
sewardj8badbaa2007-05-08 09:20:25 +0000744 get_Event_dea(ev),
745 mkIRExpr_HWord( get_Event_dszB(ev) ) );
sewardj5155dec2005-10-12 10:09:23 +0000746 regparms = 3;
747 i++;
748 break;
sewardj8badbaa2007-05-08 09:20:25 +0000749 case Ev_Dw:
750 /* Data write */
sewardj5155dec2005-10-12 10:09:23 +0000751 helperName = "log_0I_1Dw_cache_access";
752 helperAddr = &log_0I_1Dw_cache_access;
753 argv = mkIRExprVec_3( i_node_expr,
sewardj8badbaa2007-05-08 09:20:25 +0000754 get_Event_dea(ev),
755 mkIRExpr_HWord( get_Event_dszB(ev) ) );
sewardj5155dec2005-10-12 10:09:23 +0000756 regparms = 3;
757 i++;
758 break;
sewardj8badbaa2007-05-08 09:20:25 +0000759 case Ev_Bc:
760 /* Conditional branch */
761 helperName = "log_cond_branch";
762 helperAddr = &log_cond_branch;
763 argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bc.taken );
764 regparms = 2;
765 i++;
766 break;
767 case Ev_Bi:
768 /* Branch to an unknown destination */
769 helperName = "log_ind_branch";
770 helperAddr = &log_ind_branch;
771 argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bi.dst );
772 regparms = 2;
773 i++;
774 break;
sewardj5155dec2005-10-12 10:09:23 +0000775 default:
776 tl_assert(0);
777 }
778
779 /* Add the helper. */
780 tl_assert(helperName);
781 tl_assert(helperAddr);
782 tl_assert(argv);
sewardj5bb86822005-12-23 12:47:42 +0000783 di = unsafeIRDirty_0_N( regparms,
784 helperName, VG_(fnptr_to_fnentry)( helperAddr ),
785 argv );
sewardj0b9d74a2006-12-24 02:24:11 +0000786 addStmtToIRSB( cgs->sbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000787 }
788
sewardj5155dec2005-10-12 10:09:23 +0000789 cgs->events_used = 0;
njn4f9c9342002-04-29 16:03:24 +0000790}
njn14d01ce2004-11-26 11:30:14 +0000791
njnfd9f6222005-10-16 00:17:37 +0000792static void addEvent_Ir ( CgState* cgs, InstrInfo* inode )
sewardj5155dec2005-10-12 10:09:23 +0000793{
794 Event* evt;
sewardj5155dec2005-10-12 10:09:23 +0000795 if (cgs->events_used == N_EVENTS)
796 flushEvents(cgs);
797 tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS);
798 evt = &cgs->events[cgs->events_used];
sewardj8badbaa2007-05-08 09:20:25 +0000799 init_Event(evt);
800 evt->tag = Ev_Ir;
njnfd9f6222005-10-16 00:17:37 +0000801 evt->inode = inode;
sewardj5155dec2005-10-12 10:09:23 +0000802 cgs->events_used++;
803}
804
njnfd9f6222005-10-16 00:17:37 +0000805static
806void addEvent_Dr ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea )
sewardj5155dec2005-10-12 10:09:23 +0000807{
njnfd9f6222005-10-16 00:17:37 +0000808 Event* evt;
sewardj5155dec2005-10-12 10:09:23 +0000809 tl_assert(isIRAtom(ea));
njnfd9f6222005-10-16 00:17:37 +0000810 tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE);
sewardj8badbaa2007-05-08 09:20:25 +0000811 if (!clo_cache_sim)
812 return;
njnfd9f6222005-10-16 00:17:37 +0000813 if (cgs->events_used == N_EVENTS)
814 flushEvents(cgs);
815 tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS);
816 evt = &cgs->events[cgs->events_used];
sewardj8badbaa2007-05-08 09:20:25 +0000817 init_Event(evt);
818 evt->tag = Ev_Dr;
819 evt->inode = inode;
820 evt->Ev.Dr.szB = datasize;
821 evt->Ev.Dr.ea = ea;
njnfd9f6222005-10-16 00:17:37 +0000822 cgs->events_used++;
823}
sewardj5155dec2005-10-12 10:09:23 +0000824
njnfd9f6222005-10-16 00:17:37 +0000825static
826void addEvent_Dw ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea )
827{
828 Event* lastEvt;
829 Event* evt;
830
831 tl_assert(isIRAtom(ea));
832 tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE);
833
sewardj8badbaa2007-05-08 09:20:25 +0000834 if (!clo_cache_sim)
835 return;
836
njnfd9f6222005-10-16 00:17:37 +0000837 /* Is it possible to merge this write with the preceding read? */
838 lastEvt = &cgs->events[cgs->events_used-1];
sewardj5155dec2005-10-12 10:09:23 +0000839 if (cgs->events_used > 0
sewardj8badbaa2007-05-08 09:20:25 +0000840 && lastEvt->tag == Ev_Dr
841 && lastEvt->Ev.Dr.szB == datasize
842 && lastEvt->inode == inode
843 && eqIRAtom(lastEvt->Ev.Dr.ea, ea))
njnfd9f6222005-10-16 00:17:37 +0000844 {
sewardj8badbaa2007-05-08 09:20:25 +0000845 lastEvt->tag = Ev_Dm;
sewardj5155dec2005-10-12 10:09:23 +0000846 return;
847 }
848
849 /* No. Add as normal. */
850 if (cgs->events_used == N_EVENTS)
851 flushEvents(cgs);
852 tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS);
njnfd9f6222005-10-16 00:17:37 +0000853 evt = &cgs->events[cgs->events_used];
sewardj8badbaa2007-05-08 09:20:25 +0000854 init_Event(evt);
855 evt->tag = Ev_Dw;
856 evt->inode = inode;
857 evt->Ev.Dw.szB = datasize;
858 evt->Ev.Dw.ea = ea;
859 cgs->events_used++;
860}
861
862static
863void addEvent_Bc ( CgState* cgs, InstrInfo* inode, IRAtom* guard )
864{
865 Event* evt;
866 tl_assert(isIRAtom(guard));
867 tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, guard)
868 == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64));
869 if (!clo_branch_sim)
870 return;
871 if (cgs->events_used == N_EVENTS)
872 flushEvents(cgs);
873 tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS);
874 evt = &cgs->events[cgs->events_used];
875 init_Event(evt);
876 evt->tag = Ev_Bc;
877 evt->inode = inode;
878 evt->Ev.Bc.taken = guard;
879 cgs->events_used++;
880}
881
882static
883void addEvent_Bi ( CgState* cgs, InstrInfo* inode, IRAtom* whereTo )
884{
885 Event* evt;
886 tl_assert(isIRAtom(whereTo));
887 tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, whereTo)
888 == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64));
889 if (!clo_branch_sim)
890 return;
891 if (cgs->events_used == N_EVENTS)
892 flushEvents(cgs);
893 tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS);
894 evt = &cgs->events[cgs->events_used];
895 init_Event(evt);
896 evt->tag = Ev_Bi;
897 evt->inode = inode;
898 evt->Ev.Bi.dst = whereTo;
sewardj5155dec2005-10-12 10:09:23 +0000899 cgs->events_used++;
900}
901
902////////////////////////////////////////////////////////////
903
904
sewardj4ba057c2005-10-18 12:04:18 +0000905static
sewardj0b9d74a2006-12-24 02:24:11 +0000906IRSB* cg_instrument ( VgCallbackClosure* closure,
907 IRSB* sbIn,
sewardj461df9c2006-01-17 02:06:39 +0000908 VexGuestLayout* layout,
909 VexGuestExtents* vge,
sewardj4ba057c2005-10-18 12:04:18 +0000910 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000911{
njnfd9f6222005-10-16 00:17:37 +0000912 Int i, isize;
sewardj5155dec2005-10-12 10:09:23 +0000913 IRStmt* st;
914 Addr64 cia; /* address of current insn */
915 CgState cgs;
sewardj0b9d74a2006-12-24 02:24:11 +0000916 IRTypeEnv* tyenv = sbIn->tyenv;
njnfd9f6222005-10-16 00:17:37 +0000917 InstrInfo* curr_inode = NULL;
sewardj5155dec2005-10-12 10:09:23 +0000918
sewardjd54babf2005-03-21 00:55:49 +0000919 if (gWordTy != hWordTy) {
920 /* We don't currently support this case. */
921 VG_(tool_panic)("host/guest word size mismatch");
922 }
923
sewardj0b9d74a2006-12-24 02:24:11 +0000924 // Set up new SB
925 cgs.sbOut = deepCopyIRSBExceptStmts(sbIn);
njn6a3009b2005-03-20 00:20:06 +0000926
sewardja9f538c2005-10-23 12:06:55 +0000927 // Copy verbatim any IR preamble preceding the first IMark
njn6a3009b2005-03-20 00:20:06 +0000928 i = 0;
sewardj0b9d74a2006-12-24 02:24:11 +0000929 while (i < sbIn->stmts_used && sbIn->stmts[i]->tag != Ist_IMark) {
930 addStmtToIRSB( cgs.sbOut, sbIn->stmts[i] );
sewardja9f538c2005-10-23 12:06:55 +0000931 i++;
932 }
933
934 // Get the first statement, and initial cia from it
sewardj0b9d74a2006-12-24 02:24:11 +0000935 tl_assert(sbIn->stmts_used > 0);
936 tl_assert(i < sbIn->stmts_used);
937 st = sbIn->stmts[i];
njn6a3009b2005-03-20 00:20:06 +0000938 tl_assert(Ist_IMark == st->tag);
sewardj8badbaa2007-05-08 09:20:25 +0000939
940 cia = st->Ist.IMark.addr;
941 isize = st->Ist.IMark.len;
942 // If Vex fails to decode an instruction, the size will be zero.
943 // Pretend otherwise.
944 if (isize == 0) isize = VG_MIN_INSTR_SZB;
njn6a3009b2005-03-20 00:20:06 +0000945
sewardj5155dec2005-10-12 10:09:23 +0000946 // Set up running state and get block info
sewardj3a384b32006-01-22 01:12:51 +0000947 tl_assert(closure->readdr == vge->base[0]);
sewardj5155dec2005-10-12 10:09:23 +0000948 cgs.events_used = 0;
sewardj0b9d74a2006-12-24 02:24:11 +0000949 cgs.sbInfo = get_SB_info(sbIn, (Addr)closure->readdr);
950 cgs.sbInfo_i = 0;
njn6a3009b2005-03-20 00:20:06 +0000951
sewardj5155dec2005-10-12 10:09:23 +0000952 if (DEBUG_CG)
953 VG_(printf)("\n\n---------- cg_instrument ----------\n");
njn6a3009b2005-03-20 00:20:06 +0000954
njnfd9f6222005-10-16 00:17:37 +0000955 // Traverse the block, initialising inodes, adding events and flushing as
956 // necessary.
sewardj0b9d74a2006-12-24 02:24:11 +0000957 for (/*use current i*/; i < sbIn->stmts_used; i++) {
njn6a3009b2005-03-20 00:20:06 +0000958
sewardj0b9d74a2006-12-24 02:24:11 +0000959 st = sbIn->stmts[i];
sewardj5155dec2005-10-12 10:09:23 +0000960 tl_assert(isFlatIRStmt(st));
njnb3507ea2005-08-02 23:07:02 +0000961
sewardj5155dec2005-10-12 10:09:23 +0000962 switch (st->tag) {
963 case Ist_NoOp:
964 case Ist_AbiHint:
965 case Ist_Put:
966 case Ist_PutI:
sewardj72d75132007-11-09 23:06:35 +0000967 case Ist_MBE:
sewardj5155dec2005-10-12 10:09:23 +0000968 break;
njn20677cc2005-08-12 23:47:51 +0000969
sewardj5155dec2005-10-12 10:09:23 +0000970 case Ist_IMark:
njnfd9f6222005-10-16 00:17:37 +0000971 cia = st->Ist.IMark.addr;
972 isize = st->Ist.IMark.len;
973
974 // If Vex fails to decode an instruction, the size will be zero.
975 // Pretend otherwise.
976 if (isize == 0) isize = VG_MIN_INSTR_SZB;
977
njna5ad9ba2005-11-10 15:20:37 +0000978 // Sanity-check size.
979 tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB)
980 || VG_CLREQ_SZB == isize );
njnfd9f6222005-10-16 00:17:37 +0000981
982 // Get space for and init the inode, record it as the current one.
983 // Subsequent Dr/Dw/Dm events from the same instruction will
984 // also use it.
985 curr_inode = setup_InstrInfo(&cgs, cia, isize);
986
987 addEvent_Ir( &cgs, curr_inode );
sewardj5155dec2005-10-12 10:09:23 +0000988 break;
989
sewardj0b9d74a2006-12-24 02:24:11 +0000990 case Ist_WrTmp: {
991 IRExpr* data = st->Ist.WrTmp.data;
sewardj5155dec2005-10-12 10:09:23 +0000992 if (data->tag == Iex_Load) {
993 IRExpr* aexpr = data->Iex.Load.addr;
sewardj5155dec2005-10-12 10:09:23 +0000994 // Note also, endianness info is ignored. I guess
995 // that's not interesting.
njnfd9f6222005-10-16 00:17:37 +0000996 addEvent_Dr( &cgs, curr_inode, sizeofIRType(data->Iex.Load.ty),
997 aexpr );
sewardj5155dec2005-10-12 10:09:23 +0000998 }
999 break;
njnb3507ea2005-08-02 23:07:02 +00001000 }
1001
sewardj5155dec2005-10-12 10:09:23 +00001002 case Ist_Store: {
1003 IRExpr* data = st->Ist.Store.data;
1004 IRExpr* aexpr = st->Ist.Store.addr;
njnfd9f6222005-10-16 00:17:37 +00001005 addEvent_Dw( &cgs, curr_inode,
1006 sizeofIRType(typeOfIRExpr(tyenv, data)), aexpr );
sewardj5155dec2005-10-12 10:09:23 +00001007 break;
1008 }
njnb3507ea2005-08-02 23:07:02 +00001009
sewardj5155dec2005-10-12 10:09:23 +00001010 case Ist_Dirty: {
1011 Int dataSize;
1012 IRDirty* d = st->Ist.Dirty.details;
1013 if (d->mFx != Ifx_None) {
njnfd9f6222005-10-16 00:17:37 +00001014 /* This dirty helper accesses memory. Collect the details. */
sewardj5155dec2005-10-12 10:09:23 +00001015 tl_assert(d->mAddr != NULL);
1016 tl_assert(d->mSize != 0);
1017 dataSize = d->mSize;
1018 // Large (eg. 28B, 108B, 512B on x86) data-sized
1019 // instructions will be done inaccurately, but they're
1020 // very rare and this avoids errors from hitting more
1021 // than two cache lines in the simulation.
1022 if (dataSize > MIN_LINE_SIZE)
1023 dataSize = MIN_LINE_SIZE;
1024 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
njnfd9f6222005-10-16 00:17:37 +00001025 addEvent_Dr( &cgs, curr_inode, dataSize, d->mAddr );
sewardj5155dec2005-10-12 10:09:23 +00001026 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
njnfd9f6222005-10-16 00:17:37 +00001027 addEvent_Dw( &cgs, curr_inode, dataSize, d->mAddr );
sewardj5155dec2005-10-12 10:09:23 +00001028 } else {
1029 tl_assert(d->mAddr == NULL);
1030 tl_assert(d->mSize == 0);
1031 }
1032 break;
1033 }
njn6a3009b2005-03-20 00:20:06 +00001034
sewardj8badbaa2007-05-08 09:20:25 +00001035 case Ist_Exit: {
1036 /* Stuff to widen the guard expression to a host word, so
1037 we can pass it to the branch predictor simulation
1038 functions easily. */
1039 Bool inverted;
1040 Addr64 nia, sea;
1041 IRConst* dst;
1042 IROp tyW = hWordTy;
1043 IROp widen = tyW==Ity_I32 ? Iop_1Uto32 : Iop_1Uto64;
1044 IROp opXOR = tyW==Ity_I32 ? Iop_Xor32 : Iop_Xor64;
1045 IRTemp guard1 = newIRTemp(cgs.sbOut->tyenv, Ity_I1);
1046 IRTemp guardW = newIRTemp(cgs.sbOut->tyenv, tyW);
1047 IRTemp guard = newIRTemp(cgs.sbOut->tyenv, tyW);
1048 IRExpr* one = tyW==Ity_I32 ? IRExpr_Const(IRConst_U32(1))
1049 : IRExpr_Const(IRConst_U64(1));
1050
1051 /* First we need to figure out whether the side exit got
1052 inverted by the ir optimiser. To do that, figure out
1053 the next (fallthrough) instruction's address and the
1054 side exit address and see if they are the same. */
1055 nia = cia + (Addr64)isize;
1056 if (tyW == Ity_I32)
1057 nia &= 0xFFFFFFFFULL;
1058
1059 /* Side exit address */
1060 dst = st->Ist.Exit.dst;
1061 if (tyW == Ity_I32) {
1062 tl_assert(dst->tag == Ico_U32);
1063 sea = (Addr64)(UInt)dst->Ico.U32;
1064 } else {
1065 tl_assert(tyW == Ity_I64);
1066 tl_assert(dst->tag == Ico_U64);
1067 sea = dst->Ico.U64;
1068 }
1069
1070 inverted = nia == sea;
1071
1072 /* Widen the guard expression. */
1073 addStmtToIRSB( cgs.sbOut,
1074 IRStmt_WrTmp( guard1, st->Ist.Exit.guard ));
1075 addStmtToIRSB( cgs.sbOut,
1076 IRStmt_WrTmp( guardW,
1077 IRExpr_Unop(widen,
1078 IRExpr_RdTmp(guard1))) );
1079 /* If the exit is inverted, invert the sense of the guard. */
1080 addStmtToIRSB(
1081 cgs.sbOut,
1082 IRStmt_WrTmp(
1083 guard,
1084 inverted ? IRExpr_Binop(opXOR, IRExpr_RdTmp(guardW), one)
1085 : IRExpr_RdTmp(guardW)
1086 ));
1087 /* And post the event. */
1088 addEvent_Bc( &cgs, curr_inode, IRExpr_RdTmp(guard) );
1089
sewardj5155dec2005-10-12 10:09:23 +00001090 /* We may never reach the next statement, so need to flush
1091 all outstanding transactions now. */
1092 flushEvents( &cgs );
1093 break;
sewardj8badbaa2007-05-08 09:20:25 +00001094 }
sewardj5155dec2005-10-12 10:09:23 +00001095
1096 default:
1097 tl_assert(0);
1098 break;
njnb3507ea2005-08-02 23:07:02 +00001099 }
njn6a3009b2005-03-20 00:20:06 +00001100
sewardj5155dec2005-10-12 10:09:23 +00001101 /* Copy the original statement */
sewardj0b9d74a2006-12-24 02:24:11 +00001102 addStmtToIRSB( cgs.sbOut, st );
njn6a3009b2005-03-20 00:20:06 +00001103
sewardj5155dec2005-10-12 10:09:23 +00001104 if (DEBUG_CG) {
1105 ppIRStmt(st);
1106 VG_(printf)("\n");
1107 }
1108 }
1109
sewardj8badbaa2007-05-08 09:20:25 +00001110 /* Deal with branches to unknown destinations. Except ignore ones
1111 which are function returns as we assume the return stack
1112 predictor never mispredicts. */
1113 if (sbIn->jumpkind == Ijk_Boring) {
1114 if (0) { ppIRExpr( sbIn->next ); VG_(printf)("\n"); }
1115 switch (sbIn->next->tag) {
1116 case Iex_Const:
1117 break; /* boring - branch to known address */
1118 case Iex_RdTmp:
1119 /* looks like an indirect branch (branch to unknown) */
1120 addEvent_Bi( &cgs, curr_inode, sbIn->next );
1121 break;
1122 default:
1123 /* shouldn't happen - if the incoming IR is properly
1124 flattened, should only have tmp and const cases to
1125 consider. */
1126 tl_assert(0);
1127 }
1128 }
1129
sewardj5155dec2005-10-12 10:09:23 +00001130 /* At the end of the bb. Flush outstandings. */
sewardj5155dec2005-10-12 10:09:23 +00001131 flushEvents( &cgs );
1132
sewardj5155dec2005-10-12 10:09:23 +00001133 /* done. stay sane ... */
sewardj0b9d74a2006-12-24 02:24:11 +00001134 tl_assert(cgs.sbInfo_i == cgs.sbInfo->n_instrs);
sewardj5155dec2005-10-12 10:09:23 +00001135
1136 if (DEBUG_CG) {
1137 VG_(printf)( "goto {");
sewardj0b9d74a2006-12-24 02:24:11 +00001138 ppIRJumpKind(sbIn->jumpkind);
sewardj5155dec2005-10-12 10:09:23 +00001139 VG_(printf)( "} ");
sewardj0b9d74a2006-12-24 02:24:11 +00001140 ppIRExpr( sbIn->next );
sewardj5155dec2005-10-12 10:09:23 +00001141 VG_(printf)( "}\n");
1142 }
1143
sewardj0b9d74a2006-12-24 02:24:11 +00001144 return cgs.sbOut;
njn14d01ce2004-11-26 11:30:14 +00001145}
njn4f9c9342002-04-29 16:03:24 +00001146
1147/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +00001148/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +00001149/*------------------------------------------------------------*/
1150
sewardjb5f6f512005-03-10 23:59:00 +00001151#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +00001152
1153static cache_t clo_I1_cache = UNDEFINED_CACHE;
1154static cache_t clo_D1_cache = UNDEFINED_CACHE;
1155static cache_t clo_L2_cache = UNDEFINED_CACHE;
1156
njn7cf0bd32002-06-08 13:36:03 +00001157/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +00001158static
njna1d1a642004-11-26 18:36:02 +00001159void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +00001160{
weidendo144b76c2009-01-26 22:56:14 +00001161 /* Simulator requires line size and set count to be powers of two */
1162 if (( cache->size % (cache->line_size * cache->assoc) != 0) ||
1163 (-1 == VG_(log2)(cache->size/cache->line_size/cache->assoc))) {
njn6f74a7e2009-03-12 00:06:45 +00001164 VG_UMSG("error: %s set count not a power of two; aborting.", name);
njna1d1a642004-11-26 18:36:02 +00001165 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +00001166 }
1167
sewardj07133bf2002-06-13 10:25:56 +00001168 if (-1 == VG_(log2)(cache->line_size)) {
njn6f74a7e2009-03-12 00:06:45 +00001169 VG_UMSG("error: %s line size of %dB not a power of two; aborting.",
1170 name, cache->line_size);
njna1d1a642004-11-26 18:36:02 +00001171 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +00001172 }
1173
njn6a3009b2005-03-20 00:20:06 +00001174 // Then check line size >= 16 -- any smaller and a single instruction could
1175 // straddle three cache lines, which breaks a simulation assertion and is
1176 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +00001177 if (cache->line_size < MIN_LINE_SIZE) {
njn6f74a7e2009-03-12 00:06:45 +00001178 VG_UMSG("error: %s line size of %dB too small; aborting.",
1179 name, cache->line_size);
njna1d1a642004-11-26 18:36:02 +00001180 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +00001181 }
1182
1183 /* Then check cache size > line size (causes seg faults if not). */
1184 if (cache->size <= cache->line_size) {
njn6f74a7e2009-03-12 00:06:45 +00001185 VG_UMSG("error: %s cache size of %dB <= line size of %dB; aborting.",
1186 name, cache->size, cache->line_size);
njna1d1a642004-11-26 18:36:02 +00001187 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +00001188 }
1189
1190 /* Then check assoc <= (size / line size) (seg faults otherwise). */
1191 if (cache->assoc > (cache->size / cache->line_size)) {
njn6f74a7e2009-03-12 00:06:45 +00001192 VG_UMSG("warning: %s associativity > (size / line size); aborting.",
1193 name);
njna1d1a642004-11-26 18:36:02 +00001194 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +00001195 }
1196}
1197
sewardj07133bf2002-06-13 10:25:56 +00001198static
nethercoteb35a8b92004-09-11 16:45:27 +00001199void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +00001200{
nethercote9313ac42004-07-06 21:54:20 +00001201#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
1202
nethercoteb35a8b92004-09-11 16:45:27 +00001203 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +00001204
nethercoteb35a8b92004-09-11 16:45:27 +00001205 // Count how many were defined on the command line.
1206 if (DEFINED(clo_I1_cache)) { n_clos++; }
1207 if (DEFINED(clo_D1_cache)) { n_clos++; }
1208 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +00001209
njna1d1a642004-11-26 18:36:02 +00001210 // Set the cache config (using auto-detection, if supported by the
1211 // architecture)
njnaf839f52005-06-23 03:27:57 +00001212 VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +00001213
nethercote9313ac42004-07-06 21:54:20 +00001214 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +00001215 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
1216 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
1217 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +00001218
nethercote9313ac42004-07-06 21:54:20 +00001219 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +00001220 check_cache(I1c, "I1");
1221 check_cache(D1c, "D1");
1222 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +00001223
njn6f74a7e2009-03-12 00:06:45 +00001224 if (VG_(clo_verbosity) >= 2) {
1225 VG_UMSG("Cache configuration used:");
1226 VG_UMSG(" I1: %dB, %d-way, %dB lines",
1227 I1c->size, I1c->assoc, I1c->line_size);
1228 VG_UMSG(" D1: %dB, %d-way, %dB lines",
1229 D1c->size, D1c->assoc, D1c->line_size);
1230 VG_UMSG(" L2: %dB, %d-way, %dB lines",
1231 L2c->size, L2c->assoc, L2c->line_size);
njn7cf0bd32002-06-08 13:36:03 +00001232 }
nethercote9313ac42004-07-06 21:54:20 +00001233#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +00001234}
1235
njn4f9c9342002-04-29 16:03:24 +00001236/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +00001237/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +00001238/*------------------------------------------------------------*/
1239
nethercote9313ac42004-07-06 21:54:20 +00001240// Total reads/writes/misses. Calculated during CC traversal at the end.
1241// All auto-zeroed.
sewardj8badbaa2007-05-08 09:20:25 +00001242static CacheCC Ir_total;
1243static CacheCC Dr_total;
1244static CacheCC Dw_total;
1245static BranchCC Bc_total;
1246static BranchCC Bi_total;
nethercote9313ac42004-07-06 21:54:20 +00001247
nethercote9313ac42004-07-06 21:54:20 +00001248static void fprint_CC_table_and_calc_totals(void)
1249{
njnd3bef4f2005-10-15 17:46:18 +00001250 Int i, fd;
sewardj92645592005-07-23 09:18:34 +00001251 SysRes sres;
njnd3bef4f2005-10-15 17:46:18 +00001252 Char buf[512], *currFile = NULL, *currFn = NULL;
1253 LineCC* lineCC;
njn4f9c9342002-04-29 16:03:24 +00001254
njn7064fb22008-05-29 23:09:52 +00001255 // Setup output filename. Nb: it's important to do this now, ie. as late
1256 // as possible. If we do it at start-up and the program forks and the
1257 // output file format string contains a %p (pid) specifier, both the
1258 // parent and child will incorrectly write to the same file; this
1259 // happened in 3.3.0.
1260 Char* cachegrind_out_file =
1261 VG_(expand_file_name)("--cachegrind-out-file", clo_cachegrind_out_file);
1262
sewardj92645592005-07-23 09:18:34 +00001263 sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
1264 VKI_S_IRUSR|VKI_S_IWUSR);
njncda2f0f2009-05-18 02:12:08 +00001265 if (sr_isError(sres)) {
nethercote9313ac42004-07-06 21:54:20 +00001266 // If the file can't be opened for whatever reason (conflict
1267 // between multiple cachegrinded processes?), give up now.
njn6f74a7e2009-03-12 00:06:45 +00001268 VG_UMSG("error: can't open cache simulation output file '%s'",
1269 cachegrind_out_file );
1270 VG_UMSG(" ... so simulation results will be missing.");
njn7064fb22008-05-29 23:09:52 +00001271 VG_(free)(cachegrind_out_file);
sewardj0744b6c2002-12-11 00:45:42 +00001272 return;
sewardj92645592005-07-23 09:18:34 +00001273 } else {
njncda2f0f2009-05-18 02:12:08 +00001274 fd = sr_Res(sres);
njn7064fb22008-05-29 23:09:52 +00001275 VG_(free)(cachegrind_out_file);
sewardj0744b6c2002-12-11 00:45:42 +00001276 }
njn4f9c9342002-04-29 16:03:24 +00001277
nethercote9313ac42004-07-06 21:54:20 +00001278 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
1279 // the 2nd colon makes cg_annotate's output look nicer.
1280 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
1281 "desc: D1 cache: %s\n"
1282 "desc: L2 cache: %s\n",
1283 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +00001284 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +00001285
nethercote9313ac42004-07-06 21:54:20 +00001286 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +00001287 VG_(strcpy)(buf, "cmd:");
1288 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
sewardj45f4e7c2005-09-27 19:20:21 +00001289 if (VG_(args_the_exename)) {
1290 VG_(write)(fd, " ", 1);
1291 VG_(write)(fd, VG_(args_the_exename),
1292 VG_(strlen)( VG_(args_the_exename) ));
1293 }
sewardj14c7cc52007-02-25 15:08:24 +00001294 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
1295 HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
1296 if (arg) {
sewardj45f4e7c2005-09-27 19:20:21 +00001297 VG_(write)(fd, " ", 1);
sewardj14c7cc52007-02-25 15:08:24 +00001298 VG_(write)(fd, arg, VG_(strlen)( arg ));
sewardj45f4e7c2005-09-27 19:20:21 +00001299 }
njn4f9c9342002-04-29 16:03:24 +00001300 }
nethercote9313ac42004-07-06 21:54:20 +00001301 // "events:" line
sewardj8badbaa2007-05-08 09:20:25 +00001302 if (clo_cache_sim && clo_branch_sim) {
1303 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw "
1304 "Bc Bcm Bi Bim\n");
1305 }
1306 else if (clo_cache_sim && !clo_branch_sim) {
1307 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw "
1308 "\n");
1309 }
1310 else if (!clo_cache_sim && clo_branch_sim) {
1311 VG_(sprintf)(buf, "\nevents: Ir "
1312 "Bc Bcm Bi Bim\n");
1313 }
1314 else
1315 tl_assert(0); /* can't happen */
1316
njn4f9c9342002-04-29 16:03:24 +00001317 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
1318
njnd3bef4f2005-10-15 17:46:18 +00001319 // Traverse every lineCC
njne2a9ad32007-09-17 05:30:48 +00001320 VG_(OSetGen_ResetIter)(CC_table);
1321 while ( (lineCC = VG_(OSetGen_Next)(CC_table)) ) {
njn4311fe62005-12-08 23:18:50 +00001322 Bool just_hit_a_new_file = False;
njnd3bef4f2005-10-15 17:46:18 +00001323 // If we've hit a new file, print a "fl=" line. Note that because
1324 // each string is stored exactly once in the string table, we can use
1325 // pointer comparison rather than strcmp() to test for equality, which
1326 // is good because most of the time the comparisons are equal and so
njn4311fe62005-12-08 23:18:50 +00001327 // the whole strings would have to be checked.
njnd3bef4f2005-10-15 17:46:18 +00001328 if ( lineCC->loc.file != currFile ) {
1329 currFile = lineCC->loc.file;
1330 VG_(sprintf)(buf, "fl=%s\n", currFile);
njn4f9c9342002-04-29 16:03:24 +00001331 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njnd3bef4f2005-10-15 17:46:18 +00001332 distinct_files++;
njn4311fe62005-12-08 23:18:50 +00001333 just_hit_a_new_file = True;
njn4f9c9342002-04-29 16:03:24 +00001334 }
njn4311fe62005-12-08 23:18:50 +00001335 // If we've hit a new function, print a "fn=" line. We know to do
1336 // this when the function name changes, and also every time we hit a
1337 // new file (in which case the new function name might be the same as
1338 // in the old file, hence the just_hit_a_new_file test).
1339 if ( just_hit_a_new_file || lineCC->loc.fn != currFn ) {
njnd3bef4f2005-10-15 17:46:18 +00001340 currFn = lineCC->loc.fn;
1341 VG_(sprintf)(buf, "fn=%s\n", currFn);
1342 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
1343 distinct_fns++;
1344 }
1345
1346 // Print the LineCC
sewardj8badbaa2007-05-08 09:20:25 +00001347 if (clo_cache_sim && clo_branch_sim) {
1348 VG_(sprintf)(buf, "%u %llu %llu %llu"
1349 " %llu %llu %llu"
1350 " %llu %llu %llu"
1351 " %llu %llu %llu %llu\n",
1352 lineCC->loc.line,
1353 lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.m2,
1354 lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.m2,
1355 lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.m2,
1356 lineCC->Bc.b, lineCC->Bc.mp,
1357 lineCC->Bi.b, lineCC->Bi.mp);
1358 }
1359 else if (clo_cache_sim && !clo_branch_sim) {
1360 VG_(sprintf)(buf, "%u %llu %llu %llu"
1361 " %llu %llu %llu"
1362 " %llu %llu %llu\n",
1363 lineCC->loc.line,
1364 lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.m2,
1365 lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.m2,
1366 lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.m2);
1367 }
1368 else if (!clo_cache_sim && clo_branch_sim) {
1369 VG_(sprintf)(buf, "%u %llu"
1370 " %llu %llu %llu %llu\n",
1371 lineCC->loc.line,
1372 lineCC->Ir.a,
1373 lineCC->Bc.b, lineCC->Bc.mp,
1374 lineCC->Bi.b, lineCC->Bi.mp);
1375 }
1376 else
1377 tl_assert(0);
1378
njnd3bef4f2005-10-15 17:46:18 +00001379 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
1380
1381 // Update summary stats
1382 Ir_total.a += lineCC->Ir.a;
1383 Ir_total.m1 += lineCC->Ir.m1;
1384 Ir_total.m2 += lineCC->Ir.m2;
1385 Dr_total.a += lineCC->Dr.a;
1386 Dr_total.m1 += lineCC->Dr.m1;
1387 Dr_total.m2 += lineCC->Dr.m2;
1388 Dw_total.a += lineCC->Dw.a;
1389 Dw_total.m1 += lineCC->Dw.m1;
1390 Dw_total.m2 += lineCC->Dw.m2;
sewardj8badbaa2007-05-08 09:20:25 +00001391 Bc_total.b += lineCC->Bc.b;
1392 Bc_total.mp += lineCC->Bc.mp;
1393 Bi_total.b += lineCC->Bi.b;
1394 Bi_total.mp += lineCC->Bi.mp;
njnd3bef4f2005-10-15 17:46:18 +00001395
1396 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +00001397 }
1398
nethercote9313ac42004-07-06 21:54:20 +00001399 // Summary stats must come after rest of table, since we calculate them
sewardj8badbaa2007-05-08 09:20:25 +00001400 // during traversal. */
1401 if (clo_cache_sim && clo_branch_sim) {
1402 VG_(sprintf)(buf, "summary:"
1403 " %llu %llu %llu"
1404 " %llu %llu %llu"
1405 " %llu %llu %llu"
1406 " %llu %llu %llu %llu\n",
1407 Ir_total.a, Ir_total.m1, Ir_total.m2,
1408 Dr_total.a, Dr_total.m1, Dr_total.m2,
1409 Dw_total.a, Dw_total.m1, Dw_total.m2,
1410 Bc_total.b, Bc_total.mp,
1411 Bi_total.b, Bi_total.mp);
1412 }
1413 else if (clo_cache_sim && !clo_branch_sim) {
1414 VG_(sprintf)(buf, "summary:"
1415 " %llu %llu %llu"
1416 " %llu %llu %llu"
1417 " %llu %llu %llu\n",
1418 Ir_total.a, Ir_total.m1, Ir_total.m2,
1419 Dr_total.a, Dr_total.m1, Dr_total.m2,
1420 Dw_total.a, Dw_total.m1, Dw_total.m2);
1421 }
1422 else if (!clo_cache_sim && clo_branch_sim) {
1423 VG_(sprintf)(buf, "summary:"
1424 " %llu"
1425 " %llu %llu %llu %llu\n",
1426 Ir_total.a,
1427 Bc_total.b, Bc_total.mp,
1428 Bi_total.b, Bi_total.mp);
1429 }
1430 else
1431 tl_assert(0);
1432
njn4f9c9342002-04-29 16:03:24 +00001433 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
1434 VG_(close)(fd);
1435}
1436
njn607adfc2003-09-30 14:15:44 +00001437static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +00001438{
njn607adfc2003-09-30 14:15:44 +00001439 UInt w = 0;
1440 while (n > 0) {
1441 n = n / 10;
1442 w++;
njn4f9c9342002-04-29 16:03:24 +00001443 }
sewardj46c59b12005-11-01 02:20:19 +00001444 if (w == 0) w = 1;
njn607adfc2003-09-30 14:15:44 +00001445 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +00001446}
1447
njn51d827b2005-05-09 01:02:08 +00001448static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +00001449{
njn1baf7db2006-04-18 22:34:48 +00001450 static Char buf1[128], buf2[128], buf3[128], buf4[123], fmt[128];
njn607adfc2003-09-30 14:15:44 +00001451
sewardj8badbaa2007-05-08 09:20:25 +00001452 CacheCC D_total;
1453 BranchCC B_total;
njn1d021fa2002-05-02 13:56:34 +00001454 ULong L2_total_m, L2_total_mr, L2_total_mw,
1455 L2_total, L2_total_r, L2_total_w;
njn4c245e52009-03-15 23:25:38 +00001456 Int l1, l2, l3;
njn4f9c9342002-04-29 16:03:24 +00001457
sewardj8badbaa2007-05-08 09:20:25 +00001458 /* Running with both cache and branch simulation disabled is not
1459 allowed (checked during command line option processing). */
1460 tl_assert(clo_cache_sim || clo_branch_sim);
1461
nethercote9313ac42004-07-06 21:54:20 +00001462 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +00001463
njn7cf0bd32002-06-08 13:36:03 +00001464 if (VG_(clo_verbosity) == 0)
1465 return;
1466
njnf76d27a2009-05-28 01:53:07 +00001467 // Nb: this isn't called "MAX" because that overshadows a global on Darwin.
1468 #define CG_MAX(a, b) ((a) >= (b) ? (a) : (b))
njn4c245e52009-03-15 23:25:38 +00001469
njn4f9c9342002-04-29 16:03:24 +00001470 /* I cache results. Use the I_refs value to determine the first column
1471 * width. */
njn607adfc2003-09-30 14:15:44 +00001472 l1 = ULong_width(Ir_total.a);
njnf76d27a2009-05-28 01:53:07 +00001473 l2 = ULong_width(CG_MAX(Dr_total.a, Bc_total.b));
1474 l3 = ULong_width(CG_MAX(Dw_total.a, Bi_total.b));
njn4f9c9342002-04-29 16:03:24 +00001475
njn607adfc2003-09-30 14:15:44 +00001476 /* Make format string, getting width right for numbers */
njn99cb9e32005-09-25 17:59:16 +00001477 VG_(sprintf)(fmt, "%%s %%,%dllu", l1);
njnd3bef4f2005-10-15 17:46:18 +00001478
sewardj8badbaa2007-05-08 09:20:25 +00001479 /* Always print this */
njn6f74a7e2009-03-12 00:06:45 +00001480 VG_UMSG(fmt, "I refs: ", Ir_total.a);
njn4f9c9342002-04-29 16:03:24 +00001481
sewardj8badbaa2007-05-08 09:20:25 +00001482 /* If cache profiling is enabled, show D access numbers and all
1483 miss numbers */
1484 if (clo_cache_sim) {
njn6f74a7e2009-03-12 00:06:45 +00001485 VG_UMSG(fmt, "I1 misses: ", Ir_total.m1);
1486 VG_UMSG(fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +00001487
sewardj8badbaa2007-05-08 09:20:25 +00001488 if (0 == Ir_total.a) Ir_total.a = 1;
1489 VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1);
njn6f74a7e2009-03-12 00:06:45 +00001490 VG_UMSG("I1 miss rate: %s", buf1);
njn4f9c9342002-04-29 16:03:24 +00001491
sewardj8badbaa2007-05-08 09:20:25 +00001492 VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1);
njn6f74a7e2009-03-12 00:06:45 +00001493 VG_UMSG("L2i miss rate: %s", buf1);
1494 VG_UMSG("");
njnd3bef4f2005-10-15 17:46:18 +00001495
sewardj8badbaa2007-05-08 09:20:25 +00001496 /* D cache results. Use the D_refs.rd and D_refs.wr values to
1497 * determine the width of columns 2 & 3. */
1498 D_total.a = Dr_total.a + Dw_total.a;
1499 D_total.m1 = Dr_total.m1 + Dw_total.m1;
1500 D_total.m2 = Dr_total.m2 + Dw_total.m2;
njn4f9c9342002-04-29 16:03:24 +00001501
sewardj8badbaa2007-05-08 09:20:25 +00001502 /* Make format string, getting width right for numbers */
1503 VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu rd + %%,%dllu wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +00001504
njn6f74a7e2009-03-12 00:06:45 +00001505 VG_UMSG(fmt, "D refs: ",
1506 D_total.a, Dr_total.a, Dw_total.a);
1507 VG_UMSG(fmt, "D1 misses: ",
1508 D_total.m1, Dr_total.m1, Dw_total.m1);
1509 VG_UMSG(fmt, "L2d misses: ",
1510 D_total.m2, Dr_total.m2, Dw_total.m2);
njnd3bef4f2005-10-15 17:46:18 +00001511
sewardj8badbaa2007-05-08 09:20:25 +00001512 if (0 == D_total.a) D_total.a = 1;
1513 if (0 == Dr_total.a) Dr_total.a = 1;
1514 if (0 == Dw_total.a) Dw_total.a = 1;
1515 VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1);
1516 VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2);
1517 VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3);
njn6f74a7e2009-03-12 00:06:45 +00001518 VG_UMSG("D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
njn4f9c9342002-04-29 16:03:24 +00001519
sewardj8badbaa2007-05-08 09:20:25 +00001520 VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1);
1521 VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2);
1522 VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3);
njn6f74a7e2009-03-12 00:06:45 +00001523 VG_UMSG("L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
1524 VG_UMSG("");
njn1d021fa2002-05-02 13:56:34 +00001525
sewardj8badbaa2007-05-08 09:20:25 +00001526 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +00001527
sewardj8badbaa2007-05-08 09:20:25 +00001528 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
1529 L2_total_r = Dr_total.m1 + Ir_total.m1;
1530 L2_total_w = Dw_total.m1;
njn6f74a7e2009-03-12 00:06:45 +00001531 VG_UMSG(fmt, "L2 refs: ",
1532 L2_total, L2_total_r, L2_total_w);
njn4f9c9342002-04-29 16:03:24 +00001533
sewardj8badbaa2007-05-08 09:20:25 +00001534 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
1535 L2_total_mr = Dr_total.m2 + Ir_total.m2;
1536 L2_total_mw = Dw_total.m2;
njn6f74a7e2009-03-12 00:06:45 +00001537 VG_UMSG(fmt, "L2 misses: ",
1538 L2_total_m, L2_total_mr, L2_total_mw);
njnd3bef4f2005-10-15 17:46:18 +00001539
sewardj8badbaa2007-05-08 09:20:25 +00001540 VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1);
1541 VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2);
1542 VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3);
njn6f74a7e2009-03-12 00:06:45 +00001543 VG_UMSG("L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
sewardj8badbaa2007-05-08 09:20:25 +00001544 }
1545
1546 /* If branch profiling is enabled, show branch overall results. */
1547 if (clo_branch_sim) {
1548 /* Make format string, getting width right for numbers */
1549 VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu cond + %%,%dllu ind)", l1, l2, l3);
1550
1551 if (0 == Bc_total.b) Bc_total.b = 1;
1552 if (0 == Bi_total.b) Bi_total.b = 1;
1553 B_total.b = Bc_total.b + Bi_total.b;
1554 B_total.mp = Bc_total.mp + Bi_total.mp;
1555
njn6f74a7e2009-03-12 00:06:45 +00001556 VG_UMSG("");
1557 VG_UMSG(fmt, "Branches: ",
1558 B_total.b, Bc_total.b, Bi_total.b);
sewardj8badbaa2007-05-08 09:20:25 +00001559
njn6f74a7e2009-03-12 00:06:45 +00001560 VG_UMSG(fmt, "Mispredicts: ",
1561 B_total.mp, Bc_total.mp, Bi_total.mp);
sewardj8badbaa2007-05-08 09:20:25 +00001562
1563 VG_(percentify)(B_total.mp, B_total.b, 1, l1+1, buf1);
1564 VG_(percentify)(Bc_total.mp, Bc_total.b, 1, l2+1, buf2);
1565 VG_(percentify)(Bi_total.mp, Bi_total.b, 1, l3+1, buf3);
1566
njn6f74a7e2009-03-12 00:06:45 +00001567 VG_UMSG("Mispred rate: %s (%s + %s )", buf1, buf2,buf3);
sewardj8badbaa2007-05-08 09:20:25 +00001568 }
njn4f9c9342002-04-29 16:03:24 +00001569
nethercote9313ac42004-07-06 21:54:20 +00001570 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001571 if (VG_(clo_verbosity) > 1) {
njn1baf7db2006-04-18 22:34:48 +00001572 Int debug_lookups = full_debugs + fn_debugs +
1573 file_line_debugs + no_debugs;
njnd3bef4f2005-10-15 17:46:18 +00001574
njn6f74a7e2009-03-12 00:06:45 +00001575 VG_DMSG("");
1576 VG_DMSG("cachegrind: distinct files: %d", distinct_files);
1577 VG_DMSG("cachegrind: distinct fns: %d", distinct_fns);
1578 VG_DMSG("cachegrind: distinct lines: %d", distinct_lines);
1579 VG_DMSG("cachegrind: distinct instrs:%d", distinct_instrs);
1580 VG_DMSG("cachegrind: debug lookups : %d", debug_lookups);
njn1baf7db2006-04-18 22:34:48 +00001581
1582 VG_(percentify)(full_debugs, debug_lookups, 1, 6, buf1);
1583 VG_(percentify)(file_line_debugs, debug_lookups, 1, 6, buf2);
1584 VG_(percentify)(fn_debugs, debug_lookups, 1, 6, buf3);
1585 VG_(percentify)(no_debugs, debug_lookups, 1, 6, buf4);
njn6f74a7e2009-03-12 00:06:45 +00001586 VG_DMSG("cachegrind: with full info:%s (%d)",
1587 buf1, full_debugs);
1588 VG_DMSG("cachegrind: with file/line info:%s (%d)",
1589 buf2, file_line_debugs);
1590 VG_DMSG("cachegrind: with fn name info:%s (%d)",
1591 buf3, fn_debugs);
1592 VG_DMSG("cachegrind: with zero info:%s (%d)",
1593 buf4, no_debugs);
njn1baf7db2006-04-18 22:34:48 +00001594
njn6f74a7e2009-03-12 00:06:45 +00001595 VG_DMSG("cachegrind: string table size: %lu",
1596 VG_(OSetGen_Size)(stringTable));
1597 VG_DMSG("cachegrind: CC table size: %lu",
1598 VG_(OSetGen_Size)(CC_table));
1599 VG_DMSG("cachegrind: InstrInfo table size: %lu",
1600 VG_(OSetGen_Size)(instrInfoTable));
njn4f9c9342002-04-29 16:03:24 +00001601 }
njn4f9c9342002-04-29 16:03:24 +00001602}
1603
nethercote9313ac42004-07-06 21:54:20 +00001604/*--------------------------------------------------------------------*/
1605/*--- Discarding BB info ---*/
1606/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001607
sewardja3a29a52005-10-12 16:16:03 +00001608// Called when a translation is removed from the translation cache for
1609// any reason at all: to free up space, because the guest code was
1610// unmapped or modified, or for any arbitrary reason.
sewardj4ba057c2005-10-18 12:04:18 +00001611static
sewardj0b9d74a2006-12-24 02:24:11 +00001612void cg_discard_superblock_info ( Addr64 orig_addr64, VexGuestExtents vge )
sewardj18d75132002-05-16 11:06:21 +00001613{
sewardj0b9d74a2006-12-24 02:24:11 +00001614 SB_info* sbInfo;
sewardj3a384b32006-01-22 01:12:51 +00001615 Addr orig_addr = (Addr)vge.base[0];
njn4294fd42002-06-05 14:41:10 +00001616
sewardj5155dec2005-10-12 10:09:23 +00001617 tl_assert(vge.n_used > 0);
1618
1619 if (DEBUG_CG)
sewardj4ba057c2005-10-18 12:04:18 +00001620 VG_(printf)( "discard_basic_block_info: %p, %p, %llu\n",
1621 (void*)(Addr)orig_addr,
sewardj5155dec2005-10-12 10:09:23 +00001622 (void*)(Addr)vge.base[0], (ULong)vge.len[0]);
njn4294fd42002-06-05 14:41:10 +00001623
sewardj4ba057c2005-10-18 12:04:18 +00001624 // Get BB info, remove from table, free BB info. Simple! Note that we
1625 // use orig_addr, not the first instruction address in vge.
njne2a9ad32007-09-17 05:30:48 +00001626 sbInfo = VG_(OSetGen_Remove)(instrInfoTable, &orig_addr);
sewardj0b9d74a2006-12-24 02:24:11 +00001627 tl_assert(NULL != sbInfo);
njne2a9ad32007-09-17 05:30:48 +00001628 VG_(OSetGen_FreeNode)(instrInfoTable, sbInfo);
sewardj18d75132002-05-16 11:06:21 +00001629}
1630
1631/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001632/*--- Command line processing ---*/
1633/*--------------------------------------------------------------------*/
1634
njn0103de52005-10-10 16:49:01 +00001635static void parse_cache_opt ( cache_t* cache, Char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001636{
njn83df0b62009-02-25 01:01:05 +00001637 Long i1, i2, i3;
1638 Char* endptr;
njn25e49d8e72002-09-23 09:36:25 +00001639
njn83df0b62009-02-25 01:01:05 +00001640 // Option argument looks like "65536,2,64". Extract them.
1641 i1 = VG_(strtoll10)(opt, &endptr); if (*endptr != ',') goto bad;
1642 i2 = VG_(strtoll10)(endptr+1, &endptr); if (*endptr != ',') goto bad;
1643 i3 = VG_(strtoll10)(endptr+1, &endptr); if (*endptr != '\0') goto bad;
njn25e49d8e72002-09-23 09:36:25 +00001644
njn83df0b62009-02-25 01:01:05 +00001645 // Check for overflow.
1646 cache->size = (Int)i1;
1647 cache->assoc = (Int)i2;
1648 cache->line_size = (Int)i3;
1649 if (cache->size != i1) goto overflow;
1650 if (cache->assoc != i2) goto overflow;
1651 if (cache->line_size != i3) goto overflow;
njn25e49d8e72002-09-23 09:36:25 +00001652
njn25e49d8e72002-09-23 09:36:25 +00001653 return;
1654
njn83df0b62009-02-25 01:01:05 +00001655 overflow:
njn6f74a7e2009-03-12 00:06:45 +00001656 VG_UMSG("one of the cache parameters was too large and overflowed\n");
njn25e49d8e72002-09-23 09:36:25 +00001657 bad:
njn83df0b62009-02-25 01:01:05 +00001658 // XXX: this omits the "--I1/D1/L2=" part from the message, but that's
1659 // not a big deal.
sewardj6893d652006-10-15 01:25:13 +00001660 VG_(err_bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001661}
1662
njn51d827b2005-05-09 01:02:08 +00001663static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001664{
njn83df0b62009-02-25 01:01:05 +00001665 Char* tmp_str;
1666
nethercote9313ac42004-07-06 21:54:20 +00001667 // 5 is length of "--I1="
njn83df0b62009-02-25 01:01:05 +00001668 if VG_STR_CLO(arg, "--I1", tmp_str)
1669 parse_cache_opt(&clo_I1_cache, tmp_str);
1670 else if VG_STR_CLO(arg, "--D1", tmp_str)
1671 parse_cache_opt(&clo_D1_cache, tmp_str);
1672 else if VG_STR_CLO(arg, "--L2", tmp_str)
1673 parse_cache_opt(&clo_L2_cache, tmp_str);
1674
1675 else if VG_STR_CLO( arg, "--cachegrind-out-file", clo_cachegrind_out_file) {}
1676 else if VG_BOOL_CLO(arg, "--cache-sim", clo_cache_sim) {}
1677 else if VG_BOOL_CLO(arg, "--branch-sim", clo_branch_sim) {}
njn25e49d8e72002-09-23 09:36:25 +00001678 else
1679 return False;
1680
1681 return True;
1682}
1683
njn51d827b2005-05-09 01:02:08 +00001684static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001685{
njn3e884182003-04-15 13:03:23 +00001686 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001687" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1688" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001689" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
sewardj8badbaa2007-05-08 09:20:25 +00001690" --cache-sim=yes|no [yes] collect cache stats?\n"
1691" --branch-sim=yes|no [no] collect branch prediction stats?\n"
njn374a36d2007-11-23 01:41:32 +00001692" --cachegrind-out-file=<file> output file name [cachegrind.out.%%p]\n"
njn3e884182003-04-15 13:03:23 +00001693 );
1694}
1695
njn51d827b2005-05-09 01:02:08 +00001696static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001697{
1698 VG_(printf)(
1699" (none)\n"
1700 );
njn25e49d8e72002-09-23 09:36:25 +00001701}
1702
1703/*--------------------------------------------------------------------*/
1704/*--- Setup ---*/
1705/*--------------------------------------------------------------------*/
1706
sewardje1216cb2007-02-07 19:55:30 +00001707static void cg_post_clo_init(void); /* just below */
1708
njn51d827b2005-05-09 01:02:08 +00001709static void cg_pre_clo_init(void)
1710{
njn51d827b2005-05-09 01:02:08 +00001711 VG_(details_name) ("Cachegrind");
1712 VG_(details_version) (NULL);
sewardj8badbaa2007-05-08 09:20:25 +00001713 VG_(details_description) ("a cache and branch-prediction profiler");
njn51d827b2005-05-09 01:02:08 +00001714 VG_(details_copyright_author)(
njn9f207462009-03-10 22:02:09 +00001715 "Copyright (C) 2002-2009, and GNU GPL'd, by Nicholas Nethercote et al.");
njn51d827b2005-05-09 01:02:08 +00001716 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardje8089302006-10-17 02:15:17 +00001717 VG_(details_avg_translation_sizeB) ( 500 );
njn51d827b2005-05-09 01:02:08 +00001718
1719 VG_(basic_tool_funcs) (cg_post_clo_init,
1720 cg_instrument,
1721 cg_fini);
1722
sewardj0b9d74a2006-12-24 02:24:11 +00001723 VG_(needs_superblock_discards)(cg_discard_superblock_info);
njn51d827b2005-05-09 01:02:08 +00001724 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1725 cg_print_usage,
1726 cg_print_debug_usage);
sewardje1216cb2007-02-07 19:55:30 +00001727}
1728
1729static void cg_post_clo_init(void)
1730{
sewardje1216cb2007-02-07 19:55:30 +00001731 cache_t I1c, D1c, L2c;
njn51d827b2005-05-09 01:02:08 +00001732
sewardj8badbaa2007-05-08 09:20:25 +00001733 /* Can't disable both cache and branch profiling */
1734 if ((!clo_cache_sim) && (!clo_branch_sim)) {
njn6f74a7e2009-03-12 00:06:45 +00001735 VG_UMSG("ERROR: --cache-sim=no --branch-sim=no is not allowed.");
1736 VG_UMSG("You must select cache profiling, or branch profiling, or both.");
sewardj8badbaa2007-05-08 09:20:25 +00001737 VG_(exit)(2);
1738 }
1739
njne2a9ad32007-09-17 05:30:48 +00001740 CC_table =
1741 VG_(OSetGen_Create)(offsetof(LineCC, loc),
1742 cmp_CodeLoc_LineCC,
sewardj9c606bd2008-09-18 18:12:50 +00001743 VG_(malloc), "cg.main.cpci.1",
1744 VG_(free));
njne2a9ad32007-09-17 05:30:48 +00001745 instrInfoTable =
1746 VG_(OSetGen_Create)(/*keyOff*/0,
1747 NULL,
sewardj9c606bd2008-09-18 18:12:50 +00001748 VG_(malloc), "cg.main.cpci.2",
1749 VG_(free));
njne2a9ad32007-09-17 05:30:48 +00001750 stringTable =
1751 VG_(OSetGen_Create)(/*keyOff*/0,
1752 stringCmp,
sewardj9c606bd2008-09-18 18:12:50 +00001753 VG_(malloc), "cg.main.cpci.3",
1754 VG_(free));
sewardje1216cb2007-02-07 19:55:30 +00001755
1756 configure_caches(&I1c, &D1c, &L2c);
1757
1758 cachesim_I1_initcache(I1c);
1759 cachesim_D1_initcache(D1c);
1760 cachesim_L2_initcache(L2c);
njn51d827b2005-05-09 01:02:08 +00001761}
1762
sewardj45f4e7c2005-09-27 19:20:21 +00001763VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init)
fitzhardinge98abfc72003-12-16 02:05:15 +00001764
njn25e49d8e72002-09-23 09:36:25 +00001765/*--------------------------------------------------------------------*/
njnf69f9452005-07-03 17:53:11 +00001766/*--- end ---*/
sewardj18d75132002-05-16 11:06:21 +00001767/*--------------------------------------------------------------------*/
njnd3bef4f2005-10-15 17:46:18 +00001768