blob: c313b7aaec486517457ce84f4d1b4efa89edc224 [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"
njn717cde52005-05-10 02:47:21 +000036#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +000037#include "pub_tool_options.h"
njn31513b42005-06-01 03:09:59 +000038#include "pub_tool_profile.h"
njn43b9a8a2005-05-10 04:37:01 +000039#include "pub_tool_tooliface.h"
njn25e49d8e72002-09-23 09:36:25 +000040
nethercoteb35a8b92004-09-11 16:45:27 +000041#include "cg_arch.h"
nethercote27fc1da2004-01-04 16:56:57 +000042#include "cg_sim.c"
njn4f9c9342002-04-29 16:03:24 +000043
njn25e49d8e72002-09-23 09:36:25 +000044/*------------------------------------------------------------*/
45/*--- Constants ---*/
46/*------------------------------------------------------------*/
njn4f9c9342002-04-29 16:03:24 +000047
nethercote9313ac42004-07-06 21:54:20 +000048#define MIN_LINE_SIZE 16
49#define FILE_LEN 256
50#define FN_LEN 256
njn7cf0bd32002-06-08 13:36:03 +000051
52/*------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +000053/*--- Profiling events ---*/
njn7cf0bd32002-06-08 13:36:03 +000054/*------------------------------------------------------------*/
55
njn25e49d8e72002-09-23 09:36:25 +000056typedef
57 enum {
nethercote9313ac42004-07-06 21:54:20 +000058 VgpGetLineCC = VgpFini+1,
njn25e49d8e72002-09-23 09:36:25 +000059 VgpCacheSimulate,
60 VgpCacheResults
61 }
nethercote7cc9c232004-01-21 15:08:04 +000062 VgpToolCC;
sewardj07133bf2002-06-13 10:25:56 +000063
njn4f9c9342002-04-29 16:03:24 +000064/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +000065/*--- Types and Data Structures ---*/
njn4f9c9342002-04-29 16:03:24 +000066/*------------------------------------------------------------*/
67
68typedef struct _CC CC;
69struct _CC {
70 ULong a;
71 ULong m1;
72 ULong m2;
73};
74
nethercote9313ac42004-07-06 21:54:20 +000075//------------------------------------------------------------
76// Primary data structure #1: CC table
77// - Holds the per-source-line hit/miss stats, grouped by file/function/line.
78// - hash(file, hash(fn, hash(line+CC)))
79// - Each hash table is separately chained.
80// - The array sizes below work fairly well for Konqueror.
81// - Lookups done by instr_addr, which is converted immediately to a source
82// location.
83// - Traversed for dumping stats at end in file/func/line hierarchy.
njn4f9c9342002-04-29 16:03:24 +000084
85#define N_FILE_ENTRIES 251
86#define N_FN_ENTRIES 53
nethercote9313ac42004-07-06 21:54:20 +000087#define N_LINE_ENTRIES 37
njn4f9c9342002-04-29 16:03:24 +000088
nethercote9313ac42004-07-06 21:54:20 +000089typedef struct _lineCC lineCC;
90struct _lineCC {
91 Int line;
92 CC Ir;
93 CC Dr;
94 CC Dw;
95 lineCC* next;
njn4f9c9342002-04-29 16:03:24 +000096};
97
nethercote9313ac42004-07-06 21:54:20 +000098typedef struct _fnCC fnCC;
99struct _fnCC {
100 Char* fn;
101 fnCC* next;
102 lineCC* lines[N_LINE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000103};
104
nethercote9313ac42004-07-06 21:54:20 +0000105typedef struct _fileCC fileCC;
106struct _fileCC {
107 Char* file;
108 fileCC* next;
109 fnCC* fns[N_FN_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000110};
111
nethercote9313ac42004-07-06 21:54:20 +0000112// Top level of CC table. Auto-zeroed.
113static fileCC *CC_table[N_FILE_ENTRIES];
njn4f9c9342002-04-29 16:03:24 +0000114
nethercote9313ac42004-07-06 21:54:20 +0000115//------------------------------------------------------------
116// Primary data structre #2: Instr-info table
117// - Holds the cached info about each instr that is used for simulation.
118// - table(BB_start_addr, list(instr_info))
119// - For each BB, each instr_info in the list holds info about the
njn6a3009b2005-03-20 00:20:06 +0000120// instruction (instr_len, instr_addr, etc), plus a pointer to its line
nethercote9313ac42004-07-06 21:54:20 +0000121// CC. This node is what's passed to the simulation function.
122// - When BBs are discarded the relevant list(instr_details) is freed.
123
124typedef struct _instr_info instr_info;
125struct _instr_info {
nethercoteca1f2dc2004-07-21 08:49:02 +0000126 Addr instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000127 UChar instr_len;
nethercoteca1f2dc2004-07-21 08:49:02 +0000128 UChar data_size;
129 lineCC* parent; // parent line-CC
nethercote9313ac42004-07-06 21:54:20 +0000130};
131
132typedef struct _BB_info BB_info;
133struct _BB_info {
134 BB_info* next; // next field
135 Addr BB_addr; // key
136 Int n_instrs;
137 instr_info instrs[0];
138};
139
140VgHashTable instr_info_table; // hash(Addr, BB_info)
141
142//------------------------------------------------------------
143// Stats
sewardj4f29ddf2002-05-03 22:29:04 +0000144static Int distinct_files = 0;
145static Int distinct_fns = 0;
nethercote9313ac42004-07-06 21:54:20 +0000146static Int distinct_lines = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000147static Int distinct_instrs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000148
sewardj4f29ddf2002-05-03 22:29:04 +0000149static Int full_debug_BBs = 0;
150static Int file_line_debug_BBs = 0;
nethercote9313ac42004-07-06 21:54:20 +0000151static Int fn_debug_BBs = 0;
sewardj4f29ddf2002-05-03 22:29:04 +0000152static Int no_debug_BBs = 0;
njn4f9c9342002-04-29 16:03:24 +0000153
sewardj4f29ddf2002-05-03 22:29:04 +0000154static Int BB_retranslations = 0;
njn4f9c9342002-04-29 16:03:24 +0000155
nethercote9313ac42004-07-06 21:54:20 +0000156/*------------------------------------------------------------*/
157/*--- CC table operations ---*/
158/*------------------------------------------------------------*/
njn4294fd42002-06-05 14:41:10 +0000159
nethercote9313ac42004-07-06 21:54:20 +0000160static void get_debug_info(Addr instr_addr, Char file[FILE_LEN],
161 Char fn[FN_LEN], Int* line)
njn4f9c9342002-04-29 16:03:24 +0000162{
nethercote9313ac42004-07-06 21:54:20 +0000163 Bool found_file_line = VG_(get_filename_linenum)(instr_addr, file,
164 FILE_LEN, line);
165 Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN);
njn4f9c9342002-04-29 16:03:24 +0000166
nethercote9313ac42004-07-06 21:54:20 +0000167 if (!found_file_line) {
168 VG_(strcpy)(file, "???");
169 *line = 0;
170 }
171 if (!found_fn) {
172 VG_(strcpy)(fn, "???");
173 }
174 if (found_file_line) {
175 if (found_fn) full_debug_BBs++;
176 else file_line_debug_BBs++;
177 } else {
178 if (found_fn) fn_debug_BBs++;
179 else no_debug_BBs++;
njn4f9c9342002-04-29 16:03:24 +0000180 }
181}
182
njn4f9c9342002-04-29 16:03:24 +0000183static UInt hash(Char *s, UInt table_size)
184{
nethercote9313ac42004-07-06 21:54:20 +0000185 const int hash_constant = 256;
186 int hash_value = 0;
187 for ( ; *s; s++)
188 hash_value = (hash_constant * hash_value + *s) % table_size;
189 return hash_value;
njn4f9c9342002-04-29 16:03:24 +0000190}
191
nethercote9313ac42004-07-06 21:54:20 +0000192static __inline__
193fileCC* new_fileCC(Char filename[], fileCC* next)
nethercote09d853e2004-01-21 16:12:55 +0000194{
nethercote9313ac42004-07-06 21:54:20 +0000195 // Using calloc() zeroes the fns[] array
196 fileCC* cc = VG_(calloc)(1, sizeof(fileCC));
197 cc->file = VG_(strdup)(filename);
198 cc->next = next;
199 return cc;
nethercote09d853e2004-01-21 16:12:55 +0000200}
201
nethercote9313ac42004-07-06 21:54:20 +0000202static __inline__
203fnCC* new_fnCC(Char fn[], fnCC* next)
njn4f9c9342002-04-29 16:03:24 +0000204{
nethercote9313ac42004-07-06 21:54:20 +0000205 // Using calloc() zeroes the lines[] array
206 fnCC* cc = VG_(calloc)(1, sizeof(fnCC));
207 cc->fn = VG_(strdup)(fn);
208 cc->next = next;
209 return cc;
210}
njn4f9c9342002-04-29 16:03:24 +0000211
nethercote9313ac42004-07-06 21:54:20 +0000212static __inline__
213lineCC* new_lineCC(Int line, lineCC* next)
214{
215 // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array
216 lineCC* cc = VG_(calloc)(1, sizeof(lineCC));
217 cc->line = line;
218 cc->next = next;
219 return cc;
220}
njn4f9c9342002-04-29 16:03:24 +0000221
nethercote9313ac42004-07-06 21:54:20 +0000222static __inline__
223instr_info* new_instr_info(Addr instr_addr, lineCC* parent, instr_info* next)
224{
njn6a3009b2005-03-20 00:20:06 +0000225 // Using calloc() zeroes instr_len and data_size
nethercote9313ac42004-07-06 21:54:20 +0000226 instr_info* ii = VG_(calloc)(1, sizeof(instr_info));
227 ii->instr_addr = instr_addr;
228 ii->parent = parent;
229 return ii;
230}
231
232// Do a three step traversal: by file, then fn, then line.
233// In all cases prepends new nodes to their chain. Returns a pointer to the
234// line node, creates a new one if necessary.
njn6a3009b2005-03-20 00:20:06 +0000235static lineCC* get_lineCC(Addr origAddr)
nethercote9313ac42004-07-06 21:54:20 +0000236{
237 fileCC *curr_fileCC;
238 fnCC *curr_fnCC;
239 lineCC *curr_lineCC;
240 Char file[FILE_LEN], fn[FN_LEN];
241 Int line;
242 UInt file_hash, fn_hash, line_hash;
243
njn6a3009b2005-03-20 00:20:06 +0000244 get_debug_info(origAddr, file, fn, &line);
nethercote9313ac42004-07-06 21:54:20 +0000245
246 VGP_PUSHCC(VgpGetLineCC);
247
248 // level 1
249 file_hash = hash(file, N_FILE_ENTRIES);
250 curr_fileCC = CC_table[file_hash];
251 while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) {
252 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000253 }
nethercote9313ac42004-07-06 21:54:20 +0000254 if (NULL == curr_fileCC) {
255 CC_table[file_hash] = curr_fileCC =
256 new_fileCC(file, CC_table[file_hash]);
njn4f9c9342002-04-29 16:03:24 +0000257 distinct_files++;
258 }
259
nethercote9313ac42004-07-06 21:54:20 +0000260 // level 2
261 fn_hash = hash(fn, N_FN_ENTRIES);
262 curr_fnCC = curr_fileCC->fns[fn_hash];
263 while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) {
264 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000265 }
nethercote9313ac42004-07-06 21:54:20 +0000266 if (NULL == curr_fnCC) {
267 curr_fileCC->fns[fn_hash] = curr_fnCC =
268 new_fnCC(fn, curr_fileCC->fns[fn_hash]);
njn4f9c9342002-04-29 16:03:24 +0000269 distinct_fns++;
270 }
271
nethercote9313ac42004-07-06 21:54:20 +0000272 // level 3
273 line_hash = line % N_LINE_ENTRIES;
274 curr_lineCC = curr_fnCC->lines[line_hash];
275 while (NULL != curr_lineCC && line != curr_lineCC->line) {
276 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000277 }
nethercote9313ac42004-07-06 21:54:20 +0000278 if (NULL == curr_lineCC) {
279 curr_fnCC->lines[line_hash] = curr_lineCC =
280 new_lineCC(line, curr_fnCC->lines[line_hash]);
281 distinct_lines++;
njn4f9c9342002-04-29 16:03:24 +0000282 }
nethercote9313ac42004-07-06 21:54:20 +0000283
284 VGP_POPCC(VgpGetLineCC);
285 return curr_lineCC;
njn4f9c9342002-04-29 16:03:24 +0000286}
287
288/*------------------------------------------------------------*/
nethercote9313ac42004-07-06 21:54:20 +0000289/*--- Cache simulation functions ---*/
njn4f9c9342002-04-29 16:03:24 +0000290/*------------------------------------------------------------*/
291
njn9fb73db2005-03-27 01:55:21 +0000292static VGA_REGPARM(1)
nethercote9313ac42004-07-06 21:54:20 +0000293void log_1I_0D_cache_access(instr_info* n)
njn25e49d8e72002-09-23 09:36:25 +0000294{
njn6a3009b2005-03-20 00:20:06 +0000295 //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n",
296 // n, n->instr_addr, n->instr_len);
njn25e49d8e72002-09-23 09:36:25 +0000297 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000298 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000299 &n->parent->Ir.m1, &n->parent->Ir.m2);
300 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000301 VGP_POPCC(VgpCacheSimulate);
302}
303
njn9fb73db2005-03-27 01:55:21 +0000304static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000305void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000306{
nethercote9313ac42004-07-06 21:54:20 +0000307 //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000308 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000309 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000310 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000311 &n->parent->Ir.m1, &n->parent->Ir.m2);
312 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000313
nethercote9313ac42004-07-06 21:54:20 +0000314 cachesim_D1_doref(data_addr, n->data_size,
315 &n->parent->Dr.m1, &n->parent->Dr.m2);
316 n->parent->Dr.a++;
njn25e49d8e72002-09-23 09:36:25 +0000317 VGP_POPCC(VgpCacheSimulate);
318}
319
njn9fb73db2005-03-27 01:55:21 +0000320static VGA_REGPARM(2)
nethercote9313ac42004-07-06 21:54:20 +0000321void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr)
njn25e49d8e72002-09-23 09:36:25 +0000322{
nethercote9313ac42004-07-06 21:54:20 +0000323 //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000324 // n, n->instr_addr, n->instr_len, data_addr, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000325 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000326 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000327 &n->parent->Ir.m1, &n->parent->Ir.m2);
328 n->parent->Ir.a++;
329
330 cachesim_D1_doref(data_addr, n->data_size,
331 &n->parent->Dw.m1, &n->parent->Dw.m2);
332 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000333 VGP_POPCC(VgpCacheSimulate);
334}
335
njn9fb73db2005-03-27 01:55:21 +0000336static VGA_REGPARM(3)
nethercote9313ac42004-07-06 21:54:20 +0000337void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2)
njn25e49d8e72002-09-23 09:36:25 +0000338{
339 //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n",
njn6a3009b2005-03-20 00:20:06 +0000340 // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size);
njn25e49d8e72002-09-23 09:36:25 +0000341 VGP_PUSHCC(VgpCacheSimulate);
njn6a3009b2005-03-20 00:20:06 +0000342 cachesim_I1_doref(n->instr_addr, n->instr_len,
nethercote9313ac42004-07-06 21:54:20 +0000343 &n->parent->Ir.m1, &n->parent->Ir.m2);
344 n->parent->Ir.a++;
njn25e49d8e72002-09-23 09:36:25 +0000345
nethercote9313ac42004-07-06 21:54:20 +0000346 cachesim_D1_doref(data_addr1, n->data_size,
347 &n->parent->Dr.m1, &n->parent->Dr.m2);
348 n->parent->Dr.a++;
349 cachesim_D1_doref(data_addr2, n->data_size,
350 &n->parent->Dw.m1, &n->parent->Dw.m2);
351 n->parent->Dw.a++;
njn25e49d8e72002-09-23 09:36:25 +0000352 VGP_POPCC(VgpCacheSimulate);
353}
354
nethercote9313ac42004-07-06 21:54:20 +0000355/*------------------------------------------------------------*/
356/*--- Instrumentation ---*/
357/*------------------------------------------------------------*/
358
nethercote564b2b02004-08-07 15:54:53 +0000359static
njn6a3009b2005-03-20 00:20:06 +0000360BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore)
nethercote9313ac42004-07-06 21:54:20 +0000361{
362 Int i, n_instrs;
njn6a3009b2005-03-20 00:20:06 +0000363 IRStmt* st;
364 BB_info* bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000365 VgHashNode** dummy;
366
njn6a3009b2005-03-20 00:20:06 +0000367 // Count number of original instrs in BB
368 n_instrs = 0;
369 for (i = 0; i < bbIn->stmts_used; i++) {
370 st = bbIn->stmts[i];
371 if (Ist_IMark == st->tag) n_instrs++;
nethercote9313ac42004-07-06 21:54:20 +0000372 }
373
374 // Get the BB_info
njn6a3009b2005-03-20 00:20:06 +0000375 bbInfo = (BB_info*)VG_(HT_get_node)(instr_info_table, origAddr, &dummy);
376 *bbSeenBefore = ( NULL == bbInfo ? False : True );
377 if (*bbSeenBefore) {
nethercote9313ac42004-07-06 21:54:20 +0000378 // BB must have been translated before, but flushed from the TT
njn6a3009b2005-03-20 00:20:06 +0000379 tl_assert(bbInfo->n_instrs == n_instrs );
nethercote9313ac42004-07-06 21:54:20 +0000380 BB_retranslations++;
381 } else {
382 // BB never translated before (at this address, at least; could have
383 // been unloaded and then reloaded elsewhere in memory)
njn6a3009b2005-03-20 00:20:06 +0000384 bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info));
385 bbInfo->BB_addr = origAddr;
386 bbInfo->n_instrs = n_instrs;
387 VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo );
nethercote9313ac42004-07-06 21:54:20 +0000388 distinct_instrs++;
389 }
njn6a3009b2005-03-20 00:20:06 +0000390 return bbInfo;
nethercote9313ac42004-07-06 21:54:20 +0000391}
njn6a3009b2005-03-20 00:20:06 +0000392
njn6a3009b2005-03-20 00:20:06 +0000393static
394void handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st,
395 Addr* instrAddr, UInt* instrLen,
396 IRExpr** loadAddrExpr, IRExpr** storeAddrExpr,
397 UInt* dataSize)
398{
sewardj7f4a8622005-03-26 21:55:21 +0000399 tl_assert(isFlatIRStmt(st));
400
njn6a3009b2005-03-20 00:20:06 +0000401 switch (st->tag) {
sewardj21dc3452005-03-21 00:27:41 +0000402 case Ist_NoOp:
njn6a3009b2005-03-20 00:20:06 +0000403 break;
404
sewardje6da2fa2005-05-12 17:57:14 +0000405 case Ist_AbiHint:
406 /* ABI hints aren't interesting to cachegrind. Ignore. */
407 break;
408
njn6a3009b2005-03-20 00:20:06 +0000409 case Ist_IMark:
sewardj2b641fe2005-03-21 11:53:38 +0000410 /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this
411 to the host's native pointer type; if that is 32 bits then it
412 discards the upper 32 bits. If we are cachegrinding on a
413 32-bit host then we are also ensured that the guest word size
njn51d827b2005-05-09 01:02:08 +0000414 is 32 bits, due to the assertion in cg_instrument that the
sewardj2b641fe2005-03-21 11:53:38 +0000415 host and guest word sizes must be the same. Hence
416 st->Ist.IMark.addr will have been derived from a 32-bit guest
417 code address and truncation of it is safe. I believe this
418 assignment should be correct for both 32- and 64-bit
419 machines. */
420 *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr);
njn6a3009b2005-03-20 00:20:06 +0000421 *instrLen = st->Ist.IMark.len;
422 addStmtToIRBB( bbOut, st );
423 break;
424
425 case Ist_Tmp: {
426 IRExpr* data = st->Ist.Tmp.data;
427 if (data->tag == Iex_LDle) {
428 IRExpr* aexpr = data->Iex.LDle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000429 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000430
431 // XXX: repe cmpsb does two loads... the first one is ignored here!
432 //tl_assert( NULL == *loadAddrExpr ); // XXX: ???
433 *loadAddrExpr = aexpr;
434 *dataSize = sizeofIRType(data->Iex.LDle.ty);
435 }
436 addStmtToIRBB( bbOut, st );
437 break;
438 }
439
440 case Ist_STle: {
441 IRExpr* data = st->Ist.STle.data;
442 IRExpr* aexpr = st->Ist.STle.addr;
sewardj710d6c22005-03-20 18:55:15 +0000443 tl_assert( isIRAtom(aexpr) );
njn6a3009b2005-03-20 00:20:06 +0000444 tl_assert( NULL == *storeAddrExpr ); // XXX: ???
445 *storeAddrExpr = aexpr;
446 *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data));
447 addStmtToIRBB( bbOut, st );
448 break;
449 }
450
sewardj7f4a8622005-03-26 21:55:21 +0000451 case Ist_Dirty: {
452 IRDirty* d = st->Ist.Dirty.details;
453 if (d->mFx != Ifx_None) {
454 /* This dirty helper accesses memory. Collect the
455 details. */
456 tl_assert(d->mAddr != NULL);
457 tl_assert(d->mSize != 0);
458 *dataSize = d->mSize;
459 if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify)
460 *loadAddrExpr = d->mAddr;
461 if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify)
462 *storeAddrExpr = d->mAddr;
463 } else {
464 tl_assert(d->mAddr == NULL);
465 tl_assert(d->mSize == 0);
466 }
467 addStmtToIRBB( bbOut, st );
468 break;
469 }
470
njn6a3009b2005-03-20 00:20:06 +0000471 case Ist_Put:
472 case Ist_PutI:
473 case Ist_Exit:
njn6a3009b2005-03-20 00:20:06 +0000474 case Ist_MFence:
475 addStmtToIRBB( bbOut, st );
476 break;
477
478 default:
479 VG_(printf)("\n");
480 ppIRStmt(st);
481 VG_(printf)("\n");
482 VG_(tool_panic)("Cachegrind: unhandled IRStmt");
483 }
484}
nethercote9313ac42004-07-06 21:54:20 +0000485
nethercote564b2b02004-08-07 15:54:53 +0000486static
njn6a3009b2005-03-20 00:20:06 +0000487void do_details( instr_info* n, Bool bbSeenBefore,
488 Addr instr_addr, Int instr_len, Int data_size )
nethercote9313ac42004-07-06 21:54:20 +0000489{
njn6a3009b2005-03-20 00:20:06 +0000490 if (bbSeenBefore) {
njnca82cc02004-11-22 17:18:48 +0000491 tl_assert( n->instr_addr == instr_addr );
njn6a3009b2005-03-20 00:20:06 +0000492 tl_assert( n->instr_len == instr_len );
njnca82cc02004-11-22 17:18:48 +0000493 tl_assert( n->data_size == data_size );
njn6a3009b2005-03-20 00:20:06 +0000494 // Don't check that (n->parent == parent)... it's conceivable that
nethercote9313ac42004-07-06 21:54:20 +0000495 // the debug info might change; the other asserts should be enough to
496 // detect anything strange.
497 } else {
njn6a3009b2005-03-20 00:20:06 +0000498 lineCC* parent = get_lineCC(instr_addr);
nethercote9313ac42004-07-06 21:54:20 +0000499 n->instr_addr = instr_addr;
njn6a3009b2005-03-20 00:20:06 +0000500 n->instr_len = instr_len;
nethercote9313ac42004-07-06 21:54:20 +0000501 n->data_size = data_size;
502 n->parent = parent;
503 }
504}
505
njn6a3009b2005-03-20 00:20:06 +0000506static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
nethercote9313ac42004-07-06 21:54:20 +0000507{
njn6a3009b2005-03-20 00:20:06 +0000508 // I'm assuming that for 'modify' instructions, that Vex always makes
509 // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp
510 // expressions, or both Const expressions.
sewardj710d6c22005-03-20 18:55:15 +0000511 tl_assert(isIRAtom(loadAddrExpr));
512 tl_assert(isIRAtom(storeAddrExpr));
513 return eqIRAtom(loadAddrExpr, storeAddrExpr);
njn6a3009b2005-03-20 00:20:06 +0000514}
515
516// Instrumentation for the end of each original instruction.
517static
518void endOfInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore,
519 UInt instrAddr, UInt instrLen, UInt dataSize,
520 IRExpr* loadAddrExpr, IRExpr* storeAddrExpr)
521{
522 IRDirty* di;
523 IRExpr *arg1, *arg2, *arg3, **argv;
524 Int argc;
525 Char* helperName;
526 void* helperAddr;
sewardj17a56bf2005-03-21 01:35:02 +0000527 IRType wordTy;
528
529 // Stay sane ...
530 tl_assert(sizeof(HWord) == sizeof(void*));
531 if (sizeof(HWord) == 4) {
532 wordTy = Ity_I32;
533 } else
534 if (sizeof(HWord) == 8) {
535 wordTy = Ity_I64;
536 } else {
537 VG_(tool_panic)("endOfInstr: strange word size");
538 }
539
540 if (loadAddrExpr)
541 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr));
542 if (storeAddrExpr)
543 tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr));
544
njn6a3009b2005-03-20 00:20:06 +0000545
546 // Nb: instrLen will be zero if Vex failed to decode it.
547 tl_assert( 0 == instrLen ||
njna60a7c12005-05-08 17:49:37 +0000548 (instrLen >= VGA_MIN_INSTR_SZB &&
549 instrLen <= VGA_MAX_INSTR_SZB) );
njn6a3009b2005-03-20 00:20:06 +0000550
njn016712a2005-04-04 02:52:16 +0000551 // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be
552 // done inaccurately, but they're very rare and this avoids errors from
553 // hitting more than two cache lines in the simulation.
554 if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE;
555
njn6a3009b2005-03-20 00:20:06 +0000556 // Setup 1st arg: instr_info node's address
sewardj17a56bf2005-03-21 01:35:02 +0000557 // Believed to be 64-bit clean
njn6a3009b2005-03-20 00:20:06 +0000558 do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize );
sewardj17a56bf2005-03-21 01:35:02 +0000559 arg1 = mkIRExpr_HWord( (HWord)i_node );
njn6a3009b2005-03-20 00:20:06 +0000560
561 if (!loadAddrExpr && !storeAddrExpr) {
562 // no load/store
563 tl_assert(0 == dataSize);
564 helperName = "log_1I_0D_cache_access";
565 helperAddr = &log_1I_0D_cache_access;
566 argc = 1;
567 argv = mkIRExprVec_1(arg1);
568
569 } else if (loadAddrExpr && !storeAddrExpr) {
570 // load
sewardj710d6c22005-03-20 18:55:15 +0000571 tl_assert( isIRAtom(loadAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000572 helperName = "log_1I_1Dr_cache_access";
573 helperAddr = &log_1I_1Dr_cache_access;
574 argc = 2;
575 arg2 = loadAddrExpr;
576 argv = mkIRExprVec_2(arg1, arg2);
577
578 } else if (!loadAddrExpr && storeAddrExpr) {
579 // store
sewardj710d6c22005-03-20 18:55:15 +0000580 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000581 helperName = "log_1I_1Dw_cache_access";
582 helperAddr = &log_1I_1Dw_cache_access;
583 argc = 2;
584 arg2 = storeAddrExpr;
585 argv = mkIRExprVec_2(arg1, arg2);
586
587 } else {
588 tl_assert( loadAddrExpr && storeAddrExpr );
sewardj710d6c22005-03-20 18:55:15 +0000589 tl_assert( isIRAtom(loadAddrExpr) );
590 tl_assert( isIRAtom(storeAddrExpr) );
njn6a3009b2005-03-20 00:20:06 +0000591
592 if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) {
593 // modify
594 helperName = "log_1I_1Dr_cache_access";
595 helperAddr = &log_1I_1Dr_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000596 argc = 2;
njn6a3009b2005-03-20 00:20:06 +0000597 arg2 = loadAddrExpr;
598 argv = mkIRExprVec_2(arg1, arg2);
599
nethercote9313ac42004-07-06 21:54:20 +0000600 } else {
njn6a3009b2005-03-20 00:20:06 +0000601 // load/store
602 helperName = "log_1I_2D_cache_access";
603 helperAddr = &log_1I_2D_cache_access;
nethercote9313ac42004-07-06 21:54:20 +0000604 argc = 3;
njn6a3009b2005-03-20 00:20:06 +0000605 arg2 = loadAddrExpr;
606 arg3 = storeAddrExpr;
607 argv = mkIRExprVec_3(arg1, arg2, arg3);
njn4f9c9342002-04-29 16:03:24 +0000608 }
609 }
610
njn6a3009b2005-03-20 00:20:06 +0000611 // Add call to the instrumentation function
612 di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv);
613 addStmtToIRBB( bbOut, IRStmt_Dirty(di) );
njn4f9c9342002-04-29 16:03:24 +0000614}
njn14d01ce2004-11-26 11:30:14 +0000615
njn51d827b2005-05-09 01:02:08 +0000616static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout,
617 IRType gWordTy, IRType hWordTy )
njn14d01ce2004-11-26 11:30:14 +0000618{
njn6a3009b2005-03-20 00:20:06 +0000619 Int i, dataSize = 0, bbInfo_i;
620 IRBB* bbOut;
621 IRStmt* st;
622 BB_info* bbInfo;
623 Bool bbSeenBefore = False;
624 Addr instrAddr, origAddr;
625 UInt instrLen;
626 IRExpr *loadAddrExpr, *storeAddrExpr;
627
sewardjd54babf2005-03-21 00:55:49 +0000628 if (gWordTy != hWordTy) {
629 /* We don't currently support this case. */
630 VG_(tool_panic)("host/guest word size mismatch");
631 }
632
njn6a3009b2005-03-20 00:20:06 +0000633 /* Set up BB */
634 bbOut = emptyIRBB();
635 bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv);
636 bbOut->next = dopyIRExpr(bbIn->next);
637 bbOut->jumpkind = bbIn->jumpkind;
638
639 // Get the first statement, and origAddr from it
640 i = 0;
641 tl_assert(bbIn->stmts_used > 0);
642 st = bbIn->stmts[0];
643 tl_assert(Ist_IMark == st->tag);
644 origAddr = (Addr)st->Ist.IMark.addr;
645 tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow
646
647 // Get block info
648 bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore);
649 bbInfo_i = 0;
650
651 do {
652 // We should be at an IMark statement
653 tl_assert(Ist_IMark == st->tag);
654
655 // Reset stuff for this original instruction
656 loadAddrExpr = storeAddrExpr = NULL;
657 dataSize = 0;
658
659 // Process all the statements for this original instruction (ie. until
660 // the next IMark statement, or the end of the block)
661 do {
662 handleOneStatement(bbIn->tyenv, bbOut, st, &instrAddr, &instrLen,
663 &loadAddrExpr, &storeAddrExpr, &dataSize);
664 i++;
665 st = ( i < bbIn->stmts_used ? bbIn->stmts[i] : NULL );
666 }
667 while (st && Ist_IMark != st->tag);
668
669 // Add instrumentation for this original instruction.
670 endOfInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore,
671 instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr);
672
673 bbInfo_i++;
674 }
675 while (st);
676
677 return bbOut;
njn14d01ce2004-11-26 11:30:14 +0000678}
njn4f9c9342002-04-29 16:03:24 +0000679
680/*------------------------------------------------------------*/
nethercoteb35a8b92004-09-11 16:45:27 +0000681/*--- Cache configuration ---*/
njn4f9c9342002-04-29 16:03:24 +0000682/*------------------------------------------------------------*/
683
sewardjb5f6f512005-03-10 23:59:00 +0000684#define UNDEFINED_CACHE { -1, -1, -1 }
njn25e49d8e72002-09-23 09:36:25 +0000685
686static cache_t clo_I1_cache = UNDEFINED_CACHE;
687static cache_t clo_D1_cache = UNDEFINED_CACHE;
688static cache_t clo_L2_cache = UNDEFINED_CACHE;
689
njn7cf0bd32002-06-08 13:36:03 +0000690/* Checks cache config is ok; makes it so if not. */
sewardj07133bf2002-06-13 10:25:56 +0000691static
njna1d1a642004-11-26 18:36:02 +0000692void check_cache(cache_t* cache, Char *name)
njn7cf0bd32002-06-08 13:36:03 +0000693{
694 /* First check they're all powers of two */
sewardj07133bf2002-06-13 10:25:56 +0000695 if (-1 == VG_(log2)(cache->size)) {
njn7cf0bd32002-06-08 13:36:03 +0000696 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000697 "error: %s size of %dB not a power of two; aborting.",
698 name, cache->size);
699 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000700 }
701
sewardj07133bf2002-06-13 10:25:56 +0000702 if (-1 == VG_(log2)(cache->assoc)) {
njn7cf0bd32002-06-08 13:36:03 +0000703 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000704 "error: %s associativity of %d not a power of two; aborting.",
705 name, cache->assoc);
706 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000707 }
708
sewardj07133bf2002-06-13 10:25:56 +0000709 if (-1 == VG_(log2)(cache->line_size)) {
njn7cf0bd32002-06-08 13:36:03 +0000710 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000711 "error: %s line size of %dB not a power of two; aborting.",
712 name, cache->line_size);
713 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000714 }
715
njn6a3009b2005-03-20 00:20:06 +0000716 // Then check line size >= 16 -- any smaller and a single instruction could
717 // straddle three cache lines, which breaks a simulation assertion and is
718 // stupid anyway.
njn7cf0bd32002-06-08 13:36:03 +0000719 if (cache->line_size < MIN_LINE_SIZE) {
720 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000721 "error: %s line size of %dB too small; aborting.",
722 name, cache->line_size);
723 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000724 }
725
726 /* Then check cache size > line size (causes seg faults if not). */
727 if (cache->size <= cache->line_size) {
728 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000729 "error: %s cache size of %dB <= line size of %dB; aborting.",
730 name, cache->size, cache->line_size);
731 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000732 }
733
734 /* Then check assoc <= (size / line size) (seg faults otherwise). */
735 if (cache->assoc > (cache->size / cache->line_size)) {
736 VG_(message)(Vg_UserMsg,
njna1d1a642004-11-26 18:36:02 +0000737 "warning: %s associativity > (size / line size); aborting.", name);
738 VG_(exit)(1);
njn7cf0bd32002-06-08 13:36:03 +0000739 }
740}
741
sewardj07133bf2002-06-13 10:25:56 +0000742static
nethercoteb35a8b92004-09-11 16:45:27 +0000743void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c)
njn7cf0bd32002-06-08 13:36:03 +0000744{
nethercote9313ac42004-07-06 21:54:20 +0000745#define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size)
746
nethercoteb35a8b92004-09-11 16:45:27 +0000747 Int n_clos = 0;
nethercote9313ac42004-07-06 21:54:20 +0000748
nethercoteb35a8b92004-09-11 16:45:27 +0000749 // Count how many were defined on the command line.
750 if (DEFINED(clo_I1_cache)) { n_clos++; }
751 if (DEFINED(clo_D1_cache)) { n_clos++; }
752 if (DEFINED(clo_L2_cache)) { n_clos++; }
njn7cf0bd32002-06-08 13:36:03 +0000753
njna1d1a642004-11-26 18:36:02 +0000754 // Set the cache config (using auto-detection, if supported by the
755 // architecture)
756 VGA_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) );
sewardjb1a77a42002-07-13 13:31:20 +0000757
nethercote9313ac42004-07-06 21:54:20 +0000758 // Then replace with any defined on the command line.
nethercoteb35a8b92004-09-11 16:45:27 +0000759 if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; }
760 if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; }
761 if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; }
njn7cf0bd32002-06-08 13:36:03 +0000762
nethercote9313ac42004-07-06 21:54:20 +0000763 // Then check values and fix if not acceptable.
njna1d1a642004-11-26 18:36:02 +0000764 check_cache(I1c, "I1");
765 check_cache(D1c, "D1");
766 check_cache(L2c, "L2");
njn7cf0bd32002-06-08 13:36:03 +0000767
768 if (VG_(clo_verbosity) > 1) {
769 VG_(message)(Vg_UserMsg, "Cache configuration used:");
770 VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines",
771 I1c->size, I1c->assoc, I1c->line_size);
772 VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines",
773 D1c->size, D1c->assoc, D1c->line_size);
774 VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines",
775 L2c->size, L2c->assoc, L2c->line_size);
776 }
nethercote9313ac42004-07-06 21:54:20 +0000777#undef CMD_LINE_DEFINED
njn7cf0bd32002-06-08 13:36:03 +0000778}
779
njn4f9c9342002-04-29 16:03:24 +0000780/*------------------------------------------------------------*/
njn51d827b2005-05-09 01:02:08 +0000781/*--- cg_fini() and related function ---*/
njn4f9c9342002-04-29 16:03:24 +0000782/*------------------------------------------------------------*/
783
nethercote9313ac42004-07-06 21:54:20 +0000784// Total reads/writes/misses. Calculated during CC traversal at the end.
785// All auto-zeroed.
786static CC Ir_total;
787static CC Dr_total;
788static CC Dw_total;
789
790static Char* cachegrind_out_file;
791
nethercote9313ac42004-07-06 21:54:20 +0000792static void fprint_lineCC(Int fd, lineCC* n)
njn4f9c9342002-04-29 16:03:24 +0000793{
nethercote9313ac42004-07-06 21:54:20 +0000794 Char buf[512];
795 VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
796 n->line,
797 n->Ir.a, n->Ir.m1, n->Ir.m2,
798 n->Dr.a, n->Dr.m1, n->Dr.m2,
799 n->Dw.a, n->Dw.m1, n->Dw.m2);
800 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
801
802 Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2;
803 Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2;
804 Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2;
805}
806
807static void fprint_CC_table_and_calc_totals(void)
808{
809 Int fd;
810 Char buf[512];
811 fileCC *curr_fileCC;
812 fnCC *curr_fnCC;
813 lineCC *curr_lineCC;
814 Int i, j, k;
njn4f9c9342002-04-29 16:03:24 +0000815
njn25e49d8e72002-09-23 09:36:25 +0000816 VGP_PUSHCC(VgpCacheResults);
njn13f02932003-04-30 20:23:58 +0000817
njndb918dd2003-07-22 20:45:11 +0000818 fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
njn13f02932003-04-30 20:23:58 +0000819 VKI_S_IRUSR|VKI_S_IWUSR);
nethercote50da0f32003-10-30 10:33:30 +0000820 if (fd < 0) {
nethercote9313ac42004-07-06 21:54:20 +0000821 // If the file can't be opened for whatever reason (conflict
822 // between multiple cachegrinded processes?), give up now.
njnee0e6a32005-04-24 00:21:01 +0000823 VG_(message)(Vg_UserMsg,
njn02bc4b82005-05-15 17:28:26 +0000824 "error: can't open cache simulation output file '%s'",
njnee0e6a32005-04-24 00:21:01 +0000825 cachegrind_out_file );
826 VG_(message)(Vg_UserMsg,
827 " ... so simulation results will be missing.");
sewardj0744b6c2002-12-11 00:45:42 +0000828 return;
829 }
njn4f9c9342002-04-29 16:03:24 +0000830
nethercote9313ac42004-07-06 21:54:20 +0000831 // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after
832 // the 2nd colon makes cg_annotate's output look nicer.
833 VG_(sprintf)(buf, "desc: I1 cache: %s\n"
834 "desc: D1 cache: %s\n"
835 "desc: L2 cache: %s\n",
836 I1.desc_line, D1.desc_line, L2.desc_line);
njn7cf0bd32002-06-08 13:36:03 +0000837 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn4f9c9342002-04-29 16:03:24 +0000838
nethercote9313ac42004-07-06 21:54:20 +0000839 // "cmd:" line
njn4f9c9342002-04-29 16:03:24 +0000840 VG_(strcpy)(buf, "cmd:");
841 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
njn25e49d8e72002-09-23 09:36:25 +0000842 for (i = 0; i < VG_(client_argc); i++) {
sewardj4bf3aba2005-05-13 23:32:43 +0000843 if (VG_(client_argv)[i] == NULL)
844 continue;
thughes6f7eb9c2004-10-06 13:50:12 +0000845 VG_(write)(fd, " ", 1);
thughes30c43d82004-10-06 13:49:36 +0000846 VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i]));
njn4f9c9342002-04-29 16:03:24 +0000847 }
nethercote9313ac42004-07-06 21:54:20 +0000848 // "events:" line
njn4f9c9342002-04-29 16:03:24 +0000849 VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n");
850 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
851
nethercote9313ac42004-07-06 21:54:20 +0000852 // Six loops here: three for the hash table arrays, and three for the
853 // chains hanging off the hash table arrays.
njn4f9c9342002-04-29 16:03:24 +0000854 for (i = 0; i < N_FILE_ENTRIES; i++) {
nethercote9313ac42004-07-06 21:54:20 +0000855 curr_fileCC = CC_table[i];
856 while (curr_fileCC != NULL) {
857 VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file);
njn4f9c9342002-04-29 16:03:24 +0000858 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
859
860 for (j = 0; j < N_FN_ENTRIES; j++) {
nethercote9313ac42004-07-06 21:54:20 +0000861 curr_fnCC = curr_fileCC->fns[j];
862 while (curr_fnCC != NULL) {
863 VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn);
njn4f9c9342002-04-29 16:03:24 +0000864 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
865
nethercote9313ac42004-07-06 21:54:20 +0000866 for (k = 0; k < N_LINE_ENTRIES; k++) {
867 curr_lineCC = curr_fnCC->lines[k];
868 while (curr_lineCC != NULL) {
869 fprint_lineCC(fd, curr_lineCC);
870 curr_lineCC = curr_lineCC->next;
njn4f9c9342002-04-29 16:03:24 +0000871 }
872 }
nethercote9313ac42004-07-06 21:54:20 +0000873 curr_fnCC = curr_fnCC->next;
njn4f9c9342002-04-29 16:03:24 +0000874 }
875 }
nethercote9313ac42004-07-06 21:54:20 +0000876 curr_fileCC = curr_fileCC->next;
njn4f9c9342002-04-29 16:03:24 +0000877 }
878 }
879
nethercote9313ac42004-07-06 21:54:20 +0000880 // Summary stats must come after rest of table, since we calculate them
881 // during traversal. */
njn4f9c9342002-04-29 16:03:24 +0000882 VG_(sprintf)(buf, "summary: "
nethercote9313ac42004-07-06 21:54:20 +0000883 "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n",
njn4f9c9342002-04-29 16:03:24 +0000884 Ir_total.a, Ir_total.m1, Ir_total.m2,
885 Dr_total.a, Dr_total.m1, Dr_total.m2,
886 Dw_total.a, Dw_total.m1, Dw_total.m2);
887 VG_(write)(fd, (void*)buf, VG_(strlen)(buf));
888 VG_(close)(fd);
889}
890
njn607adfc2003-09-30 14:15:44 +0000891static UInt ULong_width(ULong n)
njn4f9c9342002-04-29 16:03:24 +0000892{
njn607adfc2003-09-30 14:15:44 +0000893 UInt w = 0;
894 while (n > 0) {
895 n = n / 10;
896 w++;
njn4f9c9342002-04-29 16:03:24 +0000897 }
njn607adfc2003-09-30 14:15:44 +0000898 return w + (w-1)/3; // add space for commas
njn4f9c9342002-04-29 16:03:24 +0000899}
900
sewardj4f29ddf2002-05-03 22:29:04 +0000901static
daywalker8ad1a402003-09-18 01:15:32 +0000902void percentify(Int n, Int ex, Int field_width, char buf[])
njn4f9c9342002-04-29 16:03:24 +0000903{
904 int i, len, space;
905
daywalker8ad1a402003-09-18 01:15:32 +0000906 VG_(sprintf)(buf, "%d.%d%%", n / ex, n % ex);
njn4f9c9342002-04-29 16:03:24 +0000907 len = VG_(strlen)(buf);
908 space = field_width - len;
njn25e49d8e72002-09-23 09:36:25 +0000909 if (space < 0) space = 0; /* Allow for v. small field_width */
njn4f9c9342002-04-29 16:03:24 +0000910 i = len;
911
912 /* Right justify in field */
913 for ( ; i >= 0; i--) buf[i + space] = buf[i];
914 for (i = 0; i < space; i++) buf[i] = ' ';
915}
916
njn51d827b2005-05-09 01:02:08 +0000917static void cg_fini(Int exitcode)
njn4f9c9342002-04-29 16:03:24 +0000918{
nethercote9313ac42004-07-06 21:54:20 +0000919 static char buf1[128], buf2[128], buf3[128], fmt [128];
njn607adfc2003-09-30 14:15:44 +0000920
njn4f9c9342002-04-29 16:03:24 +0000921 CC D_total;
njn1d021fa2002-05-02 13:56:34 +0000922 ULong L2_total_m, L2_total_mr, L2_total_mw,
923 L2_total, L2_total_r, L2_total_w;
njn4f9c9342002-04-29 16:03:24 +0000924 Int l1, l2, l3;
925 Int p;
926
nethercote9313ac42004-07-06 21:54:20 +0000927 fprint_CC_table_and_calc_totals();
njn4f9c9342002-04-29 16:03:24 +0000928
njn7cf0bd32002-06-08 13:36:03 +0000929 if (VG_(clo_verbosity) == 0)
930 return;
931
njn4f9c9342002-04-29 16:03:24 +0000932 /* I cache results. Use the I_refs value to determine the first column
933 * width. */
njn607adfc2003-09-30 14:15:44 +0000934 l1 = ULong_width(Ir_total.a);
935 l2 = ULong_width(Dr_total.a);
936 l3 = ULong_width(Dw_total.a);
njn4f9c9342002-04-29 16:03:24 +0000937
njn607adfc2003-09-30 14:15:44 +0000938 /* Make format string, getting width right for numbers */
939 VG_(sprintf)(fmt, "%%s %%,%dld", l1);
940
941 VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a);
942 VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1);
943 VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000944
945 p = 100;
946
njn25e49d8e72002-09-23 09:36:25 +0000947 if (0 == Ir_total.a) Ir_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000948 percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1);
949 VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1);
950
951 percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1);
952 VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1);
953 VG_(message)(Vg_UserMsg, "");
954
955 /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the
956 * width of columns 2 & 3. */
957 D_total.a = Dr_total.a + Dw_total.a;
958 D_total.m1 = Dr_total.m1 + Dw_total.m1;
959 D_total.m2 = Dr_total.m2 + Dw_total.m2;
960
njn607adfc2003-09-30 14:15:44 +0000961 /* Make format string, getting width right for numbers */
962 VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3);
njn4f9c9342002-04-29 16:03:24 +0000963
njn607adfc2003-09-30 14:15:44 +0000964 VG_(message)(Vg_UserMsg, fmt, "D refs: ",
965 D_total.a, Dr_total.a, Dw_total.a);
966 VG_(message)(Vg_UserMsg, fmt, "D1 misses: ",
967 D_total.m1, Dr_total.m1, Dw_total.m1);
968 VG_(message)(Vg_UserMsg, fmt, "L2d misses: ",
969 D_total.m2, Dr_total.m2, Dw_total.m2);
njn4f9c9342002-04-29 16:03:24 +0000970
971 p = 10;
972
njn25e49d8e72002-09-23 09:36:25 +0000973 if (0 == D_total.a) D_total.a = 1;
974 if (0 == Dr_total.a) Dr_total.a = 1;
975 if (0 == Dw_total.a) Dw_total.a = 1;
njn4f9c9342002-04-29 16:03:24 +0000976 percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1);
977 percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2);
978 percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3);
979 VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3);
980
981 percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1);
982 percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2);
983 percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3);
984 VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3);
985 VG_(message)(Vg_UserMsg, "");
986
987 /* L2 overall results */
njn1d021fa2002-05-02 13:56:34 +0000988
989 L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1;
990 L2_total_r = Dr_total.m1 + Ir_total.m1;
991 L2_total_w = Dw_total.m1;
njn607adfc2003-09-30 14:15:44 +0000992 VG_(message)(Vg_UserMsg, fmt, "L2 refs: ",
993 L2_total, L2_total_r, L2_total_w);
njn1d021fa2002-05-02 13:56:34 +0000994
njn4f9c9342002-04-29 16:03:24 +0000995 L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2;
996 L2_total_mr = Dr_total.m2 + Ir_total.m2;
997 L2_total_mw = Dw_total.m2;
njn607adfc2003-09-30 14:15:44 +0000998 VG_(message)(Vg_UserMsg, fmt, "L2 misses: ",
999 L2_total_m, L2_total_mr, L2_total_mw);
njn4f9c9342002-04-29 16:03:24 +00001000
1001 percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1);
1002 percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2);
1003 percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3);
1004 VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3);
1005
1006
nethercote9313ac42004-07-06 21:54:20 +00001007 // Various stats
njn4f9c9342002-04-29 16:03:24 +00001008 if (VG_(clo_verbosity) > 1) {
nethercote9313ac42004-07-06 21:54:20 +00001009 int BB_lookups = full_debug_BBs + fn_debug_BBs +
njn4f9c9342002-04-29 16:03:24 +00001010 file_line_debug_BBs + no_debug_BBs;
1011
1012 VG_(message)(Vg_DebugMsg, "");
1013 VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files);
1014 VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns);
nethercote9313ac42004-07-06 21:54:20 +00001015 VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines);
1016 VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs);
njn4f9c9342002-04-29 16:03:24 +00001017 VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups);
1018 VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)",
1019 full_debug_BBs * 100 / BB_lookups,
1020 full_debug_BBs);
1021 VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)",
1022 file_line_debug_BBs * 100 / BB_lookups,
1023 file_line_debug_BBs);
1024 VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)",
nethercote9313ac42004-07-06 21:54:20 +00001025 fn_debug_BBs * 100 / BB_lookups,
1026 fn_debug_BBs);
njn4f9c9342002-04-29 16:03:24 +00001027 VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)",
1028 no_debug_BBs * 100 / BB_lookups,
1029 no_debug_BBs);
1030 VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations);
njn4f9c9342002-04-29 16:03:24 +00001031 }
njn25e49d8e72002-09-23 09:36:25 +00001032 VGP_POPCC(VgpCacheResults);
njn4f9c9342002-04-29 16:03:24 +00001033}
1034
nethercote9313ac42004-07-06 21:54:20 +00001035/*--------------------------------------------------------------------*/
1036/*--- Discarding BB info ---*/
1037/*--------------------------------------------------------------------*/
sewardj18d75132002-05-16 11:06:21 +00001038
nethercote9313ac42004-07-06 21:54:20 +00001039// Called when a translation is invalidated due to code unloading.
njn51d827b2005-05-09 01:02:08 +00001040static void cg_discard_basic_block_info ( Addr a, SizeT size )
sewardj18d75132002-05-16 11:06:21 +00001041{
nethercote9313ac42004-07-06 21:54:20 +00001042 VgHashNode** prev_next_ptr;
njn6a3009b2005-03-20 00:20:06 +00001043 VgHashNode* bbInfo;
njn4294fd42002-06-05 14:41:10 +00001044
nethercote928a5f72004-11-03 18:10:37 +00001045 if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size);
njn4294fd42002-06-05 14:41:10 +00001046
nethercote9313ac42004-07-06 21:54:20 +00001047 // Get BB info, remove from table, free BB info. Simple!
njn6a3009b2005-03-20 00:20:06 +00001048 bbInfo = VG_(HT_get_node)(instr_info_table, a, &prev_next_ptr);
1049 tl_assert(NULL != bbInfo);
1050 *prev_next_ptr = bbInfo->next;
1051 VG_(free)(bbInfo);
sewardj18d75132002-05-16 11:06:21 +00001052}
1053
1054/*--------------------------------------------------------------------*/
njn25e49d8e72002-09-23 09:36:25 +00001055/*--- Command line processing ---*/
1056/*--------------------------------------------------------------------*/
1057
nethercote9313ac42004-07-06 21:54:20 +00001058static void parse_cache_opt ( cache_t* cache, char* opt )
njn25e49d8e72002-09-23 09:36:25 +00001059{
nethercote9313ac42004-07-06 21:54:20 +00001060 int i = 0, i2, i3;
njn25e49d8e72002-09-23 09:36:25 +00001061
nethercote9313ac42004-07-06 21:54:20 +00001062 // Option argument looks like "65536,2,64".
1063 // Find commas, replace with NULs to make three independent
1064 // strings, then extract numbers, put NULs back. Yuck.
njn25e49d8e72002-09-23 09:36:25 +00001065 while (VG_(isdigit)(opt[i])) i++;
1066 if (',' == opt[i]) {
1067 opt[i++] = '\0';
1068 i2 = i;
1069 } else goto bad;
1070 while (VG_(isdigit)(opt[i])) i++;
1071 if (',' == opt[i]) {
1072 opt[i++] = '\0';
1073 i3 = i;
1074 } else goto bad;
1075 while (VG_(isdigit)(opt[i])) i++;
1076 if ('\0' != opt[i]) goto bad;
1077
nethercote9313ac42004-07-06 21:54:20 +00001078 cache->size = (Int)VG_(atoll)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001079 cache->assoc = (Int)VG_(atoll)(opt + i2);
1080 cache->line_size = (Int)VG_(atoll)(opt + i3);
1081
nethercote9313ac42004-07-06 21:54:20 +00001082 opt[i2-1] = ',';
1083 opt[i3-1] = ',';
njn25e49d8e72002-09-23 09:36:25 +00001084 return;
1085
1086 bad:
nethercote9313ac42004-07-06 21:54:20 +00001087 VG_(bad_option)(opt);
njn25e49d8e72002-09-23 09:36:25 +00001088}
1089
njn51d827b2005-05-09 01:02:08 +00001090static Bool cg_process_cmd_line_option(Char* arg)
njn25e49d8e72002-09-23 09:36:25 +00001091{
nethercote9313ac42004-07-06 21:54:20 +00001092 // 5 is length of "--I1="
njn39c86652003-05-21 10:13:39 +00001093 if (VG_CLO_STREQN(5, arg, "--I1="))
nethercote9313ac42004-07-06 21:54:20 +00001094 parse_cache_opt(&clo_I1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001095 else if (VG_CLO_STREQN(5, arg, "--D1="))
nethercote9313ac42004-07-06 21:54:20 +00001096 parse_cache_opt(&clo_D1_cache, &arg[5]);
njn39c86652003-05-21 10:13:39 +00001097 else if (VG_CLO_STREQN(5, arg, "--L2="))
nethercote9313ac42004-07-06 21:54:20 +00001098 parse_cache_opt(&clo_L2_cache, &arg[5]);
njn25e49d8e72002-09-23 09:36:25 +00001099 else
1100 return False;
1101
1102 return True;
1103}
1104
njn51d827b2005-05-09 01:02:08 +00001105static void cg_print_usage(void)
njn25e49d8e72002-09-23 09:36:25 +00001106{
njn3e884182003-04-15 13:03:23 +00001107 VG_(printf)(
njn25e49d8e72002-09-23 09:36:25 +00001108" --I1=<size>,<assoc>,<line_size> set I1 cache manually\n"
1109" --D1=<size>,<assoc>,<line_size> set D1 cache manually\n"
njn3e884182003-04-15 13:03:23 +00001110" --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"
1111 );
1112}
1113
njn51d827b2005-05-09 01:02:08 +00001114static void cg_print_debug_usage(void)
njn3e884182003-04-15 13:03:23 +00001115{
1116 VG_(printf)(
1117" (none)\n"
1118 );
njn25e49d8e72002-09-23 09:36:25 +00001119}
1120
1121/*--------------------------------------------------------------------*/
1122/*--- Setup ---*/
1123/*--------------------------------------------------------------------*/
1124
njn51d827b2005-05-09 01:02:08 +00001125static void cg_post_clo_init(void)
njn25e49d8e72002-09-23 09:36:25 +00001126{
1127 cache_t I1c, D1c, L2c;
njn25e49d8e72002-09-23 09:36:25 +00001128
nethercoteb35a8b92004-09-11 16:45:27 +00001129 configure_caches(&I1c, &D1c, &L2c);
njn25e49d8e72002-09-23 09:36:25 +00001130
1131 cachesim_I1_initcache(I1c);
1132 cachesim_D1_initcache(D1c);
1133 cachesim_L2_initcache(L2c);
1134
njn31066fd2005-03-26 00:42:02 +00001135 VG_(register_profile_event)(VgpGetLineCC, "get-lineCC");
1136 VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate");
1137 VG_(register_profile_event)(VgpCacheResults, "cache-results");
njn25e49d8e72002-09-23 09:36:25 +00001138}
1139
njn51d827b2005-05-09 01:02:08 +00001140static void cg_pre_clo_init(void)
1141{
1142 Char* base_dir = NULL;
1143
1144 VG_(details_name) ("Cachegrind");
1145 VG_(details_version) (NULL);
1146 VG_(details_description) ("an I1/D1/L2 cache profiler");
1147 VG_(details_copyright_author)(
1148 "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al.");
1149 VG_(details_bug_reports_to) (VG_BUGS_TO);
1150 VG_(details_avg_translation_sizeB) ( 155 );
1151
1152 VG_(basic_tool_funcs) (cg_post_clo_init,
1153 cg_instrument,
1154 cg_fini);
1155
1156 VG_(needs_basic_block_discards)(cg_discard_basic_block_info);
1157 VG_(needs_command_line_options)(cg_process_cmd_line_option,
1158 cg_print_usage,
1159 cg_print_debug_usage);
1160
1161 /* Get working directory */
1162 tl_assert( VG_(getcwd_alloc)(&base_dir) );
1163
1164 /* Block is big enough for dir name + cachegrind.out.<pid> */
1165 cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char));
1166 VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d",
1167 base_dir, VG_(getpid)());
1168 VG_(free)(base_dir);
1169
1170 instr_info_table = VG_(HT_construct)();
1171}
1172
1173VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0)
fitzhardinge98abfc72003-12-16 02:05:15 +00001174
njn25e49d8e72002-09-23 09:36:25 +00001175/*--------------------------------------------------------------------*/
njn25cac76cb2002-09-23 11:21:57 +00001176/*--- end cg_main.c ---*/
sewardj18d75132002-05-16 11:06:21 +00001177/*--------------------------------------------------------------------*/