artdaq  v3_07_01
Globals.hh
1 #ifndef ARTDAQ_DAQDATA_GLOBALS_HH
2 #define ARTDAQ_DAQDATA_GLOBALS_HH
3 
4 #include <sstream>
5 #include "artdaq-utilities/Plugins/MetricManager.hh"
6 #include "artdaq/DAQdata/PortManager.hh"
7 
8 #define my_rank artdaq::Globals::my_rank_
9 #define app_name artdaq::Globals::app_name_
10 #define metricMan artdaq::Globals::metricMan_
11 #define portMan artdaq::Globals::portMan_
12 #define seedAndRandom() artdaq::Globals::seedAndRandom_()
13 #define GetPartitionNumber() artdaq::Globals::getPartitionNumber_()
14 
15 #define GetMFIteration() artdaq::Globals::GetMFIteration_()
16 #define GetMFModuleName() artdaq::Globals::GetMFModuleName_()
17 #define SetMFModuleName(name) artdaq::Globals::SetMFModuleName_(name)
18 #define SetMFIteration(name) artdaq::Globals::SetMFIteration_(name)
19 
20 //https://stackoverflow.com/questions/21594140/c-how-to-ensure-different-random-number-generation-in-c-when-program-is-execut
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 
28 namespace artdaq {
32 class Globals
33 {
34 public:
35  static int my_rank_;
36  static std::unique_ptr<MetricManager> metricMan_;
37  static std::unique_ptr<PortManager> portMan_;
38  static std::string app_name_;
39  static int partition_number_;
40 
41  static std::mutex mftrace_mutex_;
42  static std::string mftrace_module_;
43  static std::string mftrace_iteration_;
44 
49  static uint32_t seedAndRandom_()
50  {
51  static bool initialized_ = false;
52  if (!initialized_)
53  {
54  int fp = open("/dev/random", O_RDONLY);
55  if (fp == -1) abort();
56  unsigned seed;
57  unsigned pos = 0;
58  while (pos < sizeof(seed))
59  {
60  int amt = read(fp, (char *)&seed + pos, sizeof(seed) - pos);
61  if (amt <= 0) abort();
62  pos += amt;
63  }
64  srand(seed);
65  close(fp);
66  initialized_ = true;
67  }
68  return rand();
69  }
70 
75  static int getPartitionNumber_()
76  {
77  uint32_t part_u = 0;
78 
79  // 23-May-2018, KAB: added the option to return the partition number data member
80  // and gave it precedence over the env var since it is typcally based on information
81  // that the user specified on the command line.
82  if (partition_number_ >= 0)
83  {
84  part_u = static_cast<uint32_t>(partition_number_);
85  }
86  else
87  {
88  auto part = getenv("ARTDAQ_PARTITION_NUMBER"); // 0-127
89  if (part != nullptr)
90  {
91  try
92  {
93  auto part_s = std::string(part);
94  part_u = static_cast<uint32_t>(std::stoll(part_s, 0, 0));
95  }
96  catch (const std::invalid_argument &)
97  {}
98  catch (const std::out_of_range &)
99  {}
100  }
101  partition_number_ = part_u & 0x7F;
102  }
103 
104  return (part_u & 0x7F);
105  }
106 
111  static std::string GetMFIteration_()
112  {
113  std::unique_lock<std::mutex> lk(mftrace_mutex_);
114  return mftrace_iteration_;
115  }
116 
121  static std::string GetMFModuleName_()
122  {
123  std::unique_lock<std::mutex> lk(mftrace_mutex_);
124  return mftrace_module_;
125  }
126 
131  static void SetMFIteration_(std::string name)
132  {
133  std::unique_lock<std::mutex> lk(mftrace_mutex_);
134  mftrace_iteration_ = name;
135  }
136 
141  static void SetMFModuleName_(std::string name)
142  {
143  std::unique_lock<std::mutex> lk(mftrace_mutex_);
144  mftrace_module_ = name;
145  }
146 
150  static void CleanUpGlobals()
151  {
152  metricMan_.reset(nullptr);
153  portMan_.reset(nullptr);
154  }
155 };
156 } // namespace artdaq
157 
158 #include "artdaq-core/Utilities/TimeUtils.hh"
159 #include "artdaq-core/Utilities/configureMessageFacility.hh"
160 #include "fhiclcpp/ParameterSet.h"
161 #include "fhiclcpp/types/Atom.h"
162 #include "fhiclcpp/types/ConfigurationTable.h"
163 #include "fhiclcpp/types/Sequence.h"
164 #include "fhiclcpp/types/Table.h"
165 #include "fhiclcpp/types/TableFragment.h"
166 #include "tracemf.h"
167 #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:75
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:49
static std::unique_ptr< MetricManager > metricMan_
A handle to MetricManager.
Definition: Globals.hh:36
static int my_rank_
The rank of the current application.
Definition: Globals.hh:35
static std::string app_name_
The name of the current application, to be used in logging and metrics.
Definition: Globals.hh:38
static void CleanUpGlobals()
Clean up statically-allocated Manager class instances.
Definition: Globals.hh:150
static std::string GetMFIteration_()
Get the current iteration for MessageFacility messages.
Definition: Globals.hh:111
static std::mutex mftrace_mutex_
Mutex to protect mftrace_module_ and mftrace_iteration_.
Definition: Globals.hh:41
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:43
static void SetMFModuleName_(std::string name)
Set the current module name for MessageFacility messages.
Definition: Globals.hh:141
static std::unique_ptr< PortManager > portMan_
A handle to PortManager.
Definition: Globals.hh:37
static void SetMFIteration_(std::string name)
Set the current iteration for MessageFacility messages.
Definition: Globals.hh:131
The artdaq::Globals class contains several variables which are useful across the entire artdaq system...
Definition: Globals.hh:32
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:42
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:39
static std::string GetMFModuleName_()
Get the current module name for MessageFacility messages.
Definition: Globals.hh:121