NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/includes/ds3d/common/hpp/profiling.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef DS3D_COMMON_HPP_PROFILING_HPP
19 #define DS3D_COMMON_HPP_PROFILING_HPP
20 
21 // inlcude all ds3d hpp header files
22 #include <ds3d/common/common.h>
23 #include <sys/time.h>
24 #include <chrono>
25 
26 namespace ds3d { namespace profiling {
27 
28 class FpsCalculation {
29 public:
30  FpsCalculation(uint32_t interval) : _max_frame_nums(interval) {}
31  float updateFps(uint32_t source_id)
32  {
33  struct timeval time_now;
34  gettimeofday(&time_now, nullptr);
35  double now = (double)time_now.tv_sec + time_now.tv_usec / (double)1000000; // second
36  float fps = -1.0f;
37  auto iSrc = _timestamps.find(source_id);
38  if (iSrc != _timestamps.end()) {
39  auto& tms = iSrc->second;
40  fps = tms.size() / (now - tms.front());
41  while (tms.size() >= _max_frame_nums) {
42  tms.pop();
43  }
44  } else {
45  iSrc = _timestamps.emplace(source_id, std::queue<double>()).first;
46  }
47  iSrc->second.push(now);
48 
49  return fps;
50  }
51 
52 private:
53  std::unordered_map<uint32_t, std::queue<double>> _timestamps;
54  uint32_t _max_frame_nums = 50;
55 };
56 
57 class Timing {
58 public:
59  Timing(uint32_t maxSlots = 50) : _maxTimeLotNum(maxSlots) {}
60  ~Timing() = default;
61  void push(double t)
62  {
63  _timelots.push(t);
64  _total += t;
65  DS_ASSERT(_maxTimeLotNum > 0);
66  while (_timelots.size() > _maxTimeLotNum) {
67  _total -= _timelots.front();
68  _timelots.pop();
69  }
70  }
71  double avg() const
72  {
73  if (_timelots.size()) {
74  return _total / _timelots.size();
75  }
76  return 0;
77  }
78 
79 private:
80  std::queue<double> _timelots;
81  double _total = 0;
82  uint32_t _maxTimeLotNum = 0;
83 };
84 
85 class FileWriter {
86  std::ofstream _file;
87  std::string _path;
88 
89 public:
90  FileWriter() = default;
91  ~FileWriter() { close(); }
92 
93  bool open(const std::string& path, std::ios::openmode mode = std::ios::out | std::ios::binary)
94  {
95  _path = path;
96  _file.open(path.c_str(), mode);
97  return _file.is_open();
98  }
99  bool isOpen() const { return _file.is_open(); }
100 
101  void close()
102  {
103  if (_file.is_open()) {
104  _file.close();
105  }
106  }
107 
108  bool write(const void* buf, size_t size)
109  {
110  DS_ASSERT(_file.is_open());
111  return _file.write((const char*)buf, size).good();
112  }
113 };
114 
115 class FileReader {
116  std::ifstream _file;
117  std::string _path;
118 
119 public:
120  FileReader() = default;
122 
123  bool open(const std::string& path, std::ios::openmode mode = std::ios::in | std::ios::binary)
124  {
125  _path = path;
126  _file.open(path.c_str(), mode);
127  return _file.is_open();
128  }
129  bool isOpen() const { return _file.is_open(); }
130  bool eof() const { return _file.eof(); }
131 
132  void close()
133  {
134  if (_file.is_open()) {
135  _file.close();
136  }
137  }
138 
139  int32_t read(void* buf, size_t size)
140  {
141  DS_ASSERT(_file.is_open());
142  if (_file) {
143  return (int32_t)_file.readsome((char*)buf, size);
144  }
145  return -1;
146  }
147 };
148 
149 }} // namespace ds3d::profiling
150 
151 #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: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:93
ds3d::profiling::Timing::~Timing
~Timing()=default
ds3d::profiling::FpsCalculation::FpsCalculation
FpsCalculation(uint32_t interval)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:30
ds3d::profiling::FileReader::~FileReader
~FileReader()
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:121
FpsCalculation
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:78
ds3d::profiling::Timing::Timing
Timing(uint32_t maxSlots=50)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:59
ds3d::profiling::FileWriter::close
void close()
Definition: sources/includes/ds3d/common/hpp/profiling.hpp:101
ds3d::profiling::FpsCalculation::updateFps
float updateFps(uint32_t source_id)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:31
ds3d::profiling::FileReader::close
void close()
Definition: sources/includes/ds3d/common/hpp/profiling.hpp:132
DS_ASSERT
#define DS_ASSERT(...)
Definition: sources/includes/ds3d/common/defines.h:36
ds3d::profiling::FileWriter::write
bool write(const void *buf, size_t size)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:108
ds3d::profiling::FileReader::FileReader
FileReader()=default
ds3d::profiling::FileReader::open
bool open(const std::string &path, std::ios::openmode mode=std::ios::in|std::ios::binary)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:123
ds3d::profiling::FileReader::read
int32_t read(void *buf, size_t size)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:139
ds3d::profiling::FileWriter::~FileWriter
~FileWriter()
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:91
ds3d::profiling::Timing::avg
double avg() const
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:71
ds3d::profiling::FileWriter::FileWriter
FileWriter()=default
ds3d::profiling::FileWriter::isOpen
bool isOpen() const
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:99
ds3d::profiling::Timing::push
void push(double t)
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:61
ds3d::profiling::FileReader::isOpen
bool isOpen() const
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:129
ds3d::profiling::FileReader::eof
bool eof() const
Definition: 9.1/sources/includes/ds3d/common/hpp/profiling.hpp:130
ds3d
Definition: sources/includes/ds3d/common/abi_dataprocess.h:25