#ifndef SHRPCSTATS_HT
#define SHRPCSTATS_HT TRUE
/*=========================================================================
 * Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved.
 *
 * Name - shrpcstats.ht
 *
 * Purpose - Definition of all shared page cache statistics
 *
 * $Id: shrpcstats.ht,v 1.2 2008-01-09 22:51:27 stever Exp $
 *
 *========================================================================
 */

/*******  GemStone configuration files  *************************************/

/*******  GemStone constants/types defined in this and other modules ********/
#include "flag.ht"
/* no more includes. KEEP THIS LINE */

#define SHRPC_SESSION_STATS_SIZE 48

/*=========================================================================
 * Cache Statistics definitions
 *========================================================================
 */
#define SHRPC_PROC_NAME_SIZE  31

#if defined(REPOSDISK_HT)
// For internal GemStone builds,
// REP_NUM_PAGE_KINDS defined in reposdisk.ht
#define SHR_NUM_PAGE_KINDS REP_NUM_PAGE_KINDS
#else
// customer build: defined it here Gcsi interface build
#define SHR_NUM_PAGE_KINDS 21
#endif

enum {
  SHRPC_MONITOR = 0x1,
  SHRPC_STONE = 0x2,
  SHRPC_PGSVR = 0x4,
  SHRPC_GEM = 0x8,
  SHRPC_STATMON = 0x10,
  SHRPC_APP = 0x20,
  SHRPC_SYSTEM = 0x40, // not actually stored in the SPC, used only by statmon
  SHRPC_ALL = 0x7F // mask with all valid bits set
};

class ShrPcMonStatSType 
{
 public:
  unsigned int     numberOfFreeFrames;
  unsigned int gemsInCacheCount;
  unsigned int localDirtyPageCount;
  unsigned int globalDirtyPageCount;
    signed int wakeupInterval;
  unsigned int numRecentActiveProcesses;
  unsigned int gemDirtyPageKinds[(int)SHR_NUM_PAGE_KINDS];
  unsigned int stoneDirtyPageKinds[(int)SHR_NUM_PAGE_KINDS];
  unsigned int spinLockPageFrameSleepCount;
  unsigned int spinLockHashTableSleepCount;
  unsigned int spinLockFreeFrameSleepCount;
  unsigned int spinLockFreePceSleepCount;
  unsigned int spinLockSmcQSleepCount;
  unsigned int spinLockOtherSleepCount;
    signed int targetFreeFrameCount;
  unsigned int pagesRemovedFromCacheCount;
  unsigned int pagesNotRemovedFromCacheCount;
  unsigned int pagesNotFoundInCacheCount;
  unsigned int pagesRemovedDirtyFromCacheCount;
  unsigned int spinLockNewSymSleepCount;
  unsigned int pinnedTotalCount;
  unsigned int pinnedSharedCount;
  unsigned int pinnedDataPagesCount;     
  unsigned int pinnedOtPagesCount;
  unsigned int pageServersInCacheCount;
  unsigned int frameCount;
  unsigned int spinLockCount;
  unsigned int numSharedCounters;
  unsigned int slotsCrashedCount;
  // must be last!
  unsigned int slotsRecoveredCount;

  unsigned int *firstStat() { return &this->numberOfFreeFrames; }
  static size_t elementSize() { return sizeof(int); }

  };


// =====================================================
  // Offsets used to access  stg->LockWaitQueues 
  //
  // The RcRetryQueue, Lock1WaitQueue, Lock2WaitQueue  
  // allow sessions to wait for lock acquisition on a single
  // object.  Each queue has one associated persistent object, assigned
  //  by the first session to use that queue.
  // Acquiring these locks is subject to the deadlock detection code
  // in stndocall.c 
enum {
  RC_RETRY_WAIT_IDX = 0, // RcRetryQueue
  OBJ_1_WAIT_IDX = 1,	// Lock1WaitQueue
  OBJ_N_WAIT_IDX = 10,	// LockNWaitQueue
  LOCK_REQ_IDX = 11, // must be last, LockReqQueue - general lock requests 
                   //   sessions waiting for locks only because another session
		   //   holds commit token.
			
