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