CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/EventFilter/Modules/src/FUShmOutputModule.cc

Go to the documentation of this file.
00001 /*
00002    Description:
00003      EDM output module that will write data to shared memory for 
00004      the resource broker to send to the Storage Manager.
00005      See the CMS EvF Storage Manager wiki page for further notes.
00006 
00007    $Id: FUShmOutputModule.cc,v 1.13 2011/04/14 15:24:51 mommsen Exp $
00008 */
00009 
00010 #include "EventFilter/Utilities/interface/i2oEvfMsgs.h"
00011 #include "EventFilter/Utilities/interface/ShmOutputModuleRegistry.h"
00012 #include "IOPool/Streamer/interface/EventMessage.h"
00013 #include "EventFilter/Modules/src/FUShmOutputModule.h"
00014 #include "DataFormats/Provenance/interface/EventID.h"
00015 
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018 #include "FWCore/Utilities/interface/DebugMacros.h"
00019 #include "FWCore/Utilities/interface/Exception.h"
00020 #include "FWCore/Utilities/src/Guid.h"
00021 
00022 #include "xdaq/Application.h"
00023 #include "xdaq/ApplicationContext.h"
00024 #include "xdaq/ApplicationGroup.h"
00025 #include "zlib.h"
00026 
00027 #include <string>
00028 #include <fstream>
00029 #include <iostream>
00030 
00031 using namespace edm;
00032 using namespace std;
00033 
00034 static SM_SharedMemoryHandle sm_sharedmemory;
00035 
00036 namespace edm
00037 {
00038 
00042   bool FUShmOutputModule::fuIdsInitialized_ = false;
00043   uint32 FUShmOutputModule::fuGuidValue_ = 0;
00044 
00045   FUShmOutputModule::FUShmOutputModule(edm::ParameterSet const& ps):
00046     shmBuffer_(0)
00047     , name_(ps.getParameter<std::string>( "@module_label" ))
00048     , count_(0)
00049   {
00050     FDEBUG(9) << "FUShmOutputModule: constructor" << endl;
00051     if(edm::Service<evf::ShmOutputModuleRegistry>())
00052       edm::Service<evf::ShmOutputModuleRegistry>()->registerModule(name_, this);  
00053     if (! fuIdsInitialized_) {
00054       fuIdsInitialized_ = true;
00055 
00056       edm::Guid guidObj(true);
00057       std::string guidString = guidObj.toString();
00058 
00059       uLong crc = crc32(0L, Z_NULL, 0);
00060       Bytef* buf = (Bytef*)guidString.data();
00061       crc = crc32(crc, buf, guidString.length());
00062       fuGuidValue_ = crc;
00063     }
00064   }
00065   
00066   FUShmOutputModule::~FUShmOutputModule()
00067   {
00068     FDEBUG(9) << "FUShmOutputModule: FUShmOutputModule destructor" << endl;
00069     sm_sharedmemory.detachShmBuffer();
00070     //shmdt(shmBuffer_);
00071   }
00072 
00073   void FUShmOutputModule::doOutputHeader(InitMsgBuilder const& initMessage)
00074   {
00075     count_ = 0;
00076     if(!shmBuffer_) shmBuffer_ = sm_sharedmemory.getShmBuffer();
00077     if(!shmBuffer_) edm::LogError("FUShmOutputModule") 
00078       << " Error getting shared memory buffer for INIT. " 
00079       << " Make sure you configure the ResourceBroker before the FUEventProcessor! "
00080       << " No INIT is sent - this is probably fatal!";
00081     if(shmBuffer_)
00082     {
00083       unsigned char* buffer = (unsigned char*) initMessage.startAddress();
00084       unsigned int size = initMessage.size();
00085       FDEBUG(10) << "writing out INIT message with size = " << size << std::endl;
00086       // no method in InitMsgBuilder to get the output module id, recast
00087       InitMsgView dummymsg(buffer);
00088       uint32 dmoduleId = dummymsg.outputModuleId();
00089 
00090       //bool ret = shmBuffer_->writeRecoInitMsg(dmoduleId, buffer, size);
00091       bool ret = sm_sharedmemory.getShmBuffer()->writeRecoInitMsg(dmoduleId, getpid(), fuGuidValue_, buffer, size);
00092       if(!ret) edm::LogError("FUShmOutputModule") << " Error writing preamble to ShmBuffer";
00093     }
00094   }
00095 
00096   void FUShmOutputModule::doOutputEvent(EventMsgBuilder const& eventMessage)
00097   {
00098     if(!shmBuffer_) edm::LogError("FUShmOutputModule") 
00099       << " Invalid shared memory buffer at first event"
00100       << " Make sure you configure the ResourceBroker before the FUEventProcessor! "
00101       << " No event is sent - this is fatal! Should throw here";
00102     else
00103     {
00104       count_++;
00105       unsigned char* buffer = (unsigned char*) eventMessage.startAddress();
00106       unsigned int size = eventMessage.size();
00107       EventMsgView eventView(eventMessage.startAddress());
00108       unsigned int runid = eventView.run();
00109       unsigned int eventid = eventView.event();
00110       unsigned int outModId = eventView.outModId();
00111       FDEBUG(10) << "FUShmOutputModule: event size = " << size << std::endl;
00112       //bool ret = shmBuffer_->writeRecoEventData(runid, eventid, outModId, buffer, size);
00113       bool ret = sm_sharedmemory.getShmBuffer()->writeRecoEventData(runid, eventid, outModId, getpid(), fuGuidValue_, buffer, size);
00114       if(!ret) edm::LogError("FUShmOutputModule") << " Error with writing data to ShmBuffer";
00115     }
00116   }
00117 
00118   void FUShmOutputModule::start()
00119   {
00120     //shmBuffer_ = evf::FUShmBuffer::getShmBuffer();
00121     shmBuffer_ = sm_sharedmemory.getShmBuffer();
00122     if(0==shmBuffer_) 
00123       edm::LogError("FUShmOutputModule")<<"Failed to attach to shared memory";
00124   }
00125 
00126   void FUShmOutputModule::stop()
00127   {
00128     FDEBUG(9) << "FUShmOutputModule: sending terminate run" << std::endl;
00129     if(0!=shmBuffer_){
00130       sm_sharedmemory.detachShmBuffer();
00131       //shmdt(shmBuffer_);
00132       shmBuffer_ = 0;
00133     }
00134   }
00135 
00136 }