xpcom/ds/TimeStamp_darwin.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/TimeStamp_darwin.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,195 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +//
    1.11 +// Implement TimeStamp::Now() with mach_absolute_time
    1.12 +//
    1.13 +// The "tick" unit for mach_absolute_time is defined using mach_timebase_info() which
    1.14 +// gives a conversion ratio to nanoseconds. For more information see Apple's QA1398.
    1.15 +//
    1.16 +// This code is inspired by Chromium's time_mac.cc. The biggest
    1.17 +// differences are that we explicitly initialize using
    1.18 +// TimeStamp::Initialize() instead of lazily in Now() and that
    1.19 +// we store the time value in ticks and convert when needed instead
    1.20 +// of storing the time value in nanoseconds.
    1.21 +
    1.22 +#include <mach/mach_time.h>
    1.23 +#include <sys/time.h>
    1.24 +#include <sys/sysctl.h>
    1.25 +#include <time.h>
    1.26 +#include <unistd.h>
    1.27 +
    1.28 +#include "mozilla/TimeStamp.h"
    1.29 +#include "nsDebug.h"
    1.30 +
    1.31 +// Estimate of the smallest duration of time we can measure.
    1.32 +static uint64_t sResolution;
    1.33 +static uint64_t sResolutionSigDigs;
    1.34 +
    1.35 +static const uint64_t kNsPerMs   =    1000000;
    1.36 +static const uint64_t kUsPerSec  =    1000000;
    1.37 +static const double kNsPerMsd    =    1000000.0;
    1.38 +static const double kNsPerSecd   = 1000000000.0;
    1.39 +
    1.40 +static bool gInitialized = false;
    1.41 +static double sNsPerTick;
    1.42 +
    1.43 +static uint64_t
    1.44 +ClockTime()
    1.45 +{
    1.46 +  // mach_absolute_time is it when it comes to ticks on the Mac.  Other calls
    1.47 +  // with less precision (such as TickCount) just call through to
    1.48 +  // mach_absolute_time.
    1.49 +  //
    1.50 +  // At the time of writing mach_absolute_time returns the number of nanoseconds
    1.51 +  // since boot. This won't overflow 64bits for 500+ years so we aren't going
    1.52 +  // to worry about that possiblity
    1.53 +  return mach_absolute_time();
    1.54 +}
    1.55 +
    1.56 +static uint64_t
    1.57 +ClockResolutionNs()
    1.58 +{
    1.59 +  uint64_t start = ClockTime();
    1.60 +  uint64_t end = ClockTime();
    1.61 +  uint64_t minres = (end - start);
    1.62 +
    1.63 +  // 10 total trials is arbitrary: what we're trying to avoid by
    1.64 +  // looping is getting unlucky and being interrupted by a context
    1.65 +  // switch or signal, or being bitten by paging/cache effects
    1.66 +  for (int i = 0; i < 9; ++i) {
    1.67 +    start = ClockTime();
    1.68 +    end = ClockTime();
    1.69 +
    1.70 +    uint64_t candidate = (start - end);
    1.71 +    if (candidate < minres)
    1.72 +      minres = candidate;
    1.73 +  }
    1.74 +
    1.75 +  if (0 == minres) {
    1.76 +    // measurable resolution is either incredibly low, ~1ns, or very
    1.77 +    // high.  fall back on NSPR's resolution assumption
    1.78 +    minres = 1 * kNsPerMs;
    1.79 +  }
    1.80 +
    1.81 +  return minres;
    1.82 +}
    1.83 +
    1.84 +namespace mozilla {
    1.85 +
    1.86 +double
    1.87 +TimeDuration::ToSeconds() const
    1.88 +{
    1.89 +  NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
    1.90 +  return (mValue * sNsPerTick) / kNsPerSecd;
    1.91 +}
    1.92 +
    1.93 +double
    1.94 +TimeDuration::ToSecondsSigDigits() const
    1.95 +{
    1.96 +  NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
    1.97 +  // don't report a value < mResolution ...
    1.98 +  int64_t valueSigDigs = sResolution * (mValue / sResolution);
    1.99 +  // and chop off insignificant digits
   1.100 +  valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs);
   1.101 +  return (valueSigDigs * sNsPerTick) / kNsPerSecd;
   1.102 +}
   1.103 +
   1.104 +TimeDuration
   1.105 +TimeDuration::FromMilliseconds(double aMilliseconds)
   1.106 +{
   1.107 +  NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
   1.108 +  return TimeDuration::FromTicks(int64_t((aMilliseconds * kNsPerMsd) / sNsPerTick));
   1.109 +}
   1.110 +
   1.111 +TimeDuration
   1.112 +TimeDuration::Resolution()
   1.113 +{
   1.114 +  NS_ABORT_IF_FALSE(gInitialized, "calling TimeDuration too early");
   1.115 +  return TimeDuration::FromTicks(int64_t(sResolution));
   1.116 +}
   1.117 +
   1.118 +nsresult
   1.119 +TimeStamp::Startup()
   1.120 +{
   1.121 +  if (gInitialized)
   1.122 +    return NS_OK;
   1.123 +
   1.124 +  mach_timebase_info_data_t timebaseInfo;
   1.125 +  // Apple's QA1398 suggests that the output from mach_timebase_info
   1.126 +  // will not change while a program is running, so it should be safe
   1.127 +  // to cache the result.
   1.128 +  kern_return_t kr = mach_timebase_info(&timebaseInfo);
   1.129 +  if (kr != KERN_SUCCESS)
   1.130 +    NS_RUNTIMEABORT("mach_timebase_info failed");
   1.131 +
   1.132 +  sNsPerTick = double(timebaseInfo.numer) / timebaseInfo.denom;
   1.133 +
   1.134 +  sResolution = ClockResolutionNs();
   1.135 +
   1.136 +  // find the number of significant digits in sResolution, for the
   1.137 +  // sake of ToSecondsSigDigits()
   1.138 +  for (sResolutionSigDigs = 1;
   1.139 +       !(sResolutionSigDigs == sResolution
   1.140 +         || 10*sResolutionSigDigs > sResolution);
   1.141 +       sResolutionSigDigs *= 10);
   1.142 +
   1.143 +  gInitialized = true;
   1.144 +
   1.145 +  return NS_OK;
   1.146 +}
   1.147 +
   1.148 +void
   1.149 +TimeStamp::Shutdown()
   1.150 +{
   1.151 +}
   1.152 +
   1.153 +TimeStamp
   1.154 +TimeStamp::Now(bool aHighResolution)
   1.155 +{
   1.156 +  return TimeStamp(ClockTime());
   1.157 +}
   1.158 +
   1.159 +// Computes and returns the process uptime in microseconds.
   1.160 +// Returns 0 if an error was encountered.
   1.161 +
   1.162 +uint64_t
   1.163 +TimeStamp::ComputeProcessUptime()
   1.164 +{
   1.165 +  struct timeval tv;
   1.166 +  int rv = gettimeofday(&tv, nullptr);
   1.167 +
   1.168 +  if (rv == -1) {
   1.169 +    return 0;
   1.170 +  }
   1.171 +
   1.172 +  int mib[] = {
   1.173 +    CTL_KERN,
   1.174 +    KERN_PROC,
   1.175 +    KERN_PROC_PID,
   1.176 +    getpid(),
   1.177 +  };
   1.178 +  u_int mibLen = sizeof(mib) / sizeof(mib[0]);
   1.179 +
   1.180 +  struct kinfo_proc proc;
   1.181 +  size_t bufferSize = sizeof(proc);
   1.182 +  rv = sysctl(mib, mibLen, &proc, &bufferSize, nullptr, 0);
   1.183 +
   1.184 +  if (rv == -1)
   1.185 +    return 0;
   1.186 +
   1.187 +  uint64_t startTime =
   1.188 +    ((uint64_t)proc.kp_proc.p_un.__p_starttime.tv_sec * kUsPerSec) +
   1.189 +    proc.kp_proc.p_un.__p_starttime.tv_usec;
   1.190 +  uint64_t now = (tv.tv_sec * kUsPerSec) + tv.tv_usec;
   1.191 +
   1.192 +  if (startTime > now)
   1.193 +    return 0;
   1.194 +
   1.195 +  return now - startTime;
   1.196 +}
   1.197 +
   1.198 +} // namespace mozilla

mercurial