artdaq  v3_02_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 
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  static int partition_number_;
38 
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  uint32_t part_u = 0;
75 
76  // 23-May-2018, KAB: added the option to return the partition number data member
77  // and gave it precedence over the env var since it is typcally based on information
78  // that the user specified on the command line.
79  if (partition_number_ >= 0)
80  {
81  part_u = static_cast<uint32_t>(partition_number_);
82  }
83  else
84  {
85  auto part = getenv("ARTDAQ_PARTITION_NUMBER"); // 0-127
86  if (part != nullptr)
87  {
88  try
89  {
90  auto part_s = std::string(part);
91  part_u = static_cast<uint32_t>(std::stoll(part_s, 0, 0));
92  }
93  catch (std::invalid_argument) {}
94  catch (std::out_of_range) {}
95  }
96  }
97 
98  return (part_u & 0x7F);
99  }
100  };
101 }
102 
103 #include "artdaq-core/Utilities/configureMessageFacility.hh"
104 #include "tracemf.h"
105 #include "artdaq-core/Utilities/TimeUtils.hh"
106 #include "fhiclcpp/ParameterSet.h"
107 #include "fhiclcpp/types/Atom.h"
108 #include "fhiclcpp/types/Table.h"
109 #include "fhiclcpp/types/Sequence.h"
110 #include "fhiclcpp/types/TableFragment.h"
111 #if MESSAGEFACILITY_HEX_VERSION >= 0x20103
112 # include "fhiclcpp/types/ConfigurationTable.h"
113 #endif
114 #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
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:40
The artdaq::Globals class contains several variables which are useful across the entire artdaq system...
Definition: Globals.hh:31
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:39
static int partition_number_
The partition number of the current application.
Definition: Globals.hh:37