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