artdaq  v3_03_00
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 <unistd.h>
23 #include <stdlib.h>
24 
25 
29 namespace artdaq
30 {
34  class Globals
35  {
36  public:
37  static int my_rank_;
38  static std::unique_ptr<MetricManager> metricMan_;
39  static std::unique_ptr<PortManager> portMan_;
40  static std::string app_name_;
41  static int partition_number_;
42 
43  static std::mutex mftrace_mutex_;
44  static std::string mftrace_module_;
45  static std::string mftrace_iteration_;
46 
51  static uint32_t seedAndRandom_()
52  {
53  static bool initialized_ = false;
54  if (!initialized_)
55  {
56  int fp = open("/dev/random", O_RDONLY);
57  if (fp == -1) abort();
58  unsigned seed;
59  unsigned pos = 0;
60  while (pos < sizeof(seed))
61  {
62  int amt = read(fp, (char *)&seed + pos, sizeof(seed) - pos);
63  if (amt <= 0) abort();
64  pos += amt;
65  }
66  srand(seed);
67  close(fp);
68  initialized_ = true;
69  }
70  return rand();
71  }
72 
77  static int getPartitionNumber_()
78  {
79  uint32_t part_u = 0;
80 
81  // 23-May-2018, KAB: added the option to return the partition number data member
82  // and gave it precedence over the env var since it is typcally based on information
83  // that the user specified on the command line.
84  if (partition_number_ >= 0)
85  {
86  part_u = static_cast<uint32_t>(partition_number_);
87  }
88  else
89  {
90  auto part = getenv("ARTDAQ_PARTITION_NUMBER"); // 0-127
91  if (part != nullptr)
92  {
93  try
94  {
95  auto part_s = std::string(part);
96  part_u = static_cast<uint32_t>(std::stoll(part_s, 0, 0));
97  }
98  catch (std::invalid_argument) {}
99  catch (std::out_of_range) {}
100  }
101  partition_number_ = part_u & 0x7F;
102  }
103 
104  return (part_u & 0x7F);
105  }
106 
107  static std::string GetMFIteration_()
108  {
109  std::unique_lock<std::mutex> lk(mftrace_mutex_);
110  return mftrace_iteration_;
111  }
112 
113  static std::string GetMFModuleName_()
114  {
115  std::unique_lock<std::mutex> lk(mftrace_mutex_);
116  return mftrace_module_;
117  }
118 
119  static void SetMFIteration_(std::string name)
120  {
121  std::unique_lock<std::mutex> lk(mftrace_mutex_);
122  mftrace_iteration_ = name;
123  }
124 
125  static void SetMFModuleName_(std::string name)
126  {
127  std::unique_lock<std::mutex> lk(mftrace_mutex_);
128  mftrace_module_ = name;
129  }
130 
131  static void CleanUpGlobals()
132  {
133  metricMan_.reset(nullptr);
134  portMan_.reset(nullptr);
135  }
136  };
137 }
138 
139 #include "artdaq-core/Utilities/configureMessageFacility.hh"
140 #include "tracemf.h"
141 #include "artdaq-core/Utilities/TimeUtils.hh"
142 #include "fhiclcpp/ParameterSet.h"
143 #include "fhiclcpp/types/Atom.h"
144 #include "fhiclcpp/types/Table.h"
145 #include "fhiclcpp/types/Sequence.h"
146 #include "fhiclcpp/types/TableFragment.h"
147 #include "fhiclcpp/types/ConfigurationTable.h"
148 #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:77
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:51
static std::unique_ptr< MetricManager > metricMan_
A handle to MetricManager.
Definition: Globals.hh:38
static int my_rank_
The rank of the current application.
Definition: Globals.hh:37
static std::string app_name_
The name of the current application, to be used in logging and metrics.
Definition: Globals.hh:40
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:45
static std::unique_ptr< PortManager > portMan_
A handle to PortManager.
Definition: Globals.hh:39
The artdaq::Globals class contains several variables which are useful across the entire artdaq system...
Definition: Globals.hh:34
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:44
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:41