Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CPUMeter.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// Simple macro showing capabilities of the TGSpeedo widget.
4///
5/// \macro_code
6///
7/// \author Bertrand Bellenot
8
9#include "TSystem.h"
10#include "TGFrame.h"
11#include "TGWindow.h"
12#include "TGSpeedo.h"
13
14class TGShapedMain : public TGMainFrame {
15
16protected:
17 const TGPicture *fBgnd; // picture used as mask
18 TGSpeedo *fSpeedo; // analog meter
19 TTimer *fTimer; // update timer
20 Int_t fActInfo; // actual information value
21
22public:
23 TGShapedMain(const TGWindow *p, int w, int h);
24 virtual ~TGShapedMain();
25
26 void CloseWindow();
27 TGSpeedo *GetSpeedo() const { return fSpeedo; }
28 Int_t GetActInfo() const { return fActInfo; }
29 void ToggleInfos();
30
31 ClassDef(TGShapedMain, 0)
32};
33
34
35// globals
36TGShapedMain *gMainWindow;
37TGSpeedo *gSpeedo;
38Float_t prev_load;
39Int_t old_memUsage;
40
41//______________________________________________________________________________
42TGShapedMain::TGShapedMain(const TGWindow *p, int w, int h) :
43 TGMainFrame(p, w, h)
44{
45 // Constructor.
46
47 fActInfo = 1;
48
49 fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
50 fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
51 fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
52 Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
53 AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX));
54 fSpeedo->SetDisplayText("Used RAM", "[MB]");
55 fTimer = new TTimer(100);
56 fTimer->SetCommand("Update()");
57
58 fBgnd = fSpeedo->GetPicture();
59 gVirtualX->ShapeCombineMask(GetId(), 0, 0, fBgnd->GetMask());
60 SetBackgroundPixmap(fBgnd->GetPicture());
61 SetWMSizeHints(fBgnd->GetWidth(), fBgnd->GetHeight(), fBgnd->GetWidth(),
62 fBgnd->GetHeight(), 1, 1);
63
64 MapSubwindows();
65 MapWindow();
66 // To avoid closing the window while TGSpeedo is drawing
67 DontCallClose();
68 // To avoid closing the window while TGSpeedo is drawing
69 Resize(GetDefaultSize());
70 // Set fixed size
71 SetWMSizeHints(GetDefaultWidth(), GetDefaultHeight(), GetDefaultWidth(),
72 GetDefaultHeight(), 1, 1);
73 SetWindowName("ROOT CPU Load Meter");
74 fTimer->TurnOn();
75}
76
77//______________________________________________________________________________
78void TGShapedMain::ToggleInfos()
79{
80 // Toggle information displayed in Analog Meter
81
82 if (fActInfo < 2)
83 fActInfo++;
84 else
85 fActInfo = 0;
86 if (fActInfo == 0)
87 fSpeedo->SetDisplayText("Total RAM", "[MB]");
88 else if (fActInfo == 1)
89 fSpeedo->SetDisplayText("Used RAM", "[MB]");
90 else if (fActInfo == 2)
91 fSpeedo->SetDisplayText("Free RAM", "[MB]");
92}
93
94//______________________________________________________________________________
95TGShapedMain::~TGShapedMain()
96{
97 // Destructor.
98
99 delete fTimer;
100 delete fSpeedo;
101}
102
103//______________________________________________________________________________
104void TGShapedMain::CloseWindow()
105{
106 // Close Window.
107
108 if (fTimer)
109 fTimer->TurnOff();
110 DestroyWindow();
111}
112
113void Update()
114{
115 MemInfo_t memInfo;
116 CpuInfo_t cpuInfo;
117 Float_t act_load = 0.0;
118 Int_t memUsage = 0;
119 prev_load = act_load;
120 old_memUsage = memUsage;
121
122 // Get CPU information
123 gSystem->GetCpuInfo(&cpuInfo, 100);
124 // actual CPU load
125 act_load = cpuInfo.fTotal;
126 // Get Memory information
127 gSystem->GetMemInfo(&memInfo);
128 // choose which value to display
129 if (gMainWindow->GetActInfo() == 0)
130 memUsage = memInfo.fMemTotal;
131 else if (gMainWindow->GetActInfo() == 1)
132 memUsage = memInfo.fMemUsed;
133 else if (gMainWindow->GetActInfo() == 2)
134 memUsage = memInfo.fMemFree;
135 // small threshold to avoid "trembling" needle
136 if (fabs(act_load-prev_load) > 0.9) {
137 gSpeedo->SetScaleValue(act_load, 10);
138 prev_load = act_load;
139 }
140 // update only if value has changed
141 if (memUsage != old_memUsage) {
142 gSpeedo->SetOdoValue(memUsage);
143 old_memUsage = memUsage;
144 }
145}
146
147//______________________________________________________________________________
148void CPUMeter()
149{
150 // Main application.
151
152 gMainWindow = new TGShapedMain(gClient->GetRoot(), 500, 200);
153 gSpeedo = gMainWindow->GetSpeedo();
154
155 // set threshold values
156 gSpeedo->SetThresholds(12.5, 50.0, 87.5);
157 // set threshold colors
160 // enable threshold
161 gSpeedo->EnableThreshold();
162 gSpeedo->SetScaleValue(0.0, 5);
163 // enable peak marker
164 gSpeedo->EnablePeakMark();
165
166}
167
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
#define ClassDef(name, id)
Definition Rtypes.h:325
#define gClient
Definition TGClient.h:166
@ kLHintsCenterX
Definition TGLayout.h:32
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
#define gVirtualX
Definition TVirtualX.h:338
virtual void CloseWindow()
Close and delete main frame.
Definition TGFrame.cxx:1731
void SetOdoValue(Int_t val)
Set actual value of odo meter.
Definition TGSpeedo.cxx:338
void EnableThreshold()
Definition TGSpeedo.h:105
@ kOrange
Definition TGSpeedo.h:41
void SetThresholds(Float_t th1=0.0, Float_t th2=0.0, Float_t th3=0.0)
Definition TGSpeedo.h:101
void SetThresholdColors(EGlowColor col1, EGlowColor col2, EGlowColor col3)
Definition TGSpeedo.h:103
void SetScaleValue(Float_t val)
Set actual scale (needle position) value.
Definition TGSpeedo.cxx:397
void EnablePeakMark()
Definition TGSpeedo.h:107
virtual int GetMemInfo(MemInfo_t *info) const
Returns ram and swap memory usage info into the MemInfo_t structure.
Definition TSystem.cxx:2483
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:2473
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Float_t fTotal
Definition TSystem.h:171
Int_t fMemFree
Definition TSystem.h:182
Int_t fMemUsed
Definition TSystem.h:181
Int_t fMemTotal
Definition TSystem.h:180