memstat.c 26.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
/*
 * Copyright (C) 1996-1998 by the Board of Trustees
 *    of Leland Stanford Junior University.
 * 
 * This file is part of the SimOS distribution. 
 * See LICENSE file for terms of the license. 
 *
 */

/****************************************************************
 * memstat.c
 *
 * Keep track of memory cache lines and the transitions that they go
 * through.  
 * 
 * Last Modified by: $Author: apatti $
 * $Date: 2003/08/18 16:31:26 $
 ****************************************************************/
 
/* #define DEBUGGING */ 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <stdarg.h>
#include "sim_error.h"
#include "simutil.h" 
#include "simrecord.h" 
#include "cpu_interface.h"
#include "memstat.h"
#include "annotations.h"
#include "machine_params.h"
#include "statrecord.h"
#include "false_sharing.h"
#include "hw_events.h"
#include "tcl_init.h"

/* Get rid of this */
#include "../../memsystems/memsys.h"
#if 0 //XXX
#define STAT_MEM_SIZE(x)	MEM_SIZE(x)
#else
#define STAT_MEM_SIZE(x)	0x1fd00000
#endif

/*******************************************************************
 * The #define NO_RACE_CONDITION option can be used to debug the 
 * caches by verifying that only certain state transitions actually
 * take place. NO_RACE_CONDITION can only be used with UMA,NUMA and
 * PerfectMem, not with Flash-lite 
 *******************************************************************/
#define NO_RACE_CONDITION ( memsysVec.type != FLASHLITE )

/********************************************************************
 * MemStat state machine. 
 * This simple FSM consists of the states CACHE_COLD ... CACHE_WARMED
 * and is managed for every physical line and processor of the machine
 * The state is encoded in 16 bits, the top 12 being reserved to store
 * the PID of the process that evicted the line. 
 ********************************************************************/

#define CACHE_COLD        0  
#define CACHE_REPLACED_K  1
#define CACHE_REPLACED_U  2
#define CACHE_INVALIDATED 3
#define CACHE_REPL_INV    4
#define CACHE_INCACHE     5
#define CACHE_WARMED      6

char *categName[] =  {"ice","cold",
                      "inv","capinv",
                      "cap","Kcap","SelfCap","OtherCap"};

#define MS_MAXPID         4095  /* 2**12 -1 */
#define PIDOFFSET         4
#define MEM_LINE(_pA)     ((_pA)>>log2SCACHE_LINE_SIZE)
#define MEM_L1D_LINE(_pA) ((_pA)>>log2DCACHE_LINE_SIZE)
#define MEM_L1I_LINE(_pA) ((_pA)>>log2ICACHE_LINE_SIZE)

#define MS_pid(_cpu) 0

#define MemStatNone 0
#define MemStatGlobal 1
#define MemStatGlobalPID 2


int memstatOption = MemStatGlobal;

static struct MemStatState {
   /* State machine turned on or not! */
   int init;  
   int trackPID;
   int trackL1;

   uint cacheMask;
   int bitsPerState;
   /* L2 State machines */
   uint lastAddr[MAX_MACHINES];
   uint trNumCPUs[MAX_MACHINES];
   uint trWarnings[MAX_MACHINES];
   uint *trPtr[MAX_MACHINES];

   /* L1 State machines (cap/compulsory) */
   uint *l1DtrPtr[MAX_MACHINES];
   uint *l1ItrPtr[MAX_MACHINES];

   /*
    * PID mappings. 
    * Memstat pid table. Hive uses pids sparsly. We therefore 
    * use an indirection table to index the pids. This saves us
    * a couple of bits (exactly 4) under the safe assumption that
    * no more than 4K processes will be spawned during the run.
    */
   int pidCount[MAX_MACHINES];
   int pidTable[MAX_MACHINES][32*1024];
} memStat;

struct MemStatBucket {
   /* Instruction & Data */
   StatRecordFieldDesc dStall;
   StatRecordFieldDesc dL2Stall;
   StatRecordFieldDesc dUpgradeStall;
   StatRecordFieldDesc dL2DirtyStall;

