DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
ZmqContext.hpp
Go to the documentation of this file.
1#ifndef IPM_INCLUDE_IPM_ZMQCONTEXT_HPP_
2#define IPM_INCLUDE_IPM_ZMQCONTEXT_HPP_
3
13#include "ers/Issue.hpp"
14#include "logging/Logging.hpp" // NOTE: if ISSUES ARE DECLARED BEFORE include logging/Logging.hpp, TLOG_DEBUG<<issue wont work.
15#include "zmq.hpp"
16
17enum
18{
37};
38
39namespace dunedaq {
40
50 ZmqOperationError,
51 "An exception occured while calling " << operation << " on the ZMQ " << direction << " socket: "
52 << what << " (connection_string: " << connection_string << ")",
53 ((std::string)operation)((std::string)direction)((const char*)what)(
54 (std::string)connection_string)) // NOLINT
56
65 ZmqSendError,
66 "An exception occurred while sending " << N << " bytes to " << topic << ": " << what,
67 ((const char*)what)((int)N)((std::string)topic)) // NOLINT
69
77 ZmqReceiveError,
78 "An exception occured while receiving " << which << ": " << what,
79 ((const char*)what)((const char*)which)) // NOLINT
81
89 ZmqSubscribeError,
90 "An execption occured while subscribing to " << topic << ": " << what,
91 ((const char*)what)((std::string)topic)) // NOLINT
93
101 ZmqUnsubscribeError,
102 "An execption occured while unsubscribing from " << topic << ": " << what,
103 ((const char*)what)((std::string)topic)) // NOLINT
105
106namespace ipm {
108{
109public:
111 {
112 static ZmqContext s_ctx;
113 return s_ctx;
114 }
115
116 zmq::context_t& GetContext() { return m_context; }
117
118 void set_context_threads(int nthreads)
119 {
120 TLOG_DEBUG(TLVL_ZMQCONTEXT) << "Setting ZMQ Context IO thread count to " << nthreads;
121 m_context.set(zmq::ctxopt::io_threads, nthreads);
122 }
123 void set_context_maxsockets(int max_sockets)
124 {
125 TLOG_DEBUG(TLVL_ZMQCONTEXT) << "Setting ZMQ Context max sockets to " << max_sockets;
126 m_context.set(zmq::ctxopt::max_sockets, max_sockets);
127 }
128
129private:
131 {
132 auto threads_c = getenv("IPM_ZMQ_IO_THREADS");
133 if (threads_c != nullptr) {
134 auto threads = std::atoi(threads_c); // NOLINT If a conversion error occurs, we discard the result
135 if (threads > 1) {
136 set_context_threads(threads);
137 }
138 }
139
140 bool sockets_set = false;
141 auto sockets_c = getenv("IPM_ZMQ_MAX_SOCKETS");
142 if (sockets_c != nullptr) {
143 auto sockets = std::atoi(sockets_c); // NOLINT If a conversion error occurs, we discard the result
144 if (sockets > s_minimum_sockets) {
145 set_context_maxsockets(sockets);
146 sockets_set = true;
147 }
148 }
149 if (!sockets_set) {
151 }
152 }
153 ~ZmqContext() { m_context.close(); }
154 zmq::context_t m_context;
155 static constexpr int s_minimum_sockets = 16636;
156
157 ZmqContext(ZmqContext const&) = delete;
159 ZmqContext& operator=(ZmqContext const&) = delete;
161};
162} // namespace ipm
163} // namespace dunedaq
164
165#endif // IPM_INCLUDE_IPM_ZMQCONTEXT_HPP_
#define ERS_DECLARE_ISSUE(namespace_name, class_name, message, attributes)
@ TLVL_ZMQSUBSCRIBER_RECV_DATA_2
@ TLVL_ZMQSUBSCRIBER_RECV_HDR
@ TLVL_ZMQSUBSCRIBER_RECV_END
@ TLVL_ZMQRECEIVER_RECV_HDR_2
@ TLVL_ZMQSUBSCRIBER_RECV_DATA
@ TLVL_ZMQSENDER_SEND_END
@ TLVL_ZMQRECEIVER_RECV_DATA
@ TLVL_ZMQSUBSCRIBER_RECV_HDR_2
@ TLVL_ZMQCONTEXT
@ TLVL_ZMQPUBLISHER_SEND_START
@ TLVL_ZMQPUBLISHER_SEND_ERR
@ TLVL_CONNECTIONSTRING
@ TLVL_ZMQRECEIVER_RECV_END
@ TLVL_ZMQRECEIVER_RECV_HDR
@ TLVL_ZMQRECEIVER_RECV_DATA_2
@ TLVL_ZMQSENDER_SEND_ERR
@ TLVL_ZMQPUBLISHER_SEND_END
@ TLVL_ZMQSENDER_SEND_START
void set_context_threads(int nthreads)
static ZmqContext & instance()
ZmqContext & operator=(ZmqContext &&)=delete
void set_context_maxsockets(int max_sockets)
ZmqContext(ZmqContext const &)=delete
static constexpr int s_minimum_sockets
ZmqContext(ZmqContext &&)=delete
zmq::context_t & GetContext()
ZmqContext & operator=(ZmqContext const &)=delete
#define TLOG_DEBUG(lvl,...)
Definition Logging.hpp:112
The DUNE-DAQ namespace.
Definition DataStore.hpp:57