Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:27

0001 // $Id: PHTimer.C,v 1.2 2010/12/05 02:30:28 bbannier Exp $
0002 
0003 /*!
0004 \file    PHTimer.cxx
0005 \brief   high precision timer
0006 \author  Sean Kelly, Hugo Pereira
0007 \version $Revision: 1.2 $
0008 \date    $Date: 2010/12/05 02:30:28 $
0009 */
0010 
0011 #include "PHTimer.h"
0012 
0013 #include <climits>
0014 #include <cmath>
0015 #include <cstddef>
0016 #include <fstream>
0017 #include <stdexcept>
0018 
0019 //______________________________________________________________________
0020 // static members
0021 PHTimer::Frequency PHTimer::_frequency = PHTimer::Frequency();
0022 const double PHTimer::_twopower32 = pow(2, 32);
0023 
0024 //______________________________________________________________________
0025 void PHTimer::Frequency::set_cpu_freq(const std::string& path)
0026 {
0027   // Set the default to 2 GHz
0028   _frequency = 2e9;
0029 
0030   // Open the cpuinfo file
0031   std::ifstream cpuProcFile(path);
0032   if (!cpuProcFile.is_open())
0033   {
0034     throw std::runtime_error(std::string("cpu info. unavailable"));
0035   }
0036 
0037   // Now parse it looking for the string "cpu MHz"
0038   char readLine[1024];
0039   std::string searchString("cpu MHz");
0040   while ((cpuProcFile.rdstate() & std::ios::failbit) == 0)
0041   {
0042     // Read into the raw char array and then construct a std::string
0043     // (std::string) to do the searching
0044     cpuProcFile.getline(readLine, 1024);
0045     std::string readLineString(readLine);
0046     if (readLineString.find(searchString) != std::string::npos)
0047     {
0048       // Now look for the :, the clock frequency will follow it
0049       size_t semicolonPosition = readLineString.find(':', 0);
0050       if (semicolonPosition == std::string::npos)
0051       {
0052         throw std::runtime_error(std::string("wrong format for cpu info file"));
0053       }
0054       std::string frequencyString(readLineString.substr(semicolonPosition + 1));
0055 
0056       // Make a string stream for the conversion to floating number
0057       double freqMHz = 0;
0058       std::istringstream frequencySstream(frequencyString);
0059 
0060       frequencySstream >> freqMHz;
0061       _frequency = freqMHz * 1e6;
0062     }
0063   }
0064 }
0065 
0066 //______________________________________________________________________
0067 double PHTimer::get_difference(const PHTimer::time_struct& t0, const PHTimer::time_struct& t1)
0068 {
0069   unsigned long diff_high = t0._high - t1._high;
0070   unsigned long diff_low;
0071   if (t0._low < t1._low)
0072   {
0073     --diff_high;
0074     diff_low = (UINT_MAX - t1._low) + t0._low + 1;
0075   }
0076   else
0077   {
0078     diff_low = t0._low - t1._low;
0079   }
0080 
0081   return (_twopower32 * diff_high + diff_low) * _frequency.period();
0082 }
0083 
0084 //_______________________________________________________
0085 void PHTimer::PRINT(std::ostream& os, const std::string& message)
0086 {
0087   const int max_col = 80;
0088   if (message.empty())
0089   {
0090     os << std::string(max_col, '-') << std::endl;
0091     return;
0092   }
0093   int fill = max_col - message.size() - 2;
0094   int pre = static_cast<int>(std::floor(fill / 2.0));
0095   int post = fill - pre;
0096   os << std::string(pre, '-') << " ";
0097   os << message << " ";
0098   os << std::string(post, '-') << std::endl;
0099 }