blob: 64c90c311758bfe7f9a552ac89b52e272527b975 [file] [log] [blame]
sewardj07133bf2002-06-13 10:25:56 +00001
njn4f9c9342002-04-29 16:03:24 +00002/*--------------------------------------------------------------------*/
njn101e5722005-04-21 02:37:54 +00003/*--- Cachegrind: everything but the simulation itself. ---*/
njn25cac76cb2002-09-23 11:21:57 +00004/*--- cg_main.c ---*/
njn4f9c9342002-04-29 16:03:24 +00005/*--------------------------------------------------------------------*/
6
7/*
nethercote137bc552003-11-14 17:47:54 +00008 This file is part of Cachegrind, a Valgrind tool for cache
njnc9539842002-10-02 13:26:35 +00009 profiling programs.
njn4f9c9342002-04-29 16:03:24 +000010
njn53612422005-03-12 16:22:54 +000011 Copyright (C) 2002-2005 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000012 njn@valgrind.org
njn4f9c9342002-04-29 16:03:24 +000013
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 02111-1307, USA.
28
njn25e49d8e72002-09-23 09:36:25 +000029 The GNU General Public License is contained in the file COPYING.
njn4f9c9342002-04-29 16:03:24 +000030*/
31
nethercote46063202004-09-02 08:51:43 +000032#include "tool.h"
njnea27e462005-05-31 02:38:09 +000033#include "pub_tool_debuginfo.h"
njn81c00df2005-05-14 21:28:43 +000034#include "pub_tool_hashtable.h"
njn97405b22005-06-02 03:39:33 +000035#include "pub_tool_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +000036#include "pub_tool_libcassert.h"
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//------------------------------------------------------------
121// Primary data structre #2: Instr-info table
122// - 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 +0000231static __inline__
232instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
233{
njn6a3009b2005-03-20 00:20:06 +0000234 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000235 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
236 ii->instr_addr = instr_addr;
237 ii->parent = parent;
238 return ii;
239}
240
241// Do a three step traversal: by file, then fn, then line.
242// In all cases prepends new nodes to their chain. Returns a pointer to the
243// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000244static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000245{
246 fileCC *curr_fileCC;
247 fnCC *curr_fnCC;
248 lineCC *curr_lineCC;
249 Char file[FILE_LEN], fn[FN_LEN];
250 Int line;
251 UInt file_hash, fn_hash, line_hash;
252
njn6a3009b2005-03-20 00:20:06 +0000253 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000254
255 VGP_PUSHCC(VgpGetLineCC);
256
257 // level 1
258 file_hash = hash(file, N_FILE_ENTRIES);
259 curr_fileCC = CC_table[file_hash];
260 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
261 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000262 }
nethercote9313ac42004-07-06 21:54:20 +0000263 if (NULL == curr_fileCC) {
264 CC_table[file_hash] = curr_fileCC =
265 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000266 distinct_files++;
267 }
268
nethercote9313ac42004-07-06 21:54:20 +0000269 // level 2
270 fn_hash = hash(fn, N_FN_ENTRIES);
271 curr_fnCC = curr_fileCC->fns[fn_hash];
272 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
273 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000274 }
nethercote9313ac42004-07-06 21:54:20 +0000275 if (NULL == curr_fnCC) {
276 curr_fileCC->fns[fn_hash] = curr_fnCC =
277 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000278 distinct_fns++;
279 }
280
nethercote9313ac42004-07-06 21:54:20 +0000281 // level 3
282 line_hash = line % N_LINE_ENTRIES;
283 curr_lineCC = curr_fnCC->lines[line_hash];
284 while (NULL != curr_lineCC && line != curr_lineCC->line) {
285 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000286 }
nethercote9313ac42004-07-06 21:54:20 +0000287 if (NULL == curr_lineCC) {
288 curr_fnCC->lines[line_hash] = curr_lineCC =
289 new_lineCC(line, curr_fnCC->lines[line_hash]);
290 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000291 }
nethercote9313ac42004-07-06 21:54:20 +0000292
293 VGP_POPCC(VgpGetLineCC);
294 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000295}
296
297/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000298/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000299/*------------------------------------------------------------*/
300
njn9fb73db2005-03-27 01:55:21 +0000301static VGA_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000302void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000303{
njn6a3009b2005-03-20 00:20:06 +0000304 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
305 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000306 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000307 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000308 &n->parent->Ir.m1, &n->parent->Ir.m2);
309 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000310 VGP_POPCC(VgpCacheSimulate);
311}
312
njn9fb73db2005-03-27 01:55:21 +0000313static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000314void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000315{
nethercote9313ac42004-07-06 21:54:20 +0000316 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000317 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000318 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000319 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000320 &n->parent->Ir.m1, &n->parent->Ir.m2);
321 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000322
nethercote9313ac42004-07-06 21:54:20 +0000323 cachesim_D1_doref(data_addr, n->data_size,
324 &n->parent->Dr.m1, &n->parent->Dr.m2);
325 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000326 VGP_POPCC(VgpCacheSimulate);
327}
328
njn9fb73db2005-03-27 01:55:21 +0000329static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000330void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000331{
nethercote9313ac42004-07-06 21:54:20 +0000332 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000333 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000334 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000335 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000336 &n->parent->Ir.m1, &n->parent->Ir.m2);
337 n->parent->Ir.a++;
338
339 cachesim_D1_doref(data_addr, n->data_size,
340 &n->parent->Dw.m1, &n->parent->Dw.m2);
341 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000342 VGP_POPCC(VgpCacheSimulate);
343}
344
njn9fb73db2005-03-27 01:55:21 +0000345static VGA_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000346void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000347{
348 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000349 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000350 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000351 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000352 &n->parent->Ir.m1, &n->parent->Ir.m2);
353 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000354
nethercote9313ac42004-07-06 21:54:20 +0000355 cachesim_D1_doref(data_addr1, n->data_size,
356 &n->parent->Dr.m1, &n->parent->Dr.m2);
357 n->parent->Dr.a++;
358 cachesim_D1_doref(data_addr2, n->data_size,
359 &n->parent->Dw.m1, &n->parent->Dw.m2);
360 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000361 VGP_POPCC(VgpCacheSimulate);
362}
363
nethercote9313ac42004-07-06 21:54:20 +0000364/*------------------------------------------------------------*/
365/*--- Instrumentation ---*/
366/*------------------------------------------------------------*/
367
nethercote564b2b02004-08-07 15:54:53 +0000368static
njn6a3009b2005-03-20 00:20:06 +0000369BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000370{
371 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000372 IRStmt* st;
373 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000374 VgHashNode** dummy;
375
njn6a3009b2005-03-20 00:20:06 +0000376 // Count number of original instrs in BB
377 n_instrs = 0;
378 for (i = 0; i < bbIn->stmts_used; i++) {
379 st = bbIn->stmts[i];
380 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000381 }
382
383 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000384 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
385 *bbSeenBefore = ( NULL == bbInfo ? False : True );
386 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000387 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000388 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000389 BB_retranslations++;
390 } else {
391 // BB never translated before (at this address, at least; could have
392 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000393 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
394 bbInfo->BB_addr = origAddr;
395 bbInfo->n_instrs = n_instrs;
396 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000397 distinct_instrs++;
398 }
njn6a3009b2005-03-20 00:20:06 +0000399 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000400}
njn6a3009b2005-03-20 00:20:06 +0000401
njn6a3009b2005-03-20 00:20:06 +0000402static
403void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
404 Addr* instrAddr, UInt* instrLen,
405 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
406 UInt* dataSize)
407{
sewardj7f4a8622005-03-26 21:55:21 +0000408 tl_assert(isFlatIRStmt(st));
409
njn6a3009b2005-03-20 00:20:06 +0000410 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000411 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000412 break;
413
sewardje6da2fa2005-05-12 17:57:14 +0000414 case Ist_AbiHint:
415 /* ABI hints aren't interesting to cachegrind. Ignore. */
416 break;
417
njn6a3009b2005-03-20 00:20:06 +0000418 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000419 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
420 to the host's native pointer type; if that is 32 bits then it
421 discards the upper 32 bits. If we are cachegrinding on a
422 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000423 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000424 host and guest word sizes must be the same. Hence
425 st->Ist.IMark.addr will have been derived from a 32-bit guest
426 code address and truncation of it is safe. I believe this
427 assignment should be correct for both 32- and 64-bit
428 machines. */
429 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000430 *instrLen = st->Ist.IMark.len;
431 addStmtToIRBB( bbOut, st );
432 break;
433
434 case Ist_Tmp: {
435 IRExpr* data = st->Ist.Tmp.data;
436 if (data->tag == Iex_LDle) {
437 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000438 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000439
440 // XXX: repe cmpsb does two loads... the first one is ignored here!
441 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
442 *loadAddrExpr = aexpr;
443 *dataSize = sizeofIRType(data->Iex.LDle.ty);
444 }
445 addStmtToIRBB( bbOut, st );
446 break;
447 }
448
449 case Ist_STle: {
450 IRExpr* data = st->Ist.STle.data;
451 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000452 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000453 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
454 *storeAddrExpr = aexpr;
455 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
456 addStmtToIRBB( bbOut, st );
457 break;
458 }
459
sewardj7f4a8622005-03-26 21:55:21 +0000460 case Ist_Dirty: {
461 IRDirty* d = st->Ist.Dirty.details;
462 if (d->mFx != Ifx_None) {
463 /* This dirty helper accesses memory. Collect the
464 details. */
465 tl_assert(d->mAddr != NULL);
466 tl_assert(d->mSize != 0);
467 *dataSize = d->mSize;
468 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
469 *loadAddrExpr = d->mAddr;
470 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
471 *storeAddrExpr = d->mAddr;
472 } else {
473 tl_assert(d->mAddr == NULL);
474 tl_assert(d->mSize == 0);
475 }
476 addStmtToIRBB( bbOut, st );
477 break;
478 }
479
njn6a3009b2005-03-20 00:20:06 +0000480 case Ist_Put:
481 case Ist_PutI:
482 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000483 case Ist_MFence:
484 addStmtToIRBB( bbOut, st );
485 break;
486
487 default:
488 VG_(printf)("\n");
489 ppIRStmt(st);
490 VG_(printf)("\n");
491 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
492 }
493}
nethercote9313ac42004-07-06 21:54:20 +0000494
nethercote564b2b02004-08-07 15:54:53 +0000495static
njn6a3009b2005-03-20 00:20:06 +0000496void do_details( instr_info* n, Bool bbSeenBefore,
497 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000498{
njn6a3009b2005-03-20 00:20:06 +0000499 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000500 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000501 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000502 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000503 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000504 // the debug info might change; the other asserts should be enough to
505 // detect anything strange.
506 } else {
njn6a3009b2005-03-20 00:20:06 +0000507 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000508 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000509 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000510 n->data_size = data_size;
511 n->parent = parent;
512 }
513}
514
njn6a3009b2005-03-20 00:20:06 +0000515static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000516{
njn6a3009b2005-03-20 00:20:06 +0000517 // I'm assuming that for 'modify' instructions, that Vex always makes
518 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
519 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000520 tl_assert(isIRAtom(loadAddrExpr));
521 tl_assert(isIRAtom(storeAddrExpr));
522 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000523}
524
525// Instrumentation for the end of each original instruction.
526static
527void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
528 UInt instrAddr, UInt instrLen, UInt dataSize,
529 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
530{
531 IRDirty* di;
532 IRExpr *arg1, *arg2, *arg3, **argv;
533 Int argc;
534 Char* helperName;
535 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000536 IRType wordTy;
537
538 // Stay sane ...
539 tl_assert(sizeof(HWord) == sizeof(void*));
540 if (sizeof(HWord) == 4) {
541 wordTy = Ity_I32;
542 } else
543 if (sizeof(HWord) == 8) {
544 wordTy = Ity_I64;
545 } else {
546 VG_(tool_panic)("endOfInstr: strange word size");
547 }
548
549 if (loadAddrExpr)
550 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
551 if (storeAddrExpr)
552 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
553
njn6a3009b2005-03-20 00:20:06 +0000554
555 // Nb: instrLen will be zero if Vex failed to decode it.
556 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000557 (instrLen >= VGA_MIN_INSTR_SZB &&
558 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000559
njn016712a2005-04-04 02:52:16 +0000560 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
561 // done inaccurately, but they're very rare and this avoids errors from
562 // hitting more than two cache lines in the simulation.
563 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
564
njn6a3009b2005-03-20 00:20:06 +0000565 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000566 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000567 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000568 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000569
570 if (!loadAddrExpr && !storeAddrExpr) {
571 // no load/store
572 tl_assert(0 == dataSize);
573 helperName = "log_1I_0D_cache_access";
574 helperAddr = &log_1I_0D_cache_access;
575 argc = 1;
576 argv = mkIRExprVec_1(arg1);
577
578 } else if (loadAddrExpr && !storeAddrExpr) {
579 // load
sewardj710d6c22005-03-20 18:55:15 +0000580 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000581 helperName = "log_1I_1Dr_cache_access";
582 helperAddr = &log_1I_1Dr_cache_access;
583 argc = 2;
584 arg2 = loadAddrExpr;
585 argv = mkIRExprVec_2(arg1, arg2);
586
587 } else if (!loadAddrExpr && storeAddrExpr) {
588 // store
sewardj710d6c22005-03-20 18:55:15 +0000589 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000590 helperName = "log_1I_1Dw_cache_access";
591 helperAddr = &log_1I_1Dw_cache_access;
592 argc = 2;
593 arg2 = storeAddrExpr;
594 argv = mkIRExprVec_2(arg1, arg2);
595
596 } else {
597 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000598 tl_assert( isIRAtom(loadAddrExpr) );
599 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000600
601 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
602 // modify
603 helperName = "log_1I_1Dr_cache_access";
604 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000605 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000606 arg2 = loadAddrExpr;
607 argv = mkIRExprVec_2(arg1, arg2);
608
nethercote9313ac42004-07-06 21:54:20 +0000609 } else {
njn6a3009b2005-03-20 00:20:06 +0000610 // load/store
611 helperName = "log_1I_2D_cache_access";
612 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000613 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000614 arg2 = loadAddrExpr;
615 arg3 = storeAddrExpr;
616 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000617 }
618 }
619
njn6a3009b2005-03-20 00:20:06 +0000620 // Add call to the instrumentation function
621 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
622 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000623}
njn14d01ce2004-11-26 11:30:14 +0000624
njn51d827b2005-05-09 01:02:08 +0000625static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
626 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000627{
njn6a3009b2005-03-20 00:20:06 +0000628 Int i, dataSize = 0, bbInfo_i;
629 IRBB* bbOut;
630 IRStmt* st;
631 BB_info* bbInfo;
632 Bool bbSeenBefore = False;
633 Addr instrAddr, origAddr;
634 UInt instrLen;
635 IRExpr *loadAddrExpr, *storeAddrExpr;
636
sewardjd54babf2005-03-21 00:55:49 +0000637 if (gWordTy != hWordTy) {
638 /* We don't currently support this case. */
639 VG_(tool_panic)("host/guest word size mismatch");
640 }
641
njn6a3009b2005-03-20 00:20:06 +0000642 /* Set up BB */
643 bbOut = emptyIRBB();
644 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
645 bbOut->next = dopyIRExpr(bbIn->next);
646 bbOut->jumpkind = bbIn->jumpkind;
647
648 // Get the first statement, and origAddr from it
649 i = 0;
650 tl_assert(bbIn->stmts_used > 0);
651 st = bbIn->stmts[0];
652 tl_assert(Ist_IMark == st->tag);
653 origAddr = (Addr)st->Ist.IMark.addr;
654 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
655
656 // Get block info
657 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
658 bbInfo_i = 0;
659
660 do {
661 // We should be at an IMark statement
662 tl_assert(Ist_IMark == st->tag);
663
664 // Reset stuff for this original instruction
665 loadAddrExpr = storeAddrExpr = NULL;
666 dataSize = 0;
667
668 // Process all the statements for this original instruction (ie. until
669 // the next IMark statement, or the end of the block)
670 do {
671 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
672 &loadAddrExpr, &storeAddrExpr, &dataSize);
673 i++;
674 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
675 }
676 while (st && Ist_IMark != st->tag);
677
678 // Add instrumentation for this original instruction.
679 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
680 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
681
682 bbInfo_i++;
683 }
684 while (st);
685
686 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000687}
njn4f9c9342002-04-29 16:03:24 +0000688
689/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000690/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000691/*------------------------------------------------------------*/
692
sewardjb5f6f512005-03-10 23:59:00 +0000693#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000694
695static cache_t clo_I1_cache = UNDEFINED_CACHE;
696static cache_t clo_D1_cache = UNDEFINED_CACHE;
697static cache_t clo_L2_cache = UNDEFINED_CACHE;
698
njn7cf0bd32002-06-08 13:36:03 +0000699/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000700static
njna1d1a642004-11-26 18:36:02 +0000701void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000702{
703 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000704 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000705 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000706 "error: %s size of %dB not a power of two; aborting.",
707 name, cache->size);
708 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000709 }
710
sewardj07133bf2002-06-13 10:25:56 +0000711 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000712 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000713 "error: %s associativity of %d not a power of two; aborting.",
714 name, cache->assoc);
715 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000716 }
717
sewardj07133bf2002-06-13 10:25:56 +0000718 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000719 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000720 "error: %s line size of %dB not a power of two; aborting.",
721 name, cache->line_size);
722 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000723 }
724
njn6a3009b2005-03-20 00:20:06 +0000725 // Then check line size >= 16 -- any smaller and a single instruction could
726 // straddle three cache lines, which breaks a simulation assertion and is
727 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000728 if (cache->line_size < MIN_LINE_SIZE) {
729 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000730 "error: %s line size of %dB too small; aborting.",
731 name, cache->line_size);
732 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000733 }
734
735 /* Then check cache size > line size (causes seg faults if not). */
736 if (cache->size <= cache->line_size) {
737 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000738 "error: %s cache size of %dB <= line size of %dB; aborting.",
739 name, cache->size, cache->line_size);
740 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000741 }
742
743 /* Then check assoc <= (size / line size) (seg faults otherwise). */
744 if (cache->assoc > (cache->size / cache->line_size)) {
745 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000746 "warning: %s associativity > (size / line size); aborting.", name);
747 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000748 }
749}
750
sewardj07133bf2002-06-13 10:25:56 +0000751static
nethercoteb35a8b92004-09-11 16:45:27 +0000752void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000753{
nethercote9313ac42004-07-06 21:54:20 +0000754#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
755
nethercoteb35a8b92004-09-11 16:45:27 +0000756 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000757
nethercoteb35a8b92004-09-11 16:45:27 +0000758 // Count how many were defined on the command line.
759 if (DEFINED(clo_I1_cache)) { n_clos++; }
760 if (DEFINED(clo_D1_cache)) { n_clos++; }
761 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000762
njna1d1a642004-11-26 18:36:02 +0000763 // Set the cache config (using auto-detection, if supported by the
764 // architecture)
765 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000766
nethercote9313ac42004-07-06 21:54:20 +0000767 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000768 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
769 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
770 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000771
nethercote9313ac42004-07-06 21:54:20 +0000772 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000773 check_cache(I1c, "I1");
774 check_cache(D1c, "D1");
775 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000776
777 if (VG_(clo_verbosity) > 1) {
778 VG_(message)(Vg_UserMsg, "Cache configuration used:");
779 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
780 I1c->size, I1c->assoc, I1c->line_size);
781 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
782 D1c->size, D1c->assoc, D1c->line_size);
783 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
784 L2c->size, L2c->assoc, L2c->line_size);
785 }
nethercote9313ac42004-07-06 21:54:20 +0000786#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000787}
788
njn4f9c9342002-04-29 16:03:24 +0000789/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000790/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000791/*------------------------------------------------------------*/
792
nethercote9313ac42004-07-06 21:54:20 +0000793// Total reads/writes/misses. Calculated during CC traversal at the end.
794// All auto-zeroed.
795static CC Ir_total;
796static CC Dr_total;
797static CC Dw_total;
798
799static Char* cachegrind_out_file;
800
nethercote9313ac42004-07-06 21:54:20 +0000801static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000802{
nethercote9313ac42004-07-06 21:54:20 +0000803 Char buf[512];
804 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
805 n->line,
806 n->Ir.a, n->Ir.m1, n->Ir.m2,
807 n->Dr.a, n->Dr.m1, n->Dr.m2,
808 n->Dw.a, n->Dw.m1, n->Dw.m2);
809 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
810
811 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
812 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
813 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
814}
815
816static void fprint_CC_table_and_calc_totals(void)
817{
818 Int fd;
819 Char buf[512];
820 fileCC *curr_fileCC;
821 fnCC *curr_fnCC;
822 lineCC *curr_lineCC;
823 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000824
njn25e49d8e72002-09-23 09:36:25 +0000825 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000826
njndb918dd2003-07-22 20:45:11 +0000827 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000828 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000829 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000830 // If the file can't be opened for whatever reason (conflict
831 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000832 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000833 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000834 cachegrind_out_file );
835 VG_(message)(Vg_UserMsg,
836 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000837 return;
838 }
njn4f9c9342002-04-29 16:03:24 +0000839
nethercote9313ac42004-07-06 21:54:20 +0000840 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
841 // the 2nd colon makes cg_annotate's output look nicer.
842 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
843 "desc: D1 cache: %s\n"
844 "desc: L2 cache: %s\n",
845 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000846 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000847
nethercote9313ac42004-07-06 21:54:20 +0000848 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000849 VG_(strcpy)(buf, "cmd:");
850 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000851 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000852 if (VG_(client_argv)[i] == NULL)
853 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000854 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000855 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000856 }
nethercote9313ac42004-07-06 21:54:20 +0000857 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000858 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
859 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
860
nethercote9313ac42004-07-06 21:54:20 +0000861 // Six loops here: three for the hash table arrays, and three for the
862 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000863 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000864 curr_fileCC = CC_table[i];
865 while (curr_fileCC != NULL) {
866 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000867 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
868
869 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000870 curr_fnCC = curr_fileCC->fns[j];
871 while (curr_fnCC != NULL) {
872 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000873 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
874
nethercote9313ac42004-07-06 21:54:20 +0000875 for (k = 0; k < N_LINE_ENTRIES; k++) {
876 curr_lineCC = curr_fnCC->lines[k];
877 while (curr_lineCC != NULL) {
878 fprint_lineCC(fd, curr_lineCC);
879 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000880 }
881 }
nethercote9313ac42004-07-06 21:54:20 +0000882 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000883 }
884 }
nethercote9313ac42004-07-06 21:54:20 +0000885 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000886 }
887 }
888
nethercote9313ac42004-07-06 21:54:20 +0000889 // Summary stats must come after rest of table, since we calculate them
890 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000891 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000892 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000893 Ir_total.a, Ir_total.m1, Ir_total.m2,
894 Dr_total.a, Dr_total.m1, Dr_total.m2,
895 Dw_total.a, Dw_total.m1, Dw_total.m2);
896 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
897 VG_(close)(fd);
898}
899
njn607adfc2003-09-30 14:15:44 +0000900static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000901{
njn607adfc2003-09-30 14:15:44 +0000902 UInt w = 0;
903 while (n > 0) {
904 n = n / 10;
905 w++;
njn4f9c9342002-04-29 16:03:24 +0000906 }
njn607adfc2003-09-30 14:15:44 +0000907 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000908}
909
sewardj4f29ddf2002-05-03 22:29:04 +0000910static
daywalker8ad1a402003-09-18 01:15:32 +0000911void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000912{
913 int i, len, space;
914
daywalker8ad1a402003-09-18 01:15:32 +0000915 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000916 len = VG_(strlen)(buf);
917 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000918 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000919 i = len;
920
921 /* Right justify in field */
922 for ( ; i >= 0; i--) buf[i + space] = buf[i];
923 for (i = 0; i < space; i++) buf[i] = ' ';
924}
925
njn51d827b2005-05-09 01:02:08 +0000926static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000927{
nethercote9313ac42004-07-06 21:54:20 +0000928 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000929
njn4f9c9342002-04-29 16:03:24 +0000930 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000931 ULong L2_total_m, L2_total_mr, L2_total_mw,
932 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000933 Int l1, l2, l3;
934 Int p;
935
nethercote9313ac42004-07-06 21:54:20 +0000936 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000937
njn7cf0bd32002-06-08 13:36:03 +0000938 if (VG_(clo_verbosity) == 0)
939 return;
940
njn4f9c9342002-04-29 16:03:24 +0000941 /* I cache results. Use the I_refs value to determine the first column
942 * width. */
njn607adfc2003-09-30 14:15:44 +0000943 l1 = ULong_width(Ir_total.a);
944 l2 = ULong_width(Dr_total.a);
945 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000946
njn607adfc2003-09-30 14:15:44 +0000947 /* Make format string, getting width right for numbers */
948 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
949
950 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
951 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
952 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000953
954 p = 100;
955
njn25e49d8e72002-09-23 09:36:25 +0000956 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000957 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
958 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
959
960 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
961 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
962 VG_(message)(Vg_UserMsg, "");
963
964 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
965 * width of columns 2 & 3. */
966 D_total.a = Dr_total.a + Dw_total.a;
967 D_total.m1 = Dr_total.m1 + Dw_total.m1;
968 D_total.m2 = Dr_total.m2 + Dw_total.m2;
969
njn607adfc2003-09-30 14:15:44 +0000970 /* Make format string, getting width right for numbers */
971 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000972
njn607adfc2003-09-30 14:15:44 +0000973 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
974 D_total.a, Dr_total.a, Dw_total.a);
975 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
976 D_total.m1, Dr_total.m1, Dw_total.m1);
977 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
978 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000979
980 p = 10;
981
njn25e49d8e72002-09-23 09:36:25 +0000982 if (0 == D_total.a) D_total.a = 1;
983 if (0 == Dr_total.a) Dr_total.a = 1;
984 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000985 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
986 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
987 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
988 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
989
990 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
991 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
992 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
993 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
994 VG_(message)(Vg_UserMsg, "");
995
996 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000997
998 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
999 L2_total_r = Dr_total.m1 + Ir_total.m1;
1000 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +00001001 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
1002 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +00001003
njn4f9c9342002-04-29 16:03:24 +00001004 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
1005 L2_total_mr = Dr_total.m2 + Ir_total.m2;
1006 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +00001007 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
1008 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +00001009
1010 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
1011 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
1012 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
1013 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1014
1015
nethercote9313ac42004-07-06 21:54:20 +00001016 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001017 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001018 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001019 file_line_debug_BBs + no_debug_BBs;
1020
1021 VG_(message)(Vg_DebugMsg, "");
1022 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1023 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001024 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1025 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001026 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1027 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1028 full_debug_BBs * 100 / BB_lookups,
1029 full_debug_BBs);
1030 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1031 file_line_debug_BBs * 100 / BB_lookups,
1032 file_line_debug_BBs);
1033 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001034 fn_debug_BBs * 100 / BB_lookups,
1035 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001036 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1037 no_debug_BBs * 100 / BB_lookups,
1038 no_debug_BBs);
1039 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001040 }
njn25e49d8e72002-09-23 09:36:25 +00001041 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001042}
1043
nethercote9313ac42004-07-06 21:54:20 +00001044/*--------------------------------------------------------------------*/
1045/*--- Discarding BB info ---*/
1046/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001047
nethercote9313ac42004-07-06 21:54:20 +00001048// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001049static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001050{
nethercote9313ac42004-07-06 21:54:20 +00001051 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001052 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001053
nethercote928a5f72004-11-03 18:10:37 +00001054 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001055
nethercote9313ac42004-07-06 21:54:20 +00001056 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001057 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1058 tl_assert(NULL != bbInfo);
1059 *prev_next_ptr = bbInfo->next;
1060 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001061}
1062
1063/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001064/*--- Command line processing ---*/
1065/*--------------------------------------------------------------------*/
1066
nethercote9313ac42004-07-06 21:54:20 +00001067static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001068{
nethercote9313ac42004-07-06 21:54:20 +00001069 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001070
nethercote9313ac42004-07-06 21:54:20 +00001071 // Option argument looks like "65536,2,64".
1072 // Find commas, replace with NULs to make three independent
1073 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001074 while (VG_(isdigit)(opt[i])) i++;
1075 if (',' == opt[i]) {
1076 opt[i++] = '\0';
1077 i2 = i;
1078 } else goto bad;
1079 while (VG_(isdigit)(opt[i])) i++;
1080 if (',' == opt[i]) {
1081 opt[i++] = '\0';
1082 i3 = i;
1083 } else goto bad;
1084 while (VG_(isdigit)(opt[i])) i++;
1085 if ('\0' != opt[i]) goto bad;
1086
nethercote9313ac42004-07-06 21:54:20 +00001087 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001088 cache->assoc = (Int)VG_(atoll)(opt + i2);
1089 cache->line_size = (Int)VG_(atoll)(opt + i3);
1090
nethercote9313ac42004-07-06 21:54:20 +00001091 opt[i2-1] = ',';
1092 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001093 return;
1094
1095 bad:
nethercote9313ac42004-07-06 21:54:20 +00001096 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001097}
1098
njn51d827b2005-05-09 01:02:08 +00001099static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001100{
nethercote9313ac42004-07-06 21:54:20 +00001101 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001102 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001103 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001104 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001105 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001106 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001107 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001108 else
1109 return False;
1110
1111 return True;
1112}
1113
njn51d827b2005-05-09 01:02:08 +00001114static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001115{
njn3e884182003-04-15 13:03:23 +00001116 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001117" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1118" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001119" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1120 );
1121}
1122
njn51d827b2005-05-09 01:02:08 +00001123static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001124{
1125 VG_(printf)(
1126" (none)\n"
1127 );
njn25e49d8e72002-09-23 09:36:25 +00001128}
1129
1130/*--------------------------------------------------------------------*/
1131/*--- Setup ---*/
1132/*--------------------------------------------------------------------*/
1133
njn51d827b2005-05-09 01:02:08 +00001134static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001135{
1136 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001137
nethercoteb35a8b92004-09-11 16:45:27 +00001138 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001139
1140 cachesim_I1_initcache(I1c);
1141 cachesim_D1_initcache(D1c);
1142 cachesim_L2_initcache(L2c);
1143
njn31066fd2005-03-26 00:42:02 +00001144 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1145 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1146 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001147}
1148
njn51d827b2005-05-09 01:02:08 +00001149static void cg_pre_clo_init(void)
1150{
1151 Char* base_dir = NULL;
1152
1153 VG_(details_name) ("Cachegrind");
1154 VG_(details_version) (NULL);
1155 VG_(details_description) ("an I1/D1/L2 cache profiler");
1156 VG_(details_copyright_author)(
1157 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1158 VG_(details_bug_reports_to) (VG_BUGS_TO);
1159 VG_(details_avg_translation_sizeB) ( 155 );
1160
1161 VG_(basic_tool_funcs) (cg_post_clo_init,
1162 cg_instrument,
1163 cg_fini);
1164
1165 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1166 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1167 cg_print_usage,
1168 cg_print_debug_usage);
1169
1170 /* Get working directory */
1171 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1172
1173 /* Block is big enough for dir name + cachegrind.out.<pid> */
1174 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1175 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1176 base_dir, VG_(getpid)());
1177 VG_(free)(base_dir);
1178
1179 instr_info_table = VG_(HT_construct)();
1180}
1181
1182VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001183
njn25e49d8e72002-09-23 09:36:25 +00001184/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001185/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001186/*--------------------------------------------------------------------*/