diff options
author | Tom Rondeau <trondeau@vt.edu> | 2013-05-25 15:51:34 -0400 |
---|---|---|
committer | Tom Rondeau <trondeau@vt.edu> | 2013-05-25 15:51:34 -0400 |
commit | 855f617bb2b172402fce6c8f3d77ca8b6d749b7f (patch) | |
tree | 28ea41c3d442d3aa4dc8425612390efff9b6615c /gnuradio-runtime | |
parent | b573271f7740d16ea2f05b8c998d83c1783ece1e (diff) |
runtime: fix use of gr::realtime.
Diffstat (limited to 'gnuradio-runtime')
-rw-r--r-- | gnuradio-runtime/include/gnuradio/realtime.h | 2 | ||||
-rw-r--r-- | gnuradio-runtime/include/gnuradio/realtime_impl.h | 25 | ||||
-rw-r--r-- | gnuradio-runtime/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gnuradio-runtime/lib/realtime.cc | 2 | ||||
-rw-r--r-- | gnuradio-runtime/lib/realtime_impl.cc | 204 | ||||
-rw-r--r-- | gnuradio-runtime/swig/realtime.i | 19 |
6 files changed, 131 insertions, 122 deletions
diff --git a/gnuradio-runtime/include/gnuradio/realtime.h b/gnuradio-runtime/include/gnuradio/realtime.h index f3910f8c7d..b07caf6b21 100644 --- a/gnuradio-runtime/include/gnuradio/realtime.h +++ b/gnuradio-runtime/include/gnuradio/realtime.h @@ -28,8 +28,6 @@ namespace gr { - typedef impl::rt_status_t rt_status_t; - /*! * \brief If possible, enable high-priority "real time" scheduling. * \ingroup misc diff --git a/gnuradio-runtime/include/gnuradio/realtime_impl.h b/gnuradio-runtime/include/gnuradio/realtime_impl.h index 264fae7b27..bc2250257b 100644 --- a/gnuradio-runtime/include/gnuradio/realtime_impl.h +++ b/gnuradio-runtime/include/gnuradio/realtime_impl.h @@ -32,20 +32,20 @@ * \sa sys_pri.h */ namespace gr { - namespace impl { - typedef enum { - RT_OK = 0, - RT_NOT_IMPLEMENTED, - RT_NO_PRIVS, - RT_OTHER_ERROR - } rt_status_t; + typedef enum { + RT_OK = 0, + RT_NOT_IMPLEMENTED, + RT_NO_PRIVS, + RT_OTHER_ERROR + } rt_status_t; - enum rt_sched_policy { - RT_SCHED_RR = 0, // round robin - RT_SCHED_FIFO = 1, // first in first out - }; + enum rt_sched_policy { + RT_SCHED_RR = 0, // round robin + RT_SCHED_FIFO = 1, // first in first out + }; + namespace impl { /* * Define the range for our virtual priorities (don't change * these) @@ -88,8 +88,7 @@ namespace gr { // NOTE: If you change this, you need to change the code in // gnuradio-runtime/swig/realtime.i, see note there. - rt_status_t - GR_RUNTIME_API enable_realtime_scheduling(rt_sched_param = rt_sched_param()); + GR_RUNTIME_API rt_status_t enable_realtime_scheduling(rt_sched_param = rt_sched_param()); } /* namespace impl */ } /* namespace gr */ diff --git a/gnuradio-runtime/lib/CMakeLists.txt b/gnuradio-runtime/lib/CMakeLists.txt index a23202b5c0..29222ae91f 100644 --- a/gnuradio-runtime/lib/CMakeLists.txt +++ b/gnuradio-runtime/lib/CMakeLists.txt @@ -95,6 +95,7 @@ list(APPEND gnuradio_runtime_sources pagesize.cc prefs.cc realtime.cc + realtime_impl.cc scheduler.cc scheduler_sts.cc scheduler_tpb.cc diff --git a/gnuradio-runtime/lib/realtime.cc b/gnuradio-runtime/lib/realtime.cc index e3fea1c6a6..99deff5b52 100644 --- a/gnuradio-runtime/lib/realtime.cc +++ b/gnuradio-runtime/lib/realtime.cc @@ -31,7 +31,7 @@ namespace gr { rt_status_t enable_realtime_scheduling() { - return gr::enable_realtime_scheduling(); + return gr::impl::enable_realtime_scheduling(); } } /* namespace gr */ diff --git a/gnuradio-runtime/lib/realtime_impl.cc b/gnuradio-runtime/lib/realtime_impl.cc index 5dadc26bbd..54db9d8d70 100644 --- a/gnuradio-runtime/lib/realtime_impl.cc +++ b/gnuradio-runtime/lib/realtime_impl.cc @@ -24,7 +24,7 @@ #include <config.h> #endif -#include <realtime.h> +#include <gnuradio/realtime_impl.h> #ifdef HAVE_SCHED_H #include <sched.h> @@ -36,26 +36,30 @@ #include <errno.h> #include <stdio.h> +#include <iostream> + #if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER) #include <pthread.h> namespace gr { + namespace impl { + + /*! + * Rescale our virtual priority so that it maps to the middle 1/2 of + * the priorities given by min_real_pri and max_real_pri. + */ + static int + rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri) + { + float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri)); + float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri)); + float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min()); + float y = m * (virtual_pri - rt_priority_min()) + rmin; + int y_int = static_cast<int>(rint(y)); + return std::max(min_real_pri, std::min(max_real_pri, y_int)); + } - /*! - * Rescale our virtual priority so that it maps to the middle 1/2 of - * the priorities given by min_real_pri and max_real_pri. - */ - static int - rescale_virtual_pri(int virtual_pri, int min_real_pri, int max_real_pri) - { - float rmin = min_real_pri + (0.25 * (max_real_pri - min_real_pri)); - float rmax = min_real_pri + (0.75 * (max_real_pri - min_real_pri)); - float m = (rmax - rmin) / (rt_priority_max() - rt_priority_min()); - float y = m * (virtual_pri - rt_priority_min()) + rmin; - int y_int = static_cast<int>(rint(y)); - return std::max(min_real_pri, std::min(max_real_pri, y_int)); - } - + } // namespace impl } // namespace gr #endif @@ -64,72 +68,77 @@ namespace gr { #if defined(HAVE_PTHREAD_SETSCHEDPARAM) namespace gr { - - rt_status_t - enable_realtime_scheduling(rt_sched_param p) - { - int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; - int min_real_pri = sched_get_priority_min(policy); - int max_real_pri = sched_get_priority_max(policy); - int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); - - // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. - // fprintf(stderr, "pthread_setschedparam: policy = %d, pri = %d\n", policy, pri); - - struct sched_param param; - memset (¶m, 0, sizeof (param)); - param.sched_priority = pri; - int result = pthread_setschedparam (pthread_self(), policy, ¶m); - if (result != 0) { - if (result == EPERM) // N.B., return value, not errno - return RT_NO_PRIVS; - else { - fprintf(stderr, - "pthread_setschedparam: failed to set real time priority: %s\n", - strerror(result)); - return RT_OTHER_ERROR; + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; + int min_real_pri = sched_get_priority_min(policy); + int max_real_pri = sched_get_priority_max(policy); + int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); + + // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. + // fprintf(stderr, "pthread_setschedparam: policy = %d, pri = %d\n", policy, pri); + + struct sched_param param; + memset (¶m, 0, sizeof (param)); + param.sched_priority = pri; + int result = pthread_setschedparam (pthread_self(), policy, ¶m); + if(result != 0) { + if(result == EPERM) // N.B., return value, not errno + return RT_NO_PRIVS; + else { + fprintf(stderr, + "pthread_setschedparam: failed to set real time priority: %s\n", + strerror(result)); + return RT_OTHER_ERROR; + } } + + //printf("SCHED_FIFO enabled with priority = %d\n", pri); + return RT_OK; } - //printf("SCHED_FIFO enabled with priority = %d\n", pri); - return RT_OK; - } + } // namespace impl } // namespace gr #elif defined(HAVE_SCHED_SETSCHEDULER) namespace gr { - - rt_status_t - enable_realtime_scheduling(rt_sched_param p) - { - int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; - int min_real_pri = sched_get_priority_min(policy); - int max_real_pri = sched_get_priority_max(policy); - int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); - - // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. - // fprintf(stderr, "sched_setscheduler: policy = %d, pri = %d\n", policy, pri); - - int pid = 0; // this process - struct sched_param param; - memset(¶m, 0, sizeof(param)); - param.sched_priority = pri; - int result = sched_setscheduler(pid, policy, ¶m); - if (result != 0){ - if (errno == EPERM) - return RT_NO_PRIVS; - else { - perror ("sched_setscheduler: failed to set real time priority"); - return RT_OTHER_ERROR; + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + int policy = p.policy == RT_SCHED_FIFO ? SCHED_FIFO : SCHED_RR; + int min_real_pri = sched_get_priority_min(policy); + int max_real_pri = sched_get_priority_max(policy); + int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri); + + // FIXME check hard and soft limits with getrlimit, and limit the value we ask for. + // fprintf(stderr, "sched_setscheduler: policy = %d, pri = %d\n", policy, pri); + + int pid = 0; // this process + struct sched_param param; + memset(¶m, 0, sizeof(param)); + param.sched_priority = pri; + int result = sched_setscheduler(pid, policy, ¶m); + if(result != 0){ + if(errno == EPERM) + return RT_NO_PRIVS; + else { + perror("sched_setscheduler: failed to set real time priority"); + return RT_OTHER_ERROR; + } } - } - //printf("SCHED_FIFO enabled with priority = %d\n", pri); - return RT_OK; - } + //printf("SCHED_FIFO enabled with priority = %d\n", pri); + return RT_OK; + } + } // namespace impl } // namespace gr #elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) @@ -137,42 +146,47 @@ namespace gr { #include <windows.h> namespace gr { + namespace impl { - rt_status_t enable_realtime_scheduling(rt_sched_param p){ - - //set the priority class on the process - int pri_class = (true)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; - if (SetPriorityClass(GetCurrentProcess(), pri_class) == 0) - return RT_OTHER_ERROR; - - //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 - }; - const double priority = double(p.priority)/(rt_priority_max() - rt_priority_min()); - size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 - pri_index %= sizeof(priorities)/sizeof(*priorities); //range check + rt_status_t enable_realtime_scheduling(rt_sched_param p) + { + //set the priority class on the process + int pri_class = (true)? REALTIME_PRIORITY_CLASS : NORMAL_PRIORITY_CLASS; + if(SetPriorityClass(GetCurrentProcess(), pri_class) == 0) + return RT_OTHER_ERROR; - //set the thread priority on the thread - if (SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) - return RT_OTHER_ERROR; + //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 + }; + const double priority = double(p.priority)/(rt_priority_max() - rt_priority_min()); + size_t pri_index = size_t((priority+1.0)*6/2.0); // -1 -> 0, +1 -> 6 + pri_index %= sizeof(priorities)/sizeof(*priorities); //range check + + //set the thread priority on the thread + if(SetThreadPriority(GetCurrentThread(), priorities[pri_index]) == 0) + return RT_OTHER_ERROR; - //printf("SetPriorityClass + SetThreadPriority\n"); - return RT_OK; + //printf("SetPriorityClass + SetThreadPriority\n"); + return RT_OK; } + } // namespace impl } // namespace gr #else namespace gr { + namespace impl { + + rt_status_t + enable_realtime_scheduling(rt_sched_param p) + { + return RT_NOT_IMPLEMENTED; + } - rt_status_t - enable_realtime_scheduling(rt_sched_param p) - { - return RT_NOT_IMPLEMENTED; - } + } // namespace impl } // namespace gr #endif diff --git a/gnuradio-runtime/swig/realtime.i b/gnuradio-runtime/swig/realtime.i index d408249a19..82fc10e74c 100644 --- a/gnuradio-runtime/swig/realtime.i +++ b/gnuradio-runtime/swig/realtime.i @@ -30,16 +30,13 @@ // pkg-config strips -I/usr/include from the --cflags path. namespace gr { - namespace impl { - typedef enum { - RT_OK = 0, - RT_NOT_IMPLEMENTED, - RT_NO_PRIVS, - RT_OTHER_ERROR - } rt_status_t; - } -} + typedef enum { + RT_OK = 0, + RT_NOT_IMPLEMENTED, + RT_NO_PRIVS, + RT_OTHER_ERROR + } rt_status_t; -typedef gr::rt_status_t rt_status_t; -rt_status_t gr::enable_realtime_scheduling(); + gr::rt_status_t gr::enable_realtime_scheduling(); +} |