commands/memsampler/memsampler.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // memsampler.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/ioutils.h>
       
    14 #include <fshell/common.mmh>
       
    15 #include <e32cons.h>
       
    16 #include FSHELL_D32BTRACE_HEADER
       
    17 #include "memsamplerdd.h"
       
    18 
       
    19 _LIT(KLdd, "memsamplerdd");	
       
    20 const TInt KMaxCategories = 256;
       
    21 const TInt KBtraceBufferSize = 64 * 1024;
       
    22 
       
    23 using namespace IoUtils;
       
    24 
       
    25 
       
    26 class TChunkInfo
       
    27 	{
       
    28 public:
       
    29 	TChunkInfo(TUint32 aAddress);
       
    30 	TChunkInfo(const TDesC& aName, TUint32 aAddress, TInt aMaxSize);
       
    31 	static TBool Match(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    32 	static TInt SortByUpdateHistoryAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    33 	static TInt SortByUpdateHistoryDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    34 	static TInt SortByNameAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    35 	static TInt SortByNameDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    36 	static TInt SortByMaxSizeAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    37 	static TInt SortByMaxSizeDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    38 	static TInt SortByCurrentSizeAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    39 	static TInt SortByCurrentSizeDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    40 	static TInt SortByHighWaterMarkAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    41 	static TInt SortByHighWaterMarkDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB);
       
    42 public:
       
    43 	TInt iUpdateCount;
       
    44 	TFullName iName;
       
    45 	TUint32 iAddress;
       
    46 	TInt iMaxSize;
       
    47 	TInt iCurrentSize;
       
    48 	TInt iHighWaterMark;
       
    49 	};
       
    50 
       
    51 TChunkInfo::TChunkInfo(TUint32 aAddress)
       
    52 	: iUpdateCount(0), iAddress(aAddress), iMaxSize(0), iCurrentSize(0), iHighWaterMark(0)
       
    53 	{
       
    54 	}
       
    55 
       
    56 TChunkInfo::TChunkInfo(const TDesC& aName, TUint32 aAddress, TInt aMaxSize)
       
    57 	: iUpdateCount(0), iName(aName), iAddress(aAddress), iMaxSize(aMaxSize), iCurrentSize(0), iHighWaterMark(0)
       
    58 	{
       
    59 	}
       
    60 
       
    61 TBool TChunkInfo::Match(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    62 	{
       
    63 	return (aChunkInfoA.iAddress == aChunkInfoB.iAddress);
       
    64 	}
       
    65 
       
    66 TInt TChunkInfo::SortByUpdateHistoryAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    67 	{
       
    68 	return (aChunkInfoA.iUpdateCount > aChunkInfoB.iUpdateCount);
       
    69 	}
       
    70 
       
    71 TInt TChunkInfo::SortByUpdateHistoryDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    72 	{
       
    73 	return (aChunkInfoA.iUpdateCount < aChunkInfoB.iUpdateCount);
       
    74 	}
       
    75 
       
    76 TInt TChunkInfo::SortByNameAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    77 	{
       
    78 	return (aChunkInfoA.iName.CompareC(aChunkInfoB.iName));
       
    79 	}
       
    80 
       
    81 TInt TChunkInfo::SortByNameDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    82 	{
       
    83 	return (aChunkInfoB.iName.CompareC(aChunkInfoA.iName));
       
    84 	}
       
    85 
       
    86 TInt TChunkInfo::SortByMaxSizeAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    87 	{
       
    88 	return (aChunkInfoA.iMaxSize > aChunkInfoB.iMaxSize);
       
    89 	}
       
    90 
       
    91 TInt TChunkInfo::SortByMaxSizeDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    92 	{
       
    93 	return (aChunkInfoA.iMaxSize < aChunkInfoB.iMaxSize);
       
    94 	}
       
    95 
       
    96 TInt TChunkInfo::SortByCurrentSizeAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
    97 	{
       
    98 	return (aChunkInfoA.iCurrentSize > aChunkInfoB.iCurrentSize);
       
    99 	}
       
   100 
       
   101 TInt TChunkInfo::SortByCurrentSizeDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
   102 	{
       
   103 	return (aChunkInfoA.iCurrentSize < aChunkInfoB.iCurrentSize);
       
   104 	}
       
   105 
       
   106 TInt TChunkInfo::SortByHighWaterMarkAscending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
   107 	{
       
   108 	return (aChunkInfoA.iHighWaterMark > aChunkInfoB.iHighWaterMark);
       
   109 	}
       
   110 
       
   111 TInt TChunkInfo::SortByHighWaterMarkDescending(const TChunkInfo& aChunkInfoA, const TChunkInfo& aChunkInfoB)
       
   112 	{
       
   113 	return (aChunkInfoA.iHighWaterMark < aChunkInfoB.iHighWaterMark);
       
   114 	}
       
   115 
       
   116 
       
   117 class CMemoryView : public CBase
       
   118 	{
       
   119 public:
       
   120 	enum TSortType
       
   121 		{
       
   122 		ESortUnspecified,
       
   123 		ESortByChunkName,
       
   124 		ESortByCurrentSize,
       
   125 		ESortByHighWaterMark,
       
   126 		ESortByMaxSize,
       
   127 		ESortByUpdateHistory
       
   128 		};
       
   129 	enum TSortOrder
       
   130 		{
       
   131 		EAscending,
       
   132 		EDescending
       
   133 		};
       
   134 public:
       
   135 	static CMemoryView* NewL(RIoWriteHandle& aStdout, RIoWriteHandle& aStderr, TBool aHuman);
       
   136 	~CMemoryView();
       
   137 	void ChangeSortType(TSortType aSortType);
       
   138 	void ChangeSortOrder(TSortOrder aSortOrder);
       
   139 	void ScrollUp();
       
   140 	void ScrollDown();
       
   141 	void ScrollLeft();
       
   142 	void ScrollRight();
       
   143 	void HandleNewChunk(const TDesC& aName, TUint32 aAddress, TInt aMaxSize);
       
   144 	void HandleChangedChunk(TUint32 aAddress, TInt aSize, TInt aHighWaterMark);
       
   145 	void HandleDeletedChunk(TUint32 aAddress);
       
   146 	void UpdateL();
       
   147 private:
       
   148 	CMemoryView(RIoWriteHandle& aStderr, TBool aHuman);
       
   149 	void ConstructL(RIoWriteHandle& aStdout);
       
   150 	void Sort();
       
   151 	void PrintWarning(TRefByValue<const TDesC> aFmt, ...);
       
   152 private:
       
   153 	RIoWriteHandle& iStderr;
       
   154 	TBool iHuman;
       
   155 	RIoConsoleWriteHandle iStdout;
       
   156 	TInt iNumConsoleLines;
       
   157 	TInt iNumLinesInLastUpdate;
       
   158 	RArray<TChunkInfo> iChunkList;
       
   159 	CTextBuffer* iBuffer;
       
   160 	CTextFormatter* iFormatter;
       
   161 	TSortOrder iSortOrder;
       
   162 	TLinearOrder<TChunkInfo> iSortByUpdateHistoryAscending;
       
   163 	TLinearOrder<TChunkInfo> iSortByUpdateHistoryDescending;
       
   164 	TLinearOrder<TChunkInfo> iSortByChunkNameAscending;
       
   165 	TLinearOrder<TChunkInfo> iSortByChunkNameDescending;
       
   166 	TLinearOrder<TChunkInfo> iSortByCurrentSizeAscending;
       
   167 	TLinearOrder<TChunkInfo> iSortByCurrentSizeDescending;
       
   168 	TLinearOrder<TChunkInfo> iSortByHighWaterMarkAscending;
       
   169 	TLinearOrder<TChunkInfo> iSortByHighWaterMarkDescending;
       
   170 	TLinearOrder<TChunkInfo> iSortByMaxSizeAscending;
       
   171 	TLinearOrder<TChunkInfo> iSortByMaxSizeDescending;
       
   172 	TLinearOrder<TChunkInfo>* iCurrentSortType;
       
   173 	TInt iVerticalOffset;
       
   174 	TInt iHorizontalOffset;
       
   175 	};
       
   176 
       
   177 CMemoryView* CMemoryView::NewL(RIoWriteHandle& aStdout, RIoWriteHandle& aStderr, TBool aHuman)
       
   178 	{
       
   179 	CMemoryView* self = new(ELeave) CMemoryView(aStderr, aHuman);
       
   180 	CleanupStack::PushL(self);
       
   181 	self->ConstructL(aStdout);
       
   182 	CleanupStack::Pop(self);
       
   183 	return self;
       
   184 	}
       
   185 
       
   186 CMemoryView::~CMemoryView()
       
   187 	{
       
   188 	delete iBuffer;
       
   189 	delete iFormatter;
       
   190 	iChunkList.Close();
       
   191 	}
       
   192 
       
   193 void CMemoryView::ChangeSortType(TSortType aSortType)
       
   194 	{
       
   195 	switch (aSortType)
       
   196 		{
       
   197 		default:
       
   198 		case ESortByChunkName:
       
   199 			{
       
   200 			if (iCurrentSortType == &iSortByChunkNameAscending)
       
   201 				{
       
   202 				iCurrentSortType = &iSortByChunkNameDescending;
       
   203 				}
       
   204 			else if (iCurrentSortType == &iSortByChunkNameDescending)
       
   205 				{
       
   206 				iCurrentSortType = &iSortByChunkNameAscending;
       
   207 				}
       
   208 			else
       
   209 				{
       
   210 				iCurrentSortType = &iSortByChunkNameAscending;
       
   211 				}
       
   212 			break;
       
   213 			}
       
   214 		case ESortByCurrentSize:
       
   215 			{
       
   216 			if (iCurrentSortType == &iSortByCurrentSizeAscending)
       
   217 				{
       
   218 				iCurrentSortType = &iSortByCurrentSizeDescending;
       
   219 				}
       
   220 			else if (iCurrentSortType == &iSortByCurrentSizeDescending)
       
   221 				{
       
   222 				iCurrentSortType = &iSortByCurrentSizeAscending;
       
   223 				}
       
   224 			else
       
   225 				{
       
   226 				iCurrentSortType = &iSortByCurrentSizeDescending;
       
   227 				}
       
   228 			break;
       
   229 			}
       
   230 		case ESortByHighWaterMark:
       
   231 			{
       
   232 			if (iCurrentSortType == &iSortByHighWaterMarkAscending)
       
   233 				{
       
   234 				iCurrentSortType = &iSortByHighWaterMarkDescending;
       
   235 				}
       
   236 			else if (iCurrentSortType == &iSortByHighWaterMarkDescending)
       
   237 				{
       
   238 				iCurrentSortType = &iSortByHighWaterMarkAscending;
       
   239 				}
       
   240 			else
       
   241 				{
       
   242 				iCurrentSortType = &iSortByHighWaterMarkDescending;
       
   243 				}
       
   244 			break;
       
   245 			}
       
   246 		case ESortByMaxSize:
       
   247 			{
       
   248 			if (iCurrentSortType == &iSortByMaxSizeAscending)
       
   249 				{
       
   250 				iCurrentSortType = &iSortByMaxSizeDescending;
       
   251 				}
       
   252 			else if (iCurrentSortType == &iSortByMaxSizeDescending)
       
   253 				{
       
   254 				iCurrentSortType = &iSortByMaxSizeAscending;
       
   255 				}
       
   256 			else
       
   257 				{
       
   258 				iCurrentSortType = &iSortByMaxSizeDescending;
       
   259 				}
       
   260 			break;
       
   261 			}
       
   262 		case ESortByUpdateHistory:
       
   263 			{
       
   264 			if (iCurrentSortType == &iSortByUpdateHistoryAscending)
       
   265 				{
       
   266 				iCurrentSortType = &iSortByUpdateHistoryDescending;
       
   267 				}
       
   268 			else if (iCurrentSortType == &iSortByUpdateHistoryDescending)
       
   269 				{
       
   270 				iCurrentSortType = &iSortByUpdateHistoryAscending;
       
   271 				}
       
   272 			else
       
   273 				{
       
   274 				iCurrentSortType = &iSortByUpdateHistoryAscending;
       
   275 				}
       
   276 			break;
       
   277 			}
       
   278 		}
       
   279 	Sort();
       
   280 	}
       
   281 
       
   282 void CMemoryView::ChangeSortOrder(TSortOrder aSortOrder)
       
   283 	{
       
   284 	iSortOrder = aSortOrder;
       
   285 	}
       
   286 
       
   287 void CMemoryView::ScrollUp()
       
   288 	{
       
   289 	if (iChunkList.Count() > iNumConsoleLines)
       
   290 		{
       
   291 		if (iVerticalOffset > 0)
       
   292 			{
       
   293 			// Not yet reached the top.
       
   294 			--iVerticalOffset;
       
   295 			}
       
   296 		}
       
   297 	}
       
   298 
       
   299 void CMemoryView::ScrollDown()
       
   300 	{
       
   301 	const TInt numChunks = iChunkList.Count();
       
   302 	if (numChunks > iNumConsoleLines)
       
   303 		{
       
   304 		if ((numChunks - iVerticalOffset) > iNumConsoleLines)
       
   305 			{
       
   306 			// Not yet reached the bottom.
       
   307 			++iVerticalOffset;
       
   308 			}
       
   309 		}
       
   310 	}
       
   311 
       
   312 void CMemoryView::ScrollLeft()
       
   313 	{
       
   314 	if (iHorizontalOffset > 0)
       
   315 		{
       
   316 		--iHorizontalOffset;
       
   317 		}
       
   318 	}
       
   319 
       
   320 void CMemoryView::ScrollRight()
       
   321 	{
       
   322 	// Allow the horizontal offset to increase unboundedly here - it'll be limited to something sensible in UpdateL.
       
   323 	++iHorizontalOffset;
       
   324 	}
       
   325 
       
   326 void CMemoryView::HandleNewChunk(const TDesC& aName, TUint32 aAddress, TInt aMaxSize)
       
   327 	{
       
   328 	// Age all the existing chunk's update counts by one.
       
   329 	const TInt numChunks = iChunkList.Count();
       
   330 	for (TInt i = 0; i < numChunks; ++i)
       
   331 		{
       
   332 		++iChunkList[i].iUpdateCount;
       
   333 		}
       
   334 
       
   335 	// Insert the new chunk (claiming update count zero).
       
   336 	TChunkInfo newChunkInfo(aName, aAddress, aMaxSize);
       
   337 	TInt err = iChunkList.InsertInOrderAllowRepeats(newChunkInfo, *iCurrentSortType);
       
   338 	if (err)
       
   339 		{
       
   340 		PrintWarning(_L("Couldn't handle new chunk: %d"), err);
       
   341 		}
       
   342 	}
       
   343 
       
   344 void CMemoryView::HandleChangedChunk(TUint32 aAddress, TInt aSize, TInt aHighWaterMark)
       
   345 	{
       
   346 	TChunkInfo chunkInfo(aAddress);
       
   347 	TInt pos = iChunkList.Find(chunkInfo, TChunkInfo::Match);
       
   348 	if (pos < 0)
       
   349 		{
       
   350 		PrintWarning(_L("Couldn't handle updated to chunk 0x%08x: %d"), aAddress, pos);
       
   351 		}
       
   352 	else
       
   353 		{
       
   354 		TChunkInfo& c = iChunkList[pos];
       
   355 		// Age all the chunks that have been updated more recently than this one.
       
   356 		const TInt numChunks = iChunkList.Count();
       
   357 		for (TInt i = 0; i < numChunks; ++i)
       
   358 			{
       
   359 			TChunkInfo& d = iChunkList[i];
       
   360 			if (d.iUpdateCount < c.iUpdateCount)
       
   361 				{
       
   362 				++d.iUpdateCount;
       
   363 				}
       
   364 			}
       
   365 
       
   366 		c.iUpdateCount = 0;
       
   367 		c.iCurrentSize = aSize;
       
   368 		c.iHighWaterMark = aHighWaterMark;
       
   369 		Sort();
       
   370 		}
       
   371 	}
       
   372 
       
   373 void CMemoryView::HandleDeletedChunk(TUint32 aAddress)
       
   374 	{
       
   375 	TChunkInfo chunkInfo(aAddress);
       
   376 	TInt pos = iChunkList.Find(chunkInfo, TChunkInfo::Match);
       
   377 	if (pos < 0)
       
   378 		{
       
   379 		PrintWarning(_L("Couldn't handle deletion of chunk 0x%08x: %d"), aAddress, pos);
       
   380 		}
       
   381 	else
       
   382 		{
       
   383 		TChunkInfo& c = iChunkList[pos];
       
   384 
       
   385 		// Rejuvenate chunks that haven't been updated since this one last was.
       
   386 		const TInt numChunks = iChunkList.Count();
       
   387 		for (TInt i = 0; i < numChunks; ++i)
       
   388 			{
       
   389 			TChunkInfo& d = iChunkList[i];
       
   390 			if (d.iUpdateCount > c.iUpdateCount)
       
   391 				{
       
   392 				--d.iUpdateCount;
       
   393 				}
       
   394 			}
       
   395 		
       
   396 		iChunkList.Remove(pos);
       
   397 		if (iChunkList.Count() <= iNumConsoleLines)
       
   398 			{
       
   399 			iVerticalOffset = 0;
       
   400 			}
       
   401 		}
       
   402 	}
       
   403 
       
   404 CMemoryView::CMemoryView(RIoWriteHandle& aStderr, TBool aHuman)
       
   405 	: iStderr(aStderr),
       
   406 	iHuman(aHuman),
       
   407 	iSortByUpdateHistoryAscending(TChunkInfo::SortByUpdateHistoryAscending),
       
   408 	iSortByUpdateHistoryDescending(TChunkInfo::SortByUpdateHistoryDescending),
       
   409 	iSortByChunkNameAscending(TChunkInfo::SortByNameAscending),
       
   410 	iSortByChunkNameDescending(TChunkInfo::SortByNameDescending),
       
   411 	iSortByCurrentSizeAscending(TChunkInfo::SortByCurrentSizeAscending),
       
   412 	iSortByCurrentSizeDescending(TChunkInfo::SortByCurrentSizeDescending),
       
   413 	iSortByHighWaterMarkAscending(TChunkInfo::SortByHighWaterMarkAscending),
       
   414 	iSortByHighWaterMarkDescending(TChunkInfo::SortByHighWaterMarkDescending),
       
   415 	iSortByMaxSizeAscending(TChunkInfo::SortByMaxSizeAscending),
       
   416 	iSortByMaxSizeDescending(TChunkInfo::SortByMaxSizeDescending),
       
   417 	iCurrentSortType(&iSortByUpdateHistoryAscending)
       
   418 	{
       
   419 	}
       
   420 
       
   421 void CMemoryView::ConstructL(RIoWriteHandle& aStdout)
       
   422 	{
       
   423 	if (aStdout.AttachedToConsole())
       
   424 		{
       
   425 		iStdout = aStdout;
       
   426 		TSize size;
       
   427 		User::LeaveIfError(iStdout.GetScreenSize(size));
       
   428 		iNumConsoleLines = size.iHeight;
       
   429 		iBuffer = CTextBuffer::NewL(0x100);
       
   430 		iFormatter = CTextFormatter::NewL(size.iWidth);
       
   431 		iStdout.SetCursorHeight(0);
       
   432 		iStdout.ClearScreen();
       
   433 		}
       
   434 	else
       
   435 		{
       
   436 		PrintWarning(_L("memsampler can't run unless it is attached directly to a console, aborting..."));
       
   437 		User::Leave(KErrArgument);
       
   438 		}
       
   439 
       
   440 	}
       
   441 
       
   442 void CMemoryView::Sort()
       
   443 	{
       
   444 	iChunkList.Sort(*iCurrentSortType);
       
   445 	}
       
   446 
       
   447 void CMemoryView::UpdateL()
       
   448 	{
       
   449 	iBuffer->Zero();
       
   450 	iFormatter->Zero();
       
   451 	const TInt numChunks = iChunkList.Count();
       
   452 	iBuffer->AppendL(_L("Chunk name\t   Current\t       Max\t      Peak\r\n"));
       
   453 	TInt numLines = 1;
       
   454 	for (TInt i = iVerticalOffset; i < numChunks; ++i)
       
   455 		{
       
   456 		++numLines;
       
   457 		const TChunkInfo& chunkInfo = iChunkList[i];
       
   458 		if (iHuman)
       
   459 			{
       
   460 			if (iHorizontalOffset >= chunkInfo.iName.Length())
       
   461 				{
       
   462 				// The horizontal offset is larger than this chunk name - reduce it to stop the chunk name column disappearing altogether.
       
   463 				// Note, ideally it would be nice to limit horizontal scrolling when the right most part of all the chunk names is visible.
       
   464 				// However, that would involve calculating all the column widths twice which seems a bit clumsy.
       
   465 				iHorizontalOffset = chunkInfo.iName.Length() - 1;
       
   466 				}
       
   467 			TPtrC name(chunkInfo.iName.Mid(iHorizontalOffset));
       
   468 			iBuffer->AppendFormatL(_L("%S\t"), &name);
       
   469 			iBuffer->AppendHumanReadableSizeL(chunkInfo.iCurrentSize, EColumnAlignedRight);
       
   470 			iBuffer->AppendL(_L("\t"));
       
   471 			iBuffer->AppendHumanReadableSizeL(chunkInfo.iMaxSize, EColumnAlignedRight);
       
   472 			iBuffer->AppendL(_L("\t"));
       
   473 			iBuffer->AppendHumanReadableSizeL(chunkInfo.iHighWaterMark, EColumnAlignedRight);
       
   474 			iBuffer->AppendL(_L("\r\n"));
       
   475 			}
       
   476 		else
       
   477 			{
       
   478 			iBuffer->AppendFormatL(_L("%S\t%d\t%d\t%d\r\n"), &chunkInfo.iName, chunkInfo.iCurrentSize, chunkInfo.iMaxSize, chunkInfo.iHighWaterMark);
       
   479 			}
       
   480 		if (numLines >= (iNumConsoleLines - 1))
       
   481 			{
       
   482 			break;
       
   483 			}
       
   484 		}
       
   485 	iFormatter->TabulateL(0, 1, iBuffer->Descriptor(), ETruncateLongestColumn);
       
   486 	User::LeaveIfError(iStdout.SetCursorPosAbs(TPoint(0, 0)));
       
   487 	iStdout.Write(iFormatter->Descriptor());
       
   488 	TInt numOldLines = iNumLinesInLastUpdate - numLines;
       
   489 	while (numOldLines > 0)
       
   490 		{
       
   491 		iStdout.ClearToEndOfLine();
       
   492 		iStdout.SetCursorPosRel(TPoint(0, 1));
       
   493 		--numOldLines;
       
   494 		}
       
   495 	iNumLinesInLastUpdate = numLines;
       
   496 	}
       
   497 
       
   498 void CMemoryView::PrintWarning(TRefByValue<const TDesC> aFmt, ...)
       
   499 	{
       
   500 	TOverflowTruncate overflow;
       
   501 	VA_LIST list;
       
   502 	VA_START(list, aFmt);
       
   503 	TBuf<0x100> buf(_L("Warning: "));
       
   504 	buf.AppendFormatList(aFmt, list, &overflow);
       
   505 	buf.AppendFormat(_L("\r\n"), &overflow);
       
   506 	iStderr.Write(buf);
       
   507 	}
       
   508 
       
   509 
       
   510 class CStdinReader : public CActive
       
   511 	{
       
   512 public:
       
   513 	static CStdinReader* NewL(RIoReadHandle& aStdin, CCommandBase& aCommand, CMemoryView* aMemoryView);
       
   514 	~CStdinReader();
       
   515 private: // From CActive.
       
   516 	virtual void RunL();
       
   517 	virtual void DoCancel();
       
   518 	virtual TInt RunError(TInt aError);
       
   519 private:
       
   520 	CStdinReader(RIoReadHandle& aStdin, CCommandBase& aCommand, CMemoryView* aMemoryView);
       
   521 	void QueueRead();
       
   522 private:
       
   523 	RIoConsoleReadHandle iStdin;
       
   524 	CCommandBase& iCommand;
       
   525 	CMemoryView* iMemoryView;
       
   526 	};
       
   527 
       
   528 CStdinReader* CStdinReader::NewL(RIoReadHandle& aStdin, CCommandBase& aCommand, CMemoryView* aMemoryView)
       
   529 	{
       
   530 	CStdinReader* self = new(ELeave) CStdinReader(aStdin, aCommand, aMemoryView);
       
   531 	self->QueueRead();
       
   532 	return self;
       
   533 	}
       
   534 
       
   535 CStdinReader::~CStdinReader()
       
   536 	{
       
   537 	Cancel();
       
   538 	}
       
   539 
       
   540 void CStdinReader::RunL()
       
   541 	{
       
   542 	if (iStatus.Int())
       
   543 		{
       
   544 		iCommand.Complete(iStatus.Int());
       
   545 		}
       
   546 	else
       
   547 		{
       
   548 		TBool noUpdate(EFalse);
       
   549 		CMemoryView::TSortType newSortType = CMemoryView::ESortUnspecified;
       
   550 		switch (iStdin.KeyCode())
       
   551 			{
       
   552 			case 'n':
       
   553 			case 'N':
       
   554 				{
       
   555 				newSortType = CMemoryView::ESortByChunkName;
       
   556 				break;
       
   557 				}
       
   558 			case 'c':
       
   559 			case 'C':
       
   560 				{
       
   561 				newSortType = CMemoryView::ESortByCurrentSize;
       
   562 				break;
       
   563 				}
       
   564 			case 'p':
       
   565 			case 'P':
       
   566 				{
       
   567 				newSortType = CMemoryView::ESortByHighWaterMark;
       
   568 				break;
       
   569 				}
       
   570 			case 'm':
       
   571 			case 'M':
       
   572 				{
       
   573 				newSortType = CMemoryView::ESortByMaxSize;
       
   574 				break;
       
   575 				}
       
   576 			case 'u':
       
   577 			case 'U':
       
   578 				{
       
   579 				newSortType = CMemoryView::ESortByUpdateHistory;
       
   580 				break;
       
   581 				}
       
   582 			case 'q':
       
   583 			case 'Q':
       
   584 				{
       
   585 				iCommand.Complete(KErrNone);
       
   586 				break;
       
   587 				}
       
   588 			case EKeyUpArrow:
       
   589 				{
       
   590 				if (iMemoryView)
       
   591 					{
       
   592 					iMemoryView->ScrollUp();
       
   593 					}
       
   594 				break;
       
   595 				}
       
   596 			case EKeyDownArrow:
       
   597 				{
       
   598 				if (iMemoryView)
       
   599 					{
       
   600 					iMemoryView->ScrollDown();
       
   601 					}
       
   602 				break;
       
   603 				}
       
   604 			case EKeyLeftArrow:
       
   605 				{
       
   606 				if (iMemoryView)
       
   607 					{
       
   608 					iMemoryView->ScrollLeft();
       
   609 					}
       
   610 				break;
       
   611 				}
       
   612 			case EKeyRightArrow:
       
   613 				{
       
   614 				if (iMemoryView)
       
   615 					{
       
   616 					iMemoryView->ScrollRight();
       
   617 					}
       
   618 				break;
       
   619 				}
       
   620 			default:
       
   621 				{
       
   622 				noUpdate = ETrue;
       
   623 				}
       
   624 			}
       
   625 
       
   626 		if ((newSortType != CMemoryView::ESortUnspecified) && iMemoryView)
       
   627 			{
       
   628 			iMemoryView->ChangeSortType(newSortType);
       
   629 			}
       
   630 
       
   631 		if (iMemoryView && !noUpdate)
       
   632 			{
       
   633 			iMemoryView->UpdateL();
       
   634 			}
       
   635 
       
   636 		QueueRead();
       
   637 		}
       
   638 	}
       
   639 
       
   640 void CStdinReader::DoCancel()
       
   641 	{
       
   642 	iStdin.WaitForKeyCancel();
       
   643 	}
       
   644 
       
   645 TInt CStdinReader::RunError(TInt aError)
       
   646 	{
       
   647 	iCommand.Complete(aError);
       
   648 	return KErrNone;
       
   649 	}
       
   650 
       
   651 CStdinReader::CStdinReader(RIoReadHandle& aStdin, CCommandBase& aCommand, CMemoryView* aMemoryView)
       
   652 	: CActive(CActive::EPriorityStandard), iCommand(aCommand), iMemoryView(aMemoryView)
       
   653 	{
       
   654 	iStdin = aStdin;
       
   655 	CActiveScheduler::Add(this);
       
   656 	}
       
   657 
       
   658 void CStdinReader::QueueRead()
       
   659 	{
       
   660 	iStdin.WaitForKey(iStatus);
       
   661 	SetActive();
       
   662 	}
       
   663 
       
   664 
       
   665 
       
   666 class TBtraceHeader
       
   667 	{
       
   668 public:
       
   669 	TUint8 iSize;
       
   670 	TUint8 iFlags;
       
   671 	TUint8 iCategory;
       
   672 	TUint8 iSubCategory;
       
   673 	};
       
   674 
       
   675 class CBtraceReader : public CActive
       
   676 	{
       
   677 public:
       
   678 	enum TMode
       
   679 		{
       
   680 		EConfigBtrace = 0x0001,
       
   681 		EDebug        = 0x0002
       
   682 		};
       
   683 public:
       
   684 	static CBtraceReader* NewL(TUint aMode, const TDesC& aFileName, CCommandBase& aCommand, CMemoryView* aMemoryView);
       
   685 	~CBtraceReader();
       
   686 private: // From CActive.
       
   687 	virtual void RunL();
       
   688 	virtual void DoCancel();
       
   689 	virtual TInt RunError(TInt aError);
       
   690 private:
       
   691 	CBtraceReader(const TDesC& aFileName, CCommandBase& aCommand, CMemoryView* aMemoryView);
       
   692 	void ConstructL(TUint aMode);
       
   693 	void QueueRead();
       
   694 	void DecodeFrame(const TBtraceHeader& aHeader, const TDesC8& aFrame, TUint32 aTickCount);
       
   695 	void Printf(TRefByValue<const TDesC> aFmt, ...);
       
   696 private:
       
   697 	const TDesC& iFileName;
       
   698 	CCommandBase& iCommand;
       
   699 	CMemoryView* iMemoryView;
       
   700 	RBTrace iBtrace;
       
   701 	CConsoleBase* iDebugConsole;
       
   702 	RFile iFile;
       
   703 	};
       
   704 
       
   705 CBtraceReader* CBtraceReader::NewL(TUint aMode, const TDesC& aFileName, CCommandBase& aCommand, CMemoryView* aMemoryView)
       
   706 	{
       
   707 	CBtraceReader* self = new(ELeave) CBtraceReader(aFileName, aCommand, aMemoryView);
       
   708 	CleanupStack::PushL(self);
       
   709 	self->ConstructL(aMode);
       
   710 	CleanupStack::Pop(self);
       
   711 	return self;
       
   712 	}
       
   713 
       
   714 CBtraceReader::~CBtraceReader()
       
   715 	{
       
   716 	Cancel();
       
   717 	iBtrace.Close();
       
   718 	iFile.Close();
       
   719 	delete iDebugConsole;
       
   720 	}
       
   721 
       
   722 void CBtraceReader::RunL()
       
   723 	{
       
   724 	QueueRead();
       
   725 	TUint8* data;
       
   726 	TInt size;
       
   727 	while ((size = iBtrace.GetData(data)) != 0)
       
   728 		{
       
   729 		if (iMemoryView || iDebugConsole)
       
   730 			{
       
   731 			// Only decode btrace frames if we're attached to a CMemoryView object or have a debug console.
       
   732 			TUint8* c = data;
       
   733 			TUint8* end = c + size;
       
   734 			do
       
   735 				{
       
   736 				TBtraceHeader* header = (TBtraceHeader*)c;
       
   737 				TUint8* d = c + sizeof(TBtraceHeader);
       
   738 				TUint32 tickCount = 0;
       
   739 				if (header->iFlags & BTrace::EMissingRecord)
       
   740 					{
       
   741 					User::Leave(KErrOverflow);
       
   742 					}
       
   743 				if (header->iFlags & BTrace::EHeader2Present)
       
   744 					{
       
   745 					d += 4;
       
   746 					}
       
   747 				if (header->iFlags & BTrace::ETimestampPresent)
       
   748 					{
       
   749 					tickCount = *((TUint32*)d);
       
   750 					d += 4;
       
   751 					}
       
   752 				if (header->iFlags & BTrace::ETimestamp2Present)
       
   753 					{
       
   754 					d += 4;
       
   755 					}
       
   756 				if (header->iFlags & BTrace::EContextIdPresent)
       
   757 					{
       
   758 					d += 4;
       
   759 					}
       
   760 				if (header->iFlags & BTrace::EPcPresent)
       
   761 					{
       
   762 					d += 4;
       
   763 					}
       
   764 				if (header->iFlags & BTrace::EExtraPresent)
       
   765 					{
       
   766 					d += 4;
       
   767 					}
       
   768 				TPtrC8 ptr(d, (c + header->iSize) - d);
       
   769 				DecodeFrame(*header, ptr, tickCount);
       
   770 				c += (header->iSize + 3) & ~3;
       
   771 				}
       
   772 				while (c < end);
       
   773 			}
       
   774 		if (iFileName.Length())
       
   775 			{
       
   776 			User::LeaveIfError(iFile.Write(TPtrC8(data, size)));
       
   777 			}
       
   778 		iBtrace.DataUsed();
       
   779 		}
       
   780 	if (iMemoryView)
       
   781 		{
       
   782 		iMemoryView->UpdateL();
       
   783 		}
       
   784 	}
       
   785 
       
   786 void CBtraceReader::DoCancel()
       
   787 	{
       
   788 	iBtrace.CancelRequestData();
       
   789 	}
       
   790 
       
   791 TInt CBtraceReader::RunError(TInt aError)
       
   792 	{
       
   793 	if (aError == KErrOverflow)
       
   794 		{
       
   795 		Printf(_L("Warning: BTrace buffer overflowed, aborting..."), aError);
       
   796 		iCommand.Complete(aError);
       
   797 		}
       
   798 	else if (aError)
       
   799 		{
       
   800 		Printf(_L("Warning: Could not update view (%d), aborting..."), aError);
       
   801 		iCommand.Complete(aError);
       
   802 		}
       
   803 	return KErrNone;
       
   804 	}
       
   805 
       
   806 CBtraceReader::CBtraceReader(const TDesC& aFileName, CCommandBase& aCommand, CMemoryView* aMemoryView)
       
   807 	: CActive(CActive::EPriorityStandard), iFileName(aFileName), iCommand(aCommand), iMemoryView(aMemoryView)
       
   808 	{
       
   809 	CActiveScheduler::Add(this);
       
   810 	}
       
   811 
       
   812 void CBtraceReader::ConstructL(TUint aMode)
       
   813 	{
       
   814 	if (aMode & EDebug)
       
   815 		{
       
   816 		iDebugConsole = Console::NewL(_L("debug"), TSize(KConsFullScreen,KConsFullScreen));
       
   817 		}
       
   818 	User::LeaveIfError(iBtrace.Open());
       
   819 	if (aMode & EConfigBtrace)
       
   820 		{
       
   821 		User::LeaveIfError(iBtrace.ResizeBuffer(KBtraceBufferSize));
       
   822 		// Turn everything off.
       
   823 		for (TInt i = 0; i < KMaxCategories; ++i)
       
   824 			{
       
   825 			iBtrace.SetFilter(i, 0);
       
   826 			}
       
   827 		if (aMode & EDebug)
       
   828 			{
       
   829 			iBtrace.SetFilter(BTrace::EKernPrintf, 1);
       
   830 			}
       
   831 		iBtrace.SetFilter(RMemSampler::EBtraceCategory, 1);
       
   832 		User::LeaveIfError(iBtrace.SetFilter2((const TUint32*)NULL, 0));
       
   833 		iBtrace.SetMode(RBTrace::EEnable | RBTrace::EFreeRunning);
       
   834 		}
       
   835 	if (iFileName.Length())
       
   836 		{
       
   837 		User::LeaveIfError(iFile.Replace(iCommand.FsL(), iFileName, EFileWrite));
       
   838 		}
       
   839 	QueueRead();
       
   840 	}
       
   841 
       
   842 void CBtraceReader::QueueRead()
       
   843 	{
       
   844 	iBtrace.RequestData(iStatus, 0);
       
   845 	SetActive();
       
   846 	}
       
   847 
       
   848 void CBtraceReader::DecodeFrame(const TBtraceHeader& aHeader, const TDesC8& aFrame, TUint32)
       
   849 	{
       
   850 	if (aHeader.iCategory == BTrace::EKernPrintf)
       
   851 		{
       
   852 		TUint32 threadId = *(TUint32*)aFrame.Ptr();
       
   853 		TBuf<256> text;
       
   854 		text.Copy(aFrame.Mid(4));
       
   855 		if (iDebugConsole)
       
   856 			{
       
   857 			iDebugConsole->Printf(_L("Kern::Printf (0x%08x) \'%S\'\r\n"), &threadId, &text);
       
   858 			}
       
   859 		}
       
   860 	else if (aHeader.iCategory == RMemSampler::EBtraceCategory)
       
   861 		{
       
   862 		switch (aHeader.iSubCategory)
       
   863 			{
       
   864 			case RMemSampler::ENewChunk:
       
   865 				{
       
   866 				TUint32 address = *(TUint32*)aFrame.Ptr();
       
   867 				TInt maxSize = *((TUint32*)aFrame.Ptr() + 1);
       
   868 				TFullName fullName;
       
   869 				fullName.Copy(aFrame.Mid(8));
       
   870 				if (iDebugConsole)
       
   871 					{
       
   872 					iDebugConsole->Printf(_L("New chunk - %S\r\n\taddress: 0x%08x max size: %d\r\n"), &fullName, address, maxSize);
       
   873 					}
       
   874 				if (iMemoryView)
       
   875 					{
       
   876 					iMemoryView->HandleNewChunk(fullName, address, maxSize);
       
   877 					}
       
   878 				break;
       
   879 				}
       
   880 			case RMemSampler::EChangedChunk:
       
   881 				{
       
   882 				TUint32 address = *(TUint32*)aFrame.Ptr();
       
   883 				TUint32 size = *((TUint32*)aFrame.Ptr() + 1);
       
   884 				TUint32 highWaterMark = *((TUint32*)aFrame.Ptr() + 2);
       
   885 				if (iDebugConsole)
       
   886 					{
       
   887 					iDebugConsole->Printf(_L("Changed chunk - address: 0x%08x size: %d hwm: %d\r\n"), address, size, highWaterMark);
       
   888 					}
       
   889 				if (iMemoryView)
       
   890 					{
       
   891 					iMemoryView->HandleChangedChunk(address, size, highWaterMark);
       
   892 					}
       
   893 				break;
       
   894 				}
       
   895 			case RMemSampler::EDeletedChunk:
       
   896 				{
       
   897 				TUint32 address = *(TUint32*)aFrame.Ptr();
       
   898 				if (iDebugConsole)
       
   899 					{
       
   900 					iDebugConsole->Printf(_L("Deleted chunk - address: 0x%08x\r\n"), address);
       
   901 					}
       
   902 				if (iMemoryView)
       
   903 					{
       
   904 					iMemoryView->HandleDeletedChunk(address);
       
   905 					}
       
   906 				break;
       
   907 				}
       
   908 			}
       
   909 		}
       
   910 	}
       
   911 
       
   912 void CBtraceReader::Printf(TRefByValue<const TDesC> aFmt, ...)
       
   913 	{
       
   914 	TOverflowTruncate overflow;
       
   915 	VA_LIST list;
       
   916 	VA_START(list, aFmt);
       
   917 	TBuf<0x100> buf;
       
   918 	buf.AppendFormatList(aFmt, list, &overflow);
       
   919 	iCommand.Stdout().Write(buf);
       
   920 	}
       
   921 
       
   922 
       
   923 class CCmdMemsampler : public CCommandBase
       
   924 	{
       
   925 public:
       
   926 	static CCommandBase* NewLC();
       
   927 	~CCmdMemsampler();
       
   928 private:
       
   929 	CCmdMemsampler();
       
   930 private: // From CCommandBase.
       
   931 	virtual const TDesC& Name() const;
       
   932 	virtual void DoRunL();
       
   933 	virtual void OptionsL(RCommandOptionList& aOptions);
       
   934 private:
       
   935 	RMemSampler iMemSampler;
       
   936 	CMemoryView* iMemoryView;
       
   937 	CStdinReader* iStdinReader;
       
   938 	CBtraceReader* iBtraceReader;
       
   939 	TUint iRate;
       
   940 	TBool iNoBtraceConfig;
       
   941 	TBool iDebug;
       
   942 	TBool iHuman;
       
   943 	TFileName2 iFileName;
       
   944 	TBool iNoLiveView;
       
   945 	};
       
   946 
       
   947 
       
   948 CCommandBase* CCmdMemsampler::NewLC()
       
   949 	{
       
   950 	CCmdMemsampler* self = new(ELeave) CCmdMemsampler();
       
   951 	CleanupStack::PushL(self);
       
   952 	self->BaseConstructL();
       
   953 	return self;
       
   954 	}
       
   955 
       
   956 CCmdMemsampler::~CCmdMemsampler()
       
   957 	{
       
   958 	iMemSampler.Close();
       
   959 	User::FreeLogicalDevice(KLdd);
       
   960 	delete iStdinReader;
       
   961 	delete iBtraceReader;
       
   962 	delete iMemoryView;
       
   963 	}
       
   964 
       
   965 CCmdMemsampler::CCmdMemsampler() : CCommandBase(EManualComplete), iRate(1000)
       
   966 	{
       
   967 	}
       
   968 
       
   969 const TDesC& CCmdMemsampler::Name() const
       
   970 	{
       
   971 	_LIT(KName, "memsampler");	
       
   972 	return KName;
       
   973 	}
       
   974 
       
   975 void CCmdMemsampler::DoRunL()
       
   976 	{
       
   977 	TUint mode = 0;
       
   978 	if (!iNoBtraceConfig)
       
   979 		{
       
   980 		mode |= CBtraceReader::EConfigBtrace;
       
   981 		}
       
   982 	if (iDebug)
       
   983 		{
       
   984 		mode |= CBtraceReader::EDebug;
       
   985 		}
       
   986 
       
   987 	if (!iNoLiveView)
       
   988 		{
       
   989 		iMemoryView = CMemoryView::NewL(Stdout(), Stderr(), iHuman);
       
   990 		}
       
   991 	iStdinReader = CStdinReader::NewL(Stdin(), *this, iMemoryView);
       
   992 	iBtraceReader = CBtraceReader::NewL(mode, iFileName, *this, iMemoryView);
       
   993 	User::LeaveIfError(User::LoadLogicalDevice(KLdd));
       
   994 	User::LeaveIfError(iMemSampler.Open());
       
   995 	iMemSampler.Start(iRate);
       
   996 	}
       
   997 
       
   998 void CCmdMemsampler::OptionsL(RCommandOptionList& aOptions)
       
   999 	{
       
  1000 	_LIT(KOptSampleRate, "rate");
       
  1001 	aOptions.AppendUintL(iRate, KOptSampleRate);
       
  1002 
       
  1003 	_LIT(KOptNoBtraceConfig, "no-btrace-config");
       
  1004 	aOptions.AppendBoolL(iNoBtraceConfig, KOptNoBtraceConfig);
       
  1005 
       
  1006 	_LIT(KOptDebug, "debug");
       
  1007 	aOptions.AppendBoolL(iDebug, KOptDebug);
       
  1008 
       
  1009 	_LIT(KOptHuman, "human");
       
  1010 	aOptions.AppendBoolL(iHuman, KOptHuman);
       
  1011 
       
  1012 	_LIT(KOptFile, "file");
       
  1013 	aOptions.AppendFileNameL(iFileName, KOptFile);
       
  1014 
       
  1015 	_LIT(KOptNoLiveView, "no-live-view");
       
  1016 	aOptions.AppendBoolL(iNoLiveView, KOptNoLiveView);
       
  1017 	}
       
  1018 
       
  1019 
       
  1020 EXE_BOILER_PLATE(CCmdMemsampler)
       
  1021