A Discrete-Event Network Simulator
API
system-wall-clock-ms.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2005 INRIA
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Mathieu Lacage <mathieu.lacage.inria.fr>
19 */
20
21#include <chrono>
22
24#include "log.h"
25
32namespace ns3 {
33
34NS_LOG_COMPONENT_DEFINE ("SystemWallClockMs");
35
41{
42public:
44 void Start (void);
46 int64_t End (void);
48 int64_t GetElapsedReal (void) const;
50 int64_t GetElapsedUser (void) const;
52 int64_t GetElapsedSystem (void) const;
53
54private:
55 std::chrono::system_clock::time_point m_startTime;
56 int64_t m_elapsedReal;
57 int64_t m_elapsedUser;
59};
60
61void
63{
64 NS_LOG_FUNCTION (this);
65 m_startTime = std::chrono::system_clock::now ();
66}
67
68int64_t
70{
71 //
72 // We need to return the number of milliseconds that have elapsed in some
73 // reasonably portable way. The underlying function that we will use returns
74 // a number of elapsed ticks. We can look up the number of ticks per second
75 // from the system configuration.
76 //
77 // Conceptually, we need to find the number of elapsed clock ticks and then
78 // multiply the result by the milliseconds per clock tick (or just as easily
79 // divide by clock ticks per millisecond). Integer dividing by clock ticks
80 // per millisecond is bad since this number is fractional on most machines
81 // and would result in divide by zero errors due to integer rounding.
82 //
83 // Multiplying by milliseconds per clock tick works up to a clock resolution
84 // of 1000 ticks per second. If we go past this point, we begin to get zero
85 // elapsed times when millisecondsPerTick becomes fractional and another
86 // rounding error appears.
87 //
88 // So rounding errors using integers can bite you from two direction. Since
89 // all of our targets have math coprocessors, why not just use doubles
90 // internally? Works fine, lasts a long time.
91 //
92 // If millisecondsPerTick becomes fractional, and an elapsed time greater than
93 // a millisecond is measured, the function will work as expected. If an elapsed
94 // time is measured that turns out to be less than a millisecond, we'll just
95 // return zero which would, I think, also will be expected.
96 //
97 NS_LOG_FUNCTION (this);
98
99 auto endTime = std::chrono::system_clock::now ();
100
101 std::chrono::duration<double> elapsed_seconds = endTime-m_startTime;
102 m_elapsedReal = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed_seconds).count();
103
104 //
105 // Nothing like this in MinGW, for example.
106 //
107 m_elapsedUser = 0;
108 m_elapsedSystem = 0;
109
110 return m_elapsedReal;
111}
112
113int64_t
115{
116 NS_LOG_FUNCTION (this);
117 return m_elapsedReal;
118}
119
120int64_t
122{
123 NS_LOG_FUNCTION (this);
124 return m_elapsedUser;
125}
126
127int64_t
129{
130 NS_LOG_FUNCTION (this);
131 return m_elapsedSystem;
132}
133
135 : m_priv (new SystemWallClockMsPrivate ())
136{
137 NS_LOG_FUNCTION (this);
138}
139
141{
142 NS_LOG_FUNCTION (this);
143 delete m_priv;
144 m_priv = 0;
145}
146
147void
149{
150 NS_LOG_FUNCTION (this);
151 m_priv->Start ();
152}
153
154int64_t
156{
157 NS_LOG_FUNCTION (this);
158 return m_priv->End ();
159}
160
161int64_t
163{
164 NS_LOG_FUNCTION (this);
165 return m_priv->GetElapsedReal ();
166}
167
168int64_t
170{
171 NS_LOG_FUNCTION (this);
172 return m_priv->GetElapsedUser ();
173}
174
175int64_t
177{
178 NS_LOG_FUNCTION (this);
179 return m_priv->GetElapsedSystem ();
180}
181
182} // namespace ns3
class SystemWallClockMsPrivate * m_priv
The implementation.
void Start(void)
Start a measure.
int64_t GetElapsedUser(void) const
int64_t End(void)
Stop measuring the time since Start() was called.
int64_t GetElapsedSystem(void) const
int64_t GetElapsedReal(void) const
System-dependent implementation for SystemWallClockMs.
void Start(void)
Start a measure.
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 m_elapsedReal
Elapsed real time, in ms.
int64_t End(void)
Stop measuring the time since Start() was called.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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.