blob: 159c5f8837645a7808ad9034d187f5b0e5ab03d7 [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{
njn4bd67b52005-08-11 00:47:10 +0000361 Int i, n_instrs;
362 IRStmt* st;
363 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000364
njn6a3009b2005-03-20 00:20:06 +0000365 // Count number of original instrs in BB
366 n_instrs = 0;
367 for (i = 0; i < bbIn->stmts_used; i++) {
368 st = bbIn->stmts[i];
369 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000370 }
371
372 // Get the BB_info
njn4bd67b52005-08-11 00:47:10 +0000373 bbInfo = (BB_info*)VG_(HT_lookup)(instr_info_table, origAddr);
njn6a3009b2005-03-20 00:20:06 +0000374 *bbSeenBefore = ( NULL == bbInfo ? False : True );
375 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000376 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000377 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000378 BB_retranslations++;
379 } else {
380 // BB never translated before (at this address, at least; could have
381 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000382 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
383 bbInfo->BB_addr = origAddr;
384 bbInfo->n_instrs = n_instrs;
385 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000386 distinct_instrs++;
387 }
njn6a3009b2005-03-20 00:20:06 +0000388 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000389}
njn6a3009b2005-03-20 00:20:06 +0000390
njn6a3009b2005-03-20 00:20:06 +0000391static
njnb3507ea2005-08-02 23:07:02 +0000392Bool handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st, IRStmt* st2,
njn6a3009b2005-03-20 00:20:06 +0000393 Addr* instrAddr, UInt* instrLen,
394 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
395 UInt* dataSize)
396{
sewardj7f4a8622005-03-26 21:55:21 +0000397 tl_assert(isFlatIRStmt(st));
398
njn6a3009b2005-03-20 00:20:06 +0000399 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000400 case Ist_NoOp:
njnb3507ea2005-08-02 23:07:02 +0000401 case Ist_AbiHint:
402 case Ist_Put:
403 case Ist_PutI:
404 case Ist_MFence:
njn6a3009b2005-03-20 00:20:06 +0000405 break;
406
njnb3507ea2005-08-02 23:07:02 +0000407 case Ist_Exit: {
408 // This is a conditional jump. Most of the time, we want to add the
409 // instrumentation before it, to ensure it gets executed. Eg, (1) if
410 // this conditional jump is just before an IMark:
411 //
412 // t108 = Not1(t107)
413 // [add instrumentation here]
414 // if (t108) goto {Boring} 0x3A96637D:I32
415 // ------ IMark(0x3A966370, 7) ------
416 //
417 // or (2) if this conditional jump is the last thing before the
418 // block-ending unconditional jump:
419 //
420 // t111 = Not1(t110)
421 // [add instrumentation here]
422 // if (t111) goto {Boring} 0x3A96637D:I32
423 // goto {Boring} 0x3A966370:I32
424 //
425 // One case (3) where we want the instrumentation after the conditional
426 // jump is when the conditional jump is for an x86 REP instruction:
427 //
428 // ------ IMark(0x3A967F13, 2) ------
429 // t1 = GET:I32(4)
430 // t6 = CmpEQ32(t1,0x0:I32)
431 // if (t6) goto {Boring} 0x3A967F15:I32 # ignore this cond jmp
432 // t7 = Sub32(t1,0x1:I32)
433 // PUT(4) = t7
434 // ...
435 // t56 = Not1(t55)
436 // [add instrumentation here]
437 // if (t56) goto {Boring} 0x3A967F15:I32
438 //
439 // Therefore, we return true if the next statement is an IMark, or if
440 // there is no next statement (which matches case (2), as the final
441 // unconditional jump is not represented in the IRStmt list).
442 //
443 // Note that this approach won't do in the long run for supporting
444 // PPC, but it's good enough for x86/AMD64 for the 3.0.X series.
445 if (NULL == st2 || Ist_IMark == st2->tag)
446 return True;
447 else
448 return False;
449 }
sewardje6da2fa2005-05-12 17:57:14 +0000450
njn6a3009b2005-03-20 00:20:06 +0000451 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000452 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
453 to the host's native pointer type; if that is 32 bits then it
454 discards the upper 32 bits. If we are cachegrinding on a
455 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000456 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000457 host and guest word sizes must be the same. Hence
458 st->Ist.IMark.addr will have been derived from a 32-bit guest
459 code address and truncation of it is safe. I believe this
460 assignment should be correct for both 32- and 64-bit
461 machines. */
462 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000463 *instrLen = st->Ist.IMark.len;
njn6a3009b2005-03-20 00:20:06 +0000464 break;
465
466 case Ist_Tmp: {
467 IRExpr* data = st->Ist.Tmp.data;
sewardj2e595852005-06-30 23:33:37 +0000468 if (data->tag == Iex_Load) {
469 IRExpr* aexpr = data->Iex.Load.addr;
sewardj710d6c22005-03-20 18:55:15 +0000470 tl_assert( isIRAtom(aexpr) );
sewardj2e595852005-06-30 23:33:37 +0000471 // Note also, endianness info is ignored. I guess that's not
472 // interesting.
njn6a3009b2005-03-20 00:20:06 +0000473 // XXX: repe cmpsb does two loads... the first one is ignored here!
474 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
475 *loadAddrExpr = aexpr;
sewardj2e595852005-06-30 23:33:37 +0000476 *dataSize = sizeofIRType(data->Iex.Load.ty);
njn6a3009b2005-03-20 00:20:06 +0000477 }
njn6a3009b2005-03-20 00:20:06 +0000478 break;
479 }
480
sewardj2e595852005-06-30 23:33:37 +0000481 case Ist_Store: {
482 IRExpr* data = st->Ist.Store.data;
483 IRExpr* aexpr = st->Ist.Store.addr;
sewardj710d6c22005-03-20 18:55:15 +0000484 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000485 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
486 *storeAddrExpr = aexpr;
487 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
njn6a3009b2005-03-20 00:20:06 +0000488 break;
489 }
490
sewardj7f4a8622005-03-26 21:55:21 +0000491 case Ist_Dirty: {
492 IRDirty* d = st->Ist.Dirty.details;
493 if (d->mFx != Ifx_None) {
494 /* This dirty helper accesses memory. Collect the
495 details. */
496 tl_assert(d->mAddr != NULL);
497 tl_assert(d->mSize != 0);
498 *dataSize = d->mSize;
499 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
500 *loadAddrExpr = d->mAddr;
501 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
502 *storeAddrExpr = d->mAddr;
503 } else {
504 tl_assert(d->mAddr == NULL);
505 tl_assert(d->mSize == 0);
506 }
sewardj7f4a8622005-03-26 21:55:21 +0000507 break;
508 }
509
njn6a3009b2005-03-20 00:20:06 +0000510 default:
511 VG_(printf)("\n");
512 ppIRStmt(st);
513 VG_(printf)("\n");
514 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
515 }
njnb3507ea2005-08-02 23:07:02 +0000516
517 return False;
njn6a3009b2005-03-20 00:20:06 +0000518}
nethercote9313ac42004-07-06 21:54:20 +0000519
nethercote564b2b02004-08-07 15:54:53 +0000520static
njn6a3009b2005-03-20 00:20:06 +0000521void do_details( instr_info* n, Bool bbSeenBefore,
522 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000523{
njn6a3009b2005-03-20 00:20:06 +0000524 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000525 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000526 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000527 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000528 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000529 // the debug info might change; the other asserts should be enough to
530 // detect anything strange.
531 } else {
njn6a3009b2005-03-20 00:20:06 +0000532 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000533 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000534 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000535 n->data_size = data_size;
536 n->parent = parent;
537 }
538}
539
njn6a3009b2005-03-20 00:20:06 +0000540static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000541{
njn6a3009b2005-03-20 00:20:06 +0000542 // I'm assuming that for 'modify' instructions, that Vex always makes
543 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
544 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000545 tl_assert(isIRAtom(loadAddrExpr));
546 tl_assert(isIRAtom(storeAddrExpr));
547 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000548}
549
550// Instrumentation for the end of each original instruction.
551static
njnb3507ea2005-08-02 23:07:02 +0000552void instrumentInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
553 UInt instrAddr, UInt instrLen, UInt dataSize,
554 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
njn6a3009b2005-03-20 00:20:06 +0000555{
556 IRDirty* di;
557 IRExpr *arg1, *arg2, *arg3, **argv;
558 Int argc;
559 Char* helperName;
560 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000561 IRType wordTy;
562
563 // Stay sane ...
564 tl_assert(sizeof(HWord) == sizeof(void*));
565 if (sizeof(HWord) == 4) {
566 wordTy = Ity_I32;
567 } else
568 if (sizeof(HWord) == 8) {
569 wordTy = Ity_I64;
570 } else {
njnb3507ea2005-08-02 23:07:02 +0000571 VG_(tool_panic)("instrumentInstr: strange word size");
sewardj17a56bf2005-03-21 01:35:02 +0000572 }
573
574 if (loadAddrExpr)
575 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
576 if (storeAddrExpr)
577 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
578
njn6a3009b2005-03-20 00:20:06 +0000579
580 // Nb: instrLen will be zero if Vex failed to decode it.
581 tl_assert( 0 == instrLen ||
njnaf839f52005-06-23 03:27:57 +0000582 (instrLen >= VG_MIN_INSTR_SZB &&
583 instrLen <= VG_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000584
njn016712a2005-04-04 02:52:16 +0000585 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
586 // done inaccurately, but they're very rare and this avoids errors from
587 // hitting more than two cache lines in the simulation.
588 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
589
njn6a3009b2005-03-20 00:20:06 +0000590 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000591 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000592 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000593 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000594
595 if (!loadAddrExpr && !storeAddrExpr) {
596 // no load/store
597 tl_assert(0 == dataSize);
598 helperName = "log_1I_0D_cache_access";
599 helperAddr = &log_1I_0D_cache_access;
600 argc = 1;
601 argv = mkIRExprVec_1(arg1);
602
603 } else if (loadAddrExpr && !storeAddrExpr) {
604 // load
sewardj710d6c22005-03-20 18:55:15 +0000605 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000606 helperName = "log_1I_1Dr_cache_access";
607 helperAddr = &log_1I_1Dr_cache_access;
608 argc = 2;
609 arg2 = loadAddrExpr;
610 argv = mkIRExprVec_2(arg1, arg2);
611
612 } else if (!loadAddrExpr && storeAddrExpr) {
613 // store
sewardj710d6c22005-03-20 18:55:15 +0000614 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000615 helperName = "log_1I_1Dw_cache_access";
616 helperAddr = &log_1I_1Dw_cache_access;
617 argc = 2;
618 arg2 = storeAddrExpr;
619 argv = mkIRExprVec_2(arg1, arg2);
620
621 } else {
622 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000623 tl_assert( isIRAtom(loadAddrExpr) );
624 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000625
626 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
627 // modify
628 helperName = "log_1I_1Dr_cache_access";
629 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000630 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000631 arg2 = loadAddrExpr;
632 argv = mkIRExprVec_2(arg1, arg2);
633
nethercote9313ac42004-07-06 21:54:20 +0000634 } else {
njn6a3009b2005-03-20 00:20:06 +0000635 // load/store
636 helperName = "log_1I_2D_cache_access";
637 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000638 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000639 arg2 = loadAddrExpr;
640 arg3 = storeAddrExpr;
641 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000642 }
643 }
644
njn6a3009b2005-03-20 00:20:06 +0000645 // Add call to the instrumentation function
646 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
647 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000648}
njn14d01ce2004-11-26 11:30:14 +0000649
njn51d827b2005-05-09 01:02:08 +0000650static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
651 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000652{
njn6a3009b2005-03-20 00:20:06 +0000653 Int i, dataSize = 0, bbInfo_i;
654 IRBB* bbOut;
655 IRStmt* st;
656 BB_info* bbInfo;
njnb3507ea2005-08-02 23:07:02 +0000657 Bool bbSeenBefore = False, addedInstrumentation, addInstNow;
njn6a3009b2005-03-20 00:20:06 +0000658 Addr instrAddr, origAddr;
659 UInt instrLen;
660 IRExpr *loadAddrExpr, *storeAddrExpr;
661
sewardjd54babf2005-03-21 00:55:49 +0000662 if (gWordTy != hWordTy) {
663 /* We don't currently support this case. */
664 VG_(tool_panic)("host/guest word size mismatch");
665 }
666
njn6a3009b2005-03-20 00:20:06 +0000667 /* Set up BB */
668 bbOut = emptyIRBB();
669 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
670 bbOut->next = dopyIRExpr(bbIn->next);
671 bbOut->jumpkind = bbIn->jumpkind;
672
673 // Get the first statement, and origAddr from it
674 i = 0;
675 tl_assert(bbIn->stmts_used > 0);
676 st = bbIn->stmts[0];
677 tl_assert(Ist_IMark == st->tag);
678 origAddr = (Addr)st->Ist.IMark.addr;
679 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
680
681 // Get block info
682 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
683 bbInfo_i = 0;
684
685 do {
686 // We should be at an IMark statement
687 tl_assert(Ist_IMark == st->tag);
688
689 // Reset stuff for this original instruction
690 loadAddrExpr = storeAddrExpr = NULL;
691 dataSize = 0;
njnb3507ea2005-08-02 23:07:02 +0000692 addedInstrumentation = False;
njn6a3009b2005-03-20 00:20:06 +0000693
694 // Process all the statements for this original instruction (ie. until
695 // the next IMark statement, or the end of the block)
696 do {
njnb3507ea2005-08-02 23:07:02 +0000697 IRStmt* st2 = ( i+1 < bbIn->stmts_used ? bbIn->stmts[i+1] : NULL );
698
699 addInstNow = handleOneStatement(bbIn->tyenv, bbOut, st, st2,
700 &instrAddr, &instrLen, &loadAddrExpr,
701 &storeAddrExpr, &dataSize);
702 if (addInstNow) {
703 tl_assert(!addedInstrumentation);
704 addedInstrumentation = True;
705
706 // Add instrumentation before this statement.
707 instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
708 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
709 }
710
711 addStmtToIRBB( bbOut, st );
712
njn6a3009b2005-03-20 00:20:06 +0000713 i++;
njnb3507ea2005-08-02 23:07:02 +0000714 st = st2;
njn6a3009b2005-03-20 00:20:06 +0000715 }
716 while (st && Ist_IMark != st->tag);
717
njnb3507ea2005-08-02 23:07:02 +0000718 if (!addedInstrumentation) {
719 // Add instrumentation now, after all the instruction's statements.
720 instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
721 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
722 }
njn6a3009b2005-03-20 00:20:06 +0000723
724 bbInfo_i++;
njnb3507ea2005-08-02 23:07:02 +0000725 }
njn6a3009b2005-03-20 00:20:06 +0000726 while (st);
727
728 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000729}
njn4f9c9342002-04-29 16:03:24 +0000730
731/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000732/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000733/*------------------------------------------------------------*/
734
sewardjb5f6f512005-03-10 23:59:00 +0000735#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000736
737static cache_t clo_I1_cache = UNDEFINED_CACHE;
738static cache_t clo_D1_cache = UNDEFINED_CACHE;
739static cache_t clo_L2_cache = UNDEFINED_CACHE;
740
njn7cf0bd32002-06-08 13:36:03 +0000741/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000742static
njna1d1a642004-11-26 18:36:02 +0000743void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000744{
745 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000746 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000747 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000748 "error: %s size of %dB not a power of two; aborting.",
749 name, cache->size);
750 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000751 }
752
sewardj07133bf2002-06-13 10:25:56 +0000753 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000754 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000755 "error: %s associativity of %d not a power of two; aborting.",
756 name, cache->assoc);
757 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000758 }
759
sewardj07133bf2002-06-13 10:25:56 +0000760 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000761 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000762 "error: %s line size of %dB not a power of two; aborting.",
763 name, cache->line_size);
764 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000765 }
766
njn6a3009b2005-03-20 00:20:06 +0000767 // Then check line size >= 16 -- any smaller and a single instruction could
768 // straddle three cache lines, which breaks a simulation assertion and is
769 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000770 if (cache->line_size < MIN_LINE_SIZE) {
771 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000772 "error: %s line size of %dB too small; aborting.",
773 name, cache->line_size);
774 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000775 }
776
777 /* Then check cache size > line size (causes seg faults if not). */
778 if (cache->size <= cache->line_size) {
779 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000780 "error: %s cache size of %dB <= line size of %dB; aborting.",
781 name, cache->size, cache->line_size);
782 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000783 }
784
785 /* Then check assoc <= (size / line size) (seg faults otherwise). */
786 if (cache->assoc > (cache->size / cache->line_size)) {
787 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000788 "warning: %s associativity > (size / line size); aborting.", name);
789 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000790 }
791}
792
sewardj07133bf2002-06-13 10:25:56 +0000793static
nethercoteb35a8b92004-09-11 16:45:27 +0000794void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000795{
nethercote9313ac42004-07-06 21:54:20 +0000796#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
797
nethercoteb35a8b92004-09-11 16:45:27 +0000798 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000799
nethercoteb35a8b92004-09-11 16:45:27 +0000800 // Count how many were defined on the command line.
801 if (DEFINED(clo_I1_cache)) { n_clos++; }
802 if (DEFINED(clo_D1_cache)) { n_clos++; }
803 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000804
njna1d1a642004-11-26 18:36:02 +0000805 // Set the cache config (using auto-detection, if supported by the
806 // architecture)
njnaf839f52005-06-23 03:27:57 +0000807 VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000808
nethercote9313ac42004-07-06 21:54:20 +0000809 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000810 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
811 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
812 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000813
nethercote9313ac42004-07-06 21:54:20 +0000814 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000815 check_cache(I1c, "I1");
816 check_cache(D1c, "D1");
817 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000818
819 if (VG_(clo_verbosity) > 1) {
820 VG_(message)(Vg_UserMsg, "Cache configuration used:");
821 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
822 I1c->size, I1c->assoc, I1c->line_size);
823 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
824 D1c->size, D1c->assoc, D1c->line_size);
825 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
826 L2c->size, L2c->assoc, L2c->line_size);
827 }
nethercote9313ac42004-07-06 21:54:20 +0000828#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000829}
830
njn4f9c9342002-04-29 16:03:24 +0000831/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000832/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000833/*------------------------------------------------------------*/
834
nethercote9313ac42004-07-06 21:54:20 +0000835// Total reads/writes/misses. Calculated during CC traversal at the end.
836// All auto-zeroed.
837static CC Ir_total;
838static CC Dr_total;
839static CC Dw_total;
840
841static Char* cachegrind_out_file;
842
nethercote9313ac42004-07-06 21:54:20 +0000843static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000844{
nethercote9313ac42004-07-06 21:54:20 +0000845 Char buf[512];
846 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
847 n->line,
848 n->Ir.a, n->Ir.m1, n->Ir.m2,
849 n->Dr.a, n->Dr.m1, n->Dr.m2,
850 n->Dw.a, n->Dw.m1, n->Dw.m2);
851 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
852
853 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
854 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
855 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
856}
857
858static void fprint_CC_table_and_calc_totals(void)
859{
860 Int fd;
sewardj92645592005-07-23 09:18:34 +0000861 SysRes sres;
nethercote9313ac42004-07-06 21:54:20 +0000862 Char buf[512];
863 fileCC *curr_fileCC;
864 fnCC *curr_fnCC;
865 lineCC *curr_lineCC;
866 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000867
njn25e49d8e72002-09-23 09:36:25 +0000868 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000869
sewardj92645592005-07-23 09:18:34 +0000870 sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
871 VKI_S_IRUSR|VKI_S_IWUSR);
872 if (sres.isError) {
nethercote9313ac42004-07-06 21:54:20 +0000873 // If the file can't be opened for whatever reason (conflict
874 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000875 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000876 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000877 cachegrind_out_file );
878 VG_(message)(Vg_UserMsg,
879 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000880 return;
sewardj92645592005-07-23 09:18:34 +0000881 } else {
882 fd = sres.val;
sewardj0744b6c2002-12-11 00:45:42 +0000883 }
njn4f9c9342002-04-29 16:03:24 +0000884
nethercote9313ac42004-07-06 21:54:20 +0000885 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
886 // the 2nd colon makes cg_annotate's output look nicer.
887 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
888 "desc: D1 cache: %s\n"
889 "desc: L2 cache: %s\n",
890 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000891 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000892
nethercote9313ac42004-07-06 21:54:20 +0000893 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000894 VG_(strcpy)(buf, "cmd:");
895 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000896 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000897 if (VG_(client_argv)[i] == NULL)
898 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000899 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000900 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000901 }
nethercote9313ac42004-07-06 21:54:20 +0000902 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000903 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
904 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
905
nethercote9313ac42004-07-06 21:54:20 +0000906 // Six loops here: three for the hash table arrays, and three for the
907 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000908 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000909 curr_fileCC = CC_table[i];
910 while (curr_fileCC != NULL) {
911 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000912 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
913
914 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000915 curr_fnCC = curr_fileCC->fns[j];
916 while (curr_fnCC != NULL) {
917 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000918 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
919
nethercote9313ac42004-07-06 21:54:20 +0000920 for (k = 0; k < N_LINE_ENTRIES; k++) {
921 curr_lineCC = curr_fnCC->lines[k];
922 while (curr_lineCC != NULL) {
923 fprint_lineCC(fd, curr_lineCC);
924 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000925 }
926 }
nethercote9313ac42004-07-06 21:54:20 +0000927 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000928 }
929 }
nethercote9313ac42004-07-06 21:54:20 +0000930 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000931 }
932 }
933
nethercote9313ac42004-07-06 21:54:20 +0000934 // Summary stats must come after rest of table, since we calculate them
935 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000936 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000937 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000938 Ir_total.a, Ir_total.m1, Ir_total.m2,
939 Dr_total.a, Dr_total.m1, Dr_total.m2,
940 Dw_total.a, Dw_total.m1, Dw_total.m2);
941 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
942 VG_(close)(fd);
943}
944
njn607adfc2003-09-30 14:15:44 +0000945static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000946{
njn607adfc2003-09-30 14:15:44 +0000947 UInt w = 0;
948 while (n > 0) {
949 n = n / 10;
950 w++;
njn4f9c9342002-04-29 16:03:24 +0000951 }
njn607adfc2003-09-30 14:15:44 +0000952 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000953}
954
njn51d827b2005-05-09 01:02:08 +0000955static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000956{
nethercote9313ac42004-07-06 21:54:20 +0000957 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000958
njn4f9c9342002-04-29 16:03:24 +0000959 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000960 ULong L2_total_m, L2_total_mr, L2_total_mw,
961 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000962 Int l1, l2, l3;
963 Int p;
964
nethercote9313ac42004-07-06 21:54:20 +0000965 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000966
njn7cf0bd32002-06-08 13:36:03 +0000967 if (VG_(clo_verbosity) == 0)
968 return;
969
njn4f9c9342002-04-29 16:03:24 +0000970 /* I cache results. Use the I_refs value to determine the first column
971 * width. */
njn607adfc2003-09-30 14:15:44 +0000972 l1 = ULong_width(Ir_total.a);
973 l2 = ULong_width(Dr_total.a);
974 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000975
njn607adfc2003-09-30 14:15:44 +0000976 /* Make format string, getting width right for numbers */
977 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
978
979 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
980 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
981 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000982
983 p = 100;
984
njn25e49d8e72002-09-23 09:36:25 +0000985 if (0 == Ir_total.a) Ir_total.a = 1;
njn856c54e2005-06-26 18:43:40 +0000986 VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1);
njn4f9c9342002-04-29 16:03:24 +0000987 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
988
njn856c54e2005-06-26 18:43:40 +0000989 VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1);
njn4f9c9342002-04-29 16:03:24 +0000990 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
991 VG_(message)(Vg_UserMsg, "");
992
993 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
994 * width of columns 2 & 3. */
995 D_total.a = Dr_total.a + Dw_total.a;
996 D_total.m1 = Dr_total.m1 + Dw_total.m1;
997 D_total.m2 = Dr_total.m2 + Dw_total.m2;
998
njn607adfc2003-09-30 14:15:44 +0000999 /* Make format string, getting width right for numbers */
1000 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +00001001
njn607adfc2003-09-30 14:15:44 +00001002 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
1003 D_total.a, Dr_total.a, Dw_total.a);
1004 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
1005 D_total.m1, Dr_total.m1, Dw_total.m1);
1006 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
1007 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +00001008
1009 p = 10;
1010
njn25e49d8e72002-09-23 09:36:25 +00001011 if (0 == D_total.a) D_total.a = 1;
1012 if (0 == Dr_total.a) Dr_total.a = 1;
1013 if (0 == Dw_total.a) Dw_total.a = 1;
njn856c54e2005-06-26 18:43:40 +00001014 VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1);
1015 VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2);
1016 VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001017 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1018
njn856c54e2005-06-26 18:43:40 +00001019 VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1);
1020 VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2);
1021 VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001022 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
1023 VG_(message)(Vg_UserMsg, "");
1024
1025 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +00001026
1027 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
1028 L2_total_r = Dr_total.m1 + Ir_total.m1;
1029 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +00001030 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
1031 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +00001032
njn4f9c9342002-04-29 16:03:24 +00001033 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
1034 L2_total_mr = Dr_total.m2 + Ir_total.m2;
1035 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +00001036 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
1037 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +00001038
njn856c54e2005-06-26 18:43:40 +00001039 VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1);
1040 VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2);
1041 VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3);
njn4f9c9342002-04-29 16:03:24 +00001042 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1043
1044
nethercote9313ac42004-07-06 21:54:20 +00001045 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001046 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001047 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001048 file_line_debug_BBs + no_debug_BBs;
1049
1050 VG_(message)(Vg_DebugMsg, "");
1051 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1052 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001053 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1054 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001055 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1056 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1057 full_debug_BBs * 100 / BB_lookups,
1058 full_debug_BBs);
1059 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1060 file_line_debug_BBs * 100 / BB_lookups,
1061 file_line_debug_BBs);
1062 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001063 fn_debug_BBs * 100 / BB_lookups,
1064 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001065 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1066 no_debug_BBs * 100 / BB_lookups,
1067 no_debug_BBs);
1068 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001069 }
njn25e49d8e72002-09-23 09:36:25 +00001070 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001071}
1072
nethercote9313ac42004-07-06 21:54:20 +00001073/*--------------------------------------------------------------------*/
1074/*--- Discarding BB info ---*/
1075/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001076
nethercote9313ac42004-07-06 21:54:20 +00001077// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001078static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001079{
njn6a3009b2005-03-20 00:20:06 +00001080 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001081
nethercote928a5f72004-11-03 18:10:37 +00001082 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001083
nethercote9313ac42004-07-06 21:54:20 +00001084 // Get BB info, remove from table, free BB info. Simple!
njn4bd67b52005-08-11 00:47:10 +00001085 bbInfo = VG_(HT_remove)(instr_info_table, a);
njn6a3009b2005-03-20 00:20:06 +00001086 tl_assert(NULL != bbInfo);
njn6a3009b2005-03-20 00:20:06 +00001087 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001088}
1089
1090/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001091/*--- Command line processing ---*/
1092/*--------------------------------------------------------------------*/
1093
nethercote9313ac42004-07-06 21:54:20 +00001094static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001095{
nethercote9313ac42004-07-06 21:54:20 +00001096 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001097
nethercote9313ac42004-07-06 21:54:20 +00001098 // Option argument looks like "65536,2,64".
1099 // Find commas, replace with NULs to make three independent
1100 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001101 while (VG_(isdigit)(opt[i])) i++;
1102 if (',' == opt[i]) {
1103 opt[i++] = '\0';
1104 i2 = i;
1105 } else goto bad;
1106 while (VG_(isdigit)(opt[i])) i++;
1107 if (',' == opt[i]) {
1108 opt[i++] = '\0';
1109 i3 = i;
1110 } else goto bad;
1111 while (VG_(isdigit)(opt[i])) i++;
1112 if ('\0' != opt[i]) goto bad;
1113
nethercote9313ac42004-07-06 21:54:20 +00001114 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001115 cache->assoc = (Int)VG_(atoll)(opt + i2);
1116 cache->line_size = (Int)VG_(atoll)(opt + i3);
1117
nethercote9313ac42004-07-06 21:54:20 +00001118 opt[i2-1] = ',';
1119 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001120 return;
1121
1122 bad:
nethercote9313ac42004-07-06 21:54:20 +00001123 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001124}
1125
njn51d827b2005-05-09 01:02:08 +00001126static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001127{
nethercote9313ac42004-07-06 21:54:20 +00001128 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001129 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001130 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001131 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001132 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001133 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001134 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001135 else
1136 return False;
1137
1138 return True;
1139}
1140
njn51d827b2005-05-09 01:02:08 +00001141static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001142{
njn3e884182003-04-15 13:03:23 +00001143 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001144" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1145" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001146" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1147 );
1148}
1149
njn51d827b2005-05-09 01:02:08 +00001150static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001151{
1152 VG_(printf)(
1153" (none)\n"
1154 );
njn25e49d8e72002-09-23 09:36:25 +00001155}
1156
1157/*--------------------------------------------------------------------*/
1158/*--- Setup ---*/
1159/*--------------------------------------------------------------------*/
1160
njn51d827b2005-05-09 01:02:08 +00001161static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001162{
1163 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001164
nethercoteb35a8b92004-09-11 16:45:27 +00001165 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001166
1167 cachesim_I1_initcache(I1c);
1168 cachesim_D1_initcache(D1c);
1169 cachesim_L2_initcache(L2c);
1170
njn31066fd2005-03-26 00:42:02 +00001171 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1172 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1173 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001174}
1175
njn57ca7ab2005-06-21 23:44:58 +00001176static Char base_dir[VKI_PATH_MAX];
1177
njn51d827b2005-05-09 01:02:08 +00001178static void cg_pre_clo_init(void)
1179{
njn51d827b2005-05-09 01:02:08 +00001180 VG_(details_name) ("Cachegrind");
1181 VG_(details_version) (NULL);
1182 VG_(details_description) ("an I1/D1/L2 cache profiler");
1183 VG_(details_copyright_author)(
1184 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1185 VG_(details_bug_reports_to) (VG_BUGS_TO);
1186 VG_(details_avg_translation_sizeB) ( 155 );
1187
1188 VG_(basic_tool_funcs) (cg_post_clo_init,
1189 cg_instrument,
1190 cg_fini);
1191
1192 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1193 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1194 cg_print_usage,
1195 cg_print_debug_usage);
1196
1197 /* Get working directory */
njn57ca7ab2005-06-21 23:44:58 +00001198 tl_assert( VG_(getcwd)(base_dir, VKI_PATH_MAX) );
njn51d827b2005-05-09 01:02:08 +00001199
1200 /* Block is big enough for dir name + cachegrind.out.<pid> */
1201 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1202 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1203 base_dir, VG_(getpid)());
njn51d827b2005-05-09 01:02:08 +00001204
njnf69f9452005-07-03 17:53:11 +00001205 instr_info_table = VG_(HT_construct)( 4999 ); // prime, biggish
njn51d827b2005-05-09 01:02:08 +00001206}
1207
1208VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001209
njn25e49d8e72002-09-23 09:36:25 +00001210/*--------------------------------------------------------------------*/
njnf69f9452005-07-03 17:53:11 +00001211/*--- end ---*/
sewardj18d75132002-05-16 11:06:21 +00001212/*--------------------------------------------------------------------*/