SSL TimeStamp_posix.cpp
Interaktion und PortierbarkeitC
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// // Implement TimeStamp::Now() with POSIX clocks. // // The "tick" unit for POSIX clocks is simply a nanosecond, as this is // the smallest unit of time representable by struct timespec. That // doesn't mean that a nanosecond is the resolution of TimeDurations // obtained with this API; see TimeDuration::Resolution; //
static uint64_t ClockTimeNs(const clockid_t aClockId = CLOCK_MONOTONIC) { struct timespec ts; #ifdef CLOCK_MONOTONIC_COARSE
MOZ_RELEASE_ASSERT(
aClockId == CLOCK_MONOTONIC ||
(sSupportsMonotonicCoarseClock && aClockId == CLOCK_MONOTONIC_COARSE)); #else
MOZ_RELEASE_ASSERT(aClockId == CLOCK_MONOTONIC); #endif // this can't fail: we know &ts is valid, and TimeStamp::Startup() // checks that CLOCK_MONOTONIC / CLOCK_MONOTONIC_COARSE are // supported (and aborts if the former is not).
clock_gettime(aClockId, &ts);
// tv_sec is defined to be relative to an arbitrary point in time, // but it would be madness for that point in time to be earlier than // the Epoch. So we can safely assume that even if time_t is 32 // bits, tv_sec won't overflow while the browser is open. Revisit // this argument if we're still building with 32-bit time_t around // the year 2037. return TimespecToNs(ts);
}
static uint64_t ClockResolutionNs() { // NB: why not rely on clock_getres()? Two reasons: (i) it might // lie, and (ii) it might return an "ideal" resolution that while // theoretically true, could never be measured in practice. Since // clock_gettime() likely involves a system call on your platform, // the "actual" timing resolution shouldn't be lower than syscall // overhead.
// 10 total trials is arbitrary: what we're trying to avoid by // looping is getting unlucky and being interrupted by a context // switch or signal, or being bitten by paging/cache effects for (int i = 0; i < 9; ++i) {
start = ClockTimeNs();
end = ClockTimeNs();
if (0 == minres) { // measurable resolution is either incredibly low, ~1ns, or very // high. fall back on clock_getres() struct timespec ts; if (0 == clock_getres(CLOCK_MONOTONIC, &ts)) {
minres = TimespecToNs(ts);
}
}
if (0 == minres) { // clock_getres probably failed. fall back on NSPR's resolution // assumption
minres = 1 * kNsPerMs;
}
// find the number of significant digits in sResolution, for the // sake of ToSecondsSigDigits() for (sResolutionSigDigs = 1; !(sResolutionSigDigs == sResolution ||
10 * sResolutionSigDigs > sResolution);
sResolutionSigDigs *= 10);
// Calculates the amount of jiffies that have elapsed since boot and up to the // starttime value of a specific process as found in its /proc/*/stat file. // Returns 0 if an error occurred.
// Computes the interval that has elapsed between the thread creation and the // process creation by comparing the starttime fields in the respective // /proc/*/stat files. The resulting value will be a good approximation of the // process uptime. This value will be stored at the address pointed by aTime; // if an error occurred 0 will be stored instead.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.