supereight
perfstats.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2011-2013 Gerhard Reitmayr, TU Graz
3  * SPDX-FileCopyrightText: 2014 University of Edinburgh, Imperial College, University of Manchester
4  * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
5  * SPDX-FileCopyrightText: 2021 Smart Robotics Lab, Imperial College London, Technical University of Munich
6  * SPDX-FileCopyrightText: 2021 Nils Funk
7  * SPDX-FileCopyrightText: 2021 Sotiris Papatheodorou
8  * SPDX-License-Identifier: MIT
9  */
10 
11 /*
12 
13  This code is licensed under the MIT License.
14 
15  */
16 
17 #ifndef SE_PERFSTATS_HPP
18 #define SE_PERFSTATS_HPP
19 
20 #ifdef __APPLE__
21 # include <mach/clock.h>
22 # include <mach/mach.h>
23 #endif
24 
25 
26 #include <Eigen/Dense>
27 #include <algorithm>
28 #include <fstream>
29 #include <iomanip>
30 #include <map>
31 #include <mutex>
32 #include <numeric>
33 #include <ostream>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 struct PerfStats {
39  enum Type {
50  INT,
60  };
61 
62  struct Stats {
64 
71  static double meanIter(const std::vector<double>& iter_data_vec);
72 
80  static double lastIter(const std::vector<double>& iter_data_vec);
81 
89  static double minIter(const std::vector<double>& iter_data_vec);
90 
98  static double maxIter(const std::vector<double>& iter_data_vec);
99 
107  static double sumIter(const std::vector<double>& iter_data_vec);
108 
117  static double mergeIter(const std::vector<double>& iter_data_vec, const Type type);
118 
119 
121 
128  double meanIter(const size_t iter);
129 
137  double lastIter(const size_t iter);
138 
146  double minIter(const size_t iter);
147 
155  double maxIter(const size_t iter);
156 
164  double sumIter(const size_t iter);
165 
174  double mergeIter(const size_t iter);
175 
176 
178 
183  double mean() const;
184 
190  double last() const;
191 
197  double min() const;
198 
204  double max() const;
205 
211  double sum() const;
212 
219  double merge() const;
220 
221 
225  std::string unitString();
226 
227  // <iteration/frame, vector of values at iteration/frame>
228  std::map<size_t, std::vector<double>> data_;
229  bool
231  double last_absolute_;
232  std::mutex mutex_;
234  };
235 
236  struct Results {
237  double mean;
238  double min;
239  double max;
240  double sum;
241  };
242 
243  PerfStats();
244 
245  PerfStats(const bool include_detailed);
246 
252  std::string createHeaderString();
253 
259  std::string createDataIterString();
260 
267  std::string createDataIterString(const size_t iter);
268 
274  std::string createDataString();
275 
281  void debug();
282 
289  const Stats& get(const std::string& key) const
290  {
291  return stats_.find(key)->second;
292  }
293 
300  std::vector<double> getLastData(const std::string& key);
301 
308  double getLastDataMerged(const std::string& key);
309 
316  double getSampleTime(const std::string& key);
317 
323  static double getTime();
324 
331  Type getType(const std::string& key);
332 
338  void reset();
339 
346  void reset(const std::string& key);
347 
357  double sample(const std::string& key,
358  const double value,
359  const Type type = COUNT,
360  const bool detailed = false);
361 
362  double sampleT_WB(const Eigen::Matrix4f& T_WB, const bool detailed = false);
363 
371  double sampleDurationStart(const std::string& key, const bool detailed = false);
378  double sampleDurationEnd(const std::string& key);
379 
385  void setFilestream(std::ofstream* filestream);
386 
392  void includeDetailed(const bool include_detailed)
393  {
394  if (include_detailed != include_detailed_) {
395  filestream_aligned_ = false;
396  ostream_aligned_ = false;
397  }
398  include_detailed_ = include_detailed;
399  };
400 
406  void setIter(const size_t iter)
407  {
408  iter_ = iter;
409  sample("iteration", iter, ITERATION);
410  };
411 
417  void writeToFilestream();
418 
424  void writeToOStream(std::ostream& ostream);
425 
432  void writeSummaryToOStream(std::ostream& ostream, const bool include_iter_data = true);
433 
434  std::vector<PerfStats::Type> header_order_ = {
435  FRAME,
436  ITERATION,
437  TIME,
438  DURATION,
439  MEMORY,
440  POSITION,
441  ORIENTATION,
442  DISTANCE,
443  FREQUENCY,
444  BOOL,
445  POWER,
446  ENERGY,
447  CURRENT,
448  VOLTAGE,
449  COUNT,
450  INT,
451  DOUBLE,
452  PERCENTAGE,
453  UNDEFINED};
455 
457  size_t iter_;
458  std::map<int, std::string>
460  std::map<std::string, Stats> stats_;
461 
463  std::ofstream* filestream_;
466  std::streampos filestream_pos_;
467 
470 };
471 
472 #include "impl/perfstats_impl.hpp"
473 
474 namespace se {
475 
476 
477 
478 extern PerfStats perfstats;
479 
480 
481 } // namespace se
482 
483 #endif // SE_PERFSTATS_HPP
std::ofstream * filestream_
IO function.
Definition: perfstats.hpp:463
void reset()
double sampleT_WB(const Eigen::Matrix4f &T_WB, const bool detailed=false)
bool include_detailed_
Flag to add stats marked as detailed to the output.
Definition: perfstats.hpp:454
Definition: perfstats.hpp:46
std::map< size_t, std::vector< double > > data_
Definition: perfstats.hpp:228
Definition: perfstats.hpp:47
double sampleDurationStart(const std::string &key, const bool detailed=false)
size_t filestream_last_iter_
Definition: perfstats.hpp:465
Definition: perfstats.hpp:44
Definition: perfstats.hpp:49
static double meanIter(const std::vector< double > &iter_data_vec)
Static functions to summarise data of one iteration.
double sample(const std::string &key, const double value, const Type type=COUNT, const bool detailed=false)
Definition: perfstats.hpp:41
Definition: perfstats.hpp:62
double mean() const
Functions to summarise data of the entire stats.
void writeSummaryToOStream(std::ostream &ostream, const bool include_iter_data=true)
Definition: perfstats.hpp:43
std::string createDataIterString()
static double sumIter(const std::vector< double > &iter_data_vec)
Compute the sum of the values of the stats data of an iteration.
static double mergeIter(const std::vector< double > &iter_data_vec, const Type type)
Merge the stats data of one interation into a scalar value.
static double getTime()
bool filestream_aligned_
Definition: perfstats.hpp:464
static double lastIter(const std::vector< double > &iter_data_vec)
Provide the last value of the stats data of an iteration.
std::string createHeaderString()
double mean
Definition: perfstats.hpp:237
double last() const
Provide the last stored value to the stats data.
Definition: perfstats.hpp:56
double getSampleTime(const std::string &key)
bool detailed_
Flag indicating if the stat should be excluded from basic string output.
Definition: perfstats.hpp:230
Definition: perfstats.hpp:57
std::mutex mutex_
Definition: perfstats.hpp:232
double sum
Definition: perfstats.hpp:240
void setIter(const size_t iter)
Set the current iteration and add it to the stats.
Definition: perfstats.hpp:406
double getLastDataMerged(const std::string &key)
double sampleDurationEnd(const std::string &key)
double merge() const
Merge all stats data into a scalar value.
std::map< int, std::string > order_
The order the stats are added to the stats_ map | map idx -> stat name.
Definition: perfstats.hpp:459
std::map< std::string, Stats > stats_
The map stat name -> stat.
Definition: perfstats.hpp:460
std::string createDataString()
void writeToFilestream()
Write performance stats to filestream.
double min() const
Compute the minimum value of all stats data stored so far.
double sum() const
Compute the sum of all stats data stored so far.
static double minIter(const std::vector< double > &iter_data_vec)
Compute the minimum value of the stats data of an iteration.
void includeDetailed(const bool include_detailed)
Set flag to include detailed stats to std::string output.
Definition: perfstats.hpp:392
Definition: perfstats.hpp:53
Definition: perfstats.hpp:45
Definition: perfstats.hpp:59
Definition: perfstats.hpp:52
Definition: perfstats.hpp:55
PerfStats perfstats
std::string unitString()
Definition: perfstats.hpp:38
int insertion_idx_
The index of the next stat to be inserted to performance stats.
Definition: perfstats.hpp:456
Type type_
The type of data stored in the stat struct.
Definition: perfstats.hpp:233
double max
Definition: perfstats.hpp:239
size_t ostream_last_iter_
Definition: perfstats.hpp:469
static double maxIter(const std::vector< double > &iter_data_vec)
Compute the maximum value of the stats data of an iteration.
bool ostream_aligned_
Definition: perfstats.hpp:468
void writeToOStream(std::ostream &ostream)
void debug()
double last_absolute_
The last absolute time the stat data was updated.
Definition: perfstats.hpp:231
Definition: perfstats.hpp:40
std::streampos filestream_pos_
Definition: perfstats.hpp:466
void setFilestream(std::ofstream *filestream)
Type getType(const std::string &key)
Type
Definition: perfstats.hpp:39
Definition: perfstats.hpp:54
Definition: perfstats.hpp:236
double min
Definition: perfstats.hpp:238
Definition: perfstats.hpp:51
std::vector< double > getLastData(const std::string &key)
Definition: perfstats.hpp:50
std::vector< PerfStats::Type > header_order_
The order the different types are added to the output.
Definition: perfstats.hpp:434
Definition: perfstats.hpp:58
double max() const
Compute the maximum value of all stats data stored so far.
Helper wrapper to allocate and de-allocate octants in the octree.
Definition: colour_utils.hpp:17
Definition: perfstats.hpp:48
size_t iter_
The current iteration.
Definition: perfstats.hpp:457
Definition: perfstats.hpp:42