   StatRecordFieldDesc dL1Miss;
   StatRecordFieldDesc dL2Miss;
   StatRecordFieldDesc dL2DirtyMiss;
   StatRecordFieldDesc dL1CapMiss;
   StatRecordFieldDesc dUpgrade;                 
   StatRecordFieldDesc dUpgradeNoInval;
   StatRecordFieldDesc dUpgradeSC;
   StatRecordFieldDesc dUpgradeRemote;
   
   StatRecordFieldDesc read[NUM_CATEGS];
   StatRecordFieldDesc write[NUM_CATEGS];   
   
   /* Instruction only */
   StatRecordFieldDesc instr[NUM_CATEGS];
   StatRecordFieldDesc iL1Miss;
   StatRecordFieldDesc iL2Miss;
   StatRecordFieldDesc iStall;
   StatRecordFieldDesc iL2Stall;

   StatRecordFieldDesc iL2DirtyMiss;
   StatRecordFieldDesc iL1CapMiss;

   StatRecordFieldDesc dL2RemoteMiss;
   StatRecordFieldDesc dL2RemoteStall;

   StatRecordFieldDesc iL2RemoteMiss;
   StatRecordFieldDesc iL2RemoteStall;
} memStatBucket;


/***********************************************************************
 * Statics and #defines
 **********************************************************************/
#define STAT_RECORD(_f,_inc) \
               StatRecordEntry(cpuNum,pc,vAddr,memStatBucket._f,_inc);
#define STAT_RECORD_I(_f,_inc) \
               StatRecordEntry(cpuNum,pc,0,memStatBucket._f,_inc);
#define STAT_RECORD_D(_f,_inc) \
               StatRecordEntry(cpuNum,0,vAddr,memStatBucket._f,_inc);

static int  CacheTrMiss(int cpu, PA pAddr);

int interest(PA addr);
#define ITRACE(s)  CPUWarning("%10lld (%s,%x,%d)\n", \
      (uint64)CPUVec.CycleCount(cpuNum),s,pAddr & (SCACHE_LINE_SIZE-1), cpuNum);

/*****************************************************************
 * MemStatInitCategoryFields
 *
 * Go through the cache miss categories and define fields for them.
 * This can be used by anyone needing the cache fields.
 *****************************************************************/
void 
MemStatInitCategoryFields(StatRecordFieldDesc *desc, char *tag, int type)
{
   int i;
   char str[128];
   ASSERT (memStat.init);
   for (i=0; i<NUM_CATEGS; i++) {
      int f = 1;
      switch (i) { 
      case CATEG_cap: 
         if( memStat.trackPID ) f = 0;
         break;
      case CATEG_Kcap: 
      case CATEG_OtherCap: 
      case CATEG_SelfCap: 
          if( !memStat.trackPID ) f=0;
          break;
      }
      if (f) {
         sprintf(str,"%s.%s", tag, categName[i]);
         desc[i] = StatRecordDefineField(str, type);
      }
   }
}

/****************************************************************************
 * MemStatInit
 ****************************************************************************/
#define DEF_FIELD(_field,_fl) (memStatBucket._field = StatRecordDefineField(# _field,_fl))

extern int isSolo;

