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