NVIDIA DeepStream SDK API Reference

7.0 Release
profiling.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef DS3D_COMMON_HPP_PROFILING_HPP
14 #define DS3D_COMMON_HPP_PROFILING_HPP
15 
16 // inlcude all ds3d hpp header files
17 #include <ds3d/common/common.h>
18 #include <sys/time.h>
19 #include <chrono>
20 
21 namespace ds3d { namespace profiling {
22 
24 public:
25  FpsCalculation(uint32_t interval) : _max_frame_nums(interval) {}
26  float updateFps(uint32_t source_id)
27  {
28  struct timeval time_now;
29  gettimeofday(&time_now, nullptr);
30  double now = (double)time_now.tv_sec + time_now.tv_usec / (double)1000000; // second
31  float fps = -1.0f;
32  auto iSrc = _timestamps.find(source_id);
33  if (iSrc != _timestamps.end()) {
34  auto& tms = iSrc->second;
35  fps = tms.size() / (now - tms.front());
36  while (tms.size() >= _max_frame_nums) {
37  tms.pop();
38  }
39  } else {
40  iSrc = _timestamps.emplace(source_id, std::queue<double>()).first;
41  }
42  iSrc->second.push(now);
43 
44  return fps;
45  }
46 
47 private:
48  std::unordered_map<uint32_t, std::queue<double>> _timestamps;
49  uint32_t _max_frame_nums = 50;
50 };
51 
52 class Timing {
53 public:
54  Timing(uint32_t maxSlots = 50) : _maxTimeLotNum(maxSlots) {}
55  ~Timing() = default;
56  void push(double t)
57  {
58  _timelots.push(t);
59  _total += t;
60  DS_ASSERT(_maxTimeLotNum > 0);
61  while (_timelots.size() > _maxTimeLotNum) {
62  _total -= _timelots.front();
63  _timelots.pop();
64  }
65  }
66  double avg() const
67  {
68  if (_timelots.size()) {
69  return _total / _timelots.size();
70  }
71  return 0;
72  }
73 
74 private:
75  std::queue<double> _timelots;
76  double _total = 0;
77  uint32_t _maxTimeLotNum = 0;
78 };
79 
80 class FileWriter {
81  std::ofstream _file;
82  std::string _path;
83 
84 public:
85  FileWriter() = default;
86  ~FileWriter() { close(); }
87 
88  bool open(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::binary)
89  {
90  _path = path;
91  _file.open(path.c_str(), mode);
92  return _file.is_open();
93  }
94  bool isOpen() const { return _file.is_open(); }
95 
96  void close()
97  {
98  if (_file.is_open()) {
99  _file.close();
100  }
101  }
102 
103  bool write(const void* buf, size_t size)
104  {
105  DS_ASSERT(_file.is_open());
106  return _file.write((const char*)buf, size).good();
107  }
108 };
109 
110 class FileReader {
111  std::ifstream _file;
112  std::string _path;
113 
114 public:
115  FileReader() = default;
117 
118  bool open(const std::string& path, std::ios::openmode mode = std::ios::in | std::ios::binary)
119  {
120  _path = path;
121  _file.open(path.c_str(), mode);
122  return _file.is_open();
123  }
124  bool isOpen() const { return _file.is_open(); }
125  bool eof() const { return _file.eof(); }
126 
127  void close()
128  {
129  if (_file.is_open()) {
130  _file.close();
131  }
132  }
133 
134  int32_t read(void* buf, size_t size)
135  {
136  DS_ASSERT(_file.is_open());
137  if (_file) {
138  return (int32_t)_file.readsome((char*)buf, size);
139  }
140  return -1;
141  }
142 };
143 
144 }} // namespace ds3d::profiling
145 
146 #endif // DS3D_COMMON_HPP_PROFILING_HPP
ds3d::profiling::FileWriter::open
bool open(const std::string &path, std::ios::openmode mode=std::ios::out|std::ios::binary)
Definition: profiling.hpp:88
DS_ASSERT
#define DS_ASSERT(...)
Definition: defines.h:31
ds3d::profiling::FileWriter
Definition: profiling.hpp:80
ds3d::profiling::Timing::~Timing
~Timing()=default
ds3d::profiling::FpsCalculation::FpsCalculation
FpsCalculation(uint32_t interval)
Definition: profiling.hpp:25
ds3d::profiling::FileReader::~FileReader
~FileReader()
Definition: profiling.hpp:116
ds3d::profiling::Timing::Timing
Timing(uint32_t maxSlots=50)
Definition: profiling.hpp:54
ds3d::profiling::FileWriter::close
void close()
Definition: profiling.hpp:96
ds3d::profiling::FpsCalculation::updateFps
float updateFps(uint32_t source_id)
Definition: profiling.hpp:26
ds3d::profiling::FileReader::close
void close()
Definition: profiling.hpp:127
ds3d::profiling::FileWriter::write
bool write(const void *buf, size_t size)
Definition: profiling.hpp:103
ds3d::profiling::FileReader::FileReader
FileReader()=default
ds3d::profiling::Timing
Definition: profiling.hpp:52
ds3d::profiling::FileReader::open
bool open(const std::string &path, std::ios::openmode mode=std::ios::in|std::ios::binary)
Definition: profiling.hpp:118
ds3d::profiling::FileReader::read
int32_t read(void *buf, size_t size)
Definition: profiling.hpp:134
ds3d::profiling::FileWriter::~FileWriter
~FileWriter()
Definition: profiling.hpp:86
ds3d::profiling::Timing::avg
double avg() const
Definition: profiling.hpp:66
ds3d::profiling::FileWriter::FileWriter
FileWriter()=default
common.h
ds3d::profiling::FileWriter::isOpen
bool isOpen() const
Definition: profiling.hpp:94
ds3d::profiling::FpsCalculation
Definition: profiling.hpp:23
ds3d::profiling::Timing::push
void push(double t)
Definition: profiling.hpp:56
ds3d::profiling::FileReader::isOpen
bool isOpen() const
Definition: profiling.hpp:124
ds3d::profiling::FileReader::eof
bool eof() const
Definition: profiling.hpp:125
ds3d
Definition: lidar_3d_datatype.h:35
ds3d::profiling::FileReader
Definition: profiling.hpp:110