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