/**************************************************
 * Utility functions                              *
 **************************************************/
static bool createThreads() {
    for (int i = 0; i < ROLE_SIZE; ++i) {
        threads[i] = 0;
        if (!CreateNewThread(&threads[i], (void*)DoNewThread, &roles[i])) {
            return false;
        }
    }
    return true;
}
static bool createThreads(int numOfThreads, void* func) {
    for (int i = 0; i < numOfThreads; ++i) {
        threads[i] = 0;
        if (!CreateNewThread(&threads[i], func, NULL)) {
            return false;
        }
    }
    return true;
}
MemoryMonitor::MemoryMonitor(Lng32 windowSize,
			     Lng32 sampleInterval,
			     CollHeap *heap)
  : physKBytes_(0),
    availBytesPercent_(1.0),
    commitBytesPercent_(0),
    commitPhysRatio_(0),

    pagingCounterAdded_(FALSE),
    pageFaultRate_(0),
    prevPageFault_(0),
    prevTime_(0),
    entryCount_(1),
    resetEntryCount_(TRUE),
    resetOnce_(FALSE),

    enable_(TRUE),
    pressure_(0),

    sampleInterval_(sampleInterval * 1000)
{
  // if the windowSize is 0, we do not need memory monitor.
  assert(windowSize);
  char buffer[1024];
  char *currPtr;
  size_t bytesRead;
  fd_meminfo_ = fopen("/proc/meminfo", "r");
  bytesRead = fread(buffer, 1, 1024, fd_meminfo_);
  currPtr = strstr(buffer, "MemTotal");
  sscanf(currPtr, "%*s " PF64 " kB", &memTotal_);
  physKBytes_ = memTotal_;
  physKBytesRatio_ = physKBytes_ / (8 * 1024 * 1024);
  fd_vmstat_ = fopen("/proc/vmstat", "r");

	ULng32 pageSize = 0;  

  if (!threadIsCreated_)
    {
      // and finally start the update thread
      updateThread_ = CreateNewThread(
                           &memMonitorUpdateThread, // Thread func
                           this );   // Argument for thread
      threadIsCreated_ = TRUE;
    }

};
// -----------------------------------------------------------------------------
// CSensrvTest::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CSensrvTest::ConstructL()
    {
    
    User::LeaveIfError( CreateNewThread() );
    
    
    
    iLog = CStifLogger::NewL( KSensrvTestLogPath, 
                          KSensrvTestLogFile,
                          CStifLogger::ETxt,
                          CStifLogger::EFile,
                          EFalse );
                          
                          
    // Set up status P&S value
    DefinePSKeyL( KSensrvLeaveFirstStubConstruction, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLeaveFirstStubConstruction, 0 );
    DefinePSKeyL( KSensrvLeaveSecondStubConstruction, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLeaveSecondStubConstruction, 0 );
    DefinePSKeyL( KSensrvFirstStubChannelCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvFirstStubChannelCount, 3 );
    DefinePSKeyL( KSensrvSecondStubChannelCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSecondStubChannelCount, 3 );
    DefinePSKeyL( KSensrvLatestOpenedChannel, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLatestOpenedChannel, 0 );
    DefinePSKeyL( KSensrvLatestClosedChannel, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLatestClosedChannel, 0 );
    DefinePSKeyL( KSensrvLatestStartListeningChannel, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLatestStartListeningChannel, 0 );
    DefinePSKeyL( KSensrvLatestStopListeningChannel, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvLatestStopListeningChannel, 0 );
    DefinePSKeyL( KSensrvSamplingInterval, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSamplingInterval, 5 ); // 5 millisecond
    DefinePSKeyL( KSensrvSignalAftedDataReseived, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSignalAftedDataReseived, 0 );
    DefinePSKeyL( KSensrvStubConstructionJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStubConstructionJam, 0 );
    DefinePSKeyL( KSensrvStartListeningJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStartListeningJam, 0 );
    DefinePSKeyL( KSensrvStopListeningJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStopListeningJam, 0 );    
    DefinePSKeyL( KSensrvOpenChannelAsyncJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvOpenChannelAsyncJam, 0 );
    DefinePSKeyL( KSensrvCloseChannelAsyncJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCloseChannelAsyncJam, 0 );
    DefinePSKeyL( KSensrvChannelOpenedError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvChannelOpenedError, 0 );
    DefinePSKeyL( KSensrvChannelClosedError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvChannelClosedError, 0 );
    DefinePSKeyL( KSensrvStubConstructionDelay, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStubConstructionDelay, 0 ); // not delay
    DefinePSKeyL( KSensrvWaitTestCompletion, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvWaitTestCompletion, 1 ); // Wait test completion
    DefinePSKeyL( KSensrvStartListeningLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStartListeningLeave, 0 );
    DefinePSKeyL( KSensrvStopListeningLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStopListeningLeave, 0 );
    DefinePSKeyL( KSensrvDataListeningJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvDataListeningJam, 0 );
    DefinePSKeyL( KSensrvDataListeningBufferFilledError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvDataListeningBufferFilledError, 0 );
    DefinePSKeyL( KSensrvForceBufferFilledJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvForceBufferFilledJam, 0 );
    DefinePSKeyL( KSensrvForceBufferFilledLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvForceBufferFilledLeave, 0 );
    DefinePSKeyL( KSensrvOpenChannelMethodJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvOpenChannelMethodJam, 0 );
    DefinePSKeyL( KSensrvCloseChannelMethodJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCloseChannelMethodJam, 0 );
    DefinePSKeyL( KSensrvOpenChannelError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvOpenChannelError, 0 );
    DefinePSKeyL( KSensrvCloseChannelError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCloseChannelError, 0 );
    DefinePSKeyL( KSensrvChannelOpenedWithError, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvChannelOpenedWithError, 0 );
    DefinePSKeyL( KSensrvPropertyChanged, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvPropertyChanged, 0 );//0 means that property changed notifications are not sent
    DefinePSKeyL( KSensrvOpenChannelMethodPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvOpenChannelMethodPanic, 0 );
    DefinePSKeyL( KSensrvOpenChannelAsyncPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvOpenChannelAsyncPanic, 0 );
    DefinePSKeyL( KSensrvCloseChannelMethodPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCloseChannelMethodPanic, 0 );
    DefinePSKeyL( KSensrvCloseChannelAsyncPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCloseChannelAsyncPanic, 0 );
    DefinePSKeyL( KSensrvStartListeningMethodPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStartListeningMethodPanic, 0 );
    DefinePSKeyL( KSensrvDataListeningPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvDataListeningPanic, 0 );
    DefinePSKeyL( KSensrvStopListeningMethodPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStopListeningMethodPanic, 0 );
    DefinePSKeyL( KSensrvStopListeningAsyncPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvStopListeningAsyncPanic, 0 );
    DefinePSKeyL( KSensrvForceBufferFilledPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvForceBufferFilledPanic, 0 );
    DefinePSKeyL( KSensrvSSYUnloadingCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSSYUnloadingCount, 0 );
    DefinePSKeyL( KSensrvBufferFilledNegativeCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvBufferFilledNegativeCount, 0 );
    DefinePSKeyL( KSensrvGetAllPropertyCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetAllPropertyCount, 0 );    
    DefinePSKeyL( KSensrvSetPropertyArray, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSetPropertyArray, 0 );    
    DefinePSKeyL( KSensrvGetPropertyLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetPropertyLeave, 0 );
    DefinePSKeyL( KSensrvGetPropertyJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetPropertyJam, 0 );
    DefinePSKeyL( KSensrvSetPropertyLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSetPropertyLeave, 0 );
    DefinePSKeyL( KSensrvSetPropertyJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSetPropertyJam, 0 );
    DefinePSKeyL( KSensrvGetAllPropertiesLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetAllPropertiesLeave, 0 );
    DefinePSKeyL( KSensrvGetAllPropertiesJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetAllPropertiesJam, 0 );
    DefinePSKeyL( KSensrvGetPropertyPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetPropertyPanic, 0 );
    DefinePSKeyL( KSensrvSetPropertyPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSetPropertyPanic, 0 );
    DefinePSKeyL( KSensrvGetAllPropertiesPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetAllPropertiesPanic, 0 );
    DefinePSKeyL( KSensrvCheckPropertyDependenciesLeave, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCheckPropertyDependenciesLeave, 0 );
    DefinePSKeyL( KSensrvCheckPropertyDependenciesJam, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCheckPropertyDependenciesJam, 0 );
    DefinePSKeyL( KSensrvCheckPropertyDependenciesPanic, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCheckPropertyDependenciesPanic, 0 );
    DefinePSKeyL( KSensrvGetDataOverflowTest, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetDataOverflowTest, 0 );
    DefinePSKeyL( KSensrvGetDataNotFoundTest, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvGetDataNotFoundTest, 0 );    
    DefinePSKeyL( KSensrvCheckPluginLoaded, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvCheckPluginLoaded, 0 );    
    
    TInt err( RProperty::Define( KPSUidSensrvTest, KSensrvTestProcessCommand, RProperty::EByteArray ) );
    if ( err != KErrAlreadyExists )
        {
        User::LeaveIfError( err );
        }
    RProperty::Set( KPSUidSensrvTest, KSensrvTestProcessCommand, KNullDesC );    

    DefinePSKeyL( KSensrvSingleBufferFilledNegativeCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvSingleBufferFilledNegativeCount, 0 );

    // These keys are listened to by stubs, so do not set them to zero,
    // as it will trigger the listeners of stubs if there are multiple
    // test classes. These keys are only read when they change, so their
    // initial value is irrelevant.
    DefinePSKeyL( KSensrvCreateNewChannelsFirst, RProperty::EInt );
    DefinePSKeyL( KSensrvCreateNewChannelsSecond, RProperty::EInt );
    DefinePSKeyL( KSensrvRemoveNewChannelsFirst, RProperty::EInt );
    DefinePSKeyL( KSensrvRemoveNewChannelsSecond, RProperty::EInt );
    DefinePSKeyL( KSensrvRegisterZeroChannels, RProperty::EInt );
    DefinePSKeyL( KSensrvRegisterInvalidChannels, RProperty::EInt );
    
    DefinePSKeyL( KSensrvFirstStubDoubleTappingChannelCount, RProperty::EInt );
    RProperty::Set( KPSUidSensrvTest, KSensrvFirstStubDoubleTappingChannelCount, 0 );
    }