| 1 | /* |
| 2 | * ========== Copyright Header Begin ========================================== |
| 3 | * |
| 4 | * OpenSPARC T2 Processor File: rstzip3.h |
| 5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. |
| 6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. |
| 7 | * |
| 8 | * The above named program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public |
| 10 | * License version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * The above named program is distributed in the hope that it will be |
| 13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this work; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | * |
| 21 | * ========== Copyright Header End ============================================ |
| 22 | */ |
| 23 | /* rstzip3.h |
| 24 | * rz3 compressor/decompressor metadata structures |
| 25 | * |
| 26 | * Copyright (C) 2003 Sun Microsystems, Inc. |
| 27 | * All Rights Reserved |
| 28 | */ |
| 29 | |
| 30 | #ifndef _rstzip3_h_ |
| 31 | #define _rstzip3_h_ |
| 32 | |
| 33 | #ident "@(#)1.6 06/28/04 SMI rstzip3.h" |
| 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include "zlib.h" |
| 37 | |
| 38 | #include "rstf/rstf.h" |
| 39 | |
| 40 | |
| 41 | static const int rstzip3_major_version = 3; |
| 42 | static const int rstzip3_minor_version = 23; |
| 43 | static const char rstzip3_version_str[] = "rstzip v3.23"; |
| 44 | |
| 45 | static const int rz3_bufsize_default = 32<<10; |
| 46 | |
| 47 | // In rstzip3 v3.22 the buffer size was reduced to 32k to reduce |
| 48 | // the memory foot-print of the java trace reader. This constant |
| 49 | // is used for decompressing traces older than rstzip3 v3.22 |
| 50 | static const int rz3_bufsize_pre322 = 128 << 10; |
| 51 | |
| 52 | /* we use a 16-character magic string to identify an rz3 compressed trace |
| 53 | * starting with "RZ3 " and ending with a $ and the null-terminator (\0). |
| 54 | * The remainder (16-4-2) is a 10-digit random integer |
| 55 | */ |
| 56 | #define rz3_hdr_magic "RZ3 5948486328$" |
| 57 | |
| 58 | struct rz3_header { |
| 59 | char magic[16]; |
| 60 | |
| 61 | /* *internal* version numbers for rz3. The global rstzip version number is, obviously, 3 */ |
| 62 | uint8_t major_version; |
| 63 | uint8_t minor_version; |
| 64 | |
| 65 | uint16_t reserved16; |
| 66 | uint32_t reserved32; |
| 67 | }; // struct rz3_header |
| 68 | |
| 69 | |
| 70 | static const int rz3_max_ncpus = 1<<10; |
| 71 | |
| 72 | static const uint64_t rz3_amask_mask = ((1ull<<32)-1); |
| 73 | |
| 74 | class rstzip3 { |
| 75 | public: |
| 76 | rstzip3(const char * fname, const char * mode); |
| 77 | |
| 78 | ~rstzip3(); |
| 79 | |
| 80 | int getMajorVersion() { |
| 81 | return rstzip3_major_version; |
| 82 | } |
| 83 | |
| 84 | int getMinorVersion() { |
| 85 | return rstzip3_minor_version; |
| 86 | } |
| 87 | |
| 88 | const char * getVersionStr() { |
| 89 | return rstzip3_version_str; |
| 90 | } |
| 91 | |
| 92 | int compress(rstf_unionT * buf, int nrec); |
| 93 | |
| 94 | int decompress(rstf_unionT * buf, int nrec); |
| 95 | |
| 96 | bool error(); |
| 97 | |
| 98 | void setverbose(); |
| 99 | void setstats(); |
| 100 | |
| 101 | int rz3_bufsize; |
| 102 | |
| 103 | private: |
| 104 | int compress_buffer(rstf_unionT * rstbuf, int rstbufsize); |
| 105 | void compress_inst(rstf_unionT * rstbuf, int idx); |
| 106 | void compress_dcti(rstf_unionT * rstbuf, int idx, struct rz3iu_icache_data * icdata); |
| 107 | void compress_pavadiff(rstf_unionT * rstbuf, int idx); |
| 108 | void compress_tlb(rstf_unionT * rstbuf, int idx); |
| 109 | void compress_preg(rstf_unionT * rstbuf, int idx); |
| 110 | void compress_trap(rstf_unionT * rstbuf, int idx); |
| 111 | void compress_dma(rstf_unionT * rstbuf, int idx); |
| 112 | void compress_regval(rstf_unionT * rstbuf, int idx); |
| 113 | void compress_memval(rstf_unionT * rstbuf, int idx); |
| 114 | void compress_ea_va(rstf_unionT * rstbuf, int idx); |
| 115 | void compress_rfs_cw(rstf_unionT * rstbuf, int idx); |
| 116 | void compress_rfs_bt(rstf_unionT * rstbuf, int idx); |
| 117 | |
| 118 | bool compress_value(int cpuid, uint64_t v64); |
| 119 | |
| 120 | bool regen_value(rstf_regvalT *vr, int idx); |
| 121 | |
| 122 | int decompress_buffer(rstf_unionT * rstbuf, int rstbufsize); |
| 123 | void decompress_inst(rstf_unionT * rstbuf, int idx); |
| 124 | void decompress_dcti(rstf_unionT * rstbuf, int idx, struct rz3iu_icache_data * icdata); |
| 125 | void decompress_pavadiff(rstf_unionT * rstbuf, int idx); |
| 126 | void decompress_pavadiff_pass2(rstf_unionT * rstbuf, int instr_idx); |
| 127 | void decompress_tlb(rstf_unionT * rstbuf, int idx); |
| 128 | void decompress_preg(rstf_unionT * rstbuf, int idx); |
| 129 | void decompress_trap(rstf_unionT * rstbuf, int idx); |
| 130 | void decompress_dma(rstf_unionT * rstbuf, int idx); |
| 131 | void decompress_regval(rstf_unionT * rstbuf, int idx); |
| 132 | void decompress_memval(rstf_unionT * rstbuf, int idx); |
| 133 | void decompress_ea_va(rstf_unionT * rstbuf, int idx); |
| 134 | void decompress_rfs_cw(rstf_unionT * rstbuf, int idx); |
| 135 | void decompress_rfs_bt(rstf_unionT * rstbuf, int idx); |
| 136 | |
| 137 | bool decompress_value(int cpuid, uint64_t & v64); |
| 138 | |
| 139 | // backward compatibility support |
| 140 | void decompress_pavadiff_pass2_v315(rstf_unionT * rstbuf, int instr_idx); |
| 141 | |
| 142 | public: |
| 143 | bool c_nd; // compress, not decompress |
| 144 | bool verbose; |
| 145 | bool stats; |
| 146 | |
| 147 | int64_t raw_v64_count; |
| 148 | |
| 149 | private: |
| 150 | |
| 151 | bool rstf_pre212; // input rst trace is older than v2.12 (during compression) - doesn't matter during decompression |
| 152 | |
| 153 | bool pre320; // rstzip version is v3.19 or older (during decompression) |
| 154 | |
| 155 | bool pre323; // rstzip version is v3.22 or older (during decompression) |
| 156 | |
| 157 | bool g0_nonzero_warn; |
| 158 | |
| 159 | struct rz3_header * header; |
| 160 | struct rz3_section_header * shdr; |
| 161 | struct rz3_section_data * sdata; |
| 162 | |
| 163 | gzFile gzf; |
| 164 | |
| 165 | bool rz3_error; |
| 166 | |
| 167 | int nsections; // incremented every time a section is completed and written out, or read in and decompressed |
| 168 | |
| 169 | // int n_cpuids; |
| 170 | |
| 171 | // state variables used in prediction (other than per-cpu) |
| 172 | uint16_t last_instr_cpuid; // cpuid of the prev instr seen. we need this for records corresponding to this cpuid that occur after the instr record (eg regval) |
| 173 | uint16_t pred_cpuid; // cpuid predicted for the next instr/pavadiff etc |
| 174 | uint8_t instr_preds; // some prediction bits (pc, instr, tr, pr, bt, an, ea_valid) which are aggregated for each instr |
| 175 | bool rfs_phase; |
| 176 | bool rfs_cw_phase; |
| 177 | bool rfs_bt_phase; |
| 178 | uint64_t rfs_nrecords; |
| 179 | uint64_t rfs_records_seen; |
| 180 | uint8_t prev_rtype; |
| 181 | |
| 182 | // these state variables are cleared in clear() |
| 183 | |
| 184 | struct rz3_percpu_data ** tdata; |
| 185 | |
| 186 | // buffer used to store records in case the number of records requested |
| 187 | // is not optimal |
| 188 | rstf_unionT * interface_buffer; // size is rz3_bufsize |
| 189 | |
| 190 | // size of the interface buffer. when compressing, this is rz3_bufsize |
| 191 | // when decompressing, we get this size from the section header |
| 192 | int interface_buffer_size; |
| 193 | |
| 194 | int interface_buffer_count; |
| 195 | // count of records in the buffer waiting to be compressed/copied out |
| 196 | // we try to keep this buffer empty as far as possible. When empty, |
| 197 | // we try to compress/decompress directly using the caller's buffer. |
| 198 | // if not empty or if the caller's buffer is not large enough, we |
| 199 | // use the interface buffer. |
| 200 | |
| 201 | public: |
| 202 | |
| 203 | // rtypes we compress |
| 204 | enum rtype_key_e { |
| 205 | rtype_key_INSTR = 0, |
| 206 | rtype_key_REGVAL, |
| 207 | rtype_key_PAVADIFF, |
| 208 | rtype_key_RAW |
| 209 | }; // enum rtype_key_e |
| 210 | |
| 211 | enum instr_pred_bits { |
| 212 | instr_pred_pc = 0x1, |
| 213 | instr_mispred_pc = 0xfe, |
| 214 | instr_pred_instr = 0x2, |
| 215 | instr_mispred_instr = 0xfd, |
| 216 | instr_pred_bt = 0x4, |
| 217 | instr_mispred_bt = 0xfb, |
| 218 | instr_pred_an = 0x8, |
| 219 | instr_mispred_an = 0xf7, |
| 220 | instr_pred_tr = 0x10, |
| 221 | instr_mispred_tr = 0xef, |
| 222 | instr_pred_pr = 0x20, |
| 223 | instr_mispred_pr = 0xdf, |
| 224 | instr_pred_ea_valid = 0x40, |
| 225 | instr_mispred_ea_valid = 0xbf, |
| 226 | instr_pred_hpriv = 0x80, |
| 227 | instr_mispred_hpriv = 0x7f, |
| 228 | |
| 229 | instr_pred_all = 0xff, |
| 230 | instr_mispred_none = 0xff |
| 231 | }; // enum instr_pred_bits |
| 232 | |
| 233 | // enumerated type for various compressed bitarray sections |
| 234 | enum bitarrays_e { |
| 235 | |
| 236 | rtype_key_array=0, |
| 237 | rtype_array, |
| 238 | |
| 239 | cpuid_pred_array, |
| 240 | raw_cpuid_array, |
| 241 | |
| 242 | instr_pred_all_array, |
| 243 | instr_pred_raw_array, |
| 244 | |
| 245 | raw_instr_array, |
| 246 | |
| 247 | dcti_ea_va_pred_array, |
| 248 | |
| 249 | value_iszero_array, |
| 250 | |
| 251 | valuecache_level_array, |
| 252 | |
| 253 | // important: the valuecache data array names MUST be in the following order and must be followed by raw_value64_array |
| 254 | valuecache_data0_array, |
| 255 | valuecache_data1_array, |
| 256 | valuecache_data2_array, |
| 257 | valuecache_data3_array, |
| 258 | valuecache_data4_array, |
| 259 | valuecache_data5_array, |
| 260 | valuecache_data6_array, |
| 261 | |
| 262 | raw_value64_array, // IMPORTANT: raw_value64_array MUST immediately follow the valuecache data arrays |
| 263 | |
| 264 | regval_postInstr_array, |
| 265 | regval_regtype_pred_array, |
| 266 | regval_raw_regtype_array, |
| 267 | regval_regid_pred_array, |
| 268 | regval_raw_regid_array, |
| 269 | |
| 270 | memval_fields_array, |
| 271 | memval_size_array, |
| 272 | memval_addr36_43_array, |
| 273 | memval_addr04_35_array, |
| 274 | |
| 275 | pavadiff_ictxt_pred_array, |
| 276 | pavadiff_raw_ictxt_array, |
| 277 | pavadiff_pc_pa_va_pred_array, |
| 278 | pavadiff_ea_valid_array, |
| 279 | pavadiff_dctxt_pred_array, |
| 280 | pavadiff_raw_dctxt_array, |
| 281 | pavadiff_ea_pa_va_pred_array, |
| 282 | pavadiff_lookahead_array, |
| 283 | |
| 284 | tlb_info_array, |
| 285 | |
| 286 | trap_info_array, |
| 287 | |
| 288 | dma_iswrite_array, |
| 289 | dma_nbytes_array, |
| 290 | |
| 291 | rfs_rtype_pred_array, |
| 292 | rfs_pc_pred_array, |
| 293 | rfs_instr_pred_array, |
| 294 | rfs_bt_pred_array, |
| 295 | rfs_cw_raw_reftype_array, |
| 296 | rfs_raw_cpuid_array, |
| 297 | rfs_cw_dma_size_array, |
| 298 | rfs_cw_asi_array, |
| 299 | rfs_cw_pf_fcn_array, |
| 300 | rfs_cw_va_valid_array, |
| 301 | rfs_cw_pa_pred_array, |
| 302 | |
| 303 | bitarray_count |
| 304 | }; // enum bitarrays_e |
| 305 | |
| 306 | static struct rz3_bitarray_descr bitarray_descr[]; // initialized in rstzip3.C |
| 307 | |
| 308 | // performance statistics are reported as per record, per instr and optionally relative to another parameter |
| 309 | enum perf_stats_e { |
| 310 | |
| 311 | ps_nrecords=0, |
| 312 | ps_instr_count, |
| 313 | |
| 314 | ps_brpred_refs, |
| 315 | ps_brpred_misses, |
| 316 | |
| 317 | ps_an_misses, |
| 318 | |
| 319 | ps_ras_refs, |
| 320 | ps_ras_misses, |
| 321 | |
| 322 | ps_ea_valid_misses, |
| 323 | ps_ea_va_valuecache_refs, |
| 324 | ps_ea_va_valuecache_misses, |
| 325 | |
| 326 | ps_regval_records, |
| 327 | ps_regval_raw_regtype_count, |
| 328 | ps_regval_raw_regid_count, |
| 329 | ps_regval_valuecache_refs, |
| 330 | ps_regval_valuecache_misses, |
| 331 | |
| 332 | ps_pavadiff_count, |
| 333 | ps_pavadiff_ictxt_misses, |
| 334 | ps_pavadiff_raw_pc_pa_va, |
| 335 | ps_pavadiff_ea_valid_count, |
| 336 | ps_pavadiff_dctxt_misses, |
| 337 | ps_pavadiff_raw_ea_pa_va, |
| 338 | |
| 339 | ps_MAX |
| 340 | |
| 341 | }; // enum perf_stats_e |
| 342 | |
| 343 | int * perf_stats; |
| 344 | int64_t * perf_stat_totals; |
| 345 | |
| 346 | static struct rz3_perf_stats_descr perf_stats_descr []; |
| 347 | |
| 348 | void clear(); // clear prediction-related fields - |
| 349 | void clear_stats(); |
| 350 | void update_stats(); |
| 351 | void print_stats(); |
| 352 | void print_stat_totals(); |
| 353 | |
| 354 | FILE * testfp; // write records to testfp as soon as they are decompressed; so they can be compared with the orig file |
| 355 | }; // struct rz3 |
| 356 | |
| 357 | |
| 358 | /* rz3 compressed data is organized as a series of array dumps (rz3_bitarray and rz3_rst_array) |
| 359 | * all but one array is a "bitarray" (elements <= 64 bits in size). |
| 360 | * We allocate a list of bitarrays, and identify each bitarray by a name (enum list) |
| 361 | * The following structure is used to describe properties of each array |
| 362 | */ |
| 363 | struct rz3_bitarray_descr { |
| 364 | const char *name; |
| 365 | int nbits; |
| 366 | int size_hint; |
| 367 | }; // struct rz3_bitarray_descr |
| 368 | |
| 369 | |
| 370 | struct rz3_perf_stats_descr { |
| 371 | const char * name; |
| 372 | enum rstzip3::perf_stats_e rel; // print stats relative to this parameters. ps_MAX => none |
| 373 | }; // struct rz3_perf_stats_descr |
| 374 | |
| 375 | |
| 376 | #endif // _rstzip3_h_ |
| 377 | |