void 
MemStatEarlyInit(void)
{
   int machine, i;
   for(machine=0; machine<NUM_MACHINES; machine++) {
       for(i=0;i<32*1024;i++) {
           memStat.pidTable[machine][i] = -1;
       }
   }

   /* Data-cache related counters */
   DEF_FIELD(dL1Miss,     STATRECORD_INSTRUCTION|STATRECORD_DATA);               
   DEF_FIELD(dL2Miss,     STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dL2DirtyMiss, STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dUpgrade,        STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dUpgradeNoInval, STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dUpgradeSC,      STATRECORD_INSTRUCTION|STATRECORD_DATA); 

   /* data stall time */
   DEF_FIELD(dStall,      STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dL2Stall,    STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dUpgradeStall,   STATRECORD_INSTRUCTION|STATRECORD_DATA);
   DEF_FIELD(dL2DirtyStall, STATRECORD_INSTRUCTION|STATRECORD_DATA);

   /* Instruction-cache related counters */
   DEF_FIELD(iL1Miss,     STATRECORD_INSTRUCTION); 
   DEF_FIELD(iL2Miss,     STATRECORD_INSTRUCTION );
   DEF_FIELD(iStall,      STATRECORD_INSTRUCTION );
   DEF_FIELD(iL2Stall,    STATRECORD_INSTRUCTION );
   DEF_FIELD(iL2DirtyMiss, STATRECORD_INSTRUCTION);

   /* Local/Remote memory home related counters */
   DEF_FIELD(iL2RemoteMiss,     STATRECORD_INSTRUCTION); 
   DEF_FIELD(iL2RemoteStall,     STATRECORD_INSTRUCTION); 
   DEF_FIELD(dL2RemoteMiss,     STATRECORD_INSTRUCTION|STATRECORD_DATA); 
   DEF_FIELD(dL2RemoteStall,     STATRECORD_INSTRUCTION|STATRECORD_DATA); 

   switch (memstatOption) {
   case MemStatNone:
      memStat.init = 0;
      return;
   case MemStatGlobal: 
      memStat.init = 1;
      /*  memStat.trackL1 = 1; */
      memStat.trackPID = 0;
      memStat.bitsPerState = 4;
      memStat.cacheMask = 0xf;     
      break;
   case MemStatGlobalPID:
      memStat.init = 1;
      /*   memStat.trackL1 = 1; */
      memStat.trackPID = 1;
      memStat.bitsPerState = 4+12;
      memStat.cacheMask = 0xffff;
      break;
   default: 
      ASSERT(0);
   }
   memStat.init = 1;

   /*
    * Additional counters, enabled only when the state machine is running
    */
   if (memStat.trackL1) {
      DEF_FIELD(dL1CapMiss,      STATRECORD_INSTRUCTION|STATRECORD_DATA);
   }
   MemStatInitCategoryFields(memStatBucket.read, "read",
                             STATRECORD_INSTRUCTION|STATRECORD_DATA);
   MemStatInitCategoryFields(memStatBucket.write, "write",
                             STATRECORD_INSTRUCTION|STATRECORD_DATA);
   if (memStat.trackL1) { 
      DEF_FIELD(iL1CapMiss,  STATRECORD_INSTRUCTION );
   }
   MemStatInitCategoryFields(memStatBucket.instr, "instr", 
                             STATRECORD_INSTRUCTION);   
}

void 
MemStatLateInit(void)
{
    int machine;

    if( !memStat.init) return;
    for (machine = 0; machine < NUM_MACHINES; machine++) {
        for (memStat.trNumCPUs[machine]=1;
             memStat.trNumCPUs[machine] < NUM_CPUS(machine); 
             memStat.trNumCPUs[machine] *=2) {
            continue;
        }
#if 0
        memStat.lastAddr[machine] = memStat.bitsPerState *
            memStat.trNumCPUs[machine] * (MEM_LINE(MEM_SIZE(machine)) / 8);
#else
        memStat.lastAddr[machine] = memStat.bitsPerState *
            memStat.trNumCPUs[machine] * (STAT_MEM_SIZE(machine) / 8);
#endif
        ASSERT (memStat.lastAddr[machine]);
        CPUPrint("MemStat: init cache state transitions for mach %d, %i proc,len=0x%08x\n",
                 machine, memStat.trNumCPUs[machine],
                 memStat.lastAddr[machine]);
        memStat.trPtr[machine] = (unsigned int *) 
            ZALLOC_PERM(memStat.lastAddr[machine],"MemStat");
        if (memStat.trackL1) {
            ASSERT (log2DCACHE_LINE_SIZE);
            ASSERT (log2ICACHE_LINE_SIZE);
            ASSERT (log2ICACHE_SIZE);
            ASSERT (log2DCACHE_SIZE);
            ASSERT (NUM_CPUS(machine));
            memStat.l1DtrPtr[machine] = (unsigned int *) 
                ZALLOC_PERM(NUM_CPUS(machine) *
                            MEM_L1D_LINE(STAT_MEM_SIZE(machine)) / 8,
                            "MemStat::L1D"); 
            memStat.l1ItrPtr[machine] = (unsigned int *) 
                ZALLOC_PERM(NUM_CPUS(machine) *
                            MEM_L1I_LINE(STAT_MEM_SIZE(machine)) / 8,
                            "MemStat::L1I"); 
        }
    }
}

