artdaq  v3_02_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 
7 #define my_rank artdaq::Globals::my_rank_
8 #define app_name artdaq::Globals::app_name_
9 #define metricMan artdaq::Globals::metricMan_
10 #define seedAndRandom() artdaq::Globals::seedAndRandom_()
11 
12 #define mftrace_iteration artdaq::Globals::mftrace_iteration_
13 #define mftrace_module artdaq::Globals::mftrace_module_
14 #define SetMFModuleName(name) mftrace_module = name
15 #define SetMFIteration(name) mftrace_iteration = name
16 
17 //https://stackoverflow.com/questions/21594140/c-how-to-ensure-different-random-number-generation-in-c-when-program-is-execut
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 
22 
26 namespace artdaq
27 {
31  class Globals
32  {
33  public:
34  static int my_rank_;
35  static MetricManager* metricMan_;
36  static std::string app_name_;
37 
38  // MessageFacility's module and iteration are thread-local, but we want to use them to represent global state in artdaq.
39  static std::string mftrace_module_;
40  static std::string mftrace_iteration_;
41 
46  static uint32_t seedAndRandom_()
47  {
48  static bool initialized_ = false;
49  if (!initialized_)
50  {
51  int fp = open("/dev/random", O_RDONLY);
52  if (fp == -1) abort();
53  unsigned seed;
54  unsigned pos = 0;
55  while (pos < sizeof(seed))
56  {
57  int amt = read(fp, (char *)&seed + pos, sizeof(seed) - pos);
58  if (amt <= 0) abort();
59  pos += amt;
60  }
61  srand(seed);
62  close(fp);
63  initialized_ = true;
64  }
65  return rand();
66  }
67 
72  static int GetPartitionNumber()
73  {
74  auto part = getenv("ARTDAQ_PARTITION_NUMBER"); // 0-127
75  uint32_t part_u = 0;
76  if (part != nullptr)
77  {
78  try
79  {
80  auto part_s = std::string(part);
81  part_u = static_cast<uint32_t>(std::stoll(part_s, 0, 0));
82  }
83  catch (std::invalid_argument) {}
84  catch (std::out_of_range) {}
85  }
86 
87  return (part_u & 0x7F);
88  }
89  };
90 }
91 
92 #include "artdaq-core/Utilities/configureMessageFacility.hh"
93 #include "tracemf.h"
94 #include "artdaq-core/Utilities/TimeUtils.hh"
95 #include "fhiclcpp/ParameterSet.h"
96 #include "fhiclcpp/types/Atom.h"
97 #include "fhiclcpp/types/Table.h"
98 #include "fhiclcpp/types/Sequence.h"
99 #include "fhiclcpp/types/TableFragment.h"
100 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
101 # include "fhiclcpp/types/ConfigurationTable.h"
102 #endif
103 #endif // ARTDAQ_DAQDATA_GLOBALS_HH
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:46
static int my_rank_
The rank of the current application.
Definition: Globals.hh:34
static std::string app_name_
The name of the current application, to be used in logging and metrics.
Definition: Globals.hh:36
static MetricManager * metricMan_
A handle to MetricManager.
Definition: Globals.hh:35
static int GetPartitionNumber()
Get the current partition number, as defined by the ARTDAQ_PARTITION_NUMBER environment variable...
Definition: Globals.hh:72
The artdaq::Globals class contains several variables which are useful across the entire artdaq system...
Definition: Globals.hh:31