From 7fa1f6ed0726ff0f908245e43a01f50620293e8d Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 24 Aug 2017 15:48:42 -0700 Subject: uhd: Moved get_system_time outside of public API uhd::get_system_time() is an abstracted way of reading back a time, and is not UHD-specific. As such, there's no reason to keep it in the public part of the API where we're contractually obligated not to touch it. Instead, moving it to the internal API space. --- host/lib/utils/CMakeLists.txt | 63 ++++++++++++++++++++++++++++++++++++++++++ host/lib/utils/system_time.cpp | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 host/lib/utils/system_time.cpp (limited to 'host/lib/utils') diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 659c855a1..ea59df081 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -90,6 +90,68 @@ SET_SOURCE_FILES_PROPERTIES( PROPERTIES COMPILE_DEFINITIONS "${THREAD_PRIO_DEFS}" ) +######################################################################## +# Setup defines for high resolution timing +######################################################################## +MESSAGE(STATUS "") +MESSAGE(STATUS "Configuring high resolution timing...") +INCLUDE(CheckCXXSourceCompiles) + +SET(CMAKE_REQUIRED_LIBRARIES -lrt) +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + timespec ts; + return clock_gettime(CLOCK_MONOTONIC, &ts); + } + " HAVE_CLOCK_GETTIME +) +SET(CMAKE_REQUIRED_LIBRARIES) + +INCLUDE(CheckCXXSourceCompiles) +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + mach_timebase_info_data_t info; + mach_timebase_info(&info); + mach_absolute_time(); + return 0; + } + " HAVE_MACH_ABSOLUTE_TIME +) + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + LARGE_INTEGER value; + QueryPerformanceCounter(&value); + QueryPerformanceFrequency(&value); + return 0; + } + " HAVE_QUERY_PERFORMANCE_COUNTER +) + + +IF(HAVE_CLOCK_GETTIME) + MESSAGE(STATUS " High resolution timing supported through clock_gettime.") + SET(SYSTEM_TIME_DEFS HAVE_CLOCK_GETTIME) + LIBUHD_APPEND_LIBS("-lrt") +ELSEIF(HAVE_MACH_ABSOLUTE_TIME) + MESSAGE(STATUS " High resolution timing supported through mach_absolute_time.") + SET(SYSTEM_TIME_DEFS HAVE_MACH_ABSOLUTE_TIME) +ELSEIF(HAVE_QUERY_PERFORMANCE_COUNTER) + MESSAGE(STATUS " High resolution timing supported through QueryPerformanceCounter.") + SET(SYSTEM_TIME_DEFS HAVE_QUERY_PERFORMANCE_COUNTER) +ELSE() + MESSAGE(STATUS " High resolution timing supported though microsec_clock.") + SET(SYSTEM_TIME_DEFS HAVE_MICROSEC_CLOCK) +ENDIF() + +SET_SOURCE_FILES_PROPERTIES( + ${CMAKE_CURRENT_SOURCE_DIR}/system_time.cpp + PROPERTIES COMPILE_DEFINITIONS "${SYSTEM_TIME_DEFS}" +) + ######################################################################## # Setup defines for module loading ######################################################################## @@ -178,6 +240,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/prefs.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/system_time.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp ) diff --git a/host/lib/utils/system_time.cpp b/host/lib/utils/system_time.cpp new file mode 100644 index 000000000..d371756bc --- /dev/null +++ b/host/lib/utils/system_time.cpp @@ -0,0 +1,54 @@ +// +// Copyright 2017 Ettus Research (National Instruments Corp.) +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#include + +using namespace uhd; + +#ifdef HAVE_CLOCK_GETTIME +#include +time_spec_t uhd::get_system_time(void){ + timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); + return time_spec_t(ts.tv_sec, ts.tv_nsec, 1e9); +} +#endif /* HAVE_CLOCK_GETTIME */ + + +#ifdef HAVE_MACH_ABSOLUTE_TIME +#include +time_spec_t uhd::get_system_time(void){ + mach_timebase_info_data_t info; mach_timebase_info(&info); + intmax_t nanosecs = mach_absolute_time()*info.numer/info.denom; + return time_spec_t::from_ticks(nanosecs, 1e9); +} +#endif /* HAVE_MACH_ABSOLUTE_TIME */ + + +#ifdef HAVE_QUERY_PERFORMANCE_COUNTER +#include +time_spec_t uhd::get_system_time(void){ + LARGE_INTEGER counts, freq; + QueryPerformanceCounter(&counts); + QueryPerformanceFrequency(&freq); + return time_spec_t::from_ticks(counts.QuadPart, double(freq.QuadPart)); +} +#endif /* HAVE_QUERY_PERFORMANCE_COUNTER */ + + +#ifdef HAVE_MICROSEC_CLOCK +#include +namespace pt = boost::posix_time; +time_spec_t uhd::get_system_time(void){ + pt::ptime time_now = pt::microsec_clock::universal_time(); + pt::time_duration time_dur = time_now - pt::from_time_t(0); + return time_spec_t( + time_t(time_dur.total_seconds()), + long(time_dur.fractional_seconds()), + double(pt::time_duration::ticks_per_second()) + ); +} +#endif /* HAVE_MICROSEC_CLOCK */ + -- cgit v1.2.3