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