00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "EventFilter/Utilities/interface/i2oEvfMsgs.h"
00011 #include "IOPool/Streamer/interface/EventMessage.h"
00012 #include "EventFilter/Modules/src/FUShmOutputModule.h"
00013 #include "DataFormats/Provenance/interface/EventID.h"
00014
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 #include "FWCore/Utilities/interface/DebugMacros.h"
00017 #include "FWCore/Utilities/interface/Exception.h"
00018 #include "FWCore/Utilities/src/Guid.h"
00019
00020 #include "xdaq/Application.h"
00021 #include "xdaq/ApplicationContext.h"
00022 #include "xdaq/ApplicationGroup.h"
00023 #include "zlib.h"
00024
00025 #include <string>
00026 #include <fstream>
00027 #include <iostream>
00028
00029 using namespace edm;
00030 using namespace std;
00031
00032 static SM_SharedMemoryHandle sm_sharedmemory;
00033
00034 namespace edm
00035 {
00036
00040 bool FUShmOutputModule::fuIdsInitialized_ = false;
00041 uint32 FUShmOutputModule::fuProcId_ = 0;
00042 uint32 FUShmOutputModule::fuGuidValue_ = 0;
00043
00044 FUShmOutputModule::FUShmOutputModule(edm::ParameterSet const& ps):
00045 shmBuffer_(0)
00046 {
00047 FDEBUG(9) << "FUShmOutputModule: constructor" << endl;
00048
00049 if (! fuIdsInitialized_) {
00050 fuIdsInitialized_ = true;
00051
00052 edm::Guid guidObj(true);
00053 std::string guidString = guidObj.toString();
00054
00055
00056 uLong crc = crc32(0L, Z_NULL, 0);
00057 Bytef* buf = (Bytef*)guidString.data();
00058 crc = crc32(crc, buf, guidString.length());
00059 fuGuidValue_ = crc;
00060
00061 fuProcId_ = getpid();
00062
00063
00064 }
00065 }
00066
00067 FUShmOutputModule::~FUShmOutputModule()
00068 {
00069 FDEBUG(9) << "FUShmOutputModule: FUShmOutputModule destructor" << endl;
00070 sm_sharedmemory.detachShmBuffer();
00071
00072 }
00073
00074 void FUShmOutputModule::doOutputHeader(InitMsgBuilder const& initMessage)
00075 {
00076
00077 if(!shmBuffer_) shmBuffer_ = sm_sharedmemory.getShmBuffer();
00078 if(!shmBuffer_) edm::LogError("FUShmOutputModule")
00079 << " Error getting shared memory buffer for INIT. "
00080 << " Make sure you configure the ResourceBroker before the FUEventProcessor! "
00081 << " No INIT is sent - this is probably fatal!";
00082 if(shmBuffer_)
00083 {
00084 unsigned char* buffer = (unsigned char*) initMessage.startAddress();
00085 unsigned int size = initMessage.size();
00086 FDEBUG(10) << "writing out INIT message with size = " << size << std::endl;
00087
00088 InitMsgView dummymsg(buffer);
00089 uint32 dmoduleId = dummymsg.outputModuleId();
00090
00091
00092 bool ret = sm_sharedmemory.getShmBuffer()->writeRecoInitMsg(dmoduleId, fuProcId_, fuGuidValue_, buffer, size);
00093 if(!ret) edm::LogError("FUShmOutputModule") << " Error writing preamble to ShmBuffer";
00094 }
00095 }
00096
00097 void FUShmOutputModule::doOutputEvent(EventMsgBuilder const& eventMessage)
00098 {
00099 if(!shmBuffer_) edm::LogError("FUShmOutputModule")
00100 << " Invalid shared memory buffer at first event"
00101 << " Make sure you configure the ResourceBroker before the FUEventProcessor! "
00102 << " No event is sent - this is fatal! Should throw here";
00103 else
00104 {
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
00113 bool ret = sm_sharedmemory.getShmBuffer()->writeRecoEventData(runid, eventid, outModId, fuProcId_, fuGuidValue_, buffer, size);
00114 if(!ret) edm::LogError("FUShmOutputModule") << " Error with writing data to ShmBuffer";
00115 }
00116 }
00117
00118 void FUShmOutputModule::start()
00119 {
00120
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
00132 shmBuffer_ = 0;
00133 }
00134 }
00135
00136 }