A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 namespace ns3 {
26 
27 class SystemWallClockMsPrivate {
28 public:
29  void Start (void);
30  int64_t End (void);
31  int64_t GetElapsedReal (void) const;
32  int64_t GetElapsedUser (void) const;
33  int64_t GetElapsedSystem (void) const;
34 
35 private:
36  clock_t m_startTime;
37  int64_t m_elapsedReal;
38  int64_t m_elapsedUser;
39  int64_t m_elapsedSystem;
40 };
41 
42 void
44 {
45  NS_LOG_FUNCTION (this);
46  m_startTime = std::clock ();
47 }
48 
49 int64_t
51 {
52  //
53  // We need to return the number of milliseconds that have elapsed in some
54  // reasonably portable way. The underlying function that we will use returns
55  // a number of elapsed ticks. We can look up the number of ticks per second
56  // from the system configuration.
57  //
58  // Conceptually, we need to find the number of elapsed clock ticks and then
59  // multiply the result by the milliseconds per clock tick (or just as easily
60  // divide by clock ticks per millisecond). Integer dividing by clock ticks
61  // per millisecond is bad since this number is fractional on most machines
62  // and would result in divide by zero errors due to integer rounding.
63  //
64  // Multiplying by milliseconds per clock tick works up to a clock resolution
65  // of 1000 ticks per second. If we go past this point, we begin to get zero
66  // elapsed times when millisecondsPerTick becomes fractional and another
67  // rounding error appears.
68  //
69  // So rounding errors using integers can bite you from two direction. Since
70  // all of our targets have math coprocessors, why not just use doubles
71  // internally? Works fine, lasts a long time.
72  //
73  // If millisecondsPerTick becomes fractional, and an elapsed time greater than
74  // a milliscond is measured, the function will work as expected. If an elapsed
75  // time is measured that turns out to be less than a millisecond, we'll just
76  // return zero which would, I think, also will be expected.
77  //
78  NS_LOG_FUNCTION (this);
79  static int64_t ticksPerSecond = CLOCKS_PER_SEC;
80  static double millisecondsPerTick = 1000. / ticksPerSecond;
81 
82  clock_t endTime = std::clock ();
83 
84  double tmp;
85 
86  tmp = static_cast<double> (endTime - m_startTime) * millisecondsPerTick;
87  m_elapsedReal = static_cast<int64_t> (tmp);
88 
89  //
90  // Nothing like this in MinGW, for example.
91  //
92  m_elapsedUser = 0;
93  m_elapsedSystem = 0;
94 
95  return m_elapsedReal;
96 }
97 
98 int64_t
100 {
101  NS_LOG_FUNCTION (this);
102  return m_elapsedReal;
103 }
104 
105 int64_t
107 {
108  NS_LOG_FUNCTION (this);
109  return m_elapsedUser;
110 }
111 
112 int64_t
114 {
115  NS_LOG_FUNCTION (this);
116  return m_elapsedSystem;
117 }
118 
120  : m_priv (new SystemWallClockMsPrivate ())
121 {
122  NS_LOG_FUNCTION (this);
123 }
124 
126 {
127  NS_LOG_FUNCTION (this);
128  delete m_priv;
129  m_priv = 0;
130 }
131 
132 void
134 {
135  NS_LOG_FUNCTION (this);
136  m_priv->Start ();
137 }
138 
139 int64_t
141 {
142  NS_LOG_FUNCTION (this);
143  return m_priv->End ();
144 }
145 
146 int64_t
148 {
149  NS_LOG_FUNCTION (this);
150  return m_priv->GetElapsedReal ();
151 }
152 
153 int64_t
155 {
156  NS_LOG_FUNCTION (this);
157  return m_priv->GetElapsedUser ();
158 }
159 
160 int64_t
162 {
163  NS_LOG_FUNCTION (this);
164  return m_priv->GetElapsedSystem ();
165 }
166 
167 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void Start(void)
Start a measure.
int64_t GetElapsedUser(void) const
int64_t GetElapsedReal(void) const
int64_t GetElapsedSystem(void) const
class SystemWallClockMsPrivate * m_priv
int64_t End(void)
Stop measuring the time since Start() was called.