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 ("SystemWallClockMsPrivate");
36 
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
64 {
65  NS_LOG_FUNCTION (this);
66  m_startTime = times (&m_startTimes);
67 }
68 
69 int64_t
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
126 {
127  NS_LOG_FUNCTION (this);
128  return m_elapsedReal;
129 }
130 
131 int64_t
133 {
134  NS_LOG_FUNCTION (this);
135  return m_elapsedUser;
136 }
137 
138 int64_t
140 {
141  NS_LOG_FUNCTION (this);
142  return m_elapsedSystem;
143 }
144 
146  : m_priv (new SystemWallClockMsPrivate ())
147 {
148  NS_LOG_FUNCTION (this);
149 }
150 
152 {
153  NS_LOG_FUNCTION (this);
154  delete m_priv;
155  m_priv = 0;
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION (this);
162  m_priv->Start ();
163 }
164 
165 int64_t
167 {
168  NS_LOG_FUNCTION (this);
169  return m_priv->End ();
170 }
171 
172 int64_t
174 {
175  NS_LOG_FUNCTION (this);
176  return m_priv->GetElapsedReal ();
177 }
178 
179 int64_t
181 {
182  NS_LOG_FUNCTION (this);
183  return m_priv->GetElapsedUser ();
184 }
185 
186 int64_t
188 {
189  NS_LOG_FUNCTION (this);
190  return m_priv->GetElapsedSystem ();
191 }
192 
193 } // 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:201
void Start(void)
Start a measure.
int64_t End(void)
Stop measuring the time since Start() was called.
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 GetElapsedUser(void) const
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
int64_t GetElapsedReal(void) const
clock_t m_startTime
Native real time.
int64_t GetElapsedSystem(void) const
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.