This page was last modified 08:33, 29 June 2007.
Monitoring CPU usage
From Forum Nokia Wiki
This simple control allows you to monitor CPU usage in your mobile. To use it, just instantiate the control and set two timers: one is used to periodically invoke the plot function, and the other one (low priority idle) to count the idle time. This way, you can get an approximation on how much cpu other threads are using.
void CSystemView::ConstructL()
{
iUpdateTimer = CPeriodic::NewL(CActive::EPriorityHigh);
iIdleTimer = CPeriodic::NewL(CActive::EPriorityIdle);
}
void CSystemView::CmdStartTimers()
{
// Reset counters
iIdleCounter = 0;
iCPUControl->Reset();
DrawDeferred();
const TInt KUpdateInterval = 1000000; // 1 sec
iUpdateTimer->Start(KUpdateInterval, KUpdateInterval, TCallBack(DoUpdateL, this));
const TInt KIdleInterval = 10000; // 0.01 sec
iIdleTimer->Start(KIdleInterval, KIdleInterval, TCallBack(DoIdleL, this));
}
TInt CSystemView::DoUpdateL(TAny* aPtr)
{
CSystemView* self = static_cast<CSystemView*>(aPtr);
TInt idle = self->iIdleCounter;
self->iIdleCounter = 0;
self->iCPUControl->PlotL(idle);
return 0;
}
TInt CSystemView::DoIdleL(TAny* aPtr)
{
CSystemView* self = static_cast<CSystemView*>(aPtr);
++self->iIdleCounter;
return 0;
}
#include <coecntrl.h> // CCoeControl
class CCPUControl : public CCoeControl
{
public:
void ConstructL(const TRect& aRect, const CCoeControl* aParent);
~CCPUControl();
void Reset();
void PlotL(TInt aIdleCounter);
private:
void Draw(const TRect& aRect) const;
void SizeChanged();
private:
CArrayFix<TReal32>* iCpuLoadList;
TInt iMaxIdleCounter;
TReal32 iCpuLoad;
TReal32 iTotalCpuLoad;
TUint32 iTotalTicks;
CArrayFix<TPoint>* iPointList;
TRect iPaintRect;
HBufC* iCPULoadText;
};
#include "CPUControl.h"
#include <YourApp.rsg>
#include <coemain.h> // CCoeEnv
#include <eikenv.h> // CEikonEnv
// Handle any possible FPU exception
void FpHandler(TExcType /*aType*/)
{}
void CCPUControl::ConstructL(const TRect& aRect, const CCoeControl* aParent)
{
if (aParent == 0)
CreateWindowL();
else
SetContainerWindowL(*aParent);
#if defined(__S603RD__)
User::SetExceptionHandler(FpHandler, KExceptionFpe);
#else
RThread().SetExceptionHandler(FpHandler, KExceptionFpe);
#endif
iCpuLoadList = new(ELeave) CArrayFixSeg<TReal32>(5);
iPointList = new(ELeave) CArrayFixFlat<TPoint>(5);
iCPULoadText = iCoeEnv->AllocReadResourceL(R_TEXT_CPU_LOAD);
SetRect(aRect);
ActivateL();
}
CCPUControl::~CCPUControl()
{
delete iCPULoadText;
delete iPointList;
delete iCpuLoadList;
}
void CCPUControl::Reset()
{
iCpuLoadList->Reset();
iCpuLoad = 0;
iTotalCpuLoad = 0;
iTotalTicks = 0;
iMaxIdleCounter = 0;
iPointList->Reset();
}
void CCPUControl::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(iPaintRect);
gc.SetClippingRect(iPaintRect);
gc.SetPenColor(KRgbRed);
gc.DrawPolyLine(iPointList);
gc.SetPenColor(KRgbBlack);
gc.DrawRect(iPaintRect);
const CFont* font = iEikonEnv->LegendFont();
gc.UseFont(font);
TBuf<64> cpuLoadText;
cpuLoadText.Format(*iCPULoadText, static_cast<TInt>(iCpuLoad * 100),
iTotalTicks? (iTotalCpuLoad / iTotalTicks) * 100 : 0);
gc.DrawText(cpuLoadText, iPaintRect, font->HeightInPixels(), CGraphicsContext::ERight);
gc.DiscardFont();
}
void CCPUControl::SizeChanged()
{
iPaintRect = Rect();
iPaintRect.Shrink(5, 5);
}
void CCPUControl::PlotL(TInt aIdleCounter)
{
if (aIdleCounter > iMaxIdleCounter)
iMaxIdleCounter = aIdleCounter;
iCpuLoad = static_cast<TReal32> ((0.8 * (iMaxIdleCounter - aIdleCounter))
/ iMaxIdleCounter + 0.2 * iCpuLoad);
// To calculate average load
iTotalCpuLoad += iCpuLoad;
++iTotalTicks;
if (iCpuLoadList->Count() == iPaintRect.Width() - 2)
iCpuLoadList->Delete(0);
iCpuLoadList->AppendL(iCpuLoad);
iPointList->Reset();
for (TInt i = 0; i < iCpuLoadList->Count(); ++i)
{
TInt x = iPaintRect.iBr.iX - 1 - iCpuLoadList->Count() + i;
TInt y = iPaintRect.iBr.iY - 2 -
static_cast<TInt> (iPaintRect.Height() * iCpuLoadList->At(i));
iPointList->AppendL(TPoint(x, y));
}
DrawDeferred();
}
You can check TaskSpy source code to see how this works.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory usage with UDPDatagramConnection | mganna | Mobile Java Networking & Messaging & Security | 3 | 2006-08-08 13:02 |
| development of cpu power on cell-phones | Kurt Johannes | General Discussion | 4 | 2006-09-26 17:12 |
| Some OpenGL issues on Nokia N93 | derBertl | Symbian Media (Graphics & Sounds) | 11 | 2006-09-26 07:10 |
| Create image memory usage | martin.wainamoinen | Mobile Java Media (Graphics & Sounds) | 15 | 2008-03-09 16:40 |
| possible loop in MMSPDUDecoder causing stuck thread! | carolang | General Messaging | 0 | 2006-12-15 12:48 |
