clock.cpp 6.94 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

30
#include "precompiled.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
31 32 33 34 35
#include "clock.hpp"
#include "platform.hpp"
#include "likely.hpp"
#include "config.hpp"
#include "err.hpp"
36
#include "mutex.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
37 38 39

#include <stddef.h>

40
#if defined _MSC_VER
41
#if defined _WIN32_WCE
boris@boressoft.ru's avatar
boris@boressoft.ru committed
42 43
#include <cmnintrin.h>
#else
44 45
#include <intrin.h>
#endif
boris@boressoft.ru's avatar
boris@boressoft.ru committed
46
#endif
47

Martin Sustrik's avatar
Martin Sustrik committed
48 49 50 51
#if !defined ZMQ_HAVE_WINDOWS
#include <sys/time.h>
#endif

52
#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETHRTIME
53 54 55
#include <time.h>
#endif

56 57 58 59 60 61 62 63 64
#if defined ZMQ_HAVE_OSX
#include <mach/clock.h>
#include <mach/mach.h>
#include <time.h>
#include <sys/time.h>

int clock_gettime (int clock_id, timespec *ts)
{
    // The clock_id specified is not supported on this system.
65 66 67 68
    if (clock_id != CLOCK_REALTIME) {
        errno = EINVAL;
        return -1;
    }
69 70 71 72 73 74 75 76

    clock_serv_t cclock;
    mach_timespec_t mts;
    host_get_clock_service (mach_host_self (), CALENDAR_CLOCK, &cclock);
    clock_get_time (cclock, &mts);
    mach_port_deallocate (mach_task_self (), cclock);
    ts->tv_sec = mts.tv_sec;
    ts->tv_nsec = mts.tv_nsec;
77
    return 0;
78 79 80
}
#endif

81
#ifdef ZMQ_HAVE_WINDOWS
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
typedef ULONGLONG (*f_compatible_get_tick_count64)();

static zmq::mutex_t compatible_get_tick_count64_mutex;

ULONGLONG compatible_get_tick_count64()
{
  compatible_get_tick_count64_mutex.lock();
  static DWORD s_wrap = 0;
  static DWORD s_last_tick = 0;
  const DWORD current_tick = ::GetTickCount();
  if (current_tick < s_last_tick)
    ++s_wrap;

  s_last_tick = current_tick;
  const ULONGLONG result = (static_cast<ULONGLONG>(s_wrap) << 32) + static_cast<ULONGLONG>(current_tick);
  compatible_get_tick_count64_mutex.unlock();
  return result;
}

f_compatible_get_tick_count64 init_compatible_get_tick_count64()
{
  f_compatible_get_tick_count64 func = NULL;
  HMODULE module = ::LoadLibraryA("Kernel32.dll");
  if (module != NULL)
    func = reinterpret_cast<f_compatible_get_tick_count64>(::GetProcAddress(module, "GetTickCount64"));

  if (func == NULL)
    func = compatible_get_tick_count64;

111 112
  ::FreeLibrary(module);

113 114 115 116
  return func;
}

static f_compatible_get_tick_count64 my_get_tick_count64 = init_compatible_get_tick_count64();
117
#endif
118

Martin Sustrik's avatar
Martin Sustrik committed
119 120
zmq::clock_t::clock_t () :
    last_tsc (rdtsc ()),
121 122 123
#ifdef ZMQ_HAVE_WINDOWS
    last_time (static_cast<uint64_t>((*my_get_tick_count64)()))
#else
124
    last_time (now_us () / 1000)
125
#endif
Martin Sustrik's avatar
Martin Sustrik committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
{
}

zmq::clock_t::~clock_t ()
{
}

