blob: b82d452d05beb62a3e11f806e0e65d7ca366944c [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
njn53612422005-03-12 16:22:54 +000011 Copyright (C) 2002-2005 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
nethercote46063202004-09-02 08:51:43 +000032#include "tool.h"
njn717cde52005-05-10 02:47:21 +000033#include "pub_tool_mallocfree.h"
njn43b9a8a2005-05-10 04:37:01 +000034#include "pub_tool_tooliface.h"
njn25e49d8e72002-09-23 09:36:25 +000035
nethercoteb35a8b92004-09-11 16:45:27 +000036#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000037#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000038
njn25e49d8e72002-09-23 09:36:25 +000039/*------------------------------------------------------------*/
40/*--- Constants ---*/
41/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000042
nethercote9313ac42004-07-06 21:54:20 +000043#define MIN_LINE_SIZE 16
44#define FILE_LEN 256
45#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000046
47/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000048/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000049/*------------------------------------------------------------*/
50
njn25e49d8e72002-09-23 09:36:25 +000051typedef
52 enum {
nethercote9313ac42004-07-06 21:54:20 +000053 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000054 VgpCacheSimulate,
55 VgpCacheResults
56 }
nethercote7cc9c232004-01-21 15:08:04 +000057 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000058
njn4f9c9342002-04-29 16:03:24 +000059/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000060/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000061/*------------------------------------------------------------*/
62
63typedef struct _CC CC;
64struct _CC {
65 ULong a;
66 ULong m1;
67 ULong m2;
68};
69
nethercote9313ac42004-07-06 21:54:20 +000070//------------------------------------------------------------
71// Primary data structure #1: CC table
72// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
73// - hash(file, hash(fn, hash(line+CC)))
74// - Each hash table is separately chained.
75// - The array sizes below work fairly well for Konqueror.
76// - Lookups done by instr_addr, which is converted immediately to a source
77// location.
78// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000079
80#define N_FILE_ENTRIES 251
81#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000082#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000083
nethercote9313ac42004-07-06 21:54:20 +000084typedef struct _lineCC lineCC;
85struct _lineCC {
86 Int line;
87 CC Ir;
88 CC Dr;
89 CC Dw;
90 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +000091};
92
nethercote9313ac42004-07-06 21:54:20 +000093typedef struct _fnCC fnCC;
94struct _fnCC {
95 Char* fn;
96 fnCC* next;
97 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +000098};
99
nethercote9313ac42004-07-06 21:54:20 +0000100typedef struct _fileCC fileCC;
101struct _fileCC {
102 Char* file;
103 fileCC* next;
104 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000105};
106
nethercote9313ac42004-07-06 21:54:20 +0000107// Top level of CC table. Auto-zeroed.
108static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000109
nethercote9313ac42004-07-06 21:54:20 +0000110//------------------------------------------------------------
111// Primary data structre #2: Instr-info table
112// - Holds the cached info about each instr that is used for simulation.
113// - table(BB_start_addr, list(instr_info))
114// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000115// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000116// CC. This node is what's passed to the simulation function.
117// - When BBs are discarded the relevant list(instr_details) is freed.
118
119typedef struct _instr_info instr_info;
120struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000121 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000122 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000123 UChar data_size;
124 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000125};
126
127typedef struct _BB_info BB_info;
128struct _BB_info {
129 BB_info* next; // next field
130 Addr BB_addr; // key
131 Int n_instrs;
132 instr_info instrs[0];
133};
134
135VgHashTable instr_info_table; // hash(Addr, BB_info)
136
137//------------------------------------------------------------
138// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000139static Int distinct_files = 0;
140static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000141static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000142static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000143
sewardj4f29ddf2002-05-03 22:29:04 +0000144static Int full_debug_BBs = 0;
145static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000146static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000147static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000148
sewardj4f29ddf2002-05-03 22:29:04 +0000149static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000150
nethercote9313ac42004-07-06 21:54:20 +0000151/*------------------------------------------------------------*/
152/*--- CC table operations ---*/
153/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000154
nethercote9313ac42004-07-06 21:54:20 +0000155static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
156 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000157{
nethercote9313ac42004-07-06 21:54:20 +0000158 Bool found_file_line = VG_(get_filename_linenum)(instr_addr, file,
159 FILE_LEN, line);
160 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000161
nethercote9313ac42004-07-06 21:54:20 +0000162 if (!found_file_line) {
163 VG_(strcpy)(file, "???");
164 *line = 0;
165 }
166 if (!found_fn) {
167 VG_(strcpy)(fn, "???");
168 }
169 if (found_file_line) {
170 if (found_fn) full_debug_BBs++;
171 else file_line_debug_BBs++;
172 } else {
173 if (found_fn) fn_debug_BBs++;
174 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000175 }
176}
177
njn4f9c9342002-04-29 16:03:24 +0000178static UInt hash(Char *s, UInt table_size)
179{
nethercote9313ac42004-07-06 21:54:20 +0000180 const int hash_constant = 256;
181 int hash_value = 0;
182 for ( ; *s; s++)
183 hash_value = (hash_constant * hash_value + *s) % table_size;
184 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000185}
186
nethercote9313ac42004-07-06 21:54:20 +0000187static __inline__
188fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000189{
nethercote9313ac42004-07-06 21:54:20 +0000190 // Using calloc() zeroes the fns[] array
191 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
192 cc->file = VG_(strdup)(filename);
193 cc->next = next;
194 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000195}
196
nethercote9313ac42004-07-06 21:54:20 +0000197static __inline__
198fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000199{
nethercote9313ac42004-07-06 21:54:20 +0000200 // Using calloc() zeroes the lines[] array
201 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
202 cc->fn = VG_(strdup)(fn);
203 cc->next = next;
204 return cc;
205}
njn4f9c9342002-04-29 16:03:24 +0000206
nethercote9313ac42004-07-06 21:54:20 +0000207static __inline__
208lineCC* new_lineCC(Int line, lineCC* next)
209{
210 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
211 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
212 cc->line = line;
213 cc->next = next;
214 return cc;
215}
njn4f9c9342002-04-29 16:03:24 +0000216
nethercote9313ac42004-07-06 21:54:20 +0000217static __inline__
218instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
219{
njn6a3009b2005-03-20 00:20:06 +0000220 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000221 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
222 ii->instr_addr = instr_addr;
223 ii->parent = parent;
224 return ii;
225}
226
227// Do a three step traversal: by file, then fn, then line.
228// In all cases prepends new nodes to their chain. Returns a pointer to the
229// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000230static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000231{
232 fileCC *curr_fileCC;
233 fnCC *curr_fnCC;
234 lineCC *curr_lineCC;
235 Char file[FILE_LEN], fn[FN_LEN];
236 Int line;
237 UInt file_hash, fn_hash, line_hash;
238
njn6a3009b2005-03-20 00:20:06 +0000239 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000240
241 VGP_PUSHCC(VgpGetLineCC);
242
243 // level 1
244 file_hash = hash(file, N_FILE_ENTRIES);
245 curr_fileCC = CC_table[file_hash];
246 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
247 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000248 }
nethercote9313ac42004-07-06 21:54:20 +0000249 if (NULL == curr_fileCC) {
250 CC_table[file_hash] = curr_fileCC =
251 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000252 distinct_files++;
253 }
254
nethercote9313ac42004-07-06 21:54:20 +0000255 // level 2
256 fn_hash = hash(fn, N_FN_ENTRIES);
257 curr_fnCC = curr_fileCC->fns[fn_hash];
258 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
259 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000260 }
nethercote9313ac42004-07-06 21:54:20 +0000261 if (NULL == curr_fnCC) {
262 curr_fileCC->fns[fn_hash] = curr_fnCC =
263 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000264 distinct_fns++;
265 }
266
nethercote9313ac42004-07-06 21:54:20 +0000267 // level 3
268 line_hash = line % N_LINE_ENTRIES;
269 curr_lineCC = curr_fnCC->lines[line_hash];
270 while (NULL != curr_lineCC && line != curr_lineCC->line) {
271 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000272 }
nethercote9313ac42004-07-06 21:54:20 +0000273 if (NULL == curr_lineCC) {
274 curr_fnCC->lines[line_hash] = curr_lineCC =
275 new_lineCC(line, curr_fnCC->lines[line_hash]);
276 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000277 }
nethercote9313ac42004-07-06 21:54:20 +0000278
279 VGP_POPCC(VgpGetLineCC);
280 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000281}
282
283/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000284/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000285/*------------------------------------------------------------*/
286
njn9fb73db2005-03-27 01:55:21 +0000287static VGA_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000288void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000289{
njn6a3009b2005-03-20 00:20:06 +0000290 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
291 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000292 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000293 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000294 &n->parent->Ir.m1, &n->parent->Ir.m2);
295 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000296 VGP_POPCC(VgpCacheSimulate);
297}
298
njn9fb73db2005-03-27 01:55:21 +0000299static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000300void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000301{
nethercote9313ac42004-07-06 21:54:20 +0000302 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000303 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000304 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000305 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000306 &n->parent->Ir.m1, &n->parent->Ir.m2);
307 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000308
nethercote9313ac42004-07-06 21:54:20 +0000309 cachesim_D1_doref(data_addr, n->data_size,
310 &n->parent->Dr.m1, &n->parent->Dr.m2);
311 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000312 VGP_POPCC(VgpCacheSimulate);
313}
314
njn9fb73db2005-03-27 01:55:21 +0000315static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000316void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000317{
nethercote9313ac42004-07-06 21:54:20 +0000318 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000319 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000320 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000321 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000322 &n->parent->Ir.m1, &n->parent->Ir.m2);
323 n->parent->Ir.a++;
324
325 cachesim_D1_doref(data_addr, n->data_size,
326 &n->parent->Dw.m1, &n->parent->Dw.m2);
327 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000328 VGP_POPCC(VgpCacheSimulate);
329}
330
njn9fb73db2005-03-27 01:55:21 +0000331static VGA_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000332void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000333{
334 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000335 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000336 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000337 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000338 &n->parent->Ir.m1, &n->parent->Ir.m2);
339 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000340
nethercote9313ac42004-07-06 21:54:20 +0000341 cachesim_D1_doref(data_addr1, n->data_size,
342 &n->parent->Dr.m1, &n->parent->Dr.m2);
343 n->parent->Dr.a++;
344 cachesim_D1_doref(data_addr2, n->data_size,
345 &n->parent->Dw.m1, &n->parent->Dw.m2);
346 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000347 VGP_POPCC(VgpCacheSimulate);
348}
349
nethercote9313ac42004-07-06 21:54:20 +0000350/*------------------------------------------------------------*/
351/*--- Instrumentation ---*/
352/*------------------------------------------------------------*/
353
nethercote564b2b02004-08-07 15:54:53 +0000354static
njn6a3009b2005-03-20 00:20:06 +0000355BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000356{
357 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000358 IRStmt* st;
359 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000360 VgHashNode** dummy;
361
njn6a3009b2005-03-20 00:20:06 +0000362 // Count number of original instrs in BB
363 n_instrs = 0;
364 for (i = 0; i < bbIn->stmts_used; i++) {
365 st = bbIn->stmts[i];
366 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000367 }
368
369 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000370 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
371 *bbSeenBefore = ( NULL == bbInfo ? False : True );
372 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000373 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000374 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000375 BB_retranslations++;
376 } else {
377 // BB never translated before (at this address, at least; could have
378 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000379 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
380 bbInfo->BB_addr = origAddr;
381 bbInfo->n_instrs = n_instrs;
382 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000383 distinct_instrs++;
384 }
njn6a3009b2005-03-20 00:20:06 +0000385 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000386}
njn6a3009b2005-03-20 00:20:06 +0000387
njn6a3009b2005-03-20 00:20:06 +0000388static
389void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
390 Addr* instrAddr, UInt* instrLen,
391 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
392 UInt* dataSize)
393{
sewardj7f4a8622005-03-26 21:55:21 +0000394 tl_assert(isFlatIRStmt(st));
395
njn6a3009b2005-03-20 00:20:06 +0000396 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000397 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000398 break;
399
400 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000401 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
402 to the host's native pointer type; if that is 32 bits then it
403 discards the upper 32 bits. If we are cachegrinding on a
404 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000405 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000406 host and guest word sizes must be the same. Hence
407 st->Ist.IMark.addr will have been derived from a 32-bit guest
408 code address and truncation of it is safe. I believe this
409 assignment should be correct for both 32- and 64-bit
410 machines. */
411 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000412 *instrLen = st->Ist.IMark.len;
413 addStmtToIRBB( bbOut, st );
414 break;
415
416 case Ist_Tmp: {
417 IRExpr* data = st->Ist.Tmp.data;
418 if (data->tag == Iex_LDle) {
419 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000420 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000421
422 // XXX: repe cmpsb does two loads... the first one is ignored here!
423 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
424 *loadAddrExpr = aexpr;
425 *dataSize = sizeofIRType(data->Iex.LDle.ty);
426 }
427 addStmtToIRBB( bbOut, st );
428 break;
429 }
430
431 case Ist_STle: {
432 IRExpr* data = st->Ist.STle.data;
433 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000434 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000435 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
436 *storeAddrExpr = aexpr;
437 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
438 addStmtToIRBB( bbOut, st );
439 break;
440 }
441
sewardj7f4a8622005-03-26 21:55:21 +0000442 case Ist_Dirty: {
443 IRDirty* d = st->Ist.Dirty.details;
444 if (d->mFx != Ifx_None) {
445 /* This dirty helper accesses memory. Collect the
446 details. */
447 tl_assert(d->mAddr != NULL);
448 tl_assert(d->mSize != 0);
449 *dataSize = d->mSize;
450 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
451 *loadAddrExpr = d->mAddr;
452 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
453 *storeAddrExpr = d->mAddr;
454 } else {
455 tl_assert(d->mAddr == NULL);
456 tl_assert(d->mSize == 0);
457 }
458 addStmtToIRBB( bbOut, st );
459 break;
460 }
461
njn6a3009b2005-03-20 00:20:06 +0000462 case Ist_Put:
463 case Ist_PutI:
464 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000465 case Ist_MFence:
466 addStmtToIRBB( bbOut, st );
467 break;
468
469 default:
470 VG_(printf)("\n");
471 ppIRStmt(st);
472 VG_(printf)("\n");
473 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
474 }
475}
nethercote9313ac42004-07-06 21:54:20 +0000476
nethercote564b2b02004-08-07 15:54:53 +0000477static
njn6a3009b2005-03-20 00:20:06 +0000478void do_details( instr_info* n, Bool bbSeenBefore,
479 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000480{
njn6a3009b2005-03-20 00:20:06 +0000481 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000482 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000483 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000484 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000485 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000486 // the debug info might change; the other asserts should be enough to
487 // detect anything strange.
488 } else {
njn6a3009b2005-03-20 00:20:06 +0000489 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000490 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000491 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000492 n->data_size = data_size;
493 n->parent = parent;
494 }
495}
496
njn6a3009b2005-03-20 00:20:06 +0000497static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000498{
njn6a3009b2005-03-20 00:20:06 +0000499 // I'm assuming that for 'modify' instructions, that Vex always makes
500 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
501 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000502 tl_assert(isIRAtom(loadAddrExpr));
503 tl_assert(isIRAtom(storeAddrExpr));
504 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000505}
506
507// Instrumentation for the end of each original instruction.
508static
509void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
510 UInt instrAddr, UInt instrLen, UInt dataSize,
511 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
512{
513 IRDirty* di;
514 IRExpr *arg1, *arg2, *arg3, **argv;
515 Int argc;
516 Char* helperName;
517 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000518 IRType wordTy;
519
520 // Stay sane ...
521 tl_assert(sizeof(HWord) == sizeof(void*));
522 if (sizeof(HWord) == 4) {
523 wordTy = Ity_I32;
524 } else
525 if (sizeof(HWord) == 8) {
526 wordTy = Ity_I64;
527 } else {
528 VG_(tool_panic)("endOfInstr: strange word size");
529 }
530
531 if (loadAddrExpr)
532 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
533 if (storeAddrExpr)
534 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
535
njn6a3009b2005-03-20 00:20:06 +0000536
537 // Nb: instrLen will be zero if Vex failed to decode it.
538 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000539 (instrLen >= VGA_MIN_INSTR_SZB &&
540 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000541
njn016712a2005-04-04 02:52:16 +0000542 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
543 // done inaccurately, but they're very rare and this avoids errors from
544 // hitting more than two cache lines in the simulation.
545 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
546
njn6a3009b2005-03-20 00:20:06 +0000547 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000548 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000549 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000550 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000551
552 if (!loadAddrExpr && !storeAddrExpr) {
553 // no load/store
554 tl_assert(0 == dataSize);
555 helperName = "log_1I_0D_cache_access";
556 helperAddr = &log_1I_0D_cache_access;
557 argc = 1;
558 argv = mkIRExprVec_1(arg1);
559
560 } else if (loadAddrExpr && !storeAddrExpr) {
561 // load
sewardj710d6c22005-03-20 18:55:15 +0000562 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000563 helperName = "log_1I_1Dr_cache_access";
564 helperAddr = &log_1I_1Dr_cache_access;
565 argc = 2;
566 arg2 = loadAddrExpr;
567 argv = mkIRExprVec_2(arg1, arg2);
568
569 } else if (!loadAddrExpr && storeAddrExpr) {
570 // store
sewardj710d6c22005-03-20 18:55:15 +0000571 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000572 helperName = "log_1I_1Dw_cache_access";
573 helperAddr = &log_1I_1Dw_cache_access;
574 argc = 2;
575 arg2 = storeAddrExpr;
576 argv = mkIRExprVec_2(arg1, arg2);
577
578 } else {
579 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000580 tl_assert( isIRAtom(loadAddrExpr) );
581 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000582
583 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
584 // modify
585 helperName = "log_1I_1Dr_cache_access";
586 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000587 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000588 arg2 = loadAddrExpr;
589 argv = mkIRExprVec_2(arg1, arg2);
590
nethercote9313ac42004-07-06 21:54:20 +0000591 } else {
njn6a3009b2005-03-20 00:20:06 +0000592 // load/store
593 helperName = "log_1I_2D_cache_access";
594 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000595 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000596 arg2 = loadAddrExpr;
597 arg3 = storeAddrExpr;
598 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000599 }
600 }
601
njn6a3009b2005-03-20 00:20:06 +0000602 // Add call to the instrumentation function
603 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
604 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000605}
njn14d01ce2004-11-26 11:30:14 +0000606
njn51d827b2005-05-09 01:02:08 +0000607static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
608 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000609{
njn6a3009b2005-03-20 00:20:06 +0000610 Int i, dataSize = 0, bbInfo_i;
611 IRBB* bbOut;
612 IRStmt* st;
613 BB_info* bbInfo;
614 Bool bbSeenBefore = False;
615 Addr instrAddr, origAddr;
616 UInt instrLen;
617 IRExpr *loadAddrExpr, *storeAddrExpr;
618
sewardjd54babf2005-03-21 00:55:49 +0000619 if (gWordTy != hWordTy) {
620 /* We don't currently support this case. */
621 VG_(tool_panic)("host/guest word size mismatch");
622 }
623
njn6a3009b2005-03-20 00:20:06 +0000624 /* Set up BB */
625 bbOut = emptyIRBB();
626 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
627 bbOut->next = dopyIRExpr(bbIn->next);
628 bbOut->jumpkind = bbIn->jumpkind;
629
630 // Get the first statement, and origAddr from it
631 i = 0;
632 tl_assert(bbIn->stmts_used > 0);
633 st = bbIn->stmts[0];
634 tl_assert(Ist_IMark == st->tag);
635 origAddr = (Addr)st->Ist.IMark.addr;
636 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
637
638 // Get block info
639 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
640 bbInfo_i = 0;
641
642 do {
643 // We should be at an IMark statement
644 tl_assert(Ist_IMark == st->tag);
645
646 // Reset stuff for this original instruction
647 loadAddrExpr = storeAddrExpr = NULL;
648 dataSize = 0;
649
650 // Process all the statements for this original instruction (ie. until
651 // the next IMark statement, or the end of the block)
652 do {
653 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
654 &loadAddrExpr, &storeAddrExpr, &dataSize);
655 i++;
656 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
657 }
658 while (st && Ist_IMark != st->tag);
659
660 // Add instrumentation for this original instruction.
661 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
662 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
663
664 bbInfo_i++;
665 }
666 while (st);
667
668 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000669}
njn4f9c9342002-04-29 16:03:24 +0000670
671/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000672/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000673/*------------------------------------------------------------*/
674
sewardjb5f6f512005-03-10 23:59:00 +0000675#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000676
677static cache_t clo_I1_cache = UNDEFINED_CACHE;
678static cache_t clo_D1_cache = UNDEFINED_CACHE;
679static cache_t clo_L2_cache = UNDEFINED_CACHE;
680
njn7cf0bd32002-06-08 13:36:03 +0000681/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000682static
njna1d1a642004-11-26 18:36:02 +0000683void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000684{
685 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000686 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000687 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000688 "error: %s size of %dB not a power of two; aborting.",
689 name, cache->size);
690 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000691 }
692
sewardj07133bf2002-06-13 10:25:56 +0000693 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000694 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000695 "error: %s associativity of %d not a power of two; aborting.",
696 name, cache->assoc);
697 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000698 }
699
sewardj07133bf2002-06-13 10:25:56 +0000700 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000701 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000702 "error: %s line size of %dB not a power of two; aborting.",
703 name, cache->line_size);
704 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000705 }
706
njn6a3009b2005-03-20 00:20:06 +0000707 // Then check line size >= 16 -- any smaller and a single instruction could
708 // straddle three cache lines, which breaks a simulation assertion and is
709 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000710 if (cache->line_size < MIN_LINE_SIZE) {
711 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000712 "error: %s line size of %dB too small; aborting.",
713 name, cache->line_size);
714 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000715 }
716
717 /* Then check cache size > line size (causes seg faults if not). */
718 if (cache->size <= cache->line_size) {
719 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000720 "error: %s cache size of %dB <= line size of %dB; aborting.",
721 name, cache->size, cache->line_size);
722 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000723 }
724
725 /* Then check assoc <= (size / line size) (seg faults otherwise). */
726 if (cache->assoc > (cache->size / cache->line_size)) {
727 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000728 "warning: %s associativity > (size / line size); aborting.", name);
729 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000730 }
731}
732
sewardj07133bf2002-06-13 10:25:56 +0000733static
nethercoteb35a8b92004-09-11 16:45:27 +0000734void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000735{
nethercote9313ac42004-07-06 21:54:20 +0000736#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
737
nethercoteb35a8b92004-09-11 16:45:27 +0000738 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000739
nethercoteb35a8b92004-09-11 16:45:27 +0000740 // Count how many were defined on the command line.
741 if (DEFINED(clo_I1_cache)) { n_clos++; }
742 if (DEFINED(clo_D1_cache)) { n_clos++; }
743 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000744
njna1d1a642004-11-26 18:36:02 +0000745 // Set the cache config (using auto-detection, if supported by the
746 // architecture)
747 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000748
nethercote9313ac42004-07-06 21:54:20 +0000749 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000750 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
751 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
752 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000753
nethercote9313ac42004-07-06 21:54:20 +0000754 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000755 check_cache(I1c, "I1");
756 check_cache(D1c, "D1");
757 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000758
759 if (VG_(clo_verbosity) > 1) {
760 VG_(message)(Vg_UserMsg, "Cache configuration used:");
761 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
762 I1c->size, I1c->assoc, I1c->line_size);
763 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
764 D1c->size, D1c->assoc, D1c->line_size);
765 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
766 L2c->size, L2c->assoc, L2c->line_size);
767 }
nethercote9313ac42004-07-06 21:54:20 +0000768#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000769}
770
njn4f9c9342002-04-29 16:03:24 +0000771/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000772/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000773/*------------------------------------------------------------*/
774
nethercote9313ac42004-07-06 21:54:20 +0000775// Total reads/writes/misses. Calculated during CC traversal at the end.
776// All auto-zeroed.
777static CC Ir_total;
778static CC Dr_total;
779static CC Dw_total;
780
781static Char* cachegrind_out_file;
782
nethercote9313ac42004-07-06 21:54:20 +0000783static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000784{
nethercote9313ac42004-07-06 21:54:20 +0000785 Char buf[512];
786 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
787 n->line,
788 n->Ir.a, n->Ir.m1, n->Ir.m2,
789 n->Dr.a, n->Dr.m1, n->Dr.m2,
790 n->Dw.a, n->Dw.m1, n->Dw.m2);
791 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
792
793 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
794 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
795 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
796}
797
798static void fprint_CC_table_and_calc_totals(void)
799{
800 Int fd;
801 Char buf[512];
802 fileCC *curr_fileCC;
803 fnCC *curr_fnCC;
804 lineCC *curr_lineCC;
805 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000806
njn25e49d8e72002-09-23 09:36:25 +0000807 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000808
njndb918dd2003-07-22 20:45:11 +0000809 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000810 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000811 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000812 // If the file can't be opened for whatever reason (conflict
813 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000814 VG_(message)(Vg_UserMsg,
815 "error: can't open cache simulation output file `%s'",
816 cachegrind_out_file );
817 VG_(message)(Vg_UserMsg,
818 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000819 return;
820 }
njn4f9c9342002-04-29 16:03:24 +0000821
nethercote9313ac42004-07-06 21:54:20 +0000822 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
823 // the 2nd colon makes cg_annotate's output look nicer.
824 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
825 "desc: D1 cache: %s\n"
826 "desc: L2 cache: %s\n",
827 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000828 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000829
nethercote9313ac42004-07-06 21:54:20 +0000830 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000831 VG_(strcpy)(buf, "cmd:");
832 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000833 for (i = 0; i < VG_(client_argc); i++) {
thughes6f7eb9c2004-10-06 13:50:12 +0000834 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000835 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000836 }
nethercote9313ac42004-07-06 21:54:20 +0000837 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000838 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
839 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
840
nethercote9313ac42004-07-06 21:54:20 +0000841 // Six loops here: three for the hash table arrays, and three for the
842 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000843 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000844 curr_fileCC = CC_table[i];
845 while (curr_fileCC != NULL) {
846 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000847 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
848
849 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000850 curr_fnCC = curr_fileCC->fns[j];
851 while (curr_fnCC != NULL) {
852 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000853 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
854
nethercote9313ac42004-07-06 21:54:20 +0000855 for (k = 0; k < N_LINE_ENTRIES; k++) {
856 curr_lineCC = curr_fnCC->lines[k];
857 while (curr_lineCC != NULL) {
858 fprint_lineCC(fd, curr_lineCC);
859 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000860 }
861 }
nethercote9313ac42004-07-06 21:54:20 +0000862 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000863 }
864 }
nethercote9313ac42004-07-06 21:54:20 +0000865 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000866 }
867 }
868
nethercote9313ac42004-07-06 21:54:20 +0000869 // Summary stats must come after rest of table, since we calculate them
870 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000871 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000872 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000873 Ir_total.a, Ir_total.m1, Ir_total.m2,
874 Dr_total.a, Dr_total.m1, Dr_total.m2,
875 Dw_total.a, Dw_total.m1, Dw_total.m2);
876 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
877 VG_(close)(fd);
878}
879
njn607adfc2003-09-30 14:15:44 +0000880static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000881{
njn607adfc2003-09-30 14:15:44 +0000882 UInt w = 0;
883 while (n > 0) {
884 n = n / 10;
885 w++;
njn4f9c9342002-04-29 16:03:24 +0000886 }
njn607adfc2003-09-30 14:15:44 +0000887 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000888}
889
sewardj4f29ddf2002-05-03 22:29:04 +0000890static
daywalker8ad1a402003-09-18 01:15:32 +0000891void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000892{
893 int i, len, space;
894
daywalker8ad1a402003-09-18 01:15:32 +0000895 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000896 len = VG_(strlen)(buf);
897 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000898 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000899 i = len;
900
901 /* Right justify in field */
902 for ( ; i >= 0; i--) buf[i + space] = buf[i];
903 for (i = 0; i < space; i++) buf[i] = ' ';
904}
905
njn51d827b2005-05-09 01:02:08 +0000906static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000907{
nethercote9313ac42004-07-06 21:54:20 +0000908 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000909
njn4f9c9342002-04-29 16:03:24 +0000910 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000911 ULong L2_total_m, L2_total_mr, L2_total_mw,
912 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000913 Int l1, l2, l3;
914 Int p;
915
nethercote9313ac42004-07-06 21:54:20 +0000916 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000917
njn7cf0bd32002-06-08 13:36:03 +0000918 if (VG_(clo_verbosity) == 0)
919 return;
920
njn4f9c9342002-04-29 16:03:24 +0000921 /* I cache results. Use the I_refs value to determine the first column
922 * width. */
njn607adfc2003-09-30 14:15:44 +0000923 l1 = ULong_width(Ir_total.a);
924 l2 = ULong_width(Dr_total.a);
925 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000926
njn607adfc2003-09-30 14:15:44 +0000927 /* Make format string, getting width right for numbers */
928 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
929
930 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
931 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
932 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000933
934 p = 100;
935
njn25e49d8e72002-09-23 09:36:25 +0000936 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000937 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
938 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
939
940 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
941 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
942 VG_(message)(Vg_UserMsg, "");
943
944 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
945 * width of columns 2 & 3. */
946 D_total.a = Dr_total.a + Dw_total.a;
947 D_total.m1 = Dr_total.m1 + Dw_total.m1;
948 D_total.m2 = Dr_total.m2 + Dw_total.m2;
949
njn607adfc2003-09-30 14:15:44 +0000950 /* Make format string, getting width right for numbers */
951 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000952
njn607adfc2003-09-30 14:15:44 +0000953 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
954 D_total.a, Dr_total.a, Dw_total.a);
955 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
956 D_total.m1, Dr_total.m1, Dw_total.m1);
957 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
958 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000959
960 p = 10;
961
njn25e49d8e72002-09-23 09:36:25 +0000962 if (0 == D_total.a) D_total.a = 1;
963 if (0 == Dr_total.a) Dr_total.a = 1;
964 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000965 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
966 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
967 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
968 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
969
970 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
971 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
972 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
973 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
974 VG_(message)(Vg_UserMsg, "");
975
976 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000977
978 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
979 L2_total_r = Dr_total.m1 + Ir_total.m1;
980 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000981 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
982 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000983
njn4f9c9342002-04-29 16:03:24 +0000984 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
985 L2_total_mr = Dr_total.m2 + Ir_total.m2;
986 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +0000987 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
988 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +0000989
990 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
991 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
992 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
993 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
994
995
nethercote9313ac42004-07-06 21:54:20 +0000996 // Various stats
njn4f9c9342002-04-29 16:03:24 +0000997 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +0000998 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +0000999 file_line_debug_BBs + no_debug_BBs;
1000
1001 VG_(message)(Vg_DebugMsg, "");
1002 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1003 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001004 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1005 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001006 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1007 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1008 full_debug_BBs * 100 / BB_lookups,
1009 full_debug_BBs);
1010 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1011 file_line_debug_BBs * 100 / BB_lookups,
1012 file_line_debug_BBs);
1013 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001014 fn_debug_BBs * 100 / BB_lookups,
1015 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001016 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1017 no_debug_BBs * 100 / BB_lookups,
1018 no_debug_BBs);
1019 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001020 }
njn25e49d8e72002-09-23 09:36:25 +00001021 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001022}
1023
nethercote9313ac42004-07-06 21:54:20 +00001024/*--------------------------------------------------------------------*/
1025/*--- Discarding BB info ---*/
1026/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001027
nethercote9313ac42004-07-06 21:54:20 +00001028// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001029static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001030{
nethercote9313ac42004-07-06 21:54:20 +00001031 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001032 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001033
nethercote928a5f72004-11-03 18:10:37 +00001034 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001035
nethercote9313ac42004-07-06 21:54:20 +00001036 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001037 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1038 tl_assert(NULL != bbInfo);
1039 *prev_next_ptr = bbInfo->next;
1040 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001041}
1042
1043/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001044/*--- Command line processing ---*/
1045/*--------------------------------------------------------------------*/
1046
nethercote9313ac42004-07-06 21:54:20 +00001047static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001048{
nethercote9313ac42004-07-06 21:54:20 +00001049 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001050
nethercote9313ac42004-07-06 21:54:20 +00001051 // Option argument looks like "65536,2,64".
1052 // Find commas, replace with NULs to make three independent
1053 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001054 while (VG_(isdigit)(opt[i])) i++;
1055 if (',' == opt[i]) {
1056 opt[i++] = '\0';
1057 i2 = i;
1058 } else goto bad;
1059 while (VG_(isdigit)(opt[i])) i++;
1060 if (',' == opt[i]) {
1061 opt[i++] = '\0';
1062 i3 = i;
1063 } else goto bad;
1064 while (VG_(isdigit)(opt[i])) i++;
1065 if ('\0' != opt[i]) goto bad;
1066
nethercote9313ac42004-07-06 21:54:20 +00001067 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001068 cache->assoc = (Int)VG_(atoll)(opt + i2);
1069 cache->line_size = (Int)VG_(atoll)(opt + i3);
1070
nethercote9313ac42004-07-06 21:54:20 +00001071 opt[i2-1] = ',';
1072 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001073 return;
1074
1075 bad:
nethercote9313ac42004-07-06 21:54:20 +00001076 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001077}
1078
njn51d827b2005-05-09 01:02:08 +00001079static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001080{
nethercote9313ac42004-07-06 21:54:20 +00001081 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001082 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001083 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001084 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001085 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001086 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001087 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001088 else
1089 return False;
1090
1091 return True;
1092}
1093
njn51d827b2005-05-09 01:02:08 +00001094static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001095{
njn3e884182003-04-15 13:03:23 +00001096 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001097" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1098" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001099" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1100 );
1101}
1102
njn51d827b2005-05-09 01:02:08 +00001103static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001104{
1105 VG_(printf)(
1106" (none)\n"
1107 );
njn25e49d8e72002-09-23 09:36:25 +00001108}
1109
1110/*--------------------------------------------------------------------*/
1111/*--- Setup ---*/
1112/*--------------------------------------------------------------------*/
1113
njn51d827b2005-05-09 01:02:08 +00001114static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001115{
1116 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001117
nethercoteb35a8b92004-09-11 16:45:27 +00001118 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001119
1120 cachesim_I1_initcache(I1c);
1121 cachesim_D1_initcache(D1c);
1122 cachesim_L2_initcache(L2c);
1123
njn31066fd2005-03-26 00:42:02 +00001124 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1125 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1126 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001127}
1128
njn51d827b2005-05-09 01:02:08 +00001129static void cg_pre_clo_init(void)
1130{
1131 Char* base_dir = NULL;
1132
1133 VG_(details_name) ("Cachegrind");
1134 VG_(details_version) (NULL);
1135 VG_(details_description) ("an I1/D1/L2 cache profiler");
1136 VG_(details_copyright_author)(
1137 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1138 VG_(details_bug_reports_to) (VG_BUGS_TO);
1139 VG_(details_avg_translation_sizeB) ( 155 );
1140
1141 VG_(basic_tool_funcs) (cg_post_clo_init,
1142 cg_instrument,
1143 cg_fini);
1144
1145 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1146 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1147 cg_print_usage,
1148 cg_print_debug_usage);
1149
1150 /* Get working directory */
1151 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1152
1153 /* Block is big enough for dir name + cachegrind.out.<pid> */
1154 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1155 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1156 base_dir, VG_(getpid)());
1157 VG_(free)(base_dir);
1158
1159 instr_info_table = VG_(HT_construct)();
1160}
1161
1162VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001163
njn25e49d8e72002-09-23 09:36:25 +00001164/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001165/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001166/*--------------------------------------------------------------------*/