Logo ROOT  
Reference Guide
CPUMeter.C File Reference

Detailed Description

Simple macro showing capabilities of the TGSpeedo widget.

#include "TSystem.h"
#include "TGFrame.h"
#include "TGWindow.h"
#include "TGSpeedo.h"
class TGShapedMain : public TGMainFrame {
protected:
const TGPicture *fBgnd; // picture used as mask
TGSpeedo *fSpeedo; // analog meter
TTimer *fTimer; // update timer
Int_t fActInfo; // actual information value
public:
TGShapedMain(const TGWindow *p, int w, int h);
virtual ~TGShapedMain();
void CloseWindow();
TGSpeedo *GetSpeedo() const { return fSpeedo; }
Int_t GetActInfo() const { return fActInfo; }
void ToggleInfos();
ClassDef(TGShapedMain, 0)
};
// globals
TGShapedMain *gMainWindow;
TGSpeedo *gSpeedo;
Float_t prev_load;
Int_t old_memUsage;
//______________________________________________________________________________
TGShapedMain::TGShapedMain(const TGWindow *p, int w, int h) :
TGMainFrame(p, w, h)
{
// Constructor.
fActInfo = 1;
fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX));
fSpeedo->SetDisplayText("Used RAM", "[MB]");
fTimer = new TTimer(100);
fTimer->SetCommand("Update()");
fBgnd = fSpeedo->GetPicture();
gVirtualX->ShapeCombineMask(GetId(), 0, 0, fBgnd->GetMask());
SetBackgroundPixmap(fBgnd->GetPicture());
SetWMSizeHints(fBgnd->GetWidth(), fBgnd->GetHeight(), fBgnd->GetWidth(),
fBgnd->GetHeight(), 1, 1);
MapSubwindows();
MapWindow();
// To avoid closing the window while TGSpeedo is drawing
DontCallClose();
// To avoid closing the window while TGSpeedo is drawing
Resize(GetDefaultSize());
// Set fixed size
SetWMSizeHints(GetDefaultWidth(), GetDefaultHeight(), GetDefaultWidth(),
GetDefaultHeight(), 1, 1);
SetWindowName("ROOT CPU Load Meter");
fTimer->TurnOn();
}
//______________________________________________________________________________
void TGShapedMain::ToggleInfos()
{
// Toggle information displayed in Analog Meter
if (fActInfo < 2)
fActInfo++;
else
fActInfo = 0;
if (fActInfo == 0)
fSpeedo->SetDisplayText("Total RAM", "[MB]");
else if (fActInfo == 1)
fSpeedo->SetDisplayText("Used RAM", "[MB]");
else if (fActInfo == 2)
fSpeedo->SetDisplayText("Free RAM", "[MB]");
}
//______________________________________________________________________________
TGShapedMain::~TGShapedMain()
{
// Destructor.
delete fTimer;
delete fSpeedo;
}
//______________________________________________________________________________
void TGShapedMain::CloseWindow()
{
// Close Window.
if (fTimer)
fTimer->TurnOff();
DestroyWindow();
}
void Update()
{
MemInfo_t memInfo;
CpuInfo_t cpuInfo;
Float_t act_load = 0.0;
Int_t memUsage = 0;
prev_load = act_load;
old_memUsage = memUsage;
// Get CPU information
gSystem->GetCpuInfo(&cpuInfo, 100);
// actual CPU load
act_load = cpuInfo.fTotal;
// Get Memory information
gSystem->GetMemInfo(&memInfo);
// choose which value to display
if (gMainWindow->GetActInfo() == 0)
memUsage = memInfo.fMemTotal;
else if (gMainWindow->GetActInfo() == 1)
memUsage = memInfo.fMemUsed;
else if (gMainWindow->GetActInfo() == 2)
memUsage = memInfo.fMemFree;
// small threshold to avoid "trembling" needle
if (fabs(act_load-prev_load) > 0.9) {
gSpeedo->SetScaleValue(act_load, 10);
prev_load = act_load;
}
// update only if value has changed
if (memUsage != old_memUsage) {
gSpeedo->SetOdoValue(memUsage);
old_memUsage = memUsage;
}
}
//______________________________________________________________________________
void CPUMeter()
{
// Main application.
gMainWindow = new TGShapedMain(gClient->GetRoot(), 500, 200);
gSpeedo = gMainWindow->GetSpeedo();
// set threshold values
gSpeedo->SetThresholds(12.5, 50.0, 87.5);
// set threshold colors
// enable threshold
gSpeedo->EnableThreshold();
gSpeedo->SetScaleValue(0.0, 5);
// enable peak marker
gSpeedo->EnablePeakMark();
}
Author
Bertrand Bellenot

Definition in file CPUMeter.C.

TGSpeedo::EnablePeakMark
void EnablePeakMark()
Definition: TGSpeedo.h:108
TGSpeedo::SetOdoValue
void SetOdoValue(Int_t val)
Set actual value of odo meter.
Definition: TGSpeedo.cxx:306
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:38
TGMainFrame
Definition: TGFrame.h:444
TGPicture
Definition: TGPicture.h:36
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
MemInfo_t::fMemFree
Int_t fMemFree
Definition: TSystem.h:182
Float_t
float Float_t
Definition: RtypesCore.h:57
Int_t
int Int_t
Definition: RtypesCore.h:45
TGWindow.h
gClient
#define gClient
Definition: TGClient.h:166
TGSpeedo::kGreen
@ kGreen
Definition: TGSpeedo.h:53
TTimer
Definition: TTimer.h:51
ROOT::Math::fabs
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Definition: UnaryOperators.h:131
TGSpeedo::kOrange
@ kOrange
Definition: TGSpeedo.h:53
TSystem.h
TGSpeedo
Definition: TGSpeedo.h:38
h
#define h(i)
Definition: RSha256.hxx:124
TSystem::GetCpuInfo
virtual int GetCpuInfo(CpuInfo_t *info, Int_t sampleTime=1000) const
Returns cpu load average and load info into the CpuInfo_t structure.
Definition: TSystem.cxx:2507
TGSpeedo::kRed
@ kRed
Definition: TGSpeedo.h:53
MemInfo_t
Definition: TSystem.h:179
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
TGSpeedo.h
TGMainFrame::CloseWindow
virtual void CloseWindow()
Close and delete main frame.
Definition: TGFrame.cxx:1731
TGSpeedo::SetThresholds
void SetThresholds(Float_t th1=0.0, Float_t th2=0.0, Float_t th3=0.0)
Definition: TGSpeedo.h:102
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGSpeedo::SetScaleValue
void SetScaleValue(Float_t val)
Set actual scale (needle position) value.
Definition: TGSpeedo.cxx:365
TGSpeedo::SetThresholdColors
void SetThresholdColors(EGlowColor col1, EGlowColor col2, EGlowColor col3)
Definition: TGSpeedo.h:104
MemInfo_t::fMemTotal
Int_t fMemTotal
Definition: TSystem.h:180
CpuInfo_t
Definition: TSystem.h:165
MemInfo_t::fMemUsed
Int_t fMemUsed
Definition: TSystem.h:181
TSystem::GetMemInfo
virtual int GetMemInfo(MemInfo_t *info) const
Returns ram and swap memory usage info into the MemInfo_t structure.
Definition: TSystem.cxx:2517
TGSpeedo::EnableThreshold
void EnableThreshold()
Definition: TGSpeedo.h:106
CpuInfo_t::fTotal
Float_t fTotal
Definition: TSystem.h:171