CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/Services/src/Memory.h

Go to the documentation of this file.
00001 #ifndef FWCore_Services_Memory_h
00002 #define FWCore_Services_Memory_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     Services
00006 // Class  :     SimpleMemoryCheck
00007 // 
00008 //
00009 // Original Author:  Jim Kowalkowski
00010 // $Id: Memory.h,v 1.11 2012/04/25 19:53:23 wdd Exp $
00011 //
00012 // Change Log
00013 //
00014 // 1 - Apr 25, 2008 M. Fischler
00015 //      Data structures for Event summary statistics, 
00016 //
00017 // 2 - Jan 14, 2009 Natalia Garcia Nebot
00018 //      Added:  - Average rate of growth in RSS and peak value attained.
00019 //              - Average rate of growth in VSize over time, Peak VSize
00020 
00021 
00022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00023 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00024 #include "DataFormats/Provenance/interface/EventID.h"
00025 #include <cstdio>
00026 
00027 namespace edm {
00028   class EventID;
00029   class Timestamp;
00030   class Event;
00031   class EventSetup;
00032   class ModuleDescription;
00033   class ConfigurationDescriptions;
00034 
00035   namespace service {
00036     struct procInfo
00037     {
00038       procInfo():vsize(),rss() {}
00039       procInfo(double sz, double rss_sz): vsize(sz),rss(rss_sz) {}
00040         
00041       bool operator==(const procInfo& p) const
00042       { return vsize==p.vsize && rss==p.rss; }
00043 
00044       bool operator>(const procInfo& p) const
00045       { return vsize>p.vsize || rss>p.rss; }
00046 
00047       // see proc(4) man pages for units and a description
00048       double vsize;   // in MB (used to be in pages?)
00049       double rss;     // in MB (used to be in pages?)
00050     };
00051 
00052     struct smapsInfo
00053     {
00054       smapsInfo():private_(),pss_() {}
00055       smapsInfo(double private_sz, double pss_sz): private_(private_sz),pss_(pss_sz) {}
00056       
00057       bool operator==(const smapsInfo& p) const
00058       { return private_==p.private_ && pss_==p.pss_; }
00059       
00060       bool operator>(const smapsInfo& p) const
00061       { return private_>p.private_ || pss_>p.pss_; }
00062       
00063       double private_;   // in MB
00064       double pss_;     // in MB
00065     };
00066 
00067     
00068     class SimpleMemoryCheck
00069     {
00070     public:
00071 
00072       SimpleMemoryCheck(const ParameterSet&,ActivityRegistry&);
00073       ~SimpleMemoryCheck();
00074 
00075       static void fillDescriptions(edm::ConfigurationDescriptions & descriptions);
00076 
00077       void preSourceConstruction(const ModuleDescription&);
00078       void postSourceConstruction(const ModuleDescription&);
00079       void postSource();
00080 
00081       void postBeginJob();
00082       
00083       void preEventProcessing(const edm::EventID&, const edm::Timestamp&);
00084       void postEventProcessing(const Event&, const EventSetup&);
00085       
00086       void postModuleBeginJob(const ModuleDescription&);
00087       void postModuleConstruction(const ModuleDescription&);
00088 
00089       void preModule(const ModuleDescription&);
00090       void postModule(const ModuleDescription&);
00091 
00092       void postEndJob();
00093 
00094       void postFork(unsigned int, unsigned int);
00095     private:
00096       procInfo fetch();
00097       smapsInfo fetchSmaps();
00098       double pageSize() const { return pg_size_; }
00099       double averageGrowthRate(double current, double past, int count);
00100       void update();
00101       void updateMax();
00102       void andPrint(const std::string& type,
00103                 const std::string& mdlabel, const std::string& mdname) const;
00104       void updateAndPrint(const std::string& type,
00105                         const std::string& mdlabel, const std::string& mdname);
00106       void openFiles();
00107       
00108       procInfo a_;
00109       procInfo b_;
00110       procInfo max_;
00111       procInfo* current_;
00112       procInfo* previous_;
00113       
00114       smapsInfo currentSmaps_;
00115       
00116       char buf_[500];
00117       int fd_;
00118       std::string fname_;
00119       double pg_size_;
00120       int num_to_skip_;
00121       //options
00122       bool showMallocInfo_;
00123       bool oncePerEventMode_;
00124       bool jobReportOutputOnly_;
00125       bool monitorPssAndPrivate_;
00126       int count_;
00127 
00128       //smaps
00129       FILE* smapsFile_;
00130       char* smapsLineBuffer_;
00131       size_t smapsLineBufferLen_;
00132 
00133       
00134       //Rates of growth
00135       double growthRateVsize_;
00136       double growthRateRss_;
00137 
00138       // Event summary statistics                               changeLog 1
00139       struct SignificantEvent {
00140         int count;
00141         double vsize;
00142         double deltaVsize;
00143         double rss;
00144         double deltaRss;
00145         bool monitorPssAndPrivate;
00146         double privateSize;
00147         double pss;
00148         edm::EventID event;
00149         SignificantEvent() : count(0), vsize(0), deltaVsize(0), 
00150           rss(0), deltaRss(0), monitorPssAndPrivate(false), privateSize(0), pss(0),event() {}
00151         void set (double deltaV, double deltaR, 
00152                   edm::EventID const & e, SimpleMemoryCheck *t)
00153         { count = t->count_;
00154           vsize = t->current_->vsize;
00155           deltaVsize = deltaV;
00156           rss = t->current_->rss;
00157           deltaRss = deltaR;
00158           monitorPssAndPrivate = t->monitorPssAndPrivate_;
00159           if (monitorPssAndPrivate) {
00160             privateSize = t->currentSmaps_.private_;
00161             pss = t->currentSmaps_.pss_;
00162           }
00163           event = e;
00164         }
00165       }; // SignificantEvent
00166       friend struct SignificantEvent;
00167       friend std::ostream & operator<< (std::ostream & os, 
00168                 SimpleMemoryCheck::SignificantEvent const & se); 
00169 
00170       /* 
00171         Significative events for deltaVsize:
00172         - eventM_: Event which makes the biggest value for deltaVsize
00173         - eventL1_: Event which makes the second biggest value for deltaVsize
00174         - eventL2_: Event which makes the third biggest value for deltaVsize
00175         - eventR1_: Event which makes the second biggest value for deltaVsize
00176         - eventR2_: Event which makes the third biggest value for deltaVsize
00177         M>L1>L2 and M>R1>R2
00178                 Unknown relation between Ls and Rs events ???????
00179         Significative events for vsize:
00180         - eventT1_: Event whith the biggest value for vsize
00181         - eventT2_: Event whith the second biggest value for vsize
00182         - eventT3_: Event whith the third biggest value for vsize
00183         T1>T2>T3
00184        */
00185       SignificantEvent eventM_;  
00186       SignificantEvent eventL1_; 
00187       SignificantEvent eventL2_; 
00188       SignificantEvent eventR1_; 
00189       SignificantEvent eventR2_; 
00190       SignificantEvent eventT1_; 
00191       SignificantEvent eventT2_; 
00192       SignificantEvent eventT3_; 
00193 
00194       /*
00195         Significative event for deltaRss:
00196         - eventRssT1_: Event whith the biggest value for rss
00197         - eventRssT2_: Event whith the second biggest value for rss
00198         - eventRssT3_: Event whith the third biggest value for rss
00199         T1>T2>T3
00200         Significative events for deltaRss:
00201         - eventDeltaRssT1_: Event whith the biggest value for deltaRss
00202         - eventDeltaRssT2_: Event whith the second biggest value for deltaRss
00203         - eventDeltaRssT3_: Event whith the third biggest value for deltaRss
00204         T1>T2>T3
00205        */
00206       SignificantEvent eventRssT1_;
00207       SignificantEvent eventRssT2_;
00208       SignificantEvent eventRssT3_;
00209       SignificantEvent eventDeltaRssT1_;
00210       SignificantEvent eventDeltaRssT2_;
00211       SignificantEvent eventDeltaRssT3_;
00212 
00213 
00214       void updateEventStats(edm::EventID const & e);
00215       std::string eventStatOutput(std::string title, 
00216                                   SignificantEvent const& e) const;
00217       void eventStatOutput(std::string title, 
00218                            SignificantEvent const& e,
00219                            std::map<std::string, std::string> &m) const;
00220       std::string mallOutput(std::string title, size_t const& n) const;
00221 
00222       // Module summary statistices
00223       struct SignificantModule {
00224         int    postEarlyCount;
00225         double totalDeltaVsize;
00226         double maxDeltaVsize;
00227         edm::EventID eventMaxDeltaV;
00228         double totalEarlyVsize;
00229         double maxEarlyVsize;
00230         SignificantModule() : postEarlyCount  (0)
00231         , totalDeltaVsize (0)
00232         , maxDeltaVsize   (0)
00233         , eventMaxDeltaV  ()
00234         , totalEarlyVsize (0)
00235         , maxEarlyVsize   (0)     {}
00236         void set (double deltaV, bool early);
00237       }; // SignificantModule
00238       friend struct SignificantModule;
00239       friend std::ostream & operator<< (std::ostream & os, 
00240                 SimpleMemoryCheck::SignificantModule const & se); 
00241       bool moduleSummaryRequested_;
00242       typedef std::map<std::string,SignificantModule> SignificantModulesMap;
00243       SignificantModulesMap modules_;      
00244       double moduleEntryVsize_;
00245       edm::EventID currentEventID_;
00246       void updateModuleMemoryStats(SignificantModule & m, double dv);
00247                  
00248     }; // SimpleMemoryCheck
00249     
00250     std::ostream & 
00251     operator<< (std::ostream & os, 
00252                 SimpleMemoryCheck::SignificantEvent const & se); 
00253     
00254     std::ostream & 
00255     operator<< (std::ostream & os, 
00256                 SimpleMemoryCheck::SignificantModule const & se); 
00257     
00258   }
00259 }
00260 
00261 
00262 
00263 #endif