Belle II Software development
RingBuffer.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <sys/ipc.h>
12#include <string>
13
14namespace Belle2 {
21 struct RingBufInfo {
22 int size;
23 int remain;
24 int wptr;
26 int rptr;
27 int nbuf;
28 int semid;
30 int nbusy;
32 int errtype;
34 int ninsq;
35 int nremq;
36 };
37
39 class RingBuffer {
40 public:
42 const static int c_DefaultSize = 15000000;
43
48 explicit RingBuffer(int nwords = c_DefaultSize);
50 explicit RingBuffer(const std::string& name, unsigned int nwords = 0); // Create / Attach Ring buffer
54 void openSHM(int nwords);
56 void cleanup();
57
59 int insq(const int* buf, int size, bool checkTx = false);
61 int remq(int* buf);
63 int spyq(int* buf) const;
65 int numq() const;
66
68 void txAttached();
70 void txDetached();
72 void kill();
73
75 bool isDead() const;
80 bool allRxWaiting() const;
81
83 int clear();
85 void forceClear();
86
88 int tryClear();
89
91 int shmid() const;
92
93 // Debugging functions
95 void dump_db();
97 int ninsq() const;
99 int nremq() const;
100
102 int insq_counter() const;
104 int remq_counter() const;
105
107 void dumpInfo() const;
108
109 private:
110 bool m_new{true};
111 bool m_file{false};
112 std::string m_pathname{""};
113 int m_pathfd{ -1};
114 key_t m_shmkey{IPC_PRIVATE};
115 key_t m_semkey{IPC_PRIVATE};
117 std::string m_semshmFileName{""};
118
124 bool m_procIsBusy{false};
125
126 int m_shmid{ -1};
127 int* m_shmadr{nullptr};
128 int m_shmsize{ -1};
129 struct RingBufInfo* m_bufinfo {nullptr};
130 int* m_buftop{nullptr};
131 int m_semid{ -1};
134 };
135
137}
Class to manage a Ring Buffer placed in an IPC shared memory.
Definition: RingBuffer.h:39
void dump_db()
Print some info on the RingBufInfo structure.
Definition: RingBuffer.cc:182
~RingBuffer()
Destructor.
Definition: RingBuffer.cc:79
int insq(const int *buf, int size, bool checkTx=false)
Append a buffer to the RingBuffer.
Definition: RingBuffer.cc:189
int m_shmsize
Size of shared memory segment, in bytes.
Definition: RingBuffer.h:128
struct RingBufInfo * m_bufinfo
structure to manage ring buffer.
Definition: RingBuffer.h:129
int tryClear()
Clear the RingBuffer, if the semaphore isn't locked at the moment.
Definition: RingBuffer.cc:450
bool m_file
True if m_pathfd needs to be closed.
Definition: RingBuffer.h:111
int remq_counter() const
Return number of remq() calls.
Definition: RingBuffer.cc:421
int clear()
Clear the RingBuffer.
Definition: RingBuffer.cc:426
void txAttached()
Increase the number of attached Tx counter.
Definition: RingBuffer.cc:373
int spyq(int *buf) const
Prefetch a buffer from the RingBuffer w/o removing it.
Definition: RingBuffer.cc:348
void cleanup()
Function to detach and remove shared memory.
Definition: RingBuffer.cc:162
int m_remq_counter
count remq() calls.
Definition: RingBuffer.h:132
void txDetached()
Decrease the number of attached Tx counter.
Definition: RingBuffer.cc:381
key_t m_shmkey
SHM key, see shmget(2).
Definition: RingBuffer.h:114
int m_semid
Semaphore ID.
Definition: RingBuffer.h:131
int shmid() const
Return ID of the shared memory.
Definition: RingBuffer.cc:458
void dumpInfo() const
Dump contents of RingBufInfo metadata.
Definition: RingBuffer.cc:463
static const int c_DefaultSize
Standard size of buffer, in integers (~60MB).
Definition: RingBuffer.h:42
int numq() const
Returns number of entries/buffers in the RingBuffer.
Definition: RingBuffer.cc:368
int m_shmid
ID of shared memory segment.
Definition: RingBuffer.h:126
bool allRxWaiting() const
True if and only if buffer is empty and nbusy == 0.
Definition: RingBuffer.cc:400
void kill()
Cause termination of reading processes (if they use isDead()).
Definition: RingBuffer.cc:389
int remq(int *buf)
Pick up a buffer from the RingBuffer.
Definition: RingBuffer.cc:308
int m_pathfd
Associated file descriptor.
Definition: RingBuffer.h:113
int insq_counter() const
Return number of insq() calls.
Definition: RingBuffer.cc:416
std::string m_pathname
Path for identifying shared memory if named ring buffer is created.
Definition: RingBuffer.h:112
void openSHM(int nwords)
open shared memory
Definition: RingBuffer.cc:84
bool isDead() const
If True, the ring buffer is empty and has no attached Tx modules (i.e.
Definition: RingBuffer.cc:394
bool m_procIsBusy
Is this process currently processing events from this RingBuffer?
Definition: RingBuffer.h:124
int * m_buftop
Points to memory after the end of m_bufinfo.
Definition: RingBuffer.h:130
int m_insq_counter
count insq() calls.
Definition: RingBuffer.h:133
std::string m_semshmFileName
file path containing ids of shm and sema for private shared mem, used for easier cleanup if we fail t...
Definition: RingBuffer.h:117
bool m_new
True if we created the ring buffer ourselves (and need to clean it).
Definition: RingBuffer.h:110
int ninsq() const
Return number of insq() calls for current buffer.
Definition: RingBuffer.cc:406
void forceClear()
Forcefully clear the RingBuffer with resetting semaphore.
Definition: RingBuffer.cc:441
key_t m_semkey
Semaphore key, see semget(2).
Definition: RingBuffer.h:115
int nremq() const
Return number of remq() calls for current buffer.
Definition: RingBuffer.cc:411
int * m_shmadr
Address of attached shared memory segment.
Definition: RingBuffer.h:127
Abstract base class for different kinds of events.
Internal metadata structure for RingBuffer.
Definition: RingBuffer.h:21
int numAttachedTx
number of attached sending processes.
Definition: RingBuffer.h:33
int errtype
Error state? 0: Normal, 1: buffer full and wptr>rptr, others are complicated.
Definition: RingBuffer.h:32
int wptr
Pointer for writing entries.
Definition: RingBuffer.h:24
int __readbuf
Unused.
Definition: RingBuffer.h:31
int size
ring buffer size (integers), minus this header.
Definition: RingBuffer.h:22
int semid
Semaphore ID.
Definition: RingBuffer.h:28
int nattached
Number of RingBuffer instances currently attached to this buffer.
Definition: RingBuffer.h:29
int nbusy
Number of attached reading processes currently processing events.
Definition: RingBuffer.h:30
int nremq
Count remq() calls for this buffer.
Definition: RingBuffer.h:35
int nbuf
Number of entries in ring buffer.
Definition: RingBuffer.h:27
int rptr
Pointer for reading entries.
Definition: RingBuffer.h:26
int remain
Unsure, always equal to size.
Definition: RingBuffer.h:23
int prevwptr
Previous state of wptr (for error recovery).
Definition: RingBuffer.h:25
int ninsq
Count insq() calls for this buffer.
Definition: RingBuffer.h:34