clock.cpp 4.09 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2 3
    Copyright (c) 2010-2011 250bpm s.r.o.
    Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
4 5 6 7

    This file is part of 0MQ.

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

    0MQ 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
15
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
16

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

#include "clock.hpp"
#include "platform.hpp"
#include "likely.hpp"
#include "config.hpp"
#include "err.hpp"

#include <stddef.h>

29
#if defined _MSC_VER
boris@boressoft.ru's avatar
boris@boressoft.ru committed
30 31 32
#if defined WINCE
#include <cmnintrin.h>
#else
33 34
#include <intrin.h>
#endif
boris@boressoft.ru's avatar
boris@boressoft.ru committed
35
#endif
36

Martin Sustrik's avatar
Martin Sustrik committed
37 38 39 40
#if !defined ZMQ_HAVE_WINDOWS
#include <sys/time.h>
#endif

41
#if defined HAVE_CLOCK_GETTIME || defined HAVE_GETHRTIME
42 43 44
#include <time.h>
#endif

Martin Sustrik's avatar
Martin Sustrik committed
45 46
zmq::clock_t::clock_t () :
    last_tsc (rdtsc ()),
47
    last_time (now_us () / 1000)
Martin Sustrik's avatar
Martin Sustrik committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
{
}

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.
    double ticks_div = (double) (ticksPerSecond.QuadPart / 1000000);     
    return (uint64_t) (tick.QuadPart / ticks_div);

72
#elif defined HAVE_CLOCK_GETTIME && defined CLOCK_MONOTONIC
73 74 75 76

    //  Use POSIX clock_gettime function to get precise monotonic time.
    struct timespec tv;
    int rc = clock_gettime (CLOCK_MONOTONIC, &tv);
77 78 79 80 81 82 83 84 85 86
		// 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);
		}
87 88
    return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_nsec / 1000);

89 90 91 92
#elif defined HAVE_GETHRTIME

    return (gethrtime () / 1000);

Martin Sustrik's avatar
Martin Sustrik committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#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)
        return now_us () / 1000;

    //  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;
119
    last_time = now_us () / 1000;
Martin Sustrik's avatar
Martin Sustrik committed
120 121 122 123 124 125
    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
126
    return __rdtsc ();
Martin Sustrik's avatar
Martin Sustrik committed
127 128 129 130
#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;
131 132 133 134 135 136 137 138
#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;
139 140 141 142 143
#elif defined(__s390__)
    uint64_t tsc;
    asm("\tstck\t%0\n" : "=Q" (tsc) : : "cc");
    tsc >>= 12;		/* convert to microseconds just to be consistent */
    return(tsc);
Martin Sustrik's avatar
Martin Sustrik committed
144 145 146 147
#else
    return 0;
#endif
}