A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
9
#include "
system-wall-clock-ms.h
"
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
21
namespace
ns3
22
{
23
24
NS_LOG_COMPONENT_DEFINE
(
"SystemWallClockMs"
);
25
26
/**
27
* \ingroup system
28
* \brief System-dependent implementation for SystemWallClockMs
29
*/
30
class
SystemWallClockMsPrivate
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
51
void
52
SystemWallClockMsPrivate::Start
()
53
{
54
NS_LOG_FUNCTION
(
this
);
55
m_startTime
= std::chrono::system_clock::now();
56
}
57
58
int64_t
59
SystemWallClockMsPrivate::End
()
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;
98
m_elapsedSystem
= 0;
99
100
return
m_elapsedReal
;
101
}
102
103
int64_t
104
SystemWallClockMsPrivate::GetElapsedReal
()
const
105
{
106
NS_LOG_FUNCTION
(
this
);
107
return
m_elapsedReal
;
108
}
109
110
int64_t
111
SystemWallClockMsPrivate::GetElapsedUser
()
const
112
{
113
NS_LOG_FUNCTION
(
this
);
114
return
m_elapsedUser
;
115
}
116
117
int64_t
118
SystemWallClockMsPrivate::GetElapsedSystem
()
const
119
{
120
NS_LOG_FUNCTION
(
this
);
121
return
m_elapsedSystem
;
122
}
123
124
SystemWallClockMs::SystemWallClockMs
()
125
: m_priv(new
SystemWallClockMsPrivate
())
126
{
127
NS_LOG_FUNCTION
(
this
);
128
}
129
130
SystemWallClockMs::~SystemWallClockMs
()
131
{
132
NS_LOG_FUNCTION
(
this
);
133
delete
m_priv
;
134
m_priv
=
nullptr
;
135
}
136
137
void
138
SystemWallClockMs::Start
()
139
{
140
NS_LOG_FUNCTION
(
this
);
141
m_priv
->
Start
();
142
}
143
144
int64_t
145
SystemWallClockMs::End
()
146
{
147
NS_LOG_FUNCTION
(
this
);
148
return
m_priv
->
End
();
149
}
150
151
int64_t
152
SystemWallClockMs::GetElapsedReal
()
const
153
{
154
NS_LOG_FUNCTION
(
this
);
155
return
m_priv
->
GetElapsedReal
();
156
}
157
158
int64_t
159
SystemWallClockMs::GetElapsedUser
()
const
160
{
161
NS_LOG_FUNCTION
(
this
);
162
return
m_priv
->
GetElapsedUser
();
163
}
164
165
int64_t
166
SystemWallClockMs::GetElapsedSystem
()
const
167
{
168
NS_LOG_FUNCTION
(
this
);
169
return
m_priv
->
GetElapsedSystem
();
170
}
171
172
}
// namespace ns3
ns3::SystemWallClockMs::m_priv
class SystemWallClockMsPrivate * m_priv
The implementation.
Definition
system-wall-clock-ms.h:72
ns3::SystemWallClockMs::SystemWallClockMs
SystemWallClockMs()
Definition
system-wall-clock-ms.cc:124
ns3::SystemWallClockMs::End
int64_t End()
Stop measuring the time since Start() was called.
Definition
system-wall-clock-ms.cc:145
ns3::SystemWallClockMs::GetElapsedReal
int64_t GetElapsedReal() const
Definition
system-wall-clock-ms.cc:152
ns3::SystemWallClockMs::Start
void Start()
Start a measure.
Definition
system-wall-clock-ms.cc:138
ns3::SystemWallClockMs::GetElapsedUser
int64_t GetElapsedUser() const
Definition
system-wall-clock-ms.cc:159
ns3::SystemWallClockMs::~SystemWallClockMs
~SystemWallClockMs()
Definition
system-wall-clock-ms.cc:130
ns3::SystemWallClockMs::GetElapsedSystem
int64_t GetElapsedSystem() const
Definition
system-wall-clock-ms.cc:166
ns3::SystemWallClockMsPrivate
System-dependent implementation for SystemWallClockMs.
Definition
system-wall-clock-ms.cc:31
ns3::SystemWallClockMsPrivate::Start
void Start()
Start a measure.
Definition
system-wall-clock-ms.cc:52
ns3::SystemWallClockMsPrivate::GetElapsedReal
int64_t GetElapsedReal() const
Definition
system-wall-clock-ms.cc:104
ns3::SystemWallClockMsPrivate::GetElapsedSystem
int64_t GetElapsedSystem() const
Definition
system-wall-clock-ms.cc:118
ns3::SystemWallClockMsPrivate::m_startTime
std::chrono::system_clock::time_point m_startTime
The wall clock start time.
Definition
system-wall-clock-ms.cc:45
ns3::SystemWallClockMsPrivate::m_elapsedUser
int64_t m_elapsedUser
Elapsed user time, in ms.
Definition
system-wall-clock-ms.cc:47
ns3::SystemWallClockMsPrivate::m_elapsedSystem
int64_t m_elapsedSystem
Elapsed system time, in ms.
Definition
system-wall-clock-ms.cc:48
ns3::SystemWallClockMsPrivate::End
int64_t End()
Stop measuring the time since Start() was called.
Definition
system-wall-clock-ms.cc:59
ns3::SystemWallClockMsPrivate::GetElapsedUser
int64_t GetElapsedUser() const
Definition
system-wall-clock-ms.cc:111
ns3::SystemWallClockMsPrivate::m_elapsedReal
int64_t m_elapsedReal
Elapsed real time, in ms.
Definition
system-wall-clock-ms.cc:46
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition
log.h:191
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition
log-macros-enabled.h:229
log.h
Debug message logging.
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
system-wall-clock-ms.h
ns3::SystemWallClockMs declaration.
src
core
model
system-wall-clock-ms.cc
Generated on Fri Nov 8 2024 13:58:59 for ns-3 by
1.11.0