ReFRACtor
log_timing.cc
Go to the documentation of this file.
1 #include "log_timing.h"
2 #include "logger.h"
3 #include "fp_exception.h"
4 #include <boost/regex.hpp>
5 #include <sys/resource.h>
6 #include <boost/lexical_cast.hpp>
7 #include <unistd.h>
8 #include <fstream>
9 
10 using namespace FullPhysics;
11 
12 //-----------------------------------------------------------------------
15 //-----------------------------------------------------------------------
16 
17 const double LogTiming::cpu_reference_speed = 2992.497;
18 
19 // See base class for description
21 {
22  number_divergent = Solver.number_divergent();
23  number_iteration = Solver.number_iteration();
24  write_to_log("Iteration");
25 }
26 
27 //-----------------------------------------------------------------------
29 //-----------------------------------------------------------------------
30 
32  : number_divergent(-1), number_iteration(-1)
33 {
34  const int host_name_max = 1000;
35  char hostnamebuf[host_name_max];
36  int status = gethostname(hostnamebuf, host_name_max);
37  if(status != 0)
38  throw Exception("Call to gethostname failed");
39  hostname = std::string(hostnamebuf);
40  // The /proc/cpuinfo is Linux specific. If we can't find this file,
41  // don't worry about it. We'll just use default values for
42  // everything.
43  cpu_model = "";
44  speed_mhz = -1;
45  try {
46  std::ifstream proc("/proc/cpuinfo");
47  if(proc.good()) {
48  proc.exceptions(std::ifstream::badbit | std::ifstream::failbit |
49  std::ifstream::eofbit);
50  std::string ln = "";
51  boost::smatch m;
52  while(!boost::regex_match(ln, m,
53  boost::regex("model name\\s*:\\s*(.*)")))
54  getline(proc, ln);
55  cpu_model = m[1];
56  while(!boost::regex_match(ln, m, boost::regex("cpu MHz\\s*:\\s*(.*)")))
57  getline(proc, ln);
58  speed_mhz = boost::lexical_cast<double>(m[1]);
59  }
60  } catch(std::ifstream::failure e) {
61  // Clear any partial initialization.
62  cpu_model = "";
63  speed_mhz = -1;
64  }
65  if(speed_mhz < 0)
66  cpu_factor = 1.0; // Default to not doing CPU correction
67  else
68  cpu_factor = speed_mhz / cpu_reference_speed;
69 }
70 
71 //-----------------------------------------------------------------------
73 //-----------------------------------------------------------------------
74 
75 void LogTiming::write_to_log(const std::string& Prefix) const
76 {
77  // Grab all the timing information at once, so we don't have
78  // inconsistent times in the report.
79  double wtime = wall_clock.elapsed();
80  struct rusage r_usage;
81  int status = getrusage(RUSAGE_SELF, &r_usage);
82  if(status != 0)
83  throw Exception("Call to getrusage failed");
84  double utime = r_usage.ru_utime.tv_sec + r_usage.ru_utime.tv_usec * 1e-6;
85  double stime = r_usage.ru_stime.tv_sec + r_usage.ru_stime.tv_usec * 1e-6;
86 
87  std::ostringstream log;
88  log << "\n---------------------------------------------------------\n"
89  << Prefix << " Timing\n";
90  log << "Host: " << hostname << "\n";
91  if(speed_mhz > 0)
92  log << "CPU Model: " << cpu_model << "\n"
93  << "Speed: " << speed_mhz << " MHz\n"
94  << "CPU Factor: " << cpu_factor << "\n";
95  else {
96  log << "Couldn't get CPU model and speed. Assuming CPU Factor of 1\n"
97  << "CPU Factor: " << cpu_factor << "\n";
98  }
99  log << "Total Wall clock time: " << wtime << " seconds\n"
100  << "Total User time: " << utime << " seconds\n"
101  << "Total System time: " << stime << " seconds\n";
102  // /proc/self/status is Linux specific. If we can't find this file,
103  // don't worry. We just write a message saying this isn't available.
104  try {
105  std::ifstream proc("/proc/self/status");
106  proc.exceptions(std::ifstream::badbit | std::ifstream::failbit |
107  std::ifstream::eofbit);
108  std::string ln = "";
109  boost::smatch m;
110  while(!boost::regex_match(ln, m,
111  boost::regex("VmPeak:\\s*(\\d+)\\s*kB")))
112  getline(proc, ln);
113  double t = boost::lexical_cast<double>(m[1]);
114  log << "Virtual memory maximum: " << (t / 1024) << " MB\n";
115  while(!boost::regex_match(ln, m,
116  boost::regex("VmSize:\\s*(\\d+)\\s*kB")))
117  getline(proc, ln);
118  t = boost::lexical_cast<double>(m[1]);
119  log << "Virtual memory current: " << (t / 1024) << " MB\n";
120  while(!boost::regex_match(ln, m,
121  boost::regex("VmHWM:\\s*(\\d+)\\s*kB")))
122  getline(proc, ln);
123  t = boost::lexical_cast<double>(m[1]);
124  log << "Resident memory maximum: " << (t / 1024) << " MB\n";
125  while(!boost::regex_match(ln, m,
126  boost::regex("VmRSS:\\s*(\\d+)\\s*kB")))
127  getline(proc, ln);
128  t = boost::lexical_cast<double>(m[1]);
129  log << "Resident memory current: " << (t / 1024) << " MB\n";
130  } catch(std::ifstream::failure e) {
131  log << "Memory usage information isn't available on this platform\n"
132  << "(Available on Linux only)\n";
133  }
134  if(number_iteration > 0)
135  log << "Number successful steps: " << number_iteration << "\n"
136  << "Number divergent steps: " << number_divergent << "\n"
137  << "Total iteration: " << number_iteration + number_divergent
138  << "\n"
139  << "Normalized wall clock time per iteration: "
140  << wtime / (number_iteration + number_divergent) * cpu_factor
141  << " seconds\n"
142  << "Normalized user time per iteration: "
143  << utime / (number_iteration + number_divergent) * cpu_factor
144  << " seconds\n"
145  << "Normalized system time per iteration: "
146  << stime / (number_iteration + number_divergent) * cpu_factor
147  << " seconds\n";
148  log << "---------------------------------------------------------\n";
149  Logger::info() << log.str();
150 }
151 
static LogHelper info()
Definition: logger.h:109
virtual void notify_update(const ConnorSolver &Solver)
Called when the Observed object is updated.
Definition: log_timing.cc:20
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
int number_iteration() const
Number of iterations for the last problem solved.
const Unit m("m", 1.0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
virtual void write_to_log(const std::string &Prefix="") const
Write data to disk.
Definition: log_timing.cc:75
int number_divergent() const
Number of divergent steps for the last problem solved.
LogTiming()
Constructor.
Definition: log_timing.cc:31

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10