CPUMeter.C

Go to the documentation of this file.
00001 
00002 // Simple macro showing capabilities of TGSpeedo widget.
00003 //Author: Bertrand Bellenot
00004    
00005 #include "TSystem.h"
00006 #include "TGFrame.h"
00007 #include "TGWindow.h"
00008 #include "TGSpeedo.h"
00009 
00010 class TGShapedMain : public TGMainFrame {
00011 
00012 protected:
00013    const TGPicture   *fBgnd;           // picture used as mask
00014    TGSpeedo          *fSpeedo;          // analog meter
00015    TTimer            *fTimer;           // update timer
00016    Int_t              fActInfo;         // actual information value
00017 
00018 public:
00019    TGShapedMain(const TGWindow *p, int w, int h);
00020    virtual ~TGShapedMain();
00021 
00022    void              CloseWindow();
00023    TGSpeedo         *GetSpeedo() const { return fSpeedo; }
00024    Int_t             GetActInfo() const { return fActInfo; }
00025    void              ToggleInfos();
00026 
00027    ClassDef(TGShapedMain, 0)
00028 };
00029 
00030 
00031 // globals
00032 TGShapedMain *gMainWindow;
00033 TGSpeedo *gSpeedo;
00034 Float_t prev_load;
00035 Int_t old_memUsage;
00036 
00037 //______________________________________________________________________________
00038 TGShapedMain::TGShapedMain(const TGWindow *p, int w, int h) :
00039    TGMainFrame(p, w, h)
00040 {
00041    // Constructor.
00042 
00043    fActInfo = 1;
00044 
00045    fSpeedo = new TGSpeedo(this, 0.0, 100.0, "CPU", "[%]");
00046    fSpeedo->Connect("OdoClicked()", "TGShapedMain", this, "ToggleInfos()");
00047    fSpeedo->Connect("LedClicked()", "TGShapedMain", this, "CloseWindow()");
00048    Connect("CloseWindow()", "TGShapedMain", this, "CloseWindow()");
00049    AddFrame(fSpeedo, new TGLayoutHints(kLHintsCenterX | kLHintsCenterX));
00050    fSpeedo->SetDisplayText("Used RAM", "[MB]");
00051    fTimer = new TTimer(100);
00052    fTimer->SetCommand("Update()");
00053 
00054    fBgnd = fSpeedo->GetPicture();
00055    gVirtualX->ShapeCombineMask(GetId(), 0, 0, fBgnd->GetMask());
00056    SetBackgroundPixmap(fBgnd->GetPicture());
00057    SetWMSizeHints(fBgnd->GetWidth(), fBgnd->GetHeight(), fBgnd->GetWidth(),
00058                   fBgnd->GetHeight(), 1, 1);
00059 
00060    MapSubwindows();
00061    MapWindow();
00062    // To avoid closing the window while TGSpeedo is drawing
00063    DontCallClose();
00064    // To avoid closing the window while TGSpeedo is drawing
00065    Resize(GetDefaultSize());
00066    // Set fixed size
00067    SetWMSizeHints(GetDefaultWidth(), GetDefaultHeight(), GetDefaultWidth(), 
00068                   GetDefaultHeight(), 1, 1);
00069    SetWindowName("ROOT CPU Load Meter");
00070    fTimer->TurnOn();   
00071 }
00072 
00073 //______________________________________________________________________________
00074 void TGShapedMain::ToggleInfos()
00075 {
00076    // Toggle information displayed in Analog Meter
00077 
00078    if (fActInfo < 2)
00079       fActInfo++;
00080    else
00081       fActInfo = 0;
00082    if (fActInfo == 0)
00083       fSpeedo->SetDisplayText("Total RAM", "[MB]");
00084    else if (fActInfo == 1)
00085       fSpeedo->SetDisplayText("Used RAM", "[MB]");
00086    else if (fActInfo == 2)
00087       fSpeedo->SetDisplayText("Free RAM", "[MB]");
00088 }
00089 
00090 //______________________________________________________________________________
00091 TGShapedMain::~TGShapedMain()
00092 {
00093    // Destructor.
00094 
00095    delete fTimer;
00096    delete fSpeedo;
00097 }
00098 
00099 //______________________________________________________________________________
00100 void TGShapedMain::CloseWindow()
00101 {
00102    // Close Window.
00103 
00104    if (fTimer)
00105       fTimer->TurnOff();
00106    DestroyWindow();
00107 }
00108 
00109 void Update()
00110 {
00111    MemInfo_t memInfo;
00112    CpuInfo_t cpuInfo;
00113    Float_t act_load = 0.0;
00114    Int_t memUsage = 0;
00115    prev_load = act_load;
00116    old_memUsage = memUsage;
00117 
00118    // Get CPU informations
00119    gSystem->GetCpuInfo(&cpuInfo, 100);
00120    // actual CPU load
00121    act_load = cpuInfo.fTotal;
00122    // Get Memory informations
00123    gSystem->GetMemInfo(&memInfo);
00124    // choose which value to display
00125    if (gMainWindow->GetActInfo() == 0)
00126       memUsage = memInfo.fMemTotal;
00127    else if (gMainWindow->GetActInfo() == 1)
00128       memUsage = memInfo.fMemUsed;
00129    else if (gMainWindow->GetActInfo() == 2)
00130       memUsage = memInfo.fMemFree;
00131    // small threshold to avoid "trembling" needle
00132    if (fabs(act_load-prev_load) > 0.9) {
00133       gSpeedo->SetScaleValue(act_load, 10);
00134       prev_load = act_load;
00135    }
00136    // update only if value has changed
00137    if (memUsage != old_memUsage) {
00138       gSpeedo->SetOdoValue(memUsage);
00139       old_memUsage = memUsage;
00140    }
00141 }
00142 
00143 //______________________________________________________________________________
00144 void CPUMeter()
00145 {
00146    // Main application.
00147 
00148    gMainWindow = new TGShapedMain(gClient->GetRoot(), 500, 200);
00149    gSpeedo = gMainWindow->GetSpeedo();
00150 
00151    // set threshold values
00152    gSpeedo->SetThresholds(12.5, 50.0, 87.5);
00153    // set threshold colors
00154    gSpeedo->SetThresholdColors(TGSpeedo::kGreen, TGSpeedo::kOrange, 
00155                                TGSpeedo::kRed);
00156    // enable threshold
00157    gSpeedo->EnableThreshold();
00158    gSpeedo->SetScaleValue(0.0, 5);
00159    // enable peak marker
00160    gSpeedo->EnablePeakMark();
00161 
00162 }
00163 

Generated on Tue Jul 5 15:44:23 2011 for ROOT_528-00b_version by  doxygen 1.5.1