blob: b4ec110eb77607e3a51a77d81b59def0a4067e3e [file] [log] [blame]
sewardj07133bf2002-06-13 10:25:56 +00001
njn4f9c9342002-04-29 16:03:24 +00002/*--------------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +00003/*--- Cachegrind: every 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
sewardj3c23d432002-06-01 23:43:49 +000012 njn25@cam.ac.uk
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"
njn25e49d8e72002-09-23 09:36:25 +000033//#include "vg_profile.c"
34
nethercoteb35a8b92004-09-11 16:45:27 +000035#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000036#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000037
njn25e49d8e72002-09-23 09:36:25 +000038/*------------------------------------------------------------*/
39/*--- Constants ---*/
40/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000041
nethercote9313ac42004-07-06 21:54:20 +000042#define MIN_LINE_SIZE 16
43#define FILE_LEN 256
44#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000045
46/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000047/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000048/*------------------------------------------------------------*/
49
njn25e49d8e72002-09-23 09:36:25 +000050typedef
51 enum {
nethercote9313ac42004-07-06 21:54:20 +000052 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000053 VgpCacheSimulate,
54 VgpCacheResults
55 }
nethercote7cc9c232004-01-21 15:08:04 +000056 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000057
njn4f9c9342002-04-29 16:03:24 +000058/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000059/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000060/*------------------------------------------------------------*/
61
62typedef struct _CC CC;
63struct _CC {
64 ULong a;
65 ULong m1;
66 ULong m2;
67};
68
nethercote9313ac42004-07-06 21:54:20 +000069//------------------------------------------------------------
70// Primary data structure #1: CC table
71// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
72// - hash(file, hash(fn, hash(line+CC)))
73// - Each hash table is separately chained.
74// - The array sizes below work fairly well for Konqueror.
75// - Lookups done by instr_addr, which is converted immediately to a source
76// location.
77// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000078
79#define N_FILE_ENTRIES 251
80#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000081#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000082
nethercote9313ac42004-07-06 21:54:20 +000083typedef struct _lineCC lineCC;
84struct _lineCC {
85 Int line;
86 CC Ir;
87 CC Dr;
88 CC Dw;
89 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +000090};
91
nethercote9313ac42004-07-06 21:54:20 +000092typedef struct _fnCC fnCC;
93struct _fnCC {
94 Char* fn;
95 fnCC* next;
96 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +000097};
98
nethercote9313ac42004-07-06 21:54:20 +000099typedef struct _fileCC fileCC;
100struct _fileCC {
101 Char* file;
102 fileCC* next;
103 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000104};
105
nethercote9313ac42004-07-06 21:54:20 +0000106// Top level of CC table. Auto-zeroed.
107static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000108
nethercote9313ac42004-07-06 21:54:20 +0000109//------------------------------------------------------------
110// Primary data structre #2: Instr-info table
111// - Holds the cached info about each instr that is used for simulation.
112// - table(BB_start_addr, list(instr_info))
113// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000114// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000115// CC. This node is what's passed to the simulation function.
116// - When BBs are discarded the relevant list(instr_details) is freed.
117
118typedef struct _instr_info instr_info;
119struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000120 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000121 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000122 UChar data_size;
123 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000124};
125
126typedef struct _BB_info BB_info;
127struct _BB_info {
128 BB_info* next; // next field
129 Addr BB_addr; // key
130 Int n_instrs;
131 instr_info instrs[0];
132};
133
134VgHashTable instr_info_table; // hash(Addr, BB_info)
135
136//------------------------------------------------------------
137// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000138static Int distinct_files = 0;
139static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000140static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000141static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000142
sewardj4f29ddf2002-05-03 22:29:04 +0000143static Int full_debug_BBs = 0;
144static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000145static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000146static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000147
sewardj4f29ddf2002-05-03 22:29:04 +0000148static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000149
nethercote9313ac42004-07-06 21:54:20 +0000150/*------------------------------------------------------------*/
151/*--- CC table operations ---*/
152/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000153
nethercote9313ac42004-07-06 21:54:20 +0000154static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
155 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000156{
nethercote9313ac42004-07-06 21:54:20 +0000157 Bool found_file_line = VG_(get_filename_linenum)(instr_addr, file,
158 FILE_LEN, line);
159 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000160
nethercote9313ac42004-07-06 21:54:20 +0000161 if (!found_file_line) {
162 VG_(strcpy)(file, "???");
163 *line = 0;
164 }
165 if (!found_fn) {
166 VG_(strcpy)(fn, "???");
167 }
168 if (found_file_line) {
169 if (found_fn) full_debug_BBs++;
170 else file_line_debug_BBs++;
171 } else {
172 if (found_fn) fn_debug_BBs++;
173 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000174 }
175}
176
njn4f9c9342002-04-29 16:03:24 +0000177static UInt hash(Char *s, UInt table_size)
178{
nethercote9313ac42004-07-06 21:54:20 +0000179 const int hash_constant = 256;
180 int hash_value = 0;
181 for ( ; *s; s++)
182 hash_value = (hash_constant * hash_value + *s) % table_size;
183 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000184}
185
nethercote9313ac42004-07-06 21:54:20 +0000186static __inline__
187fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000188{
nethercote9313ac42004-07-06 21:54:20 +0000189 // Using calloc() zeroes the fns[] array
190 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
191 cc->file = VG_(strdup)(filename);
192 cc->next = next;
193 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000194}
195
nethercote9313ac42004-07-06 21:54:20 +0000196static __inline__
197fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000198{
nethercote9313ac42004-07-06 21:54:20 +0000199 // Using calloc() zeroes the lines[] array
200 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
201 cc->fn = VG_(strdup)(fn);
202 cc->next = next;
203 return cc;
204}
njn4f9c9342002-04-29 16:03:24 +0000205
nethercote9313ac42004-07-06 21:54:20 +0000206static __inline__
207lineCC* new_lineCC(Int line, lineCC* next)
208{
209 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
210 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
211 cc->line = line;
212 cc->next = next;
213 return cc;
214}
njn4f9c9342002-04-29 16:03:24 +0000215
nethercote9313ac42004-07-06 21:54:20 +0000216static __inline__
217instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
218{
njn6a3009b2005-03-20 00:20:06 +0000219 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000220 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
221 ii->instr_addr = instr_addr;
222 ii->parent = parent;
223 return ii;
224}
225
226// Do a three step traversal: by file, then fn, then line.
227// In all cases prepends new nodes to their chain. Returns a pointer to the
228// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000229static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000230{
231 fileCC *curr_fileCC;
232 fnCC *curr_fnCC;
233 lineCC *curr_lineCC;
234 Char file[FILE_LEN], fn[FN_LEN];
235 Int line;
236 UInt file_hash, fn_hash, line_hash;
237
njn6a3009b2005-03-20 00:20:06 +0000238 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000239
240 VGP_PUSHCC(VgpGetLineCC);
241
242 // level 1
243 file_hash = hash(file, N_FILE_ENTRIES);
244 curr_fileCC = CC_table[file_hash];
245 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
246 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000247 }
nethercote9313ac42004-07-06 21:54:20 +0000248 if (NULL == curr_fileCC) {
249 CC_table[file_hash] = curr_fileCC =
250 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000251 distinct_files++;
252 }
253
nethercote9313ac42004-07-06 21:54:20 +0000254 // level 2
255 fn_hash = hash(fn, N_FN_ENTRIES);
256 curr_fnCC = curr_fileCC->fns[fn_hash];
257 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
258 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000259 }
nethercote9313ac42004-07-06 21:54:20 +0000260 if (NULL == curr_fnCC) {
261 curr_fileCC->fns[fn_hash] = curr_fnCC =
262 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000263 distinct_fns++;
264 }
265
nethercote9313ac42004-07-06 21:54:20 +0000266 // level 3
267 line_hash = line % N_LINE_ENTRIES;
268 curr_lineCC = curr_fnCC->lines[line_hash];
269 while (NULL != curr_lineCC && line != curr_lineCC->line) {
270 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000271 }
nethercote9313ac42004-07-06 21:54:20 +0000272 if (NULL == curr_lineCC) {
273 curr_fnCC->lines[line_hash] = curr_lineCC =
274 new_lineCC(line, curr_fnCC->lines[line_hash]);
275 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000276 }
nethercote9313ac42004-07-06 21:54:20 +0000277
278 VGP_POPCC(VgpGetLineCC);
279 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000280}
281
282/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000283/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000284/*------------------------------------------------------------*/
285
nethercoteeec46302004-08-23 15:06:23 +0000286static REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000287void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000288{
njn6a3009b2005-03-20 00:20:06 +0000289 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
290 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000291 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000292 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000293 &n->parent->Ir.m1, &n->parent->Ir.m2);
294 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000295 VGP_POPCC(VgpCacheSimulate);
296}
297
nethercoteeec46302004-08-23 15:06:23 +0000298static REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000299void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000300{
nethercote9313ac42004-07-06 21:54:20 +0000301 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000302 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000303 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000304 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000305 &n->parent->Ir.m1, &n->parent->Ir.m2);
306 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000307
nethercote9313ac42004-07-06 21:54:20 +0000308 cachesim_D1_doref(data_addr, n->data_size,
309 &n->parent->Dr.m1, &n->parent->Dr.m2);
310 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000311 VGP_POPCC(VgpCacheSimulate);
312}
313
nethercoteeec46302004-08-23 15:06:23 +0000314static REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000315void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000316{
nethercote9313ac42004-07-06 21:54:20 +0000317 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000318 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000319 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000320 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000321 &n->parent->Ir.m1, &n->parent->Ir.m2);
322 n->parent->Ir.a++;
323
324 cachesim_D1_doref(data_addr, n->data_size,
325 &n->parent->Dw.m1, &n->parent->Dw.m2);
326 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000327 VGP_POPCC(VgpCacheSimulate);
328}
329
nethercoteeec46302004-08-23 15:06:23 +0000330static REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000331void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000332{
333 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000334 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000335 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000336 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000337 &n->parent->Ir.m1, &n->parent->Ir.m2);
338 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000339
nethercote9313ac42004-07-06 21:54:20 +0000340 cachesim_D1_doref(data_addr1, n->data_size,
341 &n->parent->Dr.m1, &n->parent->Dr.m2);
342 n->parent->Dr.a++;
343 cachesim_D1_doref(data_addr2, n->data_size,
344 &n->parent->Dw.m1, &n->parent->Dw.m2);
345 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000346 VGP_POPCC(VgpCacheSimulate);
347}
348
nethercote9313ac42004-07-06 21:54:20 +0000349/*------------------------------------------------------------*/
350/*--- Instrumentation ---*/
351/*------------------------------------------------------------*/
352
nethercote564b2b02004-08-07 15:54:53 +0000353static
njn6a3009b2005-03-20 00:20:06 +0000354BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000355{
356 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000357 IRStmt* st;
358 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000359 VgHashNode** dummy;
360
njn6a3009b2005-03-20 00:20:06 +0000361 // Count number of original instrs in BB
362 n_instrs = 0;
363 for (i = 0; i < bbIn->stmts_used; i++) {
364 st = bbIn->stmts[i];
365 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000366 }
367
368 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000369 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
370 *bbSeenBefore = ( NULL == bbInfo ? False : True );
371 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000372 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000373 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000374 BB_retranslations++;
375 } else {
376 // BB never translated before (at this address, at least; could have
377 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000378 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
379 bbInfo->BB_addr = origAddr;
380 bbInfo->n_instrs = n_instrs;
381 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000382 distinct_instrs++;
383 }
njn6a3009b2005-03-20 00:20:06 +0000384 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000385}
njn6a3009b2005-03-20 00:20:06 +0000386
njn6a3009b2005-03-20 00:20:06 +0000387static
388void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
389 Addr* instrAddr, UInt* instrLen,
390 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
391 UInt* dataSize)
392{
393 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000394 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000395 break;
396
397 case Ist_IMark:
398 *instrAddr = (Addr)st->Ist.IMark.addr;
399 tl_assert(*instrAddr == st->Ist.IMark.addr); // XXX: check no overflow
400 *instrLen = st->Ist.IMark.len;
401 addStmtToIRBB( bbOut, st );
402 break;
403
404 case Ist_Tmp: {
405 IRExpr* data = st->Ist.Tmp.data;
406 if (data->tag == Iex_LDle) {
407 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000408 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000409
410 // XXX: repe cmpsb does two loads... the first one is ignored here!
411 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
412 *loadAddrExpr = aexpr;
413 *dataSize = sizeofIRType(data->Iex.LDle.ty);
414 }
415 addStmtToIRBB( bbOut, st );
416 break;
417 }
418
419 case Ist_STle: {
420 IRExpr* data = st->Ist.STle.data;
421 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000422 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000423 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
424 *storeAddrExpr = aexpr;
425 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
426 addStmtToIRBB( bbOut, st );
427 break;
428 }
429
430 case Ist_Put:
431 case Ist_PutI:
432 case Ist_Exit:
433 case Ist_Dirty:
434 case Ist_MFence:
435 addStmtToIRBB( bbOut, st );
436 break;
437
438 default:
439 VG_(printf)("\n");
440 ppIRStmt(st);
441 VG_(printf)("\n");
442 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
443 }
444}
nethercote9313ac42004-07-06 21:54:20 +0000445
nethercote564b2b02004-08-07 15:54:53 +0000446static
njn6a3009b2005-03-20 00:20:06 +0000447void do_details( instr_info* n, Bool bbSeenBefore,
448 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000449{
njn6a3009b2005-03-20 00:20:06 +0000450 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000451 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000452 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000453 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000454 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000455 // the debug info might change; the other asserts should be enough to
456 // detect anything strange.
457 } else {
njn6a3009b2005-03-20 00:20:06 +0000458 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000459 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000460 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000461 n->data_size = data_size;
462 n->parent = parent;
463 }
464}
465
njn6a3009b2005-03-20 00:20:06 +0000466// XXX: very x86-specific...
nethercote564b2b02004-08-07 15:54:53 +0000467static Bool is_valid_data_size(Int data_size)
nethercote9313ac42004-07-06 21:54:20 +0000468{
469 return (4 == data_size || 2 == data_size || 1 == data_size ||
470 8 == data_size || 10 == data_size || MIN_LINE_SIZE == data_size);
471}
472
njn6a3009b2005-03-20 00:20:06 +0000473static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000474{
njn6a3009b2005-03-20 00:20:06 +0000475 // I'm assuming that for 'modify' instructions, that Vex always makes
476 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
477 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000478 tl_assert(isIRAtom(loadAddrExpr));
479 tl_assert(isIRAtom(storeAddrExpr));
480 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000481}
482
483// Instrumentation for the end of each original instruction.
484static
485void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
486 UInt instrAddr, UInt instrLen, UInt dataSize,
487 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
488{
489 IRDirty* di;
490 IRExpr *arg1, *arg2, *arg3, **argv;
491 Int argc;
492 Char* helperName;
493 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000494 IRType wordTy;
495
496 // Stay sane ...
497 tl_assert(sizeof(HWord) == sizeof(void*));
498 if (sizeof(HWord) == 4) {
499 wordTy = Ity_I32;
500 } else
501 if (sizeof(HWord) == 8) {
502 wordTy = Ity_I64;
503 } else {
504 VG_(tool_panic)("endOfInstr: strange word size");
505 }
506
507 if (loadAddrExpr)
508 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
509 if (storeAddrExpr)
510 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
511
njn6a3009b2005-03-20 00:20:06 +0000512
513 // Nb: instrLen will be zero if Vex failed to decode it.
514 tl_assert( 0 == instrLen ||
515 (instrLen >= MIN_INSTR_SIZE && instrLen <= MAX_INSTR_SIZE) );
516
517 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000518 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000519 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000520 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000521
522 if (!loadAddrExpr && !storeAddrExpr) {
523 // no load/store
524 tl_assert(0 == dataSize);
525 helperName = "log_1I_0D_cache_access";
526 helperAddr = &log_1I_0D_cache_access;
527 argc = 1;
528 argv = mkIRExprVec_1(arg1);
529
530 } else if (loadAddrExpr && !storeAddrExpr) {
531 // load
532 tl_assert( is_valid_data_size(dataSize) );
sewardj710d6c22005-03-20 18:55:15 +0000533 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000534 helperName = "log_1I_1Dr_cache_access";
535 helperAddr = &log_1I_1Dr_cache_access;
536 argc = 2;
537 arg2 = loadAddrExpr;
538 argv = mkIRExprVec_2(arg1, arg2);
539
540 } else if (!loadAddrExpr && storeAddrExpr) {
541 // store
542 tl_assert( is_valid_data_size(dataSize) );
sewardj710d6c22005-03-20 18:55:15 +0000543 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000544 helperName = "log_1I_1Dw_cache_access";
545 helperAddr = &log_1I_1Dw_cache_access;
546 argc = 2;
547 arg2 = storeAddrExpr;
548 argv = mkIRExprVec_2(arg1, arg2);
549
550 } else {
551 tl_assert( loadAddrExpr && storeAddrExpr );
552 tl_assert( is_valid_data_size(dataSize) );
sewardj710d6c22005-03-20 18:55:15 +0000553 tl_assert( isIRAtom(loadAddrExpr) );
554 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000555
556 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
557 // modify
558 helperName = "log_1I_1Dr_cache_access";
559 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000560 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000561 arg2 = loadAddrExpr;
562 argv = mkIRExprVec_2(arg1, arg2);
563
nethercote9313ac42004-07-06 21:54:20 +0000564 } else {
njn6a3009b2005-03-20 00:20:06 +0000565 // load/store
566 helperName = "log_1I_2D_cache_access";
567 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000568 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000569 arg2 = loadAddrExpr;
570 arg3 = storeAddrExpr;
571 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000572 }
573 }
574
njn6a3009b2005-03-20 00:20:06 +0000575 // Add call to the instrumentation function
576 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
577 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000578}
njn14d01ce2004-11-26 11:30:14 +0000579
sewardjd54babf2005-03-21 00:55:49 +0000580IRBB* TL_(instrument) ( IRBB* bbIn, VexGuestLayout* layout,
581 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000582{
njn6a3009b2005-03-20 00:20:06 +0000583 Int i, dataSize = 0, bbInfo_i;
584 IRBB* bbOut;
585 IRStmt* st;
586 BB_info* bbInfo;
587 Bool bbSeenBefore = False;
588 Addr instrAddr, origAddr;
589 UInt instrLen;
590 IRExpr *loadAddrExpr, *storeAddrExpr;
591
sewardjd54babf2005-03-21 00:55:49 +0000592 if (gWordTy != hWordTy) {
593 /* We don't currently support this case. */
594 VG_(tool_panic)("host/guest word size mismatch");
595 }
596
njn6a3009b2005-03-20 00:20:06 +0000597 /* Set up BB */
598 bbOut = emptyIRBB();
599 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
600 bbOut->next = dopyIRExpr(bbIn->next);
601 bbOut->jumpkind = bbIn->jumpkind;
602
603 // Get the first statement, and origAddr from it
604 i = 0;
605 tl_assert(bbIn->stmts_used > 0);
606 st = bbIn->stmts[0];
607 tl_assert(Ist_IMark == st->tag);
608 origAddr = (Addr)st->Ist.IMark.addr;
609 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
610
611 // Get block info
612 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
613 bbInfo_i = 0;
614
615 do {
616 // We should be at an IMark statement
617 tl_assert(Ist_IMark == st->tag);
618
619 // Reset stuff for this original instruction
620 loadAddrExpr = storeAddrExpr = NULL;
621 dataSize = 0;
622
623 // Process all the statements for this original instruction (ie. until
624 // the next IMark statement, or the end of the block)
625 do {
626 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
627 &loadAddrExpr, &storeAddrExpr, &dataSize);
628 i++;
629 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
630 }
631 while (st && Ist_IMark != st->tag);
632
633 // Add instrumentation for this original instruction.
634 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
635 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
636
637 bbInfo_i++;
638 }
639 while (st);
640
641 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000642}
njn4f9c9342002-04-29 16:03:24 +0000643
644/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000645/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000646/*------------------------------------------------------------*/
647
sewardjb5f6f512005-03-10 23:59:00 +0000648#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000649
650static cache_t clo_I1_cache = UNDEFINED_CACHE;
651static cache_t clo_D1_cache = UNDEFINED_CACHE;
652static cache_t clo_L2_cache = UNDEFINED_CACHE;
653
njn7cf0bd32002-06-08 13:36:03 +0000654/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000655static
njna1d1a642004-11-26 18:36:02 +0000656void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000657{
658 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000659 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000660 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000661 "error: %s size of %dB not a power of two; aborting.",
662 name, cache->size);
663 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000664 }
665
sewardj07133bf2002-06-13 10:25:56 +0000666 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000667 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000668 "error: %s associativity of %d not a power of two; aborting.",
669 name, cache->assoc);
670 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000671 }
672
sewardj07133bf2002-06-13 10:25:56 +0000673 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000674 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000675 "error: %s line size of %dB not a power of two; aborting.",
676 name, cache->line_size);
677 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000678 }
679
njn6a3009b2005-03-20 00:20:06 +0000680 // Then check line size >= 16 -- any smaller and a single instruction could
681 // straddle three cache lines, which breaks a simulation assertion and is
682 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000683 if (cache->line_size < MIN_LINE_SIZE) {
684 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000685 "error: %s line size of %dB too small; aborting.",
686 name, cache->line_size);
687 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000688 }
689
690 /* Then check cache size > line size (causes seg faults if not). */
691 if (cache->size <= cache->line_size) {
692 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000693 "error: %s cache size of %dB <= line size of %dB; aborting.",
694 name, cache->size, cache->line_size);
695 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000696 }
697
698 /* Then check assoc <= (size / line size) (seg faults otherwise). */
699 if (cache->assoc > (cache->size / cache->line_size)) {
700 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000701 "warning: %s associativity > (size / line size); aborting.", name);
702 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000703 }
704}
705
sewardj07133bf2002-06-13 10:25:56 +0000706static
nethercoteb35a8b92004-09-11 16:45:27 +0000707void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000708{
nethercote9313ac42004-07-06 21:54:20 +0000709#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
710
nethercoteb35a8b92004-09-11 16:45:27 +0000711 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000712
nethercoteb35a8b92004-09-11 16:45:27 +0000713 // Count how many were defined on the command line.
714 if (DEFINED(clo_I1_cache)) { n_clos++; }
715 if (DEFINED(clo_D1_cache)) { n_clos++; }
716 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000717
njna1d1a642004-11-26 18:36:02 +0000718 // Set the cache config (using auto-detection, if supported by the
719 // architecture)
720 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000721
nethercote9313ac42004-07-06 21:54:20 +0000722 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000723 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
724 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
725 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000726
nethercote9313ac42004-07-06 21:54:20 +0000727 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000728 check_cache(I1c, "I1");
729 check_cache(D1c, "D1");
730 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000731
732 if (VG_(clo_verbosity) > 1) {
733 VG_(message)(Vg_UserMsg, "Cache configuration used:");
734 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
735 I1c->size, I1c->assoc, I1c->line_size);
736 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
737 D1c->size, D1c->assoc, D1c->line_size);
738 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
739 L2c->size, L2c->assoc, L2c->line_size);
740 }
nethercote9313ac42004-07-06 21:54:20 +0000741#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000742}
743
njn4f9c9342002-04-29 16:03:24 +0000744/*------------------------------------------------------------*/
njn26f02512004-11-22 18:33:15 +0000745/*--- TL_(fini)() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000746/*------------------------------------------------------------*/
747
nethercote9313ac42004-07-06 21:54:20 +0000748// Total reads/writes/misses. Calculated during CC traversal at the end.
749// All auto-zeroed.
750static CC Ir_total;
751static CC Dr_total;
752static CC Dw_total;
753
754static Char* cachegrind_out_file;
755
756static void file_err ( void )
njn4f9c9342002-04-29 16:03:24 +0000757{
nethercote9313ac42004-07-06 21:54:20 +0000758 VG_(message)(Vg_UserMsg,
759 "error: can't open cache simulation output file `%s'",
760 cachegrind_out_file );
761 VG_(message)(Vg_UserMsg,
762 " ... so simulation results will be missing.");
njn4f9c9342002-04-29 16:03:24 +0000763}
764
nethercote9313ac42004-07-06 21:54:20 +0000765static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000766{
nethercote9313ac42004-07-06 21:54:20 +0000767 Char buf[512];
768 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
769 n->line,
770 n->Ir.a, n->Ir.m1, n->Ir.m2,
771 n->Dr.a, n->Dr.m1, n->Dr.m2,
772 n->Dw.a, n->Dw.m1, n->Dw.m2);
773 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
774
775 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
776 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
777 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
778}
779
780static void fprint_CC_table_and_calc_totals(void)
781{
782 Int fd;
783 Char buf[512];
784 fileCC *curr_fileCC;
785 fnCC *curr_fnCC;
786 lineCC *curr_lineCC;
787 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000788
njn25e49d8e72002-09-23 09:36:25 +0000789 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000790
njndb918dd2003-07-22 20:45:11 +0000791 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000792 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000793 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000794 // If the file can't be opened for whatever reason (conflict
795 // between multiple cachegrinded processes?), give up now.
sewardj0744b6c2002-12-11 00:45:42 +0000796 file_err();
797 return;
798 }
njn4f9c9342002-04-29 16:03:24 +0000799
nethercote9313ac42004-07-06 21:54:20 +0000800 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
801 // the 2nd colon makes cg_annotate's output look nicer.
802 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
803 "desc: D1 cache: %s\n"
804 "desc: L2 cache: %s\n",
805 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000806 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000807
nethercote9313ac42004-07-06 21:54:20 +0000808 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000809 VG_(strcpy)(buf, "cmd:");
810 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000811 for (i = 0; i < VG_(client_argc); i++) {
thughes6f7eb9c2004-10-06 13:50:12 +0000812 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000813 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000814 }
nethercote9313ac42004-07-06 21:54:20 +0000815 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000816 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
817 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
818
nethercote9313ac42004-07-06 21:54:20 +0000819 // Six loops here: three for the hash table arrays, and three for the
820 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000821 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000822 curr_fileCC = CC_table[i];
823 while (curr_fileCC != NULL) {
824 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000825 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
826
827 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000828 curr_fnCC = curr_fileCC->fns[j];
829 while (curr_fnCC != NULL) {
830 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000831 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
832
nethercote9313ac42004-07-06 21:54:20 +0000833 for (k = 0; k < N_LINE_ENTRIES; k++) {
834 curr_lineCC = curr_fnCC->lines[k];
835 while (curr_lineCC != NULL) {
836 fprint_lineCC(fd, curr_lineCC);
837 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000838 }
839 }
nethercote9313ac42004-07-06 21:54:20 +0000840 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000841 }
842 }
nethercote9313ac42004-07-06 21:54:20 +0000843 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000844 }
845 }
846
nethercote9313ac42004-07-06 21:54:20 +0000847 // Summary stats must come after rest of table, since we calculate them
848 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000849 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000850 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000851 Ir_total.a, Ir_total.m1, Ir_total.m2,
852 Dr_total.a, Dr_total.m1, Dr_total.m2,
853 Dw_total.a, Dw_total.m1, Dw_total.m2);
854 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
855 VG_(close)(fd);
856}
857
njn607adfc2003-09-30 14:15:44 +0000858static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000859{
njn607adfc2003-09-30 14:15:44 +0000860 UInt w = 0;
861 while (n > 0) {
862 n = n / 10;
863 w++;
njn4f9c9342002-04-29 16:03:24 +0000864 }
njn607adfc2003-09-30 14:15:44 +0000865 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000866}
867
sewardj4f29ddf2002-05-03 22:29:04 +0000868static
daywalker8ad1a402003-09-18 01:15:32 +0000869void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000870{
871 int i, len, space;
872
daywalker8ad1a402003-09-18 01:15:32 +0000873 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000874 len = VG_(strlen)(buf);
875 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000876 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000877 i = len;
878
879 /* Right justify in field */
880 for ( ; i >= 0; i--) buf[i + space] = buf[i];
881 for (i = 0; i < space; i++) buf[i] = ' ';
882}
883
njn26f02512004-11-22 18:33:15 +0000884void TL_(fini)(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000885{
nethercote9313ac42004-07-06 21:54:20 +0000886 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000887
njn4f9c9342002-04-29 16:03:24 +0000888 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000889 ULong L2_total_m, L2_total_mr, L2_total_mw,
890 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000891 Int l1, l2, l3;
892 Int p;
893
nethercote9313ac42004-07-06 21:54:20 +0000894 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000895
njn7cf0bd32002-06-08 13:36:03 +0000896 if (VG_(clo_verbosity) == 0)
897 return;
898
njn4f9c9342002-04-29 16:03:24 +0000899 /* I cache results. Use the I_refs value to determine the first column
900 * width. */
njn607adfc2003-09-30 14:15:44 +0000901 l1 = ULong_width(Ir_total.a);
902 l2 = ULong_width(Dr_total.a);
903 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000904
njn607adfc2003-09-30 14:15:44 +0000905 /* Make format string, getting width right for numbers */
906 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
907
908 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
909 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
910 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000911
912 p = 100;
913
njn25e49d8e72002-09-23 09:36:25 +0000914 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000915 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
916 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
917
918 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
919 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
920 VG_(message)(Vg_UserMsg, "");
921
922 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
923 * width of columns 2 & 3. */
924 D_total.a = Dr_total.a + Dw_total.a;
925 D_total.m1 = Dr_total.m1 + Dw_total.m1;
926 D_total.m2 = Dr_total.m2 + Dw_total.m2;
927
njn607adfc2003-09-30 14:15:44 +0000928 /* Make format string, getting width right for numbers */
929 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000930
njn607adfc2003-09-30 14:15:44 +0000931 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
932 D_total.a, Dr_total.a, Dw_total.a);
933 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
934 D_total.m1, Dr_total.m1, Dw_total.m1);
935 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
936 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000937
938 p = 10;
939
njn25e49d8e72002-09-23 09:36:25 +0000940 if (0 == D_total.a) D_total.a = 1;
941 if (0 == Dr_total.a) Dr_total.a = 1;
942 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000943 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
944 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
945 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
946 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
947
948 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
949 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
950 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
951 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
952 VG_(message)(Vg_UserMsg, "");
953
954 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000955
956 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
957 L2_total_r = Dr_total.m1 + Ir_total.m1;
958 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000959 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
960 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000961
njn4f9c9342002-04-29 16:03:24 +0000962 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
963 L2_total_mr = Dr_total.m2 + Ir_total.m2;
964 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +0000965 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
966 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +0000967
968 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
969 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
970 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
971 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
972
973
nethercote9313ac42004-07-06 21:54:20 +0000974 // Various stats
njn4f9c9342002-04-29 16:03:24 +0000975 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +0000976 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +0000977 file_line_debug_BBs + no_debug_BBs;
978
979 VG_(message)(Vg_DebugMsg, "");
980 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
981 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +0000982 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
983 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +0000984 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
985 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
986 full_debug_BBs * 100 / BB_lookups,
987 full_debug_BBs);
988 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
989 file_line_debug_BBs * 100 / BB_lookups,
990 file_line_debug_BBs);
991 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +0000992 fn_debug_BBs * 100 / BB_lookups,
993 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +0000994 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
995 no_debug_BBs * 100 / BB_lookups,
996 no_debug_BBs);
997 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +0000998 }
njn25e49d8e72002-09-23 09:36:25 +0000999 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001000}
1001
nethercote9313ac42004-07-06 21:54:20 +00001002/*--------------------------------------------------------------------*/
1003/*--- Discarding BB info ---*/
1004/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001005
nethercote9313ac42004-07-06 21:54:20 +00001006// Called when a translation is invalidated due to code unloading.
njn26f02512004-11-22 18:33:15 +00001007void TL_(discard_basic_block_info) ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001008{
nethercote9313ac42004-07-06 21:54:20 +00001009 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001010 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001011
nethercote928a5f72004-11-03 18:10:37 +00001012 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001013
nethercote9313ac42004-07-06 21:54:20 +00001014 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001015 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1016 tl_assert(NULL != bbInfo);
1017 *prev_next_ptr = bbInfo->next;
1018 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001019}
1020
1021/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001022/*--- Command line processing ---*/
1023/*--------------------------------------------------------------------*/
1024
nethercote9313ac42004-07-06 21:54:20 +00001025static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001026{
nethercote9313ac42004-07-06 21:54:20 +00001027 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001028
nethercote9313ac42004-07-06 21:54:20 +00001029 // Option argument looks like "65536,2,64".
1030 // Find commas, replace with NULs to make three independent
1031 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001032 while (VG_(isdigit)(opt[i])) i++;
1033 if (',' == opt[i]) {
1034 opt[i++] = '\0';
1035 i2 = i;
1036 } else goto bad;
1037 while (VG_(isdigit)(opt[i])) i++;
1038 if (',' == opt[i]) {
1039 opt[i++] = '\0';
1040 i3 = i;
1041 } else goto bad;
1042 while (VG_(isdigit)(opt[i])) i++;
1043 if ('\0' != opt[i]) goto bad;
1044
nethercote9313ac42004-07-06 21:54:20 +00001045 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001046 cache->assoc = (Int)VG_(atoll)(opt + i2);
1047 cache->line_size = (Int)VG_(atoll)(opt + i3);
1048
nethercote9313ac42004-07-06 21:54:20 +00001049 opt[i2-1] = ',';
1050 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001051 return;
1052
1053 bad:
nethercote9313ac42004-07-06 21:54:20 +00001054 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001055}
1056
njn26f02512004-11-22 18:33:15 +00001057Bool TL_(process_cmd_line_option)(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001058{
nethercote9313ac42004-07-06 21:54:20 +00001059 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001060 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001061 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001062 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001063 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001064 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001065 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001066 else
1067 return False;
1068
1069 return True;
1070}
1071
njn26f02512004-11-22 18:33:15 +00001072void TL_(print_usage)(void)
njn25e49d8e72002-09-23 09:36:25 +00001073{
njn3e884182003-04-15 13:03:23 +00001074 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001075" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1076" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001077" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1078 );
1079}
1080
njn26f02512004-11-22 18:33:15 +00001081void TL_(print_debug_usage)(void)
njn3e884182003-04-15 13:03:23 +00001082{
1083 VG_(printf)(
1084" (none)\n"
1085 );
njn25e49d8e72002-09-23 09:36:25 +00001086}
1087
1088/*--------------------------------------------------------------------*/
1089/*--- Setup ---*/
1090/*--------------------------------------------------------------------*/
1091
njn26f02512004-11-22 18:33:15 +00001092void TL_(pre_clo_init)(void)
njn25e49d8e72002-09-23 09:36:25 +00001093{
njn13f02932003-04-30 20:23:58 +00001094 Char* base_dir = NULL;
njn607adfc2003-09-30 14:15:44 +00001095
njn810086f2002-11-14 12:42:47 +00001096 VG_(details_name) ("Cachegrind");
1097 VG_(details_version) (NULL);
1098 VG_(details_description) ("an I1/D1/L2 cache profiler");
1099 VG_(details_copyright_author)(
njn53612422005-03-12 16:22:54 +00001100 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
nethercote421281e2003-11-20 16:20:55 +00001101 VG_(details_bug_reports_to) (VG_BUGS_TO);
sewardj78210aa2002-12-01 02:55:46 +00001102 VG_(details_avg_translation_sizeB) ( 155 );
njn25e49d8e72002-09-23 09:36:25 +00001103
njn810086f2002-11-14 12:42:47 +00001104 VG_(needs_basic_block_discards)();
1105 VG_(needs_command_line_options)();
njn25e49d8e72002-09-23 09:36:25 +00001106
njn99ccf082003-09-30 13:51:23 +00001107 /* Get working directory */
njnca82cc02004-11-22 17:18:48 +00001108 tl_assert( VG_(getcwd_alloc)(&base_dir) );
njn99ccf082003-09-30 13:51:23 +00001109
njn13f02932003-04-30 20:23:58 +00001110 /* Block is big enough for dir name + cachegrind.out.<pid> */
1111 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1112 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1113 base_dir, VG_(getpid)());
njn99ccf082003-09-30 13:51:23 +00001114 VG_(free)(base_dir);
nethercote9313ac42004-07-06 21:54:20 +00001115
1116 instr_info_table = VG_(HT_construct)();
njn25e49d8e72002-09-23 09:36:25 +00001117}
1118
njn26f02512004-11-22 18:33:15 +00001119void TL_(post_clo_init)(void)
njn25e49d8e72002-09-23 09:36:25 +00001120{
1121 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001122
nethercoteb35a8b92004-09-11 16:45:27 +00001123 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001124
1125 cachesim_I1_initcache(I1c);
1126 cachesim_D1_initcache(D1c);
1127 cachesim_L2_initcache(L2c);
1128
nethercote9313ac42004-07-06 21:54:20 +00001129 VGP_(register_profile_event)(VgpGetLineCC, "get-lineCC");
njn25e49d8e72002-09-23 09:36:25 +00001130 VGP_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1131 VGP_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001132}
1133
njn26f02512004-11-22 18:33:15 +00001134VG_DETERMINE_INTERFACE_VERSION(TL_(pre_clo_init), 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001135
njn25e49d8e72002-09-23 09:36:25 +00001136/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001137/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001138/*--------------------------------------------------------------------*/