blob: a21cb8512e4f2c1035e7e83dfc8de362b2d0d755 [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
sewardje6da2fa2005-05-12 17:57:14 +0000400 case Ist_AbiHint:
401 /* ABI hints aren't interesting to cachegrind. Ignore. */
402 break;
403
njn6a3009b2005-03-20 00:20:06 +0000404 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000405 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
406 to the host's native pointer type; if that is 32 bits then it
407 discards the upper 32 bits. If we are cachegrinding on a
408 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000409 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000410 host and guest word sizes must be the same. Hence
411 st->Ist.IMark.addr will have been derived from a 32-bit guest
412 code address and truncation of it is safe. I believe this
413 assignment should be correct for both 32- and 64-bit
414 machines. */
415 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000416 *instrLen = st->Ist.IMark.len;
417 addStmtToIRBB( bbOut, st );
418 break;
419
420 case Ist_Tmp: {
421 IRExpr* data = st->Ist.Tmp.data;
422 if (data->tag == Iex_LDle) {
423 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000424 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000425
426 // XXX: repe cmpsb does two loads... the first one is ignored here!
427 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
428 *loadAddrExpr = aexpr;
429 *dataSize = sizeofIRType(data->Iex.LDle.ty);
430 }
431 addStmtToIRBB( bbOut, st );
432 break;
433 }
434
435 case Ist_STle: {
436 IRExpr* data = st->Ist.STle.data;
437 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000438 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000439 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
440 *storeAddrExpr = aexpr;
441 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
442 addStmtToIRBB( bbOut, st );
443 break;
444 }
445
sewardj7f4a8622005-03-26 21:55:21 +0000446 case Ist_Dirty: {
447 IRDirty* d = st->Ist.Dirty.details;
448 if (d->mFx != Ifx_None) {
449 /* This dirty helper accesses memory. Collect the
450 details. */
451 tl_assert(d->mAddr != NULL);
452 tl_assert(d->mSize != 0);
453 *dataSize = d->mSize;
454 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
455 *loadAddrExpr = d->mAddr;
456 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
457 *storeAddrExpr = d->mAddr;
458 } else {
459 tl_assert(d->mAddr == NULL);
460 tl_assert(d->mSize == 0);
461 }
462 addStmtToIRBB( bbOut, st );
463 break;
464 }
465
njn6a3009b2005-03-20 00:20:06 +0000466 case Ist_Put:
467 case Ist_PutI:
468 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000469 case Ist_MFence:
470 addStmtToIRBB( bbOut, st );
471 break;
472
473 default:
474 VG_(printf)("\n");
475 ppIRStmt(st);
476 VG_(printf)("\n");
477 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
478 }
479}
nethercote9313ac42004-07-06 21:54:20 +0000480
nethercote564b2b02004-08-07 15:54:53 +0000481static
njn6a3009b2005-03-20 00:20:06 +0000482void do_details( instr_info* n, Bool bbSeenBefore,
483 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000484{
njn6a3009b2005-03-20 00:20:06 +0000485 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000486 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000487 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000488 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000489 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000490 // the debug info might change; the other asserts should be enough to
491 // detect anything strange.
492 } else {
njn6a3009b2005-03-20 00:20:06 +0000493 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000494 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000495 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000496 n->data_size = data_size;
497 n->parent = parent;
498 }
499}
500
njn6a3009b2005-03-20 00:20:06 +0000501static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000502{
njn6a3009b2005-03-20 00:20:06 +0000503 // I'm assuming that for 'modify' instructions, that Vex always makes
504 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
505 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000506 tl_assert(isIRAtom(loadAddrExpr));
507 tl_assert(isIRAtom(storeAddrExpr));
508 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000509}
510
511// Instrumentation for the end of each original instruction.
512static
513void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
514 UInt instrAddr, UInt instrLen, UInt dataSize,
515 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
516{
517 IRDirty* di;
518 IRExpr *arg1, *arg2, *arg3, **argv;
519 Int argc;
520 Char* helperName;
521 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000522 IRType wordTy;
523
524 // Stay sane ...
525 tl_assert(sizeof(HWord) == sizeof(void*));
526 if (sizeof(HWord) == 4) {
527 wordTy = Ity_I32;
528 } else
529 if (sizeof(HWord) == 8) {
530 wordTy = Ity_I64;
531 } else {
532 VG_(tool_panic)("endOfInstr: strange word size");
533 }
534
535 if (loadAddrExpr)
536 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
537 if (storeAddrExpr)
538 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
539
njn6a3009b2005-03-20 00:20:06 +0000540
541 // Nb: instrLen will be zero if Vex failed to decode it.
542 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000543 (instrLen >= VGA_MIN_INSTR_SZB &&
544 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000545
njn016712a2005-04-04 02:52:16 +0000546 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
547 // done inaccurately, but they're very rare and this avoids errors from
548 // hitting more than two cache lines in the simulation.
549 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
550
njn6a3009b2005-03-20 00:20:06 +0000551 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000552 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000553 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000554 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000555
556 if (!loadAddrExpr && !storeAddrExpr) {
557 // no load/store
558 tl_assert(0 == dataSize);
559 helperName = "log_1I_0D_cache_access";
560 helperAddr = &log_1I_0D_cache_access;
561 argc = 1;
562 argv = mkIRExprVec_1(arg1);
563
564 } else if (loadAddrExpr && !storeAddrExpr) {
565 // load
sewardj710d6c22005-03-20 18:55:15 +0000566 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000567 helperName = "log_1I_1Dr_cache_access";
568 helperAddr = &log_1I_1Dr_cache_access;
569 argc = 2;
570 arg2 = loadAddrExpr;
571 argv = mkIRExprVec_2(arg1, arg2);
572
573 } else if (!loadAddrExpr && storeAddrExpr) {
574 // store
sewardj710d6c22005-03-20 18:55:15 +0000575 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000576 helperName = "log_1I_1Dw_cache_access";
577 helperAddr = &log_1I_1Dw_cache_access;
578 argc = 2;
579 arg2 = storeAddrExpr;
580 argv = mkIRExprVec_2(arg1, arg2);
581
582 } else {
583 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000584 tl_assert( isIRAtom(loadAddrExpr) );
585 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000586
587 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
588 // modify
589 helperName = "log_1I_1Dr_cache_access";
590 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000591 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000592 arg2 = loadAddrExpr;
593 argv = mkIRExprVec_2(arg1, arg2);
594
nethercote9313ac42004-07-06 21:54:20 +0000595 } else {
njn6a3009b2005-03-20 00:20:06 +0000596 // load/store
597 helperName = "log_1I_2D_cache_access";
598 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000599 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000600 arg2 = loadAddrExpr;
601 arg3 = storeAddrExpr;
602 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000603 }
604 }
605
njn6a3009b2005-03-20 00:20:06 +0000606 // Add call to the instrumentation function
607 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
608 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000609}
njn14d01ce2004-11-26 11:30:14 +0000610
njn51d827b2005-05-09 01:02:08 +0000611static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
612 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000613{
njn6a3009b2005-03-20 00:20:06 +0000614 Int i, dataSize = 0, bbInfo_i;
615 IRBB* bbOut;
616 IRStmt* st;
617 BB_info* bbInfo;
618 Bool bbSeenBefore = False;
619 Addr instrAddr, origAddr;
620 UInt instrLen;
621 IRExpr *loadAddrExpr, *storeAddrExpr;
622
sewardjd54babf2005-03-21 00:55:49 +0000623 if (gWordTy != hWordTy) {
624 /* We don't currently support this case. */
625 VG_(tool_panic)("host/guest word size mismatch");
626 }
627
njn6a3009b2005-03-20 00:20:06 +0000628 /* Set up BB */
629 bbOut = emptyIRBB();
630 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
631 bbOut->next = dopyIRExpr(bbIn->next);
632 bbOut->jumpkind = bbIn->jumpkind;
633
634 // Get the first statement, and origAddr from it
635 i = 0;
636 tl_assert(bbIn->stmts_used > 0);
637 st = bbIn->stmts[0];
638 tl_assert(Ist_IMark == st->tag);
639 origAddr = (Addr)st->Ist.IMark.addr;
640 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
641
642 // Get block info
643 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
644 bbInfo_i = 0;
645
646 do {
647 // We should be at an IMark statement
648 tl_assert(Ist_IMark == st->tag);
649
650 // Reset stuff for this original instruction
651 loadAddrExpr = storeAddrExpr = NULL;
652 dataSize = 0;
653
654 // Process all the statements for this original instruction (ie. until
655 // the next IMark statement, or the end of the block)
656 do {
657 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
658 &loadAddrExpr, &storeAddrExpr, &dataSize);
659 i++;
660 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
661 }
662 while (st && Ist_IMark != st->tag);
663
664 // Add instrumentation for this original instruction.
665 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
666 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
667
668 bbInfo_i++;
669 }
670 while (st);
671
672 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000673}
njn4f9c9342002-04-29 16:03:24 +0000674
675/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000676/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000677/*------------------------------------------------------------*/
678
sewardjb5f6f512005-03-10 23:59:00 +0000679#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000680
681static cache_t clo_I1_cache = UNDEFINED_CACHE;
682static cache_t clo_D1_cache = UNDEFINED_CACHE;
683static cache_t clo_L2_cache = UNDEFINED_CACHE;
684
njn7cf0bd32002-06-08 13:36:03 +0000685/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000686static
njna1d1a642004-11-26 18:36:02 +0000687void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000688{
689 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000690 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000691 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000692 "error: %s size of %dB not a power of two; aborting.",
693 name, cache->size);
694 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000695 }
696
sewardj07133bf2002-06-13 10:25:56 +0000697 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000698 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000699 "error: %s associativity of %d not a power of two; aborting.",
700 name, cache->assoc);
701 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000702 }
703
sewardj07133bf2002-06-13 10:25:56 +0000704 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000705 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000706 "error: %s line size of %dB not a power of two; aborting.",
707 name, cache->line_size);
708 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000709 }
710
njn6a3009b2005-03-20 00:20:06 +0000711 // Then check line size >= 16 -- any smaller and a single instruction could
712 // straddle three cache lines, which breaks a simulation assertion and is
713 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000714 if (cache->line_size < MIN_LINE_SIZE) {
715 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000716 "error: %s line size of %dB too small; aborting.",
717 name, cache->line_size);
718 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000719 }
720
721 /* Then check cache size > line size (causes seg faults if not). */
722 if (cache->size <= cache->line_size) {
723 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000724 "error: %s cache size of %dB <= line size of %dB; aborting.",
725 name, cache->size, cache->line_size);
726 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000727 }
728
729 /* Then check assoc <= (size / line size) (seg faults otherwise). */
730 if (cache->assoc > (cache->size / cache->line_size)) {
731 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000732 "warning: %s associativity > (size / line size); aborting.", name);
733 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000734 }
735}
736
sewardj07133bf2002-06-13 10:25:56 +0000737static
nethercoteb35a8b92004-09-11 16:45:27 +0000738void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000739{
nethercote9313ac42004-07-06 21:54:20 +0000740#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
741
nethercoteb35a8b92004-09-11 16:45:27 +0000742 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000743
nethercoteb35a8b92004-09-11 16:45:27 +0000744 // Count how many were defined on the command line.
745 if (DEFINED(clo_I1_cache)) { n_clos++; }
746 if (DEFINED(clo_D1_cache)) { n_clos++; }
747 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000748
njna1d1a642004-11-26 18:36:02 +0000749 // Set the cache config (using auto-detection, if supported by the
750 // architecture)
751 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000752
nethercote9313ac42004-07-06 21:54:20 +0000753 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000754 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
755 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
756 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000757
nethercote9313ac42004-07-06 21:54:20 +0000758 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000759 check_cache(I1c, "I1");
760 check_cache(D1c, "D1");
761 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000762
763 if (VG_(clo_verbosity) > 1) {
764 VG_(message)(Vg_UserMsg, "Cache configuration used:");
765 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
766 I1c->size, I1c->assoc, I1c->line_size);
767 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
768 D1c->size, D1c->assoc, D1c->line_size);
769 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
770 L2c->size, L2c->assoc, L2c->line_size);
771 }
nethercote9313ac42004-07-06 21:54:20 +0000772#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000773}
774
njn4f9c9342002-04-29 16:03:24 +0000775/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000776/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000777/*------------------------------------------------------------*/
778
nethercote9313ac42004-07-06 21:54:20 +0000779// Total reads/writes/misses. Calculated during CC traversal at the end.
780// All auto-zeroed.
781static CC Ir_total;
782static CC Dr_total;
783static CC Dw_total;
784
785static Char* cachegrind_out_file;
786
nethercote9313ac42004-07-06 21:54:20 +0000787static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000788{
nethercote9313ac42004-07-06 21:54:20 +0000789 Char buf[512];
790 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
791 n->line,
792 n->Ir.a, n->Ir.m1, n->Ir.m2,
793 n->Dr.a, n->Dr.m1, n->Dr.m2,
794 n->Dw.a, n->Dw.m1, n->Dw.m2);
795 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
796
797 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
798 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
799 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
800}
801
802static void fprint_CC_table_and_calc_totals(void)
803{
804 Int fd;
805 Char buf[512];
806 fileCC *curr_fileCC;
807 fnCC *curr_fnCC;
808 lineCC *curr_lineCC;
809 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000810
njn25e49d8e72002-09-23 09:36:25 +0000811 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000812
njndb918dd2003-07-22 20:45:11 +0000813 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000814 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000815 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000816 // If the file can't be opened for whatever reason (conflict
817 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000818 VG_(message)(Vg_UserMsg,
819 "error: can't open cache simulation output file `%s'",
820 cachegrind_out_file );
821 VG_(message)(Vg_UserMsg,
822 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000823 return;
824 }
njn4f9c9342002-04-29 16:03:24 +0000825
nethercote9313ac42004-07-06 21:54:20 +0000826 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
827 // the 2nd colon makes cg_annotate's output look nicer.
828 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
829 "desc: D1 cache: %s\n"
830 "desc: L2 cache: %s\n",
831 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000832 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000833
nethercote9313ac42004-07-06 21:54:20 +0000834 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000835 VG_(strcpy)(buf, "cmd:");
836 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000837 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000838 if (VG_(client_argv)[i] == NULL)
839 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000840 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000841 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000842 }
nethercote9313ac42004-07-06 21:54:20 +0000843 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000844 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
845 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
846
nethercote9313ac42004-07-06 21:54:20 +0000847 // Six loops here: three for the hash table arrays, and three for the
848 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000849 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000850 curr_fileCC = CC_table[i];
851 while (curr_fileCC != NULL) {
852 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000853 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
854
855 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000856 curr_fnCC = curr_fileCC->fns[j];
857 while (curr_fnCC != NULL) {
858 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000859 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
860
nethercote9313ac42004-07-06 21:54:20 +0000861 for (k = 0; k < N_LINE_ENTRIES; k++) {
862 curr_lineCC = curr_fnCC->lines[k];
863 while (curr_lineCC != NULL) {
864 fprint_lineCC(fd, curr_lineCC);
865 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000866 }
867 }
nethercote9313ac42004-07-06 21:54:20 +0000868 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000869 }
870 }
nethercote9313ac42004-07-06 21:54:20 +0000871 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000872 }
873 }
874
nethercote9313ac42004-07-06 21:54:20 +0000875 // Summary stats must come after rest of table, since we calculate them
876 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000877 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000878 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000879 Ir_total.a, Ir_total.m1, Ir_total.m2,
880 Dr_total.a, Dr_total.m1, Dr_total.m2,
881 Dw_total.a, Dw_total.m1, Dw_total.m2);
882 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
883 VG_(close)(fd);
884}
885
njn607adfc2003-09-30 14:15:44 +0000886static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000887{
njn607adfc2003-09-30 14:15:44 +0000888 UInt w = 0;
889 while (n > 0) {
890 n = n / 10;
891 w++;
njn4f9c9342002-04-29 16:03:24 +0000892 }
njn607adfc2003-09-30 14:15:44 +0000893 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000894}
895
sewardj4f29ddf2002-05-03 22:29:04 +0000896static
daywalker8ad1a402003-09-18 01:15:32 +0000897void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000898{
899 int i, len, space;
900
daywalker8ad1a402003-09-18 01:15:32 +0000901 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000902 len = VG_(strlen)(buf);
903 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000904 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000905 i = len;
906
907 /* Right justify in field */
908 for ( ; i >= 0; i--) buf[i + space] = buf[i];
909 for (i = 0; i < space; i++) buf[i] = ' ';
910}
911
njn51d827b2005-05-09 01:02:08 +0000912static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000913{
nethercote9313ac42004-07-06 21:54:20 +0000914 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000915
njn4f9c9342002-04-29 16:03:24 +0000916 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000917 ULong L2_total_m, L2_total_mr, L2_total_mw,
918 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000919 Int l1, l2, l3;
920 Int p;
921
nethercote9313ac42004-07-06 21:54:20 +0000922 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000923
njn7cf0bd32002-06-08 13:36:03 +0000924 if (VG_(clo_verbosity) == 0)
925 return;
926
njn4f9c9342002-04-29 16:03:24 +0000927 /* I cache results. Use the I_refs value to determine the first column
928 * width. */
njn607adfc2003-09-30 14:15:44 +0000929 l1 = ULong_width(Ir_total.a);
930 l2 = ULong_width(Dr_total.a);
931 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000932
njn607adfc2003-09-30 14:15:44 +0000933 /* Make format string, getting width right for numbers */
934 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
935
936 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
937 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
938 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000939
940 p = 100;
941
njn25e49d8e72002-09-23 09:36:25 +0000942 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000943 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
944 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
945
946 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
947 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
948 VG_(message)(Vg_UserMsg, "");
949
950 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
951 * width of columns 2 & 3. */
952 D_total.a = Dr_total.a + Dw_total.a;
953 D_total.m1 = Dr_total.m1 + Dw_total.m1;
954 D_total.m2 = Dr_total.m2 + Dw_total.m2;
955
njn607adfc2003-09-30 14:15:44 +0000956 /* Make format string, getting width right for numbers */
957 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000958
njn607adfc2003-09-30 14:15:44 +0000959 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
960 D_total.a, Dr_total.a, Dw_total.a);
961 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
962 D_total.m1, Dr_total.m1, Dw_total.m1);
963 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
964 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000965
966 p = 10;
967
njn25e49d8e72002-09-23 09:36:25 +0000968 if (0 == D_total.a) D_total.a = 1;
969 if (0 == Dr_total.a) Dr_total.a = 1;
970 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000971 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
972 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
973 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
974 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
975
976 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
977 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
978 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
979 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
980 VG_(message)(Vg_UserMsg, "");
981
982 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000983
984 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
985 L2_total_r = Dr_total.m1 + Ir_total.m1;
986 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000987 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
988 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000989
njn4f9c9342002-04-29 16:03:24 +0000990 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
991 L2_total_mr = Dr_total.m2 + Ir_total.m2;
992 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +0000993 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
994 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +0000995
996 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
997 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
998 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
999 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1000
1001
nethercote9313ac42004-07-06 21:54:20 +00001002 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001003 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001004 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001005 file_line_debug_BBs + no_debug_BBs;
1006
1007 VG_(message)(Vg_DebugMsg, "");
1008 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1009 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001010 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1011 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001012 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1013 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1014 full_debug_BBs * 100 / BB_lookups,
1015 full_debug_BBs);
1016 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1017 file_line_debug_BBs * 100 / BB_lookups,
1018 file_line_debug_BBs);
1019 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001020 fn_debug_BBs * 100 / BB_lookups,
1021 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001022 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1023 no_debug_BBs * 100 / BB_lookups,
1024 no_debug_BBs);
1025 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001026 }
njn25e49d8e72002-09-23 09:36:25 +00001027 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001028}
1029
nethercote9313ac42004-07-06 21:54:20 +00001030/*--------------------------------------------------------------------*/
1031/*--- Discarding BB info ---*/
1032/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001033
nethercote9313ac42004-07-06 21:54:20 +00001034// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001035static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001036{
nethercote9313ac42004-07-06 21:54:20 +00001037 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001038 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001039
nethercote928a5f72004-11-03 18:10:37 +00001040 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001041
nethercote9313ac42004-07-06 21:54:20 +00001042 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001043 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1044 tl_assert(NULL != bbInfo);
1045 *prev_next_ptr = bbInfo->next;
1046 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001047}
1048
1049/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001050/*--- Command line processing ---*/
1051/*--------------------------------------------------------------------*/
1052
nethercote9313ac42004-07-06 21:54:20 +00001053static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001054{
nethercote9313ac42004-07-06 21:54:20 +00001055 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001056
nethercote9313ac42004-07-06 21:54:20 +00001057 // Option argument looks like "65536,2,64".
1058 // Find commas, replace with NULs to make three independent
1059 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001060 while (VG_(isdigit)(opt[i])) i++;
1061 if (',' == opt[i]) {
1062 opt[i++] = '\0';
1063 i2 = i;
1064 } else goto bad;
1065 while (VG_(isdigit)(opt[i])) i++;
1066 if (',' == opt[i]) {
1067 opt[i++] = '\0';
1068 i3 = i;
1069 } else goto bad;
1070 while (VG_(isdigit)(opt[i])) i++;
1071 if ('\0' != opt[i]) goto bad;
1072
nethercote9313ac42004-07-06 21:54:20 +00001073 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001074 cache->assoc = (Int)VG_(atoll)(opt + i2);
1075 cache->line_size = (Int)VG_(atoll)(opt + i3);
1076
nethercote9313ac42004-07-06 21:54:20 +00001077 opt[i2-1] = ',';
1078 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001079 return;
1080
1081 bad:
nethercote9313ac42004-07-06 21:54:20 +00001082 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001083}
1084
njn51d827b2005-05-09 01:02:08 +00001085static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001086{
nethercote9313ac42004-07-06 21:54:20 +00001087 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001088 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001089 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001090 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001091 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001092 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001093 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001094 else
1095 return False;
1096
1097 return True;
1098}
1099
njn51d827b2005-05-09 01:02:08 +00001100static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001101{
njn3e884182003-04-15 13:03:23 +00001102 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001103" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1104" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001105" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1106 );
1107}
1108
njn51d827b2005-05-09 01:02:08 +00001109static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001110{
1111 VG_(printf)(
1112" (none)\n"
1113 );
njn25e49d8e72002-09-23 09:36:25 +00001114}
1115
1116/*--------------------------------------------------------------------*/
1117/*--- Setup ---*/
1118/*--------------------------------------------------------------------*/
1119
njn51d827b2005-05-09 01:02:08 +00001120static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001121{
1122 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001123
nethercoteb35a8b92004-09-11 16:45:27 +00001124 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001125
1126 cachesim_I1_initcache(I1c);
1127 cachesim_D1_initcache(D1c);
1128 cachesim_L2_initcache(L2c);
1129
njn31066fd2005-03-26 00:42:02 +00001130 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1131 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1132 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001133}
1134
njn51d827b2005-05-09 01:02:08 +00001135static void cg_pre_clo_init(void)
1136{
1137 Char* base_dir = NULL;
1138
1139 VG_(details_name) ("Cachegrind");
1140 VG_(details_version) (NULL);
1141 VG_(details_description) ("an I1/D1/L2 cache profiler");
1142 VG_(details_copyright_author)(
1143 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1144 VG_(details_bug_reports_to) (VG_BUGS_TO);
1145 VG_(details_avg_translation_sizeB) ( 155 );
1146
1147 VG_(basic_tool_funcs) (cg_post_clo_init,
1148 cg_instrument,
1149 cg_fini);
1150
1151 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1152 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1153 cg_print_usage,
1154 cg_print_debug_usage);
1155
1156 /* Get working directory */
1157 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1158
1159 /* Block is big enough for dir name + cachegrind.out.<pid> */
1160 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1161 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1162 base_dir, VG_(getpid)());
1163 VG_(free)(base_dir);
1164
1165 instr_info_table = VG_(HT_construct)();
1166}
1167
1168VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001169
njn25e49d8e72002-09-23 09:36:25 +00001170/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001171/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001172/*--------------------------------------------------------------------*/