commands/memsampler/memsamplerdd.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // memsamplerdd.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 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 
       
    13 #include <fshell/extrabtrace.h>
       
    14 #include <platform.h>
       
    15 #include <kern_priv.h>
       
    16 #include "memsamplerdd.h"
       
    17 #include <fshell/common.mmh>
       
    18 
       
    19 const TInt KMinRate = 10;
       
    20 const TInt KMaxRate = 10000;
       
    21 const TInt KMajorVersionNumber = 1;
       
    22 const TInt KMinorVersionNumber = 0;
       
    23 const TInt KBuildVersionNumber = 0;
       
    24 const TInt KArrayGranularity = 32;
       
    25 const TInt KDfcThreadPriority = 26;
       
    26 _LIT(KDfcThreadName, "MemSamplerDD");
       
    27 
       
    28 class DDeviceMemSampler : public DLogicalDevice
       
    29 	{
       
    30 public:
       
    31 	DDeviceMemSampler();
       
    32 	virtual TInt Install();
       
    33 	virtual void GetCaps(TDes8& aDes) const;
       
    34 	virtual TInt Create(DLogicalChannelBase*& aChannel);
       
    35 	};
       
    36 
       
    37 class TChunkInfo
       
    38 	{
       
    39 public:
       
    40 	TChunkInfo(DChunk& aChunk);
       
    41 	TBool operator==(const TChunkInfo& aChunkInfo);
       
    42 public:
       
    43 	DChunk* iAddress;
       
    44 	TInt iSize;
       
    45 	TInt iHighWaterMark;
       
    46 	TBool iSeen;
       
    47 	};
       
    48 
       
    49 TChunkInfo::TChunkInfo(DChunk& aChunk)
       
    50 	: iAddress(&aChunk), iSize(aChunk.Size()), iHighWaterMark(aChunk.Size()), iSeen(ETrue)
       
    51 	{
       
    52 	}
       
    53 
       
    54 TBool TChunkInfo::operator==(const TChunkInfo& aChunkInfo)
       
    55 	{
       
    56 	return (iSize == aChunkInfo.iSize);
       
    57 	}
       
    58 
       
    59 class DMemSamplerChannel : public DLogicalChannel
       
    60 	{
       
    61 public:
       
    62 	DMemSamplerChannel();
       
    63 	~DMemSamplerChannel();
       
    64 protected:
       
    65 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    66 	virtual void HandleMsg(TMessageBase* aMsg);
       
    67 private:
       
    68 	TInt StartSampling(TInt aRate);
       
    69 	TInt StopSampling();
       
    70 	TInt ResetSampling();
       
    71 	TInt ReadChunks();
       
    72 	void TraceNewChunk(const TChunkInfo& aChunkInfo) const;
       
    73 	void TraceChangedChunk(const TChunkInfo& aChunkInfo) const;
       
    74 	void TraceDeletedChunk(const TChunkInfo& aChunkInfo) const;
       
    75 	void TraceNewSample() const;
       
    76 #if FSHELL_PLATFORM_SYMTB >= 92
       
    77 	inline TBool Running()
       
    78 		{return iTimer.IsPending();}
       
    79 #else
       
    80 	inline TBool Running()
       
    81 		{return iTimer.iState!=NTimer::EIdle;}
       
    82 #endif
       
    83 private:
       
    84 	static void Sample(TAny*);
       
    85 	static void DoSample(TAny*);
       
    86 	static void ExitDfcThread(TAny*);
       
    87 private:
       
    88 	RArray<TChunkInfo> iChunkInfo;
       
    89 	NTimer iTimer;
       
    90 	TInt iPeriod;
       
    91 	TInt iPeriodNumber;
       
    92 	TDfcQue iPrivateDfcQ;
       
    93 	TDfc iDoSampleDfc;
       
    94 	TDfc iExitDfc;
       
    95 	DThread* iClient;
       
    96 	mutable TBool iNewSampleTraced;
       
    97 	};
       
    98 
       
    99 DECLARE_STANDARD_LDD()
       
   100 	{
       
   101 	return new DDeviceMemSampler;
       
   102 	}
       
   103 
       
   104 DDeviceMemSampler::DDeviceMemSampler()
       
   105 	{
       
   106 	iVersion=TVersion(KMajorVersionNumber, KMinorVersionNumber, KBuildVersionNumber);
       
   107 	}
       
   108 
       
   109 TInt DDeviceMemSampler::Install()
       
   110 	{
       
   111 	TInt r = SetName(&KMemSamplerName);
       
   112 	return r;
       
   113 	}
       
   114 
       
   115 void DDeviceMemSampler::GetCaps(TDes8&) const
       
   116 	{
       
   117 	}
       
   118 
       
   119 TInt DDeviceMemSampler::Create(DLogicalChannelBase*& aChannel)
       
   120 	{
       
   121 	aChannel = new DMemSamplerChannel;
       
   122 	return aChannel ? KErrNone : KErrNoMemory;
       
   123 	}
       
   124 
       
   125 DMemSamplerChannel::DMemSamplerChannel()
       
   126 	: iChunkInfo(KArrayGranularity, _FOFF(TChunkInfo, iAddress)), iTimer(Sample, this), iPeriodNumber(-1), iDoSampleDfc(DoSample, this, 1), iExitDfc(ExitDfcThread, this, 0), iNewSampleTraced(EFalse)
       
   127 	{
       
   128 	}
       
   129 
       
   130 DMemSamplerChannel::~DMemSamplerChannel()
       
   131 	{
       
   132 	Kern::SafeClose((DObject*&)iClient, NULL);
       
   133 	iExitDfc.Enque(); 
       
   134 	}
       
   135 
       
   136 void DMemSamplerChannel::ExitDfcThread(TAny*)
       
   137 	{
       
   138 	Kern::Exit(KErrNone);
       
   139 	} 
       
   140 
       
   141 TInt DMemSamplerChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*anInfo*/, const TVersion& aVer)
       
   142 	{
       
   143 	if (!Kern::QueryVersionSupported(TVersion(1,0,0),aVer))
       
   144 		{
       
   145 		return KErrNotSupported;
       
   146 		}
       
   147 	iClient=&Kern::CurrentThread();
       
   148 	iClient->Open();
       
   149 	TInt err = Kern::DfcQInit(&iPrivateDfcQ, KDfcThreadPriority, &KDfcThreadName);
       
   150 	if (err == KErrNone)
       
   151 		{
       
   152     	SetDfcQ(&iPrivateDfcQ);
       
   153 		iDoSampleDfc.SetDfcQ(&iPrivateDfcQ);
       
   154 		iExitDfc.SetDfcQ(&iPrivateDfcQ);
       
   155     	iMsgQ.Receive();		
       
   156 		}
       
   157 	
       
   158 	return err;
       
   159 	}
       
   160 
       
   161 TInt DMemSamplerChannel::StartSampling(TInt aRate)
       
   162 	{
       
   163 	DEBUG_MEMSAMPLER(Kern::Printf("START");)
       
   164 	ReadChunks();
       
   165 	aRate = Min(KMaxRate, Max(KMinRate, aRate));
       
   166 	iPeriod = NKern::TimerTicks(aRate);
       
   167 	if (!Running())
       
   168 		{
       
   169 		iTimer.OneShot(iPeriod, EFalse);
       
   170 		}
       
   171 	DEBUG_MEMSAMPLER(Kern::Printf("START end");)
       
   172 	return KErrNone;
       
   173 	}
       
   174 
       
   175 TInt DMemSamplerChannel::StopSampling()
       
   176 	{
       
   177 	DEBUG_MEMSAMPLER(Kern::Printf("STOP");)
       
   178 	if (Running())
       
   179 		{
       
   180 		iTimer.Cancel();
       
   181 		}
       
   182 	DEBUG_MEMSAMPLER(Kern::Printf("STOP end");)
       
   183 	return KErrNone;
       
   184 	}
       
   185 
       
   186 TInt DMemSamplerChannel::ResetSampling()
       
   187 	{
       
   188 	DEBUG_MEMSAMPLER(Kern::Printf("RESET");)
       
   189 	iChunkInfo.Reset();
       
   190 	DEBUG_MEMSAMPLER(Kern::Printf("RESET end");)
       
   191 	return KErrNone;
       
   192 	}
       
   193 
       
   194 TInt DMemSamplerChannel::ReadChunks()
       
   195 	{
       
   196 	++iPeriodNumber;
       
   197 	iNewSampleTraced = EFalse;
       
   198 
       
   199 	for (TInt i = (iChunkInfo.Count() - 1); i >= 0; --i)
       
   200 		{
       
   201 		iChunkInfo[i].iSeen = EFalse;
       
   202 		}
       
   203 
       
   204 	DObjectCon* const * containers = Kern::Containers();
       
   205 	DObjectCon& container = *containers[EChunk];
       
   206 	container.Wait();
       
   207 
       
   208 	for (TInt i = (container.Count() - 1); i >= 0; --i)
       
   209 		{
       
   210 		TChunkInfo thisChunk(*(DChunk*)container[i]);
       
   211 		TInt pos;
       
   212 		TInt err = iChunkInfo.FindInUnsignedKeyOrder(thisChunk, pos);
       
   213 		if (err == KErrNotFound)
       
   214 			{
       
   215 			if (pos < iChunkInfo.Count())
       
   216 				{
       
   217 				iChunkInfo.Insert(thisChunk, pos);
       
   218 				}
       
   219 			else
       
   220 				{
       
   221 				TInt err = iChunkInfo.Append(thisChunk);
       
   222 				if (err)
       
   223 					{
       
   224 					container.Signal();
       
   225 					return err;
       
   226 					}
       
   227 				}
       
   228 			TraceNewChunk(thisChunk);
       
   229 			}
       
   230 		else
       
   231 			{
       
   232 			if (thisChunk == iChunkInfo[pos])
       
   233 				{
       
   234 				// Chunk size hasn't changed - ignore.
       
   235 				iChunkInfo[pos].iSeen = ETrue;
       
   236 				}
       
   237 			else
       
   238 				{
       
   239 				TChunkInfo& c = iChunkInfo[pos];
       
   240 				if (thisChunk.iSize > c.iHighWaterMark)
       
   241 					{
       
   242 					c.iHighWaterMark = thisChunk.iSize;
       
   243 					}
       
   244 				c.iSize = thisChunk.iSize;
       
   245 				c.iSeen = ETrue;
       
   246 				TraceChangedChunk(c);
       
   247 				}
       
   248 			}
       
   249 		}
       
   250 
       
   251 	container.Signal();
       
   252 
       
   253 	for (TInt i = (iChunkInfo.Count() - 1); i >= 0; --i)
       
   254 		{
       
   255 		const TChunkInfo& chunkInfo = iChunkInfo[i];
       
   256 		if (!chunkInfo.iSeen)
       
   257 			{
       
   258 			TraceDeletedChunk(chunkInfo);
       
   259 			iChunkInfo.Remove(i);
       
   260 			}
       
   261 		}
       
   262 
       
   263 	return KErrNone;
       
   264 	}
       
   265 
       
   266 void DMemSamplerChannel::TraceNewChunk(const TChunkInfo& aChunkInfo) const
       
   267 	{
       
   268 	TraceNewSample();
       
   269 	TFullName nameBuf;
       
   270 	aChunkInfo.iAddress->FullName(nameBuf);				
       
   271 	BTraceN(RMemSampler::EBtraceCategory, RMemSampler::ENewChunk, aChunkInfo.iAddress, aChunkInfo.iAddress->MaxSize(), nameBuf.Ptr(), nameBuf.Size());
       
   272 	TraceChangedChunk(aChunkInfo);
       
   273 	}
       
   274 
       
   275 void DMemSamplerChannel::TraceChangedChunk(const TChunkInfo& aChunkInfo) const
       
   276 	{
       
   277 	TraceNewSample();
       
   278 	BTraceContext12(RMemSampler::EBtraceCategory, RMemSampler::EChangedChunk, aChunkInfo.iAddress, aChunkInfo.iSize, aChunkInfo.iHighWaterMark);
       
   279 	}
       
   280 
       
   281 void DMemSamplerChannel::TraceDeletedChunk(const TChunkInfo& aChunkInfo) const
       
   282 	{
       
   283 	TraceNewSample();
       
   284 	BTraceContext4(RMemSampler::EBtraceCategory, RMemSampler::EDeletedChunk, aChunkInfo.iAddress);
       
   285 	}
       
   286 
       
   287 void DMemSamplerChannel::TraceNewSample() const
       
   288 	{
       
   289 	if (!iNewSampleTraced)
       
   290 		{
       
   291 		iNewSampleTraced = ETrue;
       
   292 		BTraceContext8(RMemSampler::EBtraceCategory, RMemSampler::ENewSample, iPeriod, iPeriodNumber);
       
   293 		}
       
   294 	}
       
   295 
       
   296 void DMemSamplerChannel::HandleMsg(TMessageBase* aMsg)
       
   297 	{
       
   298 	TInt r=KErrNone;
       
   299 	TThreadMessage& m=*(TThreadMessage*)aMsg;
       
   300 	TInt id=m.iValue;
       
   301 	if (id==(TInt)ECloseMsg)
       
   302 		{
       
   303 		DEBUG_MEMSAMPLER(Kern::Printf("CLOSE");)
       
   304 		iTimer.Cancel();
       
   305 		m.Complete(KErrNone,EFalse);
       
   306 		iMsgQ.CompleteAll(KErrServerTerminated);
       
   307 		DEBUG_MEMSAMPLER(Kern::Printf("CLOSE end");)
       
   308 		return;
       
   309 		}
       
   310 	else
       
   311 		{
       
   312 		switch(id)
       
   313 			{
       
   314 			case RMemSampler::EControlStartProfile:
       
   315 				r = StartSampling(m.Int0());
       
   316 				break;
       
   317 			case RMemSampler::EControlStopProfile:
       
   318 				r = StopSampling();
       
   319 				break;
       
   320 			case RMemSampler::EControlResetProfile:
       
   321 				r = ResetSampling();
       
   322 				break;
       
   323 			default:
       
   324 				r = KErrNotSupported;
       
   325 				break;
       
   326 			}
       
   327 		}
       
   328 	m.Complete(r,ETrue);
       
   329 	}
       
   330 
       
   331 void DMemSamplerChannel::Sample(TAny* aPtr)
       
   332 	{
       
   333 	DMemSamplerChannel& d = *(DMemSamplerChannel*)aPtr;
       
   334 	d.iTimer.Again(d.iPeriod);
       
   335 	d.iDoSampleDfc.Add(); 
       
   336 	}
       
   337 
       
   338 void DMemSamplerChannel::DoSample(TAny* aPtr)
       
   339 	{
       
   340 	DMemSamplerChannel& d = *(DMemSamplerChannel*)aPtr;
       
   341 	d.ReadChunks();
       
   342 	}