aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorlloyd <[email protected]>2009-12-01 12:00:48 +0000
committerlloyd <[email protected]>2009-12-01 12:00:48 +0000
commit874dbb8323dd4d7eff3ff16cff0cfafc16ddbfa7 (patch)
treebc3a84c43c5924119972f24da3af89317694f0cb /src/utils
parent78f5726220b637cd8ae117bbcf8ff8d6c8dfeaed (diff)
Consolidate the non-canonical epoch timers, like cpuid and Win32's
QueryPerformanceCounter, into an entropy source hres_timer. Its results, if any, do not count as contributing entropy to the poll. Convert the other (monotonic/fixed epoch) timers to a single function get_nanoseconds_clock(), living in time.h, which statically chooses the 'best' timer type (clock_gettime, gettimeofday, std::clock, in that order depending on what is available). Add feature test macros for clock_gettime and gettimeofday. Remove the Timer class and timer.h. Remove the Timer& argument to the algorithm benchmark function.
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/info.txt2
-rw-r--r--src/utils/time.cpp87
-rw-r--r--src/utils/time.h30
3 files changed, 118 insertions, 1 deletions
diff --git a/src/utils/info.txt b/src/utils/info.txt
index 3d024fa09..110afeb64 100644
--- a/src/utils/info.txt
+++ b/src/utils/info.txt
@@ -3,5 +3,5 @@ define UTIL_FUNCTIONS
load_on always
<libs>
-tru64 -> rt
+linux,tru64 -> rt
</libs>
diff --git a/src/utils/time.cpp b/src/utils/time.cpp
new file mode 100644
index 000000000..856b1c7be
--- /dev/null
+++ b/src/utils/time.cpp
@@ -0,0 +1,87 @@
+/**
+* Time Functions
+* (C) 1999-2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/time.h>
+#include <botan/exceptn.h>
+#include <ctime>
+
+#if defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY)
+ #include <sys/time.h>
+#endif
+
+#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
+
+#ifndef _POSIX_C_SOURCE
+ #define _POSIX_C_SOURCE 199309
+#endif
+
+#include <time.h>
+
+#ifndef CLOCK_REALTIME
+ #define CLOCK_REALTIME 0
+#endif
+
+#endif
+
+namespace Botan {
+
+namespace {
+
+/**
+* Combine a two time values into a single one
+*/
+u64bit combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz)
+ {
+ static const u64bit NANOSECONDS_UNITS = 1000000000;
+
+ u64bit res = seconds * NANOSECONDS_UNITS;
+ res += parts * (NANOSECONDS_UNITS / parts_hz);
+ return res;
+ }
+
+}
+
+/**
+* Get the system clock
+*/
+u64bit system_time()
+ {
+ return static_cast<u64bit>(std::time(0));
+ }
+
+/*
+* Convert a time_t to a struct tm
+*/
+std::tm time_t_to_tm(u64bit timer)
+ {
+ std::time_t time_val = static_cast<std::time_t>(timer);
+
+ std::tm* tm_p = std::gmtime(&time_val);
+ if (tm_p == 0)
+ throw Encoding_Error("time_t_to_tm could not convert");
+ return (*tm_p);
+ }
+
+u64bit get_nanoseconds_clock()
+ {
+#if defined(BOTAN_TARGET_OS_HAS_CLOCK_GETTIME)
+ struct ::timespec tv;
+ ::clock_gettime(CLOCK_REALTIME, &tv);
+ return combine_timers(tv.tv_sec, tv.tv_nsec, 1000000000);
+
+#elif defined(BOTAN_TARGET_OS_HAS_GETTIMEOFDAY)
+ struct ::timeval tv;
+ ::gettimeofday(&tv, 0);
+ return combine_timers(tv.tv_sec, tv.tv_usec, 1000000);
+
+#else
+ return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC);
+
+#endif
+ }
+
+}
diff --git a/src/utils/time.h b/src/utils/time.h
new file mode 100644
index 000000000..c7f459096
--- /dev/null
+++ b/src/utils/time.h
@@ -0,0 +1,30 @@
+/**
+* Time Functions
+* (C) 1999-2009 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_TIME_H__
+#define BOTAN_TIME_H__
+
+#include <botan/types.h>
+#include <ctime>
+
+namespace Botan {
+
+/*
+* Time Access/Conversion Functions
+*/
+BOTAN_DLL u64bit system_time();
+
+BOTAN_DLL std::tm time_t_to_tm(u64bit);
+
+/**
+@return nanoseconds resolution timestamp, unknown epoch
+*/
+BOTAN_DLL u64bit get_nanoseconds_clock();
+
+}
+
+#endif