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
36// globals
37TGShapedMain *gMainWindow;
38TGSpeedo *gSpeedo;
39Float_t prev_load;
40Int_t old_memUsage;
41
42//______________________________________________________________________________
43TGShapedMain::TGShapedMain(const TGWindow *p, int w, int h) :
44 TGMainFrame(p, w, h)
45{
46 // Constructor.
47
48 fActInfo = 1;
49
50 fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
51 fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
52 fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
53 Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
54 AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX));
55 fSpeedo->SetDisplayText("Used RAM", "[MB]");
56 fTimer = new TTimer(100);
57 fTimer->SetCommand("Update()");
58
59 fBgnd = fSpeedo->GetPicture();
60 if(fBgnd) {
61 gVirtualX->ShapeCombineMask(GetId(), 0, 0, fBgnd->GetMask());
62 SetBackgroundPixmap(fBgnd->GetPicture());
63 SetWMSizeHints(fBgnd->GetWidth(), fBgnd->GetHeight(), fBgnd->GetWidth(),
64 fBgnd->GetHeight(), 1, 1);
65 }
66
68 MapWindow();
69 // To avoid closing the window while TGSpeedo is drawing
70 DontCallClose();
71 // To avoid closing the window while TGSpeedo is drawing
72 Resize(GetDefaultSize());
73 // Set fixed size
74 SetWMSizeHints(GetDefaultWidth(), GetDefaultHeight(), GetDefaultWidth(),
75 GetDefaultHeight(), 1, 1);
76 SetWindowName("ROOT CPU Load Meter");
77 fTimer->TurnOn();
78}
79
80//______________________________________________________________________________
81void TGShapedMain::ToggleInfos()
82{
83 // Toggle information displayed in Analog Meter
84
85 if (fActInfo < 2)
86 fActInfo++;
87 else
88 fActInfo = 0;
89 if (fActInfo == 0)
90 fSpeedo->SetDisplayText("Total RAM", "[MB]");
91 else if (fActInfo == 1)
92 fSpeedo->SetDisplayText("Used RAM", "[MB]");
93 else if (fActInfo == 2)
94 fSpeedo->SetDisplayText("Free RAM", "[MB]");
95}
96
97//______________________________________________________________________________
98TGShapedMain::~TGShapedMain()
99{
100 // Destructor.
101
102 delete fTimer;
103 delete fSpeedo;
104}
105
106//______________________________________________________________________________
107void TGShapedMain::CloseWindow()
108{
109 // Close Window.
110
111 if (fTimer)
112 fTimer->TurnOff();
113 DestroyWindow();
114}
115
116void Update()
117{
118 MemInfo_t memInfo;
119 CpuInfo_t cpuInfo;
120 Float_t act_load = 0.0;
121 Int_t memUsage = 0;
122 prev_load = act_load;
123 old_memUsage = memUsage;
124
125 // Get CPU information
126 gSystem->GetCpuInfo(&cpuInfo, 100);
127 // actual CPU load
128 act_load = cpuInfo.fTotal;
129 // Get Memory information
130 gSystem->GetMemInfo(&memInfo);
131 // choose which value to display
132 if (gMainWindow->GetActInfo() == 0)
133 memUsage = memInfo.fMemTotal;
134 else if (gMainWindow->GetActInfo() == 1)
135 memUsage = memInfo.fMemUsed;
136 else if (gMainWindow->GetActInfo() == 2)
137 memUsage = memInfo.fMemFree;
138 // small threshold to avoid "trembling" needle
139 if (fabs(act_load-prev_load) > 0.9) {
140 gSpeedo->SetScaleValue(act_load, 10);
141 prev_load = act_load;
142 }
143 // update only if value has changed
144 if (memUsage != old_memUsage) {
145 gSpeedo->SetOdoValue(memUsage);
146 old_memUsage = memUsage;
147 }
148}
149
150//______________________________________________________________________________
151void CPUMeter()
152{
153 // Main application.
154
155 gMainWindow = new TGShapedMain(gClient->GetRoot(), 500, 200);
156 gSpeedo = gMainWindow->GetSpeedo();
157
158 // set threshold values
159 gSpeedo->SetThresholds(12.5, 50.0, 87.5);
160 // set threshold colors
163 // enable threshold
164 gSpeedo->EnableThreshold();
165 gSpeedo->SetScaleValue(0.0, 5);
166 // enable peak marker
167 gSpeedo->EnablePeakMark();
168
169}
170
#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:337
#define gClient
Definition TGClient.h:156
@ kLHintsCenterX
Definition TGLayout.h:25
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize MapSubwindows
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t SetWMSizeHints
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
#define gVirtualX
Definition TVirtualX.h:337
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:397
virtual void CloseWindow()
Close and delete main frame.
Definition TGFrame.cxx:1770
The TGPicture class implements pictures and icons used in the different GUI elements and widgets.
Definition TGPicture.h:25
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:334
void EnableThreshold()
Definition TGSpeedo.h:89
@ kOrange
Definition TGSpeedo.h:25
void SetThresholds(Float_t th1=0.0, Float_t th2=0.0, Float_t th3=0.0)
Definition TGSpeedo.h:85
void SetThresholdColors(EGlowColor col1, EGlowColor col2, EGlowColor col3)
Definition TGSpeedo.h:87
void SetScaleValue(Float_t val)
Set actual scale (needle position) value.
Definition TGSpeedo.cxx:393
void EnablePeakMark()
Definition TGSpeedo.h:91
ROOT GUI Window base class.
Definition TGWindow.h:23
virtual int GetMemInfo(MemInfo_t *info) const
Returns ram and swap memory usage info into the MemInfo_t structure.
Definition TSystem.cxx:2479
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:2469
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:169
Int_t fMemFree
Definition TSystem.h:180
Int_t fMemUsed
Definition TSystem.h:179
Int_t fMemTotal
Definition TSystem.h:178