2010-06-17 01:40:44 +00:00
|
|
|
//
|
2015-08-06 17:42:23 +00:00
|
|
|
// Copyright 2010-2011,2015 Ettus Research LLC
|
2020-02-07 18:35:39 +00:00
|
|
|
// Copyright 2018-2020 Ettus Research, a National Instruments Company
|
2010-06-17 01:40:44 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2010-06-17 01:40:44 +00:00
|
|
|
//
|
|
|
|
|
|
2011-02-25 00:35:29 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2020-02-07 18:35:39 +00:00
|
|
|
#include <uhd/utils/log.hpp>
|
|
|
|
|
#include <uhd/utils/thread.hpp>
|
|
|
|
|
#include <vector>
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
bool uhd::set_thread_priority_safe(float priority, bool realtime)
|
|
|
|
|
{
|
|
|
|
|
try {
|
2010-06-17 01:40:44 +00:00
|
|
|
set_thread_priority(priority, realtime);
|
|
|
|
|
return true;
|
2020-02-07 18:35:39 +00:00
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
UHD_LOGGER_WARNING("UHD")
|
|
|
|
|
<< "Unable to set the thread priority. Performance may be "
|
|
|
|
|
"negatively affected.\n"
|
|
|
|
|
"Please see the general application notes in the manual for "
|
|
|
|
|
"instructions.\n"
|
|
|
|
|
<< e.what();
|
2010-06-17 01:40:44 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
static void check_priority_range(float priority)
|
|
|
|
|
{
|
|
|
|
|
if (priority > +1.0 or priority < -1.0) {
|
2011-04-09 23:30:16 +00:00
|
|
|
throw uhd::value_error("priority out of range [-1.0, +1.0]");
|
2020-02-07 18:35:39 +00:00
|
|
|
}
|
2010-06-17 01:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Pthread API to set priority
|
|
|
|
|
**********************************************************************/
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_PTHREAD_SETSCHEDPARAM
|
2020-02-07 18:35:39 +00:00
|
|
|
# include <pthread.h>
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
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");
|
|
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_PTHREAD_SETSCHEDPARAM */
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Pthread API to set affinity
|
|
|
|
|
**********************************************************************/
|
2019-10-02 21:35:06 +00:00
|
|
|
#ifdef HAVE_PTHREAD_SETAFFINITY_NP
|
|
|
|
|
# include <pthread.h>
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_affinity(const std::vector<size_t>& cpu_affinity_list)
|
|
|
|
|
{
|
|
|
|
|
if (cpu_affinity_list.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
cpu_set_t cpu_set;
|
|
|
|
|
CPU_ZERO(&cpu_set);
|
|
|
|
|
for (auto cpu_num : cpu_affinity_list) {
|
|
|
|
|
if (cpu_num > CPU_SETSIZE) {
|
|
|
|
|
UHD_LOG_WARNING(
|
|
|
|
|
"UHD", "CPU index " << cpu_num << " in affinity list out of range");
|
2019-10-02 21:35:06 +00:00
|
|
|
}
|
2020-02-07 18:35:39 +00:00
|
|
|
CPU_SET(cpu_num, &cpu_set);
|
|
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
int status = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);
|
|
|
|
|
if (status != 0) {
|
|
|
|
|
UHD_LOG_WARNING("UHD", "Failed to set desired affinity for thread");
|
2019-10-02 21:35:06 +00:00
|
|
|
}
|
2020-02-07 18:35:39 +00:00
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
#endif /* HAVE_PTHREAD_SETAFFINITYNP */
|
|
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Windows API to set priority
|
|
|
|
|
**********************************************************************/
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_WIN_SETTHREADPRIORITY
|
2020-02-07 18:35:39 +00:00
|
|
|
# include <windows.h>
|
|
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_WIN_SETTHREADPRIORITY */
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Windows API to set affinity
|
|
|
|
|
**********************************************************************/
|
2019-10-02 21:35:06 +00:00
|
|
|
#ifdef HAVE_WIN_SETTHREADAFFINITYMASK
|
|
|
|
|
# include <windows.h>
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_affinity(const std::vector<size_t>& cpu_affinity_list)
|
|
|
|
|
{
|
|
|
|
|
if (cpu_affinity_list.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
DWORD_PTR cpu_set{0};
|
|
|
|
|
for (auto cpu_num : cpu_affinity_list) {
|
|
|
|
|
if (cpu_num > 8 * sizeof(DWORD_PTR)) {
|
|
|
|
|
UHD_LOG_WARNING(
|
|
|
|
|
"UHD", "CPU index " << cpu_num << " in affinity list out of range");
|
2019-10-02 21:35:06 +00:00
|
|
|
}
|
2020-02-07 18:35:39 +00:00
|
|
|
cpu_set |= ((DWORD_PTR)1 << cpu_num);
|
|
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
DWORD_PTR status = SetThreadAffinityMask(GetCurrentThread(), cpu_set);
|
|
|
|
|
if (status == 0) {
|
|
|
|
|
UHD_LOG_WARNING("UHD", "Failed to set desired affinity for thread");
|
2019-10-02 21:35:06 +00:00
|
|
|
}
|
2020-02-07 18:35:39 +00:00
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
#endif /* HAVE_WIN_SETTHREADAFFINITYMASK */
|
|
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Unimplemented API to set priority
|
|
|
|
|
**********************************************************************/
|
2011-12-31 07:27:33 +00:00
|
|
|
#ifdef HAVE_THREAD_PRIO_DUMMY
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_priority(float, bool)
|
|
|
|
|
{
|
|
|
|
|
UHD_LOG_DEBUG("UHD", "Setting thread priority is not implemented");
|
|
|
|
|
}
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2011-12-31 07:27:33 +00:00
|
|
|
#endif /* HAVE_THREAD_PRIO_DUMMY */
|
2017-04-18 23:46:44 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Unimplemented API to set affinity
|
|
|
|
|
**********************************************************************/
|
2019-10-02 21:35:06 +00:00
|
|
|
#ifdef HAVE_THREAD_SETAFFINITY_DUMMY
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_affinity(const std::vector<size_t>& cpu_affinity_list)
|
|
|
|
|
{
|
|
|
|
|
UHD_LOG_DEBUG("UHD", "Setting thread affinity is not implemented");
|
|
|
|
|
}
|
2019-10-02 21:35:06 +00:00
|
|
|
#endif /* HAVE_THREAD_SETAFFINITY_DUMMY */
|
|
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_name(boost::thread* thrd, const std::string& name)
|
|
|
|
|
{
|
2017-04-18 23:46:44 +00:00
|
|
|
#ifdef HAVE_PTHREAD_SETNAME
|
2020-02-07 18:35:39 +00:00
|
|
|
pthread_setname_np(thrd->native_handle(), name.substr(0, 16).c_str());
|
2017-04-18 23:46:44 +00:00
|
|
|
#endif /* HAVE_PTHREAD_SETNAME */
|
|
|
|
|
#ifdef HAVE_THREAD_SETNAME_DUMMY
|
2020-02-07 18:35:39 +00:00
|
|
|
// Then we can't set the thread name. This function may get called
|
|
|
|
|
// before the logger starts, and thus can't log any error messages.
|
|
|
|
|
// Note that CMake will also tell the user about not being able to set
|
|
|
|
|
// thread names.
|
2017-04-18 23:46:44 +00:00
|
|
|
#endif /* HAVE_THREAD_SETNAME_DUMMY */
|
2020-02-07 18:35:39 +00:00
|
|
|
}
|
2019-05-21 21:06:14 +00:00
|
|
|
|
2020-02-07 18:35:39 +00:00
|
|
|
void uhd::set_thread_name(std::thread* thrd, const std::string& name)
|
|
|
|
|
{
|
2019-05-21 21:06:14 +00:00
|
|
|
#ifdef HAVE_PTHREAD_SETNAME
|
2020-02-07 18:35:39 +00:00
|
|
|
pthread_setname_np(thrd->native_handle(), name.substr(0, 16).c_str());
|
2019-05-21 21:06:14 +00:00
|
|
|
#endif /* HAVE_PTHREAD_SETNAME */
|
|
|
|
|
#ifdef HAVE_THREAD_SETNAME_DUMMY
|
2020-02-07 18:35:39 +00:00
|
|
|
// Then we can't set the thread name. This function may get called
|
|
|
|
|
// before the logger starts, and thus can't log any error messages.
|
|
|
|
|
// Note that CMake will also tell the user about not being able to set
|
|
|
|
|
// thread names.
|
2019-05-21 21:06:14 +00:00
|
|
|
#endif /* HAVE_THREAD_SETNAME_DUMMY */
|
2020-02-07 18:35:39 +00:00
|
|
|
}
|