loadgen/engine/src/loadgen_cpuload.cpp
changeset 55 2d9cac8919d3
parent 53 819e59dfc032
child 56 392f7045e621
equal deleted inserted replaced
53:819e59dfc032 55:2d9cac8919d3
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_cpuload.h"
       
    21 #include "loadgen_utils.h"
       
    22 #include "loadgen.hrh"
       
    23 #include <loadgen.rsg>
       
    24 #include <e32hal.h>
       
    25 #include <u32hal.h>
       
    26 #include <e32math.h>
       
    27 #include <e32svr.h>
       
    28 
       
    29 _LIT(KThreadName, "CPULoad %d");
       
    30     
       
    31 // ===================================== MEMBER FUNCTIONS =====================================
       
    32 
       
    33 CCPULoad* CCPULoad::NewL(TCPULoadAttributes& aAttributes, TInt aReferenceNumber)
       
    34     {
       
    35     CCPULoad* self = new(ELeave) CCPULoad(aAttributes, aReferenceNumber);
       
    36     CleanupStack::PushL(self);
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop(self);
       
    39     return self;    
       
    40     }
       
    41 
       
    42 // --------------------------------------------------------------------------------------------
       
    43 
       
    44 CCPULoad::~CCPULoad()
       
    45     {
       
    46     Close();
       
    47     }
       
    48 
       
    49 // --------------------------------------------------------------------------------------------
       
    50 
       
    51 CCPULoad::CCPULoad(TCPULoadAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes)
       
    52     {
       
    53     iAttributes.iId = aReferenceNumber;
       
    54     }
       
    55 
       
    56 // --------------------------------------------------------------------------------------------
       
    57 
       
    58 void CCPULoad::ConstructL()
       
    59     {
       
    60     CLoadBase::ConstructL();
       
    61     
       
    62     iType = ELoadGenCmdNewLoadCPULoad;
       
    63     
       
    64     TBuf<64> threadName;
       
    65     threadName.Format(KThreadName, iAttributes.iId);
       
    66     
       
    67     // create a thread
       
    68     User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes ));
       
    69     
       
    70     // set priority of the thread
       
    71     SetPriority();
       
    72     }
       
    73 
       
    74 // --------------------------------------------------------------------------------------------
       
    75 
       
    76 TInt CCPULoad::ThreadFunction(TAny* aThreadArg)
       
    77     {
       
    78     TCPULoadAttributes* threadArg = (TCPULoadAttributes*)aThreadArg;
       
    79     TInt err = KErrNone;
       
    80 
       
    81     // if a cpu is defined, tie this thread to the given cpu (SMP environment)
       
    82     if (threadArg->iCpu >= 0 && threadArg->iCpu != KCPUSelection_AllCPUs )
       
    83         {
       
    84         UserSvr::HalFunction(EHalGroupKernel, KHalFunction_EKernelHalLockThreadToCpu, (TAny*) threadArg->iCpu, 0);
       
    85         }
       
    86     
       
    87     CTrapCleanup* pC = CTrapCleanup::New();
       
    88     CActiveScheduler* pS = new CActiveScheduler;
       
    89     CActiveScheduler::Install(pS);
       
    90 
       
    91     // start generating load, pass pointer to arguments
       
    92     GenerateLoad(*((TCPULoadAttributes*) aThreadArg));
       
    93 
       
    94     delete pS;
       
    95     delete pC;
       
    96     
       
    97     return err;
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------------------------
       
   101 
       
   102 void CCPULoad::GenerateLoad(TCPULoadAttributes& aAttributes)
       
   103     {
       
   104     for (;;)
       
   105         {   
       
   106         if (aAttributes.iType == ECpuLoadTypeContinuous)
       
   107             {
       
   108             // do constantly heave stuff
       
   109             DoHeaveStuff(aAttributes.iMode);
       
   110             }
       
   111 
       
   112         else if (aAttributes.iType == ECpuLoadTypePeriodic)
       
   113             {
       
   114             // do periodically heave stuff
       
   115             TTime startTime;
       
   116             startTime.HomeTime(); // get start time
       
   117 
       
   118             TTime currentTime;
       
   119             TTimeIntervalMicroSeconds interval;
       
   120             TInt processPeriod; 
       
   121 			#ifdef QT_SUPPORT
       
   122 				
       
   123 			#else
       
   124 				processPeriod = CLoadGenUtils::MilliSecondsToMicroSeconds(aAttributes.iLength, aAttributes.iRandomVariance);
       
   125 			#endif
       
   126             do
       
   127                 {
       
   128                 // do heave stuff
       
   129                 DoHeaveStuff(aAttributes.iMode);
       
   130 
       
   131                 currentTime.HomeTime();
       
   132                 interval = currentTime.MicroSecondsFrom(startTime);                
       
   133                 }
       
   134             while (interval.Int64() < processPeriod);       
       
   135 
       
   136 
       
   137             // now wait
       
   138             User::After( CLoadGenUtils::MilliSecondsToMicroSeconds(aAttributes.iIdle, aAttributes.iRandomVariance) );
       
   139             }   
       
   140 
       
   141         else
       
   142             {
       
   143             User::Panic(_L("Unk.type"), 888);
       
   144             }
       
   145         }
       
   146     }
       
   147 
       
   148 // --------------------------------------------------------------------------------------------
       
   149 
       
   150 void CCPULoad::DoHeaveStuff(TInt aMode)
       
   151     {
       
   152     TTime now;
       
   153     now.HomeTime();
       
   154     TInt64 seed = now.Int64();
       
   155     
       
   156     TReal random = Math::FRand(seed);
       
   157     
       
   158     TReal target(10);
       
   159     TReal source(10);
       
   160     
       
   161     target += random;
       
   162     
       
   163     Math::Cos(target, source);
       
   164     
       
   165     source = source / 1.0382873;
       
   166     source -= 32.24343;
       
   167     source += 132.24343;
       
   168     source *= random;
       
   169     
       
   170     // yield trick
       
   171     if (aMode == ECpuLoadModeYielding)
       
   172         {
       
   173         // sleep randomly
       
   174         if (User::TickCount() % 50 == 0)
       
   175             User::AfterHighRes(1);            
       
   176         }
       
   177     }
       
   178 
       
   179 // --------------------------------------------------------------------------------------------
       
   180 
       
   181 void CCPULoad::Resume()
       
   182     {
       
   183     CLoadBase::Resume();
       
   184     
       
   185     iThread.Resume();
       
   186     }
       
   187 
       
   188 // --------------------------------------------------------------------------------------------
       
   189 
       
   190 void CCPULoad::Suspend()
       
   191     {
       
   192     CLoadBase::Suspend();
       
   193     
       
   194     iThread.Suspend();
       
   195     }
       
   196 
       
   197 // --------------------------------------------------------------------------------------------
       
   198 
       
   199 void CCPULoad::SetPriority()
       
   200     {
       
   201     CLoadBase::SetPriority();
       
   202     
       
   203     iThread.SetPriority(CLoadGenUtils::SettingItemToThreadPriority(iAttributes.iPriority));
       
   204     }
       
   205     
       
   206 // --------------------------------------------------------------------------------------------
       
   207 
       
   208 void CCPULoad::Close()
       
   209     {
       
   210     CLoadBase::Close();
       
   211 
       
   212     // kill the thread immediately
       
   213     iThread.Kill(0);   
       
   214 
       
   215     iThread.Close();
       
   216     }
       
   217     
       
   218 // --------------------------------------------------------------------------------------------
       
   219 
       
   220 TPtrC CCPULoad::Description()
       
   221     {
       
   222     TBuf<256> buf;
       
   223     TBuf<16> prioBuf;
       
   224     CLoadGenUtils::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf);
       
   225     
       
   226     if (iAttributes.iType == ECpuLoadTypeContinuous)
       
   227         {
       
   228         if (iAttributes.iMode == ECpuLoadModeYielding)
       
   229             {
       
   230             _LIT(KCPULoadEntryContinuous, "[%d] CPULoad prio=%S mode=yielding type=cont");
       
   231             buf.Format(KCPULoadEntryContinuous, iAttributes.iId, &prioBuf);
       
   232             }
       
   233         else if (iAttributes.iMode == ECpuLoadModeBlocking)
       
   234             {
       
   235             _LIT(KCPULoadEntryContinuous, "[%d] CPULoad prio=%S mode=blocking type=cont");
       
   236             buf.Format(KCPULoadEntryContinuous, iAttributes.iId, &prioBuf);
       
   237             }
       
   238         }
       
   239     
       
   240     else if (iAttributes.iType == ECpuLoadTypePeriodic)
       
   241         {
       
   242         if (iAttributes.iMode == ECpuLoadModeYielding)
       
   243             {
       
   244             _LIT(KCPULoadEntryPeriodic, "[%d] CPULoad prio=%S mode=yielding type=period peak=%dms idle=%dms random=%d%%");
       
   245             buf.Format(KCPULoadEntryPeriodic, iAttributes.iId, &prioBuf, iAttributes.iLength, iAttributes.iIdle, iAttributes.iRandomVariance);
       
   246             }        
       
   247         else if (iAttributes.iMode == ECpuLoadModeBlocking)
       
   248             {
       
   249             _LIT(KCPULoadEntryPeriodic, "[%d] CPULoad prio=%S mode=blocking type=period peak=%dms idle=%dms random=%d%%");
       
   250             buf.Format(KCPULoadEntryPeriodic, iAttributes.iId, &prioBuf, iAttributes.iLength, iAttributes.iIdle, iAttributes.iRandomVariance);
       
   251             }        
       
   252         }
       
   253     
       
   254     // if we are running a load in a specific cpu, add the "name" of the
       
   255     // cpu to the description. (SMP environment)
       
   256     if (iAttributes.iCpu >= 0 && iAttributes.iCpu != KCPUSelection_AllCPUs)
       
   257         {
       
   258         TBuf<15> cpu;
       
   259         _LIT(KCPU, " CPU%d");
       
   260         cpu.Format(KCPU, iAttributes.iCpu);
       
   261         buf.Append(cpu);
       
   262         }
       
   263     
       
   264     return TPtrC(buf);
       
   265     }               
       
   266 
       
   267 // --------------------------------------------------------------------------------------------
       
   268 
       
   269 
       
   270 // End of File