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