Bug 564076: Small parser cleanup changes. (r=mrbkap)
[mozilla-central.git] / xpcom / ds / TimeStamp_posix.cpp
blob9e4892b0e0ca2fbf3f474f38d52a0bee51b00e90
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla code.
18 * The Initial Developer of the Original Code is the Mozilla Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Chris Jones <jones.chris.g@gmail.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 // Implement TimeStamp::Now() with POSIX clocks.
42 // The "tick" unit for POSIX clocks is simply a nanosecond, as this is
43 // the smallest unit of time representable by struct timespec. That
44 // doesn't mean that a nanosecond is the resolution of TimeDurations
45 // obtained with this API; see TimeDuration::Resolution;
48 #include <time.h>
50 #include "mozilla/TimeStamp.h"
52 // Estimate of the smallest duration of time we can measure.
53 static PRUint64 sResolution;
54 static PRUint64 sResolutionSigDigs;
56 static const PRUint16 kNsPerUs = 1000;
57 static const PRUint64 kNsPerMs = 1000000;
58 static const PRUint64 kNsPerSec = 1000000000;
59 static const double kNsPerSecd = 1000000000.0;
61 static PRUint64
62 TimespecToNs(const struct timespec& ts)
64 PRUint64 baseNs = PRUint64(ts.tv_sec) * kNsPerSec;
65 return baseNs + PRUint64(ts.tv_nsec);
68 static PRUint64
69 ClockTimeNs()
71 struct timespec ts;
72 // this can't fail: we know &ts is valid, and TimeStamp::Init()
73 // checks that CLOCK_MONOTONIC is supported (and aborts if not)
74 clock_gettime(CLOCK_MONOTONIC, &ts);
76 // tv_sec is defined to be relative to an arbitrary point in time,
77 // but it would be madness for that point in time to be earlier than
78 // the Epoch. So we can safely assume that even if time_t is 32
79 // bits, tv_sec won't overflow while the browser is open. Revisit
80 // this argument if we're still building with 32-bit time_t around
81 // the year 2037.
82 return TimespecToNs(ts);
85 static PRUint64
86 ClockResolutionNs()
88 // NB: why not rely on clock_getres()? Two reasons: (i) it might
89 // lie, and (ii) it might return an "ideal" resolution that while
90 // theoretically true, could never be measured in practice. Since
91 // clock_gettime() likely involves a system call on your platform,
92 // the "actual" timing resolution shouldn't be lower than syscall
93 // overhead.
95 PRUint64 start = ClockTimeNs();
96 PRUint64 end = ClockTimeNs();
97 PRUint64 minres = (end - start);
99 // 10 total trials is arbitrary: what we're trying to avoid by
100 // looping is getting unlucky and being interrupted by a context
101 // switch or signal, or being bitten by paging/cache effects
102 for (int i = 0; i < 9; ++i) {
103 start = ClockTimeNs();
104 end = ClockTimeNs();
106 PRUint64 candidate = (start - end);
107 if (candidate < minres)
108 minres = candidate;
111 if (0 == minres) {
112 // measurable resolution is either incredibly low, ~1ns, or very
113 // high. fall back on clock_getres()
114 struct timespec ts;
115 clock_getres(CLOCK_MONOTONIC, &ts);
117 minres = TimespecToNs(ts);
120 if (0 == minres) {
121 // clock_getres probably failed. fall back on NSPR's resolution
122 // assumption
123 minres = 1 * kNsPerMs;
126 return minres;
130 namespace mozilla {
132 double
133 TimeDuration::ToSeconds() const
135 return double(mValue) / kNsPerSecd;
138 double
139 TimeDuration::ToSecondsSigDigits() const
141 // don't report a value < mResolution ...
142 PRInt64 valueSigDigs = sResolution * (mValue / sResolution);
143 // and chop off insignificant digits
144 valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
145 return double(valueSigDigs) / kNsPerSecd;
148 TimeDuration
149 TimeDuration::FromSeconds(PRInt32 aSeconds)
151 return TimeDuration::FromTicks((PRInt64(aSeconds) * PRInt64(kNsPerSec)));
154 TimeDuration
155 TimeDuration::FromMilliseconds(PRInt32 aMilliseconds)
157 return TimeDuration::FromTicks(PRInt64(aMilliseconds) * PRInt64(kNsPerMs));
160 TimeDuration
161 TimeDuration::Resolution()
163 return TimeDuration::FromTicks(sResolution);
166 struct TimeStampInitialization
168 TimeStampInitialization() {
169 TimeStamp::Startup();
171 ~TimeStampInitialization() {
172 TimeStamp::Shutdown();
176 static TimeStampInitialization initOnce;
177 static PRBool gInitialized = PR_FALSE;
179 nsresult
180 TimeStamp::Startup()
182 if (gInitialized)
183 return NS_OK;
185 struct timespec dummy;
186 if (0 != clock_gettime(CLOCK_MONOTONIC, &dummy))
187 NS_RUNTIMEABORT("CLOCK_MONOTONIC is absent!");
189 sResolution = ClockResolutionNs();
191 // find the number of significant digits in sResolution, for the
192 // sake of ToSecondsSigDigits()
193 for (sResolutionSigDigs = 1;
194 !(sResolutionSigDigs == sResolution
195 || 10*sResolutionSigDigs > sResolution);
196 sResolutionSigDigs *= 10);
198 gInitialized = PR_TRUE;
199 return NS_OK;
202 void
203 TimeStamp::Shutdown()
207 TimeStamp
208 TimeStamp::Now()
210 return TimeStamp(ClockTimeNs());