00001 /* 00002 Copyright 2005-2010 Intel Corporation. All Rights Reserved. 00003 00004 This file is part of Threading Building Blocks. 00005 00006 Threading Building Blocks is free software; you can redistribute it 00007 and/or modify it under the terms of the GNU General Public License 00008 version 2 as published by the Free Software Foundation. 00009 00010 Threading Building Blocks is distributed in the hope that it will be 00011 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00012 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Threading Building Blocks; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 00019 As a special exception, you may use this file as part of a free software 00020 library without restriction. Specifically, if other files instantiate 00021 templates or use macros or inline functions from this file, or you compile 00022 this file and link it with other files to produce an executable, this 00023 file does not by itself cause the resulting executable to be covered by 00024 the GNU General Public License. This exception does not however 00025 invalidate any other reasons why the executable file might be covered by 00026 the GNU General Public License. 00027 */ 00028 00029 #ifndef __TBB_tick_count_H 00030 #define __TBB_tick_count_H 00031 00032 #include "tbb_stddef.h" 00033 00034 #if _WIN32||_WIN64 00035 #include <windows.h> 00036 #elif __linux__ 00037 #include <ctime> 00038 #else /* generic Unix */ 00039 #include <sys/time.h> 00040 #endif /* (choice of OS) */ 00041 00042 namespace tbb { 00043 00045 00046 class tick_count { 00047 public: 00049 class interval_t { 00050 long long value; 00051 explicit interval_t( long long value_ ) : value(value_) {} 00052 public: 00054 interval_t() : value(0) {}; 00055 00057 explicit interval_t( double sec ); 00058 00060 double seconds() const; 00061 00062 friend class tbb::tick_count; 00063 00065 friend interval_t operator-( const tick_count& t1, const tick_count& t0 ); 00066 00068 friend interval_t operator+( const interval_t& i, const interval_t& j ) { 00069 return interval_t(i.value+j.value); 00070 } 00071 00073 friend interval_t operator-( const interval_t& i, const interval_t& j ) { 00074 return interval_t(i.value-j.value); 00075 } 00076 00078 interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;} 00079 00081 interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;} 00082 }; 00083 00085 tick_count() : my_count(0) {}; 00086 00088 static tick_count now(); 00089 00091 friend interval_t operator-( const tick_count& t1, const tick_count& t0 ); 00092 00093 private: 00094 long long my_count; 00095 }; 00096 00097 inline tick_count tick_count::now() { 00098 tick_count result; 00099 #if _WIN32||_WIN64 00100 LARGE_INTEGER qpcnt; 00101 QueryPerformanceCounter(&qpcnt); 00102 result.my_count = qpcnt.QuadPart; 00103 #elif __linux__ 00104 struct timespec ts; 00105 #if TBB_USE_ASSERT 00106 int status = 00107 #endif /* TBB_USE_ASSERT */ 00108 clock_gettime( CLOCK_REALTIME, &ts ); 00109 __TBB_ASSERT( status==0, "CLOCK_REALTIME not supported" ); 00110 result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec); 00111 #else /* generic Unix */ 00112 struct timeval tv; 00113 #if TBB_USE_ASSERT 00114 int status = 00115 #endif /* TBB_USE_ASSERT */ 00116 gettimeofday(&tv, NULL); 00117 __TBB_ASSERT( status==0, "gettimeofday failed" ); 00118 result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec); 00119 #endif /*(choice of OS) */ 00120 return result; 00121 } 00122 00123 inline tick_count::interval_t::interval_t( double sec ) 00124 { 00125 #if _WIN32||_WIN64 00126 LARGE_INTEGER qpfreq; 00127 QueryPerformanceFrequency(&qpfreq); 00128 value = static_cast<long long>(sec*qpfreq.QuadPart); 00129 #elif __linux__ 00130 value = static_cast<long long>(sec*1E9); 00131 #else /* generic Unix */ 00132 value = static_cast<long long>(sec*1E6); 00133 #endif /* (choice of OS) */ 00134 } 00135 00136 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) { 00137 return tick_count::interval_t( t1.my_count-t0.my_count ); 00138 } 00139 00140 inline double tick_count::interval_t::seconds() const { 00141 #if _WIN32||_WIN64 00142 LARGE_INTEGER qpfreq; 00143 QueryPerformanceFrequency(&qpfreq); 00144 return value/(double)qpfreq.QuadPart; 00145 #elif __linux__ 00146 return value*1E-9; 00147 #else /* generic Unix */ 00148 return value*1E-6; 00149 #endif /* (choice of OS) */ 00150 } 00151 00152 } // namespace tbb 00153 00154 #endif /* __TBB_tick_count_H */ 00155
Copyright © 2005-2010 Intel Corporation. All Rights Reserved.
Licensed under the GNU General Public License 2 with the runtime exception.
Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.
* Other names and brands may be claimed as the property of others.
