artdaq_core  v3_01_02
SharedMemoryFragmentManager.cc
1 #include "artdaq-core/Core/SharedMemoryFragmentManager.hh"
2 #include "tracemf.h"
3 
4 
5 artdaq::SharedMemoryFragmentManager::SharedMemoryFragmentManager(uint32_t shm_key, size_t buffer_count, size_t max_buffer_size, size_t buffer_timeout_us)
6  : SharedMemoryManager(shm_key, buffer_count, max_buffer_size, buffer_timeout_us)
7 , active_buffer_(-1)
8 {
9 
10 }
11 
13 {
14  if (!IsValid()) { return -1; }
15 
16  TLOG_ARB(13, "SharedMemoryFragmentManager") << "Sending fragment with seqID=" << fragment.sequenceID() << TLOG_ENDL;
17  artdaq::RawDataType* fragAddr = fragment.headerAddress();
18  size_t fragSize = fragment.size() * sizeof(artdaq::RawDataType);
19 
20  auto buf = GetBufferForWriting(overwrite);
21  auto sts = Write(buf, fragAddr, fragSize);
22  if(sts == fragSize)
23  {
24  TLOG_ARB(13, "SharedMemoryFragmentManager") << "Done sending Fragment with seqID=" << fragment.sequenceID() << TLOG_ENDL;
25  MarkBufferFull(buf);
26  return 0;
27  }
28  TLOG_ERROR("SharedMemoryFragmentManager") << "Unexpected status from SharedMemory Write call!" << TLOG_ENDL;
29  return -2;
30 }
31 
33 {
34  TLOG_ARB(13, "SharedMemoryFragmentManager") << "ReadFragment BEGIN" << TLOG_ENDL;
36 
37  TLOG_ARB(13, "SharedMemoryFragmentManager") << "Reading Fragment Header" << TLOG_ENDL;
38  auto sts = ReadFragmentHeader(tmpHdr);
39  if (sts != 0) return sts;
40  fragment.resize(tmpHdr.word_count - tmpHdr.num_words());
41  memcpy(fragment.headerAddress(), &tmpHdr, tmpHdr.num_words() * sizeof(artdaq::RawDataType));
42  TLOG_ARB(13, "SharedMemoryFragmentManager") << "Reading Fragment Body" << TLOG_ENDL;
43  return ReadFragmentData(fragment.headerAddress() + tmpHdr.num_words(), tmpHdr.word_count - tmpHdr.num_words());
44 }
45 
47 {
48  if (!IsValid()) return -3;
49 
51  active_buffer_ = GetBufferForReading();
52 
53  if (active_buffer_ == -1) return -1;
54 
55  auto sts = Read(active_buffer_, &header, hdrSize);
56  if (!sts) return -2;
57 
58  return 0;
59 }
60 
62 {
63  if (!IsValid() || active_buffer_ == -1 || !CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading)) {
64  TLOG_ERROR("SharedMemoryFragmentManager") << "ReadFragmentData: Buffer " << active_buffer_ << " failed status checks: IsValid()=" << std::boolalpha << IsValid() << ", CheckBuffer=" << CheckBuffer(active_buffer_, BufferSemaphoreFlags::Reading) << TLOG_ENDL;
65  return -3;
66  }
67 
68  auto sts = Read(active_buffer_, destination, words * sizeof(RawDataType));
69  if (!sts) {
70  TLOG_ERROR("SharedMemoryFragmentManager") << "ReadFragmentData: Buffer " << active_buffer_ << " returned bad status code from Read" << TLOG_ENDL;
71  return -2;
72  }
73 
74  MarkBufferEmpty(active_buffer_);
75  active_buffer_ = -1;
76  return 0;
77 }
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