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