A Discrete-Event Network Simulator
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 
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
65 {
66  NS_LOG_FUNCTION (this);
67  m_startTime = times (&m_startTimes);
68 }
69 
70 int64_t
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
127 {
128  NS_LOG_FUNCTION (this);
129  return m_elapsedReal;
130 }
131 
132 int64_t
134 {
135  NS_LOG_FUNCTION (this);
136  return m_elapsedUser;
137 }
138 
139 int64_t
141 {
142  NS_LOG_FUNCTION (this);
143  return m_elapsedSystem;
144 }
145 
147  : m_priv (new SystemWallClockMsPrivate ())
148 {
149  NS_LOG_FUNCTION (this);
150 }
151 
153 {
154  NS_LOG_FUNCTION (this);
155  delete m_priv;
156  m_priv = 0;
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this);
163  m_priv->Start ();
164 }
165 
166 int64_t
168 {
169  NS_LOG_FUNCTION (this);
170  return m_priv->End ();
171 }
172 
173 int64_t
175 {
176  NS_LOG_FUNCTION (this);
177  return m_priv->GetElapsedReal ();
178 }
179 
180 int64_t
182 {
183  NS_LOG_FUNCTION (this);
184  return m_priv->GetElapsedUser ();
185 }
186 
187 int64_t
189 {
190  NS_LOG_FUNCTION (this);
191  return m_priv->GetElapsedSystem ();
192 }
193 
194 } // namespace ns3
int64_t m_elapsedReal
Elapsed real time, in ms.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void Start(void)
Start a measure.
int64_t End(void)
Stop measuring the time since Start() was called.
int64_t GetElapsedSystem(void) const
ns3::SystemWallClockMs declaration.
void Start(void)
Start a measure.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
System-dependent implementation for SystemWallClockMs.
int64_t m_elapsedSystem
Elapsed system time, in ms.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
clock_t m_startTime
Native real time.
class SystemWallClockMsPrivate * m_priv
The implementation.
int64_t m_elapsedUser
Elapsed user time, in ms.
struct tms m_startTimes
The native time structure.
Debug message logging.
int64_t End(void)
Stop measuring the time since Start() was called.
NS_ABORT_x macro definitions.