  NUM_LOCK_WAIT_QUEUES = LOCK_REQ_IDX + 1 ,
  NUM_OBJ_WAIT_QUEUES = LOCK_REQ_IDX - OBJ_1_WAIT_IDX  // not incl RC_RETRY
};

// =====================================================

class ShrPcStnStatSType 
{
 public:
  unsigned int     totalCommits;

  unsigned int *firstStat() { return &this->totalCommits; }
  static size_t elementSize() { return sizeof(int); }

  unsigned int     numberOfCommitRecords;
  unsigned int     commitRecordsDisposable;
  unsigned int     logRecordsWritten;
  unsigned int     logRecordIoCount;
  unsigned int     deadObjsReclaimedCount; 
  unsigned int     reclaimCount;
  unsigned int     reclaimedPagesCount;
  unsigned int     checkpointCount;
  unsigned int     pagesNeedReclaimingSize;

  unsigned int     possibleDeadKobjs ;
  unsigned int     voteNotDeadKobjs;
  unsigned int     deadNotReclaimedKobjs ;

  unsigned int     numInCommitQueue;
  unsigned int     numInNotifyQueue;
  unsigned int     numInLoginWaitQueue;
  unsigned int     numInRunQueue;
  unsigned int     numInSymbolCreateQueue;
  unsigned int     numInPageWaitQueue;
  unsigned int     numInLogWaitQueue;
  unsigned int     numInLogHighQueue;
  unsigned int     numInLogLowQueue;
  unsigned int     numInRcRetryQueue;
  unsigned int     numInObjWaitQueue[NUM_OBJ_WAIT_QUEUES];
  unsigned int     numInLockReqQueue;
  unsigned int     logLowQueueAdds;
  unsigned int     logHighQueueAdds;
  unsigned int     tempPagesDisposed;
  unsigned int     persistentPagesDisposed;
  unsigned int     pageDisposesDeferred;
  unsigned int     freePages;
  unsigned int     gcWsUnionSweepCount;
  unsigned int     timeInPgsvrNetReads;
  unsigned int     timeInPgsvrNetWrites;
  unsigned int     gsMsgCount;
  unsigned int     gsMsgSessionId;
  unsigned int     gsMsgKind;
  unsigned int     stnLoopState;
  unsigned int     deferCkptCompleteCount;
  unsigned int     totalAborts;
  unsigned int     timeInStnGetLocks;
  unsigned int     stnGetLocksCount;
  unsigned int     totalNewObjsCommitted;
  unsigned int     freeOopsKCount;
  unsigned int     totalSessionsCount;
    signed int     commitTokenSession;
  unsigned int     stnLoopCount;
  unsigned int     timeInCommitRecordDisposal;
  unsigned int     commitRecordDisposalState;
  unsigned int     commitRecordsDisposedCount;
  unsigned int     commitQueueSessionNotReadyCount;
  unsigned int     commitQueueAddedToRunQueueCount;
  unsigned int     commitQueueAddedToRunQueueSessionCount;
    signed int     checkpointState;
  unsigned int     stoneCommitState;
    signed int     commitQueueThreshold;
  unsigned int     commitRecordDisposalsDeferredCount;
  unsigned int     stnLoopHibernateCount;
  unsigned int     recoverTranlogFileId;
  unsigned int     recoverTranlogBlockId;
  unsigned int     oldestCrSession;
  unsigned int     oldestCrSessNotInTrans;
  unsigned int     gcVoteState;
  unsigned int     waitingForSessionToVote;
  unsigned int     tranlogFileId;
  unsigned int     tranlogBlockId;
  unsigned int     tranlogsFull;
  unsigned int     timePerformingCommit;
  unsigned int     remoteSharedPageCacheCount;
  unsigned int     oopsReturnedByGemsCount;
  unsigned int     pagesReturnedByGemsCount;
  unsigned int     pageMgrPagesReceivedFromStoneCount;
  unsigned int     pageMgrRemoveFromCachesCount;
  unsigned int     pageMgrRemoveFromCachesPageCount;
  unsigned int     pageMgrPagesPendingRemovalRetryCount;
  unsigned int     pageMgrPagesRemovedFromCachesCount;
  unsigned int     pageMgrPagesNotRemovedFromCachesCount;
  unsigned int     pageMgrRemovePagesFromCachesPollCount;
  unsigned int     pageMgrTimeWaitingForCachePgsvrs;
  unsigned int     oopNumberKHighWaterMark;
  unsigned int     pagesWaitingForRemovalInStoneCount;
  unsigned int     pagesNeedRemovingThreshold;
  unsigned int     timeInStonePageDisposal;
  unsigned int     remoteCachesNeedServiceCount;
  unsigned int     localCacheStalePcesRemovedCount;
  unsigned int     localCacheAllocatedPceCount;
  unsigned int     localCacheFreePceCount;
  unsigned int     localCachePceCountLimit;
  unsigned int     localCachePceReclaimCount;
  unsigned int     localCacheValidPcesRemovedCount;
  unsigned int     localCacheOverflowCount;
  unsigned int     lastSmcQueueSize;
  unsigned int     sessionWithGcLock;

