os/kernelhwsrv/kerneltest/e32utils/analyse/activity.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "analyse.h"
    17 #include "activity.h"
    18 #include "output.h"
    19 
    20 #ifdef __MSVCDOTNET__
    21 #include <ostream>
    22 #include <iomanip>
    23 #else //!__MSVCDOTNET__
    24 #include <ostream.h>
    25 #include <iomanip.h>
    26 #endif //__MSVCDOTNET__
    27 
    28 #include <algorithm>
    29 
    30 
    31 // class Activity::Bucket
    32 
    33 inline Activity::Bucket::Bucket(unsigned aPeriod,int aThread)
    34 	:iPeriod(aPeriod),iThread(aThread)
    35 	{}
    36 
    37 bool Activity::Bucket::operator<(const Bucket& aRhs) const
    38 	{
    39 	if (iPeriod != aRhs.iPeriod)
    40 		return iPeriod < aRhs.iPeriod;
    41 	return iThread < aRhs.iThread;
    42 	}
    43 
    44 // class Activity::ThreadData
    45 
    46 inline Activity::ThreadData::ThreadData(const Thread& aThread)
    47 	:iThread(&aThread), iTotal(0)
    48 	{}
    49 
    50 inline Activity::ThreadData::ThreadData(unsigned aCutoff)
    51 	:iTotal(aCutoff)
    52 	{}
    53 
    54 inline bool Activity::ThreadData::operator<(const ThreadData& aRhs) const
    55 	{
    56 	return iTotal < aRhs.iTotal;
    57 	}
    58 
    59 
    60 // class Activity
    61 
    62 
    63 Activity::Activity(int aBucketSize, unsigned aBeginSample, double aCutOff)
    64 	:iBucketSize(aBucketSize), iBeginSample(aBeginSample), iCutOff(aCutOff)
    65 	{
    66 	cout << "Execution activity\n\n";
    67 	}
    68 
    69 void Activity::Sample(unsigned aNumber, const Thread& aThread, PC)
    70 	{
    71 	if (aThread.iIndex == iThreads.size())
    72 		iThreads.push_back(ThreadData(aThread));
    73 
    74 	++iThreads[aThread.iIndex].iTotal;
    75 	++iData[Bucket((aNumber - iBeginSample) / iBucketSize, aThread.iIndex)];
    76 	} 
    77 
    78 void Activity::Complete(unsigned aTotal, unsigned aActive)
    79 	{
    80 	cout.setf(ios::fixed, ios::floatfield);
    81 	cout << setfill(' ');
    82 	cout.precision(2);
    83 	const char* emptySeparator;
    84 	const char* separator;
    85 //
    86 	const unsigned ixCount = iThreads.size();
    87 	int* remap = new int[ixCount];
    88 	std::fill(remap, remap + ixCount, -1);
    89 
    90 	std::sort(iThreads.begin(), iThreads.end());
    91 	Threads::iterator cutoff = std::lower_bound(iThreads.begin(), iThreads.end(), ThreadData(iCutOff * aTotal * 0.01));
    92 	iThreads.erase(iThreads.begin(), cutoff);
    93 
    94 	const unsigned disCount = iThreads.size();
    95 	for (int ix = 0; ix < disCount; ++ix)
    96 		remap[iThreads[ix].iThread->iIndex] = ix;
    97 
    98 
    99 	if (Analyse::Format() != Analyse::EExcel)
   100 		{
   101 		cout << "ID  Thread name\n";
   102 		int ix;
   103 		for (ix = 0; ix < disCount; ++ix)
   104 			cout << char('A' + ix) << "  " << *iThreads[ix].iThread << '\n';
   105 
   106 		cout << "\nsample  ";
   107 		for (ix = 0; ix < disCount; ++ix)
   108 			cout << setw(4) << char('A' + ix) << "    ";
   109 		cout << "\n\n";
   110 		separator = " ";
   111 		emptySeparator = "        ";
   112 		}
   113 	else
   114 		{
   115 		cout << "sample";
   116 		for (int ix = 0; ix < disCount; ++ix)
   117 			cout << '\t' << *iThreads[ix].iThread;
   118 		cout << '\n';
   119 		separator = emptySeparator = "\t";
   120 		}
   121 
   122 	unsigned* totals = new unsigned[disCount];
   123 	std::fill(totals, totals + disCount, 0);
   124 	unsigned period = 0;
   125 	bool values = false;
   126 	for (Data::iterator p = iData.begin(), e = iData.end(); p != e; ++p)
   127 		{
   128 		while (period != p->first.iPeriod)
   129 			{
   130 			cout << setw(7) << (period * iBucketSize) + iBeginSample;
   131 			if (values)
   132 				{
   133 				for (int ix = 0; ix < disCount; ++ix)
   134 					cout <<	separator << Result(totals[ix], iBucketSize);
   135 				std::fill(totals, totals + disCount, 0);
   136 				values = false;
   137 				}
   138 			cout << '\n';
   139 			++period;
   140 			}
   141 		int ix = remap[p->first.iThread];
   142 		if (ix >= 0)
   143 			{
   144 			totals[ix] = p->second;
   145 			values = true;
   146 			}
   147 		}
   148 	if (values)
   149 		{
   150 		cout << setw(7) << (period * iBucketSize) + iBeginSample;
   151 		for (int ix = 0; ix < disCount; ++ix)
   152 			cout <<	separator << Result(totals[ix], iBucketSize);
   153 		cout << '\n';
   154 		}
   155 
   156 	delete [] remap;
   157 	delete [] totals;
   158 	}