Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
33};
34
35// globals
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
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{
116 Float_t act_load = 0.0;
117 Int_t memUsage = 0;
120
121 // Get CPU information
122 gSystem->GetCpuInfo(&cpuInfo, 100);
123 // actual CPU load
124 act_load = cpuInfo.fTotal;
125 // Get Memory information
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);
138 }
139 // update only if value has changed
140 if (memUsage != old_memUsage) {
141 gSpeedo->SetOdoValue(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
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
#define ClassDef(name, id)
Definition Rtypes.h:342
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gClient
Definition TGClient.h:157
@ 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:572
#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:399
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
@ kOrange
Definition TGSpeedo.h:25
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:2490
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:2480
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)