  static inline uint toKObjs(uint64_t v) {
    return (v + 1023) >> 10 ;
  }

  unsigned int     numInNetWriteQueue;
  unsigned int     epochGcCount;
  unsigned int     commitsSinceLastEpoch;
  unsigned int     epochNewKobjs;
  unsigned int     numInMainInpQueue;
  unsigned int     epochScannedKobjs;
  unsigned int     epochPossibleDeadKobjs;
  unsigned int     timeLastEpochGc;
  unsigned int     epochForceGc;
  unsigned int     bmCHeapPages;
  unsigned int     maxVotingSessions;
  unsigned int     numVotingSessions;
  unsigned int     numInLostOtQueue;
  unsigned int     stnMainWaitsForFreeAio;
  unsigned int     stnAioWriteEAGAIN;
  unsigned int     aioNumEmptyBuffers;
  unsigned int     aioNumBuffers;
  unsigned int     numInPgsvrWaitQueue;
  unsigned int     reposSizeMB;
  unsigned int     numInLdiQueue;
  unsigned int     totalGemFatalErrors;
  unsigned int     totalSigAbortsSent;
  unsigned int     totalLostOtsSent;
  unsigned int     totalSessionsStopped;
  unsigned int     totalSessionsTerminated;
  unsigned int     ldiThreadOperations;
  unsigned int     loginRequestsCount;
  unsigned int     numInNetReadWorkList;
  unsigned int     lastSessionIdStopped;
  unsigned int     lastSessionIdTerminated;
  unsigned int     lastSessionFatalError;
  unsigned int     lastSessionSigAbort;
  unsigned int     lastSessionLostOt;
  unsigned int     stnLoopNoWorkThreshold;
  unsigned int     stnLoopsSinceSleep;
  unsigned int     stnLoopsNoWork;
  unsigned int     stnTranQToRunQThreshold;
  unsigned int     numCacheWarmers;
  unsigned int     globalSessionStats[SHRPC_SESSION_STATS_SIZE];
  unsigned int     stnAioWaitsForWork;
  unsigned int     stnAioWriteFailures;
  unsigned int     stnAioCompletionFailures;
  unsigned int     stnAioFsyncFailures;
  unsigned int     stnAioSuspendEAGAIN;
  unsigned int     stnAioSuspendPrematureReturn;
  unsigned int     stnAioSyncWritesAfterCancel;
  unsigned int     netWriteThreadWakeups;
  unsigned int     netWriteThreadSocketWrites;
  unsigned int     timerThreadWakeups;
  unsigned int     gcWsUnionSize;
  unsigned int     epochLastDuration;
  unsigned int     pageManagerStarvedCount;
  unsigned int     pageManagerMaxWaitTimeMs;
  unsigned int     highWaterPageExtentId;
  unsigned int     highWaterPageRecordId;
  unsigned int     timeInGetPagesForPageMgr;
  unsigned int     timeInProcessPagesFromPageMgr;

  // Do it this way since we don't have 64 bit cache stats in 2.2.6.x
  inline void setHighWaterPageId(RepPageIdType pageId) {
    highWaterPageExtentId = REP_EXTENT_FROM_PAGE_ID_NO_CHECK(pageId);
    highWaterPageRecordId = REP_RECORD_ID_FROM_PAGE_ID_NO_CHECK(pageId);
  }

