×
Namespaces

Variants
Actions

Monitoring CPU usage using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: User:PushL (29 Jun 2007)
Last edited: hamishwillee (26 Jul 2012)

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.

This page was last modified on 26 July 2012, at 02:04.
57 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×