void
MemStatImiss(MemStatTime cycle, int cpuNum, VA pc, PA pAddr,
             MemStatTime stallTime, int missType) 
{ 
   int categ = -1;
   int machine;
   int mcpu;

   if (!memStat.init) return;

   machine = M_FROM_CPU(cpuNum);
   mcpu = MCPU_FROM_CPU(cpuNum);
   ASSERT(!(missType & E_D));

#ifdef DEBUGGING
   if( interest(pAddr) ) {
      CPUWarning("%10lld IMISS cpu=%i pAddr=%08x type=%i\n",
                 CPUVec.CycleCount(cpuNum), cpuNum, pAddr,missType);
   }
#endif

   ASSERT( (missType & E_L1) || (missType & E_L2));
   if (memStat.init) {
      if (!(missType & E_L1)) {
         categ = CacheTrMiss(cpuNum,pAddr);
         ASSERT(categ >= 0);
      }
   } else {
      categ = -1;
   }
     
   /*
    * L1 cache miss
    */
   if (missType & E_L1) {
      int l1Line  = mcpu * MEM_L1I_LINE(STAT_MEM_SIZE(machine)) +
          MEM_L1I_LINE(pAddr); 
      int l1Index = l1Line /32;
      int l1Pos   = l1Line %32; 
      
      STAT_RECORD_I(iL1Miss,1);
      STAT_RECORD_I(iStall,stallTime);

      /* L1 capacity */
      if (memStat.trackL1) {
         if (memStat.l1ItrPtr[machine][l1Index] & (1<<l1Pos)) { 
            STAT_RECORD_I(iL1CapMiss,1);
         }
         memStat.l1ItrPtr[machine][l1Index] |= (1<<l1Pos);
      }
      return;
   }
   /*
    * L2 cache miss
    */
   if (missType & E_L2) {
      int i;
      STAT_RECORD_I(iL2Miss,1);
      STAT_RECORD_I(iL2Stall, stallTime);
      if (missType & E_FOUND_IN_CACHE) {
         STAT_RECORD_I(iL2DirtyMiss,1);
      }
      if (missType & E_REMOTE) {
         STAT_RECORD_I(iL2RemoteMiss,1);
         STAT_RECORD_I(iL2RemoteStall,stallTime);
      }

      if (memStat.trackL1) {
         for(i=0; i < SCACHE_LINE_SIZE; i += ICACHE_LINE_SIZE) {
            /* clear the capacity bits in the L1 cache */
            uint L1pA   = (pAddr & ~(SCACHE_LINE_SIZE-1)) + i;
            int l1Line  = mcpu * MEM_L1I_LINE(STAT_MEM_SIZE(machine)) +
                MEM_L1I_LINE(L1pA); 
            int l1Index = l1Line /32;
            int l1Pos   = l1Line %32; 
            memStat.l1ItrPtr[machine][l1Index] &= ~(1<<l1Pos);
         }
      }
      if (memStat.init) {
         ASSERT(categ >= 0 && categ < NUM_CATEGS);
         StatRecordEntry(cpuNum,pc,0,memStatBucket.instr[categ],1);
      }
   }
}


