# RLogTime.h

    00001 /*****************************************************************************
    00002  * Author:   Vadim Zeitlin <vadim@wxwidgets.org>
    00003  *
    00004  *****************************************************************************
    00005  * Copyright (c) 2004 Vadim Zeitlin
    00006  *
    00007  * This library is free software; you can distribute it and/or modify it under
    00008  * the terms of the GNU Lesser General Public License (LGPL), as published by
    00009  * the Free Software Foundation; either version 2.1 of the License, or (at your
    00010  * option) any later version.
    00011  *
    00012  * This library is distributed in the hope that it will be useful, but WITHOUT
    00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00014  * FITNESS FOR A PARTICULAR PURPOSE.  See the LGPL in the file COPYING for more
    00015  * details.
    00016  *
    00017  */
    00018
    00019 #ifndef _rlog_time_incl
    00020 #define _rlog_time_incl
    00021
    00022 #include <rlog/common.h>
    00023
    00028 #ifdef _WIN32
    00029
    00030 #include <windows.h>
    00031
    00032 #define HAVE_QUERYPERFORMANCECOUNTER 1
    00033
    00034 typedef __int64 rlog_time_interval;
    00035
    00036 #if HAVE_QUERYPERFORMANCECOUNTER
    00037
    00038 typedef LARGE_INTEGER rlog_time;
    00039
    00040 #define RLOG_TIME_UNIT "clock cycles"
    00041
    00042 inline
    00043 void rlog_get_time(rlog_time *pt)
    00044 {
    00045     QueryPerformanceCounter(pt);
    00046 }
    00047
    00048 inline
    00049 rlog_time_interval rlog_time_diff(const rlog_time& end, const rlog_time& start)
    00050 {
    00051     long long llEnd, llStart;
    00052     memcpy(&llEnd, &end, sizeof(long long));
    00053     memcpy(&llStart, &start, sizeof(long long));
    00054     return llEnd - llStart;
    00055 }
    00056
    00057 #else // !HAVE_QUERYPERFORMANCECOUNTER
    00058
    00059 typedef FILETIME rlog_time;
    00060
    00061 #define RLOG_TIME_UNIT "usec"
    00062
    00063 inline
    00064 void rlog_get_time(rlog_time *pt)
    00065 {
    00066     GetSystemTimeAsFileTime(pt);
    00067 }
    00068
    00069 inline
    00070 rlog_time_interval rlog_time_diff(const rlog_time& end, const rlog_time& start)
    00071 {
    00072     ULONGLONG ullEnd, ullStart;
    00073     memcpy(&ullEnd, &end, sizeof(ULONGLONG));
    00074     memcpy(&ullStart, &start, sizeof(ULONGLONG));
    00075     return 10*(ullEnd - ullStart);
    00076 }
    00077
    00078 #endif // HAVE_QUERYPERFORMANCECOUNTER
    00079
    00080 inline
    00081 void sleep(int seconds)
    00082 {
    00083     ::Sleep(seconds * 1000);
    00084 }
    00085
    00086 #else // Unix
    00087
    00088 #include <sys/time.h>
    00089 #include <unistd.h> // for sleep()
    00090
    00091 #if RLOG_TIME_TSC
    00092
    00093 #include <stdint.h>
    00094
    00095 typedef uint64_t rlog_time;
    00096 typedef int64_t rlog_time_interval;
    00097
    00098 #define RLOG_TIME_UNIT "clock cycles"
    00099
    00100 inline void rlog_get_time(uint64_t *pt)
    00101 {
    00102     asm volatile("RDTSC" : "=A" (*pt));
    00103 }
    00104
    00105 inline
    00106 rlog_time_interval rlog_time_diff( const rlog_time &end, const rlog_time &start )
    00107 {
    00108     return end - start;
    00109 }
    00110
    00111 #else // !HAVE_TSC
    00112
    00113 #include <unistd.h>
    00114
    00115 typedef timeval rlog_time;
    00116 typedef long rlog_time_interval;
    00117
    00118 #define RLOG_TIME_UNIT "usec"
    00119
    00120 inline
    00121 void rlog_get_time(rlog_time *pt)
    00122 {
    00123     gettimeofday( pt, 0 );
    00124 }
    00125
    00126 inline
    00127 rlog_time_interval rlog_time_diff( const rlog_time &end, const rlog_time &start )
    00128 {
    00129     return (end.tv_sec - start.tv_sec) * 1000 * 1000 +
    00130         (end.tv_usec - start.tv_usec);
    00131 }
    00132
    00133 #endif // HAVE_TSC/!HAVE_TSC
    00134
    00135 #endif // Win32/Unix
    00136
    00137 #endif // _rlog_time_incl