diff options
author | lloyd <[email protected]> | 2008-11-11 20:58:37 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-11 20:58:37 +0000 |
commit | 8879a51da7c3b93e27439122cea5d5aa81ae38c3 (patch) | |
tree | b1faa753266d4a762fc38252df35a2435b757b92 /src/timer | |
parent | 6eff33ae263109ccbbeb32bd0ffb25c77140cc45 (diff) |
Move utils/{timer,mutex} to toplevel
Diffstat (limited to 'src/timer')
-rw-r--r-- | src/timer/cpu_counter/info.txt | 36 | ||||
-rw-r--r-- | src/timer/cpu_counter/tm_hard.cpp | 49 | ||||
-rw-r--r-- | src/timer/cpu_counter/tm_hard.h | 25 | ||||
-rw-r--r-- | src/timer/gettimeofday/info.txt | 32 | ||||
-rw-r--r-- | src/timer/gettimeofday/tm_unix.cpp | 22 | ||||
-rw-r--r-- | src/timer/gettimeofday/tm_unix.h | 25 | ||||
-rw-r--r-- | src/timer/info.txt | 10 | ||||
-rw-r--r-- | src/timer/posix_rt/info.txt | 28 | ||||
-rw-r--r-- | src/timer/posix_rt/tm_posix.cpp | 31 | ||||
-rw-r--r-- | src/timer/posix_rt/tm_posix.h | 25 | ||||
-rw-r--r-- | src/timer/timer.cpp | 57 | ||||
-rw-r--r-- | src/timer/timer.h | 30 | ||||
-rw-r--r-- | src/timer/win32_query_perf_ctr/info.txt | 26 | ||||
-rw-r--r-- | src/timer/win32_query_perf_ctr/tm_win32.cpp | 21 | ||||
-rw-r--r-- | src/timer/win32_query_perf_ctr/tm_win32.h | 25 |
15 files changed, 442 insertions, 0 deletions
diff --git a/src/timer/cpu_counter/info.txt b/src/timer/cpu_counter/info.txt new file mode 100644 index 000000000..025663a84 --- /dev/null +++ b/src/timer/cpu_counter/info.txt @@ -0,0 +1,36 @@ +realname "Hardware Timer" + +define TIMER_HARDWARE + +load_on asm_ok + +<add> +tm_hard.cpp +tm_hard.h +</add> + +<cc> +gcc +</cc> + +<arch> +# RDTSC: Pentium and up +i586 +i686 +athlon +pentium4 +pentium-m +amd64 + +ppc # PPC timebase register +ppc64 # PPC timebase register +alpha # rpcc +sparc64 # %tick register +ia64 # ar.itc +s390x +hppa +</arch> + +<requires> +timer +</requires> diff --git a/src/timer/cpu_counter/tm_hard.cpp b/src/timer/cpu_counter/tm_hard.cpp new file mode 100644 index 000000000..2f7516930 --- /dev/null +++ b/src/timer/cpu_counter/tm_hard.cpp @@ -0,0 +1,49 @@ +/************************************************* +* Hardware Timer Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/tm_hard.h> + +namespace Botan { + +/************************************************* +* Get the timestamp * +*************************************************/ +u64bit Hardware_Timer::clock() const + { + u64bit rtc = 0; + +#if defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low)); + rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; + +#elif defined(BOTAN_TARGET_ARCH_IS_PPC) || defined(BOTAN_TARGET_ARCH_IS_PPC64) + u32bit rtc_low = 0, rtc_high = 0; + asm volatile("mftbu %0; mftb %1" : "=r" (rtc_high), "=r" (rtc_low)); + rtc = (static_cast<u64bit>(rtc_high) << 32) | rtc_low; + +#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) + asm volatile("rpcc %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_SPARC64) + asm volatile("rd %%tick, %0" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_IA64) + asm volatile("mov %0=ar.itc" : "=r" (rtc)); + +#elif defined(BOTAN_TARGET_ARCH_IS_S390X) + asm volatile("stck 0(%0)" : : "a" (&rtc) : "memory", "cc"); + +#elif defined(BOTAN_TARGET_ARCH_IS_HPPA) + asm volatile("mfctl 16,%0" : "=r" (rtc)); // 64-bit only? + +#else + #error "Unsure how to access hardware timer on this system" +#endif + + return rtc; + } + +} diff --git a/src/timer/cpu_counter/tm_hard.h b/src/timer/cpu_counter/tm_hard.h new file mode 100644 index 000000000..ec5268085 --- /dev/null +++ b/src/timer/cpu_counter/tm_hard.h @@ -0,0 +1,25 @@ +/************************************************* +* Hardware Timer Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_TIMER_HARDWARE_H__ +#define BOTAN_TIMER_HARDWARE_H__ + +#include <botan/timer.h> + +namespace Botan { + +/************************************************* +* Hardware Timer * +*************************************************/ +class BOTAN_DLL Hardware_Timer : public Timer + { + public: + std::string name() const { return "Hardware Timer"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/src/timer/gettimeofday/info.txt b/src/timer/gettimeofday/info.txt new file mode 100644 index 000000000..d3812eedf --- /dev/null +++ b/src/timer/gettimeofday/info.txt @@ -0,0 +1,32 @@ +realname "Unix Timer" + +define TIMER_UNIX + +load_on auto +modset unix,beos + +<add> +tm_unix.cpp +tm_unix.h +</add> + +<os> +aix +beos +cygwin +darwin +freebsd +hpux +irix +linux +netbsd +openbsd +qnx +solaris +tru64 +</os> + +<requires> +timer +</requires> + diff --git a/src/timer/gettimeofday/tm_unix.cpp b/src/timer/gettimeofday/tm_unix.cpp new file mode 100644 index 000000000..654297753 --- /dev/null +++ b/src/timer/gettimeofday/tm_unix.cpp @@ -0,0 +1,22 @@ +/************************************************* +* Unix Timer Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/tm_unix.h> +#include <botan/util.h> +#include <sys/time.h> + +namespace Botan { + +/************************************************* +* Get the timestamp * +*************************************************/ +u64bit Unix_Timer::clock() const + { + struct ::timeval tv; + ::gettimeofday(&tv, 0); + return combine_timers(tv.tv_sec, tv.tv_usec, 1000000); + } + +} diff --git a/src/timer/gettimeofday/tm_unix.h b/src/timer/gettimeofday/tm_unix.h new file mode 100644 index 000000000..21afb3ee8 --- /dev/null +++ b/src/timer/gettimeofday/tm_unix.h @@ -0,0 +1,25 @@ +/************************************************* +* Unix Timer Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_TIMER_UNIX_H__ +#define BOTAN_TIMER_UNIX_H__ + +#include <botan/timer.h> + +namespace Botan { + +/************************************************* +* Unix Timer * +*************************************************/ +class BOTAN_DLL Unix_Timer : public Timer + { + public: + std::string name() const { return "Unix gettimeofday"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/src/timer/info.txt b/src/timer/info.txt new file mode 100644 index 000000000..c9a860a78 --- /dev/null +++ b/src/timer/info.txt @@ -0,0 +1,10 @@ +realname "Timer Base Class" + +define TIMER + +load_on auto + +<add> +timer.cpp +timer.h +</add> diff --git a/src/timer/posix_rt/info.txt b/src/timer/posix_rt/info.txt new file mode 100644 index 000000000..7501373bb --- /dev/null +++ b/src/timer/posix_rt/info.txt @@ -0,0 +1,28 @@ +realname "POSIX Timer" + +define TIMER_POSIX + +load_on auto + +<add> +tm_posix.cpp +tm_posix.h +</add> + +<libs> +linux -> rt +</libs> + +# The *BSDs put clock_gettime in sys/time.h, not time.h like POSIX says +<os> +cygwin +linux +#freebsd +#netbsd +#openbsd +</os> + +<requires> +timer +</requires> + diff --git a/src/timer/posix_rt/tm_posix.cpp b/src/timer/posix_rt/tm_posix.cpp new file mode 100644 index 000000000..601b2b43d --- /dev/null +++ b/src/timer/posix_rt/tm_posix.cpp @@ -0,0 +1,31 @@ +/************************************************* +* POSIX Timer Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/tm_posix.h> +#include <botan/util.h> + +#ifndef _POSIX_C_SOURCE + #define _POSIX_C_SOURCE 199309 +#endif + +#include <time.h> + +#ifndef CLOCK_REALTIME + #define CLOCK_REALTIME 0 +#endif + +namespace Botan { + +/************************************************* +* Get the timestamp * +*************************************************/ +u64bit POSIX_Timer::clock() const + { + struct ::timespec tv; + ::clock_gettime(CLOCK_REALTIME, &tv); + return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000); + } + +} diff --git a/src/timer/posix_rt/tm_posix.h b/src/timer/posix_rt/tm_posix.h new file mode 100644 index 000000000..077636a0a --- /dev/null +++ b/src/timer/posix_rt/tm_posix.h @@ -0,0 +1,25 @@ +/************************************************* +* POSIX Timer Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_TIMER_POSIX_H__ +#define BOTAN_TIMER_POSIX_H__ + +#include <botan/timer.h> + +namespace Botan { + +/************************************************* +* POSIX Timer * +*************************************************/ +class BOTAN_DLL POSIX_Timer : public Timer + { + public: + std::string name() const { return "POSIX clock_gettime"; } + u64bit clock() const; + }; + +} + +#endif diff --git a/src/timer/timer.cpp b/src/timer/timer.cpp new file mode 100644 index 000000000..a37cf39c3 --- /dev/null +++ b/src/timer/timer.cpp @@ -0,0 +1,57 @@ +/************************************************* +* Timestamp Functions Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/timer.h> +#include <botan/loadstor.h> +#include <botan/util.h> +#include <ctime> + +namespace Botan { + +/************************************************* +* Get the system clock * +*************************************************/ +u64bit system_time() + { + return static_cast<u64bit>(std::time(0)); + } + +/************************************************* +* Default Timer clock reading * +*************************************************/ +u64bit Timer::clock() const + { + return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); + } + +/************************************************* +* Read the clock and return the output * +*************************************************/ +u32bit Timer::fast_poll(byte out[], u32bit length) + { + const u64bit clock_value = this->clock(); + + for(u32bit j = 0; j != sizeof(clock_value); ++j) + out[j % length] ^= get_byte(j, clock_value); + + return (length < 8) ? length : 8; + } + +u32bit Timer::slow_poll(byte out[], u32bit length) + { + return fast_poll(out, length); + } + +/************************************************* +* Combine a two time values into a single one * +*************************************************/ +u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) + { + static const u64bit NANOSECONDS_UNITS = 1000000000; + parts *= (NANOSECONDS_UNITS / parts_hz); + return ((seconds * NANOSECONDS_UNITS) + parts); + } + +} diff --git a/src/timer/timer.h b/src/timer/timer.h new file mode 100644 index 000000000..5ad2cfbea --- /dev/null +++ b/src/timer/timer.h @@ -0,0 +1,30 @@ +/************************************************* +* Timestamp Functions Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_TIMERS_H__ +#define BOTAN_TIMERS_H__ + +#include <botan/rng.h> + +namespace Botan { + +/************************************************* +* Timer Interface * +*************************************************/ +class BOTAN_DLL Timer : public EntropySource + { + public: + virtual u64bit clock() const; + u32bit slow_poll(byte[], u32bit); + u32bit fast_poll(byte[], u32bit); + + virtual ~Timer() {} + protected: + static u64bit combine_timers(u32bit, u32bit, u32bit); + }; + +} + +#endif diff --git a/src/timer/win32_query_perf_ctr/info.txt b/src/timer/win32_query_perf_ctr/info.txt new file mode 100644 index 000000000..e74259184 --- /dev/null +++ b/src/timer/win32_query_perf_ctr/info.txt @@ -0,0 +1,26 @@ +realname "Win32 Timer" + +define TIMER_WIN32 +modset win32 + +load_on auto + +<add> +tm_win32.cpp +tm_win32.h +</add> + +<os> +cygwin +windows +mingw +</os> + +<libs> +windows -> user32 +</libs> + +<requires> +timer +</requires> + diff --git a/src/timer/win32_query_perf_ctr/tm_win32.cpp b/src/timer/win32_query_perf_ctr/tm_win32.cpp new file mode 100644 index 000000000..58f7b0f55 --- /dev/null +++ b/src/timer/win32_query_perf_ctr/tm_win32.cpp @@ -0,0 +1,21 @@ +/************************************************* +* Win32 Timer Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/tm_win32.h> +#include <windows.h> + +namespace Botan { + +/************************************************* +* Get the timestamp * +*************************************************/ +u64bit Win32_Timer::clock() const + { + LARGE_INTEGER tv; + ::QueryPerformanceCounter(&tv); + return tv.QuadPart; + } + +} diff --git a/src/timer/win32_query_perf_ctr/tm_win32.h b/src/timer/win32_query_perf_ctr/tm_win32.h new file mode 100644 index 000000000..d458d0a3f --- /dev/null +++ b/src/timer/win32_query_perf_ctr/tm_win32.h @@ -0,0 +1,25 @@ +/************************************************* +* Win32 Timer Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_TIMER_WIN32_H__ +#define BOTAN_TIMER_WIN32_H__ + +#include <botan/timer.h> + +namespace Botan { + +/************************************************* +* Win32 Timer * +*************************************************/ +class BOTAN_DLL Win32_Timer : public Timer + { + public: + std::string name() const { return "Win32 QueryPerformanceCounter"; } + u64bit clock() const; + }; + +} + +#endif |