blob: a7bfd6b2d02c9b811e42cb1cf9384b3dca11fde5 [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"
njnea27e462005-05-31 02:38:09 +000033#include "pub_tool_debuginfo.h"
njn81c00df2005-05-14 21:28:43 +000034#include "pub_tool_hashtable.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_tool_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_tool_libcassert.h"
njn36a20fa2005-06-03 03:08:39 +000037#include "pub_tool_libcprint.h"
njn717cde52005-05-10 02:47:21 +000038#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +000039#include "pub_tool_options.h"
njn31513b42005-06-01 03:09:59 +000040#include "pub_tool_profile.h"
njn43b9a8a2005-05-10 04:37:01 +000041#include "pub_tool_tooliface.h"
njn25e49d8e72002-09-23 09:36:25 +000042
nethercoteb35a8b92004-09-11 16:45:27 +000043#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000044#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000045
njn25e49d8e72002-09-23 09:36:25 +000046/*------------------------------------------------------------*/
47/*--- Constants ---*/
48/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000049
nethercote9313ac42004-07-06 21:54:20 +000050#define MIN_LINE_SIZE 16
51#define FILE_LEN 256
52#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000053
54/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000055/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000056/*------------------------------------------------------------*/
57
njn25e49d8e72002-09-23 09:36:25 +000058typedef
59 enum {
nethercote9313ac42004-07-06 21:54:20 +000060 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000061 VgpCacheSimulate,
62 VgpCacheResults
63 }
nethercote7cc9c232004-01-21 15:08:04 +000064 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000065
njn4f9c9342002-04-29 16:03:24 +000066/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000067/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000068/*------------------------------------------------------------*/
69
70typedef struct _CC CC;
71struct _CC {
72 ULong a;
73 ULong m1;
74 ULong m2;
75};
76
nethercote9313ac42004-07-06 21:54:20 +000077//------------------------------------------------------------
78// Primary data structure #1: CC table
79// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
80// - hash(file, hash(fn, hash(line+CC)))
81// - Each hash table is separately chained.
82// - The array sizes below work fairly well for Konqueror.
83// - Lookups done by instr_addr, which is converted immediately to a source
84// location.
85// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000086
87#define N_FILE_ENTRIES 251
88#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000089#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000090
nethercote9313ac42004-07-06 21:54:20 +000091typedef struct _lineCC lineCC;
92struct _lineCC {
93 Int line;
94 CC Ir;
95 CC Dr;
96 CC Dw;
97 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +000098};
99
nethercote9313ac42004-07-06 21:54:20 +0000100typedef struct _fnCC fnCC;
101struct _fnCC {
102 Char* fn;
103 fnCC* next;
104 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000105};
106
nethercote9313ac42004-07-06 21:54:20 +0000107typedef struct _fileCC fileCC;
108struct _fileCC {
109 Char* file;
110 fileCC* next;
111 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000112};
113
nethercote9313ac42004-07-06 21:54:20 +0000114// Top level of CC table. Auto-zeroed.
115static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000116
nethercote9313ac42004-07-06 21:54:20 +0000117//------------------------------------------------------------
118// Primary data structre #2: Instr-info table
119// - Holds the cached info about each instr that is used for simulation.
120// - table(BB_start_addr, list(instr_info))
121// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000122// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000123// CC. This node is what's passed to the simulation function.
124// - When BBs are discarded the relevant list(instr_details) is freed.
125
126typedef struct _instr_info instr_info;
127struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000128 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000129 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000130 UChar data_size;
131 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000132};
133
134typedef struct _BB_info BB_info;
135struct _BB_info {
136 BB_info* next; // next field
137 Addr BB_addr; // key
138 Int n_instrs;
139 instr_info instrs[0];
140};
141
142VgHashTable instr_info_table; // hash(Addr, BB_info)
143
144//------------------------------------------------------------
145// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000146static Int distinct_files = 0;
147static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000148static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000149static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000150
sewardj4f29ddf2002-05-03 22:29:04 +0000151static Int full_debug_BBs = 0;
152static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000153static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000154static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000155
sewardj4f29ddf2002-05-03 22:29:04 +0000156static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000157
nethercote9313ac42004-07-06 21:54:20 +0000158/*------------------------------------------------------------*/
159/*--- CC table operations ---*/
160/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000161
nethercote9313ac42004-07-06 21:54:20 +0000162static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
163 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000164{
nethercote9313ac42004-07-06 21:54:20 +0000165 Bool found_file_line = VG_(get_filename_linenum)(instr_addr, file,
166 FILE_LEN, line);
167 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000168
nethercote9313ac42004-07-06 21:54:20 +0000169 if (!found_file_line) {
170 VG_(strcpy)(file, "???");
171 *line = 0;
172 }
173 if (!found_fn) {
174 VG_(strcpy)(fn, "???");
175 }
176 if (found_file_line) {
177 if (found_fn) full_debug_BBs++;
178 else file_line_debug_BBs++;
179 } else {
180 if (found_fn) fn_debug_BBs++;
181 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000182 }
183}
184
njn4f9c9342002-04-29 16:03:24 +0000185static UInt hash(Char *s, UInt table_size)
186{
nethercote9313ac42004-07-06 21:54:20 +0000187 const int hash_constant = 256;
188 int hash_value = 0;
189 for ( ; *s; s++)
190 hash_value = (hash_constant * hash_value + *s) % table_size;
191 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000192}
193
nethercote9313ac42004-07-06 21:54:20 +0000194static __inline__
195fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000196{
nethercote9313ac42004-07-06 21:54:20 +0000197 // Using calloc() zeroes the fns[] array
198 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
199 cc->file = VG_(strdup)(filename);
200 cc->next = next;
201 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000202}
203
nethercote9313ac42004-07-06 21:54:20 +0000204static __inline__
205fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000206{
nethercote9313ac42004-07-06 21:54:20 +0000207 // Using calloc() zeroes the lines[] array
208 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
209 cc->fn = VG_(strdup)(fn);
210 cc->next = next;
211 return cc;
212}
njn4f9c9342002-04-29 16:03:24 +0000213
nethercote9313ac42004-07-06 21:54:20 +0000214static __inline__
215lineCC* new_lineCC(Int line, lineCC* next)
216{
217 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
218 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
219 cc->line = line;
220 cc->next = next;
221 return cc;
222}
njn4f9c9342002-04-29 16:03:24 +0000223
nethercote9313ac42004-07-06 21:54:20 +0000224static __inline__
225instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
226{
njn6a3009b2005-03-20 00:20:06 +0000227 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000228 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
229 ii->instr_addr = instr_addr;
230 ii->parent = parent;
231 return ii;
232}
233
234// Do a three step traversal: by file, then fn, then line.
235// In all cases prepends new nodes to their chain. Returns a pointer to the
236// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000237static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000238{
239 fileCC *curr_fileCC;
240 fnCC *curr_fnCC;
241 lineCC *curr_lineCC;
242 Char file[FILE_LEN], fn[FN_LEN];
243 Int line;
244 UInt file_hash, fn_hash, line_hash;
245
njn6a3009b2005-03-20 00:20:06 +0000246 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000247
248 VGP_PUSHCC(VgpGetLineCC);
249
250 // level 1
251 file_hash = hash(file, N_FILE_ENTRIES);
252 curr_fileCC = CC_table[file_hash];
253 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
254 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000255 }
nethercote9313ac42004-07-06 21:54:20 +0000256 if (NULL == curr_fileCC) {
257 CC_table[file_hash] = curr_fileCC =
258 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000259 distinct_files++;
260 }
261
nethercote9313ac42004-07-06 21:54:20 +0000262 // level 2
263 fn_hash = hash(fn, N_FN_ENTRIES);
264 curr_fnCC = curr_fileCC->fns[fn_hash];
265 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
266 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000267 }
nethercote9313ac42004-07-06 21:54:20 +0000268 if (NULL == curr_fnCC) {
269 curr_fileCC->fns[fn_hash] = curr_fnCC =
270 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000271 distinct_fns++;
272 }
273
nethercote9313ac42004-07-06 21:54:20 +0000274 // level 3
275 line_hash = line % N_LINE_ENTRIES;
276 curr_lineCC = curr_fnCC->lines[line_hash];
277 while (NULL != curr_lineCC && line != curr_lineCC->line) {
278 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000279 }
nethercote9313ac42004-07-06 21:54:20 +0000280 if (NULL == curr_lineCC) {
281 curr_fnCC->lines[line_hash] = curr_lineCC =
282 new_lineCC(line, curr_fnCC->lines[line_hash]);
283 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000284 }
nethercote9313ac42004-07-06 21:54:20 +0000285
286 VGP_POPCC(VgpGetLineCC);
287 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000288}
289
290/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000291/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000292/*------------------------------------------------------------*/
293
njn9fb73db2005-03-27 01:55:21 +0000294static VGA_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000295void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000296{
njn6a3009b2005-03-20 00:20:06 +0000297 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
298 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000299 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000300 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000301 &n->parent->Ir.m1, &n->parent->Ir.m2);
302 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000303 VGP_POPCC(VgpCacheSimulate);
304}
305
njn9fb73db2005-03-27 01:55:21 +0000306static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000307void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000308{
nethercote9313ac42004-07-06 21:54:20 +0000309 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000310 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000311 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000312 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000313 &n->parent->Ir.m1, &n->parent->Ir.m2);
314 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000315
nethercote9313ac42004-07-06 21:54:20 +0000316 cachesim_D1_doref(data_addr, n->data_size,
317 &n->parent->Dr.m1, &n->parent->Dr.m2);
318 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000319 VGP_POPCC(VgpCacheSimulate);
320}
321
njn9fb73db2005-03-27 01:55:21 +0000322static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000323void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000324{
nethercote9313ac42004-07-06 21:54:20 +0000325 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000326 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000327 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000328 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000329 &n->parent->Ir.m1, &n->parent->Ir.m2);
330 n->parent->Ir.a++;
331
332 cachesim_D1_doref(data_addr, n->data_size,
333 &n->parent->Dw.m1, &n->parent->Dw.m2);
334 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000335 VGP_POPCC(VgpCacheSimulate);
336}
337
njn9fb73db2005-03-27 01:55:21 +0000338static VGA_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000339void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000340{
341 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000342 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000343 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000344 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000345 &n->parent->Ir.m1, &n->parent->Ir.m2);
346 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000347
nethercote9313ac42004-07-06 21:54:20 +0000348 cachesim_D1_doref(data_addr1, n->data_size,
349 &n->parent->Dr.m1, &n->parent->Dr.m2);
350 n->parent->Dr.a++;
351 cachesim_D1_doref(data_addr2, n->data_size,
352 &n->parent->Dw.m1, &n->parent->Dw.m2);
353 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000354 VGP_POPCC(VgpCacheSimulate);
355}
356
nethercote9313ac42004-07-06 21:54:20 +0000357/*------------------------------------------------------------*/
358/*--- Instrumentation ---*/
359/*------------------------------------------------------------*/
360
nethercote564b2b02004-08-07 15:54:53 +0000361static
njn6a3009b2005-03-20 00:20:06 +0000362BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000363{
364 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000365 IRStmt* st;
366 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000367 VgHashNode** dummy;
368
njn6a3009b2005-03-20 00:20:06 +0000369 // Count number of original instrs in BB
370 n_instrs = 0;
371 for (i = 0; i < bbIn->stmts_used; i++) {
372 st = bbIn->stmts[i];
373 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000374 }
375
376 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000377 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
378 *bbSeenBefore = ( NULL == bbInfo ? False : True );
379 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000380 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000381 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000382 BB_retranslations++;
383 } else {
384 // BB never translated before (at this address, at least; could have
385 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000386 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
387 bbInfo->BB_addr = origAddr;
388 bbInfo->n_instrs = n_instrs;
389 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000390 distinct_instrs++;
391 }
njn6a3009b2005-03-20 00:20:06 +0000392 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000393}
njn6a3009b2005-03-20 00:20:06 +0000394
njn6a3009b2005-03-20 00:20:06 +0000395static
396void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
397 Addr* instrAddr, UInt* instrLen,
398 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
399 UInt* dataSize)
400{
sewardj7f4a8622005-03-26 21:55:21 +0000401 tl_assert(isFlatIRStmt(st));
402
njn6a3009b2005-03-20 00:20:06 +0000403 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000404 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000405 break;
406
sewardje6da2fa2005-05-12 17:57:14 +0000407 case Ist_AbiHint:
408 /* ABI hints aren't interesting to cachegrind. Ignore. */
409 break;
410
njn6a3009b2005-03-20 00:20:06 +0000411 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000412 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
413 to the host's native pointer type; if that is 32 bits then it
414 discards the upper 32 bits. If we are cachegrinding on a
415 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000416 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000417 host and guest word sizes must be the same. Hence
418 st->Ist.IMark.addr will have been derived from a 32-bit guest
419 code address and truncation of it is safe. I believe this
420 assignment should be correct for both 32- and 64-bit
421 machines. */
422 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000423 *instrLen = st->Ist.IMark.len;
424 addStmtToIRBB( bbOut, st );
425 break;
426
427 case Ist_Tmp: {
428 IRExpr* data = st->Ist.Tmp.data;
429 if (data->tag == Iex_LDle) {
430 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000431 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000432
433 // XXX: repe cmpsb does two loads... the first one is ignored here!
434 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
435 *loadAddrExpr = aexpr;
436 *dataSize = sizeofIRType(data->Iex.LDle.ty);
437 }
438 addStmtToIRBB( bbOut, st );
439 break;
440 }
441
442 case Ist_STle: {
443 IRExpr* data = st->Ist.STle.data;
444 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000445 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000446 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
447 *storeAddrExpr = aexpr;
448 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
449 addStmtToIRBB( bbOut, st );
450 break;
451 }
452
sewardj7f4a8622005-03-26 21:55:21 +0000453 case Ist_Dirty: {
454 IRDirty* d = st->Ist.Dirty.details;
455 if (d->mFx != Ifx_None) {
456 /* This dirty helper accesses memory. Collect the
457 details. */
458 tl_assert(d->mAddr != NULL);
459 tl_assert(d->mSize != 0);
460 *dataSize = d->mSize;
461 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
462 *loadAddrExpr = d->mAddr;
463 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
464 *storeAddrExpr = d->mAddr;
465 } else {
466 tl_assert(d->mAddr == NULL);
467 tl_assert(d->mSize == 0);
468 }
469 addStmtToIRBB( bbOut, st );
470 break;
471 }
472
njn6a3009b2005-03-20 00:20:06 +0000473 case Ist_Put:
474 case Ist_PutI:
475 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000476 case Ist_MFence:
477 addStmtToIRBB( bbOut, st );
478 break;
479
480 default:
481 VG_(printf)("\n");
482 ppIRStmt(st);
483 VG_(printf)("\n");
484 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
485 }
486}
nethercote9313ac42004-07-06 21:54:20 +0000487
nethercote564b2b02004-08-07 15:54:53 +0000488static
njn6a3009b2005-03-20 00:20:06 +0000489void do_details( instr_info* n, Bool bbSeenBefore,
490 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000491{
njn6a3009b2005-03-20 00:20:06 +0000492 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000493 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000494 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000495 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000496 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000497 // the debug info might change; the other asserts should be enough to
498 // detect anything strange.
499 } else {
njn6a3009b2005-03-20 00:20:06 +0000500 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000501 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000502 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000503 n->data_size = data_size;
504 n->parent = parent;
505 }
506}
507
njn6a3009b2005-03-20 00:20:06 +0000508static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000509{
njn6a3009b2005-03-20 00:20:06 +0000510 // I'm assuming that for 'modify' instructions, that Vex always makes
511 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
512 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000513 tl_assert(isIRAtom(loadAddrExpr));
514 tl_assert(isIRAtom(storeAddrExpr));
515 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000516}
517
518// Instrumentation for the end of each original instruction.
519static
520void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
521 UInt instrAddr, UInt instrLen, UInt dataSize,
522 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
523{
524 IRDirty* di;
525 IRExpr *arg1, *arg2, *arg3, **argv;
526 Int argc;
527 Char* helperName;
528 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000529 IRType wordTy;
530
531 // Stay sane ...
532 tl_assert(sizeof(HWord) == sizeof(void*));
533 if (sizeof(HWord) == 4) {
534 wordTy = Ity_I32;
535 } else
536 if (sizeof(HWord) == 8) {
537 wordTy = Ity_I64;
538 } else {
539 VG_(tool_panic)("endOfInstr: strange word size");
540 }
541
542 if (loadAddrExpr)
543 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
544 if (storeAddrExpr)
545 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
546
njn6a3009b2005-03-20 00:20:06 +0000547
548 // Nb: instrLen will be zero if Vex failed to decode it.
549 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000550 (instrLen >= VGA_MIN_INSTR_SZB &&
551 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000552
njn016712a2005-04-04 02:52:16 +0000553 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
554 // done inaccurately, but they're very rare and this avoids errors from
555 // hitting more than two cache lines in the simulation.
556 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
557
njn6a3009b2005-03-20 00:20:06 +0000558 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000559 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000560 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000561 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000562
563 if (!loadAddrExpr && !storeAddrExpr) {
564 // no load/store
565 tl_assert(0 == dataSize);
566 helperName = "log_1I_0D_cache_access";
567 helperAddr = &log_1I_0D_cache_access;
568 argc = 1;
569 argv = mkIRExprVec_1(arg1);
570
571 } else if (loadAddrExpr && !storeAddrExpr) {
572 // load
sewardj710d6c22005-03-20 18:55:15 +0000573 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000574 helperName = "log_1I_1Dr_cache_access";
575 helperAddr = &log_1I_1Dr_cache_access;
576 argc = 2;
577 arg2 = loadAddrExpr;
578 argv = mkIRExprVec_2(arg1, arg2);
579
580 } else if (!loadAddrExpr && storeAddrExpr) {
581 // store
sewardj710d6c22005-03-20 18:55:15 +0000582 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000583 helperName = "log_1I_1Dw_cache_access";
584 helperAddr = &log_1I_1Dw_cache_access;
585 argc = 2;
586 arg2 = storeAddrExpr;
587 argv = mkIRExprVec_2(arg1, arg2);
588
589 } else {
590 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000591 tl_assert( isIRAtom(loadAddrExpr) );
592 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000593
594 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
595 // modify
596 helperName = "log_1I_1Dr_cache_access";
597 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000598 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000599 arg2 = loadAddrExpr;
600 argv = mkIRExprVec_2(arg1, arg2);
601
nethercote9313ac42004-07-06 21:54:20 +0000602 } else {
njn6a3009b2005-03-20 00:20:06 +0000603 // load/store
604 helperName = "log_1I_2D_cache_access";
605 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000606 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000607 arg2 = loadAddrExpr;
608 arg3 = storeAddrExpr;
609 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000610 }
611 }
612
njn6a3009b2005-03-20 00:20:06 +0000613 // Add call to the instrumentation function
614 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
615 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000616}
njn14d01ce2004-11-26 11:30:14 +0000617
njn51d827b2005-05-09 01:02:08 +0000618static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
619 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000620{
njn6a3009b2005-03-20 00:20:06 +0000621 Int i, dataSize = 0, bbInfo_i;
622 IRBB* bbOut;
623 IRStmt* st;
624 BB_info* bbInfo;
625 Bool bbSeenBefore = False;
626 Addr instrAddr, origAddr;
627 UInt instrLen;
628 IRExpr *loadAddrExpr, *storeAddrExpr;
629
sewardjd54babf2005-03-21 00:55:49 +0000630 if (gWordTy != hWordTy) {
631 /* We don't currently support this case. */
632 VG_(tool_panic)("host/guest word size mismatch");
633 }
634
njn6a3009b2005-03-20 00:20:06 +0000635 /* Set up BB */
636 bbOut = emptyIRBB();
637 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
638 bbOut->next = dopyIRExpr(bbIn->next);
639 bbOut->jumpkind = bbIn->jumpkind;
640
641 // Get the first statement, and origAddr from it
642 i = 0;
643 tl_assert(bbIn->stmts_used > 0);
644 st = bbIn->stmts[0];
645 tl_assert(Ist_IMark == st->tag);
646 origAddr = (Addr)st->Ist.IMark.addr;
647 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
648
649 // Get block info
650 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
651 bbInfo_i = 0;
652
653 do {
654 // We should be at an IMark statement
655 tl_assert(Ist_IMark == st->tag);
656
657 // Reset stuff for this original instruction
658 loadAddrExpr = storeAddrExpr = NULL;
659 dataSize = 0;
660
661 // Process all the statements for this original instruction (ie. until
662 // the next IMark statement, or the end of the block)
663 do {
664 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
665 &loadAddrExpr, &storeAddrExpr, &dataSize);
666 i++;
667 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
668 }
669 while (st && Ist_IMark != st->tag);
670
671 // Add instrumentation for this original instruction.
672 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
673 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
674
675 bbInfo_i++;
676 }
677 while (st);
678
679 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000680}
njn4f9c9342002-04-29 16:03:24 +0000681
682/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000683/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000684/*------------------------------------------------------------*/
685
sewardjb5f6f512005-03-10 23:59:00 +0000686#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000687
688static cache_t clo_I1_cache = UNDEFINED_CACHE;
689static cache_t clo_D1_cache = UNDEFINED_CACHE;
690static cache_t clo_L2_cache = UNDEFINED_CACHE;
691
njn7cf0bd32002-06-08 13:36:03 +0000692/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000693static
njna1d1a642004-11-26 18:36:02 +0000694void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000695{
696 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000697 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000698 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000699 "error: %s size of %dB not a power of two; aborting.",
700 name, cache->size);
701 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000702 }
703
sewardj07133bf2002-06-13 10:25:56 +0000704 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000705 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000706 "error: %s associativity of %d not a power of two; aborting.",
707 name, cache->assoc);
708 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000709 }
710
sewardj07133bf2002-06-13 10:25:56 +0000711 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000712 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000713 "error: %s line size of %dB not a power of two; aborting.",
714 name, cache->line_size);
715 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000716 }
717
njn6a3009b2005-03-20 00:20:06 +0000718 // Then check line size >= 16 -- any smaller and a single instruction could
719 // straddle three cache lines, which breaks a simulation assertion and is
720 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000721 if (cache->line_size < MIN_LINE_SIZE) {
722 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000723 "error: %s line size of %dB too small; aborting.",
724 name, cache->line_size);
725 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000726 }
727
728 /* Then check cache size > line size (causes seg faults if not). */
729 if (cache->size <= cache->line_size) {
730 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000731 "error: %s cache size of %dB <= line size of %dB; aborting.",
732 name, cache->size, cache->line_size);
733 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000734 }
735
736 /* Then check assoc <= (size / line size) (seg faults otherwise). */
737 if (cache->assoc > (cache->size / cache->line_size)) {
738 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000739 "warning: %s associativity > (size / line size); aborting.", name);
740 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000741 }
742}
743
sewardj07133bf2002-06-13 10:25:56 +0000744static
nethercoteb35a8b92004-09-11 16:45:27 +0000745void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000746{
nethercote9313ac42004-07-06 21:54:20 +0000747#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
748
nethercoteb35a8b92004-09-11 16:45:27 +0000749 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000750
nethercoteb35a8b92004-09-11 16:45:27 +0000751 // Count how many were defined on the command line.
752 if (DEFINED(clo_I1_cache)) { n_clos++; }
753 if (DEFINED(clo_D1_cache)) { n_clos++; }
754 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000755
njna1d1a642004-11-26 18:36:02 +0000756 // Set the cache config (using auto-detection, if supported by the
757 // architecture)
758 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000759
nethercote9313ac42004-07-06 21:54:20 +0000760 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000761 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
762 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
763 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000764
nethercote9313ac42004-07-06 21:54:20 +0000765 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000766 check_cache(I1c, "I1");
767 check_cache(D1c, "D1");
768 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000769
770 if (VG_(clo_verbosity) > 1) {
771 VG_(message)(Vg_UserMsg, "Cache configuration used:");
772 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
773 I1c->size, I1c->assoc, I1c->line_size);
774 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
775 D1c->size, D1c->assoc, D1c->line_size);
776 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
777 L2c->size, L2c->assoc, L2c->line_size);
778 }
nethercote9313ac42004-07-06 21:54:20 +0000779#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000780}
781
njn4f9c9342002-04-29 16:03:24 +0000782/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000783/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000784/*------------------------------------------------------------*/
785
nethercote9313ac42004-07-06 21:54:20 +0000786// Total reads/writes/misses. Calculated during CC traversal at the end.
787// All auto-zeroed.
788static CC Ir_total;
789static CC Dr_total;
790static CC Dw_total;
791
792static Char* cachegrind_out_file;
793
nethercote9313ac42004-07-06 21:54:20 +0000794static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000795{
nethercote9313ac42004-07-06 21:54:20 +0000796 Char buf[512];
797 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
798 n->line,
799 n->Ir.a, n->Ir.m1, n->Ir.m2,
800 n->Dr.a, n->Dr.m1, n->Dr.m2,
801 n->Dw.a, n->Dw.m1, n->Dw.m2);
802 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
803
804 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
805 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
806 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
807}
808
809static void fprint_CC_table_and_calc_totals(void)
810{
811 Int fd;
812 Char buf[512];
813 fileCC *curr_fileCC;
814 fnCC *curr_fnCC;
815 lineCC *curr_lineCC;
816 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000817
njn25e49d8e72002-09-23 09:36:25 +0000818 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000819
njndb918dd2003-07-22 20:45:11 +0000820 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000821 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000822 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000823 // If the file can't be opened for whatever reason (conflict
824 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000825 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000826 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000827 cachegrind_out_file );
828 VG_(message)(Vg_UserMsg,
829 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000830 return;
831 }
njn4f9c9342002-04-29 16:03:24 +0000832
nethercote9313ac42004-07-06 21:54:20 +0000833 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
834 // the 2nd colon makes cg_annotate's output look nicer.
835 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
836 "desc: D1 cache: %s\n"
837 "desc: L2 cache: %s\n",
838 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000839 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000840
nethercote9313ac42004-07-06 21:54:20 +0000841 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000842 VG_(strcpy)(buf, "cmd:");
843 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000844 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000845 if (VG_(client_argv)[i] == NULL)
846 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000847 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000848 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000849 }
nethercote9313ac42004-07-06 21:54:20 +0000850 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000851 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
852 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
853
nethercote9313ac42004-07-06 21:54:20 +0000854 // Six loops here: three for the hash table arrays, and three for the
855 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000856 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000857 curr_fileCC = CC_table[i];
858 while (curr_fileCC != NULL) {
859 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000860 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
861
862 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000863 curr_fnCC = curr_fileCC->fns[j];
864 while (curr_fnCC != NULL) {
865 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000866 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
867
nethercote9313ac42004-07-06 21:54:20 +0000868 for (k = 0; k < N_LINE_ENTRIES; k++) {
869 curr_lineCC = curr_fnCC->lines[k];
870 while (curr_lineCC != NULL) {
871 fprint_lineCC(fd, curr_lineCC);
872 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000873 }
874 }
nethercote9313ac42004-07-06 21:54:20 +0000875 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000876 }
877 }
nethercote9313ac42004-07-06 21:54:20 +0000878 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000879 }
880 }
881
nethercote9313ac42004-07-06 21:54:20 +0000882 // Summary stats must come after rest of table, since we calculate them
883 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000884 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000885 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000886 Ir_total.a, Ir_total.m1, Ir_total.m2,
887 Dr_total.a, Dr_total.m1, Dr_total.m2,
888 Dw_total.a, Dw_total.m1, Dw_total.m2);
889 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
890 VG_(close)(fd);
891}
892
njn607adfc2003-09-30 14:15:44 +0000893static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000894{
njn607adfc2003-09-30 14:15:44 +0000895 UInt w = 0;
896 while (n > 0) {
897 n = n / 10;
898 w++;
njn4f9c9342002-04-29 16:03:24 +0000899 }
njn607adfc2003-09-30 14:15:44 +0000900 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000901}
902
sewardj4f29ddf2002-05-03 22:29:04 +0000903static
daywalker8ad1a402003-09-18 01:15:32 +0000904void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000905{
906 int i, len, space;
907
daywalker8ad1a402003-09-18 01:15:32 +0000908 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000909 len = VG_(strlen)(buf);
910 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000911 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000912 i = len;
913
914 /* Right justify in field */
915 for ( ; i >= 0; i--) buf[i + space] = buf[i];
916 for (i = 0; i < space; i++) buf[i] = ' ';
917}
918
njn51d827b2005-05-09 01:02:08 +0000919static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000920{
nethercote9313ac42004-07-06 21:54:20 +0000921 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000922
njn4f9c9342002-04-29 16:03:24 +0000923 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000924 ULong L2_total_m, L2_total_mr, L2_total_mw,
925 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000926 Int l1, l2, l3;
927 Int p;
928
nethercote9313ac42004-07-06 21:54:20 +0000929 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000930
njn7cf0bd32002-06-08 13:36:03 +0000931 if (VG_(clo_verbosity) == 0)
932 return;
933
njn4f9c9342002-04-29 16:03:24 +0000934 /* I cache results. Use the I_refs value to determine the first column
935 * width. */
njn607adfc2003-09-30 14:15:44 +0000936 l1 = ULong_width(Ir_total.a);
937 l2 = ULong_width(Dr_total.a);
938 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000939
njn607adfc2003-09-30 14:15:44 +0000940 /* Make format string, getting width right for numbers */
941 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
942
943 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
944 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
945 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000946
947 p = 100;
948
njn25e49d8e72002-09-23 09:36:25 +0000949 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000950 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
951 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
952
953 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
954 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
955 VG_(message)(Vg_UserMsg, "");
956
957 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
958 * width of columns 2 & 3. */
959 D_total.a = Dr_total.a + Dw_total.a;
960 D_total.m1 = Dr_total.m1 + Dw_total.m1;
961 D_total.m2 = Dr_total.m2 + Dw_total.m2;
962
njn607adfc2003-09-30 14:15:44 +0000963 /* Make format string, getting width right for numbers */
964 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000965
njn607adfc2003-09-30 14:15:44 +0000966 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
967 D_total.a, Dr_total.a, Dw_total.a);
968 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
969 D_total.m1, Dr_total.m1, Dw_total.m1);
970 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
971 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000972
973 p = 10;
974
njn25e49d8e72002-09-23 09:36:25 +0000975 if (0 == D_total.a) D_total.a = 1;
976 if (0 == Dr_total.a) Dr_total.a = 1;
977 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000978 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
979 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
980 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
981 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
982
983 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
984 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
985 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
986 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
987 VG_(message)(Vg_UserMsg, "");
988
989 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000990
991 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
992 L2_total_r = Dr_total.m1 + Ir_total.m1;
993 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000994 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
995 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000996
njn4f9c9342002-04-29 16:03:24 +0000997 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
998 L2_total_mr = Dr_total.m2 + Ir_total.m2;
999 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +00001000 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
1001 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +00001002
1003 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
1004 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
1005 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
1006 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1007
1008
nethercote9313ac42004-07-06 21:54:20 +00001009 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001010 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001011 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001012 file_line_debug_BBs + no_debug_BBs;
1013
1014 VG_(message)(Vg_DebugMsg, "");
1015 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1016 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001017 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1018 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001019 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1020 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1021 full_debug_BBs * 100 / BB_lookups,
1022 full_debug_BBs);
1023 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1024 file_line_debug_BBs * 100 / BB_lookups,
1025 file_line_debug_BBs);
1026 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001027 fn_debug_BBs * 100 / BB_lookups,
1028 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001029 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1030 no_debug_BBs * 100 / BB_lookups,
1031 no_debug_BBs);
1032 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001033 }
njn25e49d8e72002-09-23 09:36:25 +00001034 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001035}
1036
nethercote9313ac42004-07-06 21:54:20 +00001037/*--------------------------------------------------------------------*/
1038/*--- Discarding BB info ---*/
1039/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001040
nethercote9313ac42004-07-06 21:54:20 +00001041// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001042static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001043{
nethercote9313ac42004-07-06 21:54:20 +00001044 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001045 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001046
nethercote928a5f72004-11-03 18:10:37 +00001047 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001048
nethercote9313ac42004-07-06 21:54:20 +00001049 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001050 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1051 tl_assert(NULL != bbInfo);
1052 *prev_next_ptr = bbInfo->next;
1053 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001054}
1055
1056/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001057/*--- Command line processing ---*/
1058/*--------------------------------------------------------------------*/
1059
nethercote9313ac42004-07-06 21:54:20 +00001060static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001061{
nethercote9313ac42004-07-06 21:54:20 +00001062 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001063
nethercote9313ac42004-07-06 21:54:20 +00001064 // Option argument looks like "65536,2,64".
1065 // Find commas, replace with NULs to make three independent
1066 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001067 while (VG_(isdigit)(opt[i])) i++;
1068 if (',' == opt[i]) {
1069 opt[i++] = '\0';
1070 i2 = i;
1071 } else goto bad;
1072 while (VG_(isdigit)(opt[i])) i++;
1073 if (',' == opt[i]) {
1074 opt[i++] = '\0';
1075 i3 = i;
1076 } else goto bad;
1077 while (VG_(isdigit)(opt[i])) i++;
1078 if ('\0' != opt[i]) goto bad;
1079
nethercote9313ac42004-07-06 21:54:20 +00001080 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001081 cache->assoc = (Int)VG_(atoll)(opt + i2);
1082 cache->line_size = (Int)VG_(atoll)(opt + i3);
1083
nethercote9313ac42004-07-06 21:54:20 +00001084 opt[i2-1] = ',';
1085 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001086 return;
1087
1088 bad:
nethercote9313ac42004-07-06 21:54:20 +00001089 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001090}
1091
njn51d827b2005-05-09 01:02:08 +00001092static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001093{
nethercote9313ac42004-07-06 21:54:20 +00001094 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001095 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001096 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001097 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001098 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001099 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001100 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001101 else
1102 return False;
1103
1104 return True;
1105}
1106
njn51d827b2005-05-09 01:02:08 +00001107static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001108{
njn3e884182003-04-15 13:03:23 +00001109 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001110" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1111" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001112" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1113 );
1114}
1115
njn51d827b2005-05-09 01:02:08 +00001116static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001117{
1118 VG_(printf)(
1119" (none)\n"
1120 );
njn25e49d8e72002-09-23 09:36:25 +00001121}
1122
1123/*--------------------------------------------------------------------*/
1124/*--- Setup ---*/
1125/*--------------------------------------------------------------------*/
1126
njn51d827b2005-05-09 01:02:08 +00001127static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001128{
1129 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001130
nethercoteb35a8b92004-09-11 16:45:27 +00001131 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001132
1133 cachesim_I1_initcache(I1c);
1134 cachesim_D1_initcache(D1c);
1135 cachesim_L2_initcache(L2c);
1136
njn31066fd2005-03-26 00:42:02 +00001137 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1138 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1139 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001140}
1141
njn51d827b2005-05-09 01:02:08 +00001142static void cg_pre_clo_init(void)
1143{
1144 Char* base_dir = NULL;
1145
1146 VG_(details_name) ("Cachegrind");
1147 VG_(details_version) (NULL);
1148 VG_(details_description) ("an I1/D1/L2 cache profiler");
1149 VG_(details_copyright_author)(
1150 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1151 VG_(details_bug_reports_to) (VG_BUGS_TO);
1152 VG_(details_avg_translation_sizeB) ( 155 );
1153
1154 VG_(basic_tool_funcs) (cg_post_clo_init,
1155 cg_instrument,
1156 cg_fini);
1157
1158 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1159 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1160 cg_print_usage,
1161 cg_print_debug_usage);
1162
1163 /* Get working directory */
1164 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1165
1166 /* Block is big enough for dir name + cachegrind.out.<pid> */
1167 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1168 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1169 base_dir, VG_(getpid)());
1170 VG_(free)(base_dir);
1171
1172 instr_info_table = VG_(HT_construct)();
1173}
1174
1175VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001176
njn25e49d8e72002-09-23 09:36:25 +00001177/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001178/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001179/*--------------------------------------------------------------------*/