CARLA
Profiler.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #ifndef LIBCARLA_ENABLE_PROFILER
8 # define LIBCARLA_ENABLE_PROFILER
9 #endif // LIBCARLA_ENABLE_PROFILER
10 
11 #include "carla/Logging.h"
12 #include "carla/Version.h"
14 
15 #include <fstream>
16 #include <iomanip>
17 #include <iostream>
18 #include <mutex>
19 
20 namespace carla {
21 namespace profiler {
22 namespace detail {
23 
24  template <typename Arg, typename ... Args>
25  static void write_csv_to_stream(std::ostream &out, Arg &&arg, Args &&... args) {
26  out << std::boolalpha
27  << std::left << std::setw(44)
28  << std::forward<Arg>(arg)
29  << std::right
30  << std::fixed << std::setprecision(2);
31  using expander = int[];
32  (void)expander{0, (void(out << ", " << std::setw(10) << std::forward<Args>(args)),0)...};
33  }
34 
36  public:
37 
38  StaticProfiler(std::string filename)
39  : _filename(std::move(filename)) {
40  logging::log("PROFILER: writing profiling data to", _filename);
41  std::string header = "# LibCarla Profiler ";
42  header += carla::version();
43 #ifdef NDEBUG
44  header += " (release)";
45 #else
46  header += " (debug)";
47 #endif // NDEBUG
48  write_to_file(std::ios_base::out, header);
49  write_line("# context", "average", "maximum", "minimum", "units", "times");
50  }
51 
52  template <typename ... Args>
53  void write_line(Args &&... args) {
54  write_to_file(std::ios_base::app|std::ios_base::out, std::forward<Args>(args)...);
55  }
56 
57  private:
58 
59  template <typename ... Args>
60  void write_to_file(std::ios_base::openmode mode, Args &&... args) {
61  if (!_filename.empty()) {
62  static std::mutex MUTEX;
63  std::lock_guard<std::mutex> guard(MUTEX);
64  std::ofstream file(_filename, mode);
65  write_csv_to_stream(file, std::forward<Args>(args)...);
66  file << std::endl;
67  }
68  }
69 
70  const std::string _filename;
71  };
72 
73  ProfilerData::~ProfilerData() {
74  static StaticProfiler PROFILER{"profiler.csv"};
75  if (_count > 0u) {
76  if (_print_fps) {
77  PROFILER.write_line(_name, fps(average()), fps(minimum()), fps(maximum()), "FPS", _count);
78  } else {
79  PROFILER.write_line(_name, average(), maximum(), minimum(), "ms", _count);
80  }
81  } else {
82  log_error("profiler", _name, " was never run!");
83  }
84  }
85 
86 } // namespace detail
87 } // namespace profiler
88 } // namespace carla
static void log(Args &&... args)
Definition: Logging.h:59
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static void write_csv_to_stream(std::ostream &out, Arg &&arg, Args &&... args)
Definition: Profiler.cpp:25
void write_line(Args &&... args)
Definition: Profiler.cpp:53
static LifetimeProfiler PROFILER
void write_to_file(std::ios_base::openmode mode, Args &&... args)
Definition: Profiler.cpp:60
StaticProfiler(std::string filename)
Definition: Profiler.cpp:38