int
MemStatDmiss(MemStatTime cycle, int cpuNum, VA pc, VA vAddr, 
             PA pAddr, MemStatTime stallTime, int missType, int way)
{
   int categ=-1,i;
   int machine;
   int mcpu;

   if (!memStat.init) return 0;

   machine = M_FROM_CPU(cpuNum);
   mcpu = MCPU_FROM_CPU(cpuNum);

#ifdef DEBUGGING
   if (interest(pAddr)) { 
      CPUWarning("%10lld DMISS cpu=%i pAddr=%08x type=%#x state=%#x upgrade=%i\n",
                 CPUVec.CycleCount(cpuNum), cpuNum, pAddr, missType, 
                 MSCacheState(cpuNum, pAddr),
                 ((missType&E_UPGRADE)!=0));
   }
#endif
   
   if ((missType & E_L1) || ((missType & E_L2) && (missType & E_UPGRADE))) {
      /* no cache transition */
   } else {
      /* transition */
      ASSERT(missType & E_L2);
      if (memStat.init) {
         categ = CacheTrMiss(cpuNum,pAddr);
         if (missType & E_D) { 
            FalseSharingDefineOffset(cpuNum,pAddr,way,categ);
         }
      } else {
         categ = -1;
      }
   }
   ASSERT((missType & (E_L2 | E_L1)));

   if (missType & E_L1) {
      int l1Line  = mcpu * MEM_L1D_LINE(STAT_MEM_SIZE(machine)) +
          MEM_L1D_LINE(pAddr); 
      int l1Index = l1Line /32;
      int l1Pos   = l1Line %32; 
      STAT_RECORD(dL1Miss,1);
      STAT_RECORD(dStall,stallTime);
            
      if (memStat.trackL1) {
         /* L1 capacity */
         if (memStat.l1DtrPtr[machine][l1Index] & (1<<l1Pos)) { 
            STAT_RECORD(dL1CapMiss,1);
         }
         memStat.l1DtrPtr[machine][l1Index] |=  (1<<l1Pos);
#ifdef DEBUGGING
         if (interest(pAddr)) { 
             CPUWarning("%10lld L1 Ref cpu=%i pAddr=%08x type=%#x state=%#x\n",
                       CPUVec.CycleCount(cpuNum), cpuNum, pAddr, missType, 
                       MSCacheState(cpuNum, pAddr));
         }
#endif
      }
      return -1;
   } 

   if ((missType & E_L2) && (missType & E_UPGRADE)) {
      if (memStat.init && 
          NO_RACE_CONDITION && 
          MSCacheState(cpuNum,pAddr) != CACHE_INCACHE) {
         CPUError("MemStat: %10lld  cpu=%i pAddr=0x%08x UPGRADE NOT IN CACHE \n",
                  CPUVec.CycleCount(cpuNum), cpuNum, pAddr);
      }
      STAT_RECORD(dUpgrade,1);
      STAT_RECORD(dUpgradeStall, stallTime);
      if (!(missType & E_CAUSED_INVAL)) {
         STAT_RECORD(dUpgradeNoInval,1);
      }
      if (missType & E_SC_UPGRADE) { 
         STAT_RECORD(dUpgradeSC, 1);
      }
      if (missType & E_REMOTE) {
         STAT_RECORD_I(dL2RemoteMiss,1);
         STAT_RECORD_I(dL2RemoteStall,stallTime);
      }

#ifdef DEBUGGING
      if (interest(pAddr)) { 
         CPUWarning("%10lld L2-upgrade cpu=%i pAddr=%08x type=%#x state=%#x\n",
                    CPUVec.CycleCount(cpuNum), cpuNum, pAddr, missType, 
                    MSCacheState(cpuNum, pAddr));
      }
#endif
      return -1;
   } 
    
   /* L2 cache Miss */
   ASSERT(missType & E_L2);
   STAT_RECORD(dL2Miss, 1);
   STAT_RECORD(dL2Stall, stallTime);
   if (missType & E_FOUND_IN_CACHE) {
      STAT_RECORD(dL2DirtyMiss,1);
      STAT_RECORD(dL2DirtyStall,stallTime);
      
   }
   if (missType & E_REMOTE) {
      STAT_RECORD_I(dL2RemoteMiss,1);
      STAT_RECORD_I(dL2RemoteStall,stallTime);
   }

   if (memStat.trackL1) {
      /*
       * clear the capacity bits in the L1 cache 
       */
      for(i=0; i<SCACHE_LINE_SIZE; i+= DCACHE_LINE_SIZE) {
         uint L1pA = (pAddr & ~(SCACHE_LINE_SIZE-1)) + i;
         int l1Line = mcpu * MEM_L1D_LINE(STAT_MEM_SIZE(machine)) +
             MEM_L1D_LINE(L1pA); 
         int l1Index = l1Line /32;
         int l1Pos   = l1Line %32; 
         memStat.l1DtrPtr[machine][l1Index] &=  ~(1<<l1Pos);
      }
   }
   if (!memStat.init) return -1;
   ASSERT(categ >= 0);

   if (interest(pAddr)) {
      CPUWarning("%10lld CacheTrmiss : %i\n",CPUVec.CycleCount(cpuNum),categ);
   }
   if (missType & E_WRITE) {
      StatRecordEntry(cpuNum, pc, vAddr, memStatBucket.write[categ], 1);
   } else  { 
      StatRecordEntry(cpuNum, pc, vAddr, memStatBucket.read[categ], 1);
   }
   return missType;
}

