sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
njn | 101e572 | 2005-04-21 02:37:54 +0000 | [diff] [blame] | 3 | /*--- Cachegrind: everything but the simulation itself. ---*/ |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 4 | /*--- cg_main.c ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of Cachegrind, a Valgrind tool for cache |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 9 | profiling programs. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 10 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 11 | Copyright (C) 2002-2007 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 12 | njn@valgrind.org |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 33 | #include "pub_tool_vki.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 34 | #include "pub_tool_debuginfo.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcproc.h" |
njn | f536bbb | 2005-06-13 04:21:38 +0000 | [diff] [blame] | 40 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 41 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 42 | #include "pub_tool_options.h" |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 43 | #include "pub_tool_oset.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 44 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 45 | #include "pub_tool_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 46 | #include "pub_tool_clientstate.h" |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 47 | #include "pub_tool_machine.h" // VG_(fnptr_to_fnentry) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 48 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 49 | #include "cg_arch.h" |
nethercote | 27fc1da | 2004-01-04 16:56:57 +0000 | [diff] [blame] | 50 | #include "cg_sim.c" |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 51 | #include "cg_branchpred.c" |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 52 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 53 | /*------------------------------------------------------------*/ |
| 54 | /*--- Constants ---*/ |
| 55 | /*------------------------------------------------------------*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 56 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 57 | /* Set to 1 for very verbose debugging */ |
| 58 | #define DEBUG_CG 0 |
| 59 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 60 | #define MIN_LINE_SIZE 16 |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 61 | #define FILE_LEN VKI_PATH_MAX |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 62 | #define FN_LEN 256 |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 63 | |
| 64 | /*------------------------------------------------------------*/ |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 65 | /*--- Options ---*/ |
| 66 | /*------------------------------------------------------------*/ |
| 67 | |
| 68 | static Bool clo_cache_sim = True; /* do cache simulation? */ |
| 69 | static Bool clo_branch_sim = False; /* do branch simulation? */ |
| 70 | |
| 71 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 72 | /*--- Types and Data Structures ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 73 | /*------------------------------------------------------------*/ |
| 74 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 75 | typedef |
| 76 | struct { |
| 77 | ULong a; /* total # memory accesses of this kind */ |
| 78 | ULong m1; /* misses in the first level cache */ |
| 79 | ULong m2; /* misses in the second level cache */ |
| 80 | } |
| 81 | CacheCC; |
| 82 | |
| 83 | typedef |
| 84 | struct { |
| 85 | ULong b; /* total # branches of this kind */ |
| 86 | ULong mp; /* number of branches mispredicted */ |
| 87 | } |
| 88 | BranchCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 89 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 90 | //------------------------------------------------------------ |
| 91 | // Primary data structure #1: CC table |
| 92 | // - Holds the per-source-line hit/miss stats, grouped by file/function/line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 93 | // - an ordered set of CCs. CC indexing done by file/function/line (as |
| 94 | // determined from the instrAddr). |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 95 | // - Traversed for dumping stats at end in file/func/line hierarchy. |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 96 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 97 | typedef struct { |
| 98 | Char* file; |
| 99 | Char* fn; |
| 100 | Int line; |
| 101 | } |
| 102 | CodeLoc; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 103 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 104 | typedef struct { |
| 105 | CodeLoc loc; /* Source location that these counts pertain to */ |
| 106 | CacheCC Ir; /* Insn read counts */ |
| 107 | CacheCC Dr; /* Data read counts */ |
| 108 | CacheCC Dw; /* Data write/modify counts */ |
| 109 | BranchCC Bc; /* Conditional branch counts */ |
| 110 | BranchCC Bi; /* Indirect branch counts */ |
| 111 | } LineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 112 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 113 | // First compare file, then fn, then line. |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 114 | static Word cmp_CodeLoc_LineCC(void *vloc, void *vcc) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 115 | { |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 116 | Word res; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 117 | CodeLoc* a = (CodeLoc*)vloc; |
| 118 | CodeLoc* b = &(((LineCC*)vcc)->loc); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 119 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 120 | res = VG_(strcmp)(a->file, b->file); |
| 121 | if (0 != res) |
| 122 | return res; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 123 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 124 | res = VG_(strcmp)(a->fn, b->fn); |
| 125 | if (0 != res) |
| 126 | return res; |
| 127 | |
| 128 | return a->line - b->line; |
| 129 | } |
| 130 | |
| 131 | static OSet* CC_table; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 132 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 133 | //------------------------------------------------------------ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 134 | // Primary data structure #2: InstrInfo table |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 135 | // - Holds the cached info about each instr that is used for simulation. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 136 | // - table(SB_start_addr, list(InstrInfo)) |
| 137 | // - For each SB, each InstrInfo in the list holds info about the |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 138 | // instruction (instrLen, instrAddr, etc), plus a pointer to its line |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 139 | // CC. This node is what's passed to the simulation function. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 140 | // - When SBs are discarded the relevant list(instr_details) is freed. |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 141 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 142 | typedef struct _InstrInfo InstrInfo; |
| 143 | struct _InstrInfo { |
nethercote | ca1f2dc | 2004-07-21 08:49:02 +0000 | [diff] [blame] | 144 | Addr instr_addr; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 145 | UChar instr_len; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 146 | LineCC* parent; // parent line-CC |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 149 | typedef struct _SB_info SB_info; |
| 150 | struct _SB_info { |
| 151 | Addr SB_addr; // key; MUST BE FIRST |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 152 | Int n_instrs; |
| 153 | InstrInfo instrs[0]; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 156 | static OSet* instrInfoTable; |
| 157 | |
| 158 | //------------------------------------------------------------ |
| 159 | // Secondary data structure: string table |
| 160 | // - holds strings, avoiding dups |
| 161 | // - used for filenames and function names, each of which will be |
| 162 | // pointed to by one or more CCs. |
| 163 | // - it also allows equality checks just by pointer comparison, which |
| 164 | // is good when printing the output file at the end. |
| 165 | |
| 166 | static OSet* stringTable; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 167 | |
| 168 | //------------------------------------------------------------ |
| 169 | // Stats |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 170 | static Int distinct_files = 0; |
| 171 | static Int distinct_fns = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 172 | static Int distinct_lines = 0; |
sewardj | 4f29ddf | 2002-05-03 22:29:04 +0000 | [diff] [blame] | 173 | static Int distinct_instrs = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 174 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 175 | static Int full_debugs = 0; |
| 176 | static Int file_line_debugs = 0; |
| 177 | static Int fn_debugs = 0; |
| 178 | static Int no_debugs = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 179 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 180 | /*------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 181 | /*--- String table operations ---*/ |
| 182 | /*------------------------------------------------------------*/ |
| 183 | |
njn | afa1226 | 2005-12-24 03:10:56 +0000 | [diff] [blame] | 184 | static Word stringCmp( void* key, void* elem ) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 185 | { |
| 186 | return VG_(strcmp)(*(Char**)key, *(Char**)elem); |
| 187 | } |
| 188 | |
| 189 | // Get a permanent string; either pull it out of the string table if it's |
| 190 | // been encountered before, or dup it and put it into the string table. |
| 191 | static Char* get_perm_string(Char* s) |
| 192 | { |
| 193 | Char** s_ptr = VG_(OSet_Lookup)(stringTable, &s); |
| 194 | if (s_ptr) { |
| 195 | return *s_ptr; |
| 196 | } else { |
| 197 | Char** s_node = VG_(OSet_AllocNode)(stringTable, sizeof(Char*)); |
| 198 | *s_node = VG_(strdup)(s); |
| 199 | VG_(OSet_Insert)(stringTable, s_node); |
| 200 | return *s_node; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 205 | /*--- CC table operations ---*/ |
| 206 | /*------------------------------------------------------------*/ |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 207 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 208 | static void get_debug_info(Addr instr_addr, Char file[FILE_LEN], |
| 209 | Char fn[FN_LEN], Int* line) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 210 | { |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame^] | 211 | Char dir[FILE_LEN]; |
| 212 | Bool found_dirname; |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 213 | Bool found_file_line = VG_(get_filename_linenum)( |
| 214 | instr_addr, |
| 215 | file, FILE_LEN, |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame^] | 216 | dir, FILE_LEN, &found_dirname, |
sewardj | 7cee6f9 | 2005-06-13 17:39:06 +0000 | [diff] [blame] | 217 | line |
| 218 | ); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 219 | Bool found_fn = VG_(get_fnname)(instr_addr, fn, FN_LEN); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 220 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 221 | if (!found_file_line) { |
| 222 | VG_(strcpy)(file, "???"); |
| 223 | *line = 0; |
| 224 | } |
| 225 | if (!found_fn) { |
| 226 | VG_(strcpy)(fn, "???"); |
| 227 | } |
njn | f3b61d6 | 2007-09-17 00:41:07 +0000 | [diff] [blame^] | 228 | |
| 229 | if (found_dirname) { |
| 230 | // +1 for the '/'. |
| 231 | tl_assert(VG_(strlen)(dir) + VG_(strlen)(file) + 1 < FILE_LEN); |
| 232 | VG_(strcat)(dir, "/"); // Append '/' |
| 233 | VG_(strcat)(dir, file); // Append file to dir |
| 234 | VG_(strcpy)(file, dir); // Move dir+file to file |
| 235 | } |
| 236 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 237 | if (found_file_line) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 238 | if (found_fn) full_debugs++; |
| 239 | else file_line_debugs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 240 | } else { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 241 | if (found_fn) fn_debugs++; |
| 242 | else no_debugs++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 246 | // Do a three step traversal: by file, then fn, then line. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 247 | // Returns a pointer to the line CC, creates a new one if necessary. |
| 248 | static LineCC* get_lineCC(Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 249 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 250 | Char file[FILE_LEN], fn[FN_LEN]; |
| 251 | Int line; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 252 | CodeLoc loc; |
| 253 | LineCC* lineCC; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 254 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 255 | get_debug_info(origAddr, file, fn, &line); |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 256 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 257 | loc.file = file; |
| 258 | loc.fn = fn; |
| 259 | loc.line = line; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 260 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 261 | lineCC = VG_(OSet_Lookup)(CC_table, &loc); |
| 262 | if (!lineCC) { |
| 263 | // Allocate and zero a new node. |
| 264 | lineCC = VG_(OSet_AllocNode)(CC_table, sizeof(LineCC)); |
| 265 | lineCC->loc.file = get_perm_string(loc.file); |
| 266 | lineCC->loc.fn = get_perm_string(loc.fn); |
| 267 | lineCC->loc.line = loc.line; |
njn | 0a8db5c | 2007-04-02 03:11:41 +0000 | [diff] [blame] | 268 | lineCC->Ir.a = 0; |
| 269 | lineCC->Ir.m1 = 0; |
| 270 | lineCC->Ir.m2 = 0; |
| 271 | lineCC->Dr.a = 0; |
| 272 | lineCC->Dr.m1 = 0; |
| 273 | lineCC->Dr.m2 = 0; |
| 274 | lineCC->Dw.a = 0; |
| 275 | lineCC->Dw.m1 = 0; |
| 276 | lineCC->Dw.m2 = 0; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 277 | lineCC->Bc.b = 0; |
| 278 | lineCC->Bc.mp = 0; |
| 279 | lineCC->Bi.b = 0; |
| 280 | lineCC->Bi.mp = 0; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 281 | VG_(OSet_Insert)(CC_table, lineCC); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 282 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 283 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 284 | return lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | /*------------------------------------------------------------*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 288 | /*--- Cache simulation functions ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 289 | /*------------------------------------------------------------*/ |
| 290 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 291 | static VG_REGPARM(1) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 292 | void log_1I_0D_cache_access(InstrInfo* n) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 293 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 294 | //VG_(printf)("1I_0D : CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n", |
| 295 | // n, n->instr_addr, n->instr_len); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 296 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 297 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 298 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 299 | } |
| 300 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 301 | static VG_REGPARM(2) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 302 | void log_2I_0D_cache_access(InstrInfo* n, InstrInfo* n2) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 303 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 304 | //VG_(printf)("2I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 305 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n", |
| 306 | // n, n->instr_addr, n->instr_len, |
| 307 | // n2, n2->instr_addr, n2->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 308 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
| 309 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 310 | n->parent->Ir.a++; |
| 311 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
| 312 | &n2->parent->Ir.m1, &n2->parent->Ir.m2); |
| 313 | n2->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 317 | void log_3I_0D_cache_access(InstrInfo* n, InstrInfo* n2, InstrInfo* n3) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 318 | { |
| 319 | //VG_(printf)("3I_0D : CC1addr=0x%010lx, i1addr=0x%010lx, i1size=%lu\n" |
| 320 | // " CC2addr=0x%010lx, i2addr=0x%010lx, i2size=%lu\n" |
| 321 | // " CC3addr=0x%010lx, i3addr=0x%010lx, i3size=%lu\n", |
| 322 | // n, n->instr_addr, n->instr_len, |
| 323 | // n2, n2->instr_addr, n2->instr_len, |
| 324 | // n3, n3->instr_addr, n3->instr_len); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 325 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
| 326 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 327 | n->parent->Ir.a++; |
| 328 | cachesim_I1_doref(n2->instr_addr, n2->instr_len, |
| 329 | &n2->parent->Ir.m1, &n2->parent->Ir.m2); |
| 330 | n2->parent->Ir.a++; |
| 331 | cachesim_I1_doref(n3->instr_addr, n3->instr_len, |
| 332 | &n3->parent->Ir.m1, &n3->parent->Ir.m2); |
| 333 | n3->parent->Ir.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 337 | void log_1I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 338 | { |
| 339 | //VG_(printf)("1I_1Dr: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 340 | // " daddr=0x%010lx, dsize=%lu\n", |
| 341 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 342 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 343 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 344 | n->parent->Ir.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 345 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 346 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 347 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 348 | n->parent->Dr.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 349 | } |
| 350 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 351 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 352 | void log_1I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 353 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 354 | //VG_(printf)("1I_1Dw: CCaddr=0x%010lx, iaddr=0x%010lx, isize=%lu\n" |
| 355 | // " daddr=0x%010lx, dsize=%lu\n", |
| 356 | // n, n->instr_addr, n->instr_len, data_addr, data_size); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 357 | cachesim_I1_doref(n->instr_addr, n->instr_len, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 358 | &n->parent->Ir.m1, &n->parent->Ir.m2); |
| 359 | n->parent->Ir.a++; |
| 360 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 361 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 362 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 363 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 364 | } |
| 365 | |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 366 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 367 | void log_0I_1Dr_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 368 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 369 | //VG_(printf)("0I_1Dr: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 370 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 371 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 372 | &n->parent->Dr.m1, &n->parent->Dr.m2); |
| 373 | n->parent->Dr.a++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static VG_REGPARM(3) |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 377 | void log_0I_1Dw_cache_access(InstrInfo* n, Addr data_addr, Word data_size) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 378 | { |
| 379 | //VG_(printf)("0I_1Dw: CCaddr=0x%010lx, daddr=0x%010lx, dsize=%lu\n", |
| 380 | // n, data_addr, data_size); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 381 | cachesim_D1_doref(data_addr, data_size, |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 382 | &n->parent->Dw.m1, &n->parent->Dw.m2); |
| 383 | n->parent->Dw.a++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 384 | } |
| 385 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 386 | /* For branches, we consult two different predictors, one which |
| 387 | predicts taken/untaken for conditional branches, and the other |
| 388 | which predicts the branch target address for indirect branches |
| 389 | (jump-to-register style ones). */ |
| 390 | |
| 391 | static VG_REGPARM(2) |
| 392 | void log_cond_branch(InstrInfo* n, Word taken) |
| 393 | { |
| 394 | //VG_(printf)("cbrnch: CCaddr=0x%010lx, taken=0x%010lx\n", |
| 395 | // n, taken); |
| 396 | n->parent->Bc.b++; |
| 397 | n->parent->Bc.mp |
| 398 | += (1 & do_cond_branch_predict(n->instr_addr, taken)); |
| 399 | } |
| 400 | |
| 401 | static VG_REGPARM(2) |
| 402 | void log_ind_branch(InstrInfo* n, UWord actual_dst) |
| 403 | { |
| 404 | //VG_(printf)("ibrnch: CCaddr=0x%010lx, dst=0x%010lx\n", |
| 405 | // n, actual_dst); |
| 406 | n->parent->Bi.b++; |
| 407 | n->parent->Bi.mp |
| 408 | += (1 & do_ind_branch_predict(n->instr_addr, actual_dst)); |
| 409 | } |
| 410 | |
| 411 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 412 | /*------------------------------------------------------------*/ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 413 | /*--- Instrumentation types and structures ---*/ |
| 414 | /*------------------------------------------------------------*/ |
| 415 | |
| 416 | /* Maintain an ordered list of memory events which are outstanding, in |
| 417 | the sense that no IR has yet been generated to do the relevant |
| 418 | helper calls. The BB is scanned top to bottom and memory events |
| 419 | are added to the end of the list, merging with the most recent |
| 420 | notified event where possible (Dw immediately following Dr and |
| 421 | having the same size and EA can be merged). |
| 422 | |
| 423 | This merging is done so that for architectures which have |
| 424 | load-op-store instructions (x86, amd64), the insn is treated as if |
| 425 | it makes just one memory reference (a modify), rather than two (a |
| 426 | read followed by a write at the same address). |
| 427 | |
| 428 | At various points the list will need to be flushed, that is, IR |
| 429 | generated from it. That must happen before any possible exit from |
| 430 | the block (the end, or an IRStmt_Exit). Flushing also takes place |
| 431 | when there is no space to add a new event. |
| 432 | |
| 433 | If we require the simulation statistics to be up to date with |
| 434 | respect to possible memory exceptions, then the list would have to |
| 435 | be flushed before each memory reference. That would however lose |
| 436 | performance by inhibiting event-merging during flushing. |
| 437 | |
| 438 | Flushing the list consists of walking it start to end and emitting |
| 439 | instrumentation IR for each event, in the order in which they |
| 440 | appear. It may be possible to emit a single call for two adjacent |
| 441 | events in order to reduce the number of helper function calls made. |
| 442 | For example, it could well be profitable to handle two adjacent Ir |
| 443 | events with a single helper call. */ |
| 444 | |
| 445 | typedef |
| 446 | IRExpr |
| 447 | IRAtom; |
| 448 | |
| 449 | typedef |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 450 | enum { |
| 451 | Ev_Ir, // Instruction read |
| 452 | Ev_Dr, // Data read |
| 453 | Ev_Dw, // Data write |
| 454 | Ev_Dm, // Data modify (read then write) |
| 455 | Ev_Bc, // branch conditional |
| 456 | Ev_Bi // branch indirect (to unknown destination) |
| 457 | } |
| 458 | EventTag; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 459 | |
| 460 | typedef |
| 461 | struct { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 462 | EventTag tag; |
| 463 | InstrInfo* inode; |
| 464 | union { |
| 465 | struct { |
| 466 | } Ir; |
| 467 | struct { |
| 468 | IRAtom* ea; |
| 469 | Int szB; |
| 470 | } Dr; |
| 471 | struct { |
| 472 | IRAtom* ea; |
| 473 | Int szB; |
| 474 | } Dw; |
| 475 | struct { |
| 476 | IRAtom* ea; |
| 477 | Int szB; |
| 478 | } Dm; |
| 479 | struct { |
| 480 | IRAtom* taken; /* :: Ity_I1 */ |
| 481 | } Bc; |
| 482 | struct { |
| 483 | IRAtom* dst; |
| 484 | } Bi; |
| 485 | } Ev; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 486 | } |
| 487 | Event; |
| 488 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 489 | static void init_Event ( Event* ev ) { |
| 490 | VG_(memset)(ev, 0, sizeof(Event)); |
| 491 | } |
| 492 | |
| 493 | static IRAtom* get_Event_dea ( Event* ev ) { |
| 494 | switch (ev->tag) { |
| 495 | case Ev_Dr: return ev->Ev.Dr.ea; |
| 496 | case Ev_Dw: return ev->Ev.Dw.ea; |
| 497 | case Ev_Dm: return ev->Ev.Dm.ea; |
| 498 | default: tl_assert(0); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static Int get_Event_dszB ( Event* ev ) { |
| 503 | switch (ev->tag) { |
| 504 | case Ev_Dr: return ev->Ev.Dr.szB; |
| 505 | case Ev_Dw: return ev->Ev.Dw.szB; |
| 506 | case Ev_Dm: return ev->Ev.Dm.szB; |
| 507 | default: tl_assert(0); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 512 | /* Up to this many unnotified events are allowed. Number is |
| 513 | arbitrary. Larger numbers allow more event merging to occur, but |
| 514 | potentially induce more spilling due to extending live ranges of |
| 515 | address temporaries. */ |
| 516 | #define N_EVENTS 16 |
| 517 | |
| 518 | |
| 519 | /* A struct which holds all the running state during instrumentation. |
| 520 | Mostly to avoid passing loads of parameters everywhere. */ |
| 521 | typedef |
| 522 | struct { |
| 523 | /* The current outstanding-memory-event list. */ |
| 524 | Event events[N_EVENTS]; |
| 525 | Int events_used; |
| 526 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 527 | /* The array of InstrInfo bins for the BB. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 528 | SB_info* sbInfo; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 529 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 530 | /* Number InstrInfo bins 'used' so far. */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 531 | Int sbInfo_i; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 532 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 533 | /* The output SB being constructed. */ |
| 534 | IRSB* sbOut; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 535 | } |
| 536 | CgState; |
| 537 | |
| 538 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 539 | /*------------------------------------------------------------*/ |
| 540 | /*--- Instrumentation main ---*/ |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 541 | /*------------------------------------------------------------*/ |
| 542 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 543 | // Note that origAddr is the real origAddr, not the address of the first |
| 544 | // instruction in the block (they can be different due to redirection). |
nethercote | 564b2b0 | 2004-08-07 15:54:53 +0000 | [diff] [blame] | 545 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 546 | SB_info* get_SB_info(IRSB* sbIn, Addr origAddr) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 547 | { |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 548 | Int i, n_instrs; |
| 549 | IRStmt* st; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 550 | SB_info* sbInfo; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 551 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 552 | // Count number of original instrs in SB |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 553 | n_instrs = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 554 | for (i = 0; i < sbIn->stmts_used; i++) { |
| 555 | st = sbIn->stmts[i]; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 556 | if (Ist_IMark == st->tag) n_instrs++; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 557 | } |
| 558 | |
njn | f7d2609 | 2005-10-12 16:45:17 +0000 | [diff] [blame] | 559 | // Check that we don't have an entry for this BB in the instr-info table. |
| 560 | // If this assertion fails, there has been some screwup: some |
| 561 | // translations must have been discarded but Cachegrind hasn't discarded |
| 562 | // the corresponding entries in the instr-info table. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 563 | sbInfo = VG_(OSet_Lookup)(instrInfoTable, &origAddr); |
| 564 | tl_assert(NULL == sbInfo); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 565 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 566 | // BB never translated before (at this address, at least; could have |
| 567 | // been unloaded and then reloaded elsewhere in memory) |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 568 | sbInfo = VG_(OSet_AllocNode)(instrInfoTable, |
| 569 | sizeof(SB_info) + n_instrs*sizeof(InstrInfo)); |
| 570 | sbInfo->SB_addr = origAddr; |
| 571 | sbInfo->n_instrs = n_instrs; |
| 572 | VG_(OSet_Insert)( instrInfoTable, sbInfo ); |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 573 | distinct_instrs++; |
| 574 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 575 | return sbInfo; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 576 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 577 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 578 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 579 | static void showEvent ( Event* ev ) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 580 | { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 581 | switch (ev->tag) { |
| 582 | case Ev_Ir: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 583 | VG_(printf)("Ir %p\n", ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 584 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 585 | case Ev_Dr: |
| 586 | VG_(printf)("Dr %p %d EA=", ev->inode, ev->Ev.Dr.szB); |
| 587 | ppIRExpr(ev->Ev.Dr.ea); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 588 | VG_(printf)("\n"); |
| 589 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 590 | case Ev_Dw: |
| 591 | VG_(printf)("Dw %p %d EA=", ev->inode, ev->Ev.Dw.szB); |
| 592 | ppIRExpr(ev->Ev.Dw.ea); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 593 | VG_(printf)("\n"); |
| 594 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 595 | case Ev_Dm: |
| 596 | VG_(printf)("Dm %p %d EA=", ev->inode, ev->Ev.Dm.szB); |
| 597 | ppIRExpr(ev->Ev.Dm.ea); |
| 598 | VG_(printf)("\n"); |
| 599 | break; |
| 600 | case Ev_Bc: |
| 601 | VG_(printf)("Bc %p GA=", ev->inode); |
| 602 | ppIRExpr(ev->Ev.Bc.taken); |
| 603 | VG_(printf)("\n"); |
| 604 | break; |
| 605 | case Ev_Bi: |
| 606 | VG_(printf)("Bi %p DST=", ev->inode); |
| 607 | ppIRExpr(ev->Ev.Bi.dst); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 608 | VG_(printf)("\n"); |
| 609 | break; |
| 610 | default: |
| 611 | tl_assert(0); |
| 612 | break; |
| 613 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 614 | } |
| 615 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 616 | // Reserve and initialise an InstrInfo for the first mention of a new insn. |
| 617 | static |
| 618 | InstrInfo* setup_InstrInfo ( CgState* cgs, Addr instr_addr, UInt instr_len ) |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 619 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 620 | InstrInfo* i_node; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 621 | tl_assert(cgs->sbInfo_i >= 0); |
| 622 | tl_assert(cgs->sbInfo_i < cgs->sbInfo->n_instrs); |
| 623 | i_node = &cgs->sbInfo->instrs[ cgs->sbInfo_i ]; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 624 | i_node->instr_addr = instr_addr; |
| 625 | i_node->instr_len = instr_len; |
| 626 | i_node->parent = get_lineCC(instr_addr); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 627 | cgs->sbInfo_i++; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 628 | return i_node; |
| 629 | } |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 630 | |
sewardj | 17a56bf | 2005-03-21 01:35:02 +0000 | [diff] [blame] | 631 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 632 | /* Generate code for all outstanding memory events, and mark the queue |
| 633 | empty. Code is generated into cgs->bbOut, and this activity |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 634 | 'consumes' slots in cgs->sbInfo. */ |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 635 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 636 | static void flushEvents ( CgState* cgs ) |
| 637 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 638 | Int i, regparms; |
| 639 | Char* helperName; |
| 640 | void* helperAddr; |
| 641 | IRExpr** argv; |
| 642 | IRExpr* i_node_expr; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 643 | IRDirty* di; |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 644 | Event* ev; |
| 645 | Event* ev2; |
| 646 | Event* ev3; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 647 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 648 | i = 0; |
| 649 | while (i < cgs->events_used) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 650 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 651 | helperName = NULL; |
| 652 | helperAddr = NULL; |
| 653 | argv = NULL; |
| 654 | regparms = 0; |
| 655 | |
| 656 | /* generate IR to notify event i and possibly the ones |
| 657 | immediately following it. */ |
| 658 | tl_assert(i >= 0 && i < cgs->events_used); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 659 | |
| 660 | ev = &cgs->events[i]; |
| 661 | ev2 = ( i < cgs->events_used-1 ? &cgs->events[i+1] : NULL ); |
| 662 | ev3 = ( i < cgs->events_used-2 ? &cgs->events[i+2] : NULL ); |
| 663 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 664 | if (DEBUG_CG) { |
| 665 | VG_(printf)(" flush "); |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 666 | showEvent( ev ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 667 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 668 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 669 | i_node_expr = mkIRExpr_HWord( (HWord)ev->inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 670 | |
| 671 | /* Decide on helper fn to call and args to pass it, and advance |
| 672 | i appropriately. */ |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 673 | switch (ev->tag) { |
| 674 | case Ev_Ir: |
| 675 | /* Merge an Ir with a following Dr/Dm. */ |
| 676 | if (ev2 && (ev2->tag == Ev_Dr || ev2->tag == Ev_Dm)) { |
| 677 | /* Why is this true? It's because we're merging an Ir |
| 678 | with a following Dr or Dm. The Ir derives from the |
| 679 | instruction's IMark and the Dr/Dm from data |
| 680 | references which follow it. In short it holds |
| 681 | because each insn starts with an IMark, hence an |
| 682 | Ev_Ir, and so these Dr/Dm must pertain to the |
| 683 | immediately preceding Ir. Same applies to analogous |
| 684 | assertions in the subsequent cases. */ |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 685 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 686 | helperName = "log_1I_1Dr_cache_access"; |
| 687 | helperAddr = &log_1I_1Dr_cache_access; |
| 688 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 689 | get_Event_dea(ev2), |
| 690 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 691 | regparms = 3; |
| 692 | i += 2; |
| 693 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 694 | /* Merge an Ir with a following Dw. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 695 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 696 | if (ev2 && ev2->tag == Ev_Dw) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 697 | tl_assert(ev2->inode == ev->inode); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 698 | helperName = "log_1I_1Dw_cache_access"; |
| 699 | helperAddr = &log_1I_1Dw_cache_access; |
| 700 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 701 | get_Event_dea(ev2), |
| 702 | mkIRExpr_HWord( get_Event_dszB(ev2) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 703 | regparms = 3; |
| 704 | i += 2; |
| 705 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 706 | /* Merge an Ir with two following Irs. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 707 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 708 | if (ev2 && ev3 && ev2->tag == Ev_Ir && ev3->tag == Ev_Ir) |
njn | c285dca | 2005-10-15 22:07:28 +0000 | [diff] [blame] | 709 | { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 710 | helperName = "log_3I_0D_cache_access"; |
| 711 | helperAddr = &log_3I_0D_cache_access; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 712 | argv = mkIRExprVec_3( i_node_expr, |
| 713 | mkIRExpr_HWord( (HWord)ev2->inode ), |
| 714 | mkIRExpr_HWord( (HWord)ev3->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 715 | regparms = 3; |
| 716 | i += 3; |
| 717 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 718 | /* Merge an Ir with one following Ir. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 719 | else |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 720 | if (ev2 && ev2->tag == Ev_Ir) { |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 721 | helperName = "log_2I_0D_cache_access"; |
| 722 | helperAddr = &log_2I_0D_cache_access; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 723 | argv = mkIRExprVec_2( i_node_expr, |
| 724 | mkIRExpr_HWord( (HWord)ev2->inode ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 725 | regparms = 2; |
| 726 | i += 2; |
| 727 | } |
| 728 | /* No merging possible; emit as-is. */ |
| 729 | else { |
| 730 | helperName = "log_1I_0D_cache_access"; |
| 731 | helperAddr = &log_1I_0D_cache_access; |
| 732 | argv = mkIRExprVec_1( i_node_expr ); |
| 733 | regparms = 1; |
| 734 | i++; |
| 735 | } |
| 736 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 737 | case Ev_Dr: |
| 738 | case Ev_Dm: |
| 739 | /* Data read or modify */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 740 | helperName = "log_0I_1Dr_cache_access"; |
| 741 | helperAddr = &log_0I_1Dr_cache_access; |
| 742 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 743 | get_Event_dea(ev), |
| 744 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 745 | regparms = 3; |
| 746 | i++; |
| 747 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 748 | case Ev_Dw: |
| 749 | /* Data write */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 750 | helperName = "log_0I_1Dw_cache_access"; |
| 751 | helperAddr = &log_0I_1Dw_cache_access; |
| 752 | argv = mkIRExprVec_3( i_node_expr, |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 753 | get_Event_dea(ev), |
| 754 | mkIRExpr_HWord( get_Event_dszB(ev) ) ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 755 | regparms = 3; |
| 756 | i++; |
| 757 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 758 | case Ev_Bc: |
| 759 | /* Conditional branch */ |
| 760 | helperName = "log_cond_branch"; |
| 761 | helperAddr = &log_cond_branch; |
| 762 | argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bc.taken ); |
| 763 | regparms = 2; |
| 764 | i++; |
| 765 | break; |
| 766 | case Ev_Bi: |
| 767 | /* Branch to an unknown destination */ |
| 768 | helperName = "log_ind_branch"; |
| 769 | helperAddr = &log_ind_branch; |
| 770 | argv = mkIRExprVec_2( i_node_expr, ev->Ev.Bi.dst ); |
| 771 | regparms = 2; |
| 772 | i++; |
| 773 | break; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 774 | default: |
| 775 | tl_assert(0); |
| 776 | } |
| 777 | |
| 778 | /* Add the helper. */ |
| 779 | tl_assert(helperName); |
| 780 | tl_assert(helperAddr); |
| 781 | tl_assert(argv); |
sewardj | 5bb8682 | 2005-12-23 12:47:42 +0000 | [diff] [blame] | 782 | di = unsafeIRDirty_0_N( regparms, |
| 783 | helperName, VG_(fnptr_to_fnentry)( helperAddr ), |
| 784 | argv ); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 785 | addStmtToIRSB( cgs->sbOut, IRStmt_Dirty(di) ); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 786 | } |
| 787 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 788 | cgs->events_used = 0; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 789 | } |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 790 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 791 | static void addEvent_Ir ( CgState* cgs, InstrInfo* inode ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 792 | { |
| 793 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 794 | if (cgs->events_used == N_EVENTS) |
| 795 | flushEvents(cgs); |
| 796 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 797 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 798 | init_Event(evt); |
| 799 | evt->tag = Ev_Ir; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 800 | evt->inode = inode; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 801 | cgs->events_used++; |
| 802 | } |
| 803 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 804 | static |
| 805 | void addEvent_Dr ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 806 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 807 | Event* evt; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 808 | tl_assert(isIRAtom(ea)); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 809 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 810 | if (!clo_cache_sim) |
| 811 | return; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 812 | if (cgs->events_used == N_EVENTS) |
| 813 | flushEvents(cgs); |
| 814 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 815 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 816 | init_Event(evt); |
| 817 | evt->tag = Ev_Dr; |
| 818 | evt->inode = inode; |
| 819 | evt->Ev.Dr.szB = datasize; |
| 820 | evt->Ev.Dr.ea = ea; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 821 | cgs->events_used++; |
| 822 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 823 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 824 | static |
| 825 | void addEvent_Dw ( CgState* cgs, InstrInfo* inode, Int datasize, IRAtom* ea ) |
| 826 | { |
| 827 | Event* lastEvt; |
| 828 | Event* evt; |
| 829 | |
| 830 | tl_assert(isIRAtom(ea)); |
| 831 | tl_assert(datasize >= 1 && datasize <= MIN_LINE_SIZE); |
| 832 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 833 | if (!clo_cache_sim) |
| 834 | return; |
| 835 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 836 | /* Is it possible to merge this write with the preceding read? */ |
| 837 | lastEvt = &cgs->events[cgs->events_used-1]; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 838 | if (cgs->events_used > 0 |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 839 | && lastEvt->tag == Ev_Dr |
| 840 | && lastEvt->Ev.Dr.szB == datasize |
| 841 | && lastEvt->inode == inode |
| 842 | && eqIRAtom(lastEvt->Ev.Dr.ea, ea)) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 843 | { |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 844 | lastEvt->tag = Ev_Dm; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 845 | return; |
| 846 | } |
| 847 | |
| 848 | /* No. Add as normal. */ |
| 849 | if (cgs->events_used == N_EVENTS) |
| 850 | flushEvents(cgs); |
| 851 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 852 | evt = &cgs->events[cgs->events_used]; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 853 | init_Event(evt); |
| 854 | evt->tag = Ev_Dw; |
| 855 | evt->inode = inode; |
| 856 | evt->Ev.Dw.szB = datasize; |
| 857 | evt->Ev.Dw.ea = ea; |
| 858 | cgs->events_used++; |
| 859 | } |
| 860 | |
| 861 | static |
| 862 | void addEvent_Bc ( CgState* cgs, InstrInfo* inode, IRAtom* guard ) |
| 863 | { |
| 864 | Event* evt; |
| 865 | tl_assert(isIRAtom(guard)); |
| 866 | tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, guard) |
| 867 | == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64)); |
| 868 | if (!clo_branch_sim) |
| 869 | return; |
| 870 | if (cgs->events_used == N_EVENTS) |
| 871 | flushEvents(cgs); |
| 872 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 873 | evt = &cgs->events[cgs->events_used]; |
| 874 | init_Event(evt); |
| 875 | evt->tag = Ev_Bc; |
| 876 | evt->inode = inode; |
| 877 | evt->Ev.Bc.taken = guard; |
| 878 | cgs->events_used++; |
| 879 | } |
| 880 | |
| 881 | static |
| 882 | void addEvent_Bi ( CgState* cgs, InstrInfo* inode, IRAtom* whereTo ) |
| 883 | { |
| 884 | Event* evt; |
| 885 | tl_assert(isIRAtom(whereTo)); |
| 886 | tl_assert(typeOfIRExpr(cgs->sbOut->tyenv, whereTo) |
| 887 | == (sizeof(HWord)==4 ? Ity_I32 : Ity_I64)); |
| 888 | if (!clo_branch_sim) |
| 889 | return; |
| 890 | if (cgs->events_used == N_EVENTS) |
| 891 | flushEvents(cgs); |
| 892 | tl_assert(cgs->events_used >= 0 && cgs->events_used < N_EVENTS); |
| 893 | evt = &cgs->events[cgs->events_used]; |
| 894 | init_Event(evt); |
| 895 | evt->tag = Ev_Bi; |
| 896 | evt->inode = inode; |
| 897 | evt->Ev.Bi.dst = whereTo; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 898 | cgs->events_used++; |
| 899 | } |
| 900 | |
| 901 | //////////////////////////////////////////////////////////// |
| 902 | |
| 903 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 904 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 905 | IRSB* cg_instrument ( VgCallbackClosure* closure, |
| 906 | IRSB* sbIn, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 907 | VexGuestLayout* layout, |
| 908 | VexGuestExtents* vge, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 909 | IRType gWordTy, IRType hWordTy ) |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 910 | { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 911 | Int i, isize; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 912 | IRStmt* st; |
| 913 | Addr64 cia; /* address of current insn */ |
| 914 | CgState cgs; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 915 | IRTypeEnv* tyenv = sbIn->tyenv; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 916 | InstrInfo* curr_inode = NULL; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 917 | |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 918 | if (gWordTy != hWordTy) { |
| 919 | /* We don't currently support this case. */ |
| 920 | VG_(tool_panic)("host/guest word size mismatch"); |
| 921 | } |
| 922 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 923 | // Set up new SB |
| 924 | cgs.sbOut = deepCopyIRSBExceptStmts(sbIn); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 925 | |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 926 | // Copy verbatim any IR preamble preceding the first IMark |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 927 | i = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 928 | while (i < sbIn->stmts_used && sbIn->stmts[i]->tag != Ist_IMark) { |
| 929 | addStmtToIRSB( cgs.sbOut, sbIn->stmts[i] ); |
sewardj | a9f538c | 2005-10-23 12:06:55 +0000 | [diff] [blame] | 930 | i++; |
| 931 | } |
| 932 | |
| 933 | // Get the first statement, and initial cia from it |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 934 | tl_assert(sbIn->stmts_used > 0); |
| 935 | tl_assert(i < sbIn->stmts_used); |
| 936 | st = sbIn->stmts[i]; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 937 | tl_assert(Ist_IMark == st->tag); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 938 | |
| 939 | cia = st->Ist.IMark.addr; |
| 940 | isize = st->Ist.IMark.len; |
| 941 | // If Vex fails to decode an instruction, the size will be zero. |
| 942 | // Pretend otherwise. |
| 943 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 944 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 945 | // Set up running state and get block info |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 946 | tl_assert(closure->readdr == vge->base[0]); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 947 | cgs.events_used = 0; |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 948 | cgs.sbInfo = get_SB_info(sbIn, (Addr)closure->readdr); |
| 949 | cgs.sbInfo_i = 0; |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 950 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 951 | if (DEBUG_CG) |
| 952 | VG_(printf)("\n\n---------- cg_instrument ----------\n"); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 953 | |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 954 | // Traverse the block, initialising inodes, adding events and flushing as |
| 955 | // necessary. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 956 | for (/*use current i*/; i < sbIn->stmts_used; i++) { |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 957 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 958 | st = sbIn->stmts[i]; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 959 | tl_assert(isFlatIRStmt(st)); |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 960 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 961 | switch (st->tag) { |
| 962 | case Ist_NoOp: |
| 963 | case Ist_AbiHint: |
| 964 | case Ist_Put: |
| 965 | case Ist_PutI: |
| 966 | case Ist_MFence: |
| 967 | break; |
njn | 20677cc | 2005-08-12 23:47:51 +0000 | [diff] [blame] | 968 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 969 | case Ist_IMark: |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 970 | cia = st->Ist.IMark.addr; |
| 971 | isize = st->Ist.IMark.len; |
| 972 | |
| 973 | // If Vex fails to decode an instruction, the size will be zero. |
| 974 | // Pretend otherwise. |
| 975 | if (isize == 0) isize = VG_MIN_INSTR_SZB; |
| 976 | |
njn | a5ad9ba | 2005-11-10 15:20:37 +0000 | [diff] [blame] | 977 | // Sanity-check size. |
| 978 | tl_assert( (VG_MIN_INSTR_SZB <= isize && isize <= VG_MAX_INSTR_SZB) |
| 979 | || VG_CLREQ_SZB == isize ); |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 980 | |
| 981 | // Get space for and init the inode, record it as the current one. |
| 982 | // Subsequent Dr/Dw/Dm events from the same instruction will |
| 983 | // also use it. |
| 984 | curr_inode = setup_InstrInfo(&cgs, cia, isize); |
| 985 | |
| 986 | addEvent_Ir( &cgs, curr_inode ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 987 | break; |
| 988 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 989 | case Ist_WrTmp: { |
| 990 | IRExpr* data = st->Ist.WrTmp.data; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 991 | if (data->tag == Iex_Load) { |
| 992 | IRExpr* aexpr = data->Iex.Load.addr; |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 993 | // Note also, endianness info is ignored. I guess |
| 994 | // that's not interesting. |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 995 | addEvent_Dr( &cgs, curr_inode, sizeofIRType(data->Iex.Load.ty), |
| 996 | aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 997 | } |
| 998 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1001 | case Ist_Store: { |
| 1002 | IRExpr* data = st->Ist.Store.data; |
| 1003 | IRExpr* aexpr = st->Ist.Store.addr; |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1004 | addEvent_Dw( &cgs, curr_inode, |
| 1005 | sizeofIRType(typeOfIRExpr(tyenv, data)), aexpr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1006 | break; |
| 1007 | } |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1008 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1009 | case Ist_Dirty: { |
| 1010 | Int dataSize; |
| 1011 | IRDirty* d = st->Ist.Dirty.details; |
| 1012 | if (d->mFx != Ifx_None) { |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1013 | /* This dirty helper accesses memory. Collect the details. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1014 | tl_assert(d->mAddr != NULL); |
| 1015 | tl_assert(d->mSize != 0); |
| 1016 | dataSize = d->mSize; |
| 1017 | // Large (eg. 28B, 108B, 512B on x86) data-sized |
| 1018 | // instructions will be done inaccurately, but they're |
| 1019 | // very rare and this avoids errors from hitting more |
| 1020 | // than two cache lines in the simulation. |
| 1021 | if (dataSize > MIN_LINE_SIZE) |
| 1022 | dataSize = MIN_LINE_SIZE; |
| 1023 | if (d->mFx == Ifx_Read || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1024 | addEvent_Dr( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1025 | if (d->mFx == Ifx_Write || d->mFx == Ifx_Modify) |
njn | fd9f622 | 2005-10-16 00:17:37 +0000 | [diff] [blame] | 1026 | addEvent_Dw( &cgs, curr_inode, dataSize, d->mAddr ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1027 | } else { |
| 1028 | tl_assert(d->mAddr == NULL); |
| 1029 | tl_assert(d->mSize == 0); |
| 1030 | } |
| 1031 | break; |
| 1032 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1033 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1034 | case Ist_Exit: { |
| 1035 | /* Stuff to widen the guard expression to a host word, so |
| 1036 | we can pass it to the branch predictor simulation |
| 1037 | functions easily. */ |
| 1038 | Bool inverted; |
| 1039 | Addr64 nia, sea; |
| 1040 | IRConst* dst; |
| 1041 | IROp tyW = hWordTy; |
| 1042 | IROp widen = tyW==Ity_I32 ? Iop_1Uto32 : Iop_1Uto64; |
| 1043 | IROp opXOR = tyW==Ity_I32 ? Iop_Xor32 : Iop_Xor64; |
| 1044 | IRTemp guard1 = newIRTemp(cgs.sbOut->tyenv, Ity_I1); |
| 1045 | IRTemp guardW = newIRTemp(cgs.sbOut->tyenv, tyW); |
| 1046 | IRTemp guard = newIRTemp(cgs.sbOut->tyenv, tyW); |
| 1047 | IRExpr* one = tyW==Ity_I32 ? IRExpr_Const(IRConst_U32(1)) |
| 1048 | : IRExpr_Const(IRConst_U64(1)); |
| 1049 | |
| 1050 | /* First we need to figure out whether the side exit got |
| 1051 | inverted by the ir optimiser. To do that, figure out |
| 1052 | the next (fallthrough) instruction's address and the |
| 1053 | side exit address and see if they are the same. */ |
| 1054 | nia = cia + (Addr64)isize; |
| 1055 | if (tyW == Ity_I32) |
| 1056 | nia &= 0xFFFFFFFFULL; |
| 1057 | |
| 1058 | /* Side exit address */ |
| 1059 | dst = st->Ist.Exit.dst; |
| 1060 | if (tyW == Ity_I32) { |
| 1061 | tl_assert(dst->tag == Ico_U32); |
| 1062 | sea = (Addr64)(UInt)dst->Ico.U32; |
| 1063 | } else { |
| 1064 | tl_assert(tyW == Ity_I64); |
| 1065 | tl_assert(dst->tag == Ico_U64); |
| 1066 | sea = dst->Ico.U64; |
| 1067 | } |
| 1068 | |
| 1069 | inverted = nia == sea; |
| 1070 | |
| 1071 | /* Widen the guard expression. */ |
| 1072 | addStmtToIRSB( cgs.sbOut, |
| 1073 | IRStmt_WrTmp( guard1, st->Ist.Exit.guard )); |
| 1074 | addStmtToIRSB( cgs.sbOut, |
| 1075 | IRStmt_WrTmp( guardW, |
| 1076 | IRExpr_Unop(widen, |
| 1077 | IRExpr_RdTmp(guard1))) ); |
| 1078 | /* If the exit is inverted, invert the sense of the guard. */ |
| 1079 | addStmtToIRSB( |
| 1080 | cgs.sbOut, |
| 1081 | IRStmt_WrTmp( |
| 1082 | guard, |
| 1083 | inverted ? IRExpr_Binop(opXOR, IRExpr_RdTmp(guardW), one) |
| 1084 | : IRExpr_RdTmp(guardW) |
| 1085 | )); |
| 1086 | /* And post the event. */ |
| 1087 | addEvent_Bc( &cgs, curr_inode, IRExpr_RdTmp(guard) ); |
| 1088 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1089 | /* We may never reach the next statement, so need to flush |
| 1090 | all outstanding transactions now. */ |
| 1091 | flushEvents( &cgs ); |
| 1092 | break; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1093 | } |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1094 | |
| 1095 | default: |
| 1096 | tl_assert(0); |
| 1097 | break; |
njn | b3507ea | 2005-08-02 23:07:02 +0000 | [diff] [blame] | 1098 | } |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1099 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1100 | /* Copy the original statement */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1101 | addStmtToIRSB( cgs.sbOut, st ); |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1102 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1103 | if (DEBUG_CG) { |
| 1104 | ppIRStmt(st); |
| 1105 | VG_(printf)("\n"); |
| 1106 | } |
| 1107 | } |
| 1108 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1109 | /* Deal with branches to unknown destinations. Except ignore ones |
| 1110 | which are function returns as we assume the return stack |
| 1111 | predictor never mispredicts. */ |
| 1112 | if (sbIn->jumpkind == Ijk_Boring) { |
| 1113 | if (0) { ppIRExpr( sbIn->next ); VG_(printf)("\n"); } |
| 1114 | switch (sbIn->next->tag) { |
| 1115 | case Iex_Const: |
| 1116 | break; /* boring - branch to known address */ |
| 1117 | case Iex_RdTmp: |
| 1118 | /* looks like an indirect branch (branch to unknown) */ |
| 1119 | addEvent_Bi( &cgs, curr_inode, sbIn->next ); |
| 1120 | break; |
| 1121 | default: |
| 1122 | /* shouldn't happen - if the incoming IR is properly |
| 1123 | flattened, should only have tmp and const cases to |
| 1124 | consider. */ |
| 1125 | tl_assert(0); |
| 1126 | } |
| 1127 | } |
| 1128 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1129 | /* At the end of the bb. Flush outstandings. */ |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1130 | flushEvents( &cgs ); |
| 1131 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1132 | /* done. stay sane ... */ |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1133 | tl_assert(cgs.sbInfo_i == cgs.sbInfo->n_instrs); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1134 | |
| 1135 | if (DEBUG_CG) { |
| 1136 | VG_(printf)( "goto {"); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1137 | ppIRJumpKind(sbIn->jumpkind); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1138 | VG_(printf)( "} "); |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1139 | ppIRExpr( sbIn->next ); |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1140 | VG_(printf)( "}\n"); |
| 1141 | } |
| 1142 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1143 | return cgs.sbOut; |
njn | 14d01ce | 2004-11-26 11:30:14 +0000 | [diff] [blame] | 1144 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1145 | |
| 1146 | /*------------------------------------------------------------*/ |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1147 | /*--- Cache configuration ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1148 | /*------------------------------------------------------------*/ |
| 1149 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1150 | #define UNDEFINED_CACHE { -1, -1, -1 } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1151 | |
| 1152 | static cache_t clo_I1_cache = UNDEFINED_CACHE; |
| 1153 | static cache_t clo_D1_cache = UNDEFINED_CACHE; |
| 1154 | static cache_t clo_L2_cache = UNDEFINED_CACHE; |
| 1155 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1156 | /* Checks cache config is ok; makes it so if not. */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1157 | static |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1158 | void check_cache(cache_t* cache, Char *name) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1159 | { |
| 1160 | /* First check they're all powers of two */ |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1161 | if (-1 == VG_(log2)(cache->size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1162 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1163 | "error: %s size of %dB not a power of two; aborting.", |
| 1164 | name, cache->size); |
| 1165 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1168 | if (-1 == VG_(log2)(cache->assoc)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1169 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1170 | "error: %s associativity of %d not a power of two; aborting.", |
| 1171 | name, cache->assoc); |
| 1172 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1175 | if (-1 == VG_(log2)(cache->line_size)) { |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1176 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1177 | "error: %s line size of %dB not a power of two; aborting.", |
| 1178 | name, cache->line_size); |
| 1179 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
njn | 6a3009b | 2005-03-20 00:20:06 +0000 | [diff] [blame] | 1182 | // Then check line size >= 16 -- any smaller and a single instruction could |
| 1183 | // straddle three cache lines, which breaks a simulation assertion and is |
| 1184 | // stupid anyway. |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1185 | if (cache->line_size < MIN_LINE_SIZE) { |
| 1186 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1187 | "error: %s line size of %dB too small; aborting.", |
| 1188 | name, cache->line_size); |
| 1189 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* Then check cache size > line size (causes seg faults if not). */ |
| 1193 | if (cache->size <= cache->line_size) { |
| 1194 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1195 | "error: %s cache size of %dB <= line size of %dB; aborting.", |
| 1196 | name, cache->size, cache->line_size); |
| 1197 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* Then check assoc <= (size / line size) (seg faults otherwise). */ |
| 1201 | if (cache->assoc > (cache->size / cache->line_size)) { |
| 1202 | VG_(message)(Vg_UserMsg, |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1203 | "warning: %s associativity > (size / line size); aborting.", name); |
| 1204 | VG_(exit)(1); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
sewardj | 07133bf | 2002-06-13 10:25:56 +0000 | [diff] [blame] | 1208 | static |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1209 | void configure_caches(cache_t* I1c, cache_t* D1c, cache_t* L2c) |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1210 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1211 | #define DEFINED(L) (-1 != L.size || -1 != L.assoc || -1 != L.line_size) |
| 1212 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1213 | Int n_clos = 0; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1214 | |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1215 | // Count how many were defined on the command line. |
| 1216 | if (DEFINED(clo_I1_cache)) { n_clos++; } |
| 1217 | if (DEFINED(clo_D1_cache)) { n_clos++; } |
| 1218 | if (DEFINED(clo_L2_cache)) { n_clos++; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1219 | |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1220 | // Set the cache config (using auto-detection, if supported by the |
| 1221 | // architecture) |
njn | af839f5 | 2005-06-23 03:27:57 +0000 | [diff] [blame] | 1222 | VG_(configure_caches)( I1c, D1c, L2c, (3 == n_clos) ); |
sewardj | b1a77a4 | 2002-07-13 13:31:20 +0000 | [diff] [blame] | 1223 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1224 | // Then replace with any defined on the command line. |
nethercote | b35a8b9 | 2004-09-11 16:45:27 +0000 | [diff] [blame] | 1225 | if (DEFINED(clo_I1_cache)) { *I1c = clo_I1_cache; } |
| 1226 | if (DEFINED(clo_D1_cache)) { *D1c = clo_D1_cache; } |
| 1227 | if (DEFINED(clo_L2_cache)) { *L2c = clo_L2_cache; } |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1228 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1229 | // Then check values and fix if not acceptable. |
njn | a1d1a64 | 2004-11-26 18:36:02 +0000 | [diff] [blame] | 1230 | check_cache(I1c, "I1"); |
| 1231 | check_cache(D1c, "D1"); |
| 1232 | check_cache(L2c, "L2"); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1233 | |
| 1234 | if (VG_(clo_verbosity) > 1) { |
| 1235 | VG_(message)(Vg_UserMsg, "Cache configuration used:"); |
| 1236 | VG_(message)(Vg_UserMsg, " I1: %dB, %d-way, %dB lines", |
| 1237 | I1c->size, I1c->assoc, I1c->line_size); |
| 1238 | VG_(message)(Vg_UserMsg, " D1: %dB, %d-way, %dB lines", |
| 1239 | D1c->size, D1c->assoc, D1c->line_size); |
| 1240 | VG_(message)(Vg_UserMsg, " L2: %dB, %d-way, %dB lines", |
| 1241 | L2c->size, L2c->assoc, L2c->line_size); |
| 1242 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1243 | #undef CMD_LINE_DEFINED |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1246 | /*------------------------------------------------------------*/ |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1247 | /*--- cg_fini() and related function ---*/ |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1248 | /*------------------------------------------------------------*/ |
| 1249 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1250 | // Total reads/writes/misses. Calculated during CC traversal at the end. |
| 1251 | // All auto-zeroed. |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1252 | static CacheCC Ir_total; |
| 1253 | static CacheCC Dr_total; |
| 1254 | static CacheCC Dw_total; |
| 1255 | static BranchCC Bc_total; |
| 1256 | static BranchCC Bi_total; |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1257 | |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1258 | // The output file base name specified by the user using the |
| 1259 | // --cachegrind-out-file switch. This is combined with the |
| 1260 | // process ID and optional user-supplied qualifier (from |
| 1261 | // --log-file-qualifier) to produce the name stored in |
| 1262 | // cachegrind_out_file. |
| 1263 | static Char* cachegrind_out_file_basename = "cachegrind.out"; |
| 1264 | |
| 1265 | // The final, completed name for the output file. |
| 1266 | static Char* cachegrind_out_file = NULL; |
| 1267 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1268 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1269 | static void fprint_CC_table_and_calc_totals(void) |
| 1270 | { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1271 | Int i, fd; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1272 | SysRes sres; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1273 | Char buf[512], *currFile = NULL, *currFn = NULL; |
| 1274 | LineCC* lineCC; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1275 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1276 | sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 1277 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1278 | if (sres.isError) { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1279 | // If the file can't be opened for whatever reason (conflict |
| 1280 | // between multiple cachegrinded processes?), give up now. |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 1281 | VG_(message)(Vg_UserMsg, |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1282 | "error: can't open cache simulation output file '%s'", |
njn | ee0e6a3 | 2005-04-24 00:21:01 +0000 | [diff] [blame] | 1283 | cachegrind_out_file ); |
| 1284 | VG_(message)(Vg_UserMsg, |
| 1285 | " ... so simulation results will be missing."); |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1286 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1287 | } else { |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 1288 | fd = sres.res; |
sewardj | 0744b6c | 2002-12-11 00:45:42 +0000 | [diff] [blame] | 1289 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1290 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1291 | // "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after |
| 1292 | // the 2nd colon makes cg_annotate's output look nicer. |
| 1293 | VG_(sprintf)(buf, "desc: I1 cache: %s\n" |
| 1294 | "desc: D1 cache: %s\n" |
| 1295 | "desc: L2 cache: %s\n", |
| 1296 | I1.desc_line, D1.desc_line, L2.desc_line); |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1297 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1298 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1299 | // "cmd:" line |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1300 | VG_(strcpy)(buf, "cmd:"); |
| 1301 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1302 | if (VG_(args_the_exename)) { |
| 1303 | VG_(write)(fd, " ", 1); |
| 1304 | VG_(write)(fd, VG_(args_the_exename), |
| 1305 | VG_(strlen)( VG_(args_the_exename) )); |
| 1306 | } |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1307 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1308 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
| 1309 | if (arg) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1310 | VG_(write)(fd, " ", 1); |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1311 | VG_(write)(fd, arg, VG_(strlen)( arg )); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1312 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1313 | } |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1314 | // "events:" line |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1315 | if (clo_cache_sim && clo_branch_sim) { |
| 1316 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw " |
| 1317 | "Bc Bcm Bi Bim\n"); |
| 1318 | } |
| 1319 | else if (clo_cache_sim && !clo_branch_sim) { |
| 1320 | VG_(sprintf)(buf, "\nevents: Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw " |
| 1321 | "\n"); |
| 1322 | } |
| 1323 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1324 | VG_(sprintf)(buf, "\nevents: Ir " |
| 1325 | "Bc Bcm Bi Bim\n"); |
| 1326 | } |
| 1327 | else |
| 1328 | tl_assert(0); /* can't happen */ |
| 1329 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1330 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1331 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1332 | // Traverse every lineCC |
| 1333 | VG_(OSet_ResetIter)(CC_table); |
| 1334 | while ( (lineCC = VG_(OSet_Next)(CC_table)) ) { |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1335 | Bool just_hit_a_new_file = False; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1336 | // If we've hit a new file, print a "fl=" line. Note that because |
| 1337 | // each string is stored exactly once in the string table, we can use |
| 1338 | // pointer comparison rather than strcmp() to test for equality, which |
| 1339 | // is good because most of the time the comparisons are equal and so |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1340 | // the whole strings would have to be checked. |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1341 | if ( lineCC->loc.file != currFile ) { |
| 1342 | currFile = lineCC->loc.file; |
| 1343 | VG_(sprintf)(buf, "fl=%s\n", currFile); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1344 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1345 | distinct_files++; |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1346 | just_hit_a_new_file = True; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1347 | } |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1348 | // If we've hit a new function, print a "fn=" line. We know to do |
| 1349 | // this when the function name changes, and also every time we hit a |
| 1350 | // new file (in which case the new function name might be the same as |
| 1351 | // in the old file, hence the just_hit_a_new_file test). |
| 1352 | if ( just_hit_a_new_file || lineCC->loc.fn != currFn ) { |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1353 | currFn = lineCC->loc.fn; |
| 1354 | VG_(sprintf)(buf, "fn=%s\n", currFn); |
| 1355 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1356 | distinct_fns++; |
njn | 4311fe6 | 2005-12-08 23:18:50 +0000 | [diff] [blame] | 1357 | just_hit_a_new_file = False; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | // Print the LineCC |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1361 | if (clo_cache_sim && clo_branch_sim) { |
| 1362 | VG_(sprintf)(buf, "%u %llu %llu %llu" |
| 1363 | " %llu %llu %llu" |
| 1364 | " %llu %llu %llu" |
| 1365 | " %llu %llu %llu %llu\n", |
| 1366 | lineCC->loc.line, |
| 1367 | lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.m2, |
| 1368 | lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.m2, |
| 1369 | lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.m2, |
| 1370 | lineCC->Bc.b, lineCC->Bc.mp, |
| 1371 | lineCC->Bi.b, lineCC->Bi.mp); |
| 1372 | } |
| 1373 | else if (clo_cache_sim && !clo_branch_sim) { |
| 1374 | VG_(sprintf)(buf, "%u %llu %llu %llu" |
| 1375 | " %llu %llu %llu" |
| 1376 | " %llu %llu %llu\n", |
| 1377 | lineCC->loc.line, |
| 1378 | lineCC->Ir.a, lineCC->Ir.m1, lineCC->Ir.m2, |
| 1379 | lineCC->Dr.a, lineCC->Dr.m1, lineCC->Dr.m2, |
| 1380 | lineCC->Dw.a, lineCC->Dw.m1, lineCC->Dw.m2); |
| 1381 | } |
| 1382 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1383 | VG_(sprintf)(buf, "%u %llu" |
| 1384 | " %llu %llu %llu %llu\n", |
| 1385 | lineCC->loc.line, |
| 1386 | lineCC->Ir.a, |
| 1387 | lineCC->Bc.b, lineCC->Bc.mp, |
| 1388 | lineCC->Bi.b, lineCC->Bi.mp); |
| 1389 | } |
| 1390 | else |
| 1391 | tl_assert(0); |
| 1392 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1393 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1394 | |
| 1395 | // Update summary stats |
| 1396 | Ir_total.a += lineCC->Ir.a; |
| 1397 | Ir_total.m1 += lineCC->Ir.m1; |
| 1398 | Ir_total.m2 += lineCC->Ir.m2; |
| 1399 | Dr_total.a += lineCC->Dr.a; |
| 1400 | Dr_total.m1 += lineCC->Dr.m1; |
| 1401 | Dr_total.m2 += lineCC->Dr.m2; |
| 1402 | Dw_total.a += lineCC->Dw.a; |
| 1403 | Dw_total.m1 += lineCC->Dw.m1; |
| 1404 | Dw_total.m2 += lineCC->Dw.m2; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1405 | Bc_total.b += lineCC->Bc.b; |
| 1406 | Bc_total.mp += lineCC->Bc.mp; |
| 1407 | Bi_total.b += lineCC->Bi.b; |
| 1408 | Bi_total.mp += lineCC->Bi.mp; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1409 | |
| 1410 | distinct_lines++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1413 | // Summary stats must come after rest of table, since we calculate them |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1414 | // during traversal. */ |
| 1415 | if (clo_cache_sim && clo_branch_sim) { |
| 1416 | VG_(sprintf)(buf, "summary:" |
| 1417 | " %llu %llu %llu" |
| 1418 | " %llu %llu %llu" |
| 1419 | " %llu %llu %llu" |
| 1420 | " %llu %llu %llu %llu\n", |
| 1421 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 1422 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 1423 | Dw_total.a, Dw_total.m1, Dw_total.m2, |
| 1424 | Bc_total.b, Bc_total.mp, |
| 1425 | Bi_total.b, Bi_total.mp); |
| 1426 | } |
| 1427 | else if (clo_cache_sim && !clo_branch_sim) { |
| 1428 | VG_(sprintf)(buf, "summary:" |
| 1429 | " %llu %llu %llu" |
| 1430 | " %llu %llu %llu" |
| 1431 | " %llu %llu %llu\n", |
| 1432 | Ir_total.a, Ir_total.m1, Ir_total.m2, |
| 1433 | Dr_total.a, Dr_total.m1, Dr_total.m2, |
| 1434 | Dw_total.a, Dw_total.m1, Dw_total.m2); |
| 1435 | } |
| 1436 | else if (!clo_cache_sim && clo_branch_sim) { |
| 1437 | VG_(sprintf)(buf, "summary:" |
| 1438 | " %llu" |
| 1439 | " %llu %llu %llu %llu\n", |
| 1440 | Ir_total.a, |
| 1441 | Bc_total.b, Bc_total.mp, |
| 1442 | Bi_total.b, Bi_total.mp); |
| 1443 | } |
| 1444 | else |
| 1445 | tl_assert(0); |
| 1446 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1447 | VG_(write)(fd, (void*)buf, VG_(strlen)(buf)); |
| 1448 | VG_(close)(fd); |
| 1449 | } |
| 1450 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1451 | static UInt ULong_width(ULong n) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1452 | { |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1453 | UInt w = 0; |
| 1454 | while (n > 0) { |
| 1455 | n = n / 10; |
| 1456 | w++; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1457 | } |
sewardj | 46c59b1 | 2005-11-01 02:20:19 +0000 | [diff] [blame] | 1458 | if (w == 0) w = 1; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1459 | return w + (w-1)/3; // add space for commas |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1462 | static void cg_fini(Int exitcode) |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1463 | { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1464 | static Char buf1[128], buf2[128], buf3[128], buf4[123], fmt[128]; |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1465 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1466 | CacheCC D_total; |
| 1467 | BranchCC B_total; |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1468 | ULong L2_total_m, L2_total_mr, L2_total_mw, |
| 1469 | L2_total, L2_total_r, L2_total_w; |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1470 | Int l1, l2, l3, l4; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1471 | Int p; |
| 1472 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1473 | /* Running with both cache and branch simulation disabled is not |
| 1474 | allowed (checked during command line option processing). */ |
| 1475 | tl_assert(clo_cache_sim || clo_branch_sim); |
| 1476 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1477 | fprint_CC_table_and_calc_totals(); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1478 | |
njn | 7cf0bd3 | 2002-06-08 13:36:03 +0000 | [diff] [blame] | 1479 | if (VG_(clo_verbosity) == 0) |
| 1480 | return; |
| 1481 | |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1482 | /* I cache results. Use the I_refs value to determine the first column |
| 1483 | * width. */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1484 | l1 = ULong_width(Ir_total.a); |
| 1485 | l2 = ULong_width(Dr_total.a); |
| 1486 | l3 = ULong_width(Dw_total.a); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1487 | l4 = ULong_width(Bc_total.b + Bi_total.b); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1488 | |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1489 | /* Make format string, getting width right for numbers */ |
njn | 99cb9e3 | 2005-09-25 17:59:16 +0000 | [diff] [blame] | 1490 | VG_(sprintf)(fmt, "%%s %%,%dllu", l1); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1491 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1492 | /* Always print this */ |
njn | 607adfc | 2003-09-30 14:15:44 +0000 | [diff] [blame] | 1493 | VG_(message)(Vg_UserMsg, fmt, "I refs: ", Ir_total.a); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1494 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1495 | /* If cache profiling is enabled, show D access numbers and all |
| 1496 | miss numbers */ |
| 1497 | if (clo_cache_sim) { |
| 1498 | VG_(message)(Vg_UserMsg, fmt, "I1 misses: ", Ir_total.m1); |
| 1499 | VG_(message)(Vg_UserMsg, fmt, "L2i misses: ", Ir_total.m2); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1500 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1501 | p = 100; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1502 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1503 | if (0 == Ir_total.a) Ir_total.a = 1; |
| 1504 | VG_(percentify)(Ir_total.m1, Ir_total.a, 2, l1+1, buf1); |
| 1505 | VG_(message)(Vg_UserMsg, "I1 miss rate: %s", buf1); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1506 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1507 | VG_(percentify)(Ir_total.m2, Ir_total.a, 2, l1+1, buf1); |
| 1508 | VG_(message)(Vg_UserMsg, "L2i miss rate: %s", buf1); |
| 1509 | VG_(message)(Vg_UserMsg, ""); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1510 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1511 | /* D cache results. Use the D_refs.rd and D_refs.wr values to |
| 1512 | * determine the width of columns 2 & 3. */ |
| 1513 | D_total.a = Dr_total.a + Dw_total.a; |
| 1514 | D_total.m1 = Dr_total.m1 + Dw_total.m1; |
| 1515 | D_total.m2 = Dr_total.m2 + Dw_total.m2; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1516 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1517 | /* Make format string, getting width right for numbers */ |
| 1518 | VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu rd + %%,%dllu wr)", l1, l2, l3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1519 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1520 | VG_(message)(Vg_UserMsg, fmt, "D refs: ", |
| 1521 | D_total.a, Dr_total.a, Dw_total.a); |
| 1522 | VG_(message)(Vg_UserMsg, fmt, "D1 misses: ", |
| 1523 | D_total.m1, Dr_total.m1, Dw_total.m1); |
| 1524 | VG_(message)(Vg_UserMsg, fmt, "L2d misses: ", |
| 1525 | D_total.m2, Dr_total.m2, Dw_total.m2); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1526 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1527 | p = 10; |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1528 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1529 | if (0 == D_total.a) D_total.a = 1; |
| 1530 | if (0 == Dr_total.a) Dr_total.a = 1; |
| 1531 | if (0 == Dw_total.a) Dw_total.a = 1; |
| 1532 | VG_(percentify)( D_total.m1, D_total.a, 1, l1+1, buf1); |
| 1533 | VG_(percentify)(Dr_total.m1, Dr_total.a, 1, l2+1, buf2); |
| 1534 | VG_(percentify)(Dw_total.m1, Dw_total.a, 1, l3+1, buf3); |
| 1535 | VG_(message)(Vg_UserMsg, "D1 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1536 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1537 | VG_(percentify)( D_total.m2, D_total.a, 1, l1+1, buf1); |
| 1538 | VG_(percentify)(Dr_total.m2, Dr_total.a, 1, l2+1, buf2); |
| 1539 | VG_(percentify)(Dw_total.m2, Dw_total.a, 1, l3+1, buf3); |
| 1540 | VG_(message)(Vg_UserMsg, "L2d miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1541 | VG_(message)(Vg_UserMsg, ""); |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1542 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1543 | /* L2 overall results */ |
njn | 1d021fa | 2002-05-02 13:56:34 +0000 | [diff] [blame] | 1544 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1545 | L2_total = Dr_total.m1 + Dw_total.m1 + Ir_total.m1; |
| 1546 | L2_total_r = Dr_total.m1 + Ir_total.m1; |
| 1547 | L2_total_w = Dw_total.m1; |
| 1548 | VG_(message)(Vg_UserMsg, fmt, "L2 refs: ", |
| 1549 | L2_total, L2_total_r, L2_total_w); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1550 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1551 | L2_total_m = Dr_total.m2 + Dw_total.m2 + Ir_total.m2; |
| 1552 | L2_total_mr = Dr_total.m2 + Ir_total.m2; |
| 1553 | L2_total_mw = Dw_total.m2; |
| 1554 | VG_(message)(Vg_UserMsg, fmt, "L2 misses: ", |
| 1555 | L2_total_m, L2_total_mr, L2_total_mw); |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1556 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1557 | VG_(percentify)(L2_total_m, (Ir_total.a + D_total.a), 1, l1+1, buf1); |
| 1558 | VG_(percentify)(L2_total_mr, (Ir_total.a + Dr_total.a), 1, l2+1, buf2); |
| 1559 | VG_(percentify)(L2_total_mw, Dw_total.a, 1, l3+1, buf3); |
| 1560 | VG_(message)(Vg_UserMsg, "L2 miss rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1561 | } |
| 1562 | |
| 1563 | /* If branch profiling is enabled, show branch overall results. */ |
| 1564 | if (clo_branch_sim) { |
| 1565 | /* Make format string, getting width right for numbers */ |
| 1566 | VG_(sprintf)(fmt, "%%s %%,%dllu (%%,%dllu cond + %%,%dllu ind)", l1, l2, l3); |
| 1567 | |
| 1568 | if (0 == Bc_total.b) Bc_total.b = 1; |
| 1569 | if (0 == Bi_total.b) Bi_total.b = 1; |
| 1570 | B_total.b = Bc_total.b + Bi_total.b; |
| 1571 | B_total.mp = Bc_total.mp + Bi_total.mp; |
| 1572 | |
| 1573 | VG_(message)(Vg_UserMsg, ""); |
| 1574 | VG_(message)(Vg_UserMsg, fmt, "Branches: ", |
| 1575 | B_total.b, Bc_total.b, Bi_total.b); |
| 1576 | |
| 1577 | VG_(message)(Vg_UserMsg, fmt, "Mispredicts: ", |
| 1578 | B_total.mp, Bc_total.mp, Bi_total.mp); |
| 1579 | |
| 1580 | VG_(percentify)(B_total.mp, B_total.b, 1, l1+1, buf1); |
| 1581 | VG_(percentify)(Bc_total.mp, Bc_total.b, 1, l2+1, buf2); |
| 1582 | VG_(percentify)(Bi_total.mp, Bi_total.b, 1, l3+1, buf3); |
| 1583 | |
| 1584 | VG_(message)(Vg_UserMsg, "Mispred rate: %s (%s + %s )", buf1, buf2,buf3); |
| 1585 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1586 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1587 | // Various stats |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1588 | if (VG_(clo_verbosity) > 1) { |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1589 | Int debug_lookups = full_debugs + fn_debugs + |
| 1590 | file_line_debugs + no_debugs; |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1591 | |
njn | 1baf7db | 2006-04-18 22:34:48 +0000 | [diff] [blame] | 1592 | VG_(message)(Vg_DebugMsg, ""); |
| 1593 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct files: %d", distinct_files); |
| 1594 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct fns: %d", distinct_fns); |
| 1595 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct lines: %d", distinct_lines); |
| 1596 | VG_(message)(Vg_DebugMsg, "cachegrind: distinct instrs:%d", distinct_instrs); |
| 1597 | VG_(message)(Vg_DebugMsg, "cachegrind: debug lookups : %d", debug_lookups); |
| 1598 | |
| 1599 | VG_(percentify)(full_debugs, debug_lookups, 1, 6, buf1); |
| 1600 | VG_(percentify)(file_line_debugs, debug_lookups, 1, 6, buf2); |
| 1601 | VG_(percentify)(fn_debugs, debug_lookups, 1, 6, buf3); |
| 1602 | VG_(percentify)(no_debugs, debug_lookups, 1, 6, buf4); |
| 1603 | VG_(message)(Vg_DebugMsg, "cachegrind: with full info:%s (%d)", |
| 1604 | buf1, full_debugs); |
| 1605 | VG_(message)(Vg_DebugMsg, "cachegrind: with file/line info:%s (%d)", |
| 1606 | buf2, file_line_debugs); |
| 1607 | VG_(message)(Vg_DebugMsg, "cachegrind: with fn name info:%s (%d)", |
| 1608 | buf3, fn_debugs); |
| 1609 | VG_(message)(Vg_DebugMsg, "cachegrind: with zero info:%s (%d)", |
| 1610 | buf4, no_debugs); |
| 1611 | |
| 1612 | VG_(message)(Vg_DebugMsg, "cachegrind: string table size: %u", |
| 1613 | VG_(OSet_Size)(stringTable)); |
| 1614 | VG_(message)(Vg_DebugMsg, "cachegrind: CC table size: %u", |
| 1615 | VG_(OSet_Size)(CC_table)); |
| 1616 | VG_(message)(Vg_DebugMsg, "cachegrind: InstrInfo table size: %u", |
| 1617 | VG_(OSet_Size)(instrInfoTable)); |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1618 | } |
njn | 4f9c934 | 2002-04-29 16:03:24 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1621 | /*--------------------------------------------------------------------*/ |
| 1622 | /*--- Discarding BB info ---*/ |
| 1623 | /*--------------------------------------------------------------------*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1624 | |
sewardj | a3a29a5 | 2005-10-12 16:16:03 +0000 | [diff] [blame] | 1625 | // Called when a translation is removed from the translation cache for |
| 1626 | // any reason at all: to free up space, because the guest code was |
| 1627 | // unmapped or modified, or for any arbitrary reason. |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1628 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1629 | void cg_discard_superblock_info ( Addr64 orig_addr64, VexGuestExtents vge ) |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1630 | { |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1631 | SB_info* sbInfo; |
sewardj | 3a384b3 | 2006-01-22 01:12:51 +0000 | [diff] [blame] | 1632 | Addr orig_addr = (Addr)vge.base[0]; |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1633 | |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1634 | tl_assert(vge.n_used > 0); |
| 1635 | |
| 1636 | if (DEBUG_CG) |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1637 | VG_(printf)( "discard_basic_block_info: %p, %p, %llu\n", |
| 1638 | (void*)(Addr)orig_addr, |
sewardj | 5155dec | 2005-10-12 10:09:23 +0000 | [diff] [blame] | 1639 | (void*)(Addr)vge.base[0], (ULong)vge.len[0]); |
njn | 4294fd4 | 2002-06-05 14:41:10 +0000 | [diff] [blame] | 1640 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1641 | // Get BB info, remove from table, free BB info. Simple! Note that we |
| 1642 | // use orig_addr, not the first instruction address in vge. |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1643 | sbInfo = VG_(OSet_Remove)(instrInfoTable, &orig_addr); |
| 1644 | tl_assert(NULL != sbInfo); |
| 1645 | VG_(OSet_FreeNode)(instrInfoTable, sbInfo); |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /*--------------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1649 | /*--- Command line processing ---*/ |
| 1650 | /*--------------------------------------------------------------------*/ |
| 1651 | |
njn | 0103de5 | 2005-10-10 16:49:01 +0000 | [diff] [blame] | 1652 | static void parse_cache_opt ( cache_t* cache, Char* opt ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1653 | { |
njn | 0103de5 | 2005-10-10 16:49:01 +0000 | [diff] [blame] | 1654 | Int i = 0, i2, i3; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1655 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1656 | // Option argument looks like "65536,2,64". |
| 1657 | // Find commas, replace with NULs to make three independent |
| 1658 | // strings, then extract numbers, put NULs back. Yuck. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1659 | while (VG_(isdigit)(opt[i])) i++; |
| 1660 | if (',' == opt[i]) { |
| 1661 | opt[i++] = '\0'; |
| 1662 | i2 = i; |
| 1663 | } else goto bad; |
| 1664 | while (VG_(isdigit)(opt[i])) i++; |
| 1665 | if (',' == opt[i]) { |
| 1666 | opt[i++] = '\0'; |
| 1667 | i3 = i; |
| 1668 | } else goto bad; |
| 1669 | while (VG_(isdigit)(opt[i])) i++; |
| 1670 | if ('\0' != opt[i]) goto bad; |
| 1671 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1672 | cache->size = (Int)VG_(atoll)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1673 | cache->assoc = (Int)VG_(atoll)(opt + i2); |
| 1674 | cache->line_size = (Int)VG_(atoll)(opt + i3); |
| 1675 | |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1676 | opt[i2-1] = ','; |
| 1677 | opt[i3-1] = ','; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1678 | return; |
| 1679 | |
| 1680 | bad: |
sewardj | 6893d65 | 2006-10-15 01:25:13 +0000 | [diff] [blame] | 1681 | VG_(err_bad_option)(opt); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1684 | static Bool cg_process_cmd_line_option(Char* arg) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1685 | { |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1686 | // 5 is length of "--I1=" |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1687 | if (VG_CLO_STREQN(5, arg, "--I1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1688 | parse_cache_opt(&clo_I1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1689 | else if (VG_CLO_STREQN(5, arg, "--D1=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1690 | parse_cache_opt(&clo_D1_cache, &arg[5]); |
njn | 39c8665 | 2003-05-21 10:13:39 +0000 | [diff] [blame] | 1691 | else if (VG_CLO_STREQN(5, arg, "--L2=")) |
nethercote | 9313ac4 | 2004-07-06 21:54:20 +0000 | [diff] [blame] | 1692 | parse_cache_opt(&clo_L2_cache, &arg[5]); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1693 | else if (VG_CLO_STREQN(22, arg, "--cachegrind-out-file=")) { |
| 1694 | cachegrind_out_file_basename = &arg[22]; |
| 1695 | } |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1696 | else VG_BOOL_CLO(arg, "--cache-sim", clo_cache_sim) |
| 1697 | else VG_BOOL_CLO(arg, "--branch-sim", clo_branch_sim) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1698 | else |
| 1699 | return False; |
| 1700 | |
| 1701 | return True; |
| 1702 | } |
| 1703 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1704 | static void cg_print_usage(void) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1705 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1706 | VG_(printf)( |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1707 | " --I1=<size>,<assoc>,<line_size> set I1 cache manually\n" |
| 1708 | " --D1=<size>,<assoc>,<line_size> set D1 cache manually\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1709 | " --L2=<size>,<assoc>,<line_size> set L2 cache manually\n" |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1710 | " --cache-sim=yes|no [yes] collect cache stats?\n" |
| 1711 | " --branch-sim=yes|no [no] collect branch prediction stats?\n" |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1712 | " --cachegrind-out-file=<file> write profile data to <file>.<pid>\n" |
| 1713 | " [cachegrind.out.<pid>]\n" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1714 | ); |
| 1715 | } |
| 1716 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1717 | static void cg_print_debug_usage(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1718 | { |
| 1719 | VG_(printf)( |
| 1720 | " (none)\n" |
| 1721 | ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /*--------------------------------------------------------------------*/ |
| 1725 | /*--- Setup ---*/ |
| 1726 | /*--------------------------------------------------------------------*/ |
| 1727 | |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 1728 | static Char base_dir[VKI_PATH_MAX]; |
| 1729 | |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1730 | static void cg_post_clo_init(void); /* just below */ |
| 1731 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1732 | static void cg_pre_clo_init(void) |
| 1733 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1734 | VG_(details_name) ("Cachegrind"); |
| 1735 | VG_(details_version) (NULL); |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1736 | VG_(details_description) ("a cache and branch-prediction profiler"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1737 | VG_(details_copyright_author)( |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 1738 | "Copyright (C) 2002-2007, and GNU GPL'd, by Nicholas Nethercote et al."); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1739 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 1740 | VG_(details_avg_translation_sizeB) ( 500 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1741 | |
| 1742 | VG_(basic_tool_funcs) (cg_post_clo_init, |
| 1743 | cg_instrument, |
| 1744 | cg_fini); |
| 1745 | |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1746 | VG_(needs_superblock_discards)(cg_discard_superblock_info); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1747 | VG_(needs_command_line_options)(cg_process_cmd_line_option, |
| 1748 | cg_print_usage, |
| 1749 | cg_print_debug_usage); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static void cg_post_clo_init(void) |
| 1753 | { |
| 1754 | HChar* qual = NULL; |
| 1755 | cache_t I1c, D1c, L2c; |
| 1756 | Int filename_szB; |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1757 | |
sewardj | 8badbaa | 2007-05-08 09:20:25 +0000 | [diff] [blame] | 1758 | /* Can't disable both cache and branch profiling */ |
| 1759 | if ((!clo_cache_sim) && (!clo_branch_sim)) { |
| 1760 | VG_(message)(Vg_DebugMsg, |
| 1761 | "ERROR: --cache-sim=no --branch-sim=no is not allowed."); |
| 1762 | VG_(message)(Vg_DebugMsg, |
| 1763 | "You must select cache profiling, or branch profiling, or both."); |
| 1764 | VG_(exit)(2); |
| 1765 | } |
| 1766 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1767 | /* Get working directory */ |
sewardj | 198f34f | 2007-07-09 23:13:07 +0000 | [diff] [blame] | 1768 | tl_assert( VG_(get_startup_wd)(base_dir, VKI_PATH_MAX) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1769 | |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1770 | /* Do we have a --log-file-qualifier= to consider? */ |
| 1771 | if (VG_(clo_log_file_qualifier)) { |
| 1772 | qual = VG_(getenv)(VG_(clo_log_file_qualifier)); |
| 1773 | } |
| 1774 | |
| 1775 | /* Block is big enough for |
| 1776 | dir name ++ cachegrind_out_file_basename |
| 1777 | ++ ".<pid>" |
| 1778 | ++ the log file qualifier, if in use */ |
| 1779 | filename_szB |
| 1780 | = VG_(strlen)(base_dir) |
| 1781 | + 1 /* "/" */ |
| 1782 | + VG_(strlen)(cachegrind_out_file_basename) |
| 1783 | + 11 /* "." <pid>, assuming sizeof(pid) <= 4 */ |
| 1784 | + (qual ? (10 + VG_(strlen)(qual)) : 0) |
| 1785 | + 1; /* to guarantee checkable zero at the end */ |
| 1786 | |
| 1787 | tl_assert(filename_szB > 0); |
| 1788 | cachegrind_out_file |
| 1789 | = VG_(calloc)( sizeof(Char), filename_szB ); |
| 1790 | |
| 1791 | if (qual) { |
| 1792 | VG_(sprintf)(cachegrind_out_file, "%s/%s.%d.lfq.%s", |
| 1793 | base_dir, cachegrind_out_file_basename, |
| 1794 | VG_(getpid)(), qual); |
| 1795 | } else { |
| 1796 | VG_(sprintf)(cachegrind_out_file, "%s/%s.%d", |
| 1797 | base_dir, cachegrind_out_file_basename, |
| 1798 | VG_(getpid)()); |
| 1799 | } |
| 1800 | |
| 1801 | tl_assert( cachegrind_out_file[filename_szB-1] == 0 ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1802 | |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1803 | CC_table = VG_(OSet_Create)(offsetof(LineCC, loc), |
| 1804 | cmp_CodeLoc_LineCC, |
| 1805 | VG_(malloc), VG_(free)); |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1806 | instrInfoTable = VG_(OSet_Create)(/*keyOff*/0, |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1807 | NULL, |
| 1808 | VG_(malloc), VG_(free)); |
| 1809 | stringTable = VG_(OSet_Create)(/*keyOff*/0, |
| 1810 | stringCmp, |
| 1811 | VG_(malloc), VG_(free)); |
sewardj | e1216cb | 2007-02-07 19:55:30 +0000 | [diff] [blame] | 1812 | |
| 1813 | configure_caches(&I1c, &D1c, &L2c); |
| 1814 | |
| 1815 | cachesim_I1_initcache(I1c); |
| 1816 | cachesim_D1_initcache(D1c); |
| 1817 | cachesim_L2_initcache(L2c); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1820 | VG_DETERMINE_INTERFACE_VERSION(cg_pre_clo_init) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 1821 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1822 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1823 | /*--- end ---*/ |
sewardj | 18d7513 | 2002-05-16 11:06:21 +0000 | [diff] [blame] | 1824 | /*--------------------------------------------------------------------*/ |
njn | d3bef4f | 2005-10-15 17:46:18 +0000 | [diff] [blame] | 1825 | |