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