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#include "TVirtualX.h"
14
15class TGShapedMain : public TGMainFrame {
16
17protected:
18 const TGPicture *fBgnd; // picture used as mask
19 TGSpeedo *fSpeedo; // analog meter
20 TTimer *fTimer; // update timer
21 Int_t fActInfo; // actual information value
22
23public:
24 TGShapedMain(const TGWindow *p, int w, int h);
25 virtual ~TGShapedMain();
26
27 void CloseWindow();
28 TGSpeedo *GetSpeedo() const { return fSpeedo; }
29 Int_t GetActInfo() const { return fActInfo; }
30 void ToggleInfos();
31
32 ClassDef(TGShapedMain, 0)
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) : TGMainFrame(p, w, h)
43{
44 // Constructor.
45
46 fActInfo = 1;
47
48 fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
49 fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
50 fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
51 Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
52 AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX));
53 fSpeedo->SetDisplayText("Used RAM", "[MB]");
54 fTimer = new TTimer(100);
55 fTimer->SetCommand("Update()");
56
57 fBgnd = fSpeedo->GetPicture();
58 if (fBgnd) {
59 gVirtualX->ShapeCombineMask(GetId(), 0, 0, fBgnd->GetMask());
60 SetBackgroundPixmap(fBgnd->GetPicture());
61 SetWMSizeHints(fBgnd->GetWidth(), fBgnd->GetHeight(), fBgnd->GetWidth(), fBgnd->GetHeight(), 1, 1);
62 }
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(), GetDefaultHeight(), 1, 1);
72 SetWindowName("ROOT CPU Load Meter");
73 fTimer->TurnOn();
74}
75
76//______________________________________________________________________________
77void TGShapedMain::ToggleInfos()
78{
79 // Toggle information displayed in Analog Meter
80
81 if (fActInfo < 2)
82 fActInfo++;
83 else
84 fActInfo = 0;
85 if (fActInfo == 0)
86 fSpeedo->SetDisplayText("Total RAM", "[MB]");
87 else if (fActInfo == 1)
88 fSpeedo->SetDisplayText("Used RAM", "[MB]");
89 else if (fActInfo == 2)
90 fSpeedo->SetDisplayText("Free RAM", "[MB]");
91}
92
93//______________________________________________________________________________
94TGShapedMain::~TGShapedMain()
95{
96 // Destructor.
97
98 delete fTimer;
99 delete fSpeedo;
100}
101
102//______________________________________________________________________________
103void TGShapedMain::CloseWindow()
104{
105 // Close Window.
106
107 if (fTimer)
108 fTimer->TurnOff();
109 DestroyWindow();
110}
111
112void Update()
113{
114 MemInfo_t memInfo;
115 CpuInfo_t cpuInfo;
116 Float_t act_load = 0.0;
117 Int_t memUsage = 0;
118 prev_load = act_load;
119 old_memUsage = memUsage;
120
121 // Get CPU information
122 gSystem->GetCpuInfo(&cpuInfo, 100);
123 // actual CPU load
124 act_load = cpuInfo.fTotal;
125 // Get Memory information
126 gSystem->GetMemInfo(&memInfo);
127 // choose which value to display
128 if (gMainWindow->GetActInfo() == 0)
129 memUsage = memInfo.fMemTotal;
130 else if (gMainWindow->GetActInfo() == 1)
131 memUsage = memInfo.fMemUsed;
132 else if (gMainWindow->GetActInfo() == 2)
133 memUsage = memInfo.fMemFree;
134 // small threshold to avoid "trembling" needle
135 if (fabs(act_load - prev_load) > 0.9) {
136 gSpeedo->SetScaleValue(act_load, 10);
137 prev_load = act_load;
138 }
139 // update only if value has changed
140 if (memUsage != old_memUsage) {
141 gSpeedo->SetOdoValue(memUsage);
142 old_memUsage = memUsage;
143 }
144}
145
146//______________________________________________________________________________
147void CPUMeter()
148{
149 // Main application.
150
151 gMainWindow = new TGShapedMain(gClient->GetRoot(), 500, 200);
152 gSpeedo = gMainWindow->GetSpeedo();
153
154 // set threshold values
155 gSpeedo->SetThresholds(12.5, 50.0, 87.5);
156 // set threshold colors
158 // enable threshold
159 gSpeedo->EnableThreshold();
160 gSpeedo->SetScaleValue(0.0, 5);
161 // enable peak marker
162 gSpeedo->EnablePeakMark();
163}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
#define ClassDef(name, id)
Definition Rtypes.h:344
#define gClient
Definition TGClient.h:157
@ kLHintsCenterX
Definition TGLayout.h:25
externTSystem * gSystem
Definition TSystem.h:582
#define gVirtualX
Definition TVirtualX.h:375
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:399
virtual void CloseWindow()
Close and delete main frame.
Definition TGFrame.cxx:1762
TGSpeedo is a widget looking like a speedometer, with a needle, a counter and a small odometer window...
Definition TGSpeedo.h:22
void SetOdoValue(Int_t val)
Set actual value of odo meter.
Definition TGSpeedo.cxx:333
void EnableThreshold()
Definition TGSpeedo.h:98
@ kOrange
Definition TGSpeedo.h:31
void SetThresholds(Float_t th1=0.0, Float_t th2=0.0, Float_t th3=0.0)
Definition TGSpeedo.h:94
void SetThresholdColors(EGlowColor col1, EGlowColor col2, EGlowColor col3)
Definition TGSpeedo.h:96
void SetScaleValue(Float_t val)
Set actual scale (needle position) value.
Definition TGSpeedo.cxx:392
void EnablePeakMark()
Definition TGSpeedo.h:100
ROOT GUI Window base class.
Definition TGWindow.h:23
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:179
Int_t fMemFree
Definition TSystem.h:190
Int_t fMemUsed
Definition TSystem.h:189
Int_t fMemTotal
Definition TSystem.h:188