void  
MemStatCacheTrans(int cpuNum, PA pAddr, int type, VA evictorAddr, int isUserMode)
{
   unsigned int index,pos,state,off;
   int machine;
   int mcpu;

   if (!memStat.init) return;

   machine = M_FROM_CPU(cpuNum);
   mcpu = MCPU_FROM_CPU(cpuNum);

   /* debug */
   if (interest(pAddr)) {
      char c[5];
      c[0] = 't';
      if ((type & E_L1) && (type & E_I)) { 
         c[1] = 'i';
      }
      else if ((type & E_L1) && (type & E_D)) { 
         c[1] = 'd';
      }
      else if (type & E_L2) {
         c[1] = '2';
      }
      else 
         ASSERT(0);
      
      if ((!(type & E_EXTERNAL)) && (!(type & E_FAKE_EXTERNAL))) {
         /* Line was replaced */
         c[2] = 'r';
      }
      else if (type & E_EXTERNAL) { 
         c[2] = 'X';
      }
      else if (type & E_FAKE_EXTERNAL) { 
         c[2] = 'F';
      }
      else 
         ASSERT(0);
      
      if (type & E_WRITEBACK) { 
         c[3] = 'W';
      }
      else if (type & E_FLUSH_CLEAN) { 
         c[3] = 'C';
      }
      else if (type & E_DOWNGRADE) { 
         c[3] = 'D';
      }
      else 
         c[3]='X';
      c[4] = '\0';
      ITRACE(c); 
   } 
  
   /* for now we only measure transitions in the L2 cache  */
   
   if (type & E_L1) return;
   ASSERT (type & E_L2);
   
   pAddr &= ~(SCACHE_LINE_SIZE-1);
   ASSERT(pAddr < STAT_MEM_SIZE(machine));
   
   pos = memStat.bitsPerState *( memStat.trNumCPUs[machine] *
                                 MEM_LINE(pAddr) + mcpu);
   off = pos % 32;
   index = pos / 32;
   ASSERT(index*4<memStat.lastAddr[machine]); /* range check */

   /* get current state */
   state = ( memStat.trPtr[machine][index] >> off ) & memStat.cacheMask;

   /*
    * REPLACEMENT:
    * The line was in the cache and should be in the cache and is replaced. 
    */
   if ((!(type & E_EXTERNAL)) && (!(type & E_FAKE_EXTERNAL))) {  
      /* evict your own line */
      if (state != CACHE_INCACHE) {
         /* you better be in the cache! */
         if (NO_RACE_CONDITION) {
            CPUError("MS 0x%08x %10lld MemStat: EVICT, cpu=%i state=%i type=%i\n",
                     pAddr,CPUVec.CycleCount(cpuNum),cpuNum,state,type);
         }
         ++(memStat.trWarnings[machine]);
      }

      if (isUserMode) {
         type |= E_USER_REPLACED;
      } else {
         type |= E_KERN_REPLACED;
      }

      ASSERT(type & (E_KERN_REPLACED|E_USER_REPLACED));
      if (type & E_USER_REPLACED) {
         uint newState;
         if (memStat.pidTable[machine][MS_pid(cpuNum)] == -1) { 
            memStat.pidTable[machine][MS_pid(cpuNum)]
                = memStat.pidCount[machine]++;
            ASSERT(memStat.pidCount[machine] < MS_MAXPID);
         } 
         if( memStat.trackPID ) { 
            newState = (memStat.pidTable[machine]
                        [MS_pid(cpuNum)]<<PIDOFFSET)|CACHE_REPLACED_U;
         } else { 
            newState = CACHE_REPLACED_U;
         }
         if (interest(pAddr)) {
            LogEntry("newstate",cpuNum,"pAddr=%08x newstate=%i\n",
                      pAddr,newState);
         }
         memStat.trPtr[machine][index] &= ~(memStat.cacheMask<<off);
         memStat.trPtr[machine][index] |=(newState<<off); 
      } else {
         if (interest(pAddr)) {
             LogEntry("newstate",cpuNum,"pAddr=%08x newstate=%i\n",
                      pAddr,CACHE_REPLACED_K);
         }
         memStat.trPtr[machine][index] &= ~(memStat.cacheMask<<off);
         memStat.trPtr[machine][index] |= CACHE_REPLACED_K<<off;
      }
      return ;
   }

   if (type & E_DOWNGRADE) {
      return;    /* no transition on a writeback */
   }

   if (type & E_EXTERNAL) {
      ASSERT(type & (E_WRITEBACK | E_FLUSH_CLEAN));
      /* you better be in the cache! */
      if (state != CACHE_INCACHE) {
         if (NO_RACE_CONDITION) {
            CPUError("MemStat 0x%08x %10lld EXTERNAL cpuNum=%i state=%i type=%i\n",
                     pAddr,(uint64)CPUVec.CycleCount(cpuNum),cpuNum,state,type);
         }
         ++(memStat.trWarnings[machine]);
      }
      state = CACHE_INVALIDATED;
   }
   if (type & E_FAKE_EXTERNAL) {
      switch(state) {
      case CACHE_REPLACED_K:
         state = CACHE_REPL_INV;
         break;
      case CACHE_INCACHE:
         if (NO_RACE_CONDITION) {
            CPUError("MemStat %08x %10lld FAKE_EXTERNAL, cpuNum=%i\n",
                     pAddr,CPUVec.CycleCount(cpuNum), cpuNum);
         }
         ++(memStat.trWarnings[machine]);
         break;
      case CACHE_COLD:
         break;
      default:
         if ((state&0xf)==CACHE_REPLACED_U)
            state=CACHE_REPL_INV;
           
      }
   }
   if (interest(pAddr)) {
      LogEntry("newstate",cpuNum,"pAddr=%08x newstate=%i\n",
               pAddr,state);
   }

   /* record new state */
   memStat.trPtr[machine][index] &= ~(memStat.cacheMask<<off);
   memStat.trPtr[machine][index] |= (state <<off);
}



