A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
src
core
model
unix-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
#include "
abort.h
"
23
#include "
log.h
"
24
#include <sys/times.h>
25
#include <unistd.h>
26
33
namespace
ns3
{
34
35
NS_LOG_COMPONENT_DEFINE
(
"SystemWallClockMs"
);
36
41
class
SystemWallClockMsPrivate
{
42
public
:
44
void
Start
(
void
);
46
int64_t
End
(
void
);
48
int64_t
GetElapsedReal
(
void
)
const
;
50
int64_t
GetElapsedUser
(
void
)
const
;
52
int64_t
GetElapsedSystem
(
void
)
const
;
53
54
private
:
55
struct
tms
m_startTimes
;
56
clock_t
m_startTime
;
57
int64_t
m_elapsedReal
;
58
int64_t
m_elapsedUser
;
59
int64_t
m_elapsedSystem
;
60
};
61
62
void
63
SystemWallClockMsPrivate::Start
(
void
)
64
{
65
NS_LOG_FUNCTION
(
this
);
66
m_startTime
= times (&
m_startTimes
);
67
}
68
69
int64_t
70
SystemWallClockMsPrivate::End
(
void
)
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 divide by clock
80
// ticks per millisecond). Integer dividing by clock ticks per millisecond
81
// is bad since this number is fractional on most machines and would result
82
// 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 both 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 milliscond 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
static
int64_t ticksPerSecond = sysconf (_SC_CLK_TCK);
100
static
double
millisecondsPerTick = 1000. / ticksPerSecond;
101
102
//
103
// If sysconf () fails, we have no idea how to do the required conversion to ms.
104
//
105
NS_ABORT_MSG_IF
(ticksPerSecond == -1,
"SystemWallClockMsPrivate(): Cannot sysconf (_SC_CLK_TCK)"
);
106
107
struct
tms endTimes;
108
clock_t endTime = times (&endTimes);
109
110
double
tmp;
111
112
tmp =
static_cast<
double
>
(endTime -
m_startTime
) * millisecondsPerTick;
113
m_elapsedReal
=
static_cast<
int64_t
>
(tmp);
114
115
tmp =
static_cast<
double
>
(endTimes.tms_utime -
m_startTimes
.tms_utime) * millisecondsPerTick;
116
m_elapsedUser
=
static_cast<
int64_t
>
(tmp);
117
118
tmp =
static_cast<
double
>
(endTimes.tms_stime -
m_startTimes
.tms_stime) * millisecondsPerTick;
119
m_elapsedSystem
=
static_cast<
int64_t
>
(tmp);
120
121
return
m_elapsedReal
;
122
}
123
124
int64_t
125
SystemWallClockMsPrivate::GetElapsedReal
(
void
)
const
126
{
127
NS_LOG_FUNCTION
(
this
);
128
return
m_elapsedReal
;
129
}
130
131
int64_t
132
SystemWallClockMsPrivate::GetElapsedUser
(
void
)
const
133
{
134
NS_LOG_FUNCTION
(
this
);
135
return
m_elapsedUser
;
136
}
137
138
int64_t
139
SystemWallClockMsPrivate::GetElapsedSystem
(
void
)
const
140
{
141
NS_LOG_FUNCTION
(
this
);
142
return
m_elapsedSystem
;
143
}
144
145
SystemWallClockMs::SystemWallClockMs
()
146
: m_priv (new
SystemWallClockMsPrivate
())
147
{
148
NS_LOG_FUNCTION
(
this
);
149
}
150
151
SystemWallClockMs::~SystemWallClockMs
()
152
{
153
NS_LOG_FUNCTION
(
this
);
154
delete
m_priv
;
155
m_priv
= 0;
156
}
157
158
void
159
SystemWallClockMs::Start
(
void
)
160
{
161
NS_LOG_FUNCTION
(
this
);
162
m_priv
->
Start
();
163
}
164
165
int64_t
166
SystemWallClockMs::End
(
void
)
167
{
168
NS_LOG_FUNCTION
(
this
);
169
return
m_priv
->
End
();
170
}
171
172
int64_t
173
SystemWallClockMs::GetElapsedReal
(
void
)
const
174
{
175
NS_LOG_FUNCTION
(
this
);
176
return
m_priv
->
GetElapsedReal
();
177
}
178
179
int64_t
180
SystemWallClockMs::GetElapsedUser
(
void
)
const
181
{
182
NS_LOG_FUNCTION
(
this
);
183
return
m_priv
->
GetElapsedUser
();
184
}
185
186
int64_t
187
SystemWallClockMs::GetElapsedSystem
(
void
)
const
188
{
189
NS_LOG_FUNCTION
(
this
);
190
return
m_priv
->
GetElapsedSystem
();
191
}
192
193
}
// 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::SystemWallClockMsPrivate
System-dependent implementation for SystemWallClockMs.
Definition:
unix-system-wall-clock-ms.cc:41
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
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition:
abort.h:108
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::SystemWallClockMsPrivate::m_startTimes
struct tms m_startTimes
The native time structure.
Definition:
unix-system-wall-clock-ms.cc:55
log.h
Debug message logging.
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
abort.h
NS_ABORT_x macro definitions.
Generated on Wed Nov 7 2018 10:01:50 for ns-3 by
1.8.14