blob: e273cf867641c8ed5a3d9f0108b3f6e2bb0be7dc [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
njnc7561b92005-06-19 01:24:32 +000032#include "pub_tool_basics.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"
njneb8896b2005-06-04 20:03:55 +000037#include "pub_tool_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +000038#include "pub_tool_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +000039#include "pub_tool_libcproc.h"
njnf536bbb2005-06-13 04:21:38 +000040#include "pub_tool_machine.h"
njn717cde52005-05-10 02:47:21 +000041#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +000042#include "pub_tool_options.h"
njn31513b42005-06-01 03:09:59 +000043#include "pub_tool_profile.h"
njn43b9a8a2005-05-10 04:37:01 +000044#include "pub_tool_tooliface.h"
njn25e49d8e72002-09-23 09:36:25 +000045
nethercoteb35a8b92004-09-11 16:45:27 +000046#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000047#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000048
njn25e49d8e72002-09-23 09:36:25 +000049/*------------------------------------------------------------*/
50/*--- Constants ---*/
51/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000052
nethercote9313ac42004-07-06 21:54:20 +000053#define MIN_LINE_SIZE 16
54#define FILE_LEN 256
55#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000056
57/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000058/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000059/*------------------------------------------------------------*/
60
njn25e49d8e72002-09-23 09:36:25 +000061typedef
62 enum {
nethercote9313ac42004-07-06 21:54:20 +000063 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000064 VgpCacheSimulate,
65 VgpCacheResults
66 }
nethercote7cc9c232004-01-21 15:08:04 +000067 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000068
njn4f9c9342002-04-29 16:03:24 +000069/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000070/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000071/*------------------------------------------------------------*/
72
73typedef struct _CC CC;
74struct _CC {
75 ULong a;
76 ULong m1;
77 ULong m2;
78};
79
nethercote9313ac42004-07-06 21:54:20 +000080//------------------------------------------------------------
81// Primary data structure #1: CC table
82// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
83// - hash(file, hash(fn, hash(line+CC)))
84// - Each hash table is separately chained.
85// - The array sizes below work fairly well for Konqueror.
86// - Lookups done by instr_addr, which is converted immediately to a source
87// location.
88// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000089
90#define N_FILE_ENTRIES 251
91#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000092#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000093
nethercote9313ac42004-07-06 21:54:20 +000094typedef struct _lineCC lineCC;
95struct _lineCC {
96 Int line;
97 CC Ir;
98 CC Dr;
99 CC Dw;
100 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +0000101};
102
nethercote9313ac42004-07-06 21:54:20 +0000103typedef struct _fnCC fnCC;
104struct _fnCC {
105 Char* fn;
106 fnCC* next;
107 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000108};
109
nethercote9313ac42004-07-06 21:54:20 +0000110typedef struct _fileCC fileCC;
111struct _fileCC {
112 Char* file;
113 fileCC* next;
114 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000115};
116
nethercote9313ac42004-07-06 21:54:20 +0000117// Top level of CC table. Auto-zeroed.
118static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000119
nethercote9313ac42004-07-06 21:54:20 +0000120//------------------------------------------------------------
njnf69f9452005-07-03 17:53:11 +0000121// Primary data structure #2: Instr-info table
nethercote9313ac42004-07-06 21:54:20 +0000122// - Holds the cached info about each instr that is used for simulation.
123// - table(BB_start_addr, list(instr_info))
124// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000125// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000126// CC. This node is what's passed to the simulation function.
127// - When BBs are discarded the relevant list(instr_details) is freed.
128
129typedef struct _instr_info instr_info;
130struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000131 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000132 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000133 UChar data_size;
134 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000135};
136
137typedef struct _BB_info BB_info;
138struct _BB_info {
139 BB_info* next; // next field
140 Addr BB_addr; // key
141 Int n_instrs;
142 instr_info instrs[0];
143};
144
145VgHashTable instr_info_table; // hash(Addr, BB_info)
146
147//------------------------------------------------------------
148// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000149static Int distinct_files = 0;
150static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000151static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000152static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000153
sewardj4f29ddf2002-05-03 22:29:04 +0000154static Int full_debug_BBs = 0;
155static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000156static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000157static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000158
sewardj4f29ddf2002-05-03 22:29:04 +0000159static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000160
nethercote9313ac42004-07-06 21:54:20 +0000161/*------------------------------------------------------------*/
162/*--- CC table operations ---*/
163/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000164
nethercote9313ac42004-07-06 21:54:20 +0000165static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
166 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000167{
sewardj7cee6f92005-06-13 17:39:06 +0000168 Bool found_file_line = VG_(get_filename_linenum)(
169 instr_addr,
170 file, FILE_LEN,
171 NULL, 0, NULL,
172 line
173 );
nethercote9313ac42004-07-06 21:54:20 +0000174 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000175
nethercote9313ac42004-07-06 21:54:20 +0000176 if (!found_file_line) {
177 VG_(strcpy)(file, "???");
178 *line = 0;
179 }
180 if (!found_fn) {
181 VG_(strcpy)(fn, "???");
182 }
183 if (found_file_line) {
184 if (found_fn) full_debug_BBs++;
185 else file_line_debug_BBs++;
186 } else {
187 if (found_fn) fn_debug_BBs++;
188 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000189 }
190}
191
njn4f9c9342002-04-29 16:03:24 +0000192static UInt hash(Char *s, UInt table_size)
193{
nethercote9313ac42004-07-06 21:54:20 +0000194 const int hash_constant = 256;
195 int hash_value = 0;
196 for ( ; *s; s++)
197 hash_value = (hash_constant * hash_value + *s) % table_size;
198 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000199}
200
nethercote9313ac42004-07-06 21:54:20 +0000201static __inline__
202fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000203{
nethercote9313ac42004-07-06 21:54:20 +0000204 // Using calloc() zeroes the fns[] array
205 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
206 cc->file = VG_(strdup)(filename);
207 cc->next = next;
208 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000209}
210
nethercote9313ac42004-07-06 21:54:20 +0000211static __inline__
212fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000213{
nethercote9313ac42004-07-06 21:54:20 +0000214 // Using calloc() zeroes the lines[] array
215 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
216 cc->fn = VG_(strdup)(fn);
217 cc->next = next;
218 return cc;
219}
njn4f9c9342002-04-29 16:03:24 +0000220
nethercote9313ac42004-07-06 21:54:20 +0000221static __inline__
222lineCC* new_lineCC(Int line, lineCC* next)
223{
224 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
225 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
226 cc->line = line;
227 cc->next = next;
228 return cc;
229}
njn4f9c9342002-04-29 16:03:24 +0000230
nethercote9313ac42004-07-06 21:54:20 +0000231// Do a three step traversal: by file, then fn, then line.
232// In all cases prepends new nodes to their chain. Returns a pointer to the
233// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000234static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000235{
236 fileCC *curr_fileCC;
237 fnCC *curr_fnCC;
238 lineCC *curr_lineCC;
239 Char file[FILE_LEN], fn[FN_LEN];
240 Int line;
241 UInt file_hash, fn_hash, line_hash;
242
njn6a3009b2005-03-20 00:20:06 +0000243 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000244
245 VGP_PUSHCC(VgpGetLineCC);
246
247 // level 1
248 file_hash = hash(file, N_FILE_ENTRIES);
249 curr_fileCC = CC_table[file_hash];
250 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
251 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000252 }
nethercote9313ac42004-07-06 21:54:20 +0000253 if (NULL == curr_fileCC) {
254 CC_table[file_hash] = curr_fileCC =
255 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000256 distinct_files++;
257 }
258
nethercote9313ac42004-07-06 21:54:20 +0000259 // level 2
260 fn_hash = hash(fn, N_FN_ENTRIES);
261 curr_fnCC = curr_fileCC->fns[fn_hash];
262 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
263 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000264 }
nethercote9313ac42004-07-06 21:54:20 +0000265 if (NULL == curr_fnCC) {
266 curr_fileCC->fns[fn_hash] = curr_fnCC =
267 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000268 distinct_fns++;
269 }
270
nethercote9313ac42004-07-06 21:54:20 +0000271 // level 3
272 line_hash = line % N_LINE_ENTRIES;
273 curr_lineCC = curr_fnCC->lines[line_hash];
274 while (NULL != curr_lineCC && line != curr_lineCC->line) {
275 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000276 }
nethercote9313ac42004-07-06 21:54:20 +0000277 if (NULL == curr_lineCC) {
278 curr_fnCC->lines[line_hash] = curr_lineCC =
279 new_lineCC(line, curr_fnCC->lines[line_hash]);
280 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000281 }
nethercote9313ac42004-07-06 21:54:20 +0000282
283 VGP_POPCC(VgpGetLineCC);
284 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000285}
286
287/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000288/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000289/*------------------------------------------------------------*/
290
njnaf839f52005-06-23 03:27:57 +0000291static VG_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000292void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000293{
njn6a3009b2005-03-20 00:20:06 +0000294 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
295 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000296 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000297 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000298 &n->parent->Ir.m1, &n->parent->Ir.m2);
299 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000300 VGP_POPCC(VgpCacheSimulate);
301}
302
njnaf839f52005-06-23 03:27:57 +0000303static VG_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000304void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000305{
nethercote9313ac42004-07-06 21:54:20 +0000306 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000307 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000308 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000309 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000310 &n->parent->Ir.m1, &n->parent->Ir.m2);
311 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000312
nethercote9313ac42004-07-06 21:54:20 +0000313 cachesim_D1_doref(data_addr, n->data_size,
314 &n->parent->Dr.m1, &n->parent->Dr.m2);
315 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000316 VGP_POPCC(VgpCacheSimulate);
317}
318
njnaf839f52005-06-23 03:27:57 +0000319static VG_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000320void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000321{
nethercote9313ac42004-07-06 21:54:20 +0000322 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000323 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000324 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000325 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000326 &n->parent->Ir.m1, &n->parent->Ir.m2);
327 n->parent->Ir.a++;
328
329 cachesim_D1_doref(data_addr, n->data_size,
330 &n->parent->Dw.m1, &n->parent->Dw.m2);
331 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000332 VGP_POPCC(VgpCacheSimulate);
333}
334
njnaf839f52005-06-23 03:27:57 +0000335static VG_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000336void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000337{
338 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000339 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000340 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000341 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000342 &n->parent->Ir.m1, &n->parent->Ir.m2);
343 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000344
nethercote9313ac42004-07-06 21:54:20 +0000345 cachesim_D1_doref(data_addr1, n->data_size,
346 &n->parent->Dr.m1, &n->parent->Dr.m2);
347 n->parent->Dr.a++;
348 cachesim_D1_doref(data_addr2, n->data_size,
349 &n->parent->Dw.m1, &n->parent->Dw.m2);
350 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000351 VGP_POPCC(VgpCacheSimulate);
352}
353
nethercote9313ac42004-07-06 21:54:20 +0000354/*------------------------------------------------------------*/
355/*--- Instrumentation ---*/
356/*------------------------------------------------------------*/
357
nethercote564b2b02004-08-07 15:54:53 +0000358static
njn6a3009b2005-03-20 00:20:06 +0000359BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000360{
361 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000362 IRStmt* st;
363 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000364 VgHashNode** dummy;
365
njn6a3009b2005-03-20 00:20:06 +0000366 // Count number of original instrs in BB
367 n_instrs = 0;
368 for (i = 0; i < bbIn->stmts_used; i++) {
369 st = bbIn->stmts[i];
370 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000371 }
372
373 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000374 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
375 *bbSeenBefore = ( NULL == bbInfo ? False : True );
376 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000377 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000378 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000379 BB_retranslations++;
380 } else {
381 // BB never translated before (at this address, at least; could have
382 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000383 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
384 bbInfo->BB_addr = origAddr;
385 bbInfo->n_instrs = n_instrs;
386 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000387 distinct_instrs++;
388 }
njn6a3009b2005-03-20 00:20:06 +0000389 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000390}
njn6a3009b2005-03-20 00:20:06 +0000391
njn6a3009b2005-03-20 00:20:06 +0000392static
njnb3507ea2005-08-02 23:07:02 +0000393Bool handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st, IRStmt* st2,
njn6a3009b2005-03-20 00:20:06 +0000394 Addr* instrAddr, UInt* instrLen,
395 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
396 UInt* dataSize)
397{
sewardj7f4a8622005-03-26 21:55:21 +0000398 tl_assert(isFlatIRStmt(st));
399
njn6a3009b2005-03-20 00:20:06 +0000400 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000401 case Ist_NoOp:
njnb3507ea2005-08-02 23:07:02 +0000402 case Ist_AbiHint:
403 case Ist_Put:
404 case Ist_PutI:
405 case Ist_MFence:
njn6a3009b2005-03-20 00:20:06 +0000406 break;
407
njnb3507ea2005-08-02 23:07:02 +0000408 case Ist_Exit: {
409 // This is a conditional jump. Most of the time, we want to add the
410 // instrumentation before it, to ensure it gets executed. Eg, (1) if
411 // this conditional jump is just before an IMark:
412 //
413 // t108 = Not1(t107)
414 // [add instrumentation here]
415 // if (t108) goto {Boring} 0x3A96637D:I32
416 // ------ IMark(0x3A966370, 7) ------
417 //
418 // or (2) if this conditional jump is the last thing before the
419 // block-ending unconditional jump:
420 //
421 // t111 = Not1(t110)
422 // [add instrumentation here]
423 // if (t111) goto {Boring} 0x3A96637D:I32
424 // goto {Boring} 0x3A966370:I32
425 //
426 // One case (3) where we want the instrumentation after the conditional
427 // jump is when the conditional jump is for an x86 REP instruction:
428 //
429 // ------ IMark(0x3A967F13, 2) ------
430 // t1 = GET:I32(4)
431 // t6 = CmpEQ32(t1,0x0:I32)
432 // if (t6) goto {Boring} 0x3A967F15:I32 # ignore this cond jmp
433 // t7 = Sub32(t1,0x1:I32)
434 // PUT(4) = t7
435 // ...
436 // t56 = Not1(t55)
437 // [add instrumentation here]
438 // if (t56) goto {Boring} 0x3A967F15:I32
439 //
440 // Therefore, we return true if the next statement is an IMark, or if
441 // there is no next statement (which matches case (2), as the final
442 // unconditional jump is not represented in the IRStmt list).
443 //
444 // Note that this approach won't do in the long run for supporting
445 // PPC, but it's good enough for x86/AMD64 for the 3.0.X series.
446 if (NULL == st2 || Ist_IMark == st2->tag)
447 return True;
448 else
449 return False;
450 }
sewardje6da2fa2005-05-12 17:57:14 +0000451
njn6a3009b2005-03-20 00:20:06 +0000452 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000453 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
454 to the host's native pointer type; if that is 32 bits then it
455 discards the upper 32 bits. If we are cachegrinding on a
456 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000457 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000458 host and guest word sizes must be the same. Hence
459 st->Ist.IMark.addr will have been derived from a 32-bit guest
460 code address and truncation of it is safe. I believe this
461 assignment should be correct for both 32- and 64-bit
462 machines. */
463 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000464 *instrLen = st->Ist.IMark.len;
njn6a3009b2005-03-20 00:20:06 +0000465 break;
466
467 case Ist_Tmp: {
468 IRExpr* data = st->Ist.Tmp.data;
sewardj2e595852005-06-30 23:33:37 +0000469 if (data->tag == Iex_Load) {
470 IRExpr* aexpr = data->Iex.Load.addr;
sewardj710d6c22005-03-20 18:55:15 +0000471 tl_assert( isIRAtom(aexpr) );
sewardj2e595852005-06-30 23:33:37 +0000472 // Note also, endianness info is ignored. I guess that's not
473 // interesting.
njn6a3009b2005-03-20 00:20:06 +0000474 // XXX: repe cmpsb does two loads... the first one is ignored here!
475 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
476 *loadAddrExpr = aexpr;
sewardj2e595852005-06-30 23:33:37 +0000477 *dataSize = sizeofIRType(data->Iex.Load.ty);
njn6a3009b2005-03-20 00:20:06 +0000478 }
njn6a3009b2005-03-20 00:20:06 +0000479 break;
480 }
481
sewardj2e595852005-06-30 23:33:37 +0000482 case Ist_Store: {
483 IRExpr* data = st->Ist.Store.data;
484 IRExpr* aexpr = st->Ist.Store.addr;
sewardj710d6c22005-03-20 18:55:15 +0000485 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000486 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
487 *storeAddrExpr = aexpr;
488 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
njn6a3009b2005-03-20 00:20:06 +0000489 break;
490 }
491
sewardj7f4a8622005-03-26 21:55:21 +0000492 case Ist_Dirty: {
493 IRDirty* d = st->Ist.Dirty.details;
494 if (d->mFx != Ifx_None) {
495 /* This dirty helper accesses memory. Collect the
496 details. */
497 tl_assert(d->mAddr != NULL);
498 tl_assert(d->mSize != 0);
499 *dataSize = d->mSize;
500 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
501 *loadAddrExpr = d->mAddr;
502 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
503 *storeAddrExpr = d->mAddr;
504 } else {
505 tl_assert(d->mAddr == NULL);
506 tl_assert(d->mSize == 0);
507 }
sewardj7f4a8622005-03-26 21:55:21 +0000508 break;
509 }
510
njn6a3009b2005-03-20 00:20:06 +0000511 default:
512 VG_(printf)("\n");
513 ppIRStmt(st);
514 VG_(printf)("\n");
515 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
516 }
njnb3507ea2005-08-02 23:07:02 +0000517
518 return False;
njn6a3009b2005-03-20 00:20:06 +0000519}
nethercote9313ac42004-07-06 21:54:20 +0000520
nethercote564b2b02004-08-07 15:54:53 +0000521static
njn6a3009b2005-03-20 00:20:06 +0000522void do_details( instr_info* n, Bool bbSeenBefore,
523 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000524{
njn6a3009b2005-03-20 00:20:06 +0000525 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000526 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000527 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000528 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000529 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000530 // the debug info might change; the other asserts should be enough to
531 // detect anything strange.
532 } else {
njn6a3009b2005-03-20 00:20:06 +0000533 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000534 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000535 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000536 n->data_size = data_size;
537 n->parent = parent;
538 }
539}
540
njn6a3009b2005-03-20 00:20:06 +0000541static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000542{
njn6a3009b2005-03-20 00:20:06 +0000543 // I'm assuming that for 'modify' instructions, that Vex always makes
544 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
545 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000546 tl_assert(isIRAtom(loadAddrExpr));
547 tl_assert(isIRAtom(storeAddrExpr));
548 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000549}
550
551// Instrumentation for the end of each original instruction.
552static
njnb3507ea2005-08-02 23:07:02 +0000553void instrumentInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
554 UInt instrAddr, UInt instrLen, UInt dataSize,
555 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
njn6a3009b2005-03-20 00:20:06 +0000556{
557 IRDirty* di;
558 IRExpr *arg1, *arg2, *arg3, **argv;
559 Int argc;
560 Char* helperName;
561 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000562 IRType wordTy;
563
564 // Stay sane ...
565 tl_assert(sizeof(HWord) == sizeof(void*));
566 if (sizeof(HWord) == 4) {
567 wordTy = Ity_I32;
568 } else
569 if (sizeof(HWord) == 8) {
570 wordTy = Ity_I64;
571 } else {
njnb3507ea2005-08-02 23:07:02 +0000572 VG_(tool_panic)("instrumentInstr: strange word size");
sewardj17a56bf2005-03-21 01:35:02 +0000573 }
574
575 if (loadAddrExpr)
576 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
577 if (storeAddrExpr)
578 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
579
njn6a3009b2005-03-20 00:20:06 +0000580
581 // Nb: instrLen will be zero if Vex failed to decode it.
582 tl_assert( 0 == instrLen ||
njnaf839f52005-06-23 03:27:57 +0000583 (instrLen >= VG_MIN_INSTR_SZB &&
584 instrLen <= VG_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000585
njn016712a2005-04-04 02:52:16 +0000586 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
587 // done inaccurately, but they're very rare and this avoids errors from
588 // hitting more than two cache lines in the simulation.
589 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
590
njn6a3009b2005-03-20 00:20:06 +0000591 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000592 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000593 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000594 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000595
596 if (!loadAddrExpr && !storeAddrExpr) {
597 // no load/store
598 tl_assert(0 == dataSize);
599 helperName = "log_1I_0D_cache_access";
600 helperAddr = &log_1I_0D_cache_access;
601 argc = 1;
602 argv = mkIRExprVec_1(arg1);
603
604 } else if (loadAddrExpr && !storeAddrExpr) {
605 // load
sewardj710d6c22005-03-20 18:55:15 +0000606 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000607 helperName = "log_1I_1Dr_cache_access";
608 helperAddr = &log_1I_1Dr_cache_access;
609 argc = 2;
610 arg2 = loadAddrExpr;
611 argv = mkIRExprVec_2(arg1, arg2);
612
613 } else if (!loadAddrExpr && storeAddrExpr) {
614 // store
sewardj710d6c22005-03-20 18:55:15 +0000615 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000616 helperName = "log_1I_1Dw_cache_access";
617 helperAddr = &log_1I_1Dw_cache_access;
618 argc = 2;
619 arg2 = storeAddrExpr;
620 argv = mkIRExprVec_2(arg1, arg2);
621
622 } else {
623 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000624 tl_assert( isIRAtom(loadAddrExpr) );
625 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000626
627 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
628 // modify
629 helperName = "log_1I_1Dr_cache_access";
630 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000631 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000632 arg2 = loadAddrExpr;
633 argv = mkIRExprVec_2(arg1, arg2);
634
nethercote9313ac42004-07-06 21:54:20 +0000635 } else {
njn6a3009b2005-03-20 00:20:06 +0000636 // load/store
637 helperName = "log_1I_2D_cache_access";
638 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000639 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000640 arg2 = loadAddrExpr;
641 arg3 = storeAddrExpr;
642 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000643 }
644 }
645
njn6a3009b2005-03-20 00:20:06 +0000646 // Add call to the instrumentation function
647 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
648 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000649}
njn14d01ce2004-11-26 11:30:14 +0000650
njn51d827b2005-05-09 01:02:08 +0000651static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
652 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000653{
njn6a3009b2005-03-20 00:20:06 +0000654 Int i, dataSize = 0, bbInfo_i;
655 IRBB* bbOut;
656 IRStmt* st;
657 BB_info* bbInfo;
njnb3507ea2005-08-02 23:07:02 +0000658 Bool bbSeenBefore = False, addedInstrumentation, addInstNow;
njn6a3009b2005-03-20 00:20:06 +0000659 Addr instrAddr, origAddr;
660 UInt instrLen;
661 IRExpr *loadAddrExpr, *storeAddrExpr;
662
sewardjd54babf2005-03-21 00:55:49 +0000663 if (gWordTy != hWordTy) {
664 /* We don't currently support this case. */
665 VG_(tool_panic)("host/guest word size mismatch");
666 }
667
njn6a3009b2005-03-20 00:20:06 +0000668 /* Set up BB */
669 bbOut = emptyIRBB();
670 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
671 bbOut->next = dopyIRExpr(bbIn->next);
672 bbOut->jumpkind = bbIn->jumpkind;
673
674 // Get the first statement, and origAddr from it
675 i = 0;
676 tl_assert(bbIn->stmts_used > 0);
677 st = bbIn->stmts[0];
678 tl_assert(Ist_IMark == st->tag);
679 origAddr = (Addr)st->Ist.IMark.addr;
680 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
681
682 // Get block info
683 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
684 bbInfo_i = 0;
685
686 do {
687 // We should be at an IMark statement
688 tl_assert(Ist_IMark == st->tag);
689
690 // Reset stuff for this original instruction
691 loadAddrExpr = storeAddrExpr = NULL;
692 dataSize = 0;
njnb3507ea2005-08-02 23:07:02 +0000693 addedInstrumentation = False;
njn6a3009b2005-03-20 00:20:06 +0000694
695 // Process all the statements for this original instruction (ie. until
696 // the next IMark statement, or the end of the block)
697 do {
njnb3507ea2005-08-02 23:07:02 +0000698 IRStmt* st2 = ( i+1 < bbIn->stmts_used ? bbIn->stmts[i+1] : NULL );
699
700 addInstNow = handleOneStatement(bbIn->tyenv, bbOut, st, st2,
701 &instrAddr, &instrLen, &loadAddrExpr,
702 &storeAddrExpr, &dataSize);
703 if (addInstNow) {
704 tl_assert(!addedInstrumentation);
705 addedInstrumentation = True;
706
707 // Add instrumentation before this statement.
708 instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
709 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
710 }
711
712 addStmtToIRBB( bbOut, st );
713
njn6a3009b2005-03-20 00:20:06 +0000714 i++;
njnb3507ea2005-08-02 23:07:02 +0000715 st = st2;
njn6a3009b2005-03-20 00:20:06 +0000716 }
717 while (st && Ist_IMark != st->tag);
718
njnb3507ea2005-08-02 23:07:02 +0000719 if (!addedInstrumentation) {
720 // Add instrumentation now, after all the instruction's statements.
721 instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
722 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
723 }
njn6a3009b2005-03-20 00:20:06 +0000724
725 bbInfo_i++;
njnb3507ea2005-08-02 23:07:02 +0000726 }
njn6a3009b2005-03-20 00:20:06 +0000727 while (st);
728
729 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000730}
njn4f9c9342002-04-29 16:03:24 +0000731
732/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000733/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000734/*------------------------------------------------------------*/
735
sewardjb5f6f512005-03-10 23:59:00 +0000736#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000737
738static cache_t clo_I1_cache = UNDEFINED_CACHE;
739static cache_t clo_D1_cache = UNDEFINED_CACHE;
740static cache_t clo_L2_cache = UNDEFINED_CACHE;
741
njn7cf0bd32002-06-08 13:36:03 +0000742/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000743static
njna1d1a642004-11-26 18:36:02 +0000744void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000745{
746 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000747 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000748 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000749 "error: %s size of %dB not a power of two; aborting.",
750 name, cache->size);
751 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000752 }
753
sewardj07133bf2002-06-13 10:25:56 +0000754 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000755 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000756 "error: %s associativity of %d not a power of two; aborting.",
757 name, cache->assoc);
758 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000759 }
760
sewardj07133bf2002-06-13 10:25:56 +0000761 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000762 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000763 "error: %s line size of %dB not a power of two; aborting.",
764 name, cache->line_size);
765 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000766 }
767
njn6a3009b2005-03-20 00:20:06 +0000768 // Then check line size >= 16 -- any smaller and a single instruction could
769 // straddle three cache lines, which breaks a simulation assertion and is
770 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000771 if (cache->line_size < MIN_LINE_SIZE) {
772 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000773 "error: %s line size of %dB too small; aborting.",
774 name, cache->line_size);
775 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000776 }
777
778 /* Then check cache size > line size (causes seg faults if not). */
779 if (cache->size <= cache->line_size) {
780 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000781 "error: %s cache size of %dB <= line size of %dB; aborting.",
782 name, cache->size, cache->line_size);
783 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000784 }
785
786 /* Then check assoc <= (size / line size) (seg faults otherwise). */
787 if (cache->assoc > (cache->size / cache->line_size)) {
788 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000789 "warning: %s associativity > (size / line size); aborting.", name);
790 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000791 }
792}
793
sewardj07133bf2002-06-13 10:25:56 +0000794static
nethercoteb35a8b92004-09-11 16:45:27 +0000795void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000796{
nethercote9313ac42004-07-06 21:54:20 +0000797#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
798
nethercoteb35a8b92004-09-11 16:45:27 +0000799 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000800
nethercoteb35a8b92004-09-11 16:45:27 +0000801 // Count how many were defined on the command line.
802 if (DEFINED(clo_I1_cache)) { n_clos++; }
803 if (DEFINED(clo_D1_cache)) { n_clos++; }
804 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000805
njna1d1a642004-11-26 18:36:02 +0000806 // Set the cache config (using auto-detection, if supported by the
807 // architecture)
njnaf839f52005-06-23 03:27:57 +0000808 VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000809
nethercote9313ac42004-07-06 21:54:20 +0000810 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000811 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
812 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
813 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000814
nethercote9313ac42004-07-06 21:54:20 +0000815 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000816 check_cache(I1c, "I1");
817 check_cache(D1c, "D1");
818 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000819
820 if (VG_(clo_verbosity) > 1) {
821 VG_(message)(Vg_UserMsg, "Cache configuration used:");
822 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
823 I1c->size, I1c->assoc, I1c->line_size);
824 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
825 D1c->size, D1c->assoc, D1c->line_size);
826 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
827 L2c->size, L2c->assoc, L2c->line_size);
828 }
nethercote9313ac42004-07-06 21:54:20 +0000829#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000830}
831
njn4f9c9342002-04-29 16:03:24 +0000832/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000833/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000834/*------------------------------------------------------------*/
835
nethercote9313ac42004-07-06 21:54:20 +0000836// Total reads/writes/misses. Calculated during CC traversal at the end.
837// All auto-zeroed.
838static CC Ir_total;
839static CC Dr_total;
840static CC Dw_total;
841
842static Char* cachegrind_out_file;
843
nethercote9313ac42004-07-06 21:54:20 +0000844static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000845{
nethercote9313ac42004-07-06 21:54:20 +0000846 Char buf[512];
847 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
848 n->line,
849 n->Ir.a, n->Ir.m1, n->Ir.m2,
850 n->Dr.a, n->Dr.m1, n->Dr.m2,
851 n->Dw.a, n->Dw.m1, n->Dw.m2);
852 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
853
854 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
855 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
856 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
857}
858
859static void fprint_CC_table_and_calc_totals(void)
860{
861 Int fd;
sewardj92645592005-07-23 09:18:34 +0000862 SysRes sres;
nethercote9313ac42004-07-06 21:54:20 +0000863 Char buf[512];
864 fileCC *curr_fileCC;
865 fnCC *curr_fnCC;
866 lineCC *curr_lineCC;
867 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000868
njn25e49d8e72002-09-23 09:36:25 +0000869 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000870
sewardj92645592005-07-23 09:18:34 +0000871 sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
872 VKI_S_IRUSR|VKI_S_IWUSR);
873 if (sres.isError) {
nethercote9313ac42004-07-06 21:54:20 +0000874 // If the file can't be opened for whatever reason (conflict
875 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000876 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000877 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000878 cachegrind_out_file );
879 VG_(message)(Vg_UserMsg,
880 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000881 return;
sewardj92645592005-07-23 09:18:34 +0000882 } else {
883 fd = sres.val;
sewardj0744b6c2002-12-11 00:45:42 +0000884 }
njn4f9c9342002-04-29 16:03:24 +0000885
nethercote9313ac42004-07-06 21:54:20 +0000886 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
887 // the 2nd colon makes cg_annotate's output look nicer.
888 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
889 "desc: D1 cache: %s\n"
890 "desc: L2 cache: %s\n",
891 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000892 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000893
nethercote9313ac42004-07-06 21:54:20 +0000894 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000895 VG_(strcpy)(buf, "cmd:");
896 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000897 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000898 if (VG_(client_argv)[i] == NULL)
899 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000900 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000901 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000902 }
nethercote9313ac42004-07-06 21:54:20 +0000903 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000904 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
905 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
906
nethercote9313ac42004-07-06 21:54:20 +0000907 // Six loops here: three for the hash table arrays, and three for the
908 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000909 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000910 curr_fileCC = CC_table[i];
911 while (curr_fileCC != NULL) {
912 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000913 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
914
915 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000916 curr_fnCC = curr_fileCC->fns[j];
917 while (curr_fnCC != NULL) {
918 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000919 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
920
nethercote9313ac42004-07-06 21:54:20 +0000921 for (k = 0; k < N_LINE_ENTRIES; k++) {
922 curr_lineCC = curr_fnCC->lines[k];
923 while (curr_lineCC != NULL) {
924 fprint_lineCC(fd, curr_lineCC);
925 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000926 }
927 }
nethercote9313ac42004-07-06 21:54:20 +0000928 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000929 }
930 }
nethercote9313ac42004-07-06 21:54:20 +0000931 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000932 }
933 }
934
nethercote9313ac42004-07-06 21:54:20 +0000935 // Summary stats must come after rest of table, since we calculate them
936 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000937 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000938 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000939 Ir_total.a, Ir_total.m1, Ir_total.m2,
940 Dr_total.a, Dr_total.m1, Dr_total.m2,
941 Dw_total.a, Dw_total.m1, Dw_total.m2);
942 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
943 VG_(close)(fd);
944}
945
njn607adfc2003-09-30 14:15:44 +0000946static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000947{
njn607adfc2003-09-30 14:15:44 +0000948 UInt w = 0;
949 while (n > 0) {
950 n = n / 10;
951 w++;
njn4f9c9342002-04-29 16:03:24 +0000952 }
njn607adfc2003-09-30 14:15:44 +0000953 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000954}
955
njn51d827b2005-05-09 01:02:08 +0000956static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000957{
nethercote9313ac42004-07-06 21:54:20 +0000958 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000959
njn4f9c9342002-04-29 16:03:24 +0000960 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000961 ULong L2_total_m, L2_total_mr, L2_total_mw,
962 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000963 Int l1, l2, l3;
964 Int p;
965
nethercote9313ac42004-07-06 21:54:20 +0000966 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000967
njn7cf0bd32002-06-08 13:36:03 +0000968 if (VG_(clo_verbosity) == 0)
969 return;
970
njn4f9c9342002-04-29 16:03:24 +0000971 /* I cache results. Use the I_refs value to determine the first column
972 * width. */
njn607adfc2003-09-30 14:15:44 +0000973 l1 = ULong_width(Ir_total.a);
974 l2 = ULong_width(Dr_total.a);
975 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000976
njn607adfc2003-09-30 14:15:44 +0000977 /* Make format string, getting width right for numbers */
978 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
979
980 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
981 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
982 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000983
984 p = 100;
985
njn25e49d8e72002-09-23 09:36:25 +0000986 if (0 == Ir_total.a) Ir_total.a = 1;
njn856c54e2005-06-26 18:43:40 +0000987 VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1);
njn4f9c9342002-04-29 16:03:24 +0000988 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
989
njn856c54e2005-06-26 18:43:40 +0000990 VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1);
njn4f9c9342002-04-29 16:03:24 +0000991 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
992 VG_(message)(Vg_UserMsg, "");
993
994 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
995 * width of columns 2 & 3. */
996 D_total.a = Dr_total.a + Dw_total.a;
997 D_total.m1 = Dr_total.m1 + Dw_total.m1;
998 D_total.m2 = Dr_total.m2 + Dw_total.m2;
999
njn607adfc2003-09-30 14:15:44 +00001000 /* Make format string, getting width right for numbers */
1001 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +00001002
njn607adfc2003-09-30 14:15:44 +00001003 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
1004 D_total.a, Dr_total.a, Dw_total.a);
1005 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
1006 D_total.m1, Dr_total.m1, Dw_total.m1);
1007 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
1008 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +00001009
1010 p = 10;
1011
njn25e49d8e72002-09-23 09:36:25 +00001012 if (0 == D_total.a) D_total.a = 1;
1013 if (0 == Dr_total.a) Dr_total.a = 1;
1014 if (0 == Dw_total.a) Dw_total.a = 1;
njn856c54e2005-06-26 18:43:40 +00001015 VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1);
1016 VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2);
1017 VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001018 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1019
njn856c54e2005-06-26 18:43:40 +00001020 VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1);
1021 VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2);
1022 VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001023 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
1024 VG_(message)(Vg_UserMsg, "");
1025
1026 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +00001027
1028 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
1029 L2_total_r = Dr_total.m1 + Ir_total.m1;
1030 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +00001031 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
1032 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +00001033
njn4f9c9342002-04-29 16:03:24 +00001034 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
1035 L2_total_mr = Dr_total.m2 + Ir_total.m2;
1036 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +00001037 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
1038 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +00001039
njn856c54e2005-06-26 18:43:40 +00001040 VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1);
1041 VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2);
1042 VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001043 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1044
1045
nethercote9313ac42004-07-06 21:54:20 +00001046 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001047 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001048 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001049 file_line_debug_BBs + no_debug_BBs;
1050
1051 VG_(message)(Vg_DebugMsg, "");
1052 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1053 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001054 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1055 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001056 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1057 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1058 full_debug_BBs * 100 / BB_lookups,
1059 full_debug_BBs);
1060 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1061 file_line_debug_BBs * 100 / BB_lookups,
1062 file_line_debug_BBs);
1063 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001064 fn_debug_BBs * 100 / BB_lookups,
1065 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001066 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1067 no_debug_BBs * 100 / BB_lookups,
1068 no_debug_BBs);
1069 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001070 }
njn25e49d8e72002-09-23 09:36:25 +00001071 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001072}
1073
nethercote9313ac42004-07-06 21:54:20 +00001074/*--------------------------------------------------------------------*/
1075/*--- Discarding BB info ---*/
1076/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001077
nethercote9313ac42004-07-06 21:54:20 +00001078// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001079static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001080{
nethercote9313ac42004-07-06 21:54:20 +00001081 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001082 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001083
nethercote928a5f72004-11-03 18:10:37 +00001084 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001085
nethercote9313ac42004-07-06 21:54:20 +00001086 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001087 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1088 tl_assert(NULL != bbInfo);
1089 *prev_next_ptr = bbInfo->next;
1090 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001091}
1092
1093/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001094/*--- Command line processing ---*/
1095/*--------------------------------------------------------------------*/
1096
nethercote9313ac42004-07-06 21:54:20 +00001097static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001098{
nethercote9313ac42004-07-06 21:54:20 +00001099 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001100
nethercote9313ac42004-07-06 21:54:20 +00001101 // Option argument looks like "65536,2,64".
1102 // Find commas, replace with NULs to make three independent
1103 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001104 while (VG_(isdigit)(opt[i])) i++;
1105 if (',' == opt[i]) {
1106 opt[i++] = '\0';
1107 i2 = i;
1108 } else goto bad;
1109 while (VG_(isdigit)(opt[i])) i++;
1110 if (',' == opt[i]) {
1111 opt[i++] = '\0';
1112 i3 = i;
1113 } else goto bad;
1114 while (VG_(isdigit)(opt[i])) i++;
1115 if ('\0' != opt[i]) goto bad;
1116
nethercote9313ac42004-07-06 21:54:20 +00001117 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001118 cache->assoc = (Int)VG_(atoll)(opt + i2);
1119 cache->line_size = (Int)VG_(atoll)(opt + i3);
1120
nethercote9313ac42004-07-06 21:54:20 +00001121 opt[i2-1] = ',';
1122 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001123 return;
1124
1125 bad:
nethercote9313ac42004-07-06 21:54:20 +00001126 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001127}
1128
njn51d827b2005-05-09 01:02:08 +00001129static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001130{
nethercote9313ac42004-07-06 21:54:20 +00001131 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001132 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001133 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001134 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001135 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001136 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001137 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001138 else
1139 return False;
1140
1141 return True;
1142}
1143
njn51d827b2005-05-09 01:02:08 +00001144static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001145{
njn3e884182003-04-15 13:03:23 +00001146 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001147" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1148" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001149" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1150 );
1151}
1152
njn51d827b2005-05-09 01:02:08 +00001153static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001154{
1155 VG_(printf)(
1156" (none)\n"
1157 );
njn25e49d8e72002-09-23 09:36:25 +00001158}
1159
1160/*--------------------------------------------------------------------*/
1161/*--- Setup ---*/
1162/*--------------------------------------------------------------------*/
1163
njn51d827b2005-05-09 01:02:08 +00001164static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001165{
1166 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001167
nethercoteb35a8b92004-09-11 16:45:27 +00001168 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001169
1170 cachesim_I1_initcache(I1c);
1171 cachesim_D1_initcache(D1c);
1172 cachesim_L2_initcache(L2c);
1173
njn31066fd2005-03-26 00:42:02 +00001174 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1175 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1176 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001177}
1178
njn57ca7ab2005-06-21 23:44:58 +00001179static Char base_dir[VKI_PATH_MAX];
1180
njn51d827b2005-05-09 01:02:08 +00001181static void cg_pre_clo_init(void)
1182{
njn51d827b2005-05-09 01:02:08 +00001183 VG_(details_name) ("Cachegrind");
1184 VG_(details_version) (NULL);
1185 VG_(details_description) ("an I1/D1/L2 cache profiler");
1186 VG_(details_copyright_author)(
1187 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1188 VG_(details_bug_reports_to) (VG_BUGS_TO);
1189 VG_(details_avg_translation_sizeB) ( 155 );
1190
1191 VG_(basic_tool_funcs) (cg_post_clo_init,
1192 cg_instrument,
1193 cg_fini);
1194
1195 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1196 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1197 cg_print_usage,
1198 cg_print_debug_usage);
1199
1200 /* Get working directory */
njn57ca7ab2005-06-21 23:44:58 +00001201 tl_assert( VG_(getcwd)(base_dir, VKI_PATH_MAX) );
njn51d827b2005-05-09 01:02:08 +00001202
1203 /* Block is big enough for dir name + cachegrind.out.<pid> */
1204 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1205 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1206 base_dir, VG_(getpid)());
njn51d827b2005-05-09 01:02:08 +00001207
njnf69f9452005-07-03 17:53:11 +00001208 instr_info_table = VG_(HT_construct)( 4999 ); // prime, biggish
njn51d827b2005-05-09 01:02:08 +00001209}
1210
1211VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001212
njn25e49d8e72002-09-23 09:36:25 +00001213/*--------------------------------------------------------------------*/
njnf69f9452005-07-03 17:53:11 +00001214/*--- end ---*/
sewardj18d75132002-05-16 11:06:21 +00001215/*--------------------------------------------------------------------*/