A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
win32-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 "
system-wall-clock-ms.h
"
22
23
#include <ctime>
24
31
namespace
ns3
{
32
33
NS_LOG_COMPONENT_DEFINE
(
"SystemWallClockMs"
);
34
39
class
SystemWallClockMsPrivate
40
{
41
public
:
43
void
Start
(
void
);
45
int64_t
End
(
void
);
47
int64_t
GetElapsedReal
(
void
)
const
;
49
int64_t
GetElapsedUser
(
void
)
const
;
51
int64_t
GetElapsedSystem
(
void
)
const
;
52
53
private
:
54
clock_t
m_startTime
;
55
int64_t
m_elapsedReal
;
56
int64_t
m_elapsedUser
;
57
int64_t
m_elapsedSystem
;
58
};
59
60
void
61
SystemWallClockMsPrivate::Start
(
void
)
62
{
63
NS_LOG_FUNCTION
(
this
);
64
m_startTime
= std::clock ();
65
}
66
67
int64_t
68
SystemWallClockMsPrivate::End
(
void
)
69
{
70
//
71
// We need to return the number of milliseconds that have elapsed in some
72
// reasonably portable way. The underlying function that we will use returns
73
// a number of elapsed ticks. We can look up the number of ticks per second
74
// from the system configuration.
75
//
76
// Conceptually, we need to find the number of elapsed clock ticks and then
77
// multiply the result by the milliseconds per clock tick (or just as easily
78
// divide by clock ticks per millisecond). Integer dividing by clock ticks
79
// per millisecond is bad since this number is fractional on most machines
80
// and would result in divide by zero errors due to integer rounding.
81
//
82
// Multiplying by milliseconds per clock tick works up to a clock resolution
83
// of 1000 ticks per second. If we go past this point, we begin to get zero
84
// elapsed times when millisecondsPerTick becomes fractional and another
85
// rounding error appears.
86
//
87
// So rounding errors using integers can bite you from two direction. Since
88
// all of our targets have math coprocessors, why not just use doubles
89
// internally? Works fine, lasts a long time.
90
//
91
// If millisecondsPerTick becomes fractional, and an elapsed time greater than
92
// a millisecond is measured, the function will work as expected. If an elapsed
93
// time is measured that turns out to be less than a millisecond, we'll just
94
// return zero which would, I think, also will be expected.
95
//
96
NS_LOG_FUNCTION
(
this
);
97
static
int64_t ticksPerSecond = CLOCKS_PER_SEC;
98
static
double
millisecondsPerTick = 1000. / ticksPerSecond;
99
100
clock_t endTime = std::clock ();
101
102
double
tmp;
103
104
tmp =
static_cast<
double
>
(endTime -
m_startTime
) * millisecondsPerTick;
105
m_elapsedReal
=
static_cast<
int64_t
>
(tmp);
106
107
//
108
// Nothing like this in MinGW, for example.
109
//
110
m_elapsedUser
= 0;
111
m_elapsedSystem
= 0;
112
113
return
m_elapsedReal
;
114
}
115
116
int64_t
117
SystemWallClockMsPrivate::GetElapsedReal
(
void
)
const
118
{
119
NS_LOG_FUNCTION
(
this
);
120
return
m_elapsedReal
;
121
}
122
123
int64_t
124
SystemWallClockMsPrivate::GetElapsedUser
(
void
)
const
125
{
126
NS_LOG_FUNCTION
(
this
);
127
return
m_elapsedUser
;
128
}
129
130
int64_t
131
SystemWallClockMsPrivate::GetElapsedSystem
(
void
)
const
132
{
133
NS_LOG_FUNCTION
(
this
);
134
return
m_elapsedSystem
;
135
}
136
137
SystemWallClockMs::SystemWallClockMs
()
138
: m_priv (new SystemWallClockMsPrivate ())
139
{
140
NS_LOG_FUNCTION
(
this
);
141
}
142
143
SystemWallClockMs::~SystemWallClockMs
()
144
{
145
NS_LOG_FUNCTION
(
this
);
146
delete
m_priv
;
147
m_priv
= 0;
148
}
149
150
void
151
SystemWallClockMs::Start
(
void
)
152
{
153
NS_LOG_FUNCTION
(
this
);
154
m_priv
->
Start
();
155
}
156
157
int64_t
158
SystemWallClockMs::End
(
void
)
159
{
160
NS_LOG_FUNCTION
(
this
);
161
return
m_priv
->
End
();
162
}
163
164
int64_t
165
SystemWallClockMs::GetElapsedReal
(
void
)
const
166
{
167
NS_LOG_FUNCTION
(
this
);
168
return
m_priv
->
GetElapsedReal
();
169
}
170
171
int64_t
172
SystemWallClockMs::GetElapsedUser
(
void
)
const
173
{
174
NS_LOG_FUNCTION
(
this
);
175
return
m_priv
->
GetElapsedUser
();
176
}
177
178
int64_t
179
SystemWallClockMs::GetElapsedSystem
(
void
)
const
180
{
181
NS_LOG_FUNCTION
(
this
);
182
return
m_priv
->
GetElapsedSystem
();
183
}
184
185
}
// namespace ns3
ns3::SystemWallClockMs::m_priv
class SystemWallClockMsPrivate * m_priv
The implementation.
Definition:
system-wall-clock-ms.h:98
ns3::SystemWallClockMs::Start
void Start(void)
Start a measure.
Definition:
unix-system-wall-clock-ms.cc:160
ns3::SystemWallClockMs::SystemWallClockMs
SystemWallClockMs()
Definition:
unix-system-wall-clock-ms.cc:146
ns3::SystemWallClockMs::GetElapsedUser
int64_t GetElapsedUser(void) const
Definition:
unix-system-wall-clock-ms.cc:181
ns3::SystemWallClockMs::End
int64_t End(void)
Stop measuring the time since Start() was called.
Definition:
unix-system-wall-clock-ms.cc:167
ns3::SystemWallClockMs::~SystemWallClockMs
~SystemWallClockMs()
Definition:
unix-system-wall-clock-ms.cc:152
ns3::SystemWallClockMs::GetElapsedSystem
int64_t GetElapsedSystem(void) const
Definition:
unix-system-wall-clock-ms.cc:188
ns3::SystemWallClockMs::GetElapsedReal
int64_t GetElapsedReal(void) const
Definition:
unix-system-wall-clock-ms.cc:174
ns3::SystemWallClockMsPrivate::GetElapsedUser
int64_t GetElapsedUser(void) const
ns3::SystemWallClockMsPrivate::GetElapsedReal
int64_t GetElapsedReal(void) const
ns3::SystemWallClockMsPrivate::Start
void Start(void)
Start a measure.
ns3::SystemWallClockMsPrivate::GetElapsedSystem
int64_t GetElapsedSystem(void) const
ns3::SystemWallClockMsPrivate::m_elapsedUser
int64_t m_elapsedUser
Elapsed user time, in ms.
Definition:
unix-system-wall-clock-ms.cc:59
ns3::SystemWallClockMsPrivate::m_elapsedSystem
int64_t m_elapsedSystem
Elapsed system time, in ms.
Definition:
unix-system-wall-clock-ms.cc:60
ns3::SystemWallClockMsPrivate::m_elapsedReal
int64_t m_elapsedReal
Elapsed real time, in ms.
Definition:
unix-system-wall-clock-ms.cc:58
ns3::SystemWallClockMsPrivate::End
int64_t End(void)
Stop measuring the time since Start() was called.
ns3::SystemWallClockMsPrivate::m_startTime
clock_t m_startTime
Native real time.
Definition:
unix-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:205
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:244
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
win32-system-wall-clock-ms.cc
Generated on Tue Feb 6 2024 19:21:17 for ns-3 by
1.9.1