artdaq  v3_12_02
Globals.hh
1 #ifndef ARTDAQ_DAQDATA_GLOBALS_HH
2 #define ARTDAQ_DAQDATA_GLOBALS_HH
3 
4 #ifndef TRACE_DEFINE // as set in Globals.cc
5 #define TRACE_DECLARE
6 #endif
7 #include "artdaq-utilities/Plugins/MetricManager.hh"
8 #include "artdaq/DAQdata/PortManager.hh"
9 
10 #define my_rank artdaq::Globals::my_rank_
11 #define app_name artdaq::Globals::app_name_
12 #define metricMan artdaq::Globals::metricMan_
13 #define portMan artdaq::Globals::portMan_
14 #define seedAndRandom() artdaq::Globals::seedAndRandom_()
15 #define GetPartitionNumber() artdaq::Globals::getPartitionNumber_()
16 
17 #define GetMFIteration() artdaq::Globals::GetMFIteration_()
18 #define GetMFModuleName() artdaq::Globals::GetMFModuleName_()
19 #define SetMFModuleName(name) artdaq::Globals::SetMFModuleName_(name)
20 #define SetMFIteration(name) artdaq::Globals::SetMFIteration_(name)
21 
22 // https://stackoverflow.com/questions/21594140/c-how-to-ensure-different-random-number-generation-in-c-when-program-is-execut
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <cstdlib>
26 #include <memory>
27 #include <mutex>
28 #include <sstream>
29 #include <string>
30 
34 namespace artdaq {
38 class Globals
39 {
40 public:
41  static int my_rank_;
42  static std::unique_ptr<MetricManager> metricMan_;
43  static std::unique_ptr<PortManager> portMan_;
44  static std::string app_name_;
45  static int partition_number_;
46 
47  static std::mutex mftrace_mutex_;
48  static std::string mftrace_module_;
49  static std::string mftrace_iteration_;
50 
55  static uint32_t seedAndRandom_()
56  {
57  static bool initialized_ = false;
58  if (!initialized_)
59  {
60  int fp = open("/dev/random", O_RDONLY);
61  if (fp == -1) abort();
62  unsigned seed;
63  unsigned pos = 0;
64  while (pos < sizeof(seed))
65  {
66  int amt = read(fp, reinterpret_cast<char *>(&seed) + pos, sizeof(seed) - pos); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-bounds-pointer-arithmetic)
67  if (amt <= 0) abort();
68  pos += amt;
69  }
70  srand(seed);
71  close(fp);
72  initialized_ = true;
73  }
74  return rand(); // NOLINT(cert-msc50-cpp)
75  }
76 
81  static int getPartitionNumber_()
82  {
83  uint32_t part_u = 0;
84 
85  // 23-May-2018, KAB: added the option to return the partition number data member
86  // and gave it precedence over the env var since it is typcally based on information
87  // that the user specified on the command line.
88  if (partition_number_ >= 0)
89  {
90  part_u = static_cast<uint32_t>(partition_number_);
91  }
92  else
93  {
94  auto part = getenv("ARTDAQ_PARTITION_NUMBER"); // Valid values 0-127
95  if (part != nullptr)
96  {
97  try
98  {
99  auto part_s = std::string(part);
100  part_u = static_cast<uint32_t>(std::stoll(part_s, nullptr, 0));
101  }
102  catch (const std::invalid_argument &)
103  {}
104  catch (const std::out_of_range &)
105  {}
106  }
107  partition_number_ = part_u & 0x7F;
108  }
109 
110  return (part_u & 0x7F);
111  }
112 
117  static std::string GetMFIteration_()
118  {
119  std::unique_lock<std::mutex> lk(mftrace_mutex_);
120  return mftrace_iteration_;
121  }
122 
127  static std::string GetMFModuleName_()
128  {
129  std::unique_lock<std::mutex> lk(mftrace_mutex_);
130  return mftrace_module_;
131  }
132 
137  static void SetMFIteration_(std::string const &name)
138  {
139  std::unique_lock<std::mutex> lk(mftrace_mutex_);
140  mftrace_iteration_ = name;
141  }
142 
147  static void SetMFModuleName_(std::string const &name)
148  {
149  std::unique_lock<std::mutex> lk(mftrace_mutex_);
150  mftrace_module_ = name;
151  }
152 
156  static void CleanUpGlobals()
157  {
158  metricMan_.reset(nullptr);
159  portMan_.reset(nullptr);
160  }
161 };
162 } // namespace artdaq
163 
164 #include "tracemf.h"
165 
166 #endif // ARTDAQ_DAQDATA_GLOBALS_HH
static int getPartitionNumber_()
Get the current partition number, as defined by the ARTDAQ_PARTITION_NUMBER environment variable...
Definition: Globals.hh:81
static uint32_t seedAndRandom_()
Seed the C random number generator with the current time (if that has not been done already) and gene...
Definition: Globals.hh:55
static std::unique_ptr< MetricManager > metricMan_
A handle to MetricManager.
Definition: Globals.hh:42
static int my_rank_
The rank of the current application.
Definition: Globals.hh:41
static std::string app_name_
The name of the current application, to be used in logging and metrics.
Definition: Globals.hh:44
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:156
static std::string GetMFIteration_()
Get the current iteration for MessageFacility messages.
Definition: Globals.hh:117
static std::mutex mftrace_mutex_
Mutex to protect mftrace_module_ and mftrace_iteration_.
Definition: Globals.hh:47
static void SetMFIteration_(std::string const &name)
Set the current iteration for MessageFacility messages.
Definition: Globals.hh:137
static void SetMFModuleName_(std::string const &name)
Set the current module name for MessageFacility messages.
Definition: Globals.hh:147
static std::string mftrace_iteration_
MessageFacility&#39;s module and iteration are thread-local, but we want to use them to represent global ...
Definition: Globals.hh:49
static std::unique_ptr< PortManager > portMan_
A handle to PortManager.
Definition: Globals.hh:43
The artdaq::Globals class contains several variables which are useful across the entire artdaq system...
Definition: Globals.hh:38
static std::string mftrace_module_
MessageFacility&#39;s module and iteration are thread-local, but we want to use them to represent global ...
Definition: Globals.hh:48
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:45
static std::string GetMFModuleName_()
Get the current module name for MessageFacility messages.
Definition: Globals.hh:127