sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | 101e572 | 2005-04-21 02:37:54 +0000 | [diff] [blame] | 3 | /*--- Cachegrind: everything but the simulation itself. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- cg_main.c ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of Cachegrind, a Valgrind tool for cache |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | profiling programs. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 10 | |
sewardj | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2006 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 12 | njn@valgrind.org |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 13 | |
| 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 33 | #include "pub_tool_vki.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 34 | #include "pub_tool_debuginfo.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcproc.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 40 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 41 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 42 | #include "pub_tool_options.h" |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 43 | #include "pub_tool_oset.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 44 | #include "pub_tool_tooliface.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 45 | #include "pub_tool_clientstate.h" |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 46 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 47 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 48 | #include "cg_arch.h" |
nethercote | 27fc1da | 2004-01-04 16:56:57 +0000 | [diff] [blame] | 49 | #include "cg_sim.c" |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 50 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 51 | /*------------------------------------------------------------*/ |
| 52 | /*--- Constants ---*/ |
| 53 | /*------------------------------------------------------------*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 54 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 55 | /* Set to 1 for very verbose debugging */ |
| 56 | #define DEBUG_CG 0 |
| 57 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 58 | #define MIN_LINE_SIZE 16 |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 59 | #define FILE_LEN VKI_PATH_MAX |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 60 | #define FN_LEN 256 |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 61 | |
| 62 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 63 | /*--- Types and Data Structures ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 64 | /*------------------------------------------------------------*/ |
| 65 | |
| 66 | typedef struct _CC CC; |
| 67 | struct _CC { |
| 68 | ULong a; |
| 69 | ULong m1; |
| 70 | ULong m2; |
| 71 | }; |
| 72 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 73 | //------------------------------------------------------------ |
| 74 | // Primary data structure #1: CC table |
| 75 | // - Holds the per-source-line hit/miss stats, grouped by file/function/line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 76 | // - an ordered set of CCs. CC indexing done by file/function/line (as |
| 77 | // determined from the instrAddr). |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 78 | // - Traversed for dumping stats at end in file/func/line hierarchy. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 79 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 80 | typedef struct { |
| 81 | Char* file; |
| 82 | Char* fn; |
| 83 | Int line; |
| 84 | } |
| 85 | CodeLoc; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 86 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 87 | typedef struct _LineCC LineCC; |
| 88 | struct _LineCC { |
| 89 | CodeLoc loc; |
| 90 | CC Ir; |
| 91 | CC Dr; |
| 92 | CC Dw; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 95 | // First compare file, then fn, then line. |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 96 | static Word cmp_CodeLoc_LineCC(void *vloc, void *vcc) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 97 | { |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 98 | Word res; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 99 | CodeLoc* a = (CodeLoc*)vloc; |
| 100 | CodeLoc* b = &(((LineCC*)vcc)->loc); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 101 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 102 | res = VG_(strcmp)(a->file, b->file); |
| 103 | if (0 != res) |
| 104 | return res; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 105 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 106 | res = VG_(strcmp)(a->fn, b->fn); |
| 107 | if (0 != res) |
| 108 | return res; |
| 109 | |
| 110 | return a->line - b->line; |
| 111 | } |
| 112 | |
| 113 | static OSet* CC_table; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 114 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 115 | //------------------------------------------------------------ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 116 | // Primary data structure #2: InstrInfo table |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 117 | // - Holds the cached info about each instr that is used for simulation. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 118 | // - table(BB_start_addr, list(InstrInfo)) |
| 119 | // - For each BB, each InstrInfo in the list holds info about the |
| 120 | // instruction (instrLen, instrAddr, etc), plus a pointer to its line |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 121 | // CC. This node is what's passed to the simulation function. |
| 122 | // - When BBs are discarded the relevant list(instr_details) is freed. |
| 123 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 124 | typedef struct _InstrInfo InstrInfo; |
| 125 | struct _InstrInfo { |
nethercote | ca1f2dc | 2004-07-21 08:49:02 +0000 | [diff] [blame] | 126 | Addr instr_addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 127 | UChar instr_len; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 128 | LineCC* parent; // parent line-CC |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | typedef struct _BB_info BB_info; |
| 132 | struct _BB_info { |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 133 | Addr BB_addr; // key; MUST BE FIRST |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 134 | Int n_instrs; |
| 135 | InstrInfo instrs[0]; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 138 | static OSet* instrInfoTable; |
| 139 | |
| 140 | //------------------------------------------------------------ |
| 141 | // Secondary data structure: string table |
| 142 | // - holds strings, avoiding dups |
| 143 | // - used for filenames and function names, each of which will be |
| 144 | // pointed to by one or more CCs. |
| 145 | // - it also allows equality checks just by pointer comparison, which |
| 146 | // is good when printing the output file at the end. |
| 147 | |
| 148 | static OSet* stringTable; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 149 | |
| 150 | //------------------------------------------------------------ |
| 151 | // Stats |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 152 | static Int distinct_files = 0; |
| 153 | static Int distinct_fns = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 154 | static Int distinct_lines = 0; |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 155 | static Int distinct_instrs = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 156 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 157 | static Int full_debugs = 0; |
| 158 | static Int file_line_debugs = 0; |
| 159 | static Int fn_debugs = 0; |
| 160 | static Int no_debugs = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 161 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 162 | /*------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 163 | /*--- String table operations ---*/ |
| 164 | /*------------------------------------------------------------*/ |
| 165 | |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 166 | static Word stringCmp( void* key, void* elem ) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 167 | { |
| 168 | return VG_(strcmp)(*(Char**)key, *(Char**)elem); |
| 169 | } |
| 170 | |
| 171 | // Get a permanent string; either pull it out of the string table if it's |
| 172 | // been encountered before, or dup it and put it into the string table. |
| 173 | static Char* get_perm_string(Char* s) |
| 174 | { |
| 175 | Char** s_ptr = VG_(OSet_Lookup)(stringTable, &s); |
| 176 | if (s_ptr) { |
| 177 | return *s_ptr; |
| 178 | } else { |
| 179 | Char** s_node = VG_(OSet_AllocNode)(stringTable, sizeof(Char*)); |
| 180 | *s_node = VG_(strdup)(s); |
| 181 | VG_(OSet_Insert)(stringTable, s_node); |
| 182 | return *s_node; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 187 | /*--- CC table operations ---*/ |
| 188 | /*------------------------------------------------------------*/ |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 189 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 190 | static void get_debug_info(Addr instr_addr, Char file[FILE_LEN], |
| 191 | Char fn[FN_LEN], Int* line) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 192 | { |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 193 | Bool found_file_line = VG_(get_filename_linenum)( |
| 194 | instr_addr, |
| 195 | file, FILE_LEN, |
| 196 | NULL, 0, NULL, |
| 197 | line |
| 198 | ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 199 | Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 200 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 201 | if (!found_file_line) { |
| 202 | VG_(strcpy)(file, "???"); |
| 203 | *line = 0; |
| 204 | } |
| 205 | if (!found_fn) { |
| 206 | VG_(strcpy)(fn, "???"); |
| 207 | } |
| 208 | if (found_file_line) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 209 | if (found_fn) full_debugs++; |
| 210 | else file_line_debugs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 211 | } else { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 212 | if (found_fn) fn_debugs++; |
| 213 | else no_debugs++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 217 | // Do a three step traversal: by file, then fn, then line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 218 | // Returns a pointer to the line CC, creates a new one if necessary. |
| 219 | static LineCC* get_lineCC(Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 220 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 221 | Char file[FILE_LEN], fn[FN_LEN]; |
| 222 | Int line; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 223 | CodeLoc loc; |
| 224 | LineCC* lineCC; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 225 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 226 | get_debug_info(origAddr, file, fn, &line); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 227 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 228 | loc.file = file; |
| 229 | loc.fn = fn; |
| 230 | loc.line = line; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 231 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 232 | lineCC = VG_(OSet_Lookup)(CC_table, &loc); |
| 233 | if (!lineCC) { |
| 234 | // Allocate and zero a new node. |
| 235 | lineCC = VG_(OSet_AllocNode)(CC_table, sizeof(LineCC)); |
| 236 | lineCC->loc.file = get_perm_string(loc.file); |
| 237 | lineCC->loc.fn = get_perm_string(loc.fn); |
| 238 | lineCC->loc.line = loc.line; |
| 239 | VG_(OSet_Insert)(CC_table, lineCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 240 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 241 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 242 | return lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 246 | /*--- Cache simulation functions ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 247 | /*------------------------------------------------------------*/ |
| 248 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 249 | static VG_REGPARM(1) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 250 | void log_1I_0D_cache_access(InstrInfo* n) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 251 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 252 | //VG_(printf)("1I_0D : CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n", |
| 253 | // n, n->instr_addr, n->instr_len); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 254 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 255 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 256 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 259 | static VG_REGPARM(2) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 260 | void log_2I_0D_cache_access(InstrInfo* n, InstrInfo* n2) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 261 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 262 | //VG_(printf)("2I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 263 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n", |
| 264 | // n, n->instr_addr, n->instr_len, |
| 265 | // n2, n2->instr_addr, n2->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 266 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
| 267 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 268 | n->parent->Ir.a++; |
| 269 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
| 270 | &n2->parent->Ir.m1, &n2->parent->Ir.m2); |
| 271 | n2->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 275 | void log_3I_0D_cache_access(InstrInfo* n, InstrInfo* n2, InstrInfo* n3) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 276 | { |
| 277 | //VG_(printf)("3I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 278 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n" |
| 279 | // " CC3addr=0x%010lx, i3addr=0x%010lx, i3size=%lu\n", |
| 280 | // n, n->instr_addr, n->instr_len, |
| 281 | // n2, n2->instr_addr, n2->instr_len, |
| 282 | // n3, n3->instr_addr, n3->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 283 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
| 284 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 285 | n->parent->Ir.a++; |
| 286 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
| 287 | &n2->parent->Ir.m1, &n2->parent->Ir.m2); |
| 288 | n2->parent->Ir.a++; |
| 289 | cachesim_I1_doref(n3->instr_addr, n3->instr_len, |
| 290 | &n3->parent->Ir.m1, &n3->parent->Ir.m2); |
| 291 | n3->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 295 | void log_1I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 296 | { |
| 297 | //VG_(printf)("1I_1Dr: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 298 | // " daddr=0x%010lx, dsize=%lu\n", |
| 299 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 300 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 301 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 302 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 303 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 304 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 305 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 306 | n->parent->Dr.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 307 | } |
| 308 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 309 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 310 | void log_1I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 311 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 312 | //VG_(printf)("1I_1Dw: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 313 | // " daddr=0x%010lx, dsize=%lu\n", |
| 314 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 315 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 316 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 317 | n->parent->Ir.a++; |
| 318 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 319 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 320 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 321 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 322 | } |
| 323 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 324 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 325 | void log_0I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 326 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 327 | //VG_(printf)("0I_1Dr: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 328 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 329 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 330 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 331 | n->parent->Dr.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 335 | void log_0I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 336 | { |
| 337 | //VG_(printf)("0I_1Dw: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 338 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 339 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 340 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 341 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 342 | } |
| 343 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 344 | /*------------------------------------------------------------*/ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 345 | /*--- Instrumentation types and structures ---*/ |
| 346 | /*------------------------------------------------------------*/ |
| 347 | |
| 348 | /* Maintain an ordered list of memory events which are outstanding, in |
| 349 | the sense that no IR has yet been generated to do the relevant |
| 350 | helper calls. The BB is scanned top to bottom and memory events |
| 351 | are added to the end of the list, merging with the most recent |
| 352 | notified event where possible (Dw immediately following Dr and |
| 353 | having the same size and EA can be merged). |
| 354 | |
| 355 | This merging is done so that for architectures which have |
| 356 | load-op-store instructions (x86, amd64), the insn is treated as if |
| 357 | it makes just one memory reference (a modify), rather than two (a |
| 358 | read followed by a write at the same address). |
| 359 | |
| 360 | At various points the list will need to be flushed, that is, IR |
| 361 | generated from it. That must happen before any possible exit from |
| 362 | the block (the end, or an IRStmt_Exit). Flushing also takes place |
| 363 | when there is no space to add a new event. |
| 364 | |
| 365 | If we require the simulation statistics to be up to date with |
| 366 | respect to possible memory exceptions, then the list would have to |
| 367 | be flushed before each memory reference. That would however lose |
| 368 | performance by inhibiting event-merging during flushing. |
| 369 | |
| 370 | Flushing the list consists of walking it start to end and emitting |
| 371 | instrumentation IR for each event, in the order in which they |
| 372 | appear. It may be possible to emit a single call for two adjacent |
| 373 | events in order to reduce the number of helper function calls made. |
| 374 | For example, it could well be profitable to handle two adjacent Ir |
| 375 | events with a single helper call. */ |
| 376 | |
| 377 | typedef |
| 378 | IRExpr |
| 379 | IRAtom; |
| 380 | |
| 381 | typedef |
sewardj | 20edebf | 2005-10-12 10:29:40 +0000 | [diff] [blame] | 382 | enum { Event_Ir, Event_Dr, Event_Dw, Event_Dm } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 383 | EventKind; |
| 384 | |
| 385 | typedef |
| 386 | struct { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 387 | EventKind ekind; // All |
| 388 | InstrInfo* inode; // All; inode for this event's instruction |
| 389 | Int datasize; // Dr/Dw/Dm only |
| 390 | IRAtom* dataEA; // Dr/Dw/Dm only; IR ATOM ONLY |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 391 | } |
| 392 | Event; |
| 393 | |
| 394 | /* Up to this many unnotified events are allowed. Number is |
| 395 | arbitrary. Larger numbers allow more event merging to occur, but |
| 396 | potentially induce more spilling due to extending live ranges of |
| 397 | address temporaries. */ |
| 398 | #define N_EVENTS 16 |
| 399 | |
| 400 | |
| 401 | /* A struct which holds all the running state during instrumentation. |
| 402 | Mostly to avoid passing loads of parameters everywhere. */ |
| 403 | typedef |
| 404 | struct { |
| 405 | /* The current outstanding-memory-event list. */ |
| 406 | Event events[N_EVENTS]; |
| 407 | Int events_used; |
| 408 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 409 | /* The array of InstrInfo bins for the BB. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 410 | BB_info* bbInfo; |
| 411 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 412 | /* Number InstrInfo bins 'used' so far. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 413 | Int bbInfo_i; |
| 414 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 415 | /* The output BB being constructed. */ |
| 416 | IRBB* bbOut; |
| 417 | } |
| 418 | CgState; |
| 419 | |
| 420 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 421 | /*------------------------------------------------------------*/ |
| 422 | /*--- Instrumentation main ---*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 423 | /*------------------------------------------------------------*/ |
| 424 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 425 | // Note that origAddr is the real origAddr, not the address of the first |
| 426 | // instruction in the block (they can be different due to redirection). |
nethercote | 564b2b0 | 2004-08-07 15:54:53 +0000 | [diff] [blame] | 427 | static |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 428 | BB_info* get_BB_info(IRBB* bbIn, Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 429 | { |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 430 | Int i, n_instrs; |
| 431 | IRStmt* st; |
| 432 | BB_info* bbInfo; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 433 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 434 | // Count number of original instrs in BB |
| 435 | n_instrs = 0; |
| 436 | for (i = 0; i < bbIn->stmts_used; i++) { |
| 437 | st = bbIn->stmts[i]; |
| 438 | if (Ist_IMark == st->tag) n_instrs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 439 | } |
| 440 | |
njn | f7d2609 | 2005-10-12 16:45:17 +0000 | [diff] [blame] | 441 | // Check that we don't have an entry for this BB in the instr-info table. |
| 442 | // If this assertion fails, there has been some screwup: some |
| 443 | // translations must have been discarded but Cachegrind hasn't discarded |
| 444 | // the corresponding entries in the instr-info table. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 445 | bbInfo = VG_(OSet_Lookup)(instrInfoTable, &origAddr); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 446 | tl_assert(NULL == bbInfo); |
| 447 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 448 | // BB never translated before (at this address, at least; could have |
| 449 | // been unloaded and then reloaded elsewhere in memory) |
| 450 | bbInfo = VG_(OSet_AllocNode)(instrInfoTable, |
| 451 | sizeof(BB_info) + n_instrs*sizeof(InstrInfo)); |
| 452 | bbInfo->BB_addr = origAddr; |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 453 | bbInfo->n_instrs = n_instrs; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 454 | VG_(OSet_Insert)( instrInfoTable, bbInfo ); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 455 | distinct_instrs++; |
| 456 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 457 | return bbInfo; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 458 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 459 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 460 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 461 | static void showEvent ( Event* ev ) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 462 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 463 | switch (ev->ekind) { |
| 464 | case Event_Ir: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 465 | VG_(printf)("Ir %p\n", ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 466 | break; |
| 467 | case Event_Dr: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 468 | VG_(printf)("Dr %p %d EA=", ev->inode, ev->datasize); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 469 | ppIRExpr(ev->dataEA); |
| 470 | VG_(printf)("\n"); |
| 471 | break; |
| 472 | case Event_Dw: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 473 | VG_(printf)("Dw %p %d EA=", ev->inode, ev->datasize); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 474 | ppIRExpr(ev->dataEA); |
| 475 | VG_(printf)("\n"); |
| 476 | break; |
| 477 | case Event_Dm: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 478 | VG_(printf)("Dm %p %d EA=", ev->inode, ev->datasize); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 479 | ppIRExpr(ev->dataEA); |
| 480 | VG_(printf)("\n"); |
| 481 | break; |
| 482 | default: |
| 483 | tl_assert(0); |
| 484 | break; |
| 485 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 486 | } |
| 487 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 488 | // Reserve and initialise an InstrInfo for the first mention of a new insn. |
| 489 | static |
| 490 | InstrInfo* setup_InstrInfo ( CgState* cgs, Addr instr_addr, UInt instr_len ) |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 491 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 492 | InstrInfo* i_node; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 493 | tl_assert(cgs->bbInfo_i >= 0); |
| 494 | tl_assert(cgs->bbInfo_i < cgs->bbInfo->n_instrs); |
| 495 | i_node = &cgs->bbInfo->instrs[ cgs->bbInfo_i ]; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 496 | i_node->instr_addr = instr_addr; |
| 497 | i_node->instr_len = instr_len; |
| 498 | i_node->parent = get_lineCC(instr_addr); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 499 | cgs->bbInfo_i++; |
| 500 | return i_node; |
| 501 | } |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 502 | |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 503 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 504 | /* Generate code for all outstanding memory events, and mark the queue |
| 505 | empty. Code is generated into cgs->bbOut, and this activity |
| 506 | 'consumes' slots in cgs->bbInfo. */ |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 507 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 508 | static void flushEvents ( CgState* cgs ) |
| 509 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 510 | Int i, regparms; |
| 511 | Char* helperName; |
| 512 | void* helperAddr; |
| 513 | IRExpr** argv; |
| 514 | IRExpr* i_node_expr; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 515 | IRDirty* di; |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 516 | Event* ev; |
| 517 | Event* ev2; |
| 518 | Event* ev3; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 519 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 520 | i = 0; |
| 521 | while (i < cgs->events_used) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 522 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 523 | helperName = NULL; |
| 524 | helperAddr = NULL; |
| 525 | argv = NULL; |
| 526 | regparms = 0; |
| 527 | |
| 528 | /* generate IR to notify event i and possibly the ones |
| 529 | immediately following it. */ |
| 530 | tl_assert(i >= 0 && i < cgs->events_used); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 531 | |
| 532 | ev = &cgs->events[i]; |
| 533 | ev2 = ( i < cgs->events_used-1 ? &cgs->events[i+1] : NULL ); |
| 534 | ev3 = ( i < cgs->events_used-2 ? &cgs->events[i+2] : NULL ); |
| 535 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 536 | if (DEBUG_CG) { |
| 537 | VG_(printf)(" flush "); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 538 | showEvent( ev ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 539 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 540 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 541 | i_node_expr = mkIRExpr_HWord( (HWord)ev->inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 542 | |
| 543 | /* Decide on helper fn to call and args to pass it, and advance |
| 544 | i appropriately. */ |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 545 | switch (ev->ekind) { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 546 | case Event_Ir: |
| 547 | /* Merge with a following Dr/Dm if it is from this insn. */ |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 548 | if (ev2 && (ev2->ekind == Event_Dr || ev2->ekind == Event_Dm)) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 549 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 550 | helperName = "log_1I_1Dr_cache_access"; |
| 551 | helperAddr = &log_1I_1Dr_cache_access; |
| 552 | argv = mkIRExprVec_3( i_node_expr, |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 553 | ev2->dataEA, |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 554 | mkIRExpr_HWord( ev2->datasize ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 555 | regparms = 3; |
| 556 | i += 2; |
| 557 | } |
| 558 | /* Merge with a following Dw if it is from this insn. */ |
| 559 | else |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 560 | if (ev2 && ev2->ekind == Event_Dw) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 561 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 562 | helperName = "log_1I_1Dw_cache_access"; |
| 563 | helperAddr = &log_1I_1Dw_cache_access; |
| 564 | argv = mkIRExprVec_3( i_node_expr, |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 565 | ev2->dataEA, |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 566 | mkIRExpr_HWord( ev2->datasize ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 567 | regparms = 3; |
| 568 | i += 2; |
| 569 | } |
| 570 | /* Merge with two following Irs if possible. */ |
| 571 | else |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 572 | if (ev2 && ev3 && ev2->ekind == Event_Ir && ev3->ekind == Event_Ir) |
| 573 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 574 | helperName = "log_3I_0D_cache_access"; |
| 575 | helperAddr = &log_3I_0D_cache_access; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 576 | argv = mkIRExprVec_3( i_node_expr, |
| 577 | mkIRExpr_HWord( (HWord)ev2->inode ), |
| 578 | mkIRExpr_HWord( (HWord)ev3->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 579 | regparms = 3; |
| 580 | i += 3; |
| 581 | } |
| 582 | /* Merge with a following Ir if possible. */ |
| 583 | else |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 584 | if (ev2 && ev2->ekind == Event_Ir) { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 585 | helperName = "log_2I_0D_cache_access"; |
| 586 | helperAddr = &log_2I_0D_cache_access; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 587 | argv = mkIRExprVec_2( i_node_expr, |
| 588 | mkIRExpr_HWord( (HWord)ev2->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 589 | regparms = 2; |
| 590 | i += 2; |
| 591 | } |
| 592 | /* No merging possible; emit as-is. */ |
| 593 | else { |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 594 | // Assertion: this Event_Ir must be the last one in the |
| 595 | // events buffer, otherwise it would have been merged with a |
| 596 | // following event. |
| 597 | tl_assert(!ev2 && !ev3); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 598 | helperName = "log_1I_0D_cache_access"; |
| 599 | helperAddr = &log_1I_0D_cache_access; |
| 600 | argv = mkIRExprVec_1( i_node_expr ); |
| 601 | regparms = 1; |
| 602 | i++; |
| 603 | } |
| 604 | break; |
| 605 | case Event_Dr: |
| 606 | case Event_Dm: |
| 607 | helperName = "log_0I_1Dr_cache_access"; |
| 608 | helperAddr = &log_0I_1Dr_cache_access; |
| 609 | argv = mkIRExprVec_3( i_node_expr, |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 610 | ev->dataEA, |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 611 | mkIRExpr_HWord( ev->datasize ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 612 | regparms = 3; |
| 613 | i++; |
| 614 | break; |
| 615 | case Event_Dw: |
| 616 | helperName = "log_0I_1Dw_cache_access"; |
| 617 | helperAddr = &log_0I_1Dw_cache_access; |
| 618 | argv = mkIRExprVec_3( i_node_expr, |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 619 | ev->dataEA, |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 620 | mkIRExpr_HWord( ev->datasize ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 621 | regparms = 3; |
| 622 | i++; |
| 623 | break; |
| 624 | default: |
| 625 | tl_assert(0); |
| 626 | } |
| 627 | |
| 628 | /* Add the helper. */ |
| 629 | tl_assert(helperName); |
| 630 | tl_assert(helperAddr); |
| 631 | tl_assert(argv); |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 632 | di = unsafeIRDirty_0_N( regparms, |
| 633 | helperName, VG_(fnptr_to_fnentry)( helperAddr ), |
| 634 | argv ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 635 | addStmtToIRBB( cgs->bbOut, IRStmt_Dirty(di) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 636 | } |
| 637 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 638 | cgs->events_used = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 639 | } |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 640 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 641 | static void addEvent_Ir ( CgState* cgs, InstrInfo* inode ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 642 | { |
| 643 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 644 | if (cgs->events_used == N_EVENTS) |
| 645 | flushEvents(cgs); |
| 646 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 647 | evt = &cgs->events[cgs->events_used]; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 648 | evt->ekind = Event_Ir; |
| 649 | evt->inode = inode; |
| 650 | evt->datasize = 0; |
| 651 | evt->dataEA = NULL; /*paranoia*/ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 652 | cgs->events_used++; |
| 653 | } |
| 654 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 655 | static |
| 656 | void addEvent_Dr ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 657 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 658 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 659 | tl_assert(isIRAtom(ea)); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 660 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
| 661 | if (cgs->events_used == N_EVENTS) |
| 662 | flushEvents(cgs); |
| 663 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 664 | evt = &cgs->events[cgs->events_used]; |
| 665 | evt->ekind = Event_Dr; |
| 666 | evt->inode = inode; |
| 667 | evt->datasize = datasize; |
| 668 | evt->dataEA = ea; |
| 669 | cgs->events_used++; |
| 670 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 671 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 672 | static |
| 673 | void addEvent_Dw ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
| 674 | { |
| 675 | Event* lastEvt; |
| 676 | Event* evt; |
| 677 | |
| 678 | tl_assert(isIRAtom(ea)); |
| 679 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
| 680 | |
| 681 | /* Is it possible to merge this write with the preceding read? */ |
| 682 | lastEvt = &cgs->events[cgs->events_used-1]; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 683 | if (cgs->events_used > 0 |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 684 | && lastEvt->ekind == Event_Dr |
| 685 | && lastEvt->datasize == datasize |
| 686 | && lastEvt->inode == inode |
| 687 | && eqIRAtom(lastEvt->dataEA, ea)) |
| 688 | { |
| 689 | lastEvt->ekind = Event_Dm; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | |
| 693 | /* No. Add as normal. */ |
| 694 | if (cgs->events_used == N_EVENTS) |
| 695 | flushEvents(cgs); |
| 696 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 697 | evt = &cgs->events[cgs->events_used]; |
| 698 | evt->ekind = Event_Dw; |
| 699 | evt->inode = inode; |
| 700 | evt->datasize = datasize; |
| 701 | evt->dataEA = ea; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 702 | cgs->events_used++; |
| 703 | } |
| 704 | |
| 705 | //////////////////////////////////////////////////////////// |
| 706 | |
| 707 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 708 | static |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 709 | IRBB* cg_instrument ( VgCallbackClosure* closure, |
| 710 | IRBB* bbIn, |
| 711 | VexGuestLayout* layout, |
| 712 | VexGuestExtents* vge, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 713 | IRType gWordTy, IRType hWordTy ) |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 714 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 715 | Int i, isize; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 716 | IRStmt* st; |
| 717 | Addr64 cia; /* address of current insn */ |
| 718 | CgState cgs; |
| 719 | IRTypeEnv* tyenv = bbIn->tyenv; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 720 | InstrInfo* curr_inode = NULL; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 721 | |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 722 | if (gWordTy != hWordTy) { |
| 723 | /* We don't currently support this case. */ |
| 724 | VG_(tool_panic)("host/guest word size mismatch"); |
| 725 | } |
| 726 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 727 | /* Set up BB, including copying of the where-next stuff. */ |
| 728 | cgs.bbOut = emptyIRBB(); |
| 729 | cgs.bbOut->tyenv = dopyIRTypeEnv(tyenv); |
| 730 | tl_assert( isIRAtom(bbIn->next) ); |
| 731 | cgs.bbOut->next = dopyIRExpr(bbIn->next); |
| 732 | cgs.bbOut->jumpkind = bbIn->jumpkind; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 733 | |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 734 | // Copy verbatim any IR preamble preceding the first IMark |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 735 | i = 0; |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 736 | while (i < bbIn->stmts_used && bbIn->stmts[i]->tag != Ist_IMark) { |
| 737 | addStmtToIRBB( cgs.bbOut, bbIn->stmts[i] ); |
| 738 | i++; |
| 739 | } |
| 740 | |
| 741 | // Get the first statement, and initial cia from it |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 742 | tl_assert(bbIn->stmts_used > 0); |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 743 | tl_assert(i < bbIn->stmts_used); |
| 744 | st = bbIn->stmts[i]; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 745 | tl_assert(Ist_IMark == st->tag); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 746 | cia = st->Ist.IMark.addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 747 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 748 | // Set up running state and get block info |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 749 | tl_assert(closure->readdr == vge->base[0]); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 750 | cgs.events_used = 0; |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 751 | cgs.bbInfo = get_BB_info(bbIn, (Addr)closure->readdr); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 752 | cgs.bbInfo_i = 0; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 753 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 754 | if (DEBUG_CG) |
| 755 | VG_(printf)("\n\n---------- cg_instrument ----------\n"); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 756 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 757 | // Traverse the block, initialising inodes, adding events and flushing as |
| 758 | // necessary. |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 759 | for (/*use current i*/; i < bbIn->stmts_used; i++) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 760 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 761 | st = bbIn->stmts[i]; |
| 762 | tl_assert(isFlatIRStmt(st)); |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 763 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 764 | switch (st->tag) { |
| 765 | case Ist_NoOp: |
| 766 | case Ist_AbiHint: |
| 767 | case Ist_Put: |
| 768 | case Ist_PutI: |
| 769 | case Ist_MFence: |
| 770 | break; |
njn | 20677cc | 2005-08-12 23:47:51 +0000 | [diff] [blame] | 771 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 772 | case Ist_IMark: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 773 | cia = st->Ist.IMark.addr; |
| 774 | isize = st->Ist.IMark.len; |
| 775 | |
| 776 | // If Vex fails to decode an instruction, the size will be zero. |
| 777 | // Pretend otherwise. |
| 778 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
| 779 | |
njn | a5ad9ba | 2005-11-10 15:20:37 +0000 | [diff] [blame] | 780 | // Sanity-check size. |
| 781 | tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB) |
| 782 | || VG_CLREQ_SZB == isize ); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 783 | |
| 784 | // Get space for and init the inode, record it as the current one. |
| 785 | // Subsequent Dr/Dw/Dm events from the same instruction will |
| 786 | // also use it. |
| 787 | curr_inode = setup_InstrInfo(&cgs, cia, isize); |
| 788 | |
| 789 | addEvent_Ir( &cgs, curr_inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 790 | break; |
| 791 | |
| 792 | case Ist_Tmp: { |
| 793 | IRExpr* data = st->Ist.Tmp.data; |
| 794 | if (data->tag == Iex_Load) { |
| 795 | IRExpr* aexpr = data->Iex.Load.addr; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 796 | // Note also, endianness info is ignored. I guess |
| 797 | // that's not interesting. |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 798 | addEvent_Dr( &cgs, curr_inode, sizeofIRType(data->Iex.Load.ty), |
| 799 | aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 800 | } |
| 801 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 802 | } |
| 803 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 804 | case Ist_Store: { |
| 805 | IRExpr* data = st->Ist.Store.data; |
| 806 | IRExpr* aexpr = st->Ist.Store.addr; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 807 | addEvent_Dw( &cgs, curr_inode, |
| 808 | sizeofIRType(typeOfIRExpr(tyenv, data)), aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 809 | break; |
| 810 | } |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 811 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 812 | case Ist_Dirty: { |
| 813 | Int dataSize; |
| 814 | IRDirty* d = st->Ist.Dirty.details; |
| 815 | if (d->mFx != Ifx_None) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 816 | /* This dirty helper accesses memory. Collect the details. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 817 | tl_assert(d->mAddr != NULL); |
| 818 | tl_assert(d->mSize != 0); |
| 819 | dataSize = d->mSize; |
| 820 | // Large (eg. 28B, 108B, 512B on x86) data-sized |
| 821 | // instructions will be done inaccurately, but they're |
| 822 | // very rare and this avoids errors from hitting more |
| 823 | // than two cache lines in the simulation. |
| 824 | if (dataSize > MIN_LINE_SIZE) |
| 825 | dataSize = MIN_LINE_SIZE; |
| 826 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 827 | addEvent_Dr( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 828 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 829 | addEvent_Dw( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 830 | } else { |
| 831 | tl_assert(d->mAddr == NULL); |
| 832 | tl_assert(d->mSize == 0); |
| 833 | } |
| 834 | break; |
| 835 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 836 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 837 | case Ist_Exit: |
| 838 | /* We may never reach the next statement, so need to flush |
| 839 | all outstanding transactions now. */ |
| 840 | flushEvents( &cgs ); |
| 841 | break; |
| 842 | |
| 843 | default: |
| 844 | tl_assert(0); |
| 845 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 846 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 847 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 848 | /* Copy the original statement */ |
| 849 | addStmtToIRBB( cgs.bbOut, st ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 850 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 851 | if (DEBUG_CG) { |
| 852 | ppIRStmt(st); |
| 853 | VG_(printf)("\n"); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* At the end of the bb. Flush outstandings. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 858 | flushEvents( &cgs ); |
| 859 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 860 | /* done. stay sane ... */ |
| 861 | tl_assert(cgs.bbInfo_i == cgs.bbInfo->n_instrs); |
| 862 | |
| 863 | if (DEBUG_CG) { |
| 864 | VG_(printf)( "goto {"); |
| 865 | ppIRJumpKind(bbIn->jumpkind); |
| 866 | VG_(printf)( "} "); |
| 867 | ppIRExpr( bbIn->next ); |
| 868 | VG_(printf)( "}\n"); |
| 869 | } |
| 870 | |
| 871 | return cgs.bbOut; |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 872 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 873 | |
| 874 | /*------------------------------------------------------------*/ |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 875 | /*--- Cache configuration ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 876 | /*------------------------------------------------------------*/ |
| 877 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 878 | #define UNDEFINED_CACHE { -1, -1, -1 } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 879 | |
| 880 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 881 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
| 882 | static cache_t clo_L2_cache = UNDEFINED_CACHE; |
| 883 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 884 | /* Checks cache config is ok; makes it so if not. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 885 | static |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 886 | void check_cache(cache_t* cache, Char *name) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 887 | { |
| 888 | /* First check they're all powers of two */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 889 | if (-1 == VG_(log2)(cache->size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 890 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 891 | "error: %s size of %dB not a power of two; aborting.", |
| 892 | name, cache->size); |
| 893 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 894 | } |
| 895 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 896 | if (-1 == VG_(log2)(cache->assoc)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 897 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 898 | "error: %s associativity of %d not a power of two; aborting.", |
| 899 | name, cache->assoc); |
| 900 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 901 | } |
| 902 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 903 | if (-1 == VG_(log2)(cache->line_size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 904 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 905 | "error: %s line size of %dB not a power of two; aborting.", |
| 906 | name, cache->line_size); |
| 907 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 908 | } |
| 909 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 910 | // Then check line size >= 16 -- any smaller and a single instruction could |
| 911 | // straddle three cache lines, which breaks a simulation assertion and is |
| 912 | // stupid anyway. |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 913 | if (cache->line_size < MIN_LINE_SIZE) { |
| 914 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 915 | "error: %s line size of %dB too small; aborting.", |
| 916 | name, cache->line_size); |
| 917 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | /* Then check cache size > line size (causes seg faults if not). */ |
| 921 | if (cache->size <= cache->line_size) { |
| 922 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 923 | "error: %s cache size of %dB <= line size of %dB; aborting.", |
| 924 | name, cache->size, cache->line_size); |
| 925 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | /* Then check assoc <= (size / line size) (seg faults otherwise). */ |
| 929 | if (cache->assoc > (cache->size / cache->line_size)) { |
| 930 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 931 | "warning: %s associativity > (size / line size); aborting.", name); |
| 932 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 936 | static |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 937 | void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 938 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 939 | #define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size) |
| 940 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 941 | Int n_clos = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 942 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 943 | // Count how many were defined on the command line. |
| 944 | if (DEFINED(clo_I1_cache)) { n_clos++; } |
| 945 | if (DEFINED(clo_D1_cache)) { n_clos++; } |
| 946 | if (DEFINED(clo_L2_cache)) { n_clos++; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 947 | |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 948 | // Set the cache config (using auto-detection, if supported by the |
| 949 | // architecture) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 950 | VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) ); |
sewardj | b1a77a4 | 2002-07-13 13:31:20 +0000 | [diff] [blame] | 951 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 952 | // Then replace with any defined on the command line. |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 953 | if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; } |
| 954 | if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; } |
| 955 | if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 956 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 957 | // Then check values and fix if not acceptable. |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 958 | check_cache(I1c, "I1"); |
| 959 | check_cache(D1c, "D1"); |
| 960 | check_cache(L2c, "L2"); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 961 | |
| 962 | if (VG_(clo_verbosity) > 1) { |
| 963 | VG_(message)(Vg_UserMsg, "Cache configuration used:"); |
| 964 | VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines", |
| 965 | I1c->size, I1c->assoc, I1c->line_size); |
| 966 | VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines", |
| 967 | D1c->size, D1c->assoc, D1c->line_size); |
| 968 | VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines", |
| 969 | L2c->size, L2c->assoc, L2c->line_size); |
| 970 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 971 | #undef CMD_LINE_DEFINED |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 972 | } |
| 973 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 974 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 975 | /*--- cg_fini() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 976 | /*------------------------------------------------------------*/ |
| 977 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 978 | // Total reads/writes/misses. Calculated during CC traversal at the end. |
| 979 | // All auto-zeroed. |
| 980 | static CC Ir_total; |
| 981 | static CC Dr_total; |
| 982 | static CC Dw_total; |
| 983 | |
| 984 | static Char* cachegrind_out_file; |
| 985 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 986 | static void fprint_CC_table_and_calc_totals(void) |
| 987 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 988 | Int i, fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 989 | SysRes sres; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 990 | Char buf[512], *currFile = NULL, *currFn = NULL; |
| 991 | LineCC* lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 992 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 993 | sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 994 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 995 | if (sres.isError) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 996 | // If the file can't be opened for whatever reason (conflict |
| 997 | // between multiple cachegrinded processes?), give up now. |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 998 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 999 | "error: can't open cache simulation output file '%s'", |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 1000 | cachegrind_out_file ); |
| 1001 | VG_(message)(Vg_UserMsg, |
| 1002 | " ... so simulation results will be missing."); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1003 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1004 | } else { |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame^] | 1005 | fd = sres.res; |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1006 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1007 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1008 | // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after |
| 1009 | // the 2nd colon makes cg_annotate's output look nicer. |
| 1010 | VG_(sprintf)(buf, "desc: I1 cache: %s\n" |
| 1011 | "desc: D1 cache: %s\n" |
| 1012 | "desc: L2 cache: %s\n", |
| 1013 | I1.desc_line, D1.desc_line, L2.desc_line); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1014 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1015 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1016 | // "cmd:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1017 | VG_(strcpy)(buf, "cmd:"); |
| 1018 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1019 | if (VG_(args_the_exename)) { |
| 1020 | VG_(write)(fd, " ", 1); |
| 1021 | VG_(write)(fd, VG_(args_the_exename), |
| 1022 | VG_(strlen)( VG_(args_the_exename) )); |
| 1023 | } |
| 1024 | for (i = 0; i < VG_(args_for_client).used; i++) { |
| 1025 | if (VG_(args_for_client).strs[i]) { |
| 1026 | VG_(write)(fd, " ", 1); |
| 1027 | VG_(write)(fd, VG_(args_for_client).strs[i], |
| 1028 | VG_(strlen)(VG_(args_for_client).strs[i])); |
| 1029 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1030 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1031 | // "events:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1032 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n"); |
| 1033 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1034 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1035 | // Traverse every lineCC |
| 1036 | VG_(OSet_ResetIter)(CC_table); |
| 1037 | while ( (lineCC = VG_(OSet_Next)(CC_table)) ) { |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1038 | Bool just_hit_a_new_file = False; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1039 | // If we've hit a new file, print a "fl=" line. Note that because |
| 1040 | // each string is stored exactly once in the string table, we can use |
| 1041 | // pointer comparison rather than strcmp() to test for equality, which |
| 1042 | // is good because most of the time the comparisons are equal and so |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1043 | // the whole strings would have to be checked. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1044 | if ( lineCC->loc.file != currFile ) { |
| 1045 | currFile = lineCC->loc.file; |
| 1046 | VG_(sprintf)(buf, "fl=%s\n", currFile); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1047 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1048 | distinct_files++; |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1049 | just_hit_a_new_file = True; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1050 | } |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1051 | // If we've hit a new function, print a "fn=" line. We know to do |
| 1052 | // this when the function name changes, and also every time we hit a |
| 1053 | // new file (in which case the new function name might be the same as |
| 1054 | // in the old file, hence the just_hit_a_new_file test). |
| 1055 | if ( just_hit_a_new_file || lineCC->loc.fn != currFn ) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1056 | currFn = lineCC->loc.fn; |
| 1057 | VG_(sprintf)(buf, "fn=%s\n", currFn); |
| 1058 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1059 | distinct_fns++; |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1060 | just_hit_a_new_file = False; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | // Print the LineCC |
| 1064 | VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 1065 | lineCC->loc.line, |
| 1066 | lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.m2, |
| 1067 | lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.m2, |
| 1068 | lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.m2); |
| 1069 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1070 | |
| 1071 | // Update summary stats |
| 1072 | Ir_total.a += lineCC->Ir.a; |
| 1073 | Ir_total.m1 += lineCC->Ir.m1; |
| 1074 | Ir_total.m2 += lineCC->Ir.m2; |
| 1075 | Dr_total.a += lineCC->Dr.a; |
| 1076 | Dr_total.m1 += lineCC->Dr.m1; |
| 1077 | Dr_total.m2 += lineCC->Dr.m2; |
| 1078 | Dw_total.a += lineCC->Dw.a; |
| 1079 | Dw_total.m1 += lineCC->Dw.m1; |
| 1080 | Dw_total.m2 += lineCC->Dw.m2; |
| 1081 | |
| 1082 | distinct_lines++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1085 | // Summary stats must come after rest of table, since we calculate them |
| 1086 | // during traversal. */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1087 | VG_(sprintf)(buf, "summary: " |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1088 | "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1089 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 1090 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 1091 | Dw_total.a, Dw_total.m1, Dw_total.m2); |
| 1092 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1093 | VG_(close)(fd); |
| 1094 | } |
| 1095 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1096 | static UInt ULong_width(ULong n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1097 | { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1098 | UInt w = 0; |
| 1099 | while (n > 0) { |
| 1100 | n = n / 10; |
| 1101 | w++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1102 | } |
sewardj | 46c59b1 | 2005-11-01 02:20:19 +0000 | [diff] [blame] | 1103 | if (w == 0) w = 1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1104 | return w + (w-1)/3; // add space for commas |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1107 | static void cg_fini(Int exitcode) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1108 | { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1109 | static Char buf1[128], buf2[128], buf3[128], buf4[123], fmt[128]; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1110 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1111 | CC D_total; |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1112 | ULong L2_total_m, L2_total_mr, L2_total_mw, |
| 1113 | L2_total, L2_total_r, L2_total_w; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1114 | Int l1, l2, l3; |
| 1115 | Int p; |
| 1116 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1117 | fprint_CC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1118 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1119 | if (VG_(clo_verbosity) == 0) |
| 1120 | return; |
| 1121 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1122 | /* I cache results. Use the I_refs value to determine the first column |
| 1123 | * width. */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1124 | l1 = ULong_width(Ir_total.a); |
| 1125 | l2 = ULong_width(Dr_total.a); |
| 1126 | l3 = ULong_width(Dw_total.a); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1127 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1128 | /* Make format string, getting width right for numbers */ |
njn | 99cb9e3 | 2005-09-25 17:59:16 +0000 | [diff] [blame] | 1129 | VG_(sprintf)(fmt, "%%s %%,%dllu", l1); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1130 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1131 | VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a); |
| 1132 | VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1); |
| 1133 | VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1134 | |
| 1135 | p = 100; |
| 1136 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1137 | if (0 == Ir_total.a) Ir_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1138 | VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1139 | VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1140 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1141 | VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1142 | VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); |
| 1143 | VG_(message)(Vg_UserMsg, ""); |
| 1144 | |
| 1145 | /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the |
| 1146 | * width of columns 2 & 3. */ |
| 1147 | D_total.a = Dr_total.a + Dw_total.a; |
| 1148 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
| 1149 | D_total.m2 = Dr_total.m2 + Dw_total.m2; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1150 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1151 | /* Make format string, getting width right for numbers */ |
njn | 99cb9e3 | 2005-09-25 17:59:16 +0000 | [diff] [blame] | 1152 | VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu rd + %%,%dllu wr)", l1, l2, l3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1153 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1154 | VG_(message)(Vg_UserMsg, fmt, "D refs: ", |
| 1155 | D_total.a, Dr_total.a, Dw_total.a); |
| 1156 | VG_(message)(Vg_UserMsg, fmt, "D1 misses: ", |
| 1157 | D_total.m1, Dr_total.m1, Dw_total.m1); |
| 1158 | VG_(message)(Vg_UserMsg, fmt, "L2d misses: ", |
| 1159 | D_total.m2, Dr_total.m2, Dw_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1160 | |
| 1161 | p = 10; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1162 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1163 | if (0 == D_total.a) D_total.a = 1; |
| 1164 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1165 | if (0 == Dw_total.a) Dw_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1166 | VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); |
| 1167 | VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); |
| 1168 | VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1169 | VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1170 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1171 | VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1); |
| 1172 | VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2); |
| 1173 | VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1174 | VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1175 | VG_(message)(Vg_UserMsg, ""); |
| 1176 | |
| 1177 | /* L2 overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1178 | |
| 1179 | L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1180 | L2_total_r = Dr_total.m1 + Ir_total.m1; |
| 1181 | L2_total_w = Dw_total.m1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1182 | VG_(message)(Vg_UserMsg, fmt, "L2 refs: ", |
| 1183 | L2_total, L2_total_r, L2_total_w); |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1184 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1185 | L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2; |
| 1186 | L2_total_mr = Dr_total.m2 + Ir_total.m2; |
| 1187 | L2_total_mw = Dw_total.m2; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1188 | VG_(message)(Vg_UserMsg, fmt, "L2 misses: ", |
| 1189 | L2_total_m, L2_total_mr, L2_total_mw); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1190 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1191 | VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); |
| 1192 | VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); |
| 1193 | VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1194 | VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1195 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1196 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1197 | // Various stats |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1198 | if (VG_(clo_verbosity) > 1) { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1199 | Int debug_lookups = full_debugs + fn_debugs + |
| 1200 | file_line_debugs + no_debugs; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1201 | |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1202 | VG_(message)(Vg_DebugMsg, ""); |
| 1203 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct files: %d", distinct_files); |
| 1204 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct fns: %d", distinct_fns); |
| 1205 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct lines: %d", distinct_lines); |
| 1206 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct instrs:%d", distinct_instrs); |
| 1207 | VG_(message)(Vg_DebugMsg, "cachegrind: debug lookups : %d", debug_lookups); |
| 1208 | |
| 1209 | VG_(percentify)(full_debugs, debug_lookups, 1, 6, buf1); |
| 1210 | VG_(percentify)(file_line_debugs, debug_lookups, 1, 6, buf2); |
| 1211 | VG_(percentify)(fn_debugs, debug_lookups, 1, 6, buf3); |
| 1212 | VG_(percentify)(no_debugs, debug_lookups, 1, 6, buf4); |
| 1213 | VG_(message)(Vg_DebugMsg, "cachegrind: with full info:%s (%d)", |
| 1214 | buf1, full_debugs); |
| 1215 | VG_(message)(Vg_DebugMsg, "cachegrind: with file/line info:%s (%d)", |
| 1216 | buf2, file_line_debugs); |
| 1217 | VG_(message)(Vg_DebugMsg, "cachegrind: with fn name info:%s (%d)", |
| 1218 | buf3, fn_debugs); |
| 1219 | VG_(message)(Vg_DebugMsg, "cachegrind: with zero info:%s (%d)", |
| 1220 | buf4, no_debugs); |
| 1221 | |
| 1222 | VG_(message)(Vg_DebugMsg, "cachegrind: string table size: %u", |
| 1223 | VG_(OSet_Size)(stringTable)); |
| 1224 | VG_(message)(Vg_DebugMsg, "cachegrind: CC table size: %u", |
| 1225 | VG_(OSet_Size)(CC_table)); |
| 1226 | VG_(message)(Vg_DebugMsg, "cachegrind: InstrInfo table size: %u", |
| 1227 | VG_(OSet_Size)(instrInfoTable)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1228 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1231 | /*--------------------------------------------------------------------*/ |
| 1232 | /*--- Discarding BB info ---*/ |
| 1233 | /*--------------------------------------------------------------------*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1234 | |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 1235 | // Called when a translation is removed from the translation cache for |
| 1236 | // any reason at all: to free up space, because the guest code was |
| 1237 | // unmapped or modified, or for any arbitrary reason. |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1238 | static |
| 1239 | void cg_discard_basic_block_info ( Addr64 orig_addr64, VexGuestExtents vge ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1240 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1241 | BB_info* bbInfo; |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 1242 | Addr orig_addr = (Addr)vge.base[0]; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1243 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1244 | tl_assert(vge.n_used > 0); |
| 1245 | |
| 1246 | if (DEBUG_CG) |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1247 | VG_(printf)( "discard_basic_block_info: %p, %p, %llu\n", |
| 1248 | (void*)(Addr)orig_addr, |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1249 | (void*)(Addr)vge.base[0], (ULong)vge.len[0]); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1250 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1251 | // Get BB info, remove from table, free BB info. Simple! Note that we |
| 1252 | // use orig_addr, not the first instruction address in vge. |
| 1253 | bbInfo = VG_(OSet_Remove)(instrInfoTable, &orig_addr); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1254 | tl_assert(NULL != bbInfo); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1255 | VG_(OSet_FreeNode)(instrInfoTable, bbInfo); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1259 | /*--- Command line processing ---*/ |
| 1260 | /*--------------------------------------------------------------------*/ |
| 1261 | |
njn | 0103de5 | 2005-10-10 16:49:01 +0000 | [diff] [blame] | 1262 | static void parse_cache_opt ( cache_t* cache, Char* opt ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1263 | { |
njn | 0103de5 | 2005-10-10 16:49:01 +0000 | [diff] [blame] | 1264 | Int i = 0, i2, i3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1265 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1266 | // Option argument looks like "65536,2,64". |
| 1267 | // Find commas, replace with NULs to make three independent |
| 1268 | // strings, then extract numbers, put NULs back. Yuck. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1269 | while (VG_(isdigit)(opt[i])) i++; |
| 1270 | if (',' == opt[i]) { |
| 1271 | opt[i++] = '\0'; |
| 1272 | i2 = i; |
| 1273 | } else goto bad; |
| 1274 | while (VG_(isdigit)(opt[i])) i++; |
| 1275 | if (',' == opt[i]) { |
| 1276 | opt[i++] = '\0'; |
| 1277 | i3 = i; |
| 1278 | } else goto bad; |
| 1279 | while (VG_(isdigit)(opt[i])) i++; |
| 1280 | if ('\0' != opt[i]) goto bad; |
| 1281 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1282 | cache->size = (Int)VG_(atoll)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1283 | cache->assoc = (Int)VG_(atoll)(opt + i2); |
| 1284 | cache->line_size = (Int)VG_(atoll)(opt + i3); |
| 1285 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1286 | opt[i2-1] = ','; |
| 1287 | opt[i3-1] = ','; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | |
| 1290 | bad: |
sewardj | 6893d65 | 2006-10-15 01:25:13 +0000 | [diff] [blame] | 1291 | VG_(err_bad_option)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1294 | static Bool cg_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1295 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1296 | // 5 is length of "--I1=" |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1297 | if (VG_CLO_STREQN(5, arg, "--I1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1298 | parse_cache_opt(&clo_I1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1299 | else if (VG_CLO_STREQN(5, arg, "--D1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1300 | parse_cache_opt(&clo_D1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1301 | else if (VG_CLO_STREQN(5, arg, "--L2=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1302 | parse_cache_opt(&clo_L2_cache, &arg[5]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1303 | else |
| 1304 | return False; |
| 1305 | |
| 1306 | return True; |
| 1307 | } |
| 1308 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1309 | static void cg_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1310 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1311 | VG_(printf)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1312 | " --I1=<size>,<assoc>,<line_size> set I1 cache manually\n" |
| 1313 | " --D1=<size>,<assoc>,<line_size> set D1 cache manually\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1314 | " --L2=<size>,<assoc>,<line_size> set L2 cache manually\n" |
| 1315 | ); |
| 1316 | } |
| 1317 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1318 | static void cg_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1319 | { |
| 1320 | VG_(printf)( |
| 1321 | " (none)\n" |
| 1322 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | /*--------------------------------------------------------------------*/ |
| 1326 | /*--- Setup ---*/ |
| 1327 | /*--------------------------------------------------------------------*/ |
| 1328 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1329 | static void cg_post_clo_init(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1330 | { |
| 1331 | cache_t I1c, D1c, L2c; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1332 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1333 | configure_caches(&I1c, &D1c, &L2c); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1334 | |
| 1335 | cachesim_I1_initcache(I1c); |
| 1336 | cachesim_D1_initcache(D1c); |
| 1337 | cachesim_L2_initcache(L2c); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1340 | static Char base_dir[VKI_PATH_MAX]; |
| 1341 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1342 | static void cg_pre_clo_init(void) |
| 1343 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1344 | VG_(details_name) ("Cachegrind"); |
| 1345 | VG_(details_version) (NULL); |
| 1346 | VG_(details_description) ("an I1/D1/L2 cache profiler"); |
| 1347 | VG_(details_copyright_author)( |
sewardj | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame] | 1348 | "Copyright (C) 2002-2006, and GNU GPL'd, by Nicholas Nethercote et al."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1349 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame^] | 1350 | VG_(details_avg_translation_sizeB) ( 500 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1351 | |
| 1352 | VG_(basic_tool_funcs) (cg_post_clo_init, |
| 1353 | cg_instrument, |
| 1354 | cg_fini); |
| 1355 | |
| 1356 | VG_(needs_basic_block_discards)(cg_discard_basic_block_info); |
| 1357 | VG_(needs_command_line_options)(cg_process_cmd_line_option, |
| 1358 | cg_print_usage, |
| 1359 | cg_print_debug_usage); |
| 1360 | |
| 1361 | /* Get working directory */ |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1362 | tl_assert( VG_(getcwd)(base_dir, VKI_PATH_MAX) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* Block is big enough for dir name + cachegrind.out.<pid> */ |
| 1365 | cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char)); |
| 1366 | VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d", |
| 1367 | base_dir, VG_(getpid)()); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1368 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1369 | CC_table = VG_(OSet_Create)(offsetof(LineCC, loc), |
| 1370 | cmp_CodeLoc_LineCC, |
| 1371 | VG_(malloc), VG_(free)); |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1372 | instrInfoTable = VG_(OSet_Create)(/*keyOff*/0, |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1373 | NULL, |
| 1374 | VG_(malloc), VG_(free)); |
| 1375 | stringTable = VG_(OSet_Create)(/*keyOff*/0, |
| 1376 | stringCmp, |
| 1377 | VG_(malloc), VG_(free)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1380 | VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1381 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1382 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1383 | /*--- end ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1384 | /*--------------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1385 | |