void MemStatPrefetchNoMiss (int cpuNum, PA pAddr, int way) {
   if (interest(pAddr)) {
      LogEntry("prefetch",cpuNum,"pAddr=0x%x, way=%i \n",
               pAddr,way);
   }
   if (memStat.init) {
      int categ = CacheTrMiss(cpuNum,pAddr);
      FalseSharingDefineOffset(cpuNum,pAddr,way,categ);
   }
} 

/*****************************************************************
 * CacheTrMiss
 * 
 * This is called when a line goes back into the cache
 * to maintain the correct state machine.
 *****************************************************************/

static int
CacheTrMiss(int cpuNum, PA pAddr)
{
   int state,i;
   unsigned int pos,index,off;
   int retval = -1;
   int machine;
   int mcpu;

   if (!memStat.init) return 0;

   machine = M_FROM_CPU(cpuNum);
   mcpu = MCPU_FROM_CPU(cpuNum);

#ifdef DEBUGGING
   if (interest(pAddr)) { 
      CPUWarning("%10lld trMiss\n",CPUVec.CycleCount(cpuNum));
   }
#endif

   pAddr &=~(SCACHE_LINE_SIZE-1);
   ASSERT( memStat.trPtr[machine] );
   if (pAddr >= MEM_SIZE(machine)) {
       /* AJP: cache info doesn't match BB anyhow, so comment this out */
      //CPUWarning("MemStat::CacheTrMiss. pAddr=0x%llx > MEM_SIZE=0x%llx\n",
      //         (uint64)pAddr, (uint64)MEM_SIZE(machine));
   }
   pos = memStat.bitsPerState * (memStat.trNumCPUs[machine] *
                                 MEM_LINE(pAddr) + mcpu);
   off = pos% 32;
   index = pos / 32;
   ASSERT( index*4< memStat.lastAddr[machine] );
   /* get old state */
   state = (memStat.trPtr[machine][index] >> off ) & memStat.cacheMask;
   switch( state ) {
   case CACHE_COLD:  
      retval= CATEG_ice;
      break;
   case CACHE_REPLACED_K:
      if (memStat.trackPID) {
         retval = CATEG_Kcap;
      } else { 
         retval = CATEG_cap;
      }
      break;
   case CACHE_INVALIDATED:
      retval = CATEG_inv;
      break;
   case CACHE_REPL_INV:
      retval = CATEG_capinv;
      break;
   case CACHE_WARMED: 
      retval = CATEG_cold;
      break;
   case CACHE_INCACHE:
      if( NO_RACE_CONDITION ) {
         CPUError("%10lld MemStat CacheTrMiss, state=%i pAddr=%08x\n",
                  CPUVec.CycleCount(cpuNum), state, pAddr);
      }
      ++(memStat.trWarnings[machine]);
      break;
   default: 
      ASSERT( (state&0xf)== CACHE_REPLACED_U );
      if( memStat.trackPID) {
         if (memStat.pidTable[machine][MS_pid(cpuNum)] == -1) { 
            memStat.pidTable[machine][MS_pid(cpuNum)]
                = memStat.pidCount[machine]++;
            ASSERT(memStat.pidCount[machine] < MS_MAXPID);
         }   
         if (state>>PIDOFFSET == memStat.pidTable[machine][MS_pid(cpuNum)])
            retval = CATEG_SelfCap;
         else
            retval = CATEG_OtherCap;  
      } else {
         retval = CATEG_cap;
      }
   }
   memStat.trPtr[machine][index] &= ~(memStat.cacheMask <<off);
   memStat.trPtr[machine][index] |= CACHE_INCACHE<<off; /* it is back in the cache */
   
   if (interest(pAddr)) {
      LogEntry("newstate",cpuNum,"pAddr=%08x newstate=IN_CACHE\n",
               pAddr);
   }
   if (state == CACHE_COLD) {
      pos = memStat.bitsPerState * (memStat.trNumCPUs[machine] *
                                    MEM_LINE(pAddr) );
      off = pos % 32;
      index = pos / 32;
      for(i=0;i<memStat.trNumCPUs[machine];++i) {
         if( i != mcpu ) {
            memStat.trPtr[machine][index] &= ~(memStat.cacheMask <<off);
            memStat.trPtr[machine][index] |= (CACHE_WARMED<<off);
         }
         off += memStat.bitsPerState;
         if( off >= 32 ) {
            off -=32;
            index ++;
         }  
      }
   }
   if (interest(pAddr)) {
      ITRACE("Cache");
   }
   return retval;
}


