CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FUShmBuffer.h
Go to the documentation of this file.
1 #ifndef FUSHMBUFFER_H
2 #define FUSHMBUFFER_H 1
3 
4 
8 
9 
10 #include <sys/ipc.h>
11 #include <sys/types.h>
12 #include <sys/shm.h>
13 #include <sys/sem.h>
14 #include <errno.h>
15 
16 #include <string>
17 
18 namespace evf {
19 
20  // define event data states
21  namespace evt {
22  enum State_t { EMPTY, STOP,
30  }
31 
32  // define dqm data states
33  namespace dqm {
34  enum State_t { EMPTY,
38  }
39 
40 
42  {
43  //
44  // construction/destruction [-> static 'createShmBuffer'/'getShmBuffer']
45  //
46  private:
47  FUShmBuffer(bool segmentationMode,
48  unsigned int nRawCells,
49  unsigned int nRecoCells,
50  unsigned int nDqmCells,
51  unsigned int rawCellSize,
52  unsigned int recoCellSize,
53  unsigned int dqmCellSize);
54  public:
55  ~FUShmBuffer();
56 
57 
58  public:
59  //
60  // public member functions
61  //
62  void initialize(unsigned int shmid,unsigned int semid);
63  void reset(bool);
64 
65  unsigned int nRawCells() const { return nRawCells_; }
66  unsigned int nRecoCells() const { return nRecoCells_; }
67  unsigned int nDqmCells() const { return nDqmCells_; }
68 
69  int shmid() const { return shmid_; }
70  int semid() const { return semid_; }
71  unsigned int nClients() const { return nClients_; }
72 
73  evt::State_t evtState(unsigned int index);
74  dqm::State_t dqmState(unsigned int index);
75 
76  unsigned int evtNumber(unsigned int index);
77  pid_t evtPrcId(unsigned int index);
78  time_t evtTimeStamp(unsigned int index);
79  pid_t clientPrcId(unsigned int index);
80 
81  unsigned int nbRawCellsToWrite() const;
82  int nbRawCellsToRead() const;
83 
89 
94 
95  void scheduleRawCellForDiscard(unsigned int iCell);
96  void scheduleRawCellForDiscardServerSide(unsigned int iCell);
97 
98  void discardRawCell(FUShmRawCell* cell);
99  void discardRecoCell(unsigned int iCell);
100  void discardOrphanedRecoCell(unsigned int iCell);
101  void discardDqmCell(unsigned int iCell);
102 
103  void releaseRawCell(FUShmRawCell* cell);
104 
105  void writeRawEmptyEvent();
106  void writeRawLumiSectionEvent(unsigned int);
107  void writeRecoEmptyEvent();
108  void writeDqmEmptyEvent();
109 
111  bool scheduleRawEmptyCellForDiscard(FUShmRawCell* cell, bool &pidstatus);
113 
114  bool writeRecoInitMsg(unsigned int outModId,
115  unsigned int fuProcessId,
116  unsigned int fuGuid,
117  unsigned char *data,
118  unsigned int dataSize,
119  unsigned int nExpectedEPs);
120 
121  bool writeRecoEventData(unsigned int runNumber,
122  unsigned int evtNumber,
123  unsigned int outModId,
124  unsigned int fuProcessId,
125  unsigned int fuGuid,
126  unsigned char *data,
127  unsigned int dataSize);
128 
129  bool writeErrorEventData(unsigned int runNumber,
130  unsigned int fuProcessId,
131  unsigned int iRawCell,
132  bool checkValue);
133 
134  bool writeDqmEventData(unsigned int runNumber,
135  unsigned int evtAtUpdate,
136  unsigned int folderId,
137  unsigned int fuProcessId,
138  unsigned int fuGuid,
139  unsigned char *data,
140  unsigned int dataSize);
141 
142  void sem_print();
144  void printEvtState(unsigned int index);
145  void printDqmState(unsigned int index);
146 
147 
148  //
149  // static member functions
150  //
151  static FUShmBuffer* createShmBuffer(bool semgmentationMode,
152  unsigned int nRawCells,
153  unsigned int nRecoCells,
154  unsigned int nDqmCells,
155  unsigned int rawCellSize =0x400000, //4MB
156  unsigned int recoCellSize=0x400000, //4MB
157  unsigned int dqmCellSize =0x400000); //4MB
158 
159  static FUShmBuffer* getShmBuffer();
160 
161  static bool releaseSharedMemory();
162 
163  static unsigned int size(bool segmentationMode,
164  unsigned int nRawCells,
165  unsigned int nRecoCells,
166  unsigned int nDqmCells,
167  unsigned int rawCellSize,
168  unsigned int recoCellSize,
169  unsigned int dqmCellSize);
170 
171 
172  static key_t getShmDescriptorKey();
173  static key_t getShmKey();
174  static key_t getSemKey();
175 
176  static int shm_create(key_t key,int size);
177  static int shm_get(key_t key,int size);
178  static void* shm_attach(int shmid);
179  static int shm_nattch(int shmid);
180  static int shm_destroy(int shmid);
181 
182  static int sem_create(key_t key,int nsem);
183  static int sem_get(key_t key,int nsem);
184  static int sem_destroy(int semid);
185 
186 
187  private:
188  //
189  // private member functions
190  //
191  unsigned int nextIndex(unsigned int offset,
192  unsigned int nCells,
193  unsigned int& iNext);
194  void postIndex(unsigned int index,
195  unsigned int offset,
196  unsigned int nCells,
197  unsigned int& iLast);
198 
199  unsigned int nextRawWriteIndex();
200  unsigned int nextRawReadIndex();
201  void postRawIndexToWrite(unsigned int index);
202  void postRawIndexToRead(unsigned int index);
203 
204  unsigned int nextRecoWriteIndex();
205  unsigned int nextRecoReadIndex();
206  void postRecoIndexToWrite(unsigned int index);
207  void postRecoIndexToRead(unsigned int index);
208 
209  unsigned int nextDqmWriteIndex();
210  unsigned int nextDqmReadIndex();
211  void postDqmIndexToWrite(unsigned int index);
212  void postDqmIndexToRead(unsigned int index);
213 
214  unsigned int indexForEvtNumber(unsigned int evtNumber);
215  unsigned int indexForEvtPrcId(pid_t evtNumber);
216 
217  public:
218  bool setEvtState(unsigned int index,evt::State_t state, bool lockShm=true);
219  bool setDqmState(unsigned int index,dqm::State_t state);
220  bool setEvtDiscard(unsigned int index,unsigned int discard, bool checkValue=false, bool lockShm=true);
221  int incEvtDiscard(unsigned int index, bool lockShm=true);
222  private:
223  bool setEvtNumber(unsigned int index,unsigned int evtNumber);
224  bool setEvtTimeStamp(unsigned int index,time_t timeStamp);
225 
226  bool setClientPrcId(pid_t prcId);
227  public:
228  bool setEvtPrcId(unsigned int index,pid_t prcId);
229  bool removeClientPrcId(pid_t prcId);
230 
231  FUShmRawCell* rawCell(unsigned int iCell);
232  FUShmRecoCell* recoCell(unsigned int iCell);
233  FUShmDqmCell* dqmCell(unsigned int iCell);
234 
235  private:
236 
237  bool rawCellReadyForDiscard(unsigned int index);
238 
239  key_t shmKey(unsigned int iCell,unsigned int offset);
240  key_t rawCellShmKey(unsigned int iCell);
241  key_t recoCellShmKey(unsigned int iCell);
242  key_t dqmCellShmKey(unsigned int iCell);
243 
244  void sem_init(int isem,int value);
245  int sem_wait(int isem);
246  void sem_post(int isem);
247 
248  public:
249  void lock() { sem_wait(0); }
250  void unlock() { sem_post(0); }
251  private:
252  int waitRawWrite() { return sem_wait(1); }
253  void postRawWrite() { sem_post(1); }
254  void waitRawRead() { sem_wait(2); }
255  void postRawRead() { sem_post(2); }
256  void waitRawDiscard() { sem_wait(3); }
257  void postRawDiscard() { sem_post(3); }
258  void waitRawDiscarded() { sem_wait(4); }
259  void postRawDiscarded() { sem_post(4); }
260  void waitRecoWrite() { sem_wait(5); }
261  void postRecoWrite() { sem_post(5); }
262  void waitRecoRead() { sem_wait(6); }
263  void postRecoRead() { sem_post(6); }
264  void waitDqmWrite() { sem_wait(7); }
265  void postDqmWrite() { sem_post(7); }
266  void waitDqmRead() { sem_wait(8); }
267  void postDqmRead() { sem_post(8); }
268 
269 
270  private:
271  //
272  // member data
273  //
275  int shmid_;
276  int semid_;
277 
278  unsigned int rawWriteNext_;
279  unsigned int rawWriteLast_;
280  unsigned int rawWriteOffset_;
281  unsigned int rawReadNext_;
282  unsigned int rawReadLast_;
283  unsigned int rawReadOffset_;
284  unsigned int rawDiscardIndex_;
285 
286  unsigned int recoWriteNext_;
287  unsigned int recoWriteLast_;
288  unsigned int recoWriteOffset_;
289  unsigned int recoReadNext_;
290  unsigned int recoReadLast_;
291  unsigned int recoReadOffset_;
292 
293  unsigned int dqmWriteNext_;
294  unsigned int dqmWriteLast_;
295  unsigned int dqmWriteOffset_;
296  unsigned int dqmReadNext_;
297  unsigned int dqmReadLast_;
298  unsigned int dqmReadOffset_;
299 
300  unsigned int evtStateOffset_;
301  unsigned int evtDiscardOffset_;
302  unsigned int evtNumberOffset_;
303  unsigned int evtPrcIdOffset_;
304  unsigned int evtTimeStampOffset_;
305  unsigned int dqmStateOffset_;
306 
307  unsigned int nClients_;
308  unsigned int nClientsMax_;
309  unsigned int clientPrcIdOffset_;
310 
311  unsigned int nRawCells_;
312  unsigned int rawCellPayloadSize_;
313  unsigned int rawCellTotalSize_;
314  unsigned int rawCellOffset_;
315 
316  unsigned int recoWriteIndex_;
317  unsigned int recoReadIndex_;
318  unsigned int nRecoCells_;
319  unsigned int recoCellPayloadSize_;
320  unsigned int recoCellTotalSize_;
321  unsigned int recoCellOffset_;
322 
323  unsigned int dqmWriteIndex_;
324  unsigned int dqmReadIndex_;
325  unsigned int nDqmCells_;
326  unsigned int dqmCellPayloadSize_;
327  unsigned int dqmCellTotalSize_;
328  unsigned int dqmCellOffset_;
329 
330  static const char* shmKeyPath_;
331  static const char* semKeyPath_;
332 
333  };
334 
335 
336 } // namespace evf
337 
338 
339 #endif
void discardRecoCell(unsigned int iCell)
Definition: FUShmBuffer.cc:501
unsigned int clientPrcIdOffset_
Definition: FUShmBuffer.h:309
key_t recoCellShmKey(unsigned int iCell)
FUShmRawCell * rawCellToRead()
Definition: FUShmBuffer.cc:317
FUShmRawCell * rawCellToDiscard()
Definition: FUShmBuffer.cc:373
void postRawIndexToRead(unsigned int index)
unsigned int nRecoCells() const
Definition: FUShmBuffer.h:66
void waitRecoWrite()
Definition: FUShmBuffer.h:260
void writeRawLumiSectionEvent(unsigned int)
Definition: FUShmBuffer.cc:592
static int shm_nattch(int shmid)
bool setEvtPrcId(unsigned int index, pid_t prcId)
unsigned int dqmWriteNext_
Definition: FUShmBuffer.h:293
static void * shm_attach(int shmid)
void postDqmIndexToWrite(unsigned int index)
unsigned int rawReadOffset_
Definition: FUShmBuffer.h:283
unsigned int nextRecoReadIndex()
unsigned int dqmReadLast_
Definition: FUShmBuffer.h:297
unsigned int recoCellPayloadSize_
Definition: FUShmBuffer.h:319
unsigned int dqmReadOffset_
Definition: FUShmBuffer.h:298
bool writeRecoEventData(unsigned int runNumber, unsigned int evtNumber, unsigned int outModId, unsigned int fuProcessId, unsigned int fuGuid, unsigned char *data, unsigned int dataSize)
Definition: FUShmBuffer.cc:714
bool writeErrorEventData(unsigned int runNumber, unsigned int fuProcessId, unsigned int iRawCell, bool checkValue)
Definition: FUShmBuffer.cc:746
unsigned int nextIndex(unsigned int offset, unsigned int nCells, unsigned int &iNext)
bool writeDqmEventData(unsigned int runNumber, unsigned int evtAtUpdate, unsigned int folderId, unsigned int fuProcessId, unsigned int fuGuid, unsigned char *data, unsigned int dataSize)
Definition: FUShmBuffer.cc:820
int sem_wait(int isem)
void scheduleRawCellForDiscardServerSide(unsigned int iCell)
Definition: FUShmBuffer.cc:478
unsigned int dqmWriteLast_
Definition: FUShmBuffer.h:294
static FUShmBuffer * getShmBuffer()
Definition: FUShmBuffer.cc:986
int nbRawCellsToRead() const
Definition: FUShmBuffer.cc:293
void printDqmState(unsigned int index)
Definition: FUShmBuffer.cc:911
void releaseRawCell(FUShmRawCell *cell)
Definition: FUShmBuffer.cc:546
int shmid() const
Definition: FUShmBuffer.h:69
unsigned int evtDiscardOffset_
Definition: FUShmBuffer.h:301
unsigned int nRecoCells_
Definition: FUShmBuffer.h:318
static const char * semKeyPath_
Definition: FUShmBuffer.h:331
unsigned int dqmStateOffset_
Definition: FUShmBuffer.h:305
pid_t clientPrcId(unsigned int index)
static key_t getShmDescriptorKey()
static int shm_get(key_t key, int size)
unsigned int dqmWriteOffset_
Definition: FUShmBuffer.h:295
unsigned int nRawCells_
Definition: FUShmBuffer.h:311
unsigned int recoWriteLast_
Definition: FUShmBuffer.h:287
static bool releaseSharedMemory()
unsigned int dqmWriteIndex_
Definition: FUShmBuffer.h:323
static int sem_create(key_t key, int nsem)
bool removeClientPrcId(pid_t prcId)
void waitRawDiscard()
Definition: FUShmBuffer.h:256
static unsigned int size(bool segmentationMode, unsigned int nRawCells, unsigned int nRecoCells, unsigned int nDqmCells, unsigned int rawCellSize, unsigned int recoCellSize, unsigned int dqmCellSize)
FUShmDqmCell * dqmCellToRead()
Definition: FUShmBuffer.cc:354
unsigned int evtNumberOffset_
Definition: FUShmBuffer.h:302
unsigned int dqmCellOffset_
Definition: FUShmBuffer.h:328
evt::State_t evtState(unsigned int index)
bool writeRecoInitMsg(unsigned int outModId, unsigned int fuProcessId, unsigned int fuGuid, unsigned char *data, unsigned int dataSize, unsigned int nExpectedEPs)
Definition: FUShmBuffer.cc:693
void discardDqmCell(unsigned int iCell)
Definition: FUShmBuffer.cc:527
unsigned int nextDqmReadIndex()
time_t evtTimeStamp(unsigned int index)
unsigned int evtNumber(unsigned int index)
unsigned int recoWriteNext_
Definition: FUShmBuffer.h:286
void initialize(unsigned int shmid, unsigned int semid)
Definition: FUShmBuffer.cc:162
void writeRecoEmptyEvent()
Definition: FUShmBuffer.cc:614
bool setEvtNumber(unsigned int index, unsigned int evtNumber)
void postDqmIndexToRead(unsigned int index)
unsigned int evtStateOffset_
Definition: FUShmBuffer.h:300
void discardOrphanedRecoCell(unsigned int iCell)
Definition: FUShmBuffer.cc:517
bool setClientPrcId(pid_t prcId)
std::string sem_print_s()
Definition: FUShmBuffer.cc:863
void discardRawCell(FUShmRawCell *cell)
Definition: FUShmBuffer.cc:495
void postRawIndexToWrite(unsigned int index)
unsigned int nextRawReadIndex()
FUShmRawCell * rawCell(unsigned int iCell)
unsigned int recoReadOffset_
Definition: FUShmBuffer.h:291
unsigned int nDqmCells_
Definition: FUShmBuffer.h:325
unsigned int nClients() const
Definition: FUShmBuffer.h:71
static const char * shmKeyPath_
Definition: FUShmBuffer.h:330
unsigned int rawWriteNext_
Definition: FUShmBuffer.h:278
unsigned int recoWriteIndex_
Definition: FUShmBuffer.h:316
pid_t evtPrcId(unsigned int index)
void finishWritingRawCell(FUShmRawCell *cell)
Definition: FUShmBuffer.cc:393
unsigned int nbRawCellsToWrite() const
Definition: FUShmBuffer.cc:288
bool setEvtDiscard(unsigned int index, unsigned int discard, bool checkValue=false, bool lockShm=true)
key_t rawCellShmKey(unsigned int iCell)
static int shm_destroy(int shmid)
unsigned int nRawCells() const
Definition: FUShmBuffer.h:65
bool setEvtState(unsigned int index, evt::State_t state, bool lockShm=true)
FUShmRecoCell * recoCell(unsigned int iCell)
void writeRawEmptyEvent()
Definition: FUShmBuffer.cc:572
bool rawCellReadyForDiscard(unsigned int index)
unsigned int offset(bool)
void postRawDiscard()
Definition: FUShmBuffer.h:257
unsigned int dqmCellPayloadSize_
Definition: FUShmBuffer.h:326
static FUShmBuffer * createShmBuffer(bool semgmentationMode, unsigned int nRawCells, unsigned int nRecoCells, unsigned int nDqmCells, unsigned int rawCellSize=0x400000, unsigned int recoCellSize=0x400000, unsigned int dqmCellSize=0x400000)
Definition: FUShmBuffer.cc:917
void finishReadingDqmCell(FUShmDqmCell *cell)
Definition: FUShmBuffer.cc:438
void scheduleRawEmptyCellForDiscard()
Definition: FUShmBuffer.cc:638
unsigned int recoReadLast_
Definition: FUShmBuffer.h:290
void finishReadingRecoCell(FUShmRecoCell *cell)
Definition: FUShmBuffer.cc:426
unsigned int nextDqmWriteIndex()
key_t dqmCellShmKey(unsigned int iCell)
dqm::State_t dqmState(unsigned int index)
static key_t getShmKey()
unsigned int rawReadNext_
Definition: FUShmBuffer.h:281
unsigned int rawCellPayloadSize_
Definition: FUShmBuffer.h:312
unsigned int nextRecoWriteIndex()
unsigned int rawDiscardIndex_
Definition: FUShmBuffer.h:284
unsigned int nextRawWriteIndex()
FUShmRawCell * rawCellToWrite()
Definition: FUShmBuffer.cc:298
unsigned int rawReadLast_
Definition: FUShmBuffer.h:282
unsigned int rawWriteOffset_
Definition: FUShmBuffer.h:280
unsigned int dqmCellTotalSize_
Definition: FUShmBuffer.h:327
unsigned int dqmReadIndex_
Definition: FUShmBuffer.h:324
void reset(bool)
Definition: FUShmBuffer.cc:201
bool setEvtTimeStamp(unsigned int index, time_t timeStamp)
unsigned int rawWriteLast_
Definition: FUShmBuffer.h:279
void writeDqmEmptyEvent()
Definition: FUShmBuffer.cc:626
unsigned int dqmReadNext_
Definition: FUShmBuffer.h:296
unsigned int recoCellTotalSize_
Definition: FUShmBuffer.h:320
void waitRawDiscarded()
Definition: FUShmBuffer.h:258
void postRecoIndexToRead(unsigned int index)
char state
Definition: procUtils.cc:75
unsigned int indexForEvtNumber(unsigned int evtNumber)
static key_t getSemKey()
void printEvtState(unsigned int index)
Definition: FUShmBuffer.cc:878
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void postRawDiscarded()
Definition: FUShmBuffer.h:259
unsigned int nClientsMax_
Definition: FUShmBuffer.h:308
list key
Definition: combine.py:13
static int sem_get(key_t key, int nsem)
void sem_post(int isem)
FUShmRecoCell * recoCellToRead()
Definition: FUShmBuffer.cc:340
unsigned int rawCellTotalSize_
Definition: FUShmBuffer.h:313
FUShmBuffer(bool segmentationMode, unsigned int nRawCells, unsigned int nRecoCells, unsigned int nDqmCells, unsigned int rawCellSize, unsigned int recoCellSize, unsigned int dqmCellSize)
Definition: FUShmBuffer.cc:53
static int sem_destroy(int semid)
static int shm_create(key_t key, int size)
unsigned int nDqmCells() const
Definition: FUShmBuffer.h:67
void sem_init(int isem, int value)
unsigned int evtPrcIdOffset_
Definition: FUShmBuffer.h:303
unsigned int recoWriteOffset_
Definition: FUShmBuffer.h:288
int incEvtDiscard(unsigned int index, bool lockShm=true)
void postRecoWrite()
Definition: FUShmBuffer.h:261
unsigned int nClients_
Definition: FUShmBuffer.h:307
unsigned int rawCellOffset_
Definition: FUShmBuffer.h:314
unsigned int recoReadNext_
Definition: FUShmBuffer.h:289
unsigned int indexForEvtPrcId(pid_t evtNumber)
unsigned int recoReadIndex_
Definition: FUShmBuffer.h:317
key_t shmKey(unsigned int iCell, unsigned int offset)
void finishReadingRawCell(FUShmRawCell *cell)
Definition: FUShmBuffer.cc:410
unsigned int recoCellOffset_
Definition: FUShmBuffer.h:321
std::string timeStamp(TimePoint_t)
Definition: Utils.cc:23
void postRecoIndexToWrite(unsigned int index)
bool setDqmState(unsigned int index, dqm::State_t state)
void postIndex(unsigned int index, unsigned int offset, unsigned int nCells, unsigned int &iLast)
FUShmDqmCell * dqmCell(unsigned int iCell)
void scheduleRawEmptyCellForDiscardServerSide(FUShmRawCell *cell)
Definition: FUShmBuffer.cc:675
unsigned int evtTimeStampOffset_
Definition: FUShmBuffer.h:304
void scheduleRawCellForDiscard(unsigned int iCell)
Definition: FUShmBuffer.cc:454
int semid() const
Definition: FUShmBuffer.h:70