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