1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
/*****************************************************************************
* Author: Vadim Zeitlin <vadim@wxwidgets.org>
*
*****************************************************************************
* Copyright (c) 2004 Vadim Zeitlin
*
* This library is free software; you can distribute 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 2.1 of the License, or (at your
* option) any later version.
*
* This library 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 LGPL in the file COPYING for more
* details.
*
*/
#ifndef _rlog_time_incl
#define _rlog_time_incl
#include <rlog/common.h>
/*! @file time.h
@brief Defines functions for getting current time and benchmarking.
*/
#ifdef _WIN32
#include <windows.h>
#define HAVE_QUERYPERFORMANCECOUNTER 1
typedef __int64 rlog_time_interval;
#if HAVE_QUERYPERFORMANCECOUNTER
typedef LARGE_INTEGER rlog_time;
#define RLOG_TIME_UNIT "clock cycles"
inline
void rlog_get_time(rlog_time *pt)
{
QueryPerformanceCounter(pt);
}
inline
rlog_time_interval rlog_time_diff(const rlog_time& end, const rlog_time& start)
{
long long llEnd, llStart;
memcpy(&llEnd, &end, sizeof(long long));
memcpy(&llStart, &start, sizeof(long long));
return llEnd - llStart;
}
#else // !HAVE_QUERYPERFORMANCECOUNTER
typedef FILETIME rlog_time;
#define RLOG_TIME_UNIT "usec"
inline
void rlog_get_time(rlog_time *pt)
{
GetSystemTimeAsFileTime(pt);
}
inline
rlog_time_interval rlog_time_diff(const rlog_time& end, const rlog_time& start)
{
ULONGLONG ullEnd, ullStart;
memcpy(&ullEnd, &end, sizeof(ULONGLONG));
memcpy(&ullStart, &start, sizeof(ULONGLONG));
return 10*(ullEnd - ullStart);
}
#endif // HAVE_QUERYPERFORMANCECOUNTER
inline
void sleep(int seconds)
{
::Sleep(seconds * 1000);
}
#else // Unix
#include <sys/time.h>
#include <unistd.h> // for sleep()
#if RLOG_TIME_TSC
#include <stdint.h>
typedef uint64_t rlog_time;
typedef int64_t rlog_time_interval;
#define RLOG_TIME_UNIT "clock cycles"
inline void rlog_get_time(uint64_t *pt)
{
asm volatile("RDTSC" : "=A" (*pt));
}
inline
rlog_time_interval rlog_time_diff( const rlog_time &end, const rlog_time &start )
{
return end - start;
}
#else // !HAVE_TSC
#include <unistd.h>
typedef timeval rlog_time;
typedef long rlog_time_interval;
#define RLOG_TIME_UNIT "usec"
inline
void rlog_get_time(rlog_time *pt)
{
gettimeofday( pt, 0 );
}
inline
rlog_time_interval rlog_time_diff( const rlog_time &end, const rlog_time &start )
{
return (end.tv_sec - start.tv_sec) * 1000 * 1000 +
(end.tv_usec - start.tv_usec);
}
#endif // HAVE_TSC/!HAVE_TSC
#endif // Win32/Unix
#endif // _rlog_time_incl
|