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 | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 579 | |
| 580 | // Nb: instrLen will be zero if Vex failed to decode it. |
| 581 | tl_assert( 0 == instrLen || |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 582 | (instrLen >= VG_MIN_INSTR_SZB && |
| 583 | instrLen <= VG_MAX_INSTR_SZB) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 584 | |
njn | 016712a | 2005-04-04 02:52:16 +0000 | [diff] [blame] | 585 | // Large (eg. 28B, 108B, 512B on x86) data-sized instructions will be |
| 586 | // done inaccurately, but they're very rare and this avoids errors from |
| 587 | // hitting more than two cache lines in the simulation. |
| 588 | if (dataSize > MIN_LINE_SIZE) dataSize = MIN_LINE_SIZE; |
| 589 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 590 | // Setup 1st arg: instr_info node's address |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 591 | // Believed to be 64-bit clean |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 592 | do_details(i_node, bbSeenBefore, instrAddr, instrLen, dataSize ); |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 593 | arg1 = mkIRExpr_HWord( (HWord)i_node ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 594 | |
| 595 | if (!loadAddrExpr && !storeAddrExpr) { |
| 596 | // no load/store |
| 597 | tl_assert(0 == dataSize); |
| 598 | helperName = "log_1I_0D_cache_access"; |
| 599 | helperAddr = &log_1I_0D_cache_access; |
| 600 | argc = 1; |
| 601 | argv = mkIRExprVec_1(arg1); |
| 602 | |
| 603 | } else if (loadAddrExpr && !storeAddrExpr) { |
| 604 | // load |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 605 | tl_assert( isIRAtom(loadAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 606 | helperName = "log_1I_1Dr_cache_access"; |
| 607 | helperAddr = &log_1I_1Dr_cache_access; |
| 608 | argc = 2; |
| 609 | arg2 = loadAddrExpr; |
| 610 | argv = mkIRExprVec_2(arg1, arg2); |
| 611 | |
| 612 | } else if (!loadAddrExpr && storeAddrExpr) { |
| 613 | // store |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 614 | tl_assert( isIRAtom(storeAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 615 | helperName = "log_1I_1Dw_cache_access"; |
| 616 | helperAddr = &log_1I_1Dw_cache_access; |
| 617 | argc = 2; |
| 618 | arg2 = storeAddrExpr; |
| 619 | argv = mkIRExprVec_2(arg1, arg2); |
| 620 | |
| 621 | } else { |
| 622 | tl_assert( loadAddrExpr && storeAddrExpr ); |
sewardj | 710d6c2 | 2005-03-20 18:55:15 +0000 | [diff] [blame] | 623 | tl_assert( isIRAtom(loadAddrExpr) ); |
| 624 | tl_assert( isIRAtom(storeAddrExpr) ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 625 | |
| 626 | if ( loadStoreAddrsMatch(loadAddrExpr, storeAddrExpr) ) { |
| 627 | // modify |
| 628 | helperName = "log_1I_1Dr_cache_access"; |
| 629 | helperAddr = &log_1I_1Dr_cache_access; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 630 | argc = 2; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 631 | arg2 = loadAddrExpr; |
| 632 | argv = mkIRExprVec_2(arg1, arg2); |
| 633 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 634 | } else { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 635 | // load/store |
| 636 | helperName = "log_1I_2D_cache_access"; |
| 637 | helperAddr = &log_1I_2D_cache_access; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 638 | argc = 3; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 639 | arg2 = loadAddrExpr; |
| 640 | arg3 = storeAddrExpr; |
| 641 | argv = mkIRExprVec_3(arg1, arg2, arg3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 645 | // Add call to the instrumentation function |
| 646 | di = unsafeIRDirty_0_N( argc, helperName, helperAddr, argv); |
| 647 | addStmtToIRBB( bbOut, IRStmt_Dirty(di) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 648 | } |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 649 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 650 | static IRBB* cg_instrument ( IRBB* bbIn, VexGuestLayout* layout, |
| 651 | IRType gWordTy, IRType hWordTy ) |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 652 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 653 | Int i, dataSize = 0, bbInfo_i; |
| 654 | IRBB* bbOut; |
| 655 | IRStmt* st; |
| 656 | BB_info* bbInfo; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 657 | Bool bbSeenBefore = False, addedInstrumentation, addInstNow; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 658 | Addr instrAddr, origAddr; |
| 659 | UInt instrLen; |
| 660 | IRExpr *loadAddrExpr, *storeAddrExpr; |
| 661 | |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 662 | if (gWordTy != hWordTy) { |
| 663 | /* We don't currently support this case. */ |
| 664 | VG_(tool_panic)("host/guest word size mismatch"); |
| 665 | } |
| 666 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 667 | /* Set up BB */ |
| 668 | bbOut = emptyIRBB(); |
| 669 | bbOut->tyenv = dopyIRTypeEnv(bbIn->tyenv); |
| 670 | bbOut->next = dopyIRExpr(bbIn->next); |
| 671 | bbOut->jumpkind = bbIn->jumpkind; |
| 672 | |
| 673 | // Get the first statement, and origAddr from it |
| 674 | i = 0; |
| 675 | tl_assert(bbIn->stmts_used > 0); |
| 676 | st = bbIn->stmts[0]; |
| 677 | tl_assert(Ist_IMark == st->tag); |
| 678 | origAddr = (Addr)st->Ist.IMark.addr; |
| 679 | tl_assert(origAddr == st->Ist.IMark.addr); // XXX: check no overflow |
| 680 | |
| 681 | // Get block info |
| 682 | bbInfo = get_BB_info(bbIn, origAddr, &bbSeenBefore); |
| 683 | bbInfo_i = 0; |
| 684 | |
| 685 | do { |
| 686 | // We should be at an IMark statement |
| 687 | tl_assert(Ist_IMark == st->tag); |
| 688 | |
| 689 | // Reset stuff for this original instruction |
| 690 | loadAddrExpr = storeAddrExpr = NULL; |
| 691 | dataSize = 0; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 692 | addedInstrumentation = False; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 693 | |
| 694 | // Process all the statements for this original instruction (ie. until |
| 695 | // the next IMark statement, or the end of the block) |
| 696 | do { |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 697 | IRStmt* st2 = ( i+1 < bbIn->stmts_used ? bbIn->stmts[i+1] : NULL ); |
| 698 | |
| 699 | addInstNow = handleOneStatement(bbIn->tyenv, bbOut, st, st2, |
| 700 | &instrAddr, &instrLen, &loadAddrExpr, |
| 701 | &storeAddrExpr, &dataSize); |
| 702 | if (addInstNow) { |
| 703 | tl_assert(!addedInstrumentation); |
| 704 | addedInstrumentation = True; |
| 705 | |
| 706 | // Add instrumentation before this statement. |
| 707 | instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore, |
| 708 | instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr); |
| 709 | } |
| 710 | |
| 711 | addStmtToIRBB( bbOut, st ); |
| 712 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 713 | i++; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 714 | st = st2; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 715 | } |
| 716 | while (st && Ist_IMark != st->tag); |
| 717 | |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 718 | if (!addedInstrumentation) { |
| 719 | // Add instrumentation now, after all the instruction's statements. |
| 720 | instrumentInstr(bbOut, &bbInfo->instrs[ bbInfo_i ], bbSeenBefore, |
| 721 | instrAddr, instrLen, dataSize, loadAddrExpr, storeAddrExpr); |
| 722 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 723 | |
| 724 | bbInfo_i++; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 725 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 726 | while (st); |
| 727 | |
| 728 | return bbOut; |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 729 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 730 | |
| 731 | /*------------------------------------------------------------*/ |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 732 | /*--- Cache configuration ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 733 | /*------------------------------------------------------------*/ |
| 734 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 735 | #define UNDEFINED_CACHE { -1, -1, -1 } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 736 | |
| 737 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 738 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
| 739 | static cache_t clo_L2_cache = UNDEFINED_CACHE; |
| 740 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 741 | /* Checks cache config is ok; makes it so if not. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 742 | static |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 743 | void check_cache(cache_t* cache, Char *name) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 744 | { |
| 745 | /* First check they're all powers of two */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 746 | if (-1 == VG_(log2)(cache->size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 747 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 748 | "error: %s size of %dB not a power of two; aborting.", |
| 749 | name, cache->size); |
| 750 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 751 | } |
| 752 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 753 | if (-1 == VG_(log2)(cache->assoc)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 754 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 755 | "error: %s associativity of %d not a power of two; aborting.", |
| 756 | name, cache->assoc); |
| 757 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 758 | } |
| 759 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 760 | if (-1 == VG_(log2)(cache->line_size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 761 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 762 | "error: %s line size of %dB not a power of two; aborting.", |
| 763 | name, cache->line_size); |
| 764 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 765 | } |
| 766 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 767 | // Then check line size >= 16 -- any smaller and a single instruction could |
| 768 | // straddle three cache lines, which breaks a simulation assertion and is |
| 769 | // stupid anyway. |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 770 | if (cache->line_size < MIN_LINE_SIZE) { |
| 771 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 772 | "error: %s line size of %dB too small; aborting.", |
| 773 | name, cache->line_size); |
| 774 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* Then check cache size > line size (causes seg faults if not). */ |
| 778 | if (cache->size <= cache->line_size) { |
| 779 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 780 | "error: %s cache size of %dB <= line size of %dB; aborting.", |
| 781 | name, cache->size, cache->line_size); |
| 782 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* Then check assoc <= (size / line size) (seg faults otherwise). */ |
| 786 | if (cache->assoc > (cache->size / cache->line_size)) { |
| 787 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 788 | "warning: %s associativity > (size / line size); aborting.", name); |
| 789 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 793 | static |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 794 | void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 795 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 796 | #define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size) |
| 797 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 798 | Int n_clos = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 799 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 800 | // Count how many were defined on the command line. |
| 801 | if (DEFINED(clo_I1_cache)) { n_clos++; } |
| 802 | if (DEFINED(clo_D1_cache)) { n_clos++; } |
| 803 | if (DEFINED(clo_L2_cache)) { n_clos++; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 804 | |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 805 | // Set the cache config (using auto-detection, if supported by the |
| 806 | // architecture) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 807 | VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) ); |
sewardj | b1a77a4 | 2002-07-13 13:31:20 +0000 | [diff] [blame] | 808 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 809 | // Then replace with any defined on the command line. |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 810 | if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; } |
| 811 | if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; } |
| 812 | if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 813 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 814 | // Then check values and fix if not acceptable. |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 815 | check_cache(I1c, "I1"); |
| 816 | check_cache(D1c, "D1"); |
| 817 | check_cache(L2c, "L2"); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 818 | |
| 819 | if (VG_(clo_verbosity) > 1) { |
| 820 | VG_(message)(Vg_UserMsg, "Cache configuration used:"); |
| 821 | VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines", |
| 822 | I1c->size, I1c->assoc, I1c->line_size); |
| 823 | VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines", |
| 824 | D1c->size, D1c->assoc, D1c->line_size); |
| 825 | VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines", |
| 826 | L2c->size, L2c->assoc, L2c->line_size); |
| 827 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 828 | #undef CMD_LINE_DEFINED |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 829 | } |
| 830 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 831 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 832 | /*--- cg_fini() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 833 | /*------------------------------------------------------------*/ |
| 834 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 835 | // Total reads/writes/misses. Calculated during CC traversal at the end. |
| 836 | // All auto-zeroed. |
| 837 | static CC Ir_total; |
| 838 | static CC Dr_total; |
| 839 | static CC Dw_total; |
| 840 | |
| 841 | static Char* cachegrind_out_file; |
| 842 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 843 | static void fprint_lineCC(Int fd, lineCC* n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 844 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 845 | Char buf[512]; |
| 846 | VG_(sprintf)(buf, "%u %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 847 | n->line, |
| 848 | n->Ir.a, n->Ir.m1, n->Ir.m2, |
| 849 | n->Dr.a, n->Dr.m1, n->Dr.m2, |
| 850 | n->Dw.a, n->Dw.m1, n->Dw.m2); |
| 851 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 852 | |
| 853 | Ir_total.a += n->Ir.a; Ir_total.m1 += n->Ir.m1; Ir_total.m2 += n->Ir.m2; |
| 854 | Dr_total.a += n->Dr.a; Dr_total.m1 += n->Dr.m1; Dr_total.m2 += n->Dr.m2; |
| 855 | Dw_total.a += n->Dw.a; Dw_total.m1 += n->Dw.m1; Dw_total.m2 += n->Dw.m2; |
| 856 | } |
| 857 | |
| 858 | static void fprint_CC_table_and_calc_totals(void) |
| 859 | { |
| 860 | Int fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 861 | SysRes sres; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 862 | Char buf[512]; |
| 863 | fileCC *curr_fileCC; |
| 864 | fnCC *curr_fnCC; |
| 865 | lineCC *curr_lineCC; |
| 866 | Int i, j, k; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 867 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 868 | VGP_PUSHCC(VgpCacheResults); |
njn | 13f0293 | 2003-04-30 20:23:58 +0000 | [diff] [blame] | 869 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 870 | sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 871 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 872 | if (sres.isError) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 873 | // If the file can't be opened for whatever reason (conflict |
| 874 | // between multiple cachegrinded processes?), give up now. |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 875 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 876 | "error: can't open cache simulation output file '%s'", |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 877 | cachegrind_out_file ); |
| 878 | VG_(message)(Vg_UserMsg, |
| 879 | " ... so simulation results will be missing."); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 880 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 881 | } else { |
| 882 | fd = sres.val; |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 883 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 884 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 885 | // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after |
| 886 | // the 2nd colon makes cg_annotate's output look nicer. |
| 887 | VG_(sprintf)(buf, "desc: I1 cache: %s\n" |
| 888 | "desc: D1 cache: %s\n" |
| 889 | "desc: L2 cache: %s\n", |
| 890 | I1.desc_line, D1.desc_line, L2.desc_line); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 891 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 892 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 893 | // "cmd:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 894 | VG_(strcpy)(buf, "cmd:"); |
| 895 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 896 | for (i = 0; i < VG_(client_argc); i++) { |
sewardj | 4bf3aba | 2005-05-13 23:32:43 +0000 | [diff] [blame] | 897 | if (VG_(client_argv)[i] == NULL) |
| 898 | continue; |
thughes | 6f7eb9c | 2004-10-06 13:50:12 +0000 | [diff] [blame] | 899 | VG_(write)(fd, " ", 1); |
thughes | 30c43d8 | 2004-10-06 13:49:36 +0000 | [diff] [blame] | 900 | VG_(write)(fd, VG_(client_argv)[i], VG_(strlen)(VG_(client_argv)[i])); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 901 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 902 | // "events:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 903 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n"); |
| 904 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 905 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 906 | // Six loops here: three for the hash table arrays, and three for the |
| 907 | // chains hanging off the hash table arrays. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 908 | for (i = 0; i < N_FILE_ENTRIES; i++) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 909 | curr_fileCC = CC_table[i]; |
| 910 | while (curr_fileCC != NULL) { |
| 911 | VG_(sprintf)(buf, "fl=%s\n", curr_fileCC->file); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 912 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 913 | |
| 914 | for (j = 0; j < N_FN_ENTRIES; j++) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 915 | curr_fnCC = curr_fileCC->fns[j]; |
| 916 | while (curr_fnCC != NULL) { |
| 917 | VG_(sprintf)(buf, "fn=%s\n", curr_fnCC->fn); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 918 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 919 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 920 | for (k = 0; k < N_LINE_ENTRIES; k++) { |
| 921 | curr_lineCC = curr_fnCC->lines[k]; |
| 922 | while (curr_lineCC != NULL) { |
| 923 | fprint_lineCC(fd, curr_lineCC); |
| 924 | curr_lineCC = curr_lineCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 927 | curr_fnCC = curr_fnCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 930 | curr_fileCC = curr_fileCC->next; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 931 | } |
| 932 | } |
| 933 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 934 | // Summary stats must come after rest of table, since we calculate them |
| 935 | // during traversal. */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 936 | VG_(sprintf)(buf, "summary: " |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 937 | "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 938 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 939 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 940 | Dw_total.a, Dw_total.m1, Dw_total.m2); |
| 941 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 942 | VG_(close)(fd); |
| 943 | } |
| 944 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 945 | static UInt ULong_width(ULong n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 946 | { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 947 | UInt w = 0; |
| 948 | while (n > 0) { |
| 949 | n = n / 10; |
| 950 | w++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 951 | } |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 952 | return w + (w-1)/3; // add space for commas |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 953 | } |
| 954 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 955 | static void cg_fini(Int exitcode) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 956 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 957 | static char buf1[128], buf2[128], buf3[128], fmt [128]; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 958 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 959 | CC D_total; |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 960 | ULong L2_total_m, L2_total_mr, L2_total_mw, |
| 961 | L2_total, L2_total_r, L2_total_w; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 962 | Int l1, l2, l3; |
| 963 | Int p; |
| 964 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 965 | fprint_CC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 966 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 967 | if (VG_(clo_verbosity) == 0) |
| 968 | return; |
| 969 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 970 | /* I cache results. Use the I_refs value to determine the first column |
| 971 | * width. */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 972 | l1 = ULong_width(Ir_total.a); |
| 973 | l2 = ULong_width(Dr_total.a); |
| 974 | l3 = ULong_width(Dw_total.a); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 975 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 976 | /* Make format string, getting width right for numbers */ |
| 977 | VG_(sprintf)(fmt, "%%s %%,%dld", l1); |
| 978 | |
| 979 | VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a); |
| 980 | VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1); |
| 981 | VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 982 | |
| 983 | p = 100; |
| 984 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 985 | if (0 == Ir_total.a) Ir_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 986 | VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 987 | VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); |
| 988 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 989 | VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 990 | VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); |
| 991 | VG_(message)(Vg_UserMsg, ""); |
| 992 | |
| 993 | /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the |
| 994 | * width of columns 2 & 3. */ |
| 995 | D_total.a = Dr_total.a + Dw_total.a; |
| 996 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
| 997 | D_total.m2 = Dr_total.m2 + Dw_total.m2; |
| 998 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 999 | /* Make format string, getting width right for numbers */ |
| 1000 | VG_(sprintf)(fmt, "%%s %%,%dld (%%,%dld rd + %%,%dld wr)", l1, l2, l3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1001 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1002 | VG_(message)(Vg_UserMsg, fmt, "D refs: ", |
| 1003 | D_total.a, Dr_total.a, Dw_total.a); |
| 1004 | VG_(message)(Vg_UserMsg, fmt, "D1 misses: ", |
| 1005 | D_total.m1, Dr_total.m1, Dw_total.m1); |
| 1006 | VG_(message)(Vg_UserMsg, fmt, "L2d misses: ", |
| 1007 | D_total.m2, Dr_total.m2, Dw_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1008 | |
| 1009 | p = 10; |
| 1010 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1011 | if (0 == D_total.a) D_total.a = 1; |
| 1012 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1013 | if (0 == Dw_total.a) Dw_total.a = 1; |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1014 | VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); |
| 1015 | VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); |
| 1016 | VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1017 | VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1018 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1019 | VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1); |
| 1020 | VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2); |
| 1021 | VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1022 | VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1023 | VG_(message)(Vg_UserMsg, ""); |
| 1024 | |
| 1025 | /* L2 overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1026 | |
| 1027 | L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1028 | L2_total_r = Dr_total.m1 + Ir_total.m1; |
| 1029 | L2_total_w = Dw_total.m1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1030 | VG_(message)(Vg_UserMsg, fmt, "L2 refs: ", |
| 1031 | L2_total, L2_total_r, L2_total_w); |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1032 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1033 | L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2; |
| 1034 | L2_total_mr = Dr_total.m2 + Ir_total.m2; |
| 1035 | L2_total_mw = Dw_total.m2; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1036 | VG_(message)(Vg_UserMsg, fmt, "L2 misses: ", |
| 1037 | L2_total_m, L2_total_mr, L2_total_mw); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1038 | |
njn | 856c54e | 2005-06-26 18:43:40 +0000 | [diff] [blame] | 1039 | VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); |
| 1040 | VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); |
| 1041 | VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1042 | VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1043 | |
| 1044 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1045 | // Various stats |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1046 | if (VG_(clo_verbosity) > 1) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1047 | int BB_lookups = full_debug_BBs + fn_debug_BBs + |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1048 | file_line_debug_BBs + no_debug_BBs; |
| 1049 | |
| 1050 | VG_(message)(Vg_DebugMsg, ""); |
| 1051 | VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files); |
| 1052 | VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1053 | VG_(message)(Vg_DebugMsg, "Distinct lines: %d", distinct_lines); |
| 1054 | VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1055 | VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups); |
| 1056 | VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)", |
| 1057 | full_debug_BBs * 100 / BB_lookups, |
| 1058 | full_debug_BBs); |
| 1059 | VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)", |
| 1060 | file_line_debug_BBs * 100 / BB_lookups, |
| 1061 | file_line_debug_BBs); |
| 1062 | VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)", |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1063 | fn_debug_BBs * 100 / BB_lookups, |
| 1064 | fn_debug_BBs); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1065 | VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)", |
| 1066 | no_debug_BBs * 100 / BB_lookups, |
| 1067 | no_debug_BBs); |
| 1068 | VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1069 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1070 | VGP_POPCC(VgpCacheResults); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1073 | /*--------------------------------------------------------------------*/ |
| 1074 | /*--- Discarding BB info ---*/ |
| 1075 | /*--------------------------------------------------------------------*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1076 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1077 | // Called when a translation is invalidated due to code unloading. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1078 | static void cg_discard_basic_block_info ( Addr a, SizeT size ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1079 | { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1080 | VgHashNode* bbInfo; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1081 | |
nethercote | 928a5f7 | 2004-11-03 18:10:37 +0000 | [diff] [blame] | 1082 | 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] | 1083 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1084 | // Get BB info, remove from table, free BB info. Simple! |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame^] | 1085 | bbInfo = VG_(HT_remove)(instr_info_table, a); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1086 | tl_assert(NULL != bbInfo); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1087 | VG_(free)(bbInfo); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1091 | /*--- Command line processing ---*/ |
| 1092 | /*--------------------------------------------------------------------*/ |
| 1093 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1094 | static void parse_cache_opt ( cache_t* cache, char* opt ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1095 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1096 | int i = 0, i2, i3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1097 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1098 | // Option argument looks like "65536,2,64". |
| 1099 | // Find commas, replace with NULs to make three independent |
| 1100 | // strings, then extract numbers, put NULs back. Yuck. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1101 | while (VG_(isdigit)(opt[i])) i++; |
| 1102 | if (',' == opt[i]) { |
| 1103 | opt[i++] = '\0'; |
| 1104 | i2 = i; |
| 1105 | } else goto bad; |
| 1106 | while (VG_(isdigit)(opt[i])) i++; |
| 1107 | if (',' == opt[i]) { |
| 1108 | opt[i++] = '\0'; |
| 1109 | i3 = i; |
| 1110 | } else goto bad; |
| 1111 | while (VG_(isdigit)(opt[i])) i++; |
| 1112 | if ('\0' != opt[i]) goto bad; |
| 1113 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1114 | cache->size = (Int)VG_(atoll)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1115 | cache->assoc = (Int)VG_(atoll)(opt + i2); |
| 1116 | cache->line_size = (Int)VG_(atoll)(opt + i3); |
| 1117 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1118 | opt[i2-1] = ','; |
| 1119 | opt[i3-1] = ','; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1120 | return; |
| 1121 | |
| 1122 | bad: |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1123 | VG_(bad_option)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1126 | static Bool cg_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1127 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1128 | // 5 is length of "--I1=" |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1129 | if (VG_CLO_STREQN(5, arg, "--I1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1130 | parse_cache_opt(&clo_I1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1131 | else if (VG_CLO_STREQN(5, arg, "--D1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1132 | parse_cache_opt(&clo_D1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1133 | else if (VG_CLO_STREQN(5, arg, "--L2=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1134 | parse_cache_opt(&clo_L2_cache, &arg[5]); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1135 | else |
| 1136 | return False; |
| 1137 | |
| 1138 | return True; |
| 1139 | } |
| 1140 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1141 | static void cg_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1142 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1143 | VG_(printf)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1144 | " --I1=<size>,<assoc>,<line_size> set I1 cache manually\n" |
| 1145 | " --D1=<size>,<assoc>,<line_size> set D1 cache manually\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1146 | " --L2=<size>,<assoc>,<line_size> set L2 cache manually\n" |
| 1147 | ); |
| 1148 | } |
| 1149 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1150 | static void cg_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1151 | { |
| 1152 | VG_(printf)( |
| 1153 | " (none)\n" |
| 1154 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | /*--------------------------------------------------------------------*/ |
| 1158 | /*--- Setup ---*/ |
| 1159 | /*--------------------------------------------------------------------*/ |
| 1160 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1161 | static void cg_post_clo_init(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1162 | { |
| 1163 | cache_t I1c, D1c, L2c; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1164 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1165 | configure_caches(&I1c, &D1c, &L2c); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1166 | |
| 1167 | cachesim_I1_initcache(I1c); |
| 1168 | cachesim_D1_initcache(D1c); |
| 1169 | cachesim_L2_initcache(L2c); |
| 1170 | |
njn | 31066fd | 2005-03-26 00:42:02 +0000 | [diff] [blame] | 1171 | VG_(register_profile_event)(VgpGetLineCC, "get-lineCC"); |
| 1172 | VG_(register_profile_event)(VgpCacheSimulate, "cache-simulate"); |
| 1173 | VG_(register_profile_event)(VgpCacheResults, "cache-results"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1176 | static Char base_dir[VKI_PATH_MAX]; |
| 1177 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1178 | static void cg_pre_clo_init(void) |
| 1179 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1180 | VG_(details_name) ("Cachegrind"); |
| 1181 | VG_(details_version) (NULL); |
| 1182 | VG_(details_description) ("an I1/D1/L2 cache profiler"); |
| 1183 | VG_(details_copyright_author)( |
| 1184 | "Copyright (C) 2002-2005, and GNU GPL'd, by Nicholas Nethercote et al."); |
| 1185 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 1186 | VG_(details_avg_translation_sizeB) ( 155 ); |
| 1187 | |
| 1188 | VG_(basic_tool_funcs) (cg_post_clo_init, |
| 1189 | cg_instrument, |
| 1190 | cg_fini); |
| 1191 | |
| 1192 | VG_(needs_basic_block_discards)(cg_discard_basic_block_info); |
| 1193 | VG_(needs_command_line_options)(cg_process_cmd_line_option, |
| 1194 | cg_print_usage, |
| 1195 | cg_print_debug_usage); |
| 1196 | |
| 1197 | /* Get working directory */ |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1198 | tl_assert( VG_(getcwd)(base_dir, VKI_PATH_MAX) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1199 | |
| 1200 | /* Block is big enough for dir name + cachegrind.out.<pid> */ |
| 1201 | cachegrind_out_file = VG_(malloc)((VG_(strlen)(base_dir) + 32)*sizeof(Char)); |
| 1202 | VG_(sprintf)(cachegrind_out_file, "%s/cachegrind.out.%d", |
| 1203 | base_dir, VG_(getpid)()); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1204 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1205 | instr_info_table = VG_(HT_construct)( 4999 ); // prime, biggish |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init, 0) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1209 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1210 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1211 | /*--- end ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1212 | /*--------------------------------------------------------------------*/ |