uint64_t zmq::clock_t::now_us ()
{
#if defined ZMQ_HAVE_WINDOWS

    //  Get the high resolution counter's accuracy.
    LARGE_INTEGER ticksPerSecond;
    QueryPerformanceFrequency (&ticksPerSecond);

    //  What time is it?
    LARGE_INTEGER tick;
    QueryPerformanceCounter (&tick);

    //  Convert the tick number into the number of seconds
    //  since the system was started.
147
    double ticks_div = ticksPerSecond.QuadPart / 1000000.0;
Martin Sustrik's avatar
Martin Sustrik committed
148 149
    return (uint64_t) (tick.QuadPart / ticks_div);

150
#elif defined HAVE_CLOCK_GETTIME && defined CLOCK_MONOTONIC
151 152 153 154

    //  Use POSIX clock_gettime function to get precise monotonic time.
    struct timespec tv;
    int rc = clock_gettime (CLOCK_MONOTONIC, &tv);
155 156 157 158 159 160 161 162 163 164
    // Fix case where system has clock_gettime but CLOCK_MONOTONIC is not supported.
    // This should be a configuration check, but I looked into it and writing an
    // AC_FUNC_CLOCK_MONOTONIC seems beyond my powers.
    if( rc != 0) {
        //  Use POSIX gettimeofday function to get precise time.
        struct timeval tv;
        int rc = gettimeofday (&tv, NULL);
        errno_assert (rc == 0);
        return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
    }
165 166
    return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_nsec / 1000);

167 168 169 170
#elif defined HAVE_GETHRTIME

    return (gethrtime () / 1000);

Martin Sustrik's avatar
Martin Sustrik committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#else

    //  Use POSIX gettimeofday function to get precise time.
    struct timeval tv;
    int rc = gettimeofday (&tv, NULL);
    errno_assert (rc == 0);
    return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);

#endif
}

uint64_t zmq::clock_t::now_ms ()
{
    uint64_t tsc = rdtsc ();

    //  If TSC is not supported, get precise time and chop off the microseconds.
    if (!tsc)
188 189 190 191 192 193 194 195
    {
#ifdef ZMQ_HAVE_WINDOWS
        // Under Windows, now_us is not so reliable since QueryPerformanceCounter
        // does not guarantee that it will use a hardware that offers a monotonic timer.
        // So, lets use GetTickCount when GetTickCount64 is not available with an workaround
        // to its 32 bit limitation.
        return static_cast<uint64_t>((*my_get_tick_count64)());
#else
Martin Sustrik's avatar
Martin Sustrik committed
196
        return now_us () / 1000;
197 198
#endif
    }
Martin Sustrik's avatar
Martin Sustrik committed
199 200 201 202 203 204 205 206

    //  If TSC haven't jumped back (in case of migration to a different
    //  CPU core) and if not too much time elapsed since last measurement,
    //  we can return cached time value.
    if (likely (tsc - last_tsc <= (clock_precision / 2) && tsc >= last_tsc))
        return last_time;

    last_tsc = tsc;
207 208 209
#ifdef ZMQ_HAVE_WINDOWS
    last_time = static_cast<uint64_t>((*my_get_tick_count64)());
#else
210
    last_time = now_us () / 1000;
211
#endif
Martin Sustrik's avatar
Martin Sustrik committed
212 213 214 215 216 217
    return last_time;
}

uint64_t zmq::clock_t::rdtsc ()
{
#if (defined _MSC_VER && (defined _M_IX86 || defined _M_X64))
Martin Sustrik's avatar
Martin Sustrik committed
218
    return __rdtsc ();
Martin Sustrik's avatar
Martin Sustrik committed
219 220 221 222
#elif (defined __GNUC__ && (defined __i386__ || defined __x86_64__))
    uint32_t low, high;
    __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
    return (uint64_t) high << 32 | low;
223 224 225 226 227 228 229 230
#elif (defined __SUNPRO_CC && (__SUNPRO_CC >= 0x5100) && (defined __i386 || \
    defined __amd64 || defined __x86_64))
    union {
        uint64_t u64val;
        uint32_t u32val [2];
    } tsc;
    asm("rdtsc" : "=a" (tsc.u32val [0]), "=d" (tsc.u32val [1]));
    return tsc.u64val;
231 232 233 234
#elif defined(__s390__)
    uint64_t tsc;
    asm("\tstck\t%0\n" : "=Q" (tsc) : : "cc");
    return(tsc);
Martin Sustrik's avatar
Martin Sustrik committed
235 236 237 238
#else
    return 0;
#endif
}