blob: 036176cc57ca4e23e7d89b88039d2033bcf25984 [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"
njn81c00df2005-05-14 21:28:43 +000033#include "pub_tool_hashtable.h"
njn717cde52005-05-10 02:47:21 +000034#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +000035#include "pub_tool_options.h"
njn43b9a8a2005-05-10 04:37:01 +000036#include "pub_tool_tooliface.h"
njn25e49d8e72002-09-23 09:36:25 +000037
nethercoteb35a8b92004-09-11 16:45:27 +000038#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000039#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000040
njn25e49d8e72002-09-23 09:36:25 +000041/*------------------------------------------------------------*/
42/*--- Constants ---*/
43/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000044
nethercote9313ac42004-07-06 21:54:20 +000045#define MIN_LINE_SIZE 16
46#define FILE_LEN 256
47#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000048
49/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000050/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000051/*------------------------------------------------------------*/
52
njn25e49d8e72002-09-23 09:36:25 +000053typedef
54 enum {
nethercote9313ac42004-07-06 21:54:20 +000055 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000056 VgpCacheSimulate,
57 VgpCacheResults
58 }
nethercote7cc9c232004-01-21 15:08:04 +000059 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000060
njn4f9c9342002-04-29 16:03:24 +000061/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000062/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000063/*------------------------------------------------------------*/
64
65typedef struct _CC CC;
66struct _CC {
67 ULong a;
68 ULong m1;
69 ULong m2;
70};
71
nethercote9313ac42004-07-06 21:54:20 +000072//------------------------------------------------------------
73// Primary data structure #1: CC table
74// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
75// - hash(file, hash(fn, hash(line+CC)))
76// - Each hash table is separately chained.
77// - The array sizes below work fairly well for Konqueror.
78// - Lookups done by instr_addr, which is converted immediately to a source
79// location.
80// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000081
82#define N_FILE_ENTRIES 251
83#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000084#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000085
nethercote9313ac42004-07-06 21:54:20 +000086typedef struct _lineCC lineCC;
87struct _lineCC {
88 Int line;
89 CC Ir;
90 CC Dr;
91 CC Dw;
92 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +000093};
94
nethercote9313ac42004-07-06 21:54:20 +000095typedef struct _fnCC fnCC;
96struct _fnCC {
97 Char* fn;
98 fnCC* next;
99 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000100};
101
nethercote9313ac42004-07-06 21:54:20 +0000102typedef struct _fileCC fileCC;
103struct _fileCC {
104 Char* file;
105 fileCC* next;
106 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000107};
108
nethercote9313ac42004-07-06 21:54:20 +0000109// Top level of CC table. Auto-zeroed.
110static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000111
nethercote9313ac42004-07-06 21:54:20 +0000112//------------------------------------------------------------
113// Primary data structre #2: Instr-info table
114// - Holds the cached info about each instr that is used for simulation.
115// - table(BB_start_addr, list(instr_info))
116// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000117// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000118// CC. This node is what's passed to the simulation function.
119// - When BBs are discarded the relevant list(instr_details) is freed.
120
121typedef struct _instr_info instr_info;
122struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000123 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000124 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000125 UChar data_size;
126 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000127};
128
129typedef struct _BB_info BB_info;
130struct _BB_info {
131 BB_info* next; // next field
132 Addr BB_addr; // key
133 Int n_instrs;
134 instr_info instrs[0];
135};
136
137VgHashTable instr_info_table; // hash(Addr, BB_info)
138
139//------------------------------------------------------------
140// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000141static Int distinct_files = 0;
142static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000143static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000144static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000145
sewardj4f29ddf2002-05-03 22:29:04 +0000146static Int full_debug_BBs = 0;
147static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000148static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000149static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000150
sewardj4f29ddf2002-05-03 22:29:04 +0000151static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000152
nethercote9313ac42004-07-06 21:54:20 +0000153/*------------------------------------------------------------*/
154/*--- CC table operations ---*/
155/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000156
nethercote9313ac42004-07-06 21:54:20 +0000157static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
158 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000159{
nethercote9313ac42004-07-06 21:54:20 +0000160 Bool found_file_line = VG_(get_filename_linenum)(instr_addr, file,
161 FILE_LEN, line);
162 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000163
nethercote9313ac42004-07-06 21:54:20 +0000164 if (!found_file_line) {
165 VG_(strcpy)(file, "???");
166 *line = 0;
167 }
168 if (!found_fn) {
169 VG_(strcpy)(fn, "???");
170 }
171 if (found_file_line) {
172 if (found_fn) full_debug_BBs++;
173 else file_line_debug_BBs++;
174 } else {
175 if (found_fn) fn_debug_BBs++;
176 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000177 }
178}
179
njn4f9c9342002-04-29 16:03:24 +0000180static UInt hash(Char *s, UInt table_size)
181{
nethercote9313ac42004-07-06 21:54:20 +0000182 const int hash_constant = 256;
183 int hash_value = 0;
184 for ( ; *s; s++)
185 hash_value = (hash_constant * hash_value + *s) % table_size;
186 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000187}
188
nethercote9313ac42004-07-06 21:54:20 +0000189static __inline__
190fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000191{
nethercote9313ac42004-07-06 21:54:20 +0000192 // Using calloc() zeroes the fns[] array
193 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
194 cc->file = VG_(strdup)(filename);
195 cc->next = next;
196 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000197}
198
nethercote9313ac42004-07-06 21:54:20 +0000199static __inline__
200fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000201{
nethercote9313ac42004-07-06 21:54:20 +0000202 // Using calloc() zeroes the lines[] array
203 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
204 cc->fn = VG_(strdup)(fn);
205 cc->next = next;
206 return cc;
207}
njn4f9c9342002-04-29 16:03:24 +0000208
nethercote9313ac42004-07-06 21:54:20 +0000209static __inline__
210lineCC* new_lineCC(Int line, lineCC* next)
211{
212 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
213 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
214 cc->line = line;
215 cc->next = next;
216 return cc;
217}
njn4f9c9342002-04-29 16:03:24 +0000218
nethercote9313ac42004-07-06 21:54:20 +0000219static __inline__
220instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
221{
njn6a3009b2005-03-20 00:20:06 +0000222 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000223 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
224 ii->instr_addr = instr_addr;
225 ii->parent = parent;
226 return ii;
227}
228
229// Do a three step traversal: by file, then fn, then line.
230// In all cases prepends new nodes to their chain. Returns a pointer to the
231// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000232static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000233{
234 fileCC *curr_fileCC;
235 fnCC *curr_fnCC;
236 lineCC *curr_lineCC;
237 Char file[FILE_LEN], fn[FN_LEN];
238 Int line;
239 UInt file_hash, fn_hash, line_hash;
240
njn6a3009b2005-03-20 00:20:06 +0000241 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000242
243 VGP_PUSHCC(VgpGetLineCC);
244
245 // level 1
246 file_hash = hash(file, N_FILE_ENTRIES);
247 curr_fileCC = CC_table[file_hash];
248 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
249 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000250 }
nethercote9313ac42004-07-06 21:54:20 +0000251 if (NULL == curr_fileCC) {
252 CC_table[file_hash] = curr_fileCC =
253 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000254 distinct_files++;
255 }
256
nethercote9313ac42004-07-06 21:54:20 +0000257 // level 2
258 fn_hash = hash(fn, N_FN_ENTRIES);
259 curr_fnCC = curr_fileCC->fns[fn_hash];
260 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
261 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000262 }
nethercote9313ac42004-07-06 21:54:20 +0000263 if (NULL == curr_fnCC) {
264 curr_fileCC->fns[fn_hash] = curr_fnCC =
265 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000266 distinct_fns++;
267 }
268
nethercote9313ac42004-07-06 21:54:20 +0000269 // level 3
270 line_hash = line % N_LINE_ENTRIES;
271 curr_lineCC = curr_fnCC->lines[line_hash];
272 while (NULL != curr_lineCC && line != curr_lineCC->line) {
273 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000274 }
nethercote9313ac42004-07-06 21:54:20 +0000275 if (NULL == curr_lineCC) {
276 curr_fnCC->lines[line_hash] = curr_lineCC =
277 new_lineCC(line, curr_fnCC->lines[line_hash]);
278 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000279 }
nethercote9313ac42004-07-06 21:54:20 +0000280
281 VGP_POPCC(VgpGetLineCC);
282 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000283}
284
285/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000286/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000287/*------------------------------------------------------------*/
288
njn9fb73db2005-03-27 01:55:21 +0000289static VGA_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000290void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000291{
njn6a3009b2005-03-20 00:20:06 +0000292 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
293 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000294 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000295 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000296 &n->parent->Ir.m1, &n->parent->Ir.m2);
297 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000298 VGP_POPCC(VgpCacheSimulate);
299}
300
njn9fb73db2005-03-27 01:55:21 +0000301static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000302void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000303{
nethercote9313ac42004-07-06 21:54:20 +0000304 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000305 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000306 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000307 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000308 &n->parent->Ir.m1, &n->parent->Ir.m2);
309 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000310
nethercote9313ac42004-07-06 21:54:20 +0000311 cachesim_D1_doref(data_addr, n->data_size,
312 &n->parent->Dr.m1, &n->parent->Dr.m2);
313 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000314 VGP_POPCC(VgpCacheSimulate);
315}
316
njn9fb73db2005-03-27 01:55:21 +0000317static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000318void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000319{
nethercote9313ac42004-07-06 21:54:20 +0000320 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000321 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000322 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000323 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000324 &n->parent->Ir.m1, &n->parent->Ir.m2);
325 n->parent->Ir.a++;
326
327 cachesim_D1_doref(data_addr, n->data_size,
328 &n->parent->Dw.m1, &n->parent->Dw.m2);
329 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000330 VGP_POPCC(VgpCacheSimulate);
331}
332
njn9fb73db2005-03-27 01:55:21 +0000333static VGA_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000334void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000335{
336 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000337 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000338 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000339 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000340 &n->parent->Ir.m1, &n->parent->Ir.m2);
341 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000342
nethercote9313ac42004-07-06 21:54:20 +0000343 cachesim_D1_doref(data_addr1, n->data_size,
344 &n->parent->Dr.m1, &n->parent->Dr.m2);
345 n->parent->Dr.a++;
346 cachesim_D1_doref(data_addr2, n->data_size,
347 &n->parent->Dw.m1, &n->parent->Dw.m2);
348 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000349 VGP_POPCC(VgpCacheSimulate);
350}
351
nethercote9313ac42004-07-06 21:54:20 +0000352/*------------------------------------------------------------*/
353/*--- Instrumentation ---*/
354/*------------------------------------------------------------*/
355
nethercote564b2b02004-08-07 15:54:53 +0000356static
njn6a3009b2005-03-20 00:20:06 +0000357BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000358{
359 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000360 IRStmt* st;
361 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000362 VgHashNode** dummy;
363
njn6a3009b2005-03-20 00:20:06 +0000364 // Count number of original instrs in BB
365 n_instrs = 0;
366 for (i = 0; i < bbIn->stmts_used; i++) {
367 st = bbIn->stmts[i];
368 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000369 }
370
371 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000372 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
373 *bbSeenBefore = ( NULL == bbInfo ? False : True );
374 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000375 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000376 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000377 BB_retranslations++;
378 } else {
379 // BB never translated before (at this address, at least; could have
380 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000381 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
382 bbInfo->BB_addr = origAddr;
383 bbInfo->n_instrs = n_instrs;
384 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000385 distinct_instrs++;
386 }
njn6a3009b2005-03-20 00:20:06 +0000387 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000388}
njn6a3009b2005-03-20 00:20:06 +0000389
njn6a3009b2005-03-20 00:20:06 +0000390static
391void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
392 Addr* instrAddr, UInt* instrLen,
393 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
394 UInt* dataSize)
395{
sewardj7f4a8622005-03-26 21:55:21 +0000396 tl_assert(isFlatIRStmt(st));
397
njn6a3009b2005-03-20 00:20:06 +0000398 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000399 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000400 break;
401
sewardje6da2fa2005-05-12 17:57:14 +0000402 case Ist_AbiHint:
403 /* ABI hints aren't interesting to cachegrind. Ignore. */
404 break;
405
njn6a3009b2005-03-20 00:20:06 +0000406 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000407 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
408 to the host's native pointer type; if that is 32 bits then it
409 discards the upper 32 bits. If we are cachegrinding on a
410 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000411 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000412 host and guest word sizes must be the same. Hence
413 st->Ist.IMark.addr will have been derived from a 32-bit guest
414 code address and truncation of it is safe. I believe this
415 assignment should be correct for both 32- and 64-bit
416 machines. */
417 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000418 *instrLen = st->Ist.IMark.len;
419 addStmtToIRBB( bbOut, st );
420 break;
421
422 case Ist_Tmp: {
423 IRExpr* data = st->Ist.Tmp.data;
424 if (data->tag == Iex_LDle) {
425 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000426 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000427
428 // XXX: repe cmpsb does two loads... the first one is ignored here!
429 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
430 *loadAddrExpr = aexpr;
431 *dataSize = sizeofIRType(data->Iex.LDle.ty);
432 }
433 addStmtToIRBB( bbOut, st );
434 break;
435 }
436
437 case Ist_STle: {
438 IRExpr* data = st->Ist.STle.data;
439 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000440 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000441 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
442 *storeAddrExpr = aexpr;
443 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
444 addStmtToIRBB( bbOut, st );
445 break;
446 }
447
sewardj7f4a8622005-03-26 21:55:21 +0000448 case Ist_Dirty: {
449 IRDirty* d = st->Ist.Dirty.details;
450 if (d->mFx != Ifx_None) {
451 /* This dirty helper accesses memory. Collect the
452 details. */
453 tl_assert(d->mAddr != NULL);
454 tl_assert(d->mSize != 0);
455 *dataSize = d->mSize;
456 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
457 *loadAddrExpr = d->mAddr;
458 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
459 *storeAddrExpr = d->mAddr;
460 } else {
461 tl_assert(d->mAddr == NULL);
462 tl_assert(d->mSize == 0);
463 }
464 addStmtToIRBB( bbOut, st );
465 break;
466 }
467
njn6a3009b2005-03-20 00:20:06 +0000468 case Ist_Put:
469 case Ist_PutI:
470 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000471 case Ist_MFence:
472 addStmtToIRBB( bbOut, st );
473 break;
474
475 default:
476 VG_(printf)("\n");
477 ppIRStmt(st);
478 VG_(printf)("\n");
479 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
480 }
481}
nethercote9313ac42004-07-06 21:54:20 +0000482
nethercote564b2b02004-08-07 15:54:53 +0000483static
njn6a3009b2005-03-20 00:20:06 +0000484void do_details( instr_info* n, Bool bbSeenBefore,
485 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000486{
njn6a3009b2005-03-20 00:20:06 +0000487 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000488 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000489 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000490 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000491 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000492 // the debug info might change; the other asserts should be enough to
493 // detect anything strange.
494 } else {
njn6a3009b2005-03-20 00:20:06 +0000495 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000496 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000497 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000498 n->data_size = data_size;
499 n->parent = parent;
500 }
501}
502
njn6a3009b2005-03-20 00:20:06 +0000503static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000504{
njn6a3009b2005-03-20 00:20:06 +0000505 // I'm assuming that for 'modify' instructions, that Vex always makes
506 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
507 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000508 tl_assert(isIRAtom(loadAddrExpr));
509 tl_assert(isIRAtom(storeAddrExpr));
510 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000511}
512
513// Instrumentation for the end of each original instruction.
514static
515void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
516 UInt instrAddr, UInt instrLen, UInt dataSize,
517 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
518{
519 IRDirty* di;
520 IRExpr *arg1, *arg2, *arg3, **argv;
521 Int argc;
522 Char* helperName;
523 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000524 IRType wordTy;
525
526 // Stay sane ...
527 tl_assert(sizeof(HWord) == sizeof(void*));
528 if (sizeof(HWord) == 4) {
529 wordTy = Ity_I32;
530 } else
531 if (sizeof(HWord) == 8) {
532 wordTy = Ity_I64;
533 } else {
534 VG_(tool_panic)("endOfInstr: strange word size");
535 }
536
537 if (loadAddrExpr)
538 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
539 if (storeAddrExpr)
540 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
541
njn6a3009b2005-03-20 00:20:06 +0000542
543 // Nb: instrLen will be zero if Vex failed to decode it.
544 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000545 (instrLen >= VGA_MIN_INSTR_SZB &&
546 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000547
njn016712a2005-04-04 02:52:16 +0000548 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
549 // done inaccurately, but they're very rare and this avoids errors from
550 // hitting more than two cache lines in the simulation.
551 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
552
njn6a3009b2005-03-20 00:20:06 +0000553 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000554 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000555 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000556 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000557
558 if (!loadAddrExpr && !storeAddrExpr) {
559 // no load/store
560 tl_assert(0 == dataSize);
561 helperName = "log_1I_0D_cache_access";
562 helperAddr = &log_1I_0D_cache_access;
563 argc = 1;
564 argv = mkIRExprVec_1(arg1);
565
566 } else if (loadAddrExpr && !storeAddrExpr) {
567 // load
sewardj710d6c22005-03-20 18:55:15 +0000568 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000569 helperName = "log_1I_1Dr_cache_access";
570 helperAddr = &log_1I_1Dr_cache_access;
571 argc = 2;
572 arg2 = loadAddrExpr;
573 argv = mkIRExprVec_2(arg1, arg2);
574
575 } else if (!loadAddrExpr && storeAddrExpr) {
576 // store
sewardj710d6c22005-03-20 18:55:15 +0000577 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000578 helperName = "log_1I_1Dw_cache_access";
579 helperAddr = &log_1I_1Dw_cache_access;
580 argc = 2;
581 arg2 = storeAddrExpr;
582 argv = mkIRExprVec_2(arg1, arg2);
583
584 } else {
585 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000586 tl_assert( isIRAtom(loadAddrExpr) );
587 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000588
589 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
590 // modify
591 helperName = "log_1I_1Dr_cache_access";
592 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000593 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000594 arg2 = loadAddrExpr;
595 argv = mkIRExprVec_2(arg1, arg2);
596
nethercote9313ac42004-07-06 21:54:20 +0000597 } else {
njn6a3009b2005-03-20 00:20:06 +0000598 // load/store
599 helperName = "log_1I_2D_cache_access";
600 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000601 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000602 arg2 = loadAddrExpr;
603 arg3 = storeAddrExpr;
604 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000605 }
606 }
607
njn6a3009b2005-03-20 00:20:06 +0000608 // Add call to the instrumentation function
609 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
610 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000611}
njn14d01ce2004-11-26 11:30:14 +0000612
njn51d827b2005-05-09 01:02:08 +0000613static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
614 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000615{
njn6a3009b2005-03-20 00:20:06 +0000616 Int i, dataSize = 0, bbInfo_i;
617 IRBB* bbOut;
618 IRStmt* st;
619 BB_info* bbInfo;
620 Bool bbSeenBefore = False;
621 Addr instrAddr, origAddr;
622 UInt instrLen;
623 IRExpr *loadAddrExpr, *storeAddrExpr;
624
sewardjd54babf2005-03-21 00:55:49 +0000625 if (gWordTy != hWordTy) {
626 /* We don't currently support this case. */
627 VG_(tool_panic)("host/guest word size mismatch");
628 }
629
njn6a3009b2005-03-20 00:20:06 +0000630 /* Set up BB */
631 bbOut = emptyIRBB();
632 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
633 bbOut->next = dopyIRExpr(bbIn->next);
634 bbOut->jumpkind = bbIn->jumpkind;
635
636 // Get the first statement, and origAddr from it
637 i = 0;
638 tl_assert(bbIn->stmts_used > 0);
639 st = bbIn->stmts[0];
640 tl_assert(Ist_IMark == st->tag);
641 origAddr = (Addr)st->Ist.IMark.addr;
642 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
643
644 // Get block info
645 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
646 bbInfo_i = 0;
647
648 do {
649 // We should be at an IMark statement
650 tl_assert(Ist_IMark == st->tag);
651
652 // Reset stuff for this original instruction
653 loadAddrExpr = storeAddrExpr = NULL;
654 dataSize = 0;
655
656 // Process all the statements for this original instruction (ie. until
657 // the next IMark statement, or the end of the block)
658 do {
659 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
660 &loadAddrExpr, &storeAddrExpr, &dataSize);
661 i++;
662 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
663 }
664 while (st && Ist_IMark != st->tag);
665
666 // Add instrumentation for this original instruction.
667 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
668 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
669
670 bbInfo_i++;
671 }
672 while (st);
673
674 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000675}
njn4f9c9342002-04-29 16:03:24 +0000676
677/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000678/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000679/*------------------------------------------------------------*/
680
sewardjb5f6f512005-03-10 23:59:00 +0000681#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000682
683static cache_t clo_I1_cache = UNDEFINED_CACHE;
684static cache_t clo_D1_cache = UNDEFINED_CACHE;
685static cache_t clo_L2_cache = UNDEFINED_CACHE;
686
njn7cf0bd32002-06-08 13:36:03 +0000687/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000688static
njna1d1a642004-11-26 18:36:02 +0000689void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000690{
691 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000692 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000693 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000694 "error: %s size of %dB not a power of two; aborting.",
695 name, cache->size);
696 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000697 }
698
sewardj07133bf2002-06-13 10:25:56 +0000699 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000700 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000701 "error: %s associativity of %d not a power of two; aborting.",
702 name, cache->assoc);
703 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000704 }
705
sewardj07133bf2002-06-13 10:25:56 +0000706 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000707 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000708 "error: %s line size of %dB not a power of two; aborting.",
709 name, cache->line_size);
710 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000711 }
712
njn6a3009b2005-03-20 00:20:06 +0000713 // Then check line size >= 16 -- any smaller and a single instruction could
714 // straddle three cache lines, which breaks a simulation assertion and is
715 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000716 if (cache->line_size < MIN_LINE_SIZE) {
717 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000718 "error: %s line size of %dB too small; aborting.",
719 name, cache->line_size);
720 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000721 }
722
723 /* Then check cache size > line size (causes seg faults if not). */
724 if (cache->size <= cache->line_size) {
725 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000726 "error: %s cache size of %dB <= line size of %dB; aborting.",
727 name, cache->size, cache->line_size);
728 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000729 }
730
731 /* Then check assoc <= (size / line size) (seg faults otherwise). */
732 if (cache->assoc > (cache->size / cache->line_size)) {
733 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000734 "warning: %s associativity > (size / line size); aborting.", name);
735 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000736 }
737}
738
sewardj07133bf2002-06-13 10:25:56 +0000739static
nethercoteb35a8b92004-09-11 16:45:27 +0000740void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000741{
nethercote9313ac42004-07-06 21:54:20 +0000742#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
743
nethercoteb35a8b92004-09-11 16:45:27 +0000744 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000745
nethercoteb35a8b92004-09-11 16:45:27 +0000746 // Count how many were defined on the command line.
747 if (DEFINED(clo_I1_cache)) { n_clos++; }
748 if (DEFINED(clo_D1_cache)) { n_clos++; }
749 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000750
njna1d1a642004-11-26 18:36:02 +0000751 // Set the cache config (using auto-detection, if supported by the
752 // architecture)
753 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000754
nethercote9313ac42004-07-06 21:54:20 +0000755 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000756 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
757 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
758 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000759
nethercote9313ac42004-07-06 21:54:20 +0000760 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000761 check_cache(I1c, "I1");
762 check_cache(D1c, "D1");
763 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000764
765 if (VG_(clo_verbosity) > 1) {
766 VG_(message)(Vg_UserMsg, "Cache configuration used:");
767 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
768 I1c->size, I1c->assoc, I1c->line_size);
769 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
770 D1c->size, D1c->assoc, D1c->line_size);
771 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
772 L2c->size, L2c->assoc, L2c->line_size);
773 }
nethercote9313ac42004-07-06 21:54:20 +0000774#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000775}
776
njn4f9c9342002-04-29 16:03:24 +0000777/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000778/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000779/*------------------------------------------------------------*/
780
nethercote9313ac42004-07-06 21:54:20 +0000781// Total reads/writes/misses. Calculated during CC traversal at the end.
782// All auto-zeroed.
783static CC Ir_total;
784static CC Dr_total;
785static CC Dw_total;
786
787static Char* cachegrind_out_file;
788
nethercote9313ac42004-07-06 21:54:20 +0000789static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000790{
nethercote9313ac42004-07-06 21:54:20 +0000791 Char buf[512];
792 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
793 n->line,
794 n->Ir.a, n->Ir.m1, n->Ir.m2,
795 n->Dr.a, n->Dr.m1, n->Dr.m2,
796 n->Dw.a, n->Dw.m1, n->Dw.m2);
797 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
798
799 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
800 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
801 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
802}
803
804static void fprint_CC_table_and_calc_totals(void)
805{
806 Int fd;
807 Char buf[512];
808 fileCC *curr_fileCC;
809 fnCC *curr_fnCC;
810 lineCC *curr_lineCC;
811 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000812
njn25e49d8e72002-09-23 09:36:25 +0000813 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000814
njndb918dd2003-07-22 20:45:11 +0000815 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000816 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000817 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000818 // If the file can't be opened for whatever reason (conflict
819 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000820 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000821 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000822 cachegrind_out_file );
823 VG_(message)(Vg_UserMsg,
824 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000825 return;
826 }
njn4f9c9342002-04-29 16:03:24 +0000827
nethercote9313ac42004-07-06 21:54:20 +0000828 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
829 // the 2nd colon makes cg_annotate's output look nicer.
830 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
831 "desc: D1 cache: %s\n"
832 "desc: L2 cache: %s\n",
833 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000834 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000835
nethercote9313ac42004-07-06 21:54:20 +0000836 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000837 VG_(strcpy)(buf, "cmd:");
838 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000839 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000840 if (VG_(client_argv)[i] == NULL)
841 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000842 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000843 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000844 }
nethercote9313ac42004-07-06 21:54:20 +0000845 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000846 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
847 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
848
nethercote9313ac42004-07-06 21:54:20 +0000849 // Six loops here: three for the hash table arrays, and three for the
850 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000851 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000852 curr_fileCC = CC_table[i];
853 while (curr_fileCC != NULL) {
854 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000855 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
856
857 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000858 curr_fnCC = curr_fileCC->fns[j];
859 while (curr_fnCC != NULL) {
860 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000861 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
862
nethercote9313ac42004-07-06 21:54:20 +0000863 for (k = 0; k < N_LINE_ENTRIES; k++) {
864 curr_lineCC = curr_fnCC->lines[k];
865 while (curr_lineCC != NULL) {
866 fprint_lineCC(fd, curr_lineCC);
867 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000868 }
869 }
nethercote9313ac42004-07-06 21:54:20 +0000870 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000871 }
872 }
nethercote9313ac42004-07-06 21:54:20 +0000873 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000874 }
875 }
876
nethercote9313ac42004-07-06 21:54:20 +0000877 // Summary stats must come after rest of table, since we calculate them
878 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000879 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000880 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000881 Ir_total.a, Ir_total.m1, Ir_total.m2,
882 Dr_total.a, Dr_total.m1, Dr_total.m2,
883 Dw_total.a, Dw_total.m1, Dw_total.m2);
884 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
885 VG_(close)(fd);
886}
887
njn607adfc2003-09-30 14:15:44 +0000888static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000889{
njn607adfc2003-09-30 14:15:44 +0000890 UInt w = 0;
891 while (n > 0) {
892 n = n / 10;
893 w++;
njn4f9c9342002-04-29 16:03:24 +0000894 }
njn607adfc2003-09-30 14:15:44 +0000895 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000896}
897
sewardj4f29ddf2002-05-03 22:29:04 +0000898static
daywalker8ad1a402003-09-18 01:15:32 +0000899void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000900{
901 int i, len, space;
902
daywalker8ad1a402003-09-18 01:15:32 +0000903 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000904 len = VG_(strlen)(buf);
905 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000906 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000907 i = len;
908
909 /* Right justify in field */
910 for ( ; i >= 0; i--) buf[i + space] = buf[i];
911 for (i = 0; i < space; i++) buf[i] = ' ';
912}
913
njn51d827b2005-05-09 01:02:08 +0000914static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000915{
nethercote9313ac42004-07-06 21:54:20 +0000916 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000917
njn4f9c9342002-04-29 16:03:24 +0000918 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000919 ULong L2_total_m, L2_total_mr, L2_total_mw,
920 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000921 Int l1, l2, l3;
922 Int p;
923
nethercote9313ac42004-07-06 21:54:20 +0000924 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000925
njn7cf0bd32002-06-08 13:36:03 +0000926 if (VG_(clo_verbosity) == 0)
927 return;
928
njn4f9c9342002-04-29 16:03:24 +0000929 /* I cache results. Use the I_refs value to determine the first column
930 * width. */
njn607adfc2003-09-30 14:15:44 +0000931 l1 = ULong_width(Ir_total.a);
932 l2 = ULong_width(Dr_total.a);
933 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000934
njn607adfc2003-09-30 14:15:44 +0000935 /* Make format string, getting width right for numbers */
936 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
937
938 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
939 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
940 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000941
942 p = 100;
943
njn25e49d8e72002-09-23 09:36:25 +0000944 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000945 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
946 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
947
948 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
949 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
950 VG_(message)(Vg_UserMsg, "");
951
952 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
953 * width of columns 2 & 3. */
954 D_total.a = Dr_total.a + Dw_total.a;
955 D_total.m1 = Dr_total.m1 + Dw_total.m1;
956 D_total.m2 = Dr_total.m2 + Dw_total.m2;
957
njn607adfc2003-09-30 14:15:44 +0000958 /* Make format string, getting width right for numbers */
959 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000960
njn607adfc2003-09-30 14:15:44 +0000961 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
962 D_total.a, Dr_total.a, Dw_total.a);
963 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
964 D_total.m1, Dr_total.m1, Dw_total.m1);
965 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
966 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000967
968 p = 10;
969
njn25e49d8e72002-09-23 09:36:25 +0000970 if (0 == D_total.a) D_total.a = 1;
971 if (0 == Dr_total.a) Dr_total.a = 1;
972 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000973 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
974 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
975 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
976 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
977
978 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
979 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
980 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
981 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
982 VG_(message)(Vg_UserMsg, "");
983
984 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000985
986 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
987 L2_total_r = Dr_total.m1 + Ir_total.m1;
988 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000989 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
990 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000991
njn4f9c9342002-04-29 16:03:24 +0000992 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
993 L2_total_mr = Dr_total.m2 + Ir_total.m2;
994 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +0000995 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
996 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +0000997
998 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
999 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
1000 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
1001 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1002
1003
nethercote9313ac42004-07-06 21:54:20 +00001004 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001005 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001006 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001007 file_line_debug_BBs + no_debug_BBs;
1008
1009 VG_(message)(Vg_DebugMsg, "");
1010 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1011 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001012 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1013 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001014 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1015 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1016 full_debug_BBs * 100 / BB_lookups,
1017 full_debug_BBs);
1018 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1019 file_line_debug_BBs * 100 / BB_lookups,
1020 file_line_debug_BBs);
1021 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001022 fn_debug_BBs * 100 / BB_lookups,
1023 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001024 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1025 no_debug_BBs * 100 / BB_lookups,
1026 no_debug_BBs);
1027 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001028 }
njn25e49d8e72002-09-23 09:36:25 +00001029 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001030}
1031
nethercote9313ac42004-07-06 21:54:20 +00001032/*--------------------------------------------------------------------*/
1033/*--- Discarding BB info ---*/
1034/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001035
nethercote9313ac42004-07-06 21:54:20 +00001036// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001037static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001038{
nethercote9313ac42004-07-06 21:54:20 +00001039 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001040 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001041
nethercote928a5f72004-11-03 18:10:37 +00001042 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001043
nethercote9313ac42004-07-06 21:54:20 +00001044 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001045 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1046 tl_assert(NULL != bbInfo);
1047 *prev_next_ptr = bbInfo->next;
1048 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001049}
1050
1051/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001052/*--- Command line processing ---*/
1053/*--------------------------------------------------------------------*/
1054
nethercote9313ac42004-07-06 21:54:20 +00001055static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001056{
nethercote9313ac42004-07-06 21:54:20 +00001057 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001058
nethercote9313ac42004-07-06 21:54:20 +00001059 // Option argument looks like "65536,2,64".
1060 // Find commas, replace with NULs to make three independent
1061 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001062 while (VG_(isdigit)(opt[i])) i++;
1063 if (',' == opt[i]) {
1064 opt[i++] = '\0';
1065 i2 = i;
1066 } else goto bad;
1067 while (VG_(isdigit)(opt[i])) i++;
1068 if (',' == opt[i]) {
1069 opt[i++] = '\0';
1070 i3 = i;
1071 } else goto bad;
1072 while (VG_(isdigit)(opt[i])) i++;
1073 if ('\0' != opt[i]) goto bad;
1074
nethercote9313ac42004-07-06 21:54:20 +00001075 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001076 cache->assoc = (Int)VG_(atoll)(opt + i2);
1077 cache->line_size = (Int)VG_(atoll)(opt + i3);
1078
nethercote9313ac42004-07-06 21:54:20 +00001079 opt[i2-1] = ',';
1080 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001081 return;
1082
1083 bad:
nethercote9313ac42004-07-06 21:54:20 +00001084 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001085}
1086
njn51d827b2005-05-09 01:02:08 +00001087static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001088{
nethercote9313ac42004-07-06 21:54:20 +00001089 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001090 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001091 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001092 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001093 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001094 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001095 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001096 else
1097 return False;
1098
1099 return True;
1100}
1101
njn51d827b2005-05-09 01:02:08 +00001102static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001103{
njn3e884182003-04-15 13:03:23 +00001104 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001105" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1106" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001107" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1108 );
1109}
1110
njn51d827b2005-05-09 01:02:08 +00001111static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001112{
1113 VG_(printf)(
1114" (none)\n"
1115 );
njn25e49d8e72002-09-23 09:36:25 +00001116}
1117
1118/*--------------------------------------------------------------------*/
1119/*--- Setup ---*/
1120/*--------------------------------------------------------------------*/
1121
njn51d827b2005-05-09 01:02:08 +00001122static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001123{
1124 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001125
nethercoteb35a8b92004-09-11 16:45:27 +00001126 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001127
1128 cachesim_I1_initcache(I1c);
1129 cachesim_D1_initcache(D1c);
1130 cachesim_L2_initcache(L2c);
1131
njn31066fd2005-03-26 00:42:02 +00001132 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1133 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1134 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001135}
1136
njn51d827b2005-05-09 01:02:08 +00001137static void cg_pre_clo_init(void)
1138{
1139 Char* base_dir = NULL;
1140
1141 VG_(details_name) ("Cachegrind");
1142 VG_(details_version) (NULL);
1143 VG_(details_description) ("an I1/D1/L2 cache profiler");
1144 VG_(details_copyright_author)(
1145 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1146 VG_(details_bug_reports_to) (VG_BUGS_TO);
1147 VG_(details_avg_translation_sizeB) ( 155 );
1148
1149 VG_(basic_tool_funcs) (cg_post_clo_init,
1150 cg_instrument,
1151 cg_fini);
1152
1153 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1154 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1155 cg_print_usage,
1156 cg_print_debug_usage);
1157
1158 /* Get working directory */
1159 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1160
1161 /* Block is big enough for dir name + cachegrind.out.<pid> */
1162 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1163 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1164 base_dir, VG_(getpid)());
1165 VG_(free)(base_dir);
1166
1167 instr_info_table = VG_(HT_construct)();
1168}
1169
1170VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001171
njn25e49d8e72002-09-23 09:36:25 +00001172/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001173/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001174/*--------------------------------------------------------------------*/