ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTimer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTimer.cc
1 // $Id: PHTimer.C,v 1.2 2010/12/05 02:30:28 bbannier Exp $
2 
11 #include "PHTimer.h"
12 
13 #include <climits>
14 #include <cmath>
15 #include <cstddef>
16 #include <fstream>
17 #include <stdexcept>
18 
19 //______________________________________________________________________
20 // static members
22 const double PHTimer::_twopower32 = pow(2, 32);
23 
24 //______________________________________________________________________
25 void PHTimer::Frequency::set_cpu_freq(const std::string &path)
26 {
27  // Set the default to 2 GHz
28  _frequency = 2e9;
29 
30  // Open the cpuinfo file
31  std::ifstream cpuProcFile(path);
32  if (!cpuProcFile.is_open())
33  throw std::runtime_error(std::string("cpu info. unavailable"));
34  else
35  {
36  // Now parse it looking for the string "cpu MHz"
37  char readLine[1024];
38  std::string searchString("cpu MHz");
39  while ((cpuProcFile.rdstate() & std::ios::failbit) == 0)
40  {
41  // Read into the raw char array and then construct a std::string
42  // (std::string) to do the searching
43  cpuProcFile.getline(readLine, 1024);
44  std::string readLineString(readLine);
45  if (readLineString.find(searchString) != std::string::npos)
46  {
47  // Now look for the :, the clock frequency will follow it
48  size_t semicolonPosition = readLineString.find(':', 0);
49  if (semicolonPosition == std::string::npos) throw std::runtime_error(std::string("wrong format for cpu info file"));
50  std::string frequencyString(readLineString.substr(semicolonPosition + 1));
51 
52  // Make a string stream for the conversion to floating number
53  double freqMHz = 0;
54  std::istringstream frequencySstream(frequencyString);
55 
56  frequencySstream >> freqMHz;
57  _frequency = freqMHz * 1e6;
58  }
59  }
60  }
61 }
62 
63 //______________________________________________________________________
65 {
66  unsigned long diff_high = t0._high - t1._high;
67  unsigned long diff_low;
68  if (t0._low < t1._low)
69  {
70  --diff_high;
71  diff_low = (UINT_MAX - t1._low) + t0._low + 1;
72  }
73  else
74  diff_low = t0._low - t1._low;
75 
76  return (_twopower32 * diff_high + diff_low) * _frequency.period();
77 }
78 
79 //_______________________________________________________
80 void PHTimer::PRINT(std::ostream& os, const std::string& message)
81 {
82  const int max_col = 80;
83  if (!message.size())
84  {
85  os << std::string(max_col, '-') << std::endl;
86  return;
87  }
88  int fill = max_col - message.size() - 2;
89  int pre = static_cast<int>(std::floor(fill / 2.0));
90  int post = fill - pre;
91  os << std::string(pre, '-') << " ";
92  os << message << " ";
93  os << std::string(post, '-') << std::endl;
94 }