A Discrete-Event Network Simulator
API
win32-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 
23 #include <ctime>
24 
32 namespace ns3 {
33 
38 class SystemWallClockMsPrivate {
39 public:
41  void Start (void);
43  int64_t End (void);
45  int64_t GetElapsedReal (void) const;
47  int64_t GetElapsedUser (void) const;
49  int64_t GetElapsedSystem (void) const;
50 
51 private:
52  clock_t m_startTime;
53  int64_t m_elapsedReal;
54  int64_t m_elapsedUser;
55  int64_t m_elapsedSystem;
56 };
57 
58 void
60 {
61  NS_LOG_FUNCTION (this);
62  m_startTime = std::clock ();
63 }
64 
65 int64_t
67 {
68  //
69  // We need to return the number of milliseconds that have elapsed in some
70  // reasonably portable way. The underlying function that we will use returns
71  // a number of elapsed ticks. We can look up the number of ticks per second
72  // from the system configuration.
73  //
74  // Conceptually, we need to find the number of elapsed clock ticks and then
75  // multiply the result by the milliseconds per clock tick (or just as easily
76  // divide by clock ticks per millisecond). Integer dividing by clock ticks
77  // per millisecond is bad since this number is fractional on most machines
78  // and would result in divide by zero errors due to integer rounding.
79  //
80  // Multiplying by milliseconds per clock tick works up to a clock resolution
81  // of 1000 ticks per second. If we go past this point, we begin to get zero
82  // elapsed times when millisecondsPerTick becomes fractional and another
83  // rounding error appears.
84  //
85  // So rounding errors using integers can bite you from two direction. Since
86  // all of our targets have math coprocessors, why not just use doubles
87  // internally? Works fine, lasts a long time.
88  //
89  // If millisecondsPerTick becomes fractional, and an elapsed time greater than
90  // a milliscond is measured, the function will work as expected. If an elapsed
91  // time is measured that turns out to be less than a millisecond, we'll just
92  // return zero which would, I think, also will be expected.
93  //
94  NS_LOG_FUNCTION (this);
95  static int64_t ticksPerSecond = CLOCKS_PER_SEC;
96  static double millisecondsPerTick = 1000. / ticksPerSecond;
97 
98  clock_t endTime = std::clock ();
99 
100  double tmp;
101 
102  tmp = static_cast<double> (endTime - m_startTime) * millisecondsPerTick;
103  m_elapsedReal = static_cast<int64_t> (tmp);
104 
105  //
106  // Nothing like this in MinGW, for example.
107  //
108  m_elapsedUser = 0;
109  m_elapsedSystem = 0;
110 
111  return m_elapsedReal;
112 }
113 
114 int64_t
116 {
117  NS_LOG_FUNCTION (this);
118  return m_elapsedReal;
119 }
120 
121 int64_t
123 {
124  NS_LOG_FUNCTION (this);
125  return m_elapsedUser;
126 }
127 
128 int64_t
130 {
131  NS_LOG_FUNCTION (this);
132  return m_elapsedSystem;
133 }
134 
136  : m_priv (new SystemWallClockMsPrivate ())
137 {
138  NS_LOG_FUNCTION (this);
139 }
140 
142 {
143  NS_LOG_FUNCTION (this);
144  delete m_priv;
145  m_priv = 0;
146 }
147 
148 void
150 {
151  NS_LOG_FUNCTION (this);
152  m_priv->Start ();
153 }
154 
155 int64_t
157 {
158  NS_LOG_FUNCTION (this);
159  return m_priv->End ();
160 }
161 
162 int64_t
164 {
165  NS_LOG_FUNCTION (this);
166  return m_priv->GetElapsedReal ();
167 }
168 
169 int64_t
171 {
172  NS_LOG_FUNCTION (this);
173  return m_priv->GetElapsedUser ();
174 }
175 
176 int64_t
178 {
179  NS_LOG_FUNCTION (this);
180  return m_priv->GetElapsedSystem ();
181 }
182 
183 } // 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 "...
void Start(void)
Start a measure.
int64_t End(void)
Stop measuring the time since Start() was called.
System-independent wall clock class ns3::SystemWallClockMs declaration.
void Start(void)
Start a measure.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
int64_t GetElapsedUser(void) const
int64_t m_elapsedSystem
Elapsed system time, in ms.
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.
int64_t End(void)
Stop measuring the time since Start() was called.