Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:01

0001 /*
0002     Copyright (c) 2005-2020 Intel Corporation
0003 
0004     Licensed under the Apache License, Version 2.0 (the "License");
0005     you may not use this file except in compliance with the License.
0006     You may obtain a copy of the License at
0007 
0008         http://www.apache.org/licenses/LICENSE-2.0
0009 
0010     Unless required by applicable law or agreed to in writing, software
0011     distributed under the License is distributed on an "AS IS" BASIS,
0012     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013     See the License for the specific language governing permissions and
0014     limitations under the License.
0015 */
0016 
0017 #ifndef __TBB_tick_count_H
0018 #define __TBB_tick_count_H
0019 
0020 #include "tbb_stddef.h"
0021 
0022 #if _WIN32||_WIN64
0023 #include "machine/windows_api.h"
0024 #elif __linux__
0025 #include <ctime>
0026 #else /* generic Unix */
0027 #include <sys/time.h>
0028 #endif /* (choice of OS) */
0029 
0030 namespace tbb {
0031 
0032 //! Absolute timestamp
0033 /** @ingroup timing */
0034 class tick_count {
0035 public:
0036     //! Relative time interval.
0037     class interval_t {
0038         long long value;
0039         explicit interval_t( long long value_ ) : value(value_) {}
0040     public:
0041         //! Construct a time interval representing zero time duration
0042         interval_t() : value(0) {};
0043 
0044         //! Construct a time interval representing sec seconds time  duration
0045         explicit interval_t( double sec );
0046 
0047         //! Return the length of a time interval in seconds
0048         double seconds() const;
0049 
0050         friend class tbb::tick_count;
0051 
0052         //! Extract the intervals from the tick_counts and subtract them.
0053         friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
0054 
0055         //! Add two intervals.
0056         friend interval_t operator+( const interval_t& i, const interval_t& j ) {
0057             return interval_t(i.value+j.value);
0058         }
0059 
0060         //! Subtract two intervals.
0061         friend interval_t operator-( const interval_t& i, const interval_t& j ) {
0062             return interval_t(i.value-j.value);
0063         }
0064 
0065         //! Accumulation operator
0066         interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
0067 
0068         //! Subtraction operator
0069         interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
0070     private:
0071         static long long ticks_per_second(){
0072 #if _WIN32||_WIN64
0073             LARGE_INTEGER qpfreq;
0074             int rval = QueryPerformanceFrequency(&qpfreq);
0075             __TBB_ASSERT_EX(rval, "QueryPerformanceFrequency returned zero");
0076             return static_cast<long long>(qpfreq.QuadPart);
0077 #elif __linux__
0078             return static_cast<long long>(1E9);
0079 #else /* generic Unix */
0080             return static_cast<long long>(1E6);
0081 #endif /* (choice of OS) */
0082         }
0083     };
0084 
0085     //! Construct an absolute timestamp initialized to zero.
0086     tick_count() : my_count(0) {};
0087 
0088     //! Return current time.
0089     static tick_count now();
0090 
0091     //! Subtract two timestamps to get the time interval between
0092     friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
0093 
0094     //! Return the resolution of the clock in seconds per tick.
0095     static double resolution() { return 1.0 / interval_t::ticks_per_second(); }
0096 
0097 private:
0098     long long my_count;
0099 };
0100 
0101 inline tick_count tick_count::now() {
0102     tick_count result;
0103 #if _WIN32||_WIN64
0104     LARGE_INTEGER qpcnt;
0105     int rval = QueryPerformanceCounter(&qpcnt);
0106     __TBB_ASSERT_EX(rval, "QueryPerformanceCounter failed");
0107     result.my_count = qpcnt.QuadPart;
0108 #elif __linux__
0109     struct timespec ts;
0110     int status = clock_gettime( CLOCK_REALTIME, &ts );
0111     __TBB_ASSERT_EX( status==0, "CLOCK_REALTIME not supported" );
0112     result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
0113 #else /* generic Unix */
0114     struct timeval tv;
0115     int status = gettimeofday(&tv, NULL);
0116     __TBB_ASSERT_EX( status==0, "gettimeofday failed" );
0117     result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
0118 #endif /*(choice of OS) */
0119     return result;
0120 }
0121 
0122 inline tick_count::interval_t::interval_t( double sec ) {
0123     value = static_cast<long long>(sec*interval_t::ticks_per_second());
0124 }
0125 
0126 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
0127     return tick_count::interval_t( t1.my_count-t0.my_count );
0128 }
0129 
0130 inline double tick_count::interval_t::seconds() const {
0131     return value*tick_count::resolution();
0132 }
0133 
0134 } // namespace tbb
0135 
0136 #endif /* __TBB_tick_count_H */