2010-06-17 01:40:44 +00:00
|
|
|
//
|
2011-02-25 00:35:29 +00:00
|
|
|
// Copyright 2010-2011 Ettus Research LLC
|
2010-06-17 01:40:44 +00:00
|
|
|
//
|
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <uhd/utils/thread_priority.hpp>
|
2011-05-05 01:36:10 +00:00
|
|
|
#include <uhd/utils/msg.hpp>
|
2011-02-25 00:35:29 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2010-10-08 23:04:22 +00:00
|
|
|
#include <boost/format.hpp>
|
2010-06-17 01:40:44 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
bool uhd::set_thread_priority_safe(float priority, bool realtime){
|
|
|
|
|
try{
|
|
|
|
|
set_thread_priority(priority, realtime);
|
|
|
|
|
return true;
|
|
|
|
|
}catch(const std::exception &e){
|
2011-05-05 01:36:10 +00:00
|
|
|
UHD_MSG(warning) << boost::format(
|
2011-04-09 23:30:16 +00:00
|
|
|
"Unable to set the thread priority. Performance may be negatively affected.\n"
|
|
|
|
|
"Please see the general application notes in the manual for instructions.\n"
|
2010-10-08 23:04:22 +00:00
|
|
|
"%s\n"
|
2011-05-05 01:36:10 +00:00
|
|
|
) % e.what();
|
2010-06-17 01:40:44 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]");
|
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
|
2010-06-17 01:40:44 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2010-06-17 18:32:46 +00:00
|
|
|
//we cannot have below normal priority, set to zero
|
|
|
|
|
if (priority < 0) priority = 0;
|
2010-06-17 01:40:44 +00:00
|
|
|
|
|
|
|
|
//get the priority bounds for the selected policy
|
|
|
|
|
int min_pri = sched_get_priority_min(policy);
|
|
|
|
|
int max_pri = sched_get_priority_max(policy);
|
2011-02-25 00:35:29 +00:00
|
|
|
if (min_pri == -1 or max_pri == -1) throw uhd::os_error("error in sched_get_priority_min/max");
|
2010-06-17 01:40:44 +00:00
|
|
|
|
|
|
|
|
//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);
|
2011-02-25 00:35:29 +00:00
|
|
|
if (ret != 0) throw uhd::os_error("error in pthread_setschedparam");
|
2010-06-17 01:40:44 +00:00
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_PTHREAD_SETSCHEDPARAM */
|
2010-06-17 01:40:44 +00:00
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Windows API to set priority
|
|
|
|
|
**********************************************************************/
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_WIN_SETTHREADPRIORITY
|
2010-06-17 01:40:44 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
|
2015-03-27 17:55:48 +00:00
|
|
|
void uhd::set_thread_priority(float priority, UHD_UNUSED(bool realtime)){
|
2010-06-17 18:32:46 +00:00
|
|
|
check_priority_range(priority);
|
|
|
|
|
|
2012-02-08 22:24:17 +00:00
|
|
|
/*
|
|
|
|
|
* Process wide priority is no longer set.
|
|
|
|
|
* This is the responsibility of the application.
|
2010-06-17 01:40:44 +00:00
|
|
|
//set the priority class on the process
|
|
|
|
|
int pri_class = (realtime)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS;
|
|
|
|
|
if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0)
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::os_error("error in SetPriorityClass");
|
2012-02-08 22:24:17 +00:00
|
|
|
*/
|
2010-06-17 01:40:44 +00:00
|
|
|
|
2010-06-17 02:18:06 +00:00
|
|
|
//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
|
|
|
|
|
|
2010-06-17 01:40:44 +00:00
|
|
|
//set the thread priority on the thread
|
2010-06-17 02:18:06 +00:00
|
|
|
if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0)
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::os_error("error in SetThreadPriority");
|
2010-06-17 01:40:44 +00:00
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_WIN_SETTHREADPRIORITY */
|
2010-06-17 01:40:44 +00:00
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Unimplemented API to set priority
|
|
|
|
|
**********************************************************************/
|
2011-12-31 07:27:33 +00:00
|
|
|
#ifdef HAVE_THREAD_PRIO_DUMMY
|
2010-06-17 01:40:44 +00:00
|
|
|
void uhd::set_thread_priority(float, bool){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::not_implemented_error("set thread priority not implemented");
|
2010-06-17 01:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-31 07:27:33 +00:00
|
|
|
#endif /* HAVE_THREAD_PRIO_DUMMY */
|