TMemStatViewerGUI.cxx

Go to the documentation of this file.
00001 // @(#)root/memstat:$Name$:$Id: TMemStatViewerGUI.cxx 24658 2008-07-04 09:06:58Z anar $
00002 // Author: M.Ivanov -- Anar Manafov (A.Manafov@gsi.de) 28/04/2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 
00013 ///////////////////////////////////////////////////////////////////////////////
00014 //                                                                           //
00015 //  GUI for the TMemStat                                                     //
00016 //  used for the memory checker                                             //
00017 //  Example usage:                                                           //
00018 /*
00019   aliroot
00020   TMemStatViewer::ShowGUI("memstat.root")
00021 */
00022 // - Resize windows - (BUG to BE FIXED -> ROOT bug)                          //
00023 //                                                                           //
00024 //////////////////////////////////////////////////////////////////////////////
00025 
00026 // STD
00027 #include <functional>
00028 #include <stdexcept>
00029 #include <algorithm>
00030 // ROOT
00031 #include "TGTextView.h"
00032 #include "TGLabel.h"
00033 #include "TGTab.h"
00034 #include "TGButton.h"
00035 #include "TGNumberEntry.h"
00036 #include "TGSplitter.h"
00037 #include "TGButtonGroup.h"
00038 #include "TGComboBox.h"
00039 #include "TObjArray.h"
00040 // Memstat
00041 #include "TMemStat.h"
00042 #include "TMemStatViewerGUI.h"
00043 #include "TMemStatResource.h"
00044 #include "TMemStatDrawDlg.h"
00045 
00046 ClassImp(TMemStatViewerGUI)
00047 
00048 using namespace std;
00049 
00050 
00051 //______________________________________________________________________________
00052 struct SStringToListBox_t : public binary_function<string, TGComboBox*, bool> {
00053    bool operator()(string str, TGComboBox* box) const {
00054       if (!box)
00055          return false;
00056 
00057       box->AddEntry(str.c_str(), box->GetNumberOfEntries());
00058       return true;
00059    }
00060 };
00061 
00062 //______________________________________________________________________________
00063 struct SFillListBox_t : public binary_function<TObject*, TGComboBox*, bool> {
00064    bool operator()(TObject *aObj, TGComboBox* box) const {
00065       if (!aObj || !box)
00066          return false; //TODO: need an assert "SFillListBox_t: parameter is a NULL pointer"
00067 
00068       if ((aObj->IsA() == TObjString::Class())) {
00069          TObjString *str(dynamic_cast<TObjString*>(aObj));
00070          if (!str)
00071             return false; // TODO: need an assert "SFillListBox_t: Container's element is not a TObjString object."
00072 
00073          SStringToListBox_t()(str->String().Data(), box);
00074       }
00075 
00076       return true;
00077    }
00078 };
00079 
00080 //______________________________________________________________________________
00081 TMemStatViewerGUI::TMemStatViewerGUI(const TGWindow *p, UInt_t w, UInt_t h, Option_t* option):
00082       TGCompositeFrame(p, w, h),
00083       fViewer(NULL),
00084       fText(NULL),
00085       fNmbStackDeep(NULL),
00086       fNmbSortDeep(NULL)
00087 {
00088    // TMemStatViewerGUI constructor; fileName specifies the ROOT tree used for drawing
00089 
00090    SetCleanup(kDeepCleanup);
00091 
00092    // ************************* content of this MainFrame *************************
00093    // top level container with horizontal layout
00094    // container for all GUI elements, vertical divided
00095    TGCompositeFrame *contMain = new TGCompositeFrame(this, w, h, kVerticalFrame | kFixedWidth | kFixedHeight);
00096    AddFrame(contMain, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00097 
00098    // container for all GUI elements, horizontal divided
00099    TGCompositeFrame *contLCR = new TGCompositeFrame(contMain, w, h, kHorizontalFrame | kFixedWidth | kFixedHeight);
00100    contMain->AddFrame(contLCR, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00101 
00102    // ************************* content of ContLCR *************************
00103    // container for GUI elements on left side
00104    TGCompositeFrame *contLeft = new TGCompositeFrame(contLCR, 160, 200, kVerticalFrame | kFixedWidth | kFitHeight);
00105    contLCR->AddFrame(contLeft, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandY, 5, 3, 3, 3));
00106 
00107    // left vertical splitter
00108    TGVSplitter *splitLeft = new TGVSplitter(contLCR);
00109    splitLeft->SetFrame(contLeft, kTRUE);
00110    contLCR->AddFrame(splitLeft, new TGLayoutHints(kLHintsLeft | kLHintsExpandY));
00111 
00112    // container for GUI elements at the center
00113    TGCompositeFrame *contCenter = new TGCompositeFrame(contLCR, 150, 200, kVerticalFrame | kFixedWidth | kFitHeight);
00114    contLCR->AddFrame(contCenter, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00115 
00116    TGTab *tab = new TGTab(contCenter, 150, 200);
00117    contCenter->AddFrame(tab, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00118    TGCompositeFrame *text = tab->AddTab("Text");
00119    TGCompositeFrame *graphics = tab->AddTab("Graphics");
00120 
00121    fText = new TGTextView(text);
00122    text->AddFrame(fText, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00123 
00124    // Display everything
00125    Initialize(option);
00126 
00127    // Graphics View
00128    // TMemStat must be initialized first
00129    new TMemStatDrawDlg(graphics, fViewer);
00130 
00131    MakeStampList(contLeft);
00132    MakeSelection(contLeft);
00133    MakeContSortStat(contLeft);   // Make content for Sort Statistic
00134    MakeContSortStamp(contLeft);  // make content for sort Stamps
00135    MakeContDeep(contLeft);       // make content for sort Stamps
00136 
00137    MapSubwindows();
00138    Resize(GetDefaultSize());
00139    MapWindow();
00140 
00141    // Default View
00142    fViewer->SetSortStat( TMemStat::kTotalAllocCount );
00143    fViewer->SetSortStamp( TMemStat::kCurrent );
00144    MakePrint();
00145 }
00146 
00147 //______________________________________________________________________________
00148 TMemStatViewerGUI::~TMemStatViewerGUI()
00149 {
00150    // a dtor
00151 
00152    Cleanup();
00153    if (fViewer)
00154       fViewer->Delete();
00155 }
00156 
00157 //______________________________________________________________________________
00158 void TMemStatViewerGUI::Initialize(Option_t* option)
00159 {
00160    // initializes the GUI with default settings and opens tree for drawing
00161 
00162    delete fViewer;
00163    fViewer = new TMemStat(option);
00164    fViewer->Report();
00165 }
00166 
00167 //______________________________________________________________________________
00168 void TMemStatViewerGUI::MakeContSortStat(TGCompositeFrame *frame)
00169 {
00170    // make windows for Sorting *STAT* selection
00171 
00172    // sorting statistic option
00173    TGVButtonGroup *sortStatGroup = new TGVButtonGroup(frame, "Statistic type");
00174    frame->AddFrame(sortStatGroup, new TGLayoutHints(kLHintsExpandX));
00175    new TGRadioButton(sortStatGroup, "Total Alloc Count", rbtnTotalAllocCount);
00176    new TGRadioButton(sortStatGroup, "Total Alloc Size", rbtnTotalAllocSize);
00177    new TGRadioButton(sortStatGroup, "Alloc Count", rbtnAllocCount);
00178    new TGRadioButton(sortStatGroup, "Alloc Size", rbtnAllocSize);
00179    sortStatGroup->SetButton(rbtnTotalAllocCount);
00180    sortStatGroup->Connect("Pressed(Int_t)", "TMemStatViewerGUI", this, "HandleButtonsSortStat(Int_t)");
00181 }
00182 
00183 //______________________________________________________________________________
00184 void TMemStatViewerGUI::MakeContSortStamp(TGCompositeFrame *frame)
00185 {
00186    // make windows for Sorting *STAMP* selection
00187 
00188    // sorting stamp option
00189    TGVButtonGroup *sortStampGroup = new TGVButtonGroup(frame, "Sorting stamp");
00190    frame->AddFrame(sortStampGroup, new TGLayoutHints(kLHintsExpandX));
00191    new TGRadioButton(sortStampGroup, "Current", rbtnCurrent);
00192    new TGRadioButton(sortStampGroup, "Max Size", rbtnMaxSize);
00193    new TGRadioButton(sortStampGroup, "Max Count", rbtnMaxCount);
00194    sortStampGroup->SetButton(rbtnCurrent);
00195    sortStampGroup->Connect("Pressed(Int_t)", "TMemStatViewerGUI", this, "HandleButtonsSortStamp(Int_t)");
00196 }
00197 
00198 //______________________________________________________________________________
00199 void TMemStatViewerGUI::MakeStampList(TGCompositeFrame *frame)
00200 {
00201    // make STAMPs list box
00202 
00203    if (!fViewer)
00204       return;
00205 
00206    const TObjArray *stampList = fViewer->GetStampList();
00207    if (!stampList)
00208       return;
00209 
00210    TGHorizontalFrame *horz = new TGHorizontalFrame(frame);
00211    frame->AddFrame(horz, new TGLayoutHints(kLHintsExpandX, 2, 2, 10, 2));
00212    // text description
00213    TGLabel *lblName = new TGLabel(horz, "Stamp name:");
00214    horz->AddFrame(lblName,
00215                   new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 2, 2, 2, 2));
00216 
00217    // a list box of stamps
00218    TGComboBox *stampListBox = new TGComboBox(horz, lstStamps);
00219    stampListBox->Resize(100, 20);
00220    horz->AddFrame(stampListBox, new TGLayoutHints(kLHintsExpandX));
00221    stampListBox->Connect("Selected(const char*)", "TMemStatViewerGUI", this, "HandleStampSelect(const char*)");
00222 
00223    // filling Combo box of stamps
00224    TIter iter(stampList);
00225    for_each(iter.Begin(), TIter::End(),
00226             bind2nd(SFillListBox_t(), stampListBox));
00227 
00228    const Int_t count(stampListBox->GetNumberOfEntries());
00229    if (count <= 0)
00230       return;
00231    // Selecting the last stamp
00232    stampListBox->Select(count - 1);
00233 
00234    TObjString *str(dynamic_cast<TObjString*>(stampList->At(stampListBox->GetSelected())));
00235    if (!str)
00236       return;
00237 
00238    fViewer->SetCurrentStamp(*str);
00239 }
00240 
00241 //______________________________________________________________________________
00242 void TMemStatViewerGUI::HandleStampSelect(const char* value)
00243 {
00244    // TODO: Comment me
00245 
00246    fViewer->SetCurrentStamp(value);
00247    MakePrint();
00248 }
00249 
00250 //______________________________________________________________________________
00251 void  TMemStatViewerGUI::MakeContDeep(TGCompositeFrame *frame)
00252 {
00253    // create and layout "Deep" controls
00254 
00255    TGGroupFrame *contDeep = new TGGroupFrame(frame, "Deepnes");
00256    contDeep->SetLayoutManager(new TGMatrixLayout(contDeep, 0, 2, 5));
00257    frame->AddFrame(contDeep, new TGLayoutHints(kLHintsExpandX));
00258 
00259    // ------ Stack Deep
00260    // text description
00261    TGLabel *lblStackDeep = new TGLabel(contDeep, "Stack Deep:");
00262    contDeep->AddFrame(lblStackDeep, 0);
00263    // number entry box for specifying the stack deepness
00264    fNmbStackDeep = new TGNumberEntry(contDeep, fViewer->GetStackDeep(), 1, -1, TGNumberFormat::kNESInteger,
00265                                      TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 1, 50);
00266    contDeep->AddFrame(fNmbStackDeep, 0);
00267    fNmbStackDeep->Connect("ValueSet(Long_t)", "TMemStatViewerGUI", this, "HandleDeep(Long_t)");
00268    fNmbStackDeep->Resize(60, 20);
00269 
00270    // ------ Sort Deep
00271    // text description
00272    TGLabel *lSortDeep = new TGLabel(contDeep, "Sort Deep:");
00273    contDeep->AddFrame(lSortDeep, 0);
00274    // number entry box for specifying the number of stamps
00275    fNmbSortDeep = new TGNumberEntry(contDeep, fViewer->GetSortDeep(), 1, -1, TGNumberFormat::kNESInteger,
00276                                     TGNumberFormat::kNEANonNegative, TGNumberFormat::kNELLimitMinMax, 1, 50);
00277    contDeep->AddFrame(fNmbSortDeep, 0);
00278    fNmbSortDeep->Connect("ValueSet(Long_t)", "TMemStatViewerGUI", this, "HandleDeep(Long_t)");
00279    fNmbSortDeep->Resize(60, 20);
00280 }
00281 
00282 //______________________________________________________________________________
00283 void TMemStatViewerGUI::HandleButtonsSortStat(Int_t id)
00284 {
00285    // handles mutual radio button exclusions - set sort stat type
00286    TMemStat::StatType val;
00287    HandleRButtons(id, rbtnTotalAllocCount, &val);
00288    fViewer->SetSortStat(val);
00289    MakePrint();
00290 }
00291 
00292 //___________________________l___________________________________________________
00293 void TMemStatViewerGUI::HandleButtonsSortStamp(Int_t id)
00294 {
00295    // handles mutual radio button exclusions - set sort stat type
00296    TMemStat::StampType val;
00297    HandleRButtons(id, rbtnCurrent, &val);
00298    fViewer->SetSortStamp(val);
00299    MakePrint();
00300 }
00301 
00302 //______________________________________________________________________________
00303 void TMemStatViewerGUI::HandleDeep(Long_t /*id*/)
00304 {
00305    // handles stack deep
00306 
00307    fViewer->SetStackDeep( fNmbStackDeep->GetIntNumber() );
00308    fViewer->SetSortDeep( fNmbSortDeep->GetIntNumber() );
00309    MakePrint();
00310 }
00311 
00312 //______________________________________________________________________________
00313 void TMemStatViewerGUI::ShowGUI()
00314 {
00315    // initialize and show GUI for presentation
00316 
00317    TGMainFrame* frmMain = new TGMainFrame(gClient->GetRoot(), 950, 600);
00318    frmMain->SetWindowName("TMemStat analysis console");
00319    frmMain->SetCleanup(kDeepCleanup);
00320 
00321    TMemStatViewerGUI* calibViewer1 = new TMemStatViewerGUI(frmMain, 950, 600, "read");
00322    frmMain->AddFrame(calibViewer1, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY));
00323 
00324    // position relative to the parent's window
00325    //frmMain->CenterOnParent();
00326    frmMain->MapSubwindows();
00327    frmMain->Resize();
00328    frmMain->MapWindow();
00329 }
00330 
00331 //______________________________________________________________________________
00332 void TMemStatViewerGUI::MakePrint()
00333 {
00334    // make report and load it to the view
00335    fViewer->MakeReport( fCurLib.c_str(), fCurFunc.c_str(), 0, "/tmp/memstatprint.txt");
00336    fText->LoadFile("/tmp/memstatprint.txt");
00337 }
00338 
00339 //______________________________________________________________________________
00340 void TMemStatViewerGUI::MakeSelection(TGCompositeFrame *frame)
00341 {
00342    // TODO: Comment me
00343    if (!fViewer)
00344       return;
00345 
00346    TGGroupFrame *grp = new TGGroupFrame(frame, "Selections");
00347    frame->AddFrame(grp, new TGLayoutHints(kLHintsExpandX));
00348 
00349    // ----- Function
00350    // text description
00351    TGLabel *lblFun = new TGLabel(grp, "Function");
00352    grp->AddFrame(lblFun, new TGLayoutHints(kLHintsExpandX ));
00353    // a list box of stamps
00354    TGComboBox *lboxFunctions = new TGComboBox(grp);
00355    lboxFunctions->Resize(100, 20);
00356    grp->AddFrame(lboxFunctions, new TGLayoutHints(kLHintsExpandX ));
00357    lboxFunctions->Connect("Selected(const char*)", "TMemStatViewerGUI", this, "HandleFuncSelect(const char*)");
00358 
00359    // Add default selection - select all
00360    lboxFunctions->AddEntry("*", 0);
00361    // Fill values for Functions
00362    TMemStat::Selection_t container;
00363    fViewer->GetFillSelection( &container, TMemStat::kFunction );
00364    for_each(container.begin(), container.end(),
00365             bind2nd(SStringToListBox_t(), lboxFunctions));
00366    lboxFunctions->Select(0);
00367 
00368    // ----- Library
00369    // text description
00370    TGLabel *lblLib = new TGLabel(grp, "Libraries");
00371    grp->AddFrame(lblLib, new TGLayoutHints(kLHintsExpandX));
00372    // a list box of stamps
00373    TGComboBox *lboxLibraries = new TGComboBox(grp);
00374    lboxLibraries->Resize(100, 20);
00375    grp->AddFrame(lboxLibraries, new TGLayoutHints(kLHintsExpandX ));
00376    lboxLibraries->Connect("Selected(const char*)", "TMemStatViewerGUI", this, "HandleLibSelect(const char*)");
00377 
00378    // Add default selection - select all
00379    lboxLibraries->AddEntry("*", 0);
00380    // Fill values for Functions
00381    container.clear();
00382    fViewer->GetFillSelection( &container, TMemStat::kLibrary );
00383    for_each(container.begin(), container.end(),
00384             bind2nd(SStringToListBox_t(), lboxLibraries));
00385    lboxLibraries->Select(0);
00386 }
00387 
00388 //______________________________________________________________________________
00389 void TMemStatViewerGUI::HandleFuncSelect(const char* _val)
00390 {
00391    // TODO: Comment me
00392    fCurFunc = _val;
00393    // if _val == "*" then we don't sort
00394    if ( fCurFunc.find("*") != string::npos )
00395       fCurFunc.clear();
00396 
00397    MakePrint();
00398 }
00399 
00400 //______________________________________________________________________________
00401 void TMemStatViewerGUI::HandleLibSelect(const char* _val)
00402 {
00403    // TODO: Comment me
00404    fCurLib = _val;
00405    // if _val == "*" then we don't sort
00406    if ( fCurLib.find("*") != string::npos )
00407       fCurLib.clear();
00408 
00409    MakePrint();
00410 }

Generated on Tue Jul 5 14:43:53 2011 for ROOT_528-00b_version by  doxygen 1.5.1