Belle II Software light-2607-kasei
MsgHandler.cc
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#include <framework/pcore/MsgHandler.h>
10#include <framework/logging/Logger.h>
11
12#include <TObject.h>
13#include <TMessage.h>
14#include <RZip.h>
15
16using namespace std;
17using namespace Belle2;
18
19
20namespace {
22 const static int c_maxObjectSizeBytes = 50000000; //50MB
23}
24
26 m_buf(100000),
27 m_compBuf(0),
28 m_msg(new TMessage(kMESS_OBJECT))
29{
30 m_complevel = complevel;
31
32 //Schema evolution is needed to stream genfit tracks
33 //If disabled, streamers will crash when reading data.
34 TMessage::EnableSchemaEvolutionForAll();
35 m_msg->SetWriteMode();
36}
37
38MsgHandler::~MsgHandler() = default;
39
41{
42 m_buf.clear();
43 m_compBuf.clear();
44}
45
46void MsgHandler::add(const TObject* obj, const string& name)
47{
48 m_msg->WriteObject(obj);
49
50 int len = m_msg->Length();
51 const char* buf = m_msg->Buffer();
52
53 if (len > c_maxObjectSizeBytes) {
54 B2WARNING("MsgHandler: Object " << name << " is very large (" << len << " bytes), parallel processing may be slow.");
55 }
56
57 // Put name of object in output buffer including a final 0-byte
58 UInt_t nameLength = name.size() + 1;
59 m_buf.add(&nameLength, sizeof(nameLength));
60 m_buf.add(name.c_str(), nameLength);
61 // Copy object into buffer
62 m_buf.add(&len, sizeof(len));
63 m_buf.add(buf, len);
64 m_msg->Reset();
65}
66
68{
69 if (rectype == MSG_TERMINATE) {
70 auto* eod = new EvtMessage(nullptr, 0, rectype);
71 return eod;
72 }
73
74 // which buffer to send? defaults to uncompressed
75 auto buf = &m_buf;
76 unsigned int flags = 0;
77 // but if we have compression enabled then please compress.
78 if (m_complevel > 0) {
79 // make sure buffer for the compression is big enough.
80 m_compBuf.resize(m_buf.size());
81 // And call the root compression function
82 const int algorithm = m_complevel / 100;
83 const int level = m_complevel % 100;
84 int irep{0}, nin{(int)m_buf.size()}, nout{nin};
85 R__zipMultipleAlgorithm(level, &nin, m_buf.data(), &nout, m_compBuf.data(), &irep,
86 (ROOT::RCompressionSetting::EAlgorithm::EValues) algorithm);
87 // it returns the number of bytes of the output in irep. If that is zero or
88 // to big compression failed and we transmit uncompressed.
89 if (irep > 0 && irep <= nin) {
90 //set correct size of compressed message
91 m_compBuf.resize(irep);
92 // and set pointer to correct buffer for creating message
93 buf = &m_compBuf;
94 // also add a flag indicating it's compressed
96 }
97 }
98
99 auto* evtmsg = new EvtMessage(buf->data(), buf->size(), rectype);
100 evtmsg->setMsgFlags(flags);
101 clear();
102
103 return evtmsg;
104}
105
106void MsgHandler::decode_msg(EvtMessage* msg, vector<TObject*>& objlist,
107 vector<string>& namelist)
108{
109 const char* msgptr = msg->msg();
110 const char* end = msgptr + msg->msg_size();
111
113 // apparently message is compressed, let's decompress
114 m_compBuf.clear();
115 int nzip{0}, nout{0};
116 // ROOT wants a non-const unsigned char pointer although it only reads the data
117 auto* zipptr = reinterpret_cast<unsigned char*>(const_cast<char*>(msgptr));
118 const auto* zipend = reinterpret_cast<const unsigned char*>(end);
119 // and uncompress everything
120 while (zipptr < zipend) {
121 // first get a header of the next block so we know how big the output will be
122 if (R__unzip_header(&nzip, zipptr, &nout) != 0) {
123 B2FATAL("Cannot uncompress message header");
124 }
125 // no more output? fine
126 if (!nout) break;
127 if (zipend - zipptr > nzip) {
128 B2FATAL("Not enough bytes left to uncompress");
129 }
130 // otherwise make sure output buffer is large enough
131 int old_size = m_compBuf.size();
132 m_compBuf.resize(old_size + nout);
133 // and uncompress, the amount of bytes will be returned as irep
134 int irep{0};
135 R__unzip(&nzip, zipptr, &nout, reinterpret_cast<unsigned char*>(m_compBuf.data() + old_size), &irep);
136 // if that is not positive an error happened, bail
137 if (irep <= 0) {
138 B2FATAL("Cannot uncompress message");
139 }
140 // otherwise advance pointer by the amount of bytes compressed bytes in the block
141 zipptr += nzip;
142 }
143 // ok, decompressed successfully, set msg pointer to the correct area
144 msgptr = m_compBuf.data();
145 end = msgptr + m_compBuf.size();
146 }
147
148 while (msgptr < end) {
149 // Restore object name
150 UInt_t nameLength;
151 memcpy(&nameLength, msgptr, sizeof(nameLength));
152 msgptr += sizeof(nameLength);
153 if (nameLength == 0 || std::distance(msgptr, end) < nameLength)
154 B2FATAL("Buffer overrun while decoding object name, check length fields!");
155
156 // read full string but omit final 0-byte. This safeguards against strings containing 0-bytes
157 namelist.emplace_back(msgptr, nameLength - 1);
158 msgptr += nameLength;
159
160 // Restore object
161 UInt_t objlen;
162 memcpy(&objlen, msgptr, sizeof(objlen));
163 msgptr += sizeof(objlen);
164 if (objlen == 0 || std::distance(msgptr, end) < objlen)
165 B2FATAL("Buffer overrun while decoding object, check length fields!");
166
167 m_inMsg.SetBuffer(msgptr, objlen);
168 objlist.push_back(m_inMsg.readTObject());
169 msgptr += objlen;
170 //no need to call InMessage::Reset() here (done in SetBuffer())
171 }
172}
Class to manage streamed object.
Definition EvtMessage.h:59
int msg_size() const
Get size of message body.
bool hasMsgFlags(unsigned int flags) const
Check if the message has the given flags.
Definition EvtMessage.h:115
char * msg()
Get pointer to message body.
@ c_MsgCompressed
indicates that the message body is compressed and should be uncompressed using ROOT R__unzip_header a...
Definition EvtMessage.h:69
virtual ~MsgHandler()
Destructor.
int m_complevel
compression algorithm * 100 + compression level.
Definition MsgHandler.h:134
InMessage m_inMsg
Used for deserializing in decode_msg()
Definition MsgHandler.h:133
virtual void decode_msg(EvtMessage *msg, std::vector< TObject * > &objlist, std::vector< std::string > &namelist)
Decode an EvtMessage into a vector list of objects with names.
MsgHandler(int complevel=0)
Constructor.
Definition MsgHandler.cc:25
CharBuffer m_buf
EvtMessage character buffer for encode_msg().
Definition MsgHandler.h:130
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition MsgHandler.cc:46
CharBuffer m_compBuf
EvtMessage character buffer for compressing/decompressing.
Definition MsgHandler.h:131
std::unique_ptr< TMessage > m_msg
Used for serialising objects into m_buf.
Definition MsgHandler.h:132
virtual EvtMessage * encode_msg(ERecordType rectype)
Stream object list into an EvtMessage.
Definition MsgHandler.cc:67
virtual void clear()
Clear object list.
Definition MsgHandler.cc:40
ERecordType
What type of message is this?
Definition EvtMessage.h:25
Abstract base class for different kinds of events.
STL namespace.