00001 #ifndef FUSHMBUFFER_H
00002 #define FUSHMBUFFER_H 1
00003
00004
00005 #include "EventFilter/ShmBuffer/interface/FUShmRawCell.h"
00006 #include "EventFilter/ShmBuffer/interface/FUShmRecoCell.h"
00007 #include "EventFilter/ShmBuffer/interface/FUShmDqmCell.h"
00008
00009
00010 #include <sys/ipc.h>
00011 #include <sys/types.h>
00012 #include <sys/shm.h>
00013 #include <sys/sem.h>
00014 #include <errno.h>
00015
00016
00017 namespace evf {
00018
00019
00020 namespace evt {
00021 enum State_t { EMPTY,
00022 RAWWRITING, RAWWRITTEN,
00023 RAWREADING, RAWREAD,
00024 PROCESSING, PROCESSED,
00025 RECOWRITING,RECOWRITTEN,
00026 SENDING, SENT,
00027 DISCARDING };
00028 }
00029
00030
00031 namespace dqm {
00032 enum State_t { EMPTY,
00033 WRITING, WRITTEN,
00034 SENDING, SENT,
00035 DISCARDING };
00036 }
00037
00038
00039 class FUShmBuffer
00040 {
00041
00042
00043
00044 private:
00045 FUShmBuffer(bool segmentationMode,
00046 unsigned int nRawCells,
00047 unsigned int nRecoCells,
00048 unsigned int nDqmCells,
00049 unsigned int rawCellSize,
00050 unsigned int recoCellSize,
00051 unsigned int dqmCellSize);
00052 public:
00053 ~FUShmBuffer();
00054
00055
00056 public:
00057
00058
00059
00060 void initialize(unsigned int shmid,unsigned int semid);
00061 void reset();
00062
00063 unsigned int nRawCells() const { return nRawCells_; }
00064 unsigned int nRecoCells() const { return nRecoCells_; }
00065 unsigned int nDqmCells() const { return nDqmCells_; }
00066
00067 int shmid() const { return shmid_; }
00068 int semid() const { return semid_; }
00069 unsigned int nClients() const { return nClients_; }
00070
00071 evt::State_t evtState(unsigned int index);
00072 dqm::State_t dqmState(unsigned int index);
00073
00074 unsigned int evtNumber(unsigned int index);
00075 pid_t evtPrcId(unsigned int index);
00076 time_t evtTimeStamp(unsigned int index);
00077 pid_t clientPrcId(unsigned int index);
00078
00079 int nbRawCellsToWrite() const;
00080 int nbRawCellsToRead() const;
00081
00082 FUShmRawCell* rawCellToWrite();
00083 FUShmRawCell* rawCellToRead();
00084 FUShmRecoCell* recoCellToRead();
00085 FUShmDqmCell* dqmCellToRead();
00086 FUShmRawCell* rawCellToDiscard();
00087
00088 void finishWritingRawCell(FUShmRawCell* cell);
00089 void finishReadingRawCell(FUShmRawCell* cell);
00090 void finishReadingRecoCell(FUShmRecoCell* cell);
00091 void finishReadingDqmCell(FUShmDqmCell* cell);
00092
00093 void scheduleRawCellForDiscard(unsigned int iCell);
00094
00095 void discardRawCell(FUShmRawCell* cell);
00096 void discardRecoCell(unsigned int iCell);
00097 void discardDqmCell(unsigned int iCell);
00098
00099 void releaseRawCell(FUShmRawCell* cell);
00100
00101 void writeRawEmptyEvent();
00102 void writeRecoEmptyEvent();
00103 void writeDqmEmptyEvent();
00104
00105 void scheduleRawEmptyCellForDiscard();
00106 void scheduleRawEmptyCellForDiscard(FUShmRawCell* cell);
00107
00108 bool writeRecoInitMsg(unsigned int outModId,
00109 unsigned int fuProcessId,
00110 unsigned int fuGuid,
00111 unsigned char *data,
00112 unsigned int dataSize);
00113
00114 bool writeRecoEventData(unsigned int runNumber,
00115 unsigned int evtNumber,
00116 unsigned int outModId,
00117 unsigned int fuProcessId,
00118 unsigned int fuGuid,
00119 unsigned char *data,
00120 unsigned int dataSize);
00121
00122 bool writeErrorEventData(unsigned int runNumber,
00123 unsigned int fuProcessId,
00124 unsigned int iRawCell);
00125
00126 bool writeDqmEventData(unsigned int runNumber,
00127 unsigned int evtAtUpdate,
00128 unsigned int folderId,
00129 unsigned int fuProcessId,
00130 unsigned int fuGuid,
00131 unsigned char *data,
00132 unsigned int dataSize);
00133
00134 void sem_print();
00135 void printEvtState(unsigned int index);
00136 void printDqmState(unsigned int index);
00137
00138
00139
00140
00141
00142 static FUShmBuffer* createShmBuffer(bool semgmentationMode,
00143 unsigned int nRawCells,
00144 unsigned int nRecoCells,
00145 unsigned int nDqmCells,
00146 unsigned int rawCellSize =0x400000,
00147 unsigned int recoCellSize=0x400000,
00148 unsigned int dqmCellSize =0x400000);
00149
00150 static FUShmBuffer* getShmBuffer();
00151
00152 static bool releaseSharedMemory();
00153
00154 static unsigned int size(bool segmentationMode,
00155 unsigned int nRawCells,
00156 unsigned int nRecoCells,
00157 unsigned int nDqmCells,
00158 unsigned int rawCellSize,
00159 unsigned int recoCellSize,
00160 unsigned int dqmCellSize);
00161
00162
00163 static key_t getShmDescriptorKey();
00164 static key_t getShmKey();
00165 static key_t getSemKey();
00166
00167 static int shm_create(key_t key,int size);
00168 static int shm_get(key_t key,int size);
00169 static void* shm_attach(int shmid);
00170 static int shm_nattch(int shmid);
00171 static int shm_destroy(int shmid);
00172
00173 static int sem_create(key_t key,int nsem);
00174 static int sem_get(key_t key,int nsem);
00175 static int sem_destroy(int semid);
00176
00177
00178 private:
00179
00180
00181
00182 unsigned int nextIndex(unsigned int offset,
00183 unsigned int nCells,
00184 unsigned int& iNext);
00185 void postIndex(unsigned int index,
00186 unsigned int offset,
00187 unsigned int nCells,
00188 unsigned int& iLast);
00189
00190 unsigned int nextRawWriteIndex();
00191 unsigned int nextRawReadIndex();
00192 void postRawIndexToWrite(unsigned int index);
00193 void postRawIndexToRead(unsigned int index);
00194
00195 unsigned int nextRecoWriteIndex();
00196 unsigned int nextRecoReadIndex();
00197 void postRecoIndexToWrite(unsigned int index);
00198 void postRecoIndexToRead(unsigned int index);
00199
00200 unsigned int nextDqmWriteIndex();
00201 unsigned int nextDqmReadIndex();
00202 void postDqmIndexToWrite(unsigned int index);
00203 void postDqmIndexToRead(unsigned int index);
00204
00205 unsigned int indexForEvtNumber(unsigned int evtNumber);
00206
00207 public:
00208 bool setEvtState(unsigned int index,evt::State_t state);
00209 private:
00210 bool setDqmState(unsigned int index,dqm::State_t state);
00211 bool setEvtDiscard(unsigned int index,unsigned int discard);
00212 int incEvtDiscard(unsigned int index);
00213 bool setEvtNumber(unsigned int index,unsigned int evtNumber);
00214 bool setEvtPrcId(unsigned int index,pid_t prcId);
00215 bool setEvtTimeStamp(unsigned int index,time_t timeStamp);
00216
00217 bool setClientPrcId(pid_t prcId);
00218 public:
00219 bool removeClientPrcId(pid_t prcId);
00220
00221 private:
00222 FUShmRawCell* rawCell(unsigned int iCell);
00223 FUShmRecoCell* recoCell(unsigned int iCell);
00224 FUShmDqmCell* dqmCell(unsigned int iCell);
00225
00226 bool rawCellReadyForDiscard(unsigned int index);
00227
00228 key_t shmKey(unsigned int iCell,unsigned int offset);
00229 key_t rawCellShmKey(unsigned int iCell);
00230 key_t recoCellShmKey(unsigned int iCell);
00231 key_t dqmCellShmKey(unsigned int iCell);
00232
00233 void sem_init(int isem,int value);
00234 void sem_wait(int isem);
00235 void sem_post(int isem);
00236
00237 public:
00238 void lock() { sem_wait(0); }
00239 void unlock() { sem_post(0); }
00240 private:
00241 void waitRawWrite() { sem_wait(1); }
00242 void postRawWrite() { sem_post(1); }
00243 void waitRawRead() { sem_wait(2); }
00244 void postRawRead() { sem_post(2); }
00245 void waitRawDiscard() { sem_wait(3); }
00246 void postRawDiscard() { sem_post(3); }
00247 void waitRawDiscarded() { sem_wait(4); }
00248 void postRawDiscarded() { sem_post(4); }
00249 void waitRecoWrite() { sem_wait(5); }
00250 void postRecoWrite() { sem_post(5); }
00251 void waitRecoRead() { sem_wait(6); }
00252 void postRecoRead() { sem_post(6); }
00253 void waitDqmWrite() { sem_wait(7); }
00254 void postDqmWrite() { sem_post(7); }
00255 void waitDqmRead() { sem_wait(8); }
00256 void postDqmRead() { sem_post(8); }
00257
00258
00259 private:
00260
00261
00262
00263 bool segmentationMode_;
00264 int shmid_;
00265 int semid_;
00266
00267 unsigned int rawWriteNext_;
00268 unsigned int rawWriteLast_;
00269 unsigned int rawWriteOffset_;
00270 unsigned int rawReadNext_;
00271 unsigned int rawReadLast_;
00272 unsigned int rawReadOffset_;
00273 unsigned int rawDiscardIndex_;
00274
00275 unsigned int recoWriteNext_;
00276 unsigned int recoWriteLast_;
00277 unsigned int recoWriteOffset_;
00278 unsigned int recoReadNext_;
00279 unsigned int recoReadLast_;
00280 unsigned int recoReadOffset_;
00281
00282 unsigned int dqmWriteNext_;
00283 unsigned int dqmWriteLast_;
00284 unsigned int dqmWriteOffset_;
00285 unsigned int dqmReadNext_;
00286 unsigned int dqmReadLast_;
00287 unsigned int dqmReadOffset_;
00288
00289 unsigned int evtStateOffset_;
00290 unsigned int evtDiscardOffset_;
00291 unsigned int evtNumberOffset_;
00292 unsigned int evtPrcIdOffset_;
00293 unsigned int evtTimeStampOffset_;
00294 unsigned int dqmStateOffset_;
00295
00296 unsigned int nClients_;
00297 unsigned int nClientsMax_;
00298 unsigned int clientPrcIdOffset_;
00299
00300 unsigned int nRawCells_;
00301 unsigned int rawCellPayloadSize_;
00302 unsigned int rawCellTotalSize_;
00303 unsigned int rawCellOffset_;
00304
00305 unsigned int recoWriteIndex_;
00306 unsigned int recoReadIndex_;
00307 unsigned int nRecoCells_;
00308 unsigned int recoCellPayloadSize_;
00309 unsigned int recoCellTotalSize_;
00310 unsigned int recoCellOffset_;
00311
00312 unsigned int dqmWriteIndex_;
00313 unsigned int dqmReadIndex_;
00314 unsigned int nDqmCells_;
00315 unsigned int dqmCellPayloadSize_;
00316 unsigned int dqmCellTotalSize_;
00317 unsigned int dqmCellOffset_;
00318
00319 static const char* shmKeyPath_;
00320 static const char* semKeyPath_;
00321
00322 };
00323
00324
00325 }
00326
00327
00328 #endif