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" |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 45 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 46 | #include "cg_arch.h" |
nethercote | 27fc1da | 2004-01-04 16:56:57 +0000 | [diff] [blame] | 47 | #include "cg_sim.c" |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 48 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 49 | /*------------------------------------------------------------*/ |
| 50 | /*--- Constants ---*/ |
| 51 | /*------------------------------------------------------------*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 52 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 53 | #define MIN_LINE_SIZE 16 |
| 54 | #define FILE_LEN 256 |
| 55 | #define FN_LEN 256 |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 56 | |
| 57 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 58 | /*--- Profiling events ---*/ |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 59 | /*------------------------------------------------------------*/ |
| 60 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 61 | typedef |
| 62 | enum { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 63 | VgpGetLineCC = VgpFini+1, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | VgpCacheSimulate, |
| 65 | VgpCacheResults |
| 66 | } |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 67 | VgpToolCC; |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 68 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 69 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 70 | /*--- Types and Data Structures ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 71 | /*------------------------------------------------------------*/ |
| 72 | |
| 73 | typedef struct _CC CC; |
| 74 | struct _CC { |
| 75 | ULong a; |
| 76 | ULong m1; |
| 77 | ULong m2; |
| 78 | }; |
| 79 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 80 | //------------------------------------------------------------ |
| 81 | // Primary data structure #1: CC table |
| 82 | // - Holds the per-source-line hit/miss stats, grouped by file/function/line. |
| 83 | // - hash(file, hash(fn, hash(line+CC))) |
| 84 | // - Each hash table is separately chained. |
| 85 | // - The array sizes below work fairly well for Konqueror. |
| 86 | // - Lookups done by instr_addr, which is converted immediately to a source |
| 87 | // location. |
| 88 | // - Traversed for dumping stats at end in file/func/line hierarchy. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 89 | |
| 90 | #define N_FILE_ENTRIES 251 |
| 91 | #define N_FN_ENTRIES 53 |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 92 | #define N_LINE_ENTRIES 37 |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 93 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 94 | typedef struct _lineCC lineCC; |
| 95 | struct _lineCC { |
| 96 | Int line; |
| 97 | CC Ir; |
| 98 | CC Dr; |
| 99 | CC Dw; |
| 100 | lineCC* next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 103 | typedef struct _fnCC fnCC; |
| 104 | struct _fnCC { |
| 105 | Char* fn; |
| 106 | fnCC* next; |
| 107 | lineCC* lines[N_LINE_ENTRIES]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 110 | typedef struct _fileCC fileCC; |
| 111 | struct _fileCC { |
| 112 | Char* file; |
| 113 | fileCC* next; |
| 114 | fnCC* fns[N_FN_ENTRIES]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 117 | // Top level of CC table. Auto-zeroed. |
| 118 | static fileCC *CC_table[N_FILE_ENTRIES]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 119 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 120 | //------------------------------------------------------------ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 121 | // Primary data structure #2: Instr-info table |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 122 | // - Holds the cached info about each instr that is used for simulation. |
| 123 | // - table(BB_start_addr, list(instr_info)) |
| 124 | // - For each BB, each instr_info in the list holds info about the |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 125 | // instruction (instr_len, instr_addr, etc), plus a pointer to its line |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 126 | // CC. This node is what's passed to the simulation function. |
| 127 | // - When BBs are discarded the relevant list(instr_details) is freed. |
| 128 | |
| 129 | typedef struct _instr_info instr_info; |
| 130 | struct _instr_info { |
nethercote | ca1f2dc | 2004-07-21 08:49:02 +0000 | [diff] [blame] | 131 | Addr instr_addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 132 | UChar instr_len; |
nethercote | ca1f2dc | 2004-07-21 08:49:02 +0000 | [diff] [blame] | 133 | UChar data_size; |
| 134 | lineCC* parent; // parent line-CC |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | typedef struct _BB_info BB_info; |
| 138 | struct _BB_info { |
| 139 | BB_info* next; // next field |
| 140 | Addr BB_addr; // key |
| 141 | Int n_instrs; |
| 142 | instr_info instrs[0]; |
| 143 | }; |
| 144 | |
| 145 | VgHashTable instr_info_table; // hash(Addr, BB_info) |
| 146 | |
| 147 | //------------------------------------------------------------ |
| 148 | // Stats |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 149 | static Int distinct_files = 0; |
| 150 | static Int distinct_fns = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 151 | static Int distinct_lines = 0; |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 152 | static Int distinct_instrs = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 153 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 154 | static Int full_debug_BBs = 0; |
| 155 | static Int file_line_debug_BBs = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 156 | static Int fn_debug_BBs = 0; |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 157 | static Int no_debug_BBs = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 158 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 159 | static Int BB_retranslations = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 160 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 161 | /*------------------------------------------------------------*/ |
| 162 | /*--- CC table operations ---*/ |
| 163 | /*------------------------------------------------------------*/ |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 164 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 165 | static void get_debug_info(Addr instr_addr, Char file[FILE_LEN], |
| 166 | Char fn[FN_LEN], Int* line) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 167 | { |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 168 | Bool found_file_line = VG_(get_filename_linenum)( |
| 169 | instr_addr, |
| 170 | file, FILE_LEN, |
| 171 | NULL, 0, NULL, |
| 172 | line |
| 173 | ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 174 | Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 175 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 176 | if (!found_file_line) { |
| 177 | VG_(strcpy)(file, "???"); |
| 178 | *line = 0; |
| 179 | } |
| 180 | if (!found_fn) { |
| 181 | VG_(strcpy)(fn, "???"); |
| 182 | } |
| 183 | if (found_file_line) { |
| 184 | if (found_fn) full_debug_BBs++; |
| 185 | else file_line_debug_BBs++; |
| 186 | } else { |
| 187 | if (found_fn) fn_debug_BBs++; |
| 188 | else no_debug_BBs++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 192 | static UInt hash(Char *s, UInt table_size) |
| 193 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 194 | const int hash_constant = 256; |
| 195 | int hash_value = 0; |
| 196 | for ( ; *s; s++) |
| 197 | hash_value = (hash_constant * hash_value + *s) % table_size; |
| 198 | return hash_value; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 199 | } |
| 200 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 201 | static __inline__ |
| 202 | fileCC* new_fileCC(Char filename[], fileCC* next) |
nethercote | 09d853e | 2004-01-21 16:12:55 +0000 | [diff] [blame] | 203 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 204 | // Using calloc() zeroes the fns[] array |
| 205 | fileCC* cc = VG_(calloc)(1, sizeof(fileCC)); |
| 206 | cc->file = VG_(strdup)(filename); |
| 207 | cc->next = next; |
| 208 | return cc; |
nethercote | 09d853e | 2004-01-21 16:12:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 211 | static __inline__ |
| 212 | fnCC* new_fnCC(Char fn[], fnCC* next) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 213 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 214 | // Using calloc() zeroes the lines[] array |
| 215 | fnCC* cc = VG_(calloc)(1, sizeof(fnCC)); |
| 216 | cc->fn = VG_(strdup)(fn); |
| 217 | cc->next = next; |
| 218 | return cc; |
| 219 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 220 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 221 | static __inline__ |
| 222 | lineCC* new_lineCC(Int line, lineCC* next) |
| 223 | { |
| 224 | // Using calloc() zeroes the Ir/Dr/Dw CCs and the instrs[] array |
| 225 | lineCC* cc = VG_(calloc)(1, sizeof(lineCC)); |
| 226 | cc->line = line; |
| 227 | cc->next = next; |
| 228 | return cc; |
| 229 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 230 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 231 | // Do a three step traversal: by file, then fn, then line. |
| 232 | // In all cases prepends new nodes to their chain. Returns a pointer to the |
| 233 | // line node, creates a new one if necessary. |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 234 | static lineCC* get_lineCC(Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 235 | { |
| 236 | fileCC *curr_fileCC; |
| 237 | fnCC *curr_fnCC; |
| 238 | lineCC *curr_lineCC; |
| 239 | Char file[FILE_LEN], fn[FN_LEN]; |
| 240 | Int line; |
| 241 | UInt file_hash, fn_hash, line_hash; |
| 242 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 243 | get_debug_info(origAddr, file, fn, &line); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 244 | |
| 245 | VGP_PUSHCC(VgpGetLineCC); |
| 246 | |
| 247 | // level 1 |
| 248 | file_hash = hash(file, N_FILE_ENTRIES); |
| 249 | curr_fileCC = CC_table[file_hash]; |
| 250 | while (NULL != curr_fileCC && !VG_STREQ(file, curr_fileCC->file)) { |
| 251 | curr_fileCC = curr_fileCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 252 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 253 | if (NULL == curr_fileCC) { |
| 254 | CC_table[file_hash] = curr_fileCC = |
| 255 | new_fileCC(file, CC_table[file_hash]); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 256 | distinct_files++; |
| 257 | } |
| 258 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 259 | // level 2 |
| 260 | fn_hash = hash(fn, N_FN_ENTRIES); |
| 261 | curr_fnCC = curr_fileCC->fns[fn_hash]; |
| 262 | while (NULL != curr_fnCC && !VG_STREQ(fn, curr_fnCC->fn)) { |
| 263 | curr_fnCC = curr_fnCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 264 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 265 | if (NULL == curr_fnCC) { |
| 266 | curr_fileCC->fns[fn_hash] = curr_fnCC = |
| 267 | new_fnCC(fn, curr_fileCC->fns[fn_hash]); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 268 | distinct_fns++; |
| 269 | } |
| 270 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 271 | // level 3 |
| 272 | line_hash = line % N_LINE_ENTRIES; |
| 273 | curr_lineCC = curr_fnCC->lines[line_hash]; |
| 274 | while (NULL != curr_lineCC && line != curr_lineCC->line) { |
| 275 | curr_lineCC = curr_lineCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 276 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 277 | if (NULL == curr_lineCC) { |
| 278 | curr_fnCC->lines[line_hash] = curr_lineCC = |
| 279 | new_lineCC(line, curr_fnCC->lines[line_hash]); |
| 280 | distinct_lines++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 281 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 282 | |
| 283 | VGP_POPCC(VgpGetLineCC); |
| 284 | return curr_lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 288 | /*--- Cache simulation functions ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 289 | /*------------------------------------------------------------*/ |
| 290 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 291 | static VG_REGPARM(1) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 292 | void log_1I_0D_cache_access(instr_info* n) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 293 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 294 | //VG_(printf)("1I_0D : CCaddr=0x%x, iaddr=0x%x, isize=%u\n", |
| 295 | // n, n->instr_addr, n->instr_len); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 296 | VGP_PUSHCC(VgpCacheSimulate); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 297 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 298 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 299 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 300 | VGP_POPCC(VgpCacheSimulate); |
| 301 | } |
| 302 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 303 | static VG_REGPARM(2) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 304 | void log_1I_1Dr_cache_access(instr_info* n, Addr data_addr) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 305 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 306 | //VG_(printf)("1I_1Dr: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n", |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 307 | // n, n->instr_addr, n->instr_len, data_addr, n->data_size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 308 | VGP_PUSHCC(VgpCacheSimulate); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 309 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 310 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 311 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 312 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 313 | cachesim_D1_doref(data_addr, n->data_size, |
| 314 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 315 | n->parent->Dr.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 316 | VGP_POPCC(VgpCacheSimulate); |
| 317 | } |
| 318 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 319 | static VG_REGPARM(2) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 320 | void log_1I_1Dw_cache_access(instr_info* n, Addr data_addr) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 321 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 322 | //VG_(printf)("1I_1Dw: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n", |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 323 | // n, n->instr_addr, n->instr_len, data_addr, n->data_size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 324 | VGP_PUSHCC(VgpCacheSimulate); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 325 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 326 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 327 | n->parent->Ir.a++; |
| 328 | |
| 329 | cachesim_D1_doref(data_addr, n->data_size, |
| 330 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 331 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 332 | VGP_POPCC(VgpCacheSimulate); |
| 333 | } |
| 334 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 335 | static VG_REGPARM(3) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 336 | void log_1I_2D_cache_access(instr_info* n, Addr data_addr1, Addr data_addr2) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 337 | { |
| 338 | //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n", |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 339 | // n, n->instr_addr, n->instr_len, data_addr1, data_addr2, n->data_size); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 340 | VGP_PUSHCC(VgpCacheSimulate); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 341 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 342 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 343 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 344 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 345 | cachesim_D1_doref(data_addr1, n->data_size, |
| 346 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 347 | n->parent->Dr.a++; |
| 348 | cachesim_D1_doref(data_addr2, n->data_size, |
| 349 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 350 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 351 | VGP_POPCC(VgpCacheSimulate); |
| 352 | } |
| 353 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 354 | /*------------------------------------------------------------*/ |
| 355 | /*--- Instrumentation ---*/ |
| 356 | /*------------------------------------------------------------*/ |
| 357 | |
nethercote | 564b2b0 | 2004-08-07 15:54:53 +0000 | [diff] [blame] | 358 | static |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 359 | BB_info* get_BB_info(IRBB* bbIn, Addr origAddr, Bool* bbSeenBefore) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 360 | { |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 361 | Int i, n_instrs; |
| 362 | IRStmt* st; |
| 363 | BB_info* bbInfo; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 364 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 365 | // Count number of original instrs in BB |
| 366 | n_instrs = 0; |
| 367 | for (i = 0; i < bbIn->stmts_used; i++) { |
| 368 | st = bbIn->stmts[i]; |
| 369 | if (Ist_IMark == st->tag) n_instrs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Get the BB_info |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 373 | bbInfo = (BB_info*)VG_(HT_lookup)(instr_info_table, origAddr); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 374 | *bbSeenBefore = ( NULL == bbInfo ? False : True ); |
| 375 | if (*bbSeenBefore) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 376 | // BB must have been translated before, but flushed from the TT |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 377 | tl_assert(bbInfo->n_instrs == n_instrs ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 378 | BB_retranslations++; |
| 379 | } else { |
| 380 | // BB never translated before (at this address, at least; could have |
| 381 | // been unloaded and then reloaded elsewhere in memory) |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 382 | bbInfo = VG_(calloc)(1, sizeof(BB_info) + n_instrs*sizeof(instr_info)); |
| 383 | bbInfo->BB_addr = origAddr; |
| 384 | bbInfo->n_instrs = n_instrs; |
| 385 | VG_(HT_add_node)( instr_info_table, (VgHashNode*)bbInfo ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 386 | distinct_instrs++; |
| 387 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 388 | return bbInfo; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 389 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 390 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 391 | static |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 392 | Bool handleOneStatement(IRTypeEnv* tyenv, IRBB* bbOut, IRStmt* st, IRStmt* st2, |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 393 | Addr* instrAddr, UInt* instrLen, |
| 394 | IRExpr** loadAddrExpr, IRExpr** storeAddrExpr, |
| 395 | UInt* dataSize) |
| 396 | { |
sewardj | 7f4a862 | 2005-03-26 21:55:21 +0000 | [diff] [blame] | 397 | tl_assert(isFlatIRStmt(st)); |
| 398 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 399 | switch (st->tag) { |
sewardj | 21dc345 | 2005-03-21 00:27:41 +0000 | [diff] [blame] | 400 | case Ist_NoOp: |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 401 | case Ist_AbiHint: |
| 402 | case Ist_Put: |
| 403 | case Ist_PutI: |
| 404 | case Ist_MFence: |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 407 | case Ist_Exit: { |
| 408 | // This is a conditional jump. Most of the time, we want to add the |
| 409 | // instrumentation before it, to ensure it gets executed. Eg, (1) if |
| 410 | // this conditional jump is just before an IMark: |
| 411 | // |
| 412 | // t108 = Not1(t107) |
| 413 | // [add instrumentation here] |
| 414 | // if (t108) goto {Boring} 0x3A96637D:I32 |
| 415 | // ------ IMark(0x3A966370, 7) ------ |
| 416 | // |
| 417 | // or (2) if this conditional jump is the last thing before the |
| 418 | // block-ending unconditional jump: |
| 419 | // |
| 420 | // t111 = Not1(t110) |
| 421 | // [add instrumentation here] |
| 422 | // if (t111) goto {Boring} 0x3A96637D:I32 |
| 423 | // goto {Boring} 0x3A966370:I32 |
| 424 | // |
| 425 | // One case (3) where we want the instrumentation after the conditional |
| 426 | // jump is when the conditional jump is for an x86 REP instruction: |
| 427 | // |
| 428 | // ------ IMark(0x3A967F13, 2) ------ |
| 429 | // t1 = GET:I32(4) |
| 430 | // t6 = CmpEQ32(t1,0x0:I32) |
| 431 | // if (t6) goto {Boring} 0x3A967F15:I32 # ignore this cond jmp |
| 432 | // t7 = Sub32(t1,0x1:I32) |
| 433 | // PUT(4) = t7 |
| 434 | // ... |
| 435 | // t56 = Not1(t55) |
| 436 | // [add instrumentation here] |
| 437 | // if (t56) goto {Boring} 0x3A967F15:I32 |
| 438 | // |
| 439 | // Therefore, we return true if the next statement is an IMark, or if |
| 440 | // there is no next statement (which matches case (2), as the final |
| 441 | // unconditional jump is not represented in the IRStmt list). |
| 442 | // |
| 443 | // Note that this approach won't do in the long run for supporting |
| 444 | // PPC, but it's good enough for x86/AMD64 for the 3.0.X series. |
| 445 | if (NULL == st2 || Ist_IMark == st2->tag) |
| 446 | return True; |
| 447 | else |
| 448 | return False; |
| 449 | } |
sewardj | e6da2fa | 2005-05-12 17:57:14 +0000 | [diff] [blame] | 450 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 451 | case Ist_IMark: |
sewardj | 2b641fe | 2005-03-21 11:53:38 +0000 | [diff] [blame] | 452 | /* st->Ist.IMark.addr is a 64-bit int. ULong_to_Ptr casts this |
| 453 | to the host's native pointer type; if that is 32 bits then it |
| 454 | discards the upper 32 bits. If we are cachegrinding on a |
| 455 | 32-bit host then we are also ensured that the guest word size |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 456 | is 32 bits, due to the assertion in cg_instrument that the |
sewardj | 2b641fe | 2005-03-21 11:53:38 +0000 | [diff] [blame] | 457 | host and guest word sizes must be the same. Hence |
| 458 | st->Ist.IMark.addr will have been derived from a 32-bit guest |
| 459 | code address and truncation of it is safe. I believe this |
| 460 | assignment should be correct for both 32- and 64-bit |
| 461 | machines. */ |
| 462 | *instrAddr = (Addr)ULong_to_Ptr(st->Ist.IMark.addr); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 463 | *instrLen = st->Ist.IMark.len; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 464 | break; |
| 465 | |
| 466 | case Ist_Tmp: { |
| 467 | IRExpr* data = st->Ist.Tmp.data; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 468 | if (data->tag == Iex_Load) { |
| 469 | IRExpr* aexpr = data->Iex.Load.addr; |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 470 | tl_assert( isIRAtom(aexpr) ); |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 471 | // Note also, endianness info is ignored. I guess that's not |
| 472 | // interesting. |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 473 | // XXX: repe cmpsb does two loads... the first one is ignored here! |
| 474 | //tl_assert( NULL == *loadAddrExpr ); // XXX: ??? |
| 475 | *loadAddrExpr = aexpr; |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 476 | *dataSize = sizeofIRType(data->Iex.Load.ty); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 477 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | |
sewardj | 2e59585 | 2005-06-30 23:33:37 +0000 | [diff] [blame] | 481 | case Ist_Store: { |
| 482 | IRExpr* data = st->Ist.Store.data; |
| 483 | IRExpr* aexpr = st->Ist.Store.addr; |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 484 | tl_assert( isIRAtom(aexpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 485 | tl_assert( NULL == *storeAddrExpr ); // XXX: ??? |
| 486 | *storeAddrExpr = aexpr; |
| 487 | *dataSize = sizeofIRType(typeOfIRExpr(tyenv, data)); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 488 | break; |
| 489 | } |
| 490 | |
sewardj | 7f4a862 | 2005-03-26 21:55:21 +0000 | [diff] [blame] | 491 | case Ist_Dirty: { |
| 492 | IRDirty* d = st->Ist.Dirty.details; |
| 493 | if (d->mFx != Ifx_None) { |
| 494 | /* This dirty helper accesses memory. Collect the |
| 495 | details. */ |
| 496 | tl_assert(d->mAddr != NULL); |
| 497 | tl_assert(d->mSize != 0); |
| 498 | *dataSize = d->mSize; |
| 499 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
| 500 | *loadAddrExpr = d->mAddr; |
| 501 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
| 502 | *storeAddrExpr = d->mAddr; |
| 503 | } else { |
| 504 | tl_assert(d->mAddr == NULL); |
| 505 | tl_assert(d->mSize == 0); |
| 506 | } |
sewardj | 7f4a862 | 2005-03-26 21:55:21 +0000 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 510 | default: |
| 511 | VG_(printf)("\n"); |
| 512 | ppIRStmt(st); |
| 513 | VG_(printf)("\n"); |
| 514 | VG_(tool_panic)("Cachegrind: unhandled IRStmt"); |
| 515 | } |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 516 | |
| 517 | return False; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 518 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 519 | |
nethercote | 564b2b0 | 2004-08-07 15:54:53 +0000 | [diff] [blame] | 520 | static |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 521 | void do_details( instr_info* n, Bool bbSeenBefore, |
| 522 | Addr instr_addr, Int instr_len, Int data_size ) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 523 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 524 | if (bbSeenBefore) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 525 | tl_assert( n->instr_addr == instr_addr ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 526 | tl_assert( n->instr_len == instr_len ); |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 527 | tl_assert( n->data_size == data_size ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 528 | // Don't check that (n->parent == parent)... it's conceivable that |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 529 | // the debug info might change; the other asserts should be enough to |
| 530 | // detect anything strange. |
| 531 | } else { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 532 | lineCC* parent = get_lineCC(instr_addr); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 533 | n->instr_addr = instr_addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 534 | n->instr_len = instr_len; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 535 | n->data_size = data_size; |
| 536 | n->parent = parent; |
| 537 | } |
| 538 | } |
| 539 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 540 | static Bool loadStoreAddrsMatch(IRExpr* loadAddrExpr, IRExpr* storeAddrExpr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 541 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 542 | // I'm assuming that for 'modify' instructions, that Vex always makes |
| 543 | // the loadAddrExpr and storeAddrExpr be of the same type, ie. both Tmp |
| 544 | // expressions, or both Const expressions. |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 545 | tl_assert(isIRAtom(loadAddrExpr)); |
| 546 | tl_assert(isIRAtom(storeAddrExpr)); |
| 547 | return eqIRAtom(loadAddrExpr, storeAddrExpr); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Instrumentation for the end of each original instruction. |
| 551 | static |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 552 | void instrumentInstr(IRBB* bbOut, instr_info* i_node, Bool bbSeenBefore, |
| 553 | UInt instrAddr, UInt instrLen, UInt dataSize, |
| 554 | IRExpr* loadAddrExpr, IRExpr* storeAddrExpr) |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 555 | { |
| 556 | IRDirty* di; |
| 557 | IRExpr *arg1, *arg2, *arg3, **argv; |
| 558 | Int argc; |
| 559 | Char* helperName; |
| 560 | void* helperAddr; |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 561 | IRType wordTy; |
| 562 | |
| 563 | // Stay sane ... |
| 564 | tl_assert(sizeof(HWord) == sizeof(void*)); |
| 565 | if (sizeof(HWord) == 4) { |
| 566 | wordTy = Ity_I32; |
| 567 | } else |
| 568 | if (sizeof(HWord) == 8) { |
| 569 | wordTy = Ity_I64; |
| 570 | } else { |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 571 | VG_(tool_panic)("instrumentInstr: strange word size"); |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (loadAddrExpr) |
| 575 | tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, loadAddrExpr)); |
| 576 | if (storeAddrExpr) |
| 577 | tl_assert(wordTy == typeOfIRExpr(bbOut->tyenv, storeAddrExpr)); |
| 578 | |
njn | 016712a | 2005-04-04 02:52:16 +0000 | [diff] [blame] | 579 | // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be |
| 580 | // done inaccurately, but they're very rare and this avoids errors from |
| 581 | // hitting more than two cache lines in the simulation. |
| 582 | if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE; |
| 583 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 584 | // Setup 1st arg: instr_info node's address |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 585 | // Believed to be 64-bit clean |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 586 | do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize ); |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 587 | arg1 = mkIRExpr_HWord( (HWord)i_node ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 588 | |
| 589 | if (!loadAddrExpr && !storeAddrExpr) { |
| 590 | // no load/store |
| 591 | tl_assert(0 == dataSize); |
| 592 | helperName = "log_1I_0D_cache_access"; |
| 593 | helperAddr = &log_1I_0D_cache_access; |
| 594 | argc = 1; |
| 595 | argv = mkIRExprVec_1(arg1); |
| 596 | |
| 597 | } else if (loadAddrExpr && !storeAddrExpr) { |
| 598 | // load |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 599 | tl_assert( isIRAtom(loadAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 600 | helperName = "log_1I_1Dr_cache_access"; |
| 601 | helperAddr = &log_1I_1Dr_cache_access; |
| 602 | argc = 2; |
| 603 | arg2 = loadAddrExpr; |
| 604 | argv = mkIRExprVec_2(arg1, arg2); |
| 605 | |
| 606 | } else if (!loadAddrExpr && storeAddrExpr) { |
| 607 | // store |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 608 | tl_assert( isIRAtom(storeAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 609 | helperName = "log_1I_1Dw_cache_access"; |
| 610 | helperAddr = &log_1I_1Dw_cache_access; |
| 611 | argc = 2; |
| 612 | arg2 = storeAddrExpr; |
| 613 | argv = mkIRExprVec_2(arg1, arg2); |
| 614 | |
| 615 | } else { |
| 616 | tl_assert( loadAddrExpr && storeAddrExpr ); |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 617 | tl_assert( isIRAtom(loadAddrExpr) ); |
| 618 | tl_assert( isIRAtom(storeAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 619 | |
| 620 | if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) { |
| 621 | // modify |
| 622 | helperName = "log_1I_1Dr_cache_access"; |
| 623 | helperAddr = &log_1I_1Dr_cache_access; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 624 | argc = 2; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 625 | arg2 = loadAddrExpr; |
| 626 | argv = mkIRExprVec_2(arg1, arg2); |
| 627 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 628 | } else { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 629 | // load/store |
| 630 | helperName = "log_1I_2D_cache_access"; |
| 631 | helperAddr = &log_1I_2D_cache_access; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 632 | argc = 3; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 633 | arg2 = loadAddrExpr; |
| 634 | arg3 = storeAddrExpr; |
| 635 | argv = mkIRExprVec_3(arg1, arg2, arg3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 639 | // Add call to the instrumentation function |
| 640 | di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv); |
| 641 | addStmtToIRBB( bbOut, IRStmt_Dirty(di) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 642 | } |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 643 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 644 | static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout, |
| 645 | IRType gWordTy, IRType hWordTy ) |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 646 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 647 | Int i, dataSize = 0, bbInfo_i; |
| 648 | IRBB* bbOut; |
| 649 | IRStmt* st; |
| 650 | BB_info* bbInfo; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 651 | Bool bbSeenBefore = False, addedInstrumentation, addInstNow; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 652 | Addr instrAddr, origAddr; |
| 653 | UInt instrLen; |
| 654 | IRExpr *loadAddrExpr, *storeAddrExpr; |
| 655 | |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 656 | if (gWordTy != hWordTy) { |
| 657 | /* We don't currently support this case. */ |
| 658 | VG_(tool_panic)("host/guest word size mismatch"); |
| 659 | } |
| 660 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 661 | /* Set up BB */ |
| 662 | bbOut = emptyIRBB(); |
| 663 | bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv); |
| 664 | bbOut->next = dopyIRExpr(bbIn->next); |
| 665 | bbOut->jumpkind = bbIn->jumpkind; |
| 666 | |
| 667 | // Get the first statement, and origAddr from it |
| 668 | i = 0; |
| 669 | tl_assert(bbIn->stmts_used > 0); |
| 670 | st = bbIn->stmts[0]; |
| 671 | tl_assert(Ist_IMark == st->tag); |
| 672 | origAddr = (Addr)st->Ist.IMark.addr; |
| 673 | tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow |
| 674 | |
| 675 | // Get block info |
| 676 | bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore); |
| 677 | bbInfo_i = 0; |
| 678 | |
| 679 | do { |
| 680 | // We should be at an IMark statement |
| 681 | tl_assert(Ist_IMark == st->tag); |
| 682 | |
| 683 | // Reset stuff for this original instruction |
| 684 | loadAddrExpr = storeAddrExpr = NULL; |
| 685 | dataSize = 0; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 686 | addedInstrumentation = False; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 687 | |
| 688 | // Process all the statements for this original instruction (ie. until |
| 689 | // the next IMark statement, or the end of the block) |
| 690 | do { |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 691 | IRStmt* st2 = ( i+1 < bbIn->stmts_used ? bbIn->stmts[i+1] : NULL ); |
| 692 | |
| 693 | addInstNow = handleOneStatement(bbIn->tyenv, bbOut, st, st2, |
| 694 | &instrAddr, &instrLen, &loadAddrExpr, |
| 695 | &storeAddrExpr, &dataSize); |
| 696 | if (addInstNow) { |
| 697 | tl_assert(!addedInstrumentation); |
| 698 | addedInstrumentation = True; |
| 699 | |
njn | 20677cc | 2005-08-12 23:47:51 +0000 | [diff] [blame] | 700 | // Nb: instrLen will be zero if Vex failed to decode it. |
| 701 | // Also Client requests can appear to be very large (eg. 18 |
| 702 | // bytes on x86) because they are really multiple instructions. |
| 703 | tl_assert( 0 == instrLen || |
| 704 | bbIn->jumpkind == Ijk_ClientReq || |
| 705 | (instrLen >= VG_MIN_INSTR_SZB && |
| 706 | instrLen <= VG_MAX_INSTR_SZB) ); |
| 707 | |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 708 | // Add instrumentation before this statement. |
| 709 | instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore, |
| 710 | instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr); |
| 711 | } |
| 712 | |
| 713 | addStmtToIRBB( bbOut, st ); |
| 714 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 715 | i++; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 716 | st = st2; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 717 | } |
| 718 | while (st && Ist_IMark != st->tag); |
| 719 | |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 720 | if (!addedInstrumentation) { |
| 721 | // Add instrumentation now, after all the instruction's statements. |
| 722 | instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore, |
| 723 | instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr); |
| 724 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 725 | |
| 726 | bbInfo_i++; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 727 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 728 | while (st); |
| 729 | |
| 730 | return bbOut; |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 731 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 732 | |
| 733 | /*------------------------------------------------------------*/ |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 734 | /*--- Cache configuration ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 735 | /*------------------------------------------------------------*/ |
| 736 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 737 | #define UNDEFINED_CACHE { -1, -1, -1 } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 738 | |
| 739 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 740 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
| 741 | static cache_t clo_L2_cache = UNDEFINED_CACHE; |
| 742 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 743 | /* Checks cache config is ok; makes it so if not. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 744 | static |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 745 | void check_cache(cache_t* cache, Char *name) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 746 | { |
| 747 | /* First check they're all powers of two */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 748 | if (-1 == VG_(log2)(cache->size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 749 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 750 | "error: %s size of %dB not a power of two; aborting.", |
| 751 | name, cache->size); |
| 752 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 753 | } |
| 754 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 755 | if (-1 == VG_(log2)(cache->assoc)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 756 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 757 | "error: %s associativity of %d not a power of two; aborting.", |
| 758 | name, cache->assoc); |
| 759 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 760 | } |
| 761 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 762 | if (-1 == VG_(log2)(cache->line_size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 763 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 764 | "error: %s line size of %dB not a power of two; aborting.", |
| 765 | name, cache->line_size); |
| 766 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 767 | } |
| 768 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 769 | // Then check line size >= 16 -- any smaller and a single instruction could |
| 770 | // straddle three cache lines, which breaks a simulation assertion and is |
| 771 | // stupid anyway. |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 772 | if (cache->line_size < MIN_LINE_SIZE) { |
| 773 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 774 | "error: %s line size of %dB too small; aborting.", |
| 775 | name, cache->line_size); |
| 776 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* Then check cache size > line size (causes seg faults if not). */ |
| 780 | if (cache->size <= cache->line_size) { |
| 781 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 782 | "error: %s cache size of %dB <= line size of %dB; aborting.", |
| 783 | name, cache->size, cache->line_size); |
| 784 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* Then check assoc <= (size / line size) (seg faults otherwise). */ |
| 788 | if (cache->assoc > (cache->size / cache->line_size)) { |
| 789 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 790 | "warning: %s associativity > (size / line size); aborting.", name); |
| 791 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 795 | static |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 796 | void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 797 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 798 | #define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size) |
| 799 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 800 | Int n_clos = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 801 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 802 | // Count how many were defined on the command line. |
| 803 | if (DEFINED(clo_I1_cache)) { n_clos++; } |
| 804 | if (DEFINED(clo_D1_cache)) { n_clos++; } |
| 805 | if (DEFINED(clo_L2_cache)) { n_clos++; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 806 | |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 807 | // Set the cache config (using auto-detection, if supported by the |
| 808 | // architecture) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 809 | VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) ); |
sewardj | b1a77a4 | 2002-07-13 13:31:20 +0000 | [diff] [blame] | 810 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 811 | // Then replace with any defined on the command line. |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 812 | if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; } |
| 813 | if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; } |
| 814 | if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 815 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 816 | // Then check values and fix if not acceptable. |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 817 | check_cache(I1c, "I1"); |
| 818 | check_cache(D1c, "D1"); |
| 819 | check_cache(L2c, "L2"); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 820 | |
| 821 | if (VG_(clo_verbosity) > 1) { |
| 822 | VG_(message)(Vg_UserMsg, "Cache configuration used:"); |
| 823 | VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines", |
| 824 | I1c->size, I1c->assoc, I1c->line_size); |
| 825 | VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines", |
| 826 | D1c->size, D1c->assoc, D1c->line_size); |
| 827 | VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines", |
| 828 | L2c->size, L2c->assoc, L2c->line_size); |
| 829 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 830 | #undef CMD_LINE_DEFINED |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 831 | } |
| 832 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 833 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 834 | /*--- cg_fini() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 835 | /*------------------------------------------------------------*/ |
| 836 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 837 | // Total reads/writes/misses. Calculated during CC traversal at the end. |
| 838 | // All auto-zeroed. |
| 839 | static CC Ir_total; |
| 840 | static CC Dr_total; |
| 841 | static CC Dw_total; |
| 842 | |
| 843 | static Char* cachegrind_out_file; |
| 844 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 845 | static void fprint_lineCC(Int fd, lineCC* n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 846 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 847 | Char buf[512]; |
| 848 | VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 849 | n->line, |
| 850 | n->Ir.a, n->Ir.m1, n->Ir.m2, |
| 851 | n->Dr.a, n->Dr.m1, n->Dr.m2, |
| 852 | n->Dw.a, n->Dw.m1, n->Dw.m2); |
| 853 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 854 | |
| 855 | Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2; |
| 856 | Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2; |
| 857 | Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2; |
| 858 | } |
| 859 | |
| 860 | static void fprint_CC_table_and_calc_totals(void) |
| 861 | { |
| 862 | Int fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 863 | SysRes sres; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 864 | Char buf[512]; |
| 865 | fileCC *curr_fileCC; |
| 866 | fnCC *curr_fnCC; |
| 867 | lineCC *curr_lineCC; |
| 868 | Int i, j, k; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 869 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 870 | VGP_PUSHCC(VgpCacheResults); |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 871 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 872 | sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 873 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 874 | if (sres.isError) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 875 | // If the file can't be opened for whatever reason (conflict |
| 876 | // between multiple cachegrinded processes?), give up now. |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 877 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 878 | "error: can't open cache simulation output file '%s'", |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 879 | cachegrind_out_file ); |
| 880 | VG_(message)(Vg_UserMsg, |
| 881 | " ... so simulation results will be missing."); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 882 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 883 | } else { |
| 884 | fd = sres.val; |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 885 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 886 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 887 | // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after |
| 888 | // the 2nd colon makes cg_annotate's output look nicer. |
| 889 | VG_(sprintf)(buf, "desc: I1 cache: %s\n" |
| 890 | "desc: D1 cache: %s\n" |
| 891 | "desc: L2 cache: %s\n", |
| 892 | I1.desc_line, D1.desc_line, L2.desc_line); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 893 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 894 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 895 | // "cmd:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 896 | VG_(strcpy)(buf, "cmd:"); |
| 897 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 898 | for (i = 0; i < VG_(client_argc); i++) { |
njn | d111d10 | 2005-09-13 00:46:27 +0000 | [diff] [blame] | 899 | if (VG_(client_argv)[i]) { |
| 900 | VG_(write)(fd, " ", 1); |
| 901 | VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i])); |
| 902 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 903 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 904 | // "events:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 905 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n"); |
| 906 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 907 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 908 | // Six loops here: three for the hash table arrays, and three for the |
| 909 | // chains hanging off the hash table arrays. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 910 | for (i = 0; i < N_FILE_ENTRIES; i++) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 911 | curr_fileCC = CC_table[i]; |
| 912 | while (curr_fileCC != NULL) { |
| 913 | VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 914 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 915 | |
| 916 | for (j = 0; j < N_FN_ENTRIES; j++) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 917 | curr_fnCC = curr_fileCC->fns[j]; |
| 918 | while (curr_fnCC != NULL) { |
| 919 | VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 920 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 921 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 922 | for (k = 0; k < N_LINE_ENTRIES; k++) { |
| 923 | curr_lineCC = curr_fnCC->lines[k]; |
| 924 | while (curr_lineCC != NULL) { |
| 925 | fprint_lineCC(fd, curr_lineCC); |
| 926 | curr_lineCC = curr_lineCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 929 | curr_fnCC = curr_fnCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 932 | curr_fileCC = curr_fileCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
| 935 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 936 | // Summary stats must come after rest of table, since we calculate them |
| 937 | // during traversal. */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 938 | VG_(sprintf)(buf, "summary: " |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 939 | "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 940 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 941 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 942 | Dw_total.a, Dw_total.m1, Dw_total.m2); |
| 943 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 944 | VG_(close)(fd); |
| 945 | } |
| 946 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 947 | static UInt ULong_width(ULong n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 948 | { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 949 | UInt w = 0; |
| 950 | while (n > 0) { |
| 951 | n = n / 10; |
| 952 | w++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 953 | } |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 954 | return w + (w-1)/3; // add space for commas |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 955 | } |
| 956 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 957 | static void cg_fini(Int exitcode) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 958 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 959 | static char buf1[128], buf2[128], buf3[128], fmt [128]; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 960 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 961 | CC D_total; |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 962 | ULong L2_total_m, L2_total_mr, L2_total_mw, |
| 963 | L2_total, L2_total_r, L2_total_w; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 964 | Int l1, l2, l3; |
| 965 | Int p; |
| 966 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 967 | fprint_CC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 968 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 969 | if (VG_(clo_verbosity) == 0) |
| 970 | return; |
| 971 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 972 | /* I cache results. Use the I_refs value to determine the first column |
| 973 | * width. */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 974 | l1 = ULong_width(Ir_total.a); |
| 975 | l2 = ULong_width(Dr_total.a); |
| 976 | l3 = ULong_width(Dw_total.a); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 977 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 978 | /* Make format string, getting width right for numbers */ |
| 979 | VG_(sprintf)(fmt, "%%s %%,%dld", l1); |
| 980 | |
| 981 | VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a); |
| 982 | VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1); |
| 983 | VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 984 | |
| 985 | p = 100; |
| 986 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 987 | if (0 == Ir_total.a) Ir_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 988 | VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 989 | VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); |
| 990 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 991 | VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 992 | VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); |
| 993 | VG_(message)(Vg_UserMsg, ""); |
| 994 | |
| 995 | /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the |
| 996 | * width of columns 2 & 3. */ |
| 997 | D_total.a = Dr_total.a + Dw_total.a; |
| 998 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
| 999 | D_total.m2 = Dr_total.m2 + Dw_total.m2; |
| 1000 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1001 | /* Make format string, getting width right for numbers */ |
| 1002 | VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1003 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1004 | VG_(message)(Vg_UserMsg, fmt, "D refs: ", |
| 1005 | D_total.a, Dr_total.a, Dw_total.a); |
| 1006 | VG_(message)(Vg_UserMsg, fmt, "D1 misses: ", |
| 1007 | D_total.m1, Dr_total.m1, Dw_total.m1); |
| 1008 | VG_(message)(Vg_UserMsg, fmt, "L2d misses: ", |
| 1009 | D_total.m2, Dr_total.m2, Dw_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1010 | |
| 1011 | p = 10; |
| 1012 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1013 | if (0 == D_total.a) D_total.a = 1; |
| 1014 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1015 | if (0 == Dw_total.a) Dw_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1016 | VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); |
| 1017 | VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); |
| 1018 | VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1019 | VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1020 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1021 | VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1); |
| 1022 | VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2); |
| 1023 | VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1024 | VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1025 | VG_(message)(Vg_UserMsg, ""); |
| 1026 | |
| 1027 | /* L2 overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1028 | |
| 1029 | L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1030 | L2_total_r = Dr_total.m1 + Ir_total.m1; |
| 1031 | L2_total_w = Dw_total.m1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1032 | VG_(message)(Vg_UserMsg, fmt, "L2 refs: ", |
| 1033 | L2_total, L2_total_r, L2_total_w); |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1034 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1035 | L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2; |
| 1036 | L2_total_mr = Dr_total.m2 + Ir_total.m2; |
| 1037 | L2_total_mw = Dw_total.m2; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1038 | VG_(message)(Vg_UserMsg, fmt, "L2 misses: ", |
| 1039 | L2_total_m, L2_total_mr, L2_total_mw); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1040 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1041 | VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); |
| 1042 | VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); |
| 1043 | VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1044 | VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1045 | |
| 1046 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1047 | // Various stats |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1048 | if (VG_(clo_verbosity) > 1) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1049 | int BB_lookups = full_debug_BBs + fn_debug_BBs + |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1050 | file_line_debug_BBs + no_debug_BBs; |
| 1051 | |
| 1052 | VG_(message)(Vg_DebugMsg, ""); |
| 1053 | VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files); |
| 1054 | VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1055 | VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines); |
| 1056 | VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1057 | VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups); |
| 1058 | VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)", |
| 1059 | full_debug_BBs * 100 / BB_lookups, |
| 1060 | full_debug_BBs); |
| 1061 | VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)", |
| 1062 | file_line_debug_BBs * 100 / BB_lookups, |
| 1063 | file_line_debug_BBs); |
| 1064 | VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)", |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1065 | fn_debug_BBs * 100 / BB_lookups, |
| 1066 | fn_debug_BBs); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1067 | VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)", |
| 1068 | no_debug_BBs * 100 / BB_lookups, |
| 1069 | no_debug_BBs); |
| 1070 | VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1071 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1072 | VGP_POPCC(VgpCacheResults); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1075 | /*--------------------------------------------------------------------*/ |
| 1076 | /*--- Discarding BB info ---*/ |
| 1077 | /*--------------------------------------------------------------------*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1078 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1079 | // Called when a translation is invalidated due to code unloading. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1080 | static void cg_discard_basic_block_info ( Addr a, SizeT size ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1081 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1082 | VgHashNode* bbInfo; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1083 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1084 | if (0) VG_(printf)( "discard_basic_block_info: %p, %llu\n", a, (ULong)size); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1085 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1086 | // Get BB info, remove from table, free BB info. Simple! |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 1087 | bbInfo = VG_(HT_remove)(instr_info_table, a); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1088 | tl_assert(NULL != bbInfo); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1089 | VG_(free)(bbInfo); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1093 | /*--- Command line processing ---*/ |
| 1094 | /*--------------------------------------------------------------------*/ |
| 1095 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1096 | static void parse_cache_opt ( cache_t* cache, char* opt ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1097 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1098 | int i = 0, i2, i3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1099 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1100 | // Option argument looks like "65536,2,64". |
| 1101 | // Find commas, replace with NULs to make three independent |
| 1102 | // strings, then extract numbers, put NULs back. Yuck. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1103 | while (VG_(isdigit)(opt[i])) i++; |
| 1104 | if (',' == opt[i]) { |
| 1105 | opt[i++] = '\0'; |
| 1106 | i2 = i; |
| 1107 | } else goto bad; |
| 1108 | while (VG_(isdigit)(opt[i])) i++; |
| 1109 | if (',' == opt[i]) { |
| 1110 | opt[i++] = '\0'; |
| 1111 | i3 = i; |
| 1112 | } else goto bad; |
| 1113 | while (VG_(isdigit)(opt[i])) i++; |
| 1114 | if ('\0' != opt[i]) goto bad; |
| 1115 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1116 | cache->size = (Int)VG_(atoll)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1117 | cache->assoc = (Int)VG_(atoll)(opt + i2); |
| 1118 | cache->line_size = (Int)VG_(atoll)(opt + i3); |
| 1119 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1120 | opt[i2-1] = ','; |
| 1121 | opt[i3-1] = ','; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1122 | return; |
| 1123 | |
| 1124 | bad: |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1125 | VG_(bad_option)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1128 | static Bool cg_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1129 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1130 | // 5 is length of "--I1=" |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1131 | if (VG_CLO_STREQN(5, arg, "--I1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1132 | parse_cache_opt(&clo_I1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1133 | else if (VG_CLO_STREQN(5, arg, "--D1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1134 | parse_cache_opt(&clo_D1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1135 | else if (VG_CLO_STREQN(5, arg, "--L2=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1136 | parse_cache_opt(&clo_L2_cache, &arg[5]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1137 | else |
| 1138 | return False; |
| 1139 | |
| 1140 | return True; |
| 1141 | } |
| 1142 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1143 | static void cg_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1144 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1145 | VG_(printf)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1146 | " --I1=<size>,<assoc>,<line_size> set I1 cache manually\n" |
| 1147 | " --D1=<size>,<assoc>,<line_size> set D1 cache manually\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1148 | " --L2=<size>,<assoc>,<line_size> set L2 cache manually\n" |
| 1149 | ); |
| 1150 | } |
| 1151 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1152 | static void cg_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1153 | { |
| 1154 | VG_(printf)( |
| 1155 | " (none)\n" |
| 1156 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | /*--------------------------------------------------------------------*/ |
| 1160 | /*--- Setup ---*/ |
| 1161 | /*--------------------------------------------------------------------*/ |
| 1162 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1163 | static void cg_post_clo_init(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1164 | { |
| 1165 | cache_t I1c, D1c, L2c; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1166 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1167 | configure_caches(&I1c, &D1c, &L2c); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1168 | |
| 1169 | cachesim_I1_initcache(I1c); |
| 1170 | cachesim_D1_initcache(D1c); |
| 1171 | cachesim_L2_initcache(L2c); |
| 1172 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 1173 | VG_(register_profile_event)(VgpGetLineCC, "get-lineCC"); |
| 1174 | VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate"); |
| 1175 | VG_(register_profile_event)(VgpCacheResults, "cache-results"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1178 | static Char base_dir[VKI_PATH_MAX]; |
| 1179 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1180 | static void cg_pre_clo_init(void) |
| 1181 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1182 | VG_(details_name) ("Cachegrind"); |
| 1183 | VG_(details_version) (NULL); |
| 1184 | VG_(details_description) ("an I1/D1/L2 cache profiler"); |
| 1185 | VG_(details_copyright_author)( |
| 1186 | "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al."); |
| 1187 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 1188 | VG_(details_avg_translation_sizeB) ( 155 ); |
| 1189 | |
| 1190 | VG_(basic_tool_funcs) (cg_post_clo_init, |
| 1191 | cg_instrument, |
| 1192 | cg_fini); |
| 1193 | |
| 1194 | VG_(needs_basic_block_discards)(cg_discard_basic_block_info); |
| 1195 | VG_(needs_command_line_options)(cg_process_cmd_line_option, |
| 1196 | cg_print_usage, |
| 1197 | cg_print_debug_usage); |
| 1198 | |
| 1199 | /* Get working directory */ |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1200 | tl_assert( VG_(getcwd)(base_dir, VKI_PATH_MAX) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1201 | |
| 1202 | /* Block is big enough for dir name + cachegrind.out.<pid> */ |
| 1203 | cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char)); |
| 1204 | VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d", |
| 1205 | base_dir, VG_(getpid)()); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1206 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1207 | instr_info_table = VG_(HT_construct)( 4999 ); // prime, biggish |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1211 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1212 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1213 | /*--- end ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1214 | /*--------------------------------------------------------------------*/ |