sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3 | /*--- The cache simulation skin: cache detection; instrumentation, ---*/ |
| 4 | /*--- recording and results printing. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 5 | /*--- cg_main.c ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 6 | /*--------------------------------------------------------------------*/ |
| 7 | |
| 8 | /* |
| 9 | This file is part of Valgrind, an x86 protected-mode emulator |
| 10 | designed for debugging and profiling binaries on x86-Unixes. |
| 11 | |
sewardj | 3c23d43 | 2002-06-01 23:43:49 +0000 | [diff] [blame] | 12 | Copyright (C) 2002 Nicholas Nethercote |
| 13 | njn25@cam.ac.uk |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 30 | The GNU General Public License is contained in the file COPYING. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 33 | #include "vg_skin.h" |
| 34 | //#include "vg_profile.c" |
| 35 | |
| 36 | /* For cache simulation */ |
| 37 | typedef struct { |
| 38 | int size; /* bytes */ |
| 39 | int assoc; |
| 40 | int line_size; /* bytes */ |
| 41 | } cache_t; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 42 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 43 | #include "cg_sim_L2.c" |
| 44 | #include "cg_sim_I1.c" |
| 45 | #include "cg_sim_D1.c" |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 46 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 47 | /*------------------------------------------------------------*/ |
| 48 | /*--- Constants ---*/ |
| 49 | /*------------------------------------------------------------*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 50 | |
| 51 | /* According to IA-32 Intel Architecture Software Developer's Manual: Vol 2 */ |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 52 | #define MAX_x86_INSTR_SIZE 16 |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 53 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 54 | #define MIN_LINE_SIZE 16 |
| 55 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 56 | /* Size of various buffers used for storing strings */ |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 57 | #define FILENAME_LEN 256 |
| 58 | #define FN_NAME_LEN 256 |
| 59 | #define BUF_LEN 512 |
| 60 | #define COMMIFY_BUF_LEN 128 |
| 61 | #define RESULTS_BUF_LEN 128 |
| 62 | #define LINE_BUF_LEN 64 |
| 63 | |
| 64 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 65 | /*--- Profiling events ---*/ |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 66 | /*------------------------------------------------------------*/ |
| 67 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 68 | typedef |
| 69 | enum { |
| 70 | VgpGetBBCC = VgpFini+1, |
| 71 | VgpCacheSimulate, |
| 72 | VgpCacheResults |
| 73 | } |
| 74 | VgpSkinCC; |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 75 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 76 | /*------------------------------------------------------------*/ |
| 77 | /*--- Output file related stuff ---*/ |
| 78 | /*------------------------------------------------------------*/ |
| 79 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 80 | Char cachegrind_out_file[FILENAME_LEN]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 81 | |
| 82 | static void file_err() |
| 83 | { |
| 84 | VG_(message)(Vg_UserMsg, |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 85 | "error: can't open cache simulation output file `%s'", |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 86 | cachegrind_out_file ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 87 | VG_(exit)(1); |
| 88 | } |
| 89 | |
| 90 | /*------------------------------------------------------------*/ |
| 91 | /*--- Cost center types, operations ---*/ |
| 92 | /*------------------------------------------------------------*/ |
| 93 | |
| 94 | typedef struct _CC CC; |
| 95 | struct _CC { |
| 96 | ULong a; |
| 97 | ULong m1; |
| 98 | ULong m2; |
| 99 | }; |
| 100 | |
| 101 | static __inline__ void initCC(CC* cc) { |
| 102 | cc->a = 0; |
| 103 | cc->m1 = 0; |
| 104 | cc->m2 = 0; |
| 105 | } |
| 106 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 107 | typedef |
| 108 | enum { |
| 109 | InstrCC, /* eg. mov %eax, %ebx */ |
| 110 | ReadCC, /* eg. mov (%ecx), %esi */ |
| 111 | WriteCC, /* eg. mov %eax, (%edx) */ |
| 112 | ModCC, /* eg. incl (%eax) (read+write one addr) */ |
| 113 | ReadWriteCC, /* eg. call*l (%esi), pushl 0x4(%ebx), movsw |
| 114 | (read+write two different addrs) */ |
| 115 | } CC_type; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 116 | |
| 117 | /* Instruction-level cost-centres. The typedefs for these structs are in |
| 118 | * vg_include.c |
| 119 | * |
| 120 | * WARNING: the 'tag' field *must* be the first byte of both CC types. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 121 | * |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 122 | * This is because we use it to work out what kind of CC we're dealing with. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 123 | */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 124 | typedef |
| 125 | struct { |
| 126 | /* word 1 */ |
| 127 | UChar tag; |
| 128 | UChar instr_size; |
| 129 | /* 2 bytes padding */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 130 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 131 | /* words 2+ */ |
| 132 | Addr instr_addr; |
| 133 | CC I; |
| 134 | } |
| 135 | iCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 136 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 137 | typedef |
| 138 | struct _idCC { |
| 139 | /* word 1 */ |
| 140 | UChar tag; |
| 141 | UChar instr_size; |
| 142 | UChar data_size; |
| 143 | /* 1 byte padding */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 144 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 145 | /* words 2+ */ |
| 146 | Addr instr_addr; |
| 147 | CC I; |
| 148 | CC D; |
| 149 | } |
| 150 | idCC; |
| 151 | |
| 152 | typedef |
| 153 | struct _iddCC { |
| 154 | /* word 1 */ |
| 155 | UChar tag; |
| 156 | UChar instr_size; |
| 157 | UChar data_size; |
| 158 | /* 1 byte padding */ |
| 159 | |
| 160 | /* words 2+ */ |
| 161 | Addr instr_addr; |
| 162 | CC I; |
| 163 | CC Da; |
| 164 | CC Db; |
| 165 | } |
| 166 | iddCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 167 | |
| 168 | static void init_iCC(iCC* cc, Addr instr_addr, UInt instr_size) |
| 169 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 170 | cc->tag = InstrCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 171 | cc->instr_size = instr_size; |
| 172 | cc->instr_addr = instr_addr; |
| 173 | initCC(&cc->I); |
| 174 | } |
| 175 | |
| 176 | static void init_idCC(CC_type X_CC, idCC* cc, Addr instr_addr, |
| 177 | UInt instr_size, UInt data_size) |
| 178 | { |
| 179 | cc->tag = X_CC; |
| 180 | cc->instr_size = instr_size; |
| 181 | cc->data_size = data_size; |
| 182 | cc->instr_addr = instr_addr; |
| 183 | initCC(&cc->I); |
| 184 | initCC(&cc->D); |
| 185 | } |
| 186 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 187 | static void init_iddCC(iddCC* cc, Addr instr_addr, |
| 188 | UInt instr_size, UInt data_size) |
| 189 | { |
| 190 | cc->tag = ReadWriteCC; |
| 191 | cc->instr_size = instr_size; |
| 192 | cc->data_size = data_size; |
| 193 | cc->instr_addr = instr_addr; |
| 194 | initCC(&cc->I); |
| 195 | initCC(&cc->Da); |
| 196 | initCC(&cc->Db); |
| 197 | } |
| 198 | |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 199 | #define ADD_CC_TO(CC_type, cc, total) \ |
| 200 | total.a += ((CC_type*)BBCC_ptr)->cc.a; \ |
| 201 | total.m1 += ((CC_type*)BBCC_ptr)->cc.m1; \ |
| 202 | total.m2 += ((CC_type*)BBCC_ptr)->cc.m2; |
| 203 | |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 204 | /* If 1, address of each instruction is printed as a comment after its counts |
| 205 | * in cachegrind.out */ |
| 206 | #define PRINT_INSTR_ADDRS 0 |
| 207 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 208 | static __inline__ void sprint_iCC(Char buf[BUF_LEN], iCC* cc) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 209 | { |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 210 | #if PRINT_INSTR_ADDRS |
| 211 | VG_(sprintf)(buf, "%llu %llu %llu # %x\n", |
| 212 | cc->I.a, cc->I.m1, cc->I.m2, cc->instr_addr); |
| 213 | #else |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 214 | VG_(sprintf)(buf, "%llu %llu %llu\n", |
| 215 | cc->I.a, cc->I.m1, cc->I.m2); |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 216 | #endif |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 217 | } |
| 218 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 219 | static __inline__ void sprint_read_or_mod_CC(Char buf[BUF_LEN], idCC* cc) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 220 | { |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 221 | #if PRINT_INSTR_ADDRS |
| 222 | VG_(sprintf)(buf, "%llu %llu %llu %llu %llu %llu # %x\n", |
| 223 | cc->I.a, cc->I.m1, cc->I.m2, |
| 224 | cc->D.a, cc->D.m1, cc->D.m2, cc->instr_addr); |
| 225 | #else |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 226 | VG_(sprintf)(buf, "%llu %llu %llu %llu %llu %llu\n", |
| 227 | cc->I.a, cc->I.m1, cc->I.m2, |
| 228 | cc->D.a, cc->D.m1, cc->D.m2); |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 229 | #endif |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 230 | } |
| 231 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 232 | static __inline__ void sprint_write_CC(Char buf[BUF_LEN], idCC* cc) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 233 | { |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 234 | #if PRINT_INSTR_ADDRS |
| 235 | VG_(sprintf)(buf, "%llu %llu %llu . . . %llu %llu %llu # %x\n", |
| 236 | cc->I.a, cc->I.m1, cc->I.m2, |
| 237 | cc->D.a, cc->D.m1, cc->D.m2, cc->instr_addr); |
| 238 | #else |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 239 | VG_(sprintf)(buf, "%llu %llu %llu . . . %llu %llu %llu\n", |
| 240 | cc->I.a, cc->I.m1, cc->I.m2, |
| 241 | cc->D.a, cc->D.m1, cc->D.m2); |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 242 | #endif |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 243 | } |
| 244 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 245 | static __inline__ void sprint_read_write_CC(Char buf[BUF_LEN], iddCC* cc) |
| 246 | { |
| 247 | #if PRINT_INSTR_ADDRS |
| 248 | VG_(sprintf)(buf, "%llu %llu %llu %llu %llu %llu # %x\n", |
| 249 | cc->I.a, cc->I.m1, cc->I.m2, |
| 250 | cc->Da.a, cc->Da.m1, cc->Da.m2, |
| 251 | cc->Db.a, cc->Db.m1, cc->Db.m2, cc->instr_addr); |
| 252 | #else |
| 253 | VG_(sprintf)(buf, "%llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 254 | cc->I.a, cc->I.m1, cc->I.m2, |
| 255 | cc->Da.a, cc->Da.m1, cc->Da.m2, |
| 256 | cc->Db.a, cc->Db.m1, cc->Db.m2); |
| 257 | #endif |
| 258 | } |
| 259 | |
| 260 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 261 | /*------------------------------------------------------------*/ |
| 262 | /*--- BBCC hash table stuff ---*/ |
| 263 | /*------------------------------------------------------------*/ |
| 264 | |
| 265 | /* The table of BBCCs is of the form hash(filename, hash(fn_name, |
| 266 | * hash(BBCCs))). Each hash table is separately chained. The sizes below work |
| 267 | * fairly well for Konqueror. */ |
| 268 | |
| 269 | #define N_FILE_ENTRIES 251 |
| 270 | #define N_FN_ENTRIES 53 |
| 271 | #define N_BBCC_ENTRIES 37 |
| 272 | |
| 273 | /* The cost centres for a basic block are stored in a contiguous array. |
| 274 | * They are distinguishable by their tag field. */ |
| 275 | typedef struct _BBCC BBCC; |
| 276 | struct _BBCC { |
| 277 | Addr orig_addr; |
| 278 | UInt array_size; /* byte-size of variable length array */ |
| 279 | BBCC* next; |
| 280 | Addr array[0]; /* variable length array */ |
| 281 | }; |
| 282 | |
| 283 | typedef struct _fn_node fn_node; |
| 284 | struct _fn_node { |
| 285 | Char* fn_name; |
| 286 | BBCC* BBCCs[N_BBCC_ENTRIES]; |
| 287 | fn_node* next; |
| 288 | }; |
| 289 | |
| 290 | typedef struct _file_node file_node; |
| 291 | struct _file_node { |
| 292 | Char* filename; |
| 293 | fn_node* fns[N_FN_ENTRIES]; |
| 294 | file_node* next; |
| 295 | }; |
| 296 | |
| 297 | /* BBCC_table structure: list(filename, list(fn_name, list(BBCC))) */ |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 298 | static file_node *BBCC_table[N_FILE_ENTRIES]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 299 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 300 | static Int distinct_files = 0; |
| 301 | static Int distinct_fns = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 302 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 303 | static Int distinct_instrs = 0; |
| 304 | static Int full_debug_BBs = 0; |
| 305 | static Int file_line_debug_BBs = 0; |
| 306 | static Int fn_name_debug_BBs = 0; |
| 307 | static Int no_debug_BBs = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 308 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 309 | static Int BB_retranslations = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 310 | |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 311 | static CC Ir_discards; |
| 312 | static CC Dr_discards; |
| 313 | static CC Dw_discards; |
| 314 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 315 | static void init_BBCC_table() |
| 316 | { |
| 317 | Int i; |
| 318 | for (i = 0; i < N_FILE_ENTRIES; i++) |
| 319 | BBCC_table[i] = NULL; |
| 320 | } |
| 321 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 322 | static void get_debug_info(Addr instr_addr, Char filename[FILENAME_LEN], |
| 323 | Char fn_name[FN_NAME_LEN], Int* line_num) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 324 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 325 | Bool found1, found2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 326 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 327 | found1 = VG_(get_filename_linenum)(instr_addr, filename, |
| 328 | FILENAME_LEN, line_num); |
| 329 | found2 = VG_(get_fnname)(instr_addr, fn_name, FN_NAME_LEN); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 330 | |
| 331 | if (!found1 && !found2) { |
| 332 | no_debug_BBs++; |
| 333 | VG_(strcpy)(filename, "???"); |
| 334 | VG_(strcpy)(fn_name, "???"); |
njn | 9b3366a | 2002-06-10 15:31:16 +0000 | [diff] [blame] | 335 | *line_num = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 336 | |
| 337 | } else if ( found1 && found2) { |
| 338 | full_debug_BBs++; |
| 339 | |
| 340 | } else if ( found1 && !found2) { |
| 341 | file_line_debug_BBs++; |
| 342 | VG_(strcpy)(fn_name, "???"); |
| 343 | |
| 344 | } else /*(!found1 && found2)*/ { |
| 345 | fn_name_debug_BBs++; |
| 346 | VG_(strcpy)(filename, "???"); |
njn | 9b3366a | 2002-06-10 15:31:16 +0000 | [diff] [blame] | 347 | *line_num = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | /* Forward declaration. */ |
| 352 | static Int compute_BBCC_array_size(UCodeBlock* cb); |
| 353 | |
| 354 | static __inline__ |
| 355 | file_node* new_file_node(Char filename[FILENAME_LEN], file_node* next) |
| 356 | { |
| 357 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 358 | file_node* new = VG_(malloc)(sizeof(file_node)); |
| 359 | new->filename = VG_(strdup)(filename); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 360 | for (i = 0; i < N_FN_ENTRIES; i++) { |
| 361 | new->fns[i] = NULL; |
| 362 | } |
| 363 | new->next = next; |
| 364 | return new; |
| 365 | } |
| 366 | |
| 367 | static __inline__ |
| 368 | fn_node* new_fn_node(Char fn_name[FILENAME_LEN], fn_node* next) |
| 369 | { |
| 370 | Int i; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 371 | fn_node* new = VG_(malloc)(sizeof(fn_node)); |
| 372 | new->fn_name = VG_(strdup)(fn_name); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 373 | for (i = 0; i < N_BBCC_ENTRIES; i++) { |
| 374 | new->BBCCs[i] = NULL; |
| 375 | } |
| 376 | new->next = next; |
| 377 | return new; |
| 378 | } |
| 379 | |
| 380 | static __inline__ |
| 381 | BBCC* new_BBCC(Addr bb_orig_addr, UCodeBlock* cb, BBCC* next) |
| 382 | { |
| 383 | Int BBCC_array_size = compute_BBCC_array_size(cb); |
| 384 | BBCC* new; |
| 385 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 386 | new = (BBCC*)VG_(malloc)(sizeof(BBCC) + BBCC_array_size); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 387 | new->orig_addr = bb_orig_addr; |
| 388 | new->array_size = BBCC_array_size; |
| 389 | new->next = next; |
| 390 | |
| 391 | return new; |
| 392 | } |
| 393 | |
| 394 | #define HASH_CONSTANT 256 |
| 395 | |
| 396 | static UInt hash(Char *s, UInt table_size) |
| 397 | { |
| 398 | int hash_value = 0; |
| 399 | for ( ; *s; s++) |
| 400 | hash_value = (HASH_CONSTANT * hash_value + *s) % table_size; |
| 401 | return hash_value; |
| 402 | } |
| 403 | |
| 404 | /* Do a three step traversal: by filename, then fn_name, then instr_addr. |
| 405 | * In all cases prepends new nodes to their chain. Returns a pointer to the |
| 406 | * cost centre. Also sets BB_seen_before by reference. |
| 407 | */ |
| 408 | static __inline__ BBCC* get_BBCC(Addr bb_orig_addr, UCodeBlock* cb, |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 409 | Bool remove, Bool *BB_seen_before) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 410 | { |
| 411 | file_node *curr_file_node; |
| 412 | fn_node *curr_fn_node; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 413 | BBCC **prev_BBCC_next_ptr, *curr_BBCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 414 | Char filename[FILENAME_LEN], fn_name[FN_NAME_LEN]; |
| 415 | UInt filename_hash, fnname_hash, BBCC_hash; |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 416 | Int dummy_line_num; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 417 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 418 | get_debug_info(bb_orig_addr, filename, fn_name, &dummy_line_num); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 419 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 420 | VGP_PUSHCC(VgpGetBBCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 421 | filename_hash = hash(filename, N_FILE_ENTRIES); |
| 422 | curr_file_node = BBCC_table[filename_hash]; |
| 423 | while (NULL != curr_file_node && |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 424 | VG_(strcmp)(filename, curr_file_node->filename) != 0) { |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 425 | curr_file_node = curr_file_node->next; |
| 426 | } |
| 427 | if (NULL == curr_file_node) { |
| 428 | BBCC_table[filename_hash] = curr_file_node = |
| 429 | new_file_node(filename, BBCC_table[filename_hash]); |
| 430 | distinct_files++; |
| 431 | } |
| 432 | |
| 433 | fnname_hash = hash(fn_name, N_FN_ENTRIES); |
| 434 | curr_fn_node = curr_file_node->fns[fnname_hash]; |
| 435 | while (NULL != curr_fn_node && |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 436 | VG_(strcmp)(fn_name, curr_fn_node->fn_name) != 0) { |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 437 | curr_fn_node = curr_fn_node->next; |
| 438 | } |
| 439 | if (NULL == curr_fn_node) { |
| 440 | curr_file_node->fns[fnname_hash] = curr_fn_node = |
| 441 | new_fn_node(fn_name, curr_file_node->fns[fnname_hash]); |
| 442 | distinct_fns++; |
| 443 | } |
| 444 | |
| 445 | BBCC_hash = bb_orig_addr % N_BBCC_ENTRIES; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 446 | prev_BBCC_next_ptr = &(curr_fn_node->BBCCs[BBCC_hash]); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 447 | curr_BBCC = curr_fn_node->BBCCs[BBCC_hash]; |
| 448 | while (NULL != curr_BBCC && bb_orig_addr != curr_BBCC->orig_addr) { |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 449 | prev_BBCC_next_ptr = &(curr_BBCC->next); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 450 | curr_BBCC = curr_BBCC->next; |
| 451 | } |
| 452 | if (curr_BBCC == NULL) { |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 453 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 454 | sk_assert(False == remove); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 455 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 456 | curr_fn_node->BBCCs[BBCC_hash] = curr_BBCC = |
| 457 | new_BBCC(bb_orig_addr, cb, curr_fn_node->BBCCs[BBCC_hash]); |
| 458 | *BB_seen_before = False; |
| 459 | |
| 460 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 461 | sk_assert(bb_orig_addr == curr_BBCC->orig_addr); |
| 462 | sk_assert(curr_BBCC->array_size > 0 && curr_BBCC->array_size < 1000000); |
sewardj | 98e91bc | 2002-05-01 02:32:10 +0000 | [diff] [blame] | 463 | if (VG_(clo_verbosity) > 2) { |
| 464 | VG_(message)(Vg_DebugMsg, |
| 465 | "BB retranslation, retrieving from BBCC table"); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 466 | } |
| 467 | *BB_seen_before = True; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 468 | |
| 469 | if (True == remove) { |
| 470 | // Remove curr_BBCC from chain; it will be used and free'd by the |
| 471 | // caller. |
| 472 | *prev_BBCC_next_ptr = curr_BBCC->next; |
| 473 | |
| 474 | } else { |
| 475 | BB_retranslations++; |
| 476 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 477 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 478 | VGP_POPCC(VgpGetBBCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 479 | return curr_BBCC; |
| 480 | } |
| 481 | |
| 482 | /*------------------------------------------------------------*/ |
| 483 | /*--- Cache simulation instrumentation phase ---*/ |
| 484 | /*------------------------------------------------------------*/ |
| 485 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 486 | // SSS: do something about all these... |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 487 | #define uInstr1 VG_(new_UInstr1) |
| 488 | #define uInstr2 VG_(new_UInstr2) |
| 489 | #define uInstr3 VG_(new_UInstr3) |
| 490 | #define uLiteral VG_(set_lit_field) |
| 491 | #define uCCall VG_(set_ccall_fields) |
| 492 | #define newTemp VG_(get_new_temp) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 493 | |
| 494 | static Int compute_BBCC_array_size(UCodeBlock* cb) |
| 495 | { |
| 496 | UInstr* u_in; |
| 497 | Int i, CC_size, BBCC_size = 0; |
| 498 | Bool is_LOAD, is_STORE, is_FPU_R, is_FPU_W; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 499 | Int t_read, t_write; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 500 | |
| 501 | is_LOAD = is_STORE = is_FPU_R = is_FPU_W = False; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 502 | t_read = t_write = INVALID_TEMPREG; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 503 | |
| 504 | for (i = 0; i < cb->used; i++) { |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 505 | u_in = &cb->instrs[i]; |
| 506 | switch(u_in->opcode) { |
| 507 | |
| 508 | case INCEIP: |
| 509 | goto case_for_end_of_instr; |
| 510 | |
| 511 | case JMP: |
| 512 | if (u_in->cond != CondAlways) break; |
| 513 | |
| 514 | goto case_for_end_of_instr; |
| 515 | |
| 516 | case_for_end_of_instr: |
| 517 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 518 | if (((is_LOAD && is_STORE) || (is_FPU_R && is_FPU_W)) && |
| 519 | t_read != t_write) |
| 520 | CC_size = sizeof(iddCC); |
| 521 | else if (is_LOAD || is_STORE || is_FPU_R || is_FPU_W) |
| 522 | CC_size = sizeof(idCC); |
| 523 | else |
| 524 | CC_size = sizeof(iCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 525 | |
| 526 | BBCC_size += CC_size; |
| 527 | is_LOAD = is_STORE = is_FPU_R = is_FPU_W = False; |
| 528 | break; |
| 529 | |
| 530 | case LOAD: |
| 531 | /* Two LDBs are possible for a single instruction */ |
njn | 9aae674 | 2002-04-30 13:44:01 +0000 | [diff] [blame] | 532 | /* Also, a STORE can come after a LOAD for bts/btr/btc */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 533 | sk_assert(/*!is_LOAD &&*/ /* !is_STORE && */ |
sewardj | fc3e5d3 | 2002-04-30 10:18:48 +0000 | [diff] [blame] | 534 | !is_FPU_R && !is_FPU_W); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 535 | t_read = u_in->val1; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 536 | is_LOAD = True; |
| 537 | break; |
| 538 | |
| 539 | case STORE: |
| 540 | /* Multiple STOREs are possible for 'pushal' */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 541 | sk_assert( /*!is_STORE &&*/ !is_FPU_R && !is_FPU_W); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 542 | t_write = u_in->val2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 543 | is_STORE = True; |
| 544 | break; |
| 545 | |
| 546 | case FPU_R: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 547 | sk_assert(!is_LOAD && !is_STORE && !is_FPU_R && !is_FPU_W); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 548 | t_read = u_in->val2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 549 | is_FPU_R = True; |
| 550 | break; |
| 551 | |
| 552 | case FPU_W: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 553 | sk_assert(!is_LOAD && !is_STORE && !is_FPU_R && !is_FPU_W); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 554 | t_write = u_in->val2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 555 | is_FPU_W = True; |
| 556 | break; |
| 557 | |
| 558 | default: |
| 559 | break; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | return BBCC_size; |
| 564 | } |
| 565 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 566 | static __attribute__ ((regparm (1))) |
| 567 | void log_1I_0D_cache_access(iCC* cc) |
| 568 | { |
| 569 | //VG_(printf)("1I_0D: CCaddr=0x%x, iaddr=0x%x, isize=%u\n", |
| 570 | // cc, cc->instr_addr, cc->instr_size) |
| 571 | VGP_PUSHCC(VgpCacheSimulate); |
| 572 | cachesim_I1_doref(cc->instr_addr, cc->instr_size, &cc->I.m1, &cc->I.m2); |
| 573 | cc->I.a++; |
| 574 | VGP_POPCC(VgpCacheSimulate); |
| 575 | } |
| 576 | |
| 577 | /* Difference between this function and log_1I_0D_cache_access() is that |
| 578 | this one can be passed any kind of CC, not just an iCC. So we have to |
| 579 | be careful to make sure we don't make any assumptions about CC layout. |
| 580 | (As it stands, they would be safe, but this will avoid potential heartache |
| 581 | if anyone else changes CC layout.) |
| 582 | Note that we only do the switch for the JIFZ version because if we always |
| 583 | called this switching version, things would run about 5% slower. */ |
| 584 | static __attribute__ ((regparm (1))) |
| 585 | void log_1I_0D_cache_access_JIFZ(iCC* cc) |
| 586 | { |
| 587 | UChar instr_size; |
| 588 | Addr instr_addr; |
| 589 | CC* I; |
| 590 | |
| 591 | //VG_(printf)("1I_0D: CCaddr=0x%x, iaddr=0x%x, isize=%u\n", |
| 592 | // cc, cc->instr_addr, cc->instr_size) |
| 593 | VGP_PUSHCC(VgpCacheSimulate); |
| 594 | |
| 595 | switch(cc->tag) { |
| 596 | case InstrCC: |
| 597 | instr_size = cc->instr_size; |
| 598 | instr_addr = cc->instr_addr; |
| 599 | I = &(cc->I); |
| 600 | break; |
| 601 | case ReadCC: |
| 602 | case WriteCC: |
| 603 | case ModCC: |
| 604 | instr_size = ((idCC*)cc)->instr_size; |
| 605 | instr_addr = ((idCC*)cc)->instr_addr; |
| 606 | I = &( ((idCC*)cc)->I ); |
| 607 | break; |
| 608 | case ReadWriteCC: |
| 609 | instr_size = ((iddCC*)cc)->instr_size; |
| 610 | instr_addr = ((iddCC*)cc)->instr_addr; |
| 611 | I = &( ((iddCC*)cc)->I ); |
| 612 | break; |
| 613 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 614 | VG_(skin_panic)("Unknown CC type in log_1I_0D_cache_access_JIFZ()\n"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 615 | break; |
| 616 | } |
| 617 | cachesim_I1_doref(instr_addr, instr_size, &I->m1, &I->m2); |
| 618 | I->a++; |
| 619 | VGP_POPCC(VgpCacheSimulate); |
| 620 | } |
| 621 | |
| 622 | __attribute__ ((regparm (2))) static |
| 623 | void log_0I_1D_cache_access(idCC* cc, Addr data_addr) |
| 624 | { |
| 625 | //VG_(printf)("0I_1D: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n", |
| 626 | // cc, cc->instr_addr, cc->instr_size, data_addr, cc->data_size) |
| 627 | VGP_PUSHCC(VgpCacheSimulate); |
| 628 | cachesim_D1_doref(data_addr, cc->data_size, &cc->D.m1, &cc->D.m2); |
| 629 | cc->D.a++; |
| 630 | VGP_POPCC(VgpCacheSimulate); |
| 631 | } |
| 632 | |
| 633 | __attribute__ ((regparm (2))) static |
| 634 | void log_1I_1D_cache_access(idCC* cc, Addr data_addr) |
| 635 | { |
| 636 | //VG_(printf)("1I_1D: CCaddr=%p, iaddr=%p, isize=%u, daddr=%p, dsize=%u\n", |
| 637 | // cc, cc->instr_addr, cc->instr_size, data_addr, cc->data_size) |
| 638 | VGP_PUSHCC(VgpCacheSimulate); |
| 639 | cachesim_I1_doref(cc->instr_addr, cc->instr_size, &cc->I.m1, &cc->I.m2); |
| 640 | cc->I.a++; |
| 641 | |
| 642 | cachesim_D1_doref(data_addr, cc->data_size, &cc->D.m1, &cc->D.m2); |
| 643 | cc->D.a++; |
| 644 | VGP_POPCC(VgpCacheSimulate); |
| 645 | } |
| 646 | |
| 647 | __attribute__ ((regparm (3))) static |
| 648 | void log_0I_2D_cache_access(iddCC* cc, Addr data_addr1, Addr data_addr2) |
| 649 | { |
| 650 | //VG_(printf)("0I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=0x%x, daddr2=%p, size=%u\n", |
| 651 | // cc, cc->instr_addr, cc->instr_size, data_addr1, data_addr2, cc->data_size) |
| 652 | VGP_PUSHCC(VgpCacheSimulate); |
| 653 | cachesim_D1_doref(data_addr1, cc->data_size, &cc->Da.m1, &cc->Da.m2); |
| 654 | cc->Da.a++; |
| 655 | cachesim_D1_doref(data_addr2, cc->data_size, &cc->Db.m1, &cc->Db.m2); |
| 656 | cc->Db.a++; |
| 657 | VGP_POPCC(VgpCacheSimulate); |
| 658 | } |
| 659 | |
| 660 | __attribute__ ((regparm (3))) static |
| 661 | void log_1I_2D_cache_access(iddCC* cc, Addr data_addr1, Addr data_addr2) |
| 662 | { |
| 663 | //VG_(printf)("1I_2D: CCaddr=%p, iaddr=%p, isize=%u, daddr1=%p, daddr2=%p, dsize=%u\n", |
| 664 | // cc, cc->instr_addr, cc->instr_size, data_addr1, data_addr2, cc->data_size) |
| 665 | VGP_PUSHCC(VgpCacheSimulate); |
| 666 | cachesim_I1_doref(cc->instr_addr, cc->instr_size, &cc->I.m1, &cc->I.m2); |
| 667 | cc->I.a++; |
| 668 | |
| 669 | cachesim_D1_doref(data_addr1, cc->data_size, &cc->Da.m1, &cc->Da.m2); |
| 670 | cc->Da.a++; |
| 671 | cachesim_D1_doref(data_addr2, cc->data_size, &cc->Db.m1, &cc->Db.m2); |
| 672 | cc->Db.a++; |
| 673 | VGP_POPCC(VgpCacheSimulate); |
| 674 | } |
| 675 | |
| 676 | UCodeBlock* SK_(instrument)(UCodeBlock* cb_in, Addr orig_addr) |
| 677 | { |
| 678 | /* Use this rather than eg. -1 because it's a UInt. */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 679 | #define INVALID_DATA_SIZE 999999 |
| 680 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 681 | UCodeBlock* cb; |
| 682 | Int i; |
| 683 | UInstr* u_in; |
| 684 | BBCC* BBCC_node; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 685 | Int t_CC_addr, t_read_addr, t_write_addr, t_data_addr1, |
| 686 | t_data_addr2, t_read, t_write; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 687 | Int CC_size = -1; /* Shut gcc warnings up */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 688 | Addr x86_instr_addr = orig_addr; |
| 689 | UInt x86_instr_size, data_size = INVALID_DATA_SIZE; |
| 690 | Addr helper; |
| 691 | Int argc; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 692 | UInt stack_used; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 693 | Bool BB_seen_before = False; |
| 694 | Bool instrumented_Jcond = False; |
| 695 | Bool has_rep_prefix = False; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 696 | Addr BBCC_ptr0, BBCC_ptr; |
| 697 | |
| 698 | /* Get BBCC (creating if necessary -- requires a counting pass over the BB |
| 699 | * if it's the first time it's been seen), and point to start of the |
| 700 | * BBCC array. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 701 | BBCC_node = get_BBCC(orig_addr, cb_in, /*remove=*/False, &BB_seen_before); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 702 | BBCC_ptr0 = BBCC_ptr = (Addr)(BBCC_node->array); |
| 703 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 704 | cb = VG_(alloc_UCodeBlock)(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 705 | cb->nextTemp = cb_in->nextTemp; |
| 706 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 707 | t_CC_addr = t_read_addr = t_write_addr = t_data_addr1 = t_data_addr2 = |
| 708 | t_read = t_write = INVALID_TEMPREG; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 709 | |
| 710 | for (i = 0; i < cb_in->used; i++) { |
| 711 | u_in = &cb_in->instrs[i]; |
| 712 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 713 | /* What this is all about: we want to instrument each x86 instruction |
| 714 | * translation. The end of these are marked in three ways. The three |
| 715 | * ways, and the way we instrument them, are as follows: |
| 716 | * |
| 717 | * 1. UCode, INCEIP --> UCode, Instrumentation, INCEIP |
| 718 | * 2. UCode, Juncond --> UCode, Instrumentation, Juncond |
| 719 | * 3. UCode, Jcond, Juncond --> UCode, Instrumentation, Jcond, Juncond |
| 720 | * |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 721 | * The last UInstr in a basic block is always a Juncond. Jconds, |
| 722 | * when they appear, are always second last. We check this with |
| 723 | * various assertions. |
| 724 | * |
| 725 | * We must put the instrumentation before any jumps so that it is always |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 726 | * executed. We don't have to put the instrumentation before the INCEIP |
| 727 | * (it could go after) but we do so for consistency. |
| 728 | * |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 729 | * x86 instruction sizes are obtained from INCEIPs (for case 1) or |
| 730 | * from .extra4b field of the final JMP (for case 2 & 3). |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 731 | * |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 732 | * Note that JIFZ is treated differently. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 733 | * |
| 734 | * The instrumentation is just a call to the appropriate helper function, |
| 735 | * passing it the address of the instruction's CC. |
| 736 | */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 737 | if (instrumented_Jcond) sk_assert(u_in->opcode == JMP); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 738 | |
| 739 | switch (u_in->opcode) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 740 | case NOP: case CALLM_E: case CALLM_S: |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 741 | break; |
| 742 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 743 | /* For memory-ref instrs, copy the data_addr into a temporary to be |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 744 | * passed to the cachesim_* helper at the end of the instruction. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 745 | */ |
| 746 | case LOAD: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 747 | t_read = u_in->val1; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 748 | t_read_addr = newTemp(cb); |
| 749 | uInstr2(cb, MOV, 4, TempReg, u_in->val1, TempReg, t_read_addr); |
| 750 | data_size = u_in->size; |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 751 | VG_(copy_UInstr)(cb, u_in); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 752 | break; |
| 753 | |
| 754 | case FPU_R: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 755 | t_read = u_in->val2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 756 | t_read_addr = newTemp(cb); |
| 757 | uInstr2(cb, MOV, 4, TempReg, u_in->val2, TempReg, t_read_addr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 758 | data_size = ( u_in->size <= MIN_LINE_SIZE |
| 759 | ? u_in->size |
| 760 | : MIN_LINE_SIZE); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 761 | VG_(copy_UInstr)(cb, u_in); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 762 | break; |
| 763 | |
| 764 | /* Note that we must set t_write_addr even for mod instructions; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 765 | * That's how the code above determines whether it does a write. |
| 766 | * Without it, it would think a mod instruction is a read. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 767 | * As for the MOV, if it's a mod instruction it's redundant, but it's |
| 768 | * not expensive and mod instructions are rare anyway. */ |
| 769 | case STORE: |
| 770 | case FPU_W: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 771 | t_write = u_in->val2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 772 | t_write_addr = newTemp(cb); |
| 773 | uInstr2(cb, MOV, 4, TempReg, u_in->val2, TempReg, t_write_addr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 774 | /* 28 and 108 B data-sized instructions will be done |
| 775 | * inaccurately but they're very rare and this avoids errors |
| 776 | * from hitting more than two cache lines in the simulation. */ |
| 777 | data_size = ( u_in->size <= MIN_LINE_SIZE |
| 778 | ? u_in->size |
| 779 | : MIN_LINE_SIZE); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 780 | VG_(copy_UInstr)(cb, u_in); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 781 | break; |
| 782 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 783 | |
| 784 | /* For rep-prefixed instructions, log a single I-cache access |
| 785 | * before the UCode loop that implements the repeated part, which |
| 786 | * is where the multiple D-cache accesses are logged. */ |
| 787 | case JIFZ: |
| 788 | has_rep_prefix = True; |
| 789 | |
| 790 | /* Setup 1st and only arg: CC addr */ |
| 791 | t_CC_addr = newTemp(cb); |
| 792 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_CC_addr); |
| 793 | uLiteral(cb, BBCC_ptr); |
| 794 | |
| 795 | /* Call helper */ |
| 796 | uInstr1(cb, CCALL, 0, TempReg, t_CC_addr); |
| 797 | uCCall(cb, (Addr) & log_1I_0D_cache_access_JIFZ, 1, 1, False); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 798 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 799 | break; |
| 800 | |
| 801 | |
| 802 | /* INCEIP: insert instrumentation */ |
| 803 | case INCEIP: |
| 804 | x86_instr_size = u_in->val1; |
| 805 | goto instrument_x86_instr; |
| 806 | |
| 807 | /* JMP: insert instrumentation if the first JMP */ |
| 808 | case JMP: |
| 809 | if (instrumented_Jcond) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 810 | sk_assert(CondAlways == u_in->cond); |
| 811 | sk_assert(i+1 == cb_in->used); |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 812 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 813 | instrumented_Jcond = False; /* reset */ |
| 814 | break; |
| 815 | } |
| 816 | /* The first JMP... instrument. */ |
| 817 | if (CondAlways != u_in->cond) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 818 | sk_assert(i+2 == cb_in->used); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 819 | instrumented_Jcond = True; |
| 820 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 821 | sk_assert(i+1 == cb_in->used); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /* Get x86 instr size from final JMP. */ |
| 825 | x86_instr_size = LAST_UINSTR(cb_in).extra4b; |
| 826 | goto instrument_x86_instr; |
| 827 | |
| 828 | |
| 829 | /* Code executed at the end of each x86 instruction. */ |
| 830 | instrument_x86_instr: |
| 831 | |
| 832 | /* Initialise the CC in the BBCC array appropriately if it |
| 833 | * hasn't been initialised before. Then call appropriate sim |
| 834 | * function, passing it the CC address. */ |
| 835 | stack_used = 0; |
| 836 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 837 | sk_assert(x86_instr_size >= 1 && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 838 | x86_instr_size <= MAX_x86_INSTR_SIZE); |
| 839 | |
| 840 | #define IS_(X) (INVALID_TEMPREG != t_##X##_addr) |
| 841 | |
| 842 | if (!IS_(read) && !IS_(write)) { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 843 | sk_assert(INVALID_DATA_SIZE == data_size); |
| 844 | sk_assert(INVALID_TEMPREG == t_read_addr && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 845 | INVALID_TEMPREG == t_read && |
| 846 | INVALID_TEMPREG == t_write_addr && |
| 847 | INVALID_TEMPREG == t_write); |
| 848 | CC_size = sizeof(iCC); |
| 849 | if (!BB_seen_before) |
| 850 | init_iCC((iCC*)BBCC_ptr, x86_instr_addr, x86_instr_size); |
| 851 | helper = ( has_rep_prefix |
| 852 | ? (Addr)0 /* no extra log needed */ |
| 853 | : (Addr) & log_1I_0D_cache_access |
| 854 | ); |
| 855 | argc = 1; |
| 856 | |
| 857 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 858 | sk_assert(4 == data_size || 2 == data_size || 1 == data_size || |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 859 | 8 == data_size || 10 == data_size || |
| 860 | MIN_LINE_SIZE == data_size); |
| 861 | |
| 862 | if (IS_(read) && !IS_(write)) { |
| 863 | CC_size = sizeof(idCC); |
| 864 | /* If it uses 'rep', we've already logged the I-cache |
| 865 | * access at the JIFZ UInstr (see JIFZ case below) so |
| 866 | * don't do it here */ |
| 867 | helper = ( has_rep_prefix |
| 868 | ? (Addr) & log_0I_1D_cache_access |
| 869 | : (Addr) & log_1I_1D_cache_access |
| 870 | ); |
| 871 | argc = 2; |
| 872 | if (!BB_seen_before) |
| 873 | init_idCC(ReadCC, (idCC*)BBCC_ptr, x86_instr_addr, |
| 874 | x86_instr_size, data_size); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 875 | sk_assert(INVALID_TEMPREG != t_read_addr && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 876 | INVALID_TEMPREG != t_read && |
| 877 | INVALID_TEMPREG == t_write_addr && |
| 878 | INVALID_TEMPREG == t_write); |
| 879 | t_data_addr1 = t_read_addr; |
| 880 | |
| 881 | } else if (!IS_(read) && IS_(write)) { |
| 882 | CC_size = sizeof(idCC); |
| 883 | helper = ( has_rep_prefix |
| 884 | ? (Addr) & log_0I_1D_cache_access |
| 885 | : (Addr) & log_1I_1D_cache_access |
| 886 | ); |
| 887 | argc = 2; |
| 888 | if (!BB_seen_before) |
| 889 | init_idCC(WriteCC, (idCC*)BBCC_ptr, x86_instr_addr, |
| 890 | x86_instr_size, data_size); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 891 | sk_assert(INVALID_TEMPREG == t_read_addr && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 892 | INVALID_TEMPREG == t_read && |
| 893 | INVALID_TEMPREG != t_write_addr && |
| 894 | INVALID_TEMPREG != t_write); |
| 895 | t_data_addr1 = t_write_addr; |
| 896 | |
| 897 | } else { |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 898 | sk_assert(IS_(read) && IS_(write)); |
| 899 | sk_assert(INVALID_TEMPREG != t_read_addr && |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 900 | INVALID_TEMPREG != t_read && |
| 901 | INVALID_TEMPREG != t_write_addr && |
| 902 | INVALID_TEMPREG != t_write); |
| 903 | if (t_read == t_write) { |
| 904 | CC_size = sizeof(idCC); |
| 905 | helper = ( has_rep_prefix |
| 906 | ? (Addr) & log_0I_1D_cache_access |
| 907 | : (Addr) & log_1I_1D_cache_access |
| 908 | ); |
| 909 | argc = 2; |
| 910 | if (!BB_seen_before) |
| 911 | init_idCC(ModCC, (idCC*)BBCC_ptr, x86_instr_addr, |
| 912 | x86_instr_size, data_size); |
| 913 | t_data_addr1 = t_read_addr; |
| 914 | } else { |
| 915 | CC_size = sizeof(iddCC); |
| 916 | helper = ( has_rep_prefix |
| 917 | ? (Addr) & log_0I_2D_cache_access |
| 918 | : (Addr) & log_1I_2D_cache_access |
| 919 | ); |
| 920 | argc = 3; |
| 921 | if (!BB_seen_before) |
| 922 | init_iddCC((iddCC*)BBCC_ptr, x86_instr_addr, |
| 923 | x86_instr_size, data_size); |
| 924 | t_data_addr1 = t_read_addr; |
| 925 | t_data_addr2 = t_write_addr; |
| 926 | } |
| 927 | } |
| 928 | #undef IS_ |
| 929 | } |
| 930 | |
| 931 | /* Call the helper, if necessary */ |
| 932 | if ((Addr)0 != helper) { |
| 933 | |
| 934 | /* Setup 1st arg: CC addr */ |
| 935 | t_CC_addr = newTemp(cb); |
| 936 | uInstr2(cb, MOV, 4, Literal, 0, TempReg, t_CC_addr); |
| 937 | uLiteral(cb, BBCC_ptr); |
| 938 | |
| 939 | /* Call the helper */ |
| 940 | if (1 == argc) |
| 941 | uInstr1(cb, CCALL, 0, TempReg, t_CC_addr); |
| 942 | else if (2 == argc) |
| 943 | uInstr2(cb, CCALL, 0, TempReg, t_CC_addr, |
| 944 | TempReg, t_data_addr1); |
| 945 | else if (3 == argc) |
| 946 | uInstr3(cb, CCALL, 0, TempReg, t_CC_addr, |
| 947 | TempReg, t_data_addr1, |
| 948 | TempReg, t_data_addr2); |
| 949 | else |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 950 | VG_(skin_panic)("argc... not 1 or 2 or 3?"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 951 | |
| 952 | uCCall(cb, helper, argc, argc, False); |
| 953 | } |
| 954 | |
| 955 | /* Copy original UInstr (INCEIP or JMP) */ |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 956 | VG_(copy_UInstr)(cb, u_in); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 957 | |
| 958 | /* Update BBCC_ptr, EIP, de-init read/write temps for next instr */ |
| 959 | BBCC_ptr += CC_size; |
| 960 | x86_instr_addr += x86_instr_size; |
| 961 | t_CC_addr = t_read_addr = t_write_addr = t_data_addr1 = |
| 962 | t_data_addr2 = t_read = t_write = INVALID_TEMPREG; |
| 963 | data_size = INVALID_DATA_SIZE; |
| 964 | has_rep_prefix = False; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 965 | break; |
| 966 | |
| 967 | default: |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 968 | VG_(copy_UInstr)(cb, u_in); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /* Just check everything looks ok */ |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 974 | sk_assert(BBCC_ptr - BBCC_ptr0 == BBCC_node->array_size); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 975 | |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 976 | VG_(free_UCodeBlock)(cb_in); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 977 | return cb; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 978 | |
| 979 | #undef INVALID_DATA_SIZE |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 983 | /*--- Automagic cache initialisation stuff ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 984 | /*------------------------------------------------------------*/ |
| 985 | |
| 986 | /* Total reads/writes/misses. Calculated during CC traversal at the end. */ |
| 987 | static CC Ir_total; |
| 988 | static CC Dr_total; |
| 989 | static CC Dw_total; |
| 990 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 991 | #define UNDEFINED_CACHE ((cache_t) { -1, -1, -1 }) |
| 992 | |
| 993 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 994 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
| 995 | static cache_t clo_L2_cache = UNDEFINED_CACHE; |
| 996 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 997 | /* All CPUID info taken from sandpile.org/a32/cpuid.htm */ |
| 998 | /* Probably only works for Intel and AMD chips, and probably only for some of |
| 999 | * them. |
| 1000 | */ |
| 1001 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1002 | static __inline__ void cpuid(Int n, Int *a, Int *b, Int *c, Int *d) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1003 | { |
| 1004 | __asm__ __volatile__ ( |
| 1005 | "cpuid" |
| 1006 | : "=a" (*a), "=b" (*b), "=c" (*c), "=d" (*d) /* output */ |
| 1007 | : "0" (n) /* input */ |
| 1008 | ); |
| 1009 | } |
| 1010 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1011 | static void micro_ops_warn(Int actual_size, Int used_size, Int line_size) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1012 | { |
| 1013 | VG_(message)(Vg_DebugMsg, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1014 | "warning: Pentium with %d K micro-op instruction trace cache", |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1015 | actual_size); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1016 | VG_(message)(Vg_DebugMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1017 | " Simulating a %d KB cache with %d B lines", |
| 1018 | used_size, line_size); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /* Intel method is truly wretched. We have to do an insane indexing into an |
| 1022 | * array of pre-defined configurations for various parts of the memory |
| 1023 | * hierarchy. |
| 1024 | */ |
| 1025 | static |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1026 | Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1027 | { |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1028 | UChar info[16]; |
| 1029 | Int i, trials; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1030 | Bool L2_found = False; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1031 | |
| 1032 | if (level < 2) { |
| 1033 | VG_(message)(Vg_DebugMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1034 | "warning: CPUID level < 2 for Intel processor (%d)", |
| 1035 | level); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1036 | return -1; |
| 1037 | } |
| 1038 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1039 | cpuid(2, (Int*)&info[0], (Int*)&info[4], |
| 1040 | (Int*)&info[8], (Int*)&info[12]); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1041 | trials = info[0] - 1; /* AL register - bits 0..7 of %eax */ |
| 1042 | info[0] = 0x0; /* reset AL */ |
| 1043 | |
| 1044 | if (0 != trials) { |
| 1045 | VG_(message)(Vg_DebugMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1046 | "warning: non-zero CPUID trials for Intel processor (%d)", |
| 1047 | trials); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1048 | return -1; |
| 1049 | } |
| 1050 | |
| 1051 | for (i = 0; i < 16; i++) { |
| 1052 | |
| 1053 | switch (info[i]) { |
| 1054 | |
| 1055 | case 0x0: /* ignore zeros */ |
| 1056 | break; |
| 1057 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1058 | /* TLB info, ignore */ |
| 1059 | case 0x01: case 0x02: case 0x03: case 0x04: |
| 1060 | case 0x50: case 0x51: case 0x52: case 0x5b: case 0x5c: case 0x5d: |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | |
| 1063 | case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break; |
| 1064 | case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break; |
| 1065 | |
| 1066 | case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break; |
| 1067 | case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break; |
| 1068 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1069 | /* IA-64 info -- panic! */ |
| 1070 | case 0x10: case 0x15: case 0x1a: |
| 1071 | case 0x88: case 0x89: case 0x8a: case 0x8d: |
| 1072 | case 0x90: case 0x96: case 0x9b: |
| 1073 | VG_(message)(Vg_DebugMsg, |
| 1074 | "error: IA-64 cache stats! Cachegrind doesn't run on IA-64..."); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1075 | VG_(skin_panic)("IA-64 detected"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1076 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1077 | case 0x22: case 0x23: case 0x25: case 0x29: |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1078 | VG_(message)(Vg_DebugMsg, |
| 1079 | "warning: L3 cache detected but ignored\n"); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1082 | /* These are sectored, whatever that means */ |
| 1083 | case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break; |
| 1084 | case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break; |
| 1085 | |
| 1086 | /* If a P6 core, this means "no L2 cache". |
| 1087 | If a P4 core, this means "no L3 cache". |
| 1088 | We don't know what core it is, so don't issue a warning. To detect |
| 1089 | a missing L2 cache, we use 'L2_found'. */ |
| 1090 | case 0x40: |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1091 | break; |
| 1092 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1093 | case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break; |
| 1094 | case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break; |
| 1095 | case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break; |
| 1096 | case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break; |
| 1097 | case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1098 | |
| 1099 | /* These are sectored, whatever that means */ |
| 1100 | case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */ |
| 1101 | case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */ |
| 1102 | case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */ |
| 1103 | |
| 1104 | /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based. |
| 1105 | * conversion to byte size is a total guess; treat the 12K and 16K |
| 1106 | * cases the same since the cache byte size must be a power of two for |
| 1107 | * everything to work!. Also guessing 32 bytes for the line size... |
| 1108 | */ |
| 1109 | case 0x70: /* 12K micro-ops, 8-way */ |
| 1110 | *I1c = (cache_t) { 16, 8, 32 }; |
| 1111 | micro_ops_warn(12, 16, 32); |
| 1112 | break; |
| 1113 | case 0x71: /* 16K micro-ops, 8-way */ |
| 1114 | *I1c = (cache_t) { 16, 8, 32 }; |
| 1115 | micro_ops_warn(16, 16, 32); |
| 1116 | break; |
| 1117 | case 0x72: /* 32K micro-ops, 8-way */ |
| 1118 | *I1c = (cache_t) { 32, 8, 32 }; |
| 1119 | micro_ops_warn(32, 32, 32); |
| 1120 | break; |
| 1121 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1122 | /* These are sectored, whatever that means */ |
| 1123 | case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break; |
| 1124 | case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break; |
| 1125 | case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break; |
| 1126 | case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break; |
| 1127 | case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1128 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1129 | case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break; |
| 1130 | case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break; |
| 1131 | case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break; |
| 1132 | case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break; |
| 1133 | case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1134 | |
| 1135 | default: |
| 1136 | VG_(message)(Vg_DebugMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1137 | "warning: Unknown Intel cache config value " |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1138 | "(0x%x), ignoring", info[i]); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (!L2_found) |
| 1144 | VG_(message)(Vg_DebugMsg, |
| 1145 | "warning: L2 cache not installed, ignore L2 results."); |
| 1146 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | /* AMD method is straightforward, just extract appropriate bits from the |
| 1151 | * result registers. |
| 1152 | * |
| 1153 | * Bits, for D1 and I1: |
| 1154 | * 31..24 data L1 cache size in KBs |
| 1155 | * 23..16 data L1 cache associativity (FFh=full) |
| 1156 | * 15.. 8 data L1 cache lines per tag |
| 1157 | * 7.. 0 data L1 cache line size in bytes |
| 1158 | * |
| 1159 | * Bits, for L2: |
| 1160 | * 31..16 unified L2 cache size in KBs |
| 1161 | * 15..12 unified L2 cache associativity (0=off, FFh=full) |
| 1162 | * 11.. 8 unified L2 cache lines per tag |
| 1163 | * 7.. 0 unified L2 cache line size in bytes |
| 1164 | * |
| 1165 | * #3 The AMD K7 processor's L2 cache must be configured prior to relying |
| 1166 | * upon this information. (Whatever that means -- njn) |
| 1167 | * |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1168 | * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model |
| 1169 | * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB), |
| 1170 | * so we detect that. |
| 1171 | * |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1172 | * Returns 0 on success, non-zero on failure. |
| 1173 | */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1174 | static |
| 1175 | Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1176 | { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1177 | Int dummy, model, ext_level; |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1178 | Int I1i, D1i, L2i; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1179 | |
| 1180 | cpuid(0x80000000, &ext_level, &dummy, &dummy, &dummy); |
| 1181 | |
| 1182 | if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) { |
| 1183 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1184 | "warning: ext_level < 0x80000006 for AMD processor (0x%x)", |
| 1185 | ext_level); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1186 | return -1; |
| 1187 | } |
| 1188 | |
| 1189 | cpuid(0x80000005, &dummy, &dummy, &D1i, &I1i); |
| 1190 | cpuid(0x80000006, &dummy, &dummy, &L2i, &dummy); |
| 1191 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1192 | cpuid(0x1, &model, &dummy, &dummy, &dummy); |
| 1193 | /*VG_(message)(Vg_UserMsg,"CPU model %04x",model);*/ |
| 1194 | |
| 1195 | /* Check for Duron bug */ |
| 1196 | if (model == 0x630) { |
| 1197 | VG_(message)(Vg_UserMsg, |
| 1198 | "Buggy Duron stepping A0. Assuming L2 size=65536 bytes"); |
| 1199 | L2i = (64 << 16) | (L2i & 0xffff); |
| 1200 | } |
| 1201 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1202 | D1c->size = (D1i >> 24) & 0xff; |
| 1203 | D1c->assoc = (D1i >> 16) & 0xff; |
| 1204 | D1c->line_size = (D1i >> 0) & 0xff; |
| 1205 | |
| 1206 | I1c->size = (I1i >> 24) & 0xff; |
| 1207 | I1c->assoc = (I1i >> 16) & 0xff; |
| 1208 | I1c->line_size = (I1i >> 0) & 0xff; |
| 1209 | |
| 1210 | L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */ |
| 1211 | L2c->assoc = (L2i >> 12) & 0xf; |
| 1212 | L2c->line_size = (L2i >> 0) & 0xff; |
| 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static jmp_buf cpuid_jmpbuf; |
| 1218 | |
| 1219 | static |
| 1220 | void cpuid_SIGILL_handler(int signum) |
| 1221 | { |
| 1222 | __builtin_longjmp(cpuid_jmpbuf, 1); |
| 1223 | } |
| 1224 | |
| 1225 | static |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1226 | Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1227 | { |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1228 | Int level, res, ret; |
| 1229 | Char vendor_id[13]; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1230 | vki_ksigaction sigill_new, sigill_saved; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1231 | |
| 1232 | /* Install own SIGILL handler */ |
| 1233 | sigill_new.ksa_handler = cpuid_SIGILL_handler; |
| 1234 | sigill_new.ksa_flags = 0; |
| 1235 | sigill_new.ksa_restorer = NULL; |
| 1236 | res = VG_(ksigemptyset)( &sigill_new.ksa_mask ); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1237 | sk_assert(res == 0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1238 | |
| 1239 | res = VG_(ksigaction)( VKI_SIGILL, &sigill_new, &sigill_saved ); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1240 | sk_assert(res == 0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* Trap for illegal instruction, in case it's a really old processor that |
| 1243 | * doesn't support CPUID. */ |
| 1244 | if (__builtin_setjmp(cpuid_jmpbuf) == 0) { |
| 1245 | cpuid(0, &level, (int*)&vendor_id[0], |
| 1246 | (int*)&vendor_id[8], (int*)&vendor_id[4]); |
| 1247 | vendor_id[12] = '\0'; |
| 1248 | |
| 1249 | /* Restore old SIGILL handler */ |
| 1250 | res = VG_(ksigaction)( VKI_SIGILL, &sigill_saved, NULL ); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1251 | sk_assert(res == 0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1252 | |
| 1253 | } else { |
| 1254 | VG_(message)(Vg_DebugMsg, "CPUID instruction not supported"); |
| 1255 | |
| 1256 | /* Restore old SIGILL handler */ |
| 1257 | res = VG_(ksigaction)( VKI_SIGILL, &sigill_saved, NULL ); |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1258 | sk_assert(res == 0); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1259 | return -1; |
| 1260 | } |
| 1261 | |
| 1262 | if (0 == level) { |
| 1263 | VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n"); |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */ |
| 1268 | if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) { |
| 1269 | ret = Intel_cache_info(level, I1c, D1c, L2c); |
| 1270 | |
| 1271 | } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) { |
| 1272 | ret = AMD_cache_info(I1c, D1c, L2c); |
| 1273 | |
| 1274 | } else { |
| 1275 | VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)", |
| 1276 | vendor_id); |
| 1277 | return -1; |
| 1278 | } |
| 1279 | |
| 1280 | /* Successful! Convert sizes from KB to bytes */ |
| 1281 | I1c->size *= 1024; |
| 1282 | D1c->size *= 1024; |
| 1283 | L2c->size *= 1024; |
| 1284 | |
| 1285 | return ret; |
| 1286 | } |
| 1287 | |
| 1288 | /* Checks cache config is ok; makes it so if not. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1289 | static |
| 1290 | void check_cache(cache_t* cache, cache_t* dflt, Char *name) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1291 | { |
| 1292 | /* First check they're all powers of two */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1293 | if (-1 == VG_(log2)(cache->size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1294 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1295 | "warning: %s size of %dB not a power of two; " |
| 1296 | "defaulting to %dB", name, cache->size, dflt->size); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1297 | cache->size = dflt->size; |
| 1298 | } |
| 1299 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1300 | if (-1 == VG_(log2)(cache->assoc)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1301 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1302 | "warning: %s associativity of %d not a power of two; " |
| 1303 | "defaulting to %d-way", name, cache->assoc, dflt->assoc); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1304 | cache->assoc = dflt->assoc; |
| 1305 | } |
| 1306 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1307 | if (-1 == VG_(log2)(cache->line_size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1308 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1309 | "warning: %s line size of %dB not a power of two; " |
| 1310 | "defaulting to %dB", |
| 1311 | name, cache->line_size, dflt->line_size); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1312 | cache->line_size = dflt->line_size; |
| 1313 | } |
| 1314 | |
| 1315 | /* Then check line size >= 16 -- any smaller and a single instruction could |
| 1316 | * straddle three cache lines, which breaks a simulation assertion and is |
| 1317 | * stupid anyway. */ |
| 1318 | if (cache->line_size < MIN_LINE_SIZE) { |
| 1319 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1320 | "warning: %s line size of %dB too small; " |
| 1321 | "increasing to %dB", name, cache->line_size, MIN_LINE_SIZE); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1322 | cache->line_size = MIN_LINE_SIZE; |
| 1323 | } |
| 1324 | |
| 1325 | /* Then check cache size > line size (causes seg faults if not). */ |
| 1326 | if (cache->size <= cache->line_size) { |
| 1327 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1328 | "warning: %s cache size of %dB <= line size of %dB; " |
| 1329 | "increasing to %dB", name, cache->size, cache->line_size, |
| 1330 | cache->line_size * 2); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1331 | cache->size = cache->line_size * 2; |
| 1332 | } |
| 1333 | |
| 1334 | /* Then check assoc <= (size / line size) (seg faults otherwise). */ |
| 1335 | if (cache->assoc > (cache->size / cache->line_size)) { |
| 1336 | VG_(message)(Vg_UserMsg, |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1337 | "warning: %s associativity > (size / line size); " |
| 1338 | "increasing size to %dB", |
| 1339 | name, cache->assoc * cache->line_size); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1340 | cache->size = cache->assoc * cache->line_size; |
| 1341 | } |
| 1342 | } |
| 1343 | |
| 1344 | /* On entry, args are undefined. Fill them with any info from the |
| 1345 | * command-line, then fill in any remaining with CPUID instruction if possible, |
| 1346 | * otherwise use defaults. Then check them and fix if not ok. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1347 | static |
| 1348 | void get_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1349 | { |
| 1350 | /* Defaults are for a model 3 or 4 Athlon */ |
| 1351 | cache_t I1_dflt = (cache_t) { 65536, 2, 64 }; |
| 1352 | cache_t D1_dflt = (cache_t) { 65536, 2, 64 }; |
| 1353 | cache_t L2_dflt = (cache_t) { 262144, 8, 64 }; |
| 1354 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1355 | #define CMD_LINE_DEFINED(L) \ |
| 1356 | (-1 != clo_##L##_cache.size || \ |
| 1357 | -1 != clo_##L##_cache.assoc || \ |
| 1358 | -1 != clo_##L##_cache.line_size) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1359 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1360 | *I1c = clo_I1_cache; |
| 1361 | *D1c = clo_D1_cache; |
| 1362 | *L2c = clo_L2_cache; |
sewardj | b1a77a4 | 2002-07-13 13:31:20 +0000 | [diff] [blame] | 1363 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1364 | /* If any undefined on command-line, try CPUID */ |
| 1365 | if (! CMD_LINE_DEFINED(I1) || |
| 1366 | ! CMD_LINE_DEFINED(D1) || |
| 1367 | ! CMD_LINE_DEFINED(L2)) { |
| 1368 | |
| 1369 | /* Overwrite CPUID result for any cache defined on command-line */ |
| 1370 | if (0 == get_caches_from_CPUID(I1c, D1c, L2c)) { |
| 1371 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1372 | if (CMD_LINE_DEFINED(I1)) *I1c = clo_I1_cache; |
| 1373 | if (CMD_LINE_DEFINED(D1)) *D1c = clo_D1_cache; |
| 1374 | if (CMD_LINE_DEFINED(L2)) *L2c = clo_L2_cache; |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1375 | |
| 1376 | /* CPUID failed, use defaults for each undefined by command-line */ |
| 1377 | } else { |
| 1378 | VG_(message)(Vg_DebugMsg, |
| 1379 | "Couldn't detect cache configuration, using one " |
| 1380 | "or more defaults "); |
| 1381 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1382 | *I1c = (CMD_LINE_DEFINED(I1) ? clo_I1_cache : I1_dflt); |
| 1383 | *D1c = (CMD_LINE_DEFINED(D1) ? clo_D1_cache : D1_dflt); |
| 1384 | *L2c = (CMD_LINE_DEFINED(L2) ? clo_L2_cache : L2_dflt); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1385 | } |
| 1386 | } |
| 1387 | #undef CMD_LINE_DEFINED |
| 1388 | |
| 1389 | check_cache(I1c, &I1_dflt, "I1"); |
| 1390 | check_cache(D1c, &D1_dflt, "D1"); |
| 1391 | check_cache(L2c, &L2_dflt, "L2"); |
| 1392 | |
| 1393 | if (VG_(clo_verbosity) > 1) { |
| 1394 | VG_(message)(Vg_UserMsg, "Cache configuration used:"); |
| 1395 | VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines", |
| 1396 | I1c->size, I1c->assoc, I1c->line_size); |
| 1397 | VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines", |
| 1398 | D1c->size, D1c->assoc, D1c->line_size); |
| 1399 | VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines", |
| 1400 | L2c->size, L2c->assoc, L2c->line_size); |
| 1401 | } |
| 1402 | } |
| 1403 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1404 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1405 | /*--- SK_(fini)() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1406 | /*------------------------------------------------------------*/ |
| 1407 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1408 | static void fprint_BBCC(Int fd, BBCC* BBCC_node, Char *first_instr_fl, |
| 1409 | Char *first_instr_fn) |
| 1410 | { |
| 1411 | Addr BBCC_ptr0, BBCC_ptr; |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1412 | Char buf[BUF_LEN], curr_file[BUF_LEN], |
| 1413 | fbuf[BUF_LEN+4], lbuf[LINE_BUF_LEN]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1414 | UInt line_num; |
| 1415 | |
| 1416 | BBCC_ptr0 = BBCC_ptr = (Addr)(BBCC_node->array); |
| 1417 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1418 | /* Mark start of basic block in output, just to ease debugging */ |
| 1419 | VG_(write)(fd, (void*)"\n", 1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1420 | |
| 1421 | VG_(strcpy)(curr_file, first_instr_fl); |
| 1422 | |
| 1423 | while (BBCC_ptr - BBCC_ptr0 < BBCC_node->array_size) { |
| 1424 | |
| 1425 | /* We pretend the CC is an iCC for getting the tag. This is ok |
| 1426 | * because both CC types have tag as their first byte. Once we know |
| 1427 | * the type, we can cast and act appropriately. */ |
| 1428 | |
| 1429 | Char fl_buf[FILENAME_LEN]; |
| 1430 | Char fn_buf[FN_NAME_LEN]; |
| 1431 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1432 | Addr instr_addr; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1433 | switch ( ((iCC*)BBCC_ptr)->tag ) { |
| 1434 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1435 | case InstrCC: |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1436 | instr_addr = ((iCC*)BBCC_ptr)->instr_addr; |
| 1437 | sprint_iCC(buf, (iCC*)BBCC_ptr); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1438 | ADD_CC_TO(iCC, I, Ir_total); |
| 1439 | BBCC_ptr += sizeof(iCC); |
| 1440 | break; |
| 1441 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1442 | case ReadCC: |
| 1443 | case ModCC: |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1444 | instr_addr = ((idCC*)BBCC_ptr)->instr_addr; |
| 1445 | sprint_read_or_mod_CC(buf, (idCC*)BBCC_ptr); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1446 | ADD_CC_TO(idCC, I, Ir_total); |
| 1447 | ADD_CC_TO(idCC, D, Dr_total); |
| 1448 | BBCC_ptr += sizeof(idCC); |
| 1449 | break; |
| 1450 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1451 | case WriteCC: |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1452 | instr_addr = ((idCC*)BBCC_ptr)->instr_addr; |
| 1453 | sprint_write_CC(buf, (idCC*)BBCC_ptr); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1454 | ADD_CC_TO(idCC, I, Ir_total); |
| 1455 | ADD_CC_TO(idCC, D, Dw_total); |
| 1456 | BBCC_ptr += sizeof(idCC); |
| 1457 | break; |
| 1458 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1459 | case ReadWriteCC: |
| 1460 | instr_addr = ((iddCC*)BBCC_ptr)->instr_addr; |
| 1461 | sprint_read_write_CC(buf, (iddCC*)BBCC_ptr); |
| 1462 | ADD_CC_TO(iddCC, I, Ir_total); |
| 1463 | ADD_CC_TO(iddCC, Da, Dr_total); |
| 1464 | ADD_CC_TO(iddCC, Db, Dw_total); |
| 1465 | BBCC_ptr += sizeof(iddCC); |
| 1466 | break; |
| 1467 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1468 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1469 | VG_(skin_panic)("Unknown CC type in fprint_BBCC()\n"); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | } |
| 1472 | distinct_instrs++; |
| 1473 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1474 | get_debug_info(instr_addr, fl_buf, fn_buf, &line_num); |
| 1475 | |
| 1476 | /* Allow for filename switching in the middle of a BB; if this happens, |
| 1477 | * must print the new filename with the function name. */ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1478 | if (0 != VG_(strcmp)(fl_buf, curr_file)) { |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1479 | VG_(strcpy)(curr_file, fl_buf); |
| 1480 | VG_(sprintf)(fbuf, "fi=%s\n", curr_file); |
| 1481 | VG_(write)(fd, (void*)fbuf, VG_(strlen)(fbuf)); |
| 1482 | } |
| 1483 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1484 | /* If the function name for this instruction doesn't match that of the |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1485 | * first instruction in the BB, print warning. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1486 | if (VG_(clo_verbosity > 2) && 0 != VG_(strcmp)(fn_buf, first_instr_fn)) { |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1487 | VG_(printf)("Mismatched function names\n"); |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1488 | VG_(printf)(" filenames: BB:%s, instr:%s;" |
| 1489 | " fn_names: BB:%s, instr:%s;" |
| 1490 | " line: %d\n", |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1491 | first_instr_fl, fl_buf, |
| 1492 | first_instr_fn, fn_buf, |
| 1493 | line_num); |
| 1494 | } |
| 1495 | |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1496 | VG_(sprintf)(lbuf, "%u ", line_num); |
| 1497 | VG_(write)(fd, (void*)lbuf, VG_(strlen)(lbuf)); /* line number */ |
| 1498 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); /* cost centre */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1499 | } |
| 1500 | /* If we switched filenames in the middle of the BB without switching back, |
| 1501 | * switch back now because the subsequent BB may be relying on falling under |
| 1502 | * the original file name. */ |
| 1503 | if (0 != VG_(strcmp)(first_instr_fl, curr_file)) { |
| 1504 | VG_(sprintf)(fbuf, "fe=%s\n", first_instr_fl); |
| 1505 | VG_(write)(fd, (void*)fbuf, VG_(strlen)(fbuf)); |
| 1506 | } |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1507 | |
| 1508 | /* Mark end of basic block */ |
| 1509 | /* VG_(write)(fd, (void*)"#}\n", 3); */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1510 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1511 | sk_assert(BBCC_ptr - BBCC_ptr0 == BBCC_node->array_size); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1514 | static void fprint_BBCC_table_and_calc_totals(void) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1515 | { |
| 1516 | Int fd; |
| 1517 | Char buf[BUF_LEN]; |
| 1518 | file_node *curr_file_node; |
| 1519 | fn_node *curr_fn_node; |
| 1520 | BBCC *curr_BBCC; |
| 1521 | Int i,j,k; |
| 1522 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1523 | VGP_PUSHCC(VgpCacheResults); |
| 1524 | fd = VG_(open)(cachegrind_out_file, VKI_O_WRONLY|VKI_O_TRUNC, 0); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1525 | if (-1 == fd) { file_err(); } |
| 1526 | |
| 1527 | /* "desc:" lines (giving I1/D1/L2 cache configuration) */ |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1528 | VG_(sprintf)(buf, "desc: I1 cache: %s\n", I1.desc_line); |
| 1529 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1530 | VG_(sprintf)(buf, "desc: D1 cache: %s\n", D1.desc_line); |
| 1531 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1532 | VG_(sprintf)(buf, "desc: L2 cache: %s\n", L2.desc_line); |
| 1533 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1534 | |
| 1535 | /* "cmd:" line */ |
| 1536 | VG_(strcpy)(buf, "cmd:"); |
| 1537 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1538 | for (i = 0; i < VG_(client_argc); i++) { |
| 1539 | VG_(sprintf)(buf, " %s", VG_(client_argv)[i]); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1540 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1541 | } |
| 1542 | /* "events:" line */ |
| 1543 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw\n"); |
| 1544 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1545 | |
| 1546 | /* Six loops here: three for the hash table arrays, and three for the |
| 1547 | * chains hanging off the hash table arrays. */ |
| 1548 | for (i = 0; i < N_FILE_ENTRIES; i++) { |
| 1549 | curr_file_node = BBCC_table[i]; |
| 1550 | while (curr_file_node != NULL) { |
| 1551 | VG_(sprintf)(buf, "fl=%s\n", curr_file_node->filename); |
| 1552 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1553 | |
| 1554 | for (j = 0; j < N_FN_ENTRIES; j++) { |
| 1555 | curr_fn_node = curr_file_node->fns[j]; |
| 1556 | while (curr_fn_node != NULL) { |
| 1557 | VG_(sprintf)(buf, "fn=%s\n", curr_fn_node->fn_name); |
| 1558 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1559 | |
| 1560 | for (k = 0; k < N_BBCC_ENTRIES; k++) { |
| 1561 | curr_BBCC = curr_fn_node->BBCCs[k]; |
| 1562 | while (curr_BBCC != NULL) { |
| 1563 | fprint_BBCC(fd, curr_BBCC, |
| 1564 | |
| 1565 | curr_file_node->filename, |
| 1566 | curr_fn_node->fn_name); |
| 1567 | |
| 1568 | curr_BBCC = curr_BBCC->next; |
| 1569 | } |
| 1570 | } |
| 1571 | curr_fn_node = curr_fn_node->next; |
| 1572 | } |
| 1573 | } |
| 1574 | curr_file_node = curr_file_node->next; |
| 1575 | } |
| 1576 | } |
| 1577 | |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1578 | /* Print stats from any discarded basic blocks */ |
| 1579 | if (0 != Ir_discards.a) { |
| 1580 | |
| 1581 | VG_(sprintf)(buf, "fl=(discarded)\n"); |
| 1582 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1583 | VG_(sprintf)(buf, "fn=(discarded)\n"); |
| 1584 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1585 | |
| 1586 | /* Use 0 as line number */ |
| 1587 | VG_(sprintf)(buf, "0 %llu %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 1588 | Ir_discards.a, Ir_discards.m1, Ir_discards.m2, |
| 1589 | Dr_discards.a, Dr_discards.m1, Dr_discards.m2, |
| 1590 | Dw_discards.a, Dw_discards.m1, Dw_discards.m2); |
| 1591 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1592 | |
| 1593 | Ir_total.a += Ir_discards.a; |
| 1594 | Ir_total.m1 += Ir_discards.m1; |
| 1595 | Ir_total.m2 += Ir_discards.m2; |
| 1596 | Dr_total.a += Dr_discards.a; |
| 1597 | Dr_total.m1 += Dr_discards.m1; |
| 1598 | Dr_total.m2 += Dr_discards.m2; |
| 1599 | Dw_total.a += Dw_discards.a; |
| 1600 | Dw_total.m1 += Dw_discards.m1; |
| 1601 | Dw_total.m2 += Dw_discards.m2; |
| 1602 | } |
| 1603 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1604 | /* Summary stats must come after rest of table, since we calculate them |
| 1605 | * during traversal. */ |
| 1606 | VG_(sprintf)(buf, "summary: " |
| 1607 | "%llu %llu %llu " |
| 1608 | "%llu %llu %llu " |
| 1609 | "%llu %llu %llu\n", |
| 1610 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 1611 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 1612 | Dw_total.a, Dw_total.m1, Dw_total.m2); |
| 1613 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1614 | VG_(close)(fd); |
| 1615 | } |
| 1616 | |
| 1617 | /* Adds commas to ULong, right justifying in a field field_width wide, returns |
| 1618 | * the string in buf. */ |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 1619 | static |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1620 | Int commify(ULong n, int field_width, char buf[COMMIFY_BUF_LEN]) |
| 1621 | { |
| 1622 | int len, n_commas, i, j, new_len, space; |
| 1623 | |
| 1624 | VG_(sprintf)(buf, "%lu", n); |
| 1625 | len = VG_(strlen)(buf); |
| 1626 | n_commas = (len - 1) / 3; |
| 1627 | new_len = len + n_commas; |
| 1628 | space = field_width - new_len; |
| 1629 | |
| 1630 | /* Allow for printing a number in a field_width smaller than it's size */ |
| 1631 | if (space < 0) space = 0; |
| 1632 | |
| 1633 | /* Make j = -1 because we copy the '\0' before doing the numbers in groups |
| 1634 | * of three. */ |
| 1635 | for (j = -1, i = len ; i >= 0; i--) { |
| 1636 | buf[i + n_commas + space] = buf[i]; |
| 1637 | |
| 1638 | if (3 == ++j) { |
| 1639 | j = 0; |
| 1640 | n_commas--; |
| 1641 | buf[i + n_commas + space] = ','; |
| 1642 | } |
| 1643 | } |
| 1644 | /* Right justify in field. */ |
| 1645 | for (i = 0; i < space; i++) buf[i] = ' '; |
| 1646 | return new_len; |
| 1647 | } |
| 1648 | |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 1649 | static |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1650 | void percentify(Int n, Int pow, Int field_width, char buf[]) |
| 1651 | { |
| 1652 | int i, len, space; |
| 1653 | |
| 1654 | VG_(sprintf)(buf, "%d.%d%%", n / pow, n % pow); |
| 1655 | len = VG_(strlen)(buf); |
| 1656 | space = field_width - len; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1657 | if (space < 0) space = 0; /* Allow for v. small field_width */ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1658 | i = len; |
| 1659 | |
| 1660 | /* Right justify in field */ |
| 1661 | for ( ; i >= 0; i--) buf[i + space] = buf[i]; |
| 1662 | for (i = 0; i < space; i++) buf[i] = ' '; |
| 1663 | } |
| 1664 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1665 | void SK_(fini)(void) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1666 | { |
| 1667 | CC D_total; |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1668 | ULong L2_total_m, L2_total_mr, L2_total_mw, |
| 1669 | L2_total, L2_total_r, L2_total_w; |
njn | e0ee071 | 2002-05-03 16:41:05 +0000 | [diff] [blame] | 1670 | char buf1[RESULTS_BUF_LEN], |
| 1671 | buf2[RESULTS_BUF_LEN], |
| 1672 | buf3[RESULTS_BUF_LEN]; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1673 | Int l1, l2, l3; |
| 1674 | Int p; |
| 1675 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1676 | fprint_BBCC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1677 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1678 | if (VG_(clo_verbosity) == 0) |
| 1679 | return; |
| 1680 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1681 | /* I cache results. Use the I_refs value to determine the first column |
| 1682 | * width. */ |
| 1683 | l1 = commify(Ir_total.a, 0, buf1); |
| 1684 | VG_(message)(Vg_UserMsg, "I refs: %s", buf1); |
| 1685 | |
| 1686 | commify(Ir_total.m1, l1, buf1); |
| 1687 | VG_(message)(Vg_UserMsg, "I1 misses: %s", buf1); |
| 1688 | |
| 1689 | commify(Ir_total.m2, l1, buf1); |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 1690 | VG_(message)(Vg_UserMsg, "L2i misses: %s", buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1691 | |
| 1692 | p = 100; |
| 1693 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1694 | if (0 == Ir_total.a) Ir_total.a = 1; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1695 | percentify(Ir_total.m1 * 100 * p / Ir_total.a, p, l1+1, buf1); |
| 1696 | VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); |
| 1697 | |
| 1698 | percentify(Ir_total.m2 * 100 * p / Ir_total.a, p, l1+1, buf1); |
| 1699 | VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); |
| 1700 | VG_(message)(Vg_UserMsg, ""); |
| 1701 | |
| 1702 | /* D cache results. Use the D_refs.rd and D_refs.wr values to determine the |
| 1703 | * width of columns 2 & 3. */ |
| 1704 | D_total.a = Dr_total.a + Dw_total.a; |
| 1705 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
| 1706 | D_total.m2 = Dr_total.m2 + Dw_total.m2; |
| 1707 | |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1708 | commify( D_total.a, l1, buf1); |
| 1709 | l2 = commify(Dr_total.a, 0, buf2); |
| 1710 | l3 = commify(Dw_total.a, 0, buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1711 | VG_(message)(Vg_UserMsg, "D refs: %s (%s rd + %s wr)", |
| 1712 | buf1, buf2, buf3); |
| 1713 | |
| 1714 | commify( D_total.m1, l1, buf1); |
| 1715 | commify(Dr_total.m1, l2, buf2); |
| 1716 | commify(Dw_total.m1, l3, buf3); |
| 1717 | VG_(message)(Vg_UserMsg, "D1 misses: %s (%s rd + %s wr)", |
| 1718 | buf1, buf2, buf3); |
| 1719 | |
| 1720 | commify( D_total.m2, l1, buf1); |
| 1721 | commify(Dr_total.m2, l2, buf2); |
| 1722 | commify(Dw_total.m2, l3, buf3); |
njn | 95114da | 2002-06-05 09:39:31 +0000 | [diff] [blame] | 1723 | VG_(message)(Vg_UserMsg, "L2d misses: %s (%s rd + %s wr)", |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1724 | buf1, buf2, buf3); |
| 1725 | |
| 1726 | p = 10; |
| 1727 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1728 | if (0 == D_total.a) D_total.a = 1; |
| 1729 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1730 | if (0 == Dw_total.a) Dw_total.a = 1; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1731 | percentify( D_total.m1 * 100 * p / D_total.a, p, l1+1, buf1); |
| 1732 | percentify(Dr_total.m1 * 100 * p / Dr_total.a, p, l2+1, buf2); |
| 1733 | percentify(Dw_total.m1 * 100 * p / Dw_total.a, p, l3+1, buf3); |
| 1734 | VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1735 | |
| 1736 | percentify( D_total.m2 * 100 * p / D_total.a, p, l1+1, buf1); |
| 1737 | percentify(Dr_total.m2 * 100 * p / Dr_total.a, p, l2+1, buf2); |
| 1738 | percentify(Dw_total.m2 * 100 * p / Dw_total.a, p, l3+1, buf3); |
| 1739 | VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1740 | VG_(message)(Vg_UserMsg, ""); |
| 1741 | |
| 1742 | /* L2 overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1743 | |
| 1744 | L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1745 | L2_total_r = Dr_total.m1 + Ir_total.m1; |
| 1746 | L2_total_w = Dw_total.m1; |
| 1747 | commify(L2_total, l1, buf1); |
| 1748 | commify(L2_total_r, l2, buf2); |
| 1749 | commify(L2_total_w, l3, buf3); |
| 1750 | VG_(message)(Vg_UserMsg, "L2 refs: %s (%s rd + %s wr)", |
| 1751 | buf1, buf2, buf3); |
| 1752 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1753 | L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2; |
| 1754 | L2_total_mr = Dr_total.m2 + Ir_total.m2; |
| 1755 | L2_total_mw = Dw_total.m2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1756 | commify(L2_total_m, l1, buf1); |
| 1757 | commify(L2_total_mr, l2, buf2); |
| 1758 | commify(L2_total_mw, l3, buf3); |
| 1759 | VG_(message)(Vg_UserMsg, "L2 misses: %s (%s rd + %s wr)", |
| 1760 | buf1, buf2, buf3); |
| 1761 | |
| 1762 | percentify(L2_total_m * 100 * p / (Ir_total.a + D_total.a), p, l1+1, buf1); |
| 1763 | percentify(L2_total_mr * 100 * p / (Ir_total.a + Dr_total.a), p, l2+1, buf2); |
| 1764 | percentify(L2_total_mw * 100 * p / Dw_total.a, p, l3+1, buf3); |
| 1765 | VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1766 | |
| 1767 | |
| 1768 | /* Hash table stats */ |
| 1769 | if (VG_(clo_verbosity) > 1) { |
| 1770 | int BB_lookups = full_debug_BBs + fn_name_debug_BBs + |
| 1771 | file_line_debug_BBs + no_debug_BBs; |
| 1772 | |
| 1773 | VG_(message)(Vg_DebugMsg, ""); |
| 1774 | VG_(message)(Vg_DebugMsg, "Distinct files: %d", distinct_files); |
| 1775 | VG_(message)(Vg_DebugMsg, "Distinct fns: %d", distinct_fns); |
| 1776 | VG_(message)(Vg_DebugMsg, "BB lookups: %d", BB_lookups); |
| 1777 | VG_(message)(Vg_DebugMsg, "With full debug info:%3d%% (%d)", |
| 1778 | full_debug_BBs * 100 / BB_lookups, |
| 1779 | full_debug_BBs); |
| 1780 | VG_(message)(Vg_DebugMsg, "With file/line debug info:%3d%% (%d)", |
| 1781 | file_line_debug_BBs * 100 / BB_lookups, |
| 1782 | file_line_debug_BBs); |
| 1783 | VG_(message)(Vg_DebugMsg, "With fn name debug info:%3d%% (%d)", |
| 1784 | fn_name_debug_BBs * 100 / BB_lookups, |
| 1785 | fn_name_debug_BBs); |
| 1786 | VG_(message)(Vg_DebugMsg, "With no debug info:%3d%% (%d)", |
| 1787 | no_debug_BBs * 100 / BB_lookups, |
| 1788 | no_debug_BBs); |
| 1789 | VG_(message)(Vg_DebugMsg, "BBs Retranslated: %d", BB_retranslations); |
| 1790 | VG_(message)(Vg_DebugMsg, "Distinct instrs: %d", distinct_instrs); |
| 1791 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1792 | VGP_POPCC(VgpCacheResults); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1795 | |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1796 | /* Called when a translation is invalidated due to self-modifying code or |
| 1797 | * unloaded of a shared object. |
| 1798 | * |
| 1799 | * Finds the BBCC in the table, removes it, adds the counts to the discard |
| 1800 | * counters, and then frees the BBCC. */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1801 | void SK_(discard_basic_block_info) ( Addr a, UInt size ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1802 | { |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1803 | BBCC *BBCC_node; |
| 1804 | Addr BBCC_ptr0, BBCC_ptr; |
| 1805 | Bool BB_seen_before; |
| 1806 | |
sewardj | 83205b3 | 2002-06-14 11:08:07 +0000 | [diff] [blame] | 1807 | if (0) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1808 | VG_(printf)( "discard_basic_block_info: addr %p, size %u\n", a, size); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1809 | |
| 1810 | /* 2nd arg won't be used since BB should have been seen before (assertions |
| 1811 | * ensure this). */ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1812 | BBCC_node = get_BBCC(a, NULL, /*remove=*/True, &BB_seen_before); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1813 | BBCC_ptr0 = BBCC_ptr = (Addr)(BBCC_node->array); |
| 1814 | |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1815 | sk_assert(True == BB_seen_before); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1816 | |
| 1817 | while (BBCC_ptr - BBCC_ptr0 < BBCC_node->array_size) { |
| 1818 | |
| 1819 | /* We pretend the CC is an iCC for getting the tag. This is ok |
| 1820 | * because both CC types have tag as their first byte. Once we know |
| 1821 | * the type, we can cast and act appropriately. */ |
| 1822 | |
| 1823 | switch ( ((iCC*)BBCC_ptr)->tag ) { |
| 1824 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1825 | case InstrCC: |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1826 | ADD_CC_TO(iCC, I, Ir_discards); |
| 1827 | BBCC_ptr += sizeof(iCC); |
| 1828 | break; |
| 1829 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1830 | case ReadCC: |
| 1831 | case ModCC: |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1832 | ADD_CC_TO(idCC, I, Ir_discards); |
| 1833 | ADD_CC_TO(idCC, D, Dr_discards); |
| 1834 | BBCC_ptr += sizeof(idCC); |
| 1835 | break; |
| 1836 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1837 | case WriteCC: |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1838 | ADD_CC_TO(idCC, I, Ir_discards); |
| 1839 | ADD_CC_TO(idCC, D, Dw_discards); |
| 1840 | BBCC_ptr += sizeof(idCC); |
| 1841 | break; |
| 1842 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1843 | case ReadWriteCC: |
| 1844 | ADD_CC_TO(iddCC, I, Ir_discards); |
| 1845 | ADD_CC_TO(iddCC, Da, Dr_discards); |
| 1846 | ADD_CC_TO(iddCC, Db, Dw_discards); |
| 1847 | BBCC_ptr += sizeof(iddCC); |
| 1848 | break; |
| 1849 | |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1850 | default: |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1851 | VG_(skin_panic)("Unknown CC type in VG_(discard_basic_block_info)()\n"); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1852 | break; |
| 1853 | } |
| 1854 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1855 | VG_(free)(BBCC_node); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1859 | /*--- Command line processing ---*/ |
| 1860 | /*--------------------------------------------------------------------*/ |
| 1861 | |
| 1862 | static void parse_cache_opt ( cache_t* cache, char* orig_opt, int opt_len ) |
| 1863 | { |
| 1864 | int i1, i2, i3; |
| 1865 | int i; |
| 1866 | char *opt = VG_(strdup)(orig_opt); |
| 1867 | |
| 1868 | i = i1 = opt_len; |
| 1869 | |
| 1870 | /* Option looks like "--I1=65536,2,64". |
| 1871 | * Find commas, replace with NULs to make three independent |
| 1872 | * strings, then extract numbers. Yuck. */ |
| 1873 | while (VG_(isdigit)(opt[i])) i++; |
| 1874 | if (',' == opt[i]) { |
| 1875 | opt[i++] = '\0'; |
| 1876 | i2 = i; |
| 1877 | } else goto bad; |
| 1878 | while (VG_(isdigit)(opt[i])) i++; |
| 1879 | if (',' == opt[i]) { |
| 1880 | opt[i++] = '\0'; |
| 1881 | i3 = i; |
| 1882 | } else goto bad; |
| 1883 | while (VG_(isdigit)(opt[i])) i++; |
| 1884 | if ('\0' != opt[i]) goto bad; |
| 1885 | |
| 1886 | cache->size = (Int)VG_(atoll)(opt + i1); |
| 1887 | cache->assoc = (Int)VG_(atoll)(opt + i2); |
| 1888 | cache->line_size = (Int)VG_(atoll)(opt + i3); |
| 1889 | |
| 1890 | VG_(free)(opt); |
| 1891 | |
| 1892 | return; |
| 1893 | |
| 1894 | bad: |
| 1895 | VG_(bad_option)(orig_opt); |
| 1896 | } |
| 1897 | |
| 1898 | Bool SK_(process_cmd_line_option)(Char* arg) |
| 1899 | { |
| 1900 | /* 5 is length of "--I1=" */ |
| 1901 | if (0 == VG_(strncmp)(arg, "--I1=", 5)) |
| 1902 | parse_cache_opt(&clo_I1_cache, arg, 5); |
| 1903 | else if (0 == VG_(strncmp)(arg, "--D1=", 5)) |
| 1904 | parse_cache_opt(&clo_D1_cache, arg, 5); |
| 1905 | else if (0 == VG_(strncmp)(arg, "--L2=", 5)) |
| 1906 | parse_cache_opt(&clo_L2_cache, arg, 5); |
| 1907 | else |
| 1908 | return False; |
| 1909 | |
| 1910 | return True; |
| 1911 | } |
| 1912 | |
| 1913 | Char* SK_(usage)(void) |
| 1914 | { |
| 1915 | return |
| 1916 | " --I1=<size>,<assoc>,<line_size> set I1 cache manually\n" |
| 1917 | " --D1=<size>,<assoc>,<line_size> set D1 cache manually\n" |
| 1918 | " --L2=<size>,<assoc>,<line_size> set L2 cache manually\n"; |
| 1919 | } |
| 1920 | |
| 1921 | /*--------------------------------------------------------------------*/ |
| 1922 | /*--- Setup ---*/ |
| 1923 | /*--------------------------------------------------------------------*/ |
| 1924 | |
| 1925 | void SK_(pre_clo_init)(VgNeeds* needs, VgTrackEvents* not_used) |
| 1926 | { |
| 1927 | needs->name = "cachegrind"; |
| 1928 | needs->description = "an I1/D1/L2 cache profiler"; |
njn | e427a66 | 2002-10-02 11:08:25 +0000 | [diff] [blame^] | 1929 | needs->bug_reports_to = "njn25@cam.ac.uk"; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1930 | |
| 1931 | needs->basic_block_discards = True; |
| 1932 | needs->command_line_options = True; |
| 1933 | |
| 1934 | VG_(register_compact_helper)((Addr) & log_1I_0D_cache_access); |
| 1935 | VG_(register_compact_helper)((Addr) & log_1I_0D_cache_access_JIFZ); |
| 1936 | VG_(register_compact_helper)((Addr) & log_0I_1D_cache_access); |
| 1937 | VG_(register_compact_helper)((Addr) & log_1I_1D_cache_access); |
| 1938 | VG_(register_compact_helper)((Addr) & log_0I_2D_cache_access); |
| 1939 | VG_(register_compact_helper)((Addr) & log_1I_2D_cache_access); |
| 1940 | } |
| 1941 | |
| 1942 | void SK_(post_clo_init)(void) |
| 1943 | { |
| 1944 | cache_t I1c, D1c, L2c; |
| 1945 | Int fd; |
| 1946 | |
| 1947 | /* Set output file name: cachegrind.<pid>.out */ |
| 1948 | VG_(sprintf)(cachegrind_out_file, "cachegrind.out.%d", VG_(getpid)()); |
| 1949 | |
| 1950 | /* Make sure the output file can be written. */ |
| 1951 | fd = VG_(open)(cachegrind_out_file, VKI_O_WRONLY|VKI_O_TRUNC, 0); |
| 1952 | if (-1 == fd) { |
| 1953 | fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_WRONLY, |
| 1954 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1955 | if (-1 == fd) { |
| 1956 | file_err(); |
| 1957 | } |
| 1958 | } |
| 1959 | VG_(close)(fd); |
| 1960 | |
| 1961 | initCC(&Ir_total); |
| 1962 | initCC(&Dr_total); |
| 1963 | initCC(&Dw_total); |
| 1964 | |
| 1965 | initCC(&Ir_discards); |
| 1966 | initCC(&Dr_discards); |
| 1967 | initCC(&Dw_discards); |
| 1968 | |
| 1969 | get_caches(&I1c, &D1c, &L2c); |
| 1970 | |
| 1971 | cachesim_I1_initcache(I1c); |
| 1972 | cachesim_D1_initcache(D1c); |
| 1973 | cachesim_L2_initcache(L2c); |
| 1974 | |
| 1975 | VGP_(register_profile_event)(VgpGetBBCC, "get-BBCC"); |
| 1976 | VGP_(register_profile_event)(VgpCacheSimulate, "cache-simulate"); |
| 1977 | VGP_(register_profile_event)(VgpCacheResults, "cache-results"); |
| 1978 | |
| 1979 | init_BBCC_table(); |
| 1980 | } |
| 1981 | |
| 1982 | #if 0 |
| 1983 | Bool SK_(cheap_sanity_check)(void) { return True; } |
| 1984 | |
| 1985 | extern TTEntry* vg_tt; |
| 1986 | |
| 1987 | Bool SK_(expensive_sanity_check)(void) |
| 1988 | { |
| 1989 | Int i; |
| 1990 | Bool dummy; |
| 1991 | for (i = 0; i < 200191; i++) { |
| 1992 | if (vg_tt[i].orig_addr != (Addr)1 && |
| 1993 | vg_tt[i].orig_addr != (Addr)3) { |
| 1994 | VG_(printf)("."); |
| 1995 | get_BBCC(vg_tt[i].orig_addr, NULL, /*remove=*/True, &dummy); |
| 1996 | } |
| 1997 | } |
| 1998 | return True; |
| 1999 | } |
| 2000 | #endif |
| 2001 | |
| 2002 | /*--------------------------------------------------------------------*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 2003 | /*--- end cg_main.c ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 2004 | /*--------------------------------------------------------------------*/ |