  inline RepPageIdType getHighWaterPageId(void) {
    return REP_BUILD_PAGE_ID_NOCHECK(highWaterPageExtentId, highWaterPageRecordId);
  }

  // must be the last stat!
  unsigned int     localCacheFreeFrameCount;
} ;

class ShrPcPgsvrStatSType {
public:
  unsigned int     aioDirtyCount;
  unsigned int     aioCkptCount;
  unsigned int     asyncWritesInProgress;
  unsigned int     clientPageReads;
  unsigned int     clientPageWrites;
  unsigned int     dirtyPageSweepCount;
  unsigned int     aioRateLimit;
  unsigned int     aioRateMax;
  unsigned int     targetPercentDirty;
  unsigned int     nextSleepTime;
  int              asyncFlushesInProgress;
  unsigned int     clientPid; /*26936*/
  unsigned int     preemptedDataPages;
  unsigned int     preemptedObjectTablePages;
  unsigned int     preemptedBitmapPages;
  unsigned int     preemptedCommitRecordPages;
  unsigned int     preemptedOtherPages;
  unsigned int     clockHandFrameId;
  unsigned int     timePerformingReadIo;
  unsigned int     postCheckpointPages;
  unsigned int     clientSigAbortsSent;
  unsigned int     clientLostOtsSent;
  unsigned int     pgsvrCheckpointState;

  // must be the last stat!
  unsigned int     timePerformingReadRequests;
};

class ShrPcGemStatSType 
{
 public:
  unsigned int     commitCount;
  unsigned int     failedCommitCount;
  unsigned int     abortCount;
  unsigned int     objsCommitted;
  unsigned int     newObjsCommitted;
  unsigned int     objectsRead;
  unsigned int     classesRead;
  unsigned int     methodsRead;
  unsigned int     objectsRefreshed;
  unsigned int     numberOfScavenges;
  unsigned int     timeInScavenge;
  unsigned int     numberOfMarkSweeps;  
  unsigned int     timeInMarkSweep;
  unsigned int     numRefsStubbedMarkSweep;
  unsigned int     numRefsStubbedScavenge;
  unsigned int     numSoftRefsCleared;
  unsigned int     numLiveSoftRefs;
  unsigned int     numNonNilSoftRefs;
  unsigned int     codeGenGcCount;
  unsigned int     pomGenScavCount;
  unsigned int     scavengeOverflows;
  unsigned int     codeCacheSizeBytes;
  unsigned int     newGenSizeBytes;
  unsigned int     oldGenSizeBytes;
  unsigned int     oldGenPreGcSizeBytes;
  unsigned int     pomGenSizeBytes;
  unsigned int     permGenSizeBytes;
  unsigned int     meSpaceUsedBytes;
  unsigned int     meSpaceAllocatedBytes;
  unsigned int     objectMemoryGrowCount;
  unsigned int     workingSetSize;
  unsigned int     exportedSetSize; 
  unsigned int     trackedSetSize; 
  unsigned int     dirtyListSize;
  unsigned int     timeWaitingForCommit;
  unsigned int     timeProcessingCommit;
  unsigned int     timeStoneCommit;
  unsigned int     messagesToStn;
  unsigned int     _progressCount;

  inline void setProgress(uint64_t v) {
    _progressCount = v;
  }

  unsigned int     _progressKObjs;

 private:
  inline uint toKObjs(uint64_t v) {
    return (v + 1023) >> 10 ; // round up
  }
 public:

  inline void setProgressObjs(uint64_t v) {
    _progressKObjs = toKObjs(v);
    _progressCount = (uint) v;
  }

