artdaq_core  v3_01_04
SharedMemoryFragmentManager.cc
1 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
2 #include "tracemf.h"
3 
4 #undef TRACE_NAME
5 #define TRACE_NAME "SharedMemoryFragmentManager"
6 
7 artdaq::SharedMemoryFragmentManager::SharedMemoryFragmentManager(uint32_t shm_key, size_t buffer_count, size_t max_buffer_size, size_t buffer_timeout_us)
8  : SharedMemoryManager(shm_key, buffer_count, max_buffer_size, buffer_timeout_us)
9 , active_buffer_(-1)
10 {
11 
12 }
13 
15 {
16  if (!IsValid()) { return -1; }
17 
18  TLOG(13) << "Sending fragment with seqID=" << fragment.sequenceID() << TLOG_ENDL;
19  artdaq::RawDataType* fragAddr = fragment.headerAddress();
20  size_t fragSize = fragment.size() * sizeof(artdaq::RawDataType);
21 
22  auto buf = GetBufferForWriting(overwrite);
23  auto sts = Write(buf, fragAddr, fragSize);
24  if(sts == fragSize)
25  {
26  TLOG(13) << "Done sending Fragment with seqID=" << fragment.sequenceID() << TLOG_ENDL;
27  MarkBufferFull(buf);
28  return 0;
29  }
30  TLOG(TLVL_ERROR) << "Unexpected status from SharedMemory Write call!" << TLOG_ENDL;
31  return -2;
32 }
33 
35 {
36  TLOG(14) << "ReadFragment BEGIN" << TLOG_ENDL;
38 
39  TLOG(14) << "Reading Fragment Header" << TLOG_ENDL;
40  auto sts = ReadFragmentHeader(tmpHdr);
41  if (sts != 0) return sts;
42  fragment.resize(tmpHdr.word_count - tmpHdr.num_words());
43  memcpy(fragment.headerAddress(), &tmpHdr, tmpHdr.num_words() * sizeof(artdaq::RawDataType));
44  TLOG(14) << "Reading Fragment Body" << TLOG_ENDL;
45  return ReadFragmentData(fragment.headerAddress() + tmpHdr.num_words(), tmpHdr.word_count - tmpHdr.num_words());
46 }
47 
49 {
50  if (!IsValid()) return -3;
51 
53  active_buffer_ = GetBufferForReading();
54 
55  if (active_buffer_ == -1) return -1;
56 
57  auto sts = Read(active_buffer_, &header, hdrSize);
58  if (!sts) return -2;
59 
60  return 0;
61 }
62 
64 {
65  if (!IsValid() || active_buffer_ == -1 || !CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading)) {
66  TLOG(TLVL_ERROR) << "ReadFragmentData: Buffer " << active_buffer_ << " failed status checks: IsValid()=" << std::boolalpha << IsValid() << ", CheckBuffer=" << CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading) << TLOG_ENDL;
67  return -3;
68  }
69 
70  auto sts = Read(active_buffer_, destination, words * sizeof(RawDataType));
71  if (!sts) {
72  TLOG(TLVL_ERROR) << "ReadFragmentData: Buffer " << active_buffer_ << " returned bad status code from Read" << TLOG_ENDL;
73  return -2;
74  }
75 
76  MarkBufferEmpty(active_buffer_);
77  active_buffer_ = -1;
78  return 0;
79 }
int WriteFragment(Fragment &&fragment, bool overwrite)
Write a Fragment to the Shared Memory.
RawDataType word_count
number of RawDataType words in this Fragment
static constexpr std::size_t num_words()
Returns the number of RawDataType words present in the header.
The RawFragmentHeader class contains the basic fields used by artdaq for routing Fragment objects thr...
void resize(std::size_t sz)
Resize the data payload to hold sz RawDataType words.
Definition: Fragment.hh:973
SharedMemoryFragmentManager(uint32_t shm_key, size_t buffer_count, size_t max_buffer_size, size_t buffer_timeout_us=100 *1000000)
SharedMemoryFragmentManager Constructor.
The SharedMemoryManager creates a Shared Memory area which is divided into a number of fixed-size buf...
detail::RawFragmentHeader::RawDataType RawDataType
The RawDataType (currently a 64-bit integer) is the basic unit of data representation within artdaq ...
Definition: Fragment.hh:39
int ReadFragmentHeader(detail::RawFragmentHeader &header)
Read a Fragment Header from the Shared Memory.
int ReadFragment(Fragment &fragment)
Read a Fragment from the Shared Memory.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:84
int ReadFragmentData(RawDataType *destination, size_t words)
Read Fragment Data from the Shared Memory.
RawDataType * headerAddress()
Gets the address of the header.
Definition: Fragment.hh:1121