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