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