1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32utils/analyse/distribution.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,267 @@
1.4 +// Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "analyse.h"
1.20 +#include "distribution.h"
1.21 +#include "output.h"
1.22 +
1.23 +#ifdef __MSVCDOTNET__
1.24 +#include <ostream>
1.25 +#include <iomanip>
1.26 +#else //!__MSVCDOTNET__
1.27 +#include <ostream.h>
1.28 +#include <iomanip.h>
1.29 +#endif //__MSVCDOTNET__
1.30 +
1.31 +#include <algorithm>
1.32 +
1.33 +// class Distribution
1.34 +
1.35 +namespace {
1.36 +
1.37 +int* BucketBox(int aSize)
1.38 + {
1.39 + int* buckets = new int[aSize];
1.40 + memset(buckets, 0, sizeof(int) * aSize);
1.41 + return buckets;
1.42 + }
1.43 +
1.44 +class BucketTotals
1.45 + {
1.46 +private:
1.47 + class Generator
1.48 + {
1.49 + public:
1.50 + inline Generator()
1.51 + :iValue(0)
1.52 + {}
1.53 + inline int operator()()
1.54 + {return iValue++;}
1.55 + private:
1.56 + int iValue;
1.57 + };
1.58 + class Sorter
1.59 + {
1.60 + public:
1.61 + inline Sorter(const int* aTotals)
1.62 + :iTotals(aTotals)
1.63 + {}
1.64 + inline bool operator()(int aLhs, int aRhs) const
1.65 + {return (iTotals[aLhs] > iTotals[aRhs]);}
1.66 + private:
1.67 + const int* iTotals;
1.68 + };
1.69 +public:
1.70 + BucketTotals(Distribution::Data& aData, int aSize);
1.71 + ~BucketTotals();
1.72 + void Sort();
1.73 +//
1.74 + inline int Total() const;
1.75 + inline int Map(int aBucket) const;
1.76 + inline int operator[](int aBucket) const;
1.77 + inline Result Sample(int aSample) const;
1.78 +private:
1.79 + void Total(Distribution::Data& aData);
1.80 +private:
1.81 + int iSize;
1.82 + int* iTotals;
1.83 + int* iOrdering;
1.84 + int iTotal;
1.85 + };
1.86 +
1.87 +BucketTotals::BucketTotals(Distribution::Data& aData, int aSize)
1.88 + :iSize(aSize), iTotals(BucketBox(aSize)), iOrdering(BucketBox(aSize))
1.89 + {
1.90 + Total(aData);
1.91 + std::generate_n(iOrdering, iSize, Generator());
1.92 + }
1.93 +
1.94 +BucketTotals::~BucketTotals()
1.95 + {
1.96 + delete [] iTotals;
1.97 + delete [] iOrdering;
1.98 + }
1.99 +
1.100 +void BucketTotals::Total(Distribution::Data& aData)
1.101 + {
1.102 + int total = 0;
1.103 + for (Distribution::Data::iterator p = aData.begin(), e = aData.end(); p != e; ++p)
1.104 + {
1.105 + const int* box = p->iBuckets;
1.106 + int threadTotal = 0;
1.107 + for (int j = p->iBucketsLength; --j >=0;)
1.108 + {
1.109 + threadTotal += box[j];
1.110 + iTotals[j] += box[j];
1.111 + }
1.112 + p->iTotal = threadTotal;
1.113 + total += threadTotal;
1.114 + }
1.115 + iTotal = total;
1.116 + }
1.117 +
1.118 +void BucketTotals::Sort()
1.119 + {
1.120 + std::sort(iOrdering, iOrdering + iSize, Sorter(iTotals));
1.121 + }
1.122 +
1.123 +inline int BucketTotals::Total() const
1.124 + {return iTotal;}
1.125 +
1.126 +inline int BucketTotals::Map(int aBucket) const
1.127 + {return iOrdering[aBucket];}
1.128 +
1.129 +inline int BucketTotals::operator[](int aBucket) const
1.130 + {return iTotals[iOrdering[aBucket]];}
1.131 +
1.132 +inline Result BucketTotals::Sample(int aSample) const
1.133 + {return Result(aSample, iTotal);}
1.134 +
1.135 +};
1.136 +
1.137 +inline Distribution::ThreadData::ThreadData(const Thread& aThread, int aSize)
1.138 + :iThread(&aThread), iBuckets(BucketBox(aSize)), iTotal(0), iBucketsLength(aSize)
1.139 + {}
1.140 +
1.141 +inline Distribution::ThreadData::ThreadData(int aCutoff)
1.142 + :iTotal(aCutoff)
1.143 + {}
1.144 +
1.145 +inline bool Distribution::ThreadData::operator<(const Distribution::ThreadData& aRhs) const
1.146 + {return (iTotal > aRhs.iTotal);}
1.147 +
1.148 +Distribution::Distribution(CodeSpace& aCodeSpace, double aCutOff)
1.149 + :iCodeSpace(aCodeSpace), iCutOff(aCutOff)
1.150 + {
1.151 + cout << "Profile distribution\n\n";
1.152 + }
1.153 +
1.154 +void Distribution::Sample(unsigned , const Thread& aThread, PC aPc)
1.155 +//
1.156 +// Collect the sample in the bucket as allocated by the code space
1.157 +//
1.158 + {
1.159 + if (aThread.iIndex == iData.size())
1.160 + iData.push_back(ThreadData(aThread,iCodeSpace.Size()));
1.161 +
1.162 + ///
1.163 + int bucket = iCodeSpace.Bucket(aPc);
1.164 + if (bucket >= iData[aThread.iIndex].iBucketsLength)
1.165 + {
1.166 + int* new_buckets = new int[bucket+1];
1.167 + memset(new_buckets, 0, sizeof(int) * (bucket+1));
1.168 + memcpy(new_buckets, iData[aThread.iIndex].iBuckets, sizeof(int) * iData[aThread.iIndex].iBucketsLength);
1.169 + delete [] iData[aThread.iIndex].iBuckets;
1.170 + iData[aThread.iIndex].iBuckets = new_buckets;
1.171 + iData[aThread.iIndex].iBucketsLength = bucket+1;
1.172 + }
1.173 +
1.174 + ++iData[aThread.iIndex].iBuckets[bucket];
1.175 + }
1.176 +
1.177 +void Distribution::Complete(unsigned aTotal, unsigned aActive)
1.178 + {
1.179 + // accumulate thread and bucket totals
1.180 + const int nbuckets = iCodeSpace.Size();
1.181 + BucketTotals totals(iData, nbuckets);
1.182 + if (iCodeSpace.Ordering() == CodeSpace::ERandom)
1.183 + totals.Sort();
1.184 +
1.185 + int cutoff = int(iCutOff * totals.Total() * 0.01);
1.186 + std::sort(iData.begin(), iData.end());
1.187 + iData.erase(std::upper_bound(iData.begin(), iData.end(), ThreadData(cutoff)), iData.end());
1.188 +//
1.189 + cout.setf(ios::fixed, ios::floatfield);
1.190 + cout.precision(2);
1.191 +//
1.192 + cout << "Samples: " << aTotal << '\n';
1.193 + cout << " Active: " << aActive << " (" << double(aActive*100)/aTotal << "%)\n";
1.194 + cout << "Counted: " << totals.Total() << " (" << double(totals.Total()*100)/aTotal << "%)\n\n";
1.195 + cout << setfill(' ');
1.196 +//
1.197 + Data::iterator p, e;
1.198 + if (!Analyse::Option(Analyse::ETotalOnly))
1.199 + {
1.200 + if (Analyse::Format() != Analyse::EExcel)
1.201 + {
1.202 + cout << "ID Thread name\n";
1.203 + char id = 'A';
1.204 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.205 + cout << id++ << " " << *p->iThread << '\n';
1.206 + cout << '\n';
1.207 +
1.208 + id = 'A';
1.209 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.210 + cout << setw(4) << id++ << " ";
1.211 + cout << " total\n\n";
1.212 +
1.213 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.214 + cout << totals.Sample(p->iTotal) << " ";
1.215 + cout << " " << totals.Sample(totals.Total()) << " total\n\n";
1.216 + }
1.217 + else
1.218 + {
1.219 + cout.precision(5);
1.220 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.221 + cout << '\t' << *p->iThread;
1.222 + cout << "\ttotal\n";
1.223 + }
1.224 + }
1.225 +
1.226 + if (cutoff == 0 && iCodeSpace.Ordering() != CodeSpace::ELinear)
1.227 + cutoff = 1;
1.228 +
1.229 + for (int ix = 0; ix< nbuckets; ++ix)
1.230 + {
1.231 + int total = totals[ix];
1.232 + if (total >= cutoff)
1.233 + {
1.234 + int jx = totals.Map(ix);
1.235 + if (jx == CodeSpace::KOtherBucket && Analyse::Option(Analyse::ENoOther))
1.236 + continue;
1.237 + if (Analyse::Format() == Analyse::EExcel)
1.238 + {
1.239 + cout << iCodeSpace.Name(jx);
1.240 + if (!Analyse::Option(Analyse::ETotalOnly))
1.241 + {
1.242 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.243 + cout << '\t' << totals.Sample(jx>=p->iBucketsLength ? 0 : p->iBuckets[jx]);
1.244 + }
1.245 + cout << '\t' << totals.Sample(total) << '\n';
1.246 + }
1.247 + else
1.248 + {
1.249 + if (!Analyse::Option(Analyse::ETotalOnly))
1.250 + {
1.251 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.252 + cout << totals.Sample(jx>=p->iBucketsLength ? 0 : p->iBuckets[jx]) << " ";
1.253 + cout << " ";
1.254 + }
1.255 + cout << totals.Sample(total) << " " << iCodeSpace.Name(jx) << '\n';
1.256 + }
1.257 + }
1.258 + }
1.259 +
1.260 + if (!Analyse::Option(Analyse::ETotalOnly))
1.261 + {
1.262 + if (Analyse::Format() == Analyse::EExcel)
1.263 + {
1.264 + cout << "total";
1.265 + for (p = iData.begin(), e = iData.end(); p != e; ++p)
1.266 + cout << '\t' << totals.Sample(p->iTotal);
1.267 + cout << '\t' << totals.Sample(totals.Total()) << '\n';
1.268 + }
1.269 + }
1.270 + }