Belle II Software  release-05-01-25
Utils.cc
1 // weird bug in intel compiler and or boost::process: If we include boost
2 // process intel compile fails. It seems that including sys/wait.h after
3 // other c++ std:: headers triggers a weird behavior failing two static
4 // asserts. We work around that by including it right away
5 #include <sys/wait.h>
6 
7 #include <framework/utilities/Utils.h>
8 
9 #include <framework/gearbox/Unit.h>
10 #include <framework/logging/Logger.h>
11 
12 #include <boost/process.hpp>
13 
14 #include <sys/time.h>
15 #include <unistd.h>
16 
17 #include <cstdio>
18 #include <iomanip>
19 #include <utility>
20 
21 using namespace Belle2;
22 
23 namespace {
33  std::pair<unsigned long, unsigned long> getStatmSize()
34  {
36  const static long pageSizeKb = sysconf(_SC_PAGESIZE) / 1024;
37  static FILE* stream = nullptr;
38  static int pid = 0;
39  int currentPid = getpid();
40  if (currentPid != pid) {
41  pid = currentPid;
42  std::string statm = "/proc/" + std::to_string(pid) + "/statm";
43  stream = fopen(statm.c_str(), "r");
44  // If we use buffering we might get the same value each time we read so
45  // disable buffering
46  setvbuf(stream, nullptr, _IONBF, 0);
47  }
48  unsigned long vmSizePages{0};
49  unsigned long rssPages{0};
50  rewind(stream);
51  fscanf(stream, "%lu %lu", &vmSizePages, &rssPages);
52  return std::make_pair(vmSizePages * pageSizeKb, rssPages * pageSizeKb);
53  }
54 }
55 
56 namespace Belle2::Utils {
57 
58  double getClock()
59  {
60  timespec ts;
61  clock_gettime(CLOCK_REALTIME, &ts);
62  return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
63  }
64  double getCPUClock()
65  {
66  timespec ts;
67  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
68  return (ts.tv_sec * Unit::s) + (ts.tv_nsec * Unit::ns);
69  }
70 
71  unsigned long getVirtualMemoryKB()
72  {
73  return getStatmSize().first;
74  }
75 
76  unsigned long getRssMemoryKB()
77  {
78  return getStatmSize().second;
79  }
80 
81  Timer::Timer(std::string text):
82  m_startTime(getClock()),
83  m_text(std::move(text))
84  { }
85 
86  Timer::~Timer()
87  {
88  double elapsed = (getClock() - m_startTime) / Unit::ms;
89  B2INFO(m_text << " " << std::fixed << std::setprecision(3) << elapsed << " ms");
90  }
91 
92  std::string getCommandOutput(const std::string& command, const std::vector<std::string>& arguments,
93  bool searchPath)
94  {
95  namespace bp = boost::process;
96  auto cmd = searchPath ? bp::search_path(command) : boost::filesystem::path(command);
97  bp::ipstream cmdOut;
98  bp::child child(cmd, bp::args(arguments), bp::std_in.close(), bp::std_out > cmdOut);
99  char buffer[4096];
100  std::string result;
101  while (child.running() && cmdOut.read(buffer, sizeof(buffer))) {
102  result.append(buffer, sizeof(buffer));
103  }
104  if (cmdOut.gcount()) result.append(buffer, cmdOut.gcount());
105  return result;
106  }
107 }
Belle2::Utils::getRssMemoryKB
unsigned long getRssMemoryKB()
Returns the amount of memory the process actually occupies in the physical RAM of the machine.
Definition: Utils.cc:76
Belle2::Unit::s
static const double s
[second]
Definition: Unit.h:105
Belle2::Unit::ms
static const double ms
[millisecond]
Definition: Unit.h:106
Belle2::Unit::ns
static const double ns
Standard of [time].
Definition: Unit.h:58
Belle2::Utils::Timer::Timer
Timer(std::string text="")
Constructor, with some identifying text.
Definition: Utils.cc:81
Belle2::Utils::getCPUClock
double getCPUClock()
Return current value of the per-thread CPU clock.
Definition: Utils.cc:64
Belle2::Utils::getCommandOutput
std::string getCommandOutput(const std::string &command, const std::vector< std::string > &arguments={}, bool searchPath=true)
Execute a shell command and return its output.
Definition: Utils.cc:92
Belle2::Utils
General utility functions.
Definition: Utils.h:15
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Utils::Timer::m_startTime
double m_startTime
time at start (in ns).
Definition: Utils.h:78
Belle2::Utils::getClock
double getClock()
Return current value of the real-time clock.
Definition: Utils.cc:58
Belle2::Utils::getVirtualMemoryKB
unsigned long getVirtualMemoryKB()
Returns currently used virtual memory in KB, includes swapped and not occupied memory pages and memor...
Definition: Utils.cc:71
Belle2::Utils::Timer::m_text
std::string m_text
identifying text (printed at end).
Definition: Utils.h:79