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 {
19
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);
75 int remq(int* buf, int capacityWords = -1);
77 int spyq(int* buf) const;
79 int numq() const;
80
82 void txAttached();
84 void txDetached();
86 void kill();
87
89 bool isDead() const;
94 bool allRxWaiting() const;
95
97 int clear();
99 void forceClear();
100
102 int tryClear();
103
105 int shmid() const;
106
107 // Debugging functions
109 void dump_db();
111 int ninsq() const;
113 int nremq() const;
114
116 int insq_counter() const;
118 int remq_counter() const;
119
121 void dumpInfo() const;
122
123 private:
124 bool m_new{true};
125 bool m_file{false};
126 std::string m_pathname{""};
127 int m_pathfd{ -1};
128 key_t m_shmkey{IPC_PRIVATE};
129 key_t m_semkey{IPC_PRIVATE};
131 std::string m_semshmFileName{""};
132
138 bool m_procIsBusy{false};
139
140 int m_shmid{ -1};
141 int* m_shmadr{nullptr};
142 int m_shmsize{ -1};
143 struct RingBufInfo* m_bufinfo {nullptr};
144 int* m_buftop{nullptr};
145 int m_semid{ -1};
148 };
149
151}
void dump_db()
Print some info on the RingBufInfo structure.
~RingBuffer()
Destructor.
Definition RingBuffer.cc:79
int insq(const int *buf, int size, bool checkTx=false)
Append a buffer to the RingBuffer.
int m_shmsize
Size of shared memory segment, in bytes.
Definition RingBuffer.h:142
struct RingBufInfo * m_bufinfo
structure to manage ring buffer.
Definition RingBuffer.h:143
int tryClear()
Clear the RingBuffer, if the semaphore isn't locked at the moment.
bool m_file
True if m_pathfd needs to be closed.
Definition RingBuffer.h:125
int remq_counter() const
Return number of remq() calls.
int clear()
Clear the RingBuffer.
void txAttached()
Increase the number of attached Tx counter.
int spyq(int *buf) const
Prefetch a buffer from the RingBuffer w/o removing it.
void cleanup()
Function to detach and remove shared memory.
int m_remq_counter
count remq() calls.
Definition RingBuffer.h:146
void txDetached()
Decrease the number of attached Tx counter.
key_t m_shmkey
SHM key, see shmget(2).
Definition RingBuffer.h:128
int m_semid
Semaphore ID.
Definition RingBuffer.h:145
int shmid() const
Return ID of the shared memory.
void dumpInfo() const
Dump contents of RingBufInfo metadata.
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.
int m_shmid
ID of shared memory segment.
Definition RingBuffer.h:140
bool allRxWaiting() const
True if and only if buffer is empty and nbusy == 0.
void kill()
Cause termination of reading processes (if they use isDead()).
int m_pathfd
Associated file descriptor.
Definition RingBuffer.h:127
int insq_counter() const
Return number of insq() calls.
std::string m_pathname
Path for identifying shared memory if named ring buffer is created.
Definition RingBuffer.h:126
RingBuffer(int nwords=c_DefaultSize)
Constructor to create a new shared memory in private space.
Definition RingBuffer.cc:32
void openSHM(int nwords)
open shared memory
Definition RingBuffer.cc:84
int remq(int *buf, int capacityWords=-1)
Pick up a buffer from the RingBuffer.
bool isDead() const
If True, the ring buffer is empty and has no attached Tx modules (i.e.
bool m_procIsBusy
Is this process currently processing events from this RingBuffer?
Definition RingBuffer.h:138
int * m_buftop
Points to memory after the end of m_bufinfo.
Definition RingBuffer.h:144
int m_insq_counter
count insq() calls.
Definition RingBuffer.h:147
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:131
bool m_new
True if we created the ring buffer ourselves (and need to clean it).
Definition RingBuffer.h:124
int ninsq() const
Return number of insq() calls for current buffer.
void forceClear()
Forcefully clear the RingBuffer with resetting semaphore.
key_t m_semkey
Semaphore key, see semget(2).
Definition RingBuffer.h:129
int nremq() const
Return number of remq() calls for current buffer.
int * m_shmadr
Address of attached shared memory segment.
Definition RingBuffer.h:141
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?
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