Skip to content
Snippets Groups Projects
Commit e57da93d authored by Ilka Antcheva's avatar Ilka Antcheva
Browse files

From bertrand:

Better version of CPUMeter.C


git-svn-id: http://root.cern.ch/svn/root/trunk@20800 27541ba8-7e3a-0410-8455-c3a389f83636
parent 56d4d9c9
Branches
Tags
No related merge requests found
......@@ -11,8 +11,8 @@ class TGShapedMain : public TGMainFrame {
protected:
TGSpeedo *fSpeedo; // analog meter
TTimer *fTimer; // update timer
Int_t fActInfo; // actual information value
Bool_t fRunning; // kTRUE while updating infos
public:
TGShapedMain(const TGWindow *p, int w, int h);
......@@ -21,11 +21,16 @@ public:
void CloseWindow();
TGSpeedo *GetSpeedo() const { return fSpeedo; }
Int_t GetActInfo() const { return fActInfo; }
Bool_t IsRunning() const { return fRunning; }
void ToggleInfos();
};
// 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)
......@@ -33,23 +38,26 @@ TGShapedMain::TGShapedMain(const TGWindow *p, int w, int h) :
// Constructor.
fActInfo = 1;
fRunning = kTRUE;
fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
TGMainFrame::Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX, 0, 0, 0, 0));
fSpeedo->SetDisplayText("Used RAM", "[MB]");
fTimer = new TTimer(100);
fTimer->SetCommand("Update()");
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();
}
//______________________________________________________________________________
......@@ -81,10 +89,43 @@ void TGShapedMain::CloseWindow()
{
// Close Window.
// stop updating
fRunning = kFALSE;
// reset kDontCallClose bit to be able to really close the window
ResetBit(kDontCallClose);
if (fTimer)
fTimer->TurnOff();
delete fTimer;
delete fSpeedo;
delete this;
}
void Update()
{
MemInfo_t memInfo;
CpuInfo_t cpuInfo;
Float_t act_load = 0.0;
Int_t i, memUsage = 0;
// Get CPU informations
gSystem->GetCpuInfo(&cpuInfo, 100);
// actual CPU load
act_load = cpuInfo.fTotal;
// Get Memory informations
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;
}
}
//______________________________________________________________________________
......@@ -92,55 +133,18 @@ void CPUMeter()
{
// Main application.
MemInfo_t memInfo;
CpuInfo_t cpuInfo;
Float_t act_load, prev_load = 0.0;
Int_t i, memUsage, old_memUsage = 0;
TGShapedMain *mainWindow = new TGShapedMain(0, 500, 200);
TGSpeedo *speedo = mainWindow->GetSpeedo();
gMainWindow = new TGShapedMain(0, 500, 200);
gSpeedo = gMainWindow->GetSpeedo();
// set threshold values
speedo->SetThresholds(12.5, 50.0, 87.5);
gSpeedo->SetThresholds(12.5, 50.0, 87.5);
// set threshold colors
speedo->SetThresholdColors(TGSpeedo::kGreen, TGSpeedo::kOrange, TGSpeedo::kRed);
gSpeedo->SetThresholdColors(TGSpeedo::kGreen, TGSpeedo::kOrange, TGSpeedo::kRed);
// enable threshold
speedo->EnableThreshold();
speedo->SetScaleValue(0.0, 5);
gSpeedo->EnableThreshold();
gSpeedo->SetScaleValue(0.0, 5);
// enable peak marker
speedo->EnablePeakMark();
// update the TGSpeedo widget
gSystem->GetCpuInfo(&cpuInfo);
gSystem->GetMemInfo(&memInfo);
gSystem->ProcessEvents();
while (mainWindow->IsRunning() && mainWindow->IsMapped()) {
// Get CPU informations
gSystem->GetCpuInfo(&cpuInfo, 100);
// actual CPU load
act_load = cpuInfo.fTotal;
// Get Memory informations
gSystem->GetMemInfo(&memInfo);
// choose which value to display
if (mainWindow->GetActInfo() == 0)
memUsage = memInfo.fMemTotal;
else if (mainWindow->GetActInfo() == 1)
memUsage = memInfo.fMemUsed;
else if (mainWindow->GetActInfo() == 2)
memUsage = memInfo.fMemFree;
// small threshold to avoid "trembling" needle
if (fabs(act_load-prev_load) > 0.9) {
speedo->SetScaleValue(act_load, 10);
prev_load = act_load;
}
// update only if value has changed
if (memUsage != old_memUsage) {
speedo->SetOdoValue(memUsage);
old_memUsage = memUsage;
}
// sleep a bit
gSystem->ProcessEvents();
gSystem->Sleep(250);
}
mainWindow->CloseWindow();
gSpeedo->EnablePeakMark();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment