rlog: RLogTime.h Source File

Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | Related Pages

# 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 #if HAVE_QUERYPERFORMANCECOUNTER
    00035
    00036 typedef LARGE_INTEGER rlog_time;
    00037
    00038 #define RLOG_TIME_UNIT "clock cycles"
    00039
    00040 inline
    00041 void rlog_get_time(rlog_time *pt)
    00042 {
    00043     QueryPerformanceCounter(pt);
    00044 }
    00045
    00046 inline
    00047 long long rlog_time_diff(const rlog_time& end, const rlog_time& start)
    00048 {
    00049     long long llEnd, llStart;
    00050     memcpy(&llEnd, &end, sizeof(long long));
    00051     memcpy(&llStart, &start, sizeof(long long));
    00052     return llEnd - llStart;
    00053 }
    00054
    00055 #else // !HAVE_QUERYPERFORMANCECOUNTER
    00056
    00057 typedef FILETIME rlog_time;
    00058
    00059 #define RLOG_TIME_UNIT "usec"
    00060
    00061 inline
    00062 void rlog_get_time(rlog_time *pt)
    00063 {
    00064     GetSystemTimeAsFileTime(pt);
    00065 }
    00066
    00067 inline
    00068 int rlog_time_diff(const rlog_time& end, const rlog_time& start)
    00069 {
    00070     ULONGLONG ullEnd, ullStart;
    00071     memcpy(&ullEnd, &end, sizeof(ULONGLONG));
    00072     memcpy(&ullStart, &start, sizeof(ULONGLONG));
    00073     return 10*static_cast<int>(ullEnd - ullStart);
    00074 }
    00075
    00076 #endif // HAVE_QUERYPERFORMANCECOUNTER
    00077
    00078 inline
    00079 void sleep(int seconds)
    00080 {
    00081     ::Sleep(seconds * 1000);
    00082 }
    00083
    00084 #else // Unix
    00085
    00086 #include <sys/time.h>
    00087
    00088 #if RLOG_TIME_TSC
    00089
    00090 #include <stdint.h>
    00091
    00092 typedef uint64_t rlog_time;
    00093
    00094 #define RLOG_TIME_UNIT "clock cycles"
    00095
    00096 inline void rlog_get_time(uint64_t *pt)
    00097 {
    00098     asm volatile("RDTSC" : "=A" (*pt));
    00099 }
    00100
    00101 inline
    00102 int64_t rlog_time_diff( const rlog_time &end, const rlog_time &start )
    00103 {
    00104     return end - start;
    00105 }
    00106
    00107 #else // !HAVE_TSC
    00108
    00109 #include <unistd.h>
    00110
    00111 typedef timeval rlog_time;
    00112
    00113 #define RLOG_TIME_UNIT "usec"
    00114
    00115 inline
    00116 void rlog_get_time(rlog_time *pt)
    00117 {
    00118     gettimeofday( pt, 0 );
    00119 }
    00120
    00121 inline
    00122 int64_t rlog_time_diff( const rlog_time &end, const rlog_time &start )
    00123 {
    00124     return (end.tv_sec - start.tv_sec) * 1000 * 1000 +
    00125         (end.tv_usec - start.tv_usec);
    00126 }
    00127
    00128 #endif // HAVE_TSC/!HAVE_TSC
    00129
    00130 #endif // Win32/Unix
    00131
    00132 #endif // _rlog_time_incl