libraries/extrabtrace/src/sampler.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // sampler.cpp
       
     2 // 
       
     3 // Copyright (c) 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 #include "sampler.h"
       
    13 #include <fshell/btrace_parser_defs.h>
       
    14 #include <fshell/common.mmh>
       
    15 
       
    16 // Please note this code is NOT SMP-safe. The whole mechanism of using an interrupt doesn't work under SMP so there's no point adding TSpinLocks or similar
       
    17 #ifdef __SMP__
       
    18 #error "Sampler doesn't work under SMP!"
       
    19 #endif
       
    20 
       
    21 TUint IntStackPtr();
       
    22 TUint IDFCRunning();
       
    23 
       
    24 DCpuSampler::DCpuSampler()
       
    25 	: iTimer(NTimer(&Sample, this))
       
    26 	{
       
    27 #if defined(__MARM__)
       
    28 	iIntStackTop=(TUint*)IntStackPtr();
       
    29 #endif
       
    30 	}
       
    31 
       
    32 DCpuSampler::~DCpuSampler()
       
    33 	{
       
    34 	iTimer.Cancel();
       
    35 	}
       
    36 
       
    37 void DCpuSampler::Sample(TAny* aPtr)
       
    38 	{
       
    39 	static_cast<DCpuSampler*>(aPtr)->DoSample();
       
    40 	}
       
    41 
       
    42 void DCpuSampler::DoSample()
       
    43 	{
       
    44 	TInt err = iTimer.Again(1);
       
    45 	if (err == KErrArgument)
       
    46 		{
       
    47 		// Missed a tick, oh well
       
    48 		iTimer.OneShot(1);
       
    49 		}
       
    50 
       
    51 	NThread* thread = NKern::CurrentThread();
       
    52 	if (iCpuUsageCallbackFn)
       
    53 		{
       
    54 		// Not used by anything in fshell pkg
       
    55 		(*iCpuUsageCallbackFn)(iContext, thread);
       
    56 		}
       
    57 
       
    58 	if (iProfiling)
       
    59 		{
       
    60 		Usample(thread);
       
    61 		}
       
    62 	iContext = thread;
       
    63 	}
       
    64 
       
    65 TBool DCpuSampler::ShouldRun() const
       
    66 	{
       
    67 	return iProfiling || iCpuUsageCallbackFn != NULL;
       
    68 	}
       
    69 
       
    70 void DCpuSampler::SettingsChanged()
       
    71 	{
       
    72 	// Call with interrupts disabled
       
    73 	if (ShouldRun())
       
    74 		{
       
    75 #if FSHELL_PLATFORM_SYMTB >= 92
       
    76 		if (!iTimer.IsPending())
       
    77 #else
       
    78 		if (iTimer.iState == NTimer::EIdle)
       
    79 #endif
       
    80 			{
       
    81 			iContext = NULL;
       
    82 			iTimerHeartbeat = 0;
       
    83 			iTimer.OneShot(1);
       
    84 			}
       
    85 		}
       
    86 	else
       
    87 		{
       
    88 		iTimer.Cancel();
       
    89 		}
       
    90 	}
       
    91 
       
    92 void DCpuSampler::SetCpuUsageSampling(MExtraBtrace::TCpuUsageCallback aCallbackFn)
       
    93 	{
       
    94 	TUint irq = NKern::DisableAllInterrupts();
       
    95 	iCpuUsageCallbackFn = aCallbackFn;
       
    96 	SettingsChanged();
       
    97 	NKern::RestoreInterrupts(irq);
       
    98 	}
       
    99 
       
   100 
       
   101 void DCpuSampler::SetProfilingSampling(TBool aEnabled)
       
   102 	{
       
   103 	TUint irq = NKern::DisableAllInterrupts();
       
   104 	iProfiling = aEnabled;
       
   105 	SettingsChanged();
       
   106 	NKern::RestoreInterrupts(irq);
       
   107 	}
       
   108 
       
   109 void DCpuSampler::Usample(NThread* pN)
       
   110 	{
       
   111 	if (Kern::NThreadToDThread(pN)!=NULL)
       
   112 		{
       
   113 #if defined(__MARM__)
       
   114 		TBool idfcRunning = IDFCRunning();
       
   115 		TLinAddr pC=((iIntStackTop)[-1]) & ~1; //clear LSB bit (in case of Jazelle code)
       
   116 #else
       
   117 		// Can't get this info if we don't have the .cia functions
       
   118 		TBool idfcRunning = EFalse;
       
   119 		TLinAddr pC = 0;
       
   120 #endif
       
   121 
       
   122 		if (idfcRunning)
       
   123 			{
       
   124 			//no useful thread context
       
   125 			BTrace4(BTrace::EProfiling,BTrace::ECpuIdfcSample,pC);
       
   126 			}
       
   127 		else
       
   128 		    {
       
   129 			if (iContext!=pN)
       
   130 				{
       
   131 				BTrace8(BTrace::EProfiling,BTrace::ECpuFullSample,pC,pN);
       
   132 				}
       
   133 			else
       
   134 				{
       
   135 				//optimisation - context of previous sample is assumed
       
   136 				BTrace4(BTrace::EProfiling,BTrace::ECpuOptimisedSample,pC);
       
   137 				}
       
   138  		    }
       
   139 		}
       
   140 	else
       
   141 		{
       
   142 		BTrace0(BTrace::EProfiling,BTrace::ECpuNonSymbianThreadSample);
       
   143 		}
       
   144 	}