A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
unix-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 #include "abort.h"
23 #include "log.h"
24 #include <sys/times.h>
25 #include <unistd.h>
26 
27 NS_LOG_COMPONENT_DEFINE ("SystemWallClockMsPrivate");
28 
29 namespace ns3 {
30 
32 public:
33  void Start (void);
34  int64_t End (void);
35  int64_t GetElapsedReal (void) const;
36  int64_t GetElapsedUser (void) const;
37  int64_t GetElapsedSystem (void) const;
38 
39 private:
40  struct tms m_startTimes;
41  clock_t m_startTime;
42  int64_t m_elapsedReal;
43  int64_t m_elapsedUser;
44  int64_t m_elapsedSystem;
45 };
46 
47 void
49 {
50  NS_LOG_FUNCTION (this);
51  m_startTime = times (&m_startTimes);
52 }
53 
54 int64_t
56 {
57  //
58  // We need to return the number of milliseconds that have elapsed in some
59  // reasonably portable way. The underlying function that we will use returns
60  // a number of elapsed ticks. We can look up the number of ticks per second
61  // from the system configuration.
62  //
63  // Conceptually, we need to find the number of elapsed clock ticks and then
64  // multiply the result by the milliseconds per clock tick (or divide by clock
65  // ticks per millisecond). Integer dividing by clock ticks per millisecond
66  // is bad since this number is fractional on most machines and would result
67  // in divide by zero errors due to integer rounding.
68  //
69  // Multiplying by milliseconds per clock tick works up to a clock resolution
70  // of 1000 ticks per second. If we go past this point, we begin to get zero
71  // elapsed times when millisecondsPerTick becomes fractional and another
72  // rounding error appears.
73  //
74  // So rounding errors using integers can bite you from both direction. Since
75  // all of our targets have math coprocessors, why not just use doubles
76  // internally? Works fine, lasts a long time.
77  //
78  // If millisecondsPerTick becomes fractional, and an elapsed time greater than
79  // a milliscond is measured, the function will work as expected. If an elapsed
80  // time is measured that turns out to be less than a millisecond, we'll just
81  // return zero which would, I think, also will be expected.
82  //
83  NS_LOG_FUNCTION (this);
84  static int64_t ticksPerSecond = sysconf (_SC_CLK_TCK);
85  static double millisecondsPerTick = 1000. / ticksPerSecond;
86 
87  //
88  // If sysconf () fails, we have no idea how to do the required conversion to ms.
89  //
90  NS_ABORT_MSG_IF (ticksPerSecond == -1, "SystemWallClockMsPrivate(): Cannot sysconf (_SC_CLK_TCK)");
91 
92  struct tms endTimes;
93  clock_t endTime = times (&endTimes);
94 
95  double tmp;
96 
97  tmp = static_cast<double> (endTime - m_startTime) * millisecondsPerTick;
98  m_elapsedReal = static_cast<int64_t> (tmp);
99 
100  tmp = static_cast<double> (endTimes.tms_utime - m_startTimes.tms_utime) * millisecondsPerTick;
101  m_elapsedUser = static_cast<int64_t> (tmp);
102 
103  tmp = static_cast<double> (endTimes.tms_stime - m_startTimes.tms_stime) * millisecondsPerTick;
104  m_elapsedSystem = static_cast<int64_t> (tmp);
105 
106  return m_elapsedReal;
107 }
108 
109 int64_t
111 {
112  NS_LOG_FUNCTION (this);
113  return m_elapsedReal;
114 }
115 
116 int64_t
118 {
119  NS_LOG_FUNCTION (this);
120  return m_elapsedUser;
121 }
122 
123 int64_t
125 {
126  NS_LOG_FUNCTION (this);
127  return m_elapsedSystem;
128 }
129 
131  : m_priv (new SystemWallClockMsPrivate ())
132 {
133  NS_LOG_FUNCTION (this);
134 }
135 
137 {
138  NS_LOG_FUNCTION (this);
139  delete m_priv;
140  m_priv = 0;
141 }
142 
143 void
145 {
146  NS_LOG_FUNCTION (this);
147  m_priv->Start ();
148 }
149 
150 int64_t
152 {
153  NS_LOG_FUNCTION (this);
154  return m_priv->End ();
155 }
156 
157 int64_t
159 {
160  NS_LOG_FUNCTION (this);
161  return m_priv->GetElapsedReal ();
162 }
163 
164 int64_t
166 {
167  NS_LOG_FUNCTION (this);
168  return m_priv->GetElapsedUser ();
169 }
170 
171 int64_t
173 {
174  NS_LOG_FUNCTION (this);
175  return m_priv->GetElapsedSystem ();
176 }
177 
178 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
void Start(void)
Start a measure.
NS_LOG_COMPONENT_DEFINE("SystemWallClockMsPrivate")
int64_t GetElapsedUser(void) const
int64_t GetElapsedReal(void) const
int64_t GetElapsedSystem(void) const
class SystemWallClockMsPrivate * m_priv
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if cond is true.
Definition: abort.h:98
int64_t End(void)
Stop measuring the time since Start() was called.