CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DataFormats/GeometrySurface/plugins/BlockWipedAllocatorService.cc

Go to the documentation of this file.
00001 #include "FWCore/PluginManager/interface/ModuleDef.h"
00002 #include "FWCore/ServiceRegistry/interface/ServiceMaker.h"
00003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00004 
00005 #include "DataFormats/GeometrySurface/interface/BlockWipedAllocator.h"
00006 #include <iostream>
00007 
00008 
00009 #include "DataFormats/GeometrySurface/interface/ReferenceCounted.h"
00010 
00011 
00012 namespace {
00013   struct Dumper {
00014     void visit(BlockWipedAllocator const& alloc) const {
00015       BlockWipedAllocator::Stat sa1 = alloc.stat();
00016       std::cout << "Alloc for size " << sa1.typeSize
00017                 << ": " << sa1.blockSize
00018                 << ", " << sa1.currentOccupancy
00019                 << "/" << sa1.currentAvailable
00020                 << ", " << sa1.totalAvailable
00021                 << "/" << sa1.nBlocks
00022                 << ", " << sa1.alive
00023                 << std::endl;
00024     }
00025     
00026   };
00027   
00028 }
00029 
00032 class BlockWipedAllocatorService {
00033 private:
00034   bool m_useAlloc;
00035   bool m_silent;
00036   bool m_dump;
00037   int  m_clearFreq;
00038   BlockWipedPool pool;
00039 public:
00040   BlockWipedAllocatorService(const edm::ParameterSet & iConfig,
00041                              edm::ActivityRegistry & iAR ) :
00042     pool( std::max(256,iConfig.getUntrackedParameter<int>("blockSize",1024)),
00043           std::max(256,iConfig.getUntrackedParameter<int>("recyclerSize",4096))
00044           ) {
00045     
00046     m_useAlloc = iConfig.getUntrackedParameter<bool>("usePoolAllocator",false);
00047     m_silent = iConfig.getUntrackedParameter<bool>("silent",true);
00048     m_dump = iConfig.getUntrackedParameter<bool>("dumpEachModule",false);
00049     m_clearFreq = std::max(1,iConfig.getUntrackedParameter<int>("clearFrequency",20));
00050 
00051     blockWipedPool(&pool);
00052     if (m_useAlloc) BlockWipedPoolAllocated::usePool();
00053     iAR.watchPreSource(this,&BlockWipedAllocatorService::preSource);
00054     iAR.watchPreProcessEvent(this,&BlockWipedAllocatorService::preEventProcessing);
00055     iAR.watchPostEndJob(this,&BlockWipedAllocatorService::postEndJob);
00056     iAR.watchPreModule(this,&BlockWipedAllocatorService::preModule);
00057     iAR.watchPostModule(this,&BlockWipedAllocatorService::postModule);
00058   }
00059 
00060   // wipe the workspace before each event
00061   void preEventProcessing(const edm::EventID&, const edm::Timestamp&) { wiper();}
00062 
00063   // nope event-principal deleted in source
00064   void preSource() {
00065    // wiper();
00066   }
00067 
00068   void dump() {
00069     if (m_silent) return;
00070     std::cout << "ReferenceCounted stat"<< std::endl;
00071     std::cout << "still alive/referenced " 
00072               << ReferenceCountedPoolAllocated::s_alive
00073               << "/" << ReferenceCountedPoolAllocated::s_referenced
00074               << std::endl;
00075 
00076     std::cout << "BlockAllocator stat"<< std::endl;
00077     std::cout << "still alive " << BlockWipedPoolAllocated::s_alive << std::endl;
00078     Dumper dumper;
00079     blockWipedPool().visit(dumper);
00080   }
00081 
00082 
00083   void wiper() {
00084     dump();
00085     blockWipedPool().wipe();
00086     // blockWipedPool().clear();  // try to crash
00087     {
00088        static int c=0;
00089        c++;
00090        if (m_clearFreq==c) {
00091          blockWipedPool().clear();
00092          c=0;
00093        }
00094     }
00095 
00096   }
00097  
00098   // wipe before each module (no, obj in event....)
00099   void preModule(const edm::ModuleDescription& desc){
00100     blockWipedPool().wipe(false);
00101   }
00102 
00103   void postModule(const edm::ModuleDescription& desc){
00104     if (m_dump) dump();
00105     }
00106 
00107   // final stat
00108   void postEndJob() {
00109     wiper();
00110   }
00111   
00112 };
00113 
00114 
00115 
00116 DEFINE_FWK_SERVICE(BlockWipedAllocatorService);