A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
system-wall-clock-ms.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
7 */
8
10
11#include "log.h"
12
13#include <chrono>
14
15/**
16 * \file
17 * \ingroup system
18 * ns3::SystemWallClockMs and ns3::SystemWallClockMsPrivate implementation.
19 */
20
21namespace ns3
22{
23
24NS_LOG_COMPONENT_DEFINE("SystemWallClockMs");
25
26/**
27 * \ingroup system
28 * \brief System-dependent implementation for SystemWallClockMs
29 */
31{
32 public:
33 /** \copydoc SystemWallClockMs::Start() */
34 void Start();
35 /** \copydoc SystemWallClockMs::End() */
36 int64_t End();
37 /** \copydoc SystemWallClockMs::GetElapsedReal() */
38 int64_t GetElapsedReal() const;
39 /** \copydoc SystemWallClockMs::GetElapsedUser() */
40 int64_t GetElapsedUser() const;
41 /** \copydoc SystemWallClockMs::GetElapsedSystem() */
42 int64_t GetElapsedSystem() const;
43
44 private:
45 std::chrono::system_clock::time_point m_startTime; //!< The wall clock start time.
46 int64_t m_elapsedReal; //!< Elapsed real time, in ms.
47 int64_t m_elapsedUser; //!< Elapsed user time, in ms.
48 int64_t m_elapsedSystem; //!< Elapsed system time, in ms.
49};
50
51void
53{
54 NS_LOG_FUNCTION(this);
55 m_startTime = std::chrono::system_clock::now();
56}
57
58int64_t
60{
61 //
62 // We need to return the number of milliseconds that have elapsed in some
63 // reasonably portable way. The underlying function that we will use returns
64 // a number of elapsed ticks. We can look up the number of ticks per second
65 // from the system configuration.
66 //
67 // Conceptually, we need to find the number of elapsed clock ticks and then
68 // multiply the result by the milliseconds per clock tick (or just as easily
69 // divide by clock ticks per millisecond). Integer dividing by clock ticks
70 // per millisecond is bad since this number is fractional on most machines
71 // and would result in divide by zero errors due to integer rounding.
72 //
73 // Multiplying by milliseconds per clock tick works up to a clock resolution
74 // of 1000 ticks per second. If we go past this point, we begin to get zero
75 // elapsed times when millisecondsPerTick becomes fractional and another
76 // rounding error appears.
77 //
78 // So rounding errors using integers can bite you from two direction. Since
79 // all of our targets have math coprocessors, why not just use doubles
80 // internally? Works fine, lasts a long time.
81 //
82 // If millisecondsPerTick becomes fractional, and an elapsed time greater than
83 // a millisecond is measured, the function will work as expected. If an elapsed
84 // time is measured that turns out to be less than a millisecond, we'll just
85 // return zero which would, I think, also will be expected.
86 //
87 NS_LOG_FUNCTION(this);
88
89 auto endTime = std::chrono::system_clock::now();
90
91 std::chrono::duration<double> elapsed_seconds = endTime - m_startTime;
92 m_elapsedReal = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count();
93
94 //
95 // Nothing like this in MinGW, for example.
96 //
97 m_elapsedUser = 0;
99
100 return m_elapsedReal;
101}
102
103int64_t
109
110int64_t
116
117int64_t
123
129
131{
132 NS_LOG_FUNCTION(this);
133 delete m_priv;
134 m_priv = nullptr;
135}
136
137void
143
144int64_t
146{
147 NS_LOG_FUNCTION(this);
148 return m_priv->End();
149}
150
151int64_t
153{
154 NS_LOG_FUNCTION(this);
155 return m_priv->GetElapsedReal();
156}
157
158int64_t
160{
161 NS_LOG_FUNCTION(this);
162 return m_priv->GetElapsedUser();
163}
164
165int64_t
171
172} // namespace ns3
class SystemWallClockMsPrivate * m_priv
The implementation.
int64_t End()
Stop measuring the time since Start() was called.
void Start()
Start a measure.
System-dependent implementation for SystemWallClockMs.
std::chrono::system_clock::time_point m_startTime
The wall clock start time.
int64_t m_elapsedUser
Elapsed user time, in ms.
int64_t m_elapsedSystem
Elapsed system time, in ms.
int64_t End()
Stop measuring the time since Start() was called.
int64_t m_elapsedReal
Elapsed real time, in ms.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::SystemWallClockMs declaration.