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
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
unix-system-wall-clock-ms.cc
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
27
NS_LOG_COMPONENT_DEFINE
(
"SystemWallClockMsPrivate"
);
28
29
namespace
ns3 {
30
31
class
SystemWallClockMsPrivate
{
32
public
:
33
void
Start (
void
);
34
int64_t End (
void
);
35
int64_t GetElapsedReal (
void
)
const
;
36
int64_t GetElapsedUser (
void
)
const
;
37
int64_t GetElapsedSystem (
void
)
const
;
38
39
private
:
40
struct
tms m_startTimes;
41
clock_t m_startTime;
42
int64_t m_elapsedReal;
43
int64_t m_elapsedUser;
44
int64_t m_elapsedSystem;
45
};
46
47
void
48
SystemWallClockMsPrivate::Start (
void
)
49
{
50
NS_LOG_FUNCTION
(
this
);
51
m_startTime = times (&m_startTimes);
52
}
53
54
int64_t
55
SystemWallClockMsPrivate::End (
void
)
56
{
57
//
58
// We need to return the number of milliseconds that have elapsed in some
59
// reasonably portable way. The underlying function that we will use returns
60
// a number of elapsed ticks. We can look up the number of ticks per second
61
// from the system configuration.
62
//
63
// Conceptually, we need to find the number of elapsed clock ticks and then
64
// multiply the result by the milliseconds per clock tick (or divide by clock
65
// ticks per millisecond). Integer dividing by clock ticks per millisecond
66
// is bad since this number is fractional on most machines and would result
67
// in divide by zero errors due to integer rounding.
68
//
69
// Multiplying by milliseconds per clock tick works up to a clock resolution
70
// of 1000 ticks per second. If we go past this point, we begin to get zero
71
// elapsed times when millisecondsPerTick becomes fractional and another
72
// rounding error appears.
73
//
74
// So rounding errors using integers can bite you from both direction. Since
75
// all of our targets have math coprocessors, why not just use doubles
76
// internally? Works fine, lasts a long time.
77
//
78
// If millisecondsPerTick becomes fractional, and an elapsed time greater than
79
// a milliscond is measured, the function will work as expected. If an elapsed
80
// time is measured that turns out to be less than a millisecond, we'll just
81
// return zero which would, I think, also will be expected.
82
//
83
NS_LOG_FUNCTION
(
this
);
84
static
int64_t ticksPerSecond = sysconf (_SC_CLK_TCK);
85
static
double
millisecondsPerTick = 1000. / ticksPerSecond;
86
87
//
88
// If sysconf () fails, we have no idea how to do the required conversion to ms.
89
//
90
NS_ABORT_MSG_IF
(ticksPerSecond == -1,
"SystemWallClockMsPrivate(): Cannot sysconf (_SC_CLK_TCK)"
);
91
92
struct
tms endTimes;
93
clock_t endTime = times (&endTimes);
94
95
double
tmp;
96
97
tmp =
static_cast<
double
>
(endTime - m_startTime) * millisecondsPerTick;
98
m_elapsedReal =
static_cast<
int64_t
>
(tmp);
99
100
tmp =
static_cast<
double
>
(endTimes.tms_utime - m_startTimes.tms_utime) * millisecondsPerTick;
101
m_elapsedUser =
static_cast<
int64_t
>
(tmp);
102
103
tmp =
static_cast<
double
>
(endTimes.tms_stime - m_startTimes.tms_stime) * millisecondsPerTick;
104
m_elapsedSystem =
static_cast<
int64_t
>
(tmp);
105
106
return
m_elapsedReal;
107
}
108
109
int64_t
110
SystemWallClockMsPrivate::GetElapsedReal (
void
)
const
111
{
112
NS_LOG_FUNCTION
(
this
);
113
return
m_elapsedReal;
114
}
115
116
int64_t
117
SystemWallClockMsPrivate::GetElapsedUser (
void
)
const
118
{
119
NS_LOG_FUNCTION
(
this
);
120
return
m_elapsedUser;
121
}
122
123
int64_t
124
SystemWallClockMsPrivate::GetElapsedSystem (
void
)
const
125
{
126
NS_LOG_FUNCTION
(
this
);
127
return
m_elapsedSystem;
128
}
129
130
SystemWallClockMs::SystemWallClockMs ()
131
: m_priv (new SystemWallClockMsPrivate ())
132
{
133
NS_LOG_FUNCTION
(
this
);
134
}
135
136
SystemWallClockMs::~SystemWallClockMs ()
137
{
138
NS_LOG_FUNCTION
(
this
);
139
delete
m_priv;
140
m_priv = 0;
141
}
142
143
void
144
SystemWallClockMs::Start
(
void
)
145
{
146
NS_LOG_FUNCTION
(
this
);
147
m_priv->Start ();
148
}
149
150
int64_t
151
SystemWallClockMs::End
(
void
)
152
{
153
NS_LOG_FUNCTION
(
this
);
154
return
m_priv->End ();
155
}
156
157
int64_t
158
SystemWallClockMs::GetElapsedReal
(
void
)
const
159
{
160
NS_LOG_FUNCTION
(
this
);
161
return
m_priv->GetElapsedReal ();
162
}
163
164
int64_t
165
SystemWallClockMs::GetElapsedUser
(
void
)
const
166
{
167
NS_LOG_FUNCTION
(
this
);
168
return
m_priv->GetElapsedUser ();
169
}
170
171
int64_t
172
SystemWallClockMs::GetElapsedSystem
(
void
)
const
173
{
174
NS_LOG_FUNCTION
(
this
);
175
return
m_priv->GetElapsedSystem ();
176
}
177
178
}
// namespace ns3
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
Definition:
log.h:311
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Definition:
log.h:122
ns3::SystemWallClockMs::Start
void Start(void)
Definition:
unix-system-wall-clock-ms.cc:144
ns3::SystemWallClockMsPrivate
Definition:
unix-system-wall-clock-ms.cc:31
ns3::SystemWallClockMs::GetElapsedUser
int64_t GetElapsedUser(void) const
Definition:
unix-system-wall-clock-ms.cc:165
ns3::SystemWallClockMs::GetElapsedReal
int64_t GetElapsedReal(void) const
Definition:
unix-system-wall-clock-ms.cc:158
ns3::SystemWallClockMs::GetElapsedSystem
int64_t GetElapsedSystem(void) const
Definition:
unix-system-wall-clock-ms.cc:172
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition:
abort.h:98
ns3::SystemWallClockMs::End
int64_t End(void)
Stop measuring the time since Start() was called.
Definition:
unix-system-wall-clock-ms.cc:151
src
core
model
unix-system-wall-clock-ms.cc
Generated on Sun Mar 16 2014 11:25:02 for ns-3 by
1.8.6