Skip to main content

timings.cpp File

C++ source file with implementations for the µTest++ timings methods. More...

Included Headers

#include <cassert> #include "micro-os-plus/micro-test-plus/timings.h"

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacedetail

Internal implementation details for the µTest++ framework. More...

Description

C++ source file with implementations for the µTest++ timings methods.

This source file contains the implementations for the timestamp and timestamps classes of the µTest++ framework. It provides methods for capturing monotonic clock values, checking whether valid timestamps have been recorded, and computing the elapsed time in milliseconds and microseconds between a begin and an end timestamp.

All definitions reside within the micro_os_plus::micro_test_plus namespace.

This file must be included when building the µTest++ library.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
35
36// ----------------------------------------------------------------------------
37
38#include <cassert>
39
40#if defined(MICRO_OS_PLUS_INCLUDE_CONFIG_H)
41#include <micro-os-plus/config.h>
42#endif // MICRO_OS_PLUS_INCLUDE_CONFIG_H
43
45
46// ----------------------------------------------------------------------------
47
48#if defined(__GNUC__)
49#pragma GCC diagnostic ignored "-Waggregate-return"
50#if defined(__clang__)
51#pragma clang diagnostic ignored "-Wpre-c++17-compat"
52#pragma clang diagnostic ignored "-Wc++98-compat"
53#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
54#endif
55#endif
56
57// ============================================================================
58
60{
61 // --------------------------------------------------------------------------
62
72 {
73#if defined(_WIN32)
74 timespec_get (&value_, TIME_UTC);
75#elif defined(CLOCK_MONOTONIC)
76 clock_gettime (CLOCK_MONOTONIC, &value_);
77#endif
78 }
79
86 bool
87 timestamp::has_clock (void) const noexcept
88 {
89 return value_.tv_sec != 0 || value_.tv_nsec != 0;
90 }
91
92 // --------------------------------------------------------------------------
93
101 void
103 {
104 // Ensure it is timestamped only once.
105 if (!begin_time_.has_value ())
106 {
107 begin_time_.emplace ();
108 }
109 }
110
117 void
118 timestamps::timestamp_begin (const timespec& ts) noexcept
119 {
120 // Ensure it is timestamped only once.
121 if (!begin_time_.has_value ())
122 {
123 begin_time_.emplace (ts);
124 }
125 }
126
134 void
136 {
137 // Ensure it is timestamped only once.
138 if (!end_time_.has_value ())
139 {
140 end_time_.emplace ();
141 }
142 }
143
150 void
151 timestamps::timestamp_end (const timespec& ts) noexcept
152 {
153 // Ensure it is timestamped only once.
154 if (!end_time_.has_value ())
155 {
156 end_time_.emplace (ts);
157 }
158 }
159
166 bool
167 timestamps::has_timestamps (void) const noexcept
168 {
169 return begin_time_.has_value () && begin_time_->has_clock ()
170 && end_time_.has_value () && end_time_->has_clock ();
171 }
172
183 void
184 timestamps::compute_elapsed_time (uint32_t& milliseconds,
185 uint32_t& microseconds) const
186 {
187 assert (has_timestamps ());
188
189 // Precondition: has_timestamps() must be true before calling this method.
190 // Invoking it with disengaged optionals is undefined behaviour.
191 long long delta_ns
192 = end_time_->value ().tv_nsec - begin_time_->value ().tv_nsec;
193 long long delta_s
194 = end_time_->value ().tv_sec - begin_time_->value ().tv_sec;
195 if (delta_ns < 0)
196 {
197 delta_ns += 1000000000LL;
198 --delta_s;
199 }
200
201 // Split into milliseconds and microseconds.
202 const long long total_us = delta_s * 1000000LL + delta_ns / 1000LL;
203 milliseconds = static_cast<uint32_t> (total_us / 1000LL);
204 microseconds = static_cast<uint32_t> (total_us % 1000LL);
205 }
206
207 // --------------------------------------------------------------------------
208} // namespace micro_os_plus::micro_test_plus::detail
209
210// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.