NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/common/timestamp.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <chrono>
7 #include <cstdint>
8 
9 #include "opentelemetry/version.h"
10 
12 namespace common
13 {
20 {
21 public:
25  SystemTimestamp() noexcept : nanos_since_epoch_{0} {}
26 
32  template <class Rep, class Period>
33  explicit SystemTimestamp(const std::chrono::duration<Rep, Period> &time_since_epoch) noexcept
34  : nanos_since_epoch_{static_cast<int64_t>(
35  std::chrono::duration_cast<std::chrono::nanoseconds>(time_since_epoch).count())}
36  {}
37 
44  : SystemTimestamp{time_point.time_since_epoch()}
45  {}
46 
52  operator std::chrono::system_clock::time_point() const noexcept
53  {
55  std::chrono::duration_cast<std::chrono::system_clock::duration>(
56  std::chrono::nanoseconds{nanos_since_epoch_})};
57  }
58 
64  std::chrono::nanoseconds time_since_epoch() const noexcept
65  {
66  return std::chrono::nanoseconds{nanos_since_epoch_};
67  }
68 
74  bool operator==(const SystemTimestamp &other) const noexcept
75  {
76  return nanos_since_epoch_ == other.nanos_since_epoch_;
77  }
78 
84  bool operator!=(const SystemTimestamp &other) const noexcept
85  {
86  return nanos_since_epoch_ != other.nanos_since_epoch_;
87  }
88 
89 private:
90  int64_t nanos_since_epoch_;
91 };
92 
99 {
100 public:
104  SteadyTimestamp() noexcept : nanos_since_epoch_{0} {}
105 
111  template <class Rep, class Period>
112  explicit SteadyTimestamp(const std::chrono::duration<Rep, Period> &time_since_epoch) noexcept
113  : nanos_since_epoch_{static_cast<int64_t>(
114  std::chrono::duration_cast<std::chrono::nanoseconds>(time_since_epoch).count())}
115  {}
116 
123  : SteadyTimestamp{time_point.time_since_epoch()}
124  {}
125 
131  operator std::chrono::steady_clock::time_point() const noexcept
132  {
134  std::chrono::duration_cast<std::chrono::steady_clock::duration>(
135  std::chrono::nanoseconds{nanos_since_epoch_})};
136  }
137 
143  std::chrono::nanoseconds time_since_epoch() const noexcept
144  {
145  return std::chrono::nanoseconds{nanos_since_epoch_};
146  }
147 
153  bool operator==(const SteadyTimestamp &other) const noexcept
154  {
155  return nanos_since_epoch_ == other.nanos_since_epoch_;
156  }
157 
163  bool operator!=(const SteadyTimestamp &other) const noexcept
164  {
165  return nanos_since_epoch_ != other.nanos_since_epoch_;
166  }
167 
168 private:
169  int64_t nanos_since_epoch_;
170 };
171 
173 {
174 public:
175  template <class Rep, class Period>
176  static std::chrono::duration<Rep, Period> AdjustWaitForTimeout(
177  std::chrono::duration<Rep, Period> timeout,
178  std::chrono::duration<Rep, Period> indefinite_value) noexcept
179  {
180  // Do not call now() when this duration is max value, now() may have a expensive cost.
181  if (timeout == (std::chrono::duration<Rep, Period>::max)())
182  {
183  return indefinite_value;
184  }
185 
186  // std::future<T>::wait_for, std::this_thread::sleep_for, and std::condition_variable::wait_for
187  // may use steady_clock or system_clock.We need make sure now() + timeout do not overflow.
188  auto max_timeout = std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(
189  (std::chrono::steady_clock::time_point::max)() - std::chrono::steady_clock::now());
190  if (timeout >= max_timeout)
191  {
192  return indefinite_value;
193  }
194  max_timeout = std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(
195  (std::chrono::system_clock::time_point::max)() - std::chrono::system_clock::now());
196  if (timeout >= max_timeout)
197  {
198  return indefinite_value;
199  }
200 
201  return timeout;
202  }
203 };
204 
205 } // namespace common
common::SteadyTimestamp::SteadyTimestamp
SteadyTimestamp() noexcept
Initializes a monotonic timestamp pointing to the start of the epoch.
Definition: includes/opentelemetry/common/timestamp.h:104
common::SystemTimestamp::SystemTimestamp
SystemTimestamp(const std::chrono::duration< Rep, Period > &time_since_epoch) noexcept
Initializes a system timestamp from a duration.
Definition: includes/opentelemetry/common/timestamp.h:33
common::SystemTimestamp::SystemTimestamp
SystemTimestamp(const std::chrono::system_clock::time_point &time_point) noexcept
Initializes a system timestamp based on a point in time.
Definition: includes/opentelemetry/common/timestamp.h:43
common::SystemTimestamp
A timepoint relative to the system clock epoch.
Definition: includes/opentelemetry/common/timestamp.h:19
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
common::SystemTimestamp::SystemTimestamp
SystemTimestamp() noexcept
Initializes a system timestamp pointing to the start of the epoch.
Definition: includes/opentelemetry/common/timestamp.h:25
common::SystemTimestamp::operator!=
bool operator!=(const SystemTimestamp &other) const noexcept
Compare two steady time stamps for inequality.
Definition: includes/opentelemetry/common/timestamp.h:84
max
int max(int a, int b)
Compute the component-wise maximum.
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:60
common::SteadyTimestamp::SteadyTimestamp
SteadyTimestamp(const std::chrono::steady_clock::time_point &time_point) noexcept
Initializes a monotonic timestamp based on a point in time.
Definition: includes/opentelemetry/common/timestamp.h:122
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
common::SteadyTimestamp::operator==
bool operator==(const SteadyTimestamp &other) const noexcept
Compare two steady time stamps.
Definition: includes/opentelemetry/common/timestamp.h:153
common::SteadyTimestamp::SteadyTimestamp
SteadyTimestamp(const std::chrono::duration< Rep, Period > &time_since_epoch) noexcept
Initializes a monotonic timestamp from a duration.
Definition: includes/opentelemetry/common/timestamp.h:112
common::SystemTimestamp::time_since_epoch
std::chrono::nanoseconds time_since_epoch() const noexcept
Returns the nanoseconds since the beginning of the epoch.
Definition: includes/opentelemetry/common/timestamp.h:64
common::DurationUtil
Definition: includes/opentelemetry/common/timestamp.h:172
common::SystemTimestamp::operator==
bool operator==(const SystemTimestamp &other) const noexcept
Compare two steady time stamps.
Definition: includes/opentelemetry/common/timestamp.h:74
common::SteadyTimestamp
A timepoint relative to the monotonic clock epoch.
Definition: includes/opentelemetry/common/timestamp.h:98
common::DurationUtil::AdjustWaitForTimeout
static std::chrono::duration< Rep, Period > AdjustWaitForTimeout(std::chrono::duration< Rep, Period > timeout, std::chrono::duration< Rep, Period > indefinite_value) noexcept
Definition: includes/opentelemetry/common/timestamp.h:176
common::SteadyTimestamp::operator!=
bool operator!=(const SteadyTimestamp &other) const noexcept
Compare two steady time stamps for inequality.
Definition: includes/opentelemetry/common/timestamp.h:163
deepstream::time_point
std::chrono::steady_clock::time_point time_point
Definition: service-maker/sources/modules/measure_fps_probe/measure_fps_probe.hpp:37
common::SteadyTimestamp::time_since_epoch
std::chrono::nanoseconds time_since_epoch() const noexcept
Returns the nanoseconds since the beginning of the epoch.
Definition: includes/opentelemetry/common/timestamp.h:143
common
Definition: includes/opentelemetry/common/attribute_value.h:14