int  
MemStatInitialized(void)
{
   return( memStat.init );
}

/*****************************************************************************
 * Support routines for MemStat
 ****************************************************************************/
void 
MemStatLog(FILE *f,char *tag, SimTime time, char *desc, char *text,...)
{
   va_list ap;
   if (f) { 
      if (time) {
         fprintf(f,"%-6s ",tag);
         va_start(ap,text);
         vfprintf(f,text,ap);
         va_end(ap);
         fprintf(f,"\n");
      } else {
         fprintf(f,"DEF %-6s %s\n",tag,desc);
      }
   } 
}


/*****************************************************************
 * Very useful debugging procedures
 *****************************************************************/
int
interest( PA pAddr ) {
#ifdef DEBUGGING
#define INTERESTING_LINE  0x67460d80
#define SET_PROBLEM 1

   if( (pAddr  & ~(SCACHE_LINE_SIZE-1))== 
       ( INTERESTING_LINE  &~(SCACHE_LINE_SIZE-1))  ) {
      CPU_nop();
      return 1;
   } 
   if( SET_PROBLEM ) { 
      if( ((pAddr >> log2SCACHE_LINE_SIZE) & (SCACHE_INDEX-1))
          == ((INTERESTING_LINE>>log2SCACHE_LINE_SIZE) & (SCACHE_INDEX-1)) ) {
         CPU_nop();
         return 1;
      }
   }
#endif
   return 0;
}

 int
MSCacheState(int cpu, PA pAddr) 
{
   int pos, off, index;
   int machine;
   int mcpu;

   if (!memStat.init) return -1;

   machine = M_FROM_CPU(cpu);
   mcpu = MCPU_FROM_CPU(cpu);

   pos = memStat.bitsPerState *( memStat.trNumCPUs[machine] *
                                 MEM_LINE(pAddr) + mcpu);
   off = pos % 32;
   index = pos / 32;
   ASSERT(index*4<memStat.lastAddr[machine]); /* range check */

   /* get current state */
   return ( memStat.trPtr[machine][index] >> off ) & memStat.cacheMask;
}