  unsigned int     newSymbolRequests;
  unsigned int     timeWaitingForSymbols;   
  unsigned int     newSymbolsCount;
  unsigned int     omBytesFlushed;
  unsigned int     gemFreePages;
  unsigned int     voteNotDead;
    signed int     transLevel;
  unsigned int     timeInPgsvrNetReads;
  unsigned int     timeInPgsvrNetWrites;
  unsigned int     shadowedPagesCount;
  unsigned int     rcConflictCount;
  unsigned int     commitRetryFailureCount;
  unsigned int     pageReadsWaitingForCommit;
  unsigned int     pageReadsProcessingCommit;
  unsigned int     pageReadsStoneCommit;
  unsigned int     messagesToStnWaitingForCommit;
  unsigned int     messagesToStnProcessingCommit;
  unsigned int     messagesToStnStoneCommit;
  unsigned int     messageKindToStone;
  unsigned int     timeWaitingForStone;
  unsigned int     updateUnionsCommitCount;
  unsigned int     timeInUpdateUnionsCommit;
  unsigned int     rebuildScavPagesForCommitCount;
  unsigned int     crReadAbort;
  unsigned int     crReadCommitWithToken;
  unsigned int     crReadCommitWithoutToken;
  unsigned int     sigAbortsSent;
  unsigned int     sigAbortsReceived;
  unsigned int     lostOtsSent;
  unsigned int     lostOtsReceived;
  unsigned int     gemHasCommitToken;
  unsigned int     localCacheStalePcesRemovedCount;
  unsigned int     localCacheAllocatedPceCount;
  unsigned int     localCacheFreePceCount;
  unsigned int     localCachePceCountLimit;
  unsigned int     localCachePceReclaimCount;
  unsigned int     localCacheValidPcesRemovedCount;
  unsigned int     localCacheOverflowCount;
  unsigned int     localCacheFreeFrameCount;
  unsigned int     tempObjSpacePercentUsed;
  unsigned int     gciRpcCommandsServiced;
  unsigned int     persistentPagesCommittedCount;
  unsigned int     sleepDuringDisposeTempPageCount;
  unsigned int     bmCHeapPages;
  unsigned int     depMapKeysChanged;
  unsigned int     gciPhysBytesSent;
  unsigned int     gciPhysBytesRcvd;
  unsigned int     gciBytesSent;
  unsigned int     gciBytesRcvd;
    signed int     sessionId;
  // must be last!!!
  unsigned int     sessionStats[SHRPC_SESSION_STATS_SIZE];
};


union ShrPcStatUnion {
  ShrPcMonStatSType monitor;
  ShrPcPgsvrStatSType pgsvr;
  ShrPcStnStatSType stone;
  ShrPcGemStatSType gem;
};

class ShrPcCommonStatSType {
public:
  unsigned int     localPageCacheHits;
  unsigned int     localPageCacheMisses;
  unsigned int     localPageCacheWrites;
  unsigned int     waitsForOtherReader;
  unsigned int     rdbfDiskReads; 
  unsigned int     rdbfDiskWrites;
  unsigned int     milliSecPerIoSample;
  unsigned int     framesFromFreeList;
  unsigned int     framesFromFindFree;
  int              freeFrameLimit;
  unsigned int     timeInFramesFromFindFree;
  unsigned int     framesAddedToFreeList;
  unsigned int     pageLocateCount;
  unsigned int     otPageReads;
  unsigned int     dataPageReads;
  unsigned int     bmPageReads;
  unsigned int     miscPageReads;
  unsigned int     extentFlushCount;
    signed int     freeFrameCacheSize;
  unsigned int     freeFrameCacheNumFrames;

  unsigned int     rdbfTimeWaitingForIoMs;
  unsigned int     rdbfMicroSecsPerIoOverallAvg;
  unsigned int     rdbfMicroSecsPerIo10Avg;
  unsigned int     rdbfMicroSecsPerIo100Avg;
  unsigned int     rdbfPageIoCount;
  unsigned int     CHeapSizeKB;
  unsigned int     pinnedPrivatePagesCount;
  // must be last!!
  unsigned int     pinnedPagesCount;
};

class ShrPcStatsSType {
 public:
  signed int              processId;
  signed int              sessionId;
                             
  ShrPcCommonStatSType cmn;
  union ShrPcStatUnion u;
  intptr_t spaceWaster;

  uint* firstStat() { return (uint*)&this->processId; }
};

class GcsiResultSType {
public:
  ShrPcStatsSType   stats; // 8 byte aligned
  char              vsdName[SHRPC_PROC_NAME_SIZE + 1]; // 32 bytes
  unsigned int      statType; // 4 bytes
  unsigned int      space_waster; // 4 bytes
};

#endif /* SHRPCSTATS_HT */

