From c33928d2bbdd27688c3475e77fc461e7d16eba5a Mon Sep 17 00:00:00 2001 From: Andrej Rode Date: Tue, 18 Apr 2017 16:46:44 -0700 Subject: utils: add set_thread_name API call, move thread_priority to thread --- host/lib/utils/CMakeLists.txt | 36 ++++++++++- host/lib/utils/tasks.cpp | 13 ++-- host/lib/utils/thread.cpp | 122 +++++++++++++++++++++++++++++++++++ host/lib/utils/thread_priority.cpp | 110 ------------------------------- host/lib/utils/thread_priority_c.cpp | 2 +- 5 files changed, 165 insertions(+), 118 deletions(-) create mode 100644 host/lib/utils/thread.cpp delete mode 100644 host/lib/utils/thread_priority.cpp (limited to 'host/lib/utils') diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index f76c5763a..a053846c6 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -51,9 +51,11 @@ CHECK_CXX_SOURCE_COMPILES(" " HAVE_WIN_SETTHREADPRIORITY ) + + IF(HAVE_PTHREAD_SETSCHEDPARAM) MESSAGE(STATUS " Priority scheduling supported through pthread_setschedparam.") - SET(THREAD_PRIO_DEFS HAVE_PTHREAD_SETSCHEDPARAM) + LIST(APPEND THREAD_PRIO_DEFS HAVE_PTHREAD_SETSCHEDPARAM) LIBUHD_APPEND_LIBS(pthread) ELSEIF(HAVE_WIN_SETTHREADPRIORITY) MESSAGE(STATUS " Priority scheduling supported through windows SetThreadPriority.") @@ -63,8 +65,36 @@ ELSE() SET(THREAD_PRIO_DEFS HAVE_THREAD_PRIO_DUMMY) ENDIF() +SET(CMAKE_REQUIRED_LIBRARIES "pthread") + +CHECK_CXX_SOURCE_COMPILES(" + #include + int main(){ + pthread_t pt; + const char* pt_name = \"test\"; + pthread_setname_np(pt, pt_name); + return 0; + } + " HAVE_PTHREAD_SETNAME +) + +IF(CYGWIN) + #SCHED_RR non-operational on cygwin + SET(HAVE_PTHREAD_SETNAME False) +ENDIF(CYGWIN) + +IF(HAVE_PTHREAD_SETNAME) + MESSAGE(STATUS " Setting thread names is supported through pthread_setname_np.") + LIST(APPEND THREAD_PRIO_DEFS HAVE_PTHREAD_SETNAME) + LIBUHD_APPEND_LIBS(pthread) +ELSE() + MESSAGE(STATUS " Setting thread names is not supported.") + LIST(APPEND THREAD_PRIO_DEFS HAVE_THREAD_SETNAME_DUMMY) +ENDIF() + + SET_SOURCE_FILES_PROPERTIES( - ${CMAKE_CURRENT_SOURCE_DIR}/thread_priority.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp PROPERTIES COMPILE_DEFINITIONS "${THREAD_PRIO_DEFS}" ) @@ -146,7 +176,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/thread_priority.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp ) IF(ENABLE_C_API) diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp index 38d19502e..fb9a3052e 100644 --- a/host/lib/utils/tasks.cpp +++ b/host/lib/utils/tasks.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -32,10 +33,15 @@ using namespace uhd; class task_impl : public task{ public: - task_impl(const task_fcn_type &task_fcn): + task_impl(const task_fcn_type &task_fcn, const std::string &name): _exit(false) { _task = std::thread([this, task_fcn](){ this->task_loop(task_fcn); }); + if (not name.empty()) { +#ifdef HAVE_PTHREAD_SETNAME + pthread_setname_np(_task->native_handle(), name.substr(0,16).c_str()); +#endif /* HAVE_PTHREAD_SETNAME */ + } } ~task_impl(void){ @@ -46,7 +52,6 @@ public: } private: - void task_loop(const task_fcn_type &task_fcn){ try{ while (!_exit){ @@ -73,8 +78,8 @@ private: std::thread _task; }; -task::sptr task::make(const task_fcn_type &task_fcn){ - return task::sptr(new task_impl(task_fcn)); +task::sptr task::make(const task_fcn_type &task_fcn, const std::string &name){ + return task::sptr(new task_impl(task_fcn, name)); } msg_task::~msg_task(void){ diff --git a/host/lib/utils/thread.cpp b/host/lib/utils/thread.cpp new file mode 100644 index 000000000..9100cfd7b --- /dev/null +++ b/host/lib/utils/thread.cpp @@ -0,0 +1,122 @@ +// +// Copyright 2010-2011,2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include +#include +#include +#include +#include + +bool uhd::set_thread_priority_safe(float priority, bool realtime){ + try{ + set_thread_priority(priority, realtime); + return true; + }catch(const std::exception &e){ + UHD_LOGGER_WARNING("UHD") << boost::format( + "Unable to set the thread priority. Performance may be negatively affected.\n" + "Please see the general application notes in the manual for instructions.\n" + "%s" + ) % e.what(); + return false; + } +} + +static void check_priority_range(float priority){ + if (priority > +1.0 or priority < -1.0) + throw uhd::value_error("priority out of range [-1.0, +1.0]"); +} + +/*********************************************************************** + * Pthread API to set priority + **********************************************************************/ +#ifdef HAVE_PTHREAD_SETSCHEDPARAM + #include + + void uhd::set_thread_priority(float priority, bool realtime){ + check_priority_range(priority); + + //when realtime is not enabled, use sched other + int policy = (realtime)? SCHED_RR : SCHED_OTHER; + + //we cannot have below normal priority, set to zero + if (priority < 0) priority = 0; + + //get the priority bounds for the selected policy + int min_pri = sched_get_priority_min(policy); + int max_pri = sched_get_priority_max(policy); + if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max"); + + //set the new priority and policy + sched_param sp; + sp.sched_priority = int(priority*(max_pri - min_pri)) + min_pri; + int ret = pthread_setschedparam(pthread_self(), policy, &sp); + if (ret != 0) throw uhd::os_error("error in pthread_setschedparam"); + } +#endif /* HAVE_PTHREAD_SETSCHEDPARAM */ + +/*********************************************************************** + * Windows API to set priority + **********************************************************************/ +#ifdef HAVE_WIN_SETTHREADPRIORITY + #include + + void uhd::set_thread_priority(float priority, UHD_UNUSED(bool realtime)){ + check_priority_range(priority); + + /* + * Process wide priority is no longer set. + * This is the responsibility of the application. + //set the priority class on the process + int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; + if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) + throw uhd::os_error("error in SetPriorityClass"); + */ + + //scale the priority value to the constants + int priorities[] = { + THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, + THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL + }; + size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 + + //set the thread priority on the thread + if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) + throw uhd::os_error("error in SetThreadPriority"); + } +#endif /* HAVE_WIN_SETTHREADPRIORITY */ + +/*********************************************************************** + * Unimplemented API to set priority + **********************************************************************/ +#ifdef HAVE_THREAD_PRIO_DUMMY + void uhd::set_thread_priority(float, bool){ + throw uhd::not_implemented_error("set thread priority not implemented"); + } + +#endif /* HAVE_THREAD_PRIO_DUMMY */ + +void uhd::set_thread_name( + boost::thread *thrd, + const std::string &name +) { +#ifdef HAVE_PTHREAD_SETNAME + pthread_setname_np(thrd->native_handle(), name.substr(0,16).c_str()); +#endif /* HAVE_PTHREAD_SETNAME */ +#ifdef HAVE_THREAD_SETNAME_DUMMY + UHD_LOG_DEBUG("UHD", "Setting thread name is not implemented; wanted to set to " << name); +#endif /* HAVE_THREAD_SETNAME_DUMMY */ +} diff --git a/host/lib/utils/thread_priority.cpp b/host/lib/utils/thread_priority.cpp deleted file mode 100644 index 729edcf0a..000000000 --- a/host/lib/utils/thread_priority.cpp +++ /dev/null @@ -1,110 +0,0 @@ -// -// Copyright 2010-2011,2015 Ettus Research LLC -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// - -#include -#include -#include -#include -#include - -bool uhd::set_thread_priority_safe(float priority, bool realtime){ - try{ - set_thread_priority(priority, realtime); - return true; - }catch(const std::exception &e){ - UHD_LOGGER_WARNING("UHD") << boost::format( - "Unable to set the thread priority. Performance may be negatively affected.\n" - "Please see the general application notes in the manual for instructions.\n" - "%s" - ) % e.what(); - return false; - } -} - -static void check_priority_range(float priority){ - if (priority > +1.0 or priority < -1.0) - throw uhd::value_error("priority out of range [-1.0, +1.0]"); -} - -/*********************************************************************** - * Pthread API to set priority - **********************************************************************/ -#ifdef HAVE_PTHREAD_SETSCHEDPARAM - #include - - void uhd::set_thread_priority(float priority, bool realtime){ - check_priority_range(priority); - - //when realtime is not enabled, use sched other - int policy = (realtime)? SCHED_RR : SCHED_OTHER; - - //we cannot have below normal priority, set to zero - if (priority < 0) priority = 0; - - //get the priority bounds for the selected policy - int min_pri = sched_get_priority_min(policy); - int max_pri = sched_get_priority_max(policy); - if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max"); - - //set the new priority and policy - sched_param sp; - sp.sched_priority = int(priority*(max_pri - min_pri)) + min_pri; - int ret = pthread_setschedparam(pthread_self(), policy, &sp); - if (ret != 0) throw uhd::os_error("error in pthread_setschedparam"); - } -#endif /* HAVE_PTHREAD_SETSCHEDPARAM */ - -/*********************************************************************** - * Windows API to set priority - **********************************************************************/ -#ifdef HAVE_WIN_SETTHREADPRIORITY - #include - - void uhd::set_thread_priority(float priority, UHD_UNUSED(bool realtime)){ - check_priority_range(priority); - - /* - * Process wide priority is no longer set. - * This is the responsibility of the application. - //set the priority class on the process - int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; - if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) - throw uhd::os_error("error in SetPriorityClass"); - */ - - //scale the priority value to the constants - int priorities[] = { - THREAD_PRIORITY_IDLE, THREAD_PRIORITY_LOWEST, THREAD_PRIORITY_BELOW_NORMAL, THREAD_PRIORITY_NORMAL, - THREAD_PRIORITY_ABOVE_NORMAL, THREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_TIME_CRITICAL - }; - size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 - - //set the thread priority on the thread - if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) - throw uhd::os_error("error in SetThreadPriority"); - } -#endif /* HAVE_WIN_SETTHREADPRIORITY */ - -/*********************************************************************** - * Unimplemented API to set priority - **********************************************************************/ -#ifdef HAVE_THREAD_PRIO_DUMMY - void uhd::set_thread_priority(float, bool){ - throw uhd::not_implemented_error("set thread priority not implemented"); - } - -#endif /* HAVE_THREAD_PRIO_DUMMY */ diff --git a/host/lib/utils/thread_priority_c.cpp b/host/lib/utils/thread_priority_c.cpp index b2de9970d..822126d4a 100644 --- a/host/lib/utils/thread_priority_c.cpp +++ b/host/lib/utils/thread_priority_c.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include #include #include -- cgit v1.2.3