<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>Crypto++: hrtimer.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.3.7 --> <div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a></div> <h1>hrtimer.cpp</h1><pre class="fragment"><div>00001 <span class="comment">// hrtimer.cpp - written and placed in the public domain by Wei Dai</span> 00002 00003 <span class="preprocessor">#include "pch.h"</span> 00004 <span class="preprocessor">#include "hrtimer.h"</span> 00005 <span class="preprocessor">#include "misc.h"</span> 00006 <span class="preprocessor">#include <stddef.h></span> <span class="comment">// for NULL</span> 00007 <span class="preprocessor">#include <time.h></span> 00008 00009 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span> 00010 <span class="preprocessor"></span><span class="preprocessor">#include <windows.h></span> 00011 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span> 00012 <span class="preprocessor"></span><span class="preprocessor">#include <sys/time.h></span> 00013 <span class="preprocessor">#include <sys/times.h></span> 00014 <span class="preprocessor">#include <unistd.h></span> 00015 <span class="preprocessor">#endif</span> 00016 <span class="preprocessor"></span> 00017 <span class="preprocessor">#include <assert.h></span> 00018 00019 NAMESPACE_BEGIN(CryptoPP) 00020 00021 double <a class="code" href="class_timer_base.html">TimerBase</a>::ConvertTo(word64 t, Unit unit) 00022 { 00023 <span class="keyword">static</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000}; 00024 00025 assert(unit < <span class="keyword">sizeof</span>(unitsPerSecondTable) / <span class="keyword">sizeof</span>(unitsPerSecondTable[0])); 00026 <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)t * unitsPerSecondTable[unit] / TicksPerSecond(); 00027 } 00028 00029 <span class="keywordtype">void</span> TimerBase::StartTimer() 00030 { 00031 m_start = GetCurrentTimerValue(); 00032 m_started = <span class="keyword">true</span>; 00033 } 00034 00035 <span class="keywordtype">double</span> TimerBase::ElapsedTimeAsDouble() 00036 { 00037 <span class="keywordflow">if</span> (m_stuckAtZero) 00038 <span class="keywordflow">return</span> 0; 00039 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_started) 00040 <span class="keywordflow">return</span> ConvertTo(GetCurrentTimerValue() - m_start, m_timerUnit); 00041 <span class="keywordflow">else</span> 00042 { 00043 StartTimer(); 00044 <span class="keywordflow">return</span> 0; 00045 } 00046 } 00047 00048 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> TimerBase::ElapsedTime() 00049 { 00050 <span class="keywordtype">double</span> elapsed = ElapsedTimeAsDouble(); 00051 assert(elapsed <= ULONG_MAX); 00052 <span class="keywordflow">return</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>)elapsed; 00053 } 00054 00055 word64 ThreadUserTimer::GetCurrentTimerValue() 00056 { 00057 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span> 00058 <span class="preprocessor"></span> <span class="keyword">static</span> <span class="keywordtype">bool</span> getCurrentThreadImplemented = <span class="keyword">true</span>; 00059 <span class="keywordflow">if</span> (getCurrentThreadImplemented) 00060 { 00061 FILETIME now, ignored; 00062 <span class="keywordflow">if</span> (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now)) 00063 { 00064 DWORD lastError = GetLastError(); 00065 <span class="keywordflow">if</span> (lastError == ERROR_CALL_NOT_IMPLEMENTED) 00066 { 00067 getCurrentThreadImplemented = <span class="keyword">false</span>; 00068 <span class="keywordflow">goto</span> GetCurrentThreadNotImplemented; 00069 } 00070 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"ThreadUserTimer: GetThreadTimes failed with error "</span> + IntToString(lastError)); 00071 } 00072 <span class="keywordflow">return</span> now.dwLowDateTime + ((word64)now.dwHighDateTime << 32); 00073 } 00074 GetCurrentThreadNotImplemented: 00075 <span class="keywordflow">return</span> (word64)clock() * (10*1000*1000 / CLOCKS_PER_SEC); 00076 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span> 00077 <span class="preprocessor"></span> tms now; 00078 times(&now); 00079 <span class="keywordflow">return</span> now.tms_utime; 00080 <span class="preprocessor">#else</span> 00081 <span class="preprocessor"></span> <span class="keywordflow">return</span> clock(); 00082 <span class="preprocessor">#endif</span> 00083 <span class="preprocessor"></span>} 00084 00085 word64 ThreadUserTimer::TicksPerSecond() 00086 { 00087 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span> 00088 <span class="preprocessor"></span> <span class="keywordflow">return</span> 10*1000*1000; 00089 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span> 00090 <span class="preprocessor"></span> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">long</span> ticksPerSecond = sysconf(_SC_CLK_TCK); 00091 <span class="keywordflow">return</span> ticksPerSecond; 00092 <span class="preprocessor">#else</span> 00093 <span class="preprocessor"></span> <span class="keywordflow">return</span> CLOCKS_PER_SEC; 00094 <span class="preprocessor">#endif</span> 00095 <span class="preprocessor"></span>} 00096 00097 <span class="preprocessor">#ifdef HIGHRES_TIMER_AVAILABLE</span> 00098 <span class="preprocessor"></span> 00099 word64 Timer::GetCurrentTimerValue() 00100 { 00101 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span> 00102 <span class="preprocessor"></span> LARGE_INTEGER now; 00103 <span class="keywordflow">if</span> (!QueryPerformanceCounter(&now)) 00104 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"Timer: QueryPerformanceCounter failed with error "</span> + IntToString(GetLastError())); 00105 <span class="keywordflow">return</span> now.QuadPart; 00106 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span> 00107 <span class="preprocessor"></span> timeval now; 00108 gettimeofday(&now, NULL); 00109 <span class="keywordflow">return</span> (word64)now.tv_sec * 1000000 + now.tv_usec; 00110 <span class="preprocessor">#endif</span> 00111 <span class="preprocessor"></span>} 00112 00113 word64 Timer::TicksPerSecond() 00114 { 00115 <span class="preprocessor">#if defined(CRYPTOPP_WIN32_AVAILABLE)</span> 00116 <span class="preprocessor"></span> <span class="keyword">static</span> LARGE_INTEGER freq = {0}; 00117 <span class="keywordflow">if</span> (freq.QuadPart == 0) 00118 { 00119 <span class="keywordflow">if</span> (!QueryPerformanceFrequency(&freq)) 00120 <span class="keywordflow">throw</span> <a class="code" href="class_exception.html">Exception</a>(Exception::OTHER_ERROR, <span class="stringliteral">"Timer: QueryPerformanceFrequency failed with error "</span> + IntToString(GetLastError())); 00121 } 00122 <span class="keywordflow">return</span> freq.QuadPart; 00123 <span class="preprocessor">#elif defined(CRYPTOPP_UNIX_AVAILABLE)</span> 00124 <span class="preprocessor"></span> <span class="keywordflow">return</span> 1000000; 00125 <span class="preprocessor">#endif</span> 00126 <span class="preprocessor"></span>} 00127 00128 <span class="preprocessor">#endif</span> 00129 <span class="preprocessor"></span> 00130 NAMESPACE_END </div></pre><hr size="1"><address style="align: right;"><small>Generated on Sun Nov 7 08:23:57 2004 for Crypto++ by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address> </body> </html>