CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/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.10 2011/07/22 20:42:38 chrjones 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       int count_;
00126 
00127       //smaps
00128       FILE* smapsFile_;
00129       char* smapsLineBuffer_;
00130       size_t smapsLineBufferLen_;
00131 
00132       
00133       //Rates of growth
00134       double growthRateVsize_;
00135       double growthRateRss_;
00136 
00137       // Event summary statistics                               changeLog 1
00138       struct SignificantEvent {
00139         int count;
00140         double vsize;
00141         double deltaVsize;
00142         double rss;
00143         double deltaRss;
00144         double privateSize;
00145         double pss;
00146         edm::EventID event;
00147         SignificantEvent() : count(0), vsize(0), deltaVsize(0), 
00148         rss(0), deltaRss(0), privateSize(0), pss(0),event() {}
00149         void set (double deltaV, double deltaR, 
00150                   edm::EventID const & e, SimpleMemoryCheck *t)
00151         { count = t->count_;
00152           vsize = t->current_->vsize;
00153           deltaVsize = deltaV;
00154           rss = t->current_->rss;
00155           deltaRss = deltaR;
00156           privateSize = t->currentSmaps_.private_;
00157           pss = t->currentSmaps_.pss_;
00158           event = e;
00159         }
00160       }; // SignificantEvent
00161       friend struct SignificantEvent;
00162       friend std::ostream & operator<< (std::ostream & os, 
00163                 SimpleMemoryCheck::SignificantEvent const & se); 
00164 
00165       /* 
00166         Significative events for deltaVsize:
00167         - eventM_: Event which makes the biggest value for deltaVsize
00168         - eventL1_: Event which makes the second biggest value for deltaVsize
00169         - eventL2_: Event which makes the third biggest value for deltaVsize
00170         - eventR1_: Event which makes the second biggest value for deltaVsize
00171         - eventR2_: Event which makes the third biggest value for deltaVsize
00172         M>L1>L2 and M>R1>R2
00173                 Unknown relation between Ls and Rs events ???????
00174         Significative events for vsize:
00175         - eventT1_: Event whith the biggest value for vsize
00176         - eventT2_: Event whith the second biggest value for vsize
00177         - eventT3_: Event whith the third biggest value for vsize
00178         T1>T2>T3
00179        */
00180       SignificantEvent eventM_;  
00181       SignificantEvent eventL1_; 
00182       SignificantEvent eventL2_; 
00183       SignificantEvent eventR1_; 
00184       SignificantEvent eventR2_; 
00185       SignificantEvent eventT1_; 
00186       SignificantEvent eventT2_; 
00187       SignificantEvent eventT3_; 
00188 
00189       /*
00190         Significative event for deltaRss:
00191         - eventRssT1_: Event whith the biggest value for rss
00192         - eventRssT2_: Event whith the second biggest value for rss
00193         - eventRssT3_: Event whith the third biggest value for rss
00194         T1>T2>T3
00195         Significative events for deltaRss:
00196         - eventDeltaRssT1_: Event whith the biggest value for deltaRss
00197         - eventDeltaRssT2_: Event whith the second biggest value for deltaRss
00198         - eventDeltaRssT3_: Event whith the third biggest value for deltaRss
00199         T1>T2>T3
00200        */
00201       SignificantEvent eventRssT1_;
00202       SignificantEvent eventRssT2_;
00203       SignificantEvent eventRssT3_;
00204       SignificantEvent eventDeltaRssT1_;
00205       SignificantEvent eventDeltaRssT2_;
00206       SignificantEvent eventDeltaRssT3_;
00207 
00208 
00209       void updateEventStats(edm::EventID const & e);
00210       std::string eventStatOutput(std::string title, 
00211                                   SignificantEvent const& e) const;
00212       void eventStatOutput(std::string title, 
00213                            SignificantEvent const& e,
00214                            std::map<std::string, std::string> &m) const;
00215       std::string mallOutput(std::string title, size_t const& n) const;
00216 
00217       // Module summary statistices
00218       struct SignificantModule {
00219         int    postEarlyCount;
00220         double totalDeltaVsize;
00221         double maxDeltaVsize;
00222         edm::EventID eventMaxDeltaV;
00223         double totalEarlyVsize;
00224         double maxEarlyVsize;
00225         SignificantModule() : postEarlyCount  (0)
00226         , totalDeltaVsize (0)
00227         , maxDeltaVsize   (0)
00228         , eventMaxDeltaV  ()
00229         , totalEarlyVsize (0)
00230         , maxEarlyVsize   (0)     {}
00231         void set (double deltaV, bool early);
00232       }; // SignificantModule
00233       friend struct SignificantModule;
00234       friend std::ostream & operator<< (std::ostream & os, 
00235                 SimpleMemoryCheck::SignificantModule const & se); 
00236       bool moduleSummaryRequested_;
00237       typedef std::map<std::string,SignificantModule> SignificantModulesMap;
00238       SignificantModulesMap modules_;      
00239       double moduleEntryVsize_;
00240       edm::EventID currentEventID_;
00241       void updateModuleMemoryStats(SignificantModule & m, double dv);
00242                  
00243     }; // SimpleMemoryCheck
00244     
00245     std::ostream & 
00246     operator<< (std::ostream & os, 
00247                 SimpleMemoryCheck::SignificantEvent const & se); 
00248     
00249     std::ostream & 
00250     operator<< (std::ostream & os, 
00251                 SimpleMemoryCheck::SignificantModule const & se); 
00252     
00253   }
00254 }
00255 
00256 
00257 
00258 #endif