commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] r9607 - in gnuradio/trunk/gruel/src: include/gruel lib


From: eb
Subject: [Commit-gnuradio] r9607 - in gnuradio/trunk/gruel/src: include/gruel lib
Date: Fri, 19 Sep 2008 15:47:55 -0600 (MDT)

Author: eb
Date: 2008-09-19 15:47:55 -0600 (Fri, 19 Sep 2008)
New Revision: 9607

Added:
   gnuradio/trunk/gruel/src/include/gruel/sys_pri.h
   gnuradio/trunk/gruel/src/lib/sys_pri.cc
Modified:
   gnuradio/trunk/gruel/src/include/gruel/Makefile.am
   gnuradio/trunk/gruel/src/include/gruel/realtime.h
   gnuradio/trunk/gruel/src/lib/Makefile.am
   gnuradio/trunk/gruel/src/lib/realtime.cc
Log:
extend gruel:enable_realtime_scheduling interface to allow pri/policy

Modified: gnuradio/trunk/gruel/src/include/gruel/Makefile.am
===================================================================
--- gnuradio/trunk/gruel/src/include/gruel/Makefile.am  2008-09-19 01:44:34 UTC 
(rev 9606)
+++ gnuradio/trunk/gruel/src/include/gruel/Makefile.am  2008-09-19 21:47:55 UTC 
(rev 9607)
@@ -29,5 +29,6 @@
 gruelinclude_HEADERS = \
        $(BUILT_SOURCES) \
        realtime.h \
+       sys_pri.h \
        thread_body_wrapper.h \
        thread_group.h

Modified: gnuradio/trunk/gruel/src/include/gruel/realtime.h
===================================================================
--- gnuradio/trunk/gruel/src/include/gruel/realtime.h   2008-09-19 01:44:34 UTC 
(rev 9606)
+++ gnuradio/trunk/gruel/src/include/gruel/realtime.h   2008-09-19 21:47:55 UTC 
(rev 9607)
@@ -1,6 +1,6 @@
 /* -*- c++ -*- */
 /*
- * Copyright 2006 Free Software Foundation, Inc.
+ * Copyright 2006,2008 Free Software Foundation, Inc.
  * 
  * This file is part of GNU Radio
  * 
@@ -20,9 +20,17 @@
  * Boston, MA 02110-1301, USA.
  */
 
-#ifndef INCLUDED_REALTIME_H
-#define INCLUDED_REALTIME_H
+#ifndef INCLUDED_GRUEL_REALTIME_H
+#define INCLUDED_GRUEL_REALTIME_H
 
+#include <stdexcept>
+
+/*!
+ * \brief System independent way to ask for realtime scheduling
+ *
+ * \sa sys_pri.h
+ */
+
 namespace gruel {
 
   typedef enum {
@@ -32,13 +40,53 @@
     RT_OTHER_ERROR
   } rt_status_t;
 
+
+  enum rt_sched_policy {
+    RT_SCHED_RR   = 0,         // round robin
+    RT_SCHED_FIFO = 1,         // first in first out
+  };
+
+  /*
+   * Define the range for our virtual priorities (don't change these)
+   *
+   * Processes (or threads) with numerically higher priority values
+   * are scheduled before processes with numerically lower priority
+   * values.  Thus, the value returned by rt_priority_max() will be
+   * greater than the value returned by rt_priority_min().
+   */
+  static inline int rt_priority_min() { return  0; }
+  static inline int rt_priority_max() { return 15; }
+  static inline int rt_priority_default() { return 1; }
+
+  struct rt_sched_param {
+    int                        priority;
+    rt_sched_policy    policy;
+
+    rt_sched_param()
+      : priority(rt_priority_default()), policy(RT_SCHED_RR){}
+
+    rt_sched_param(int priority_, rt_sched_policy policy_ = RT_SCHED_RR)
+    {
+      if (priority_ < rt_priority_min() || priority_ > rt_priority_max())
+       throw std::invalid_argument("rt_sched_param: priority out of range");
+
+      priority = priority_;
+      policy = policy_;
+    }
+  };
+
   /*!
-   * \brief If possible, enable high-priority "real time" scheduling.
+   * \brief If possible, enable "realtime" scheduling.
    * \ingroup misc
+   *
+   * In general, this means that the code will be scheduled before any
+   * non-realtime (normal) processes.  Note that if your code contains
+   * an non-blocking infinite loop and you enable realtime scheduling,
+   * it's possible to hang the system.
    */
   rt_status_t
-  enable_realtime_scheduling();
+  enable_realtime_scheduling(rt_sched_param = rt_sched_param());
 
 } // namespace gruel
 
-#endif /* INCLUDED_GR_REALTIME_H */
+#endif /* INCLUDED_GRUEL_REALTIME_H */

Added: gnuradio/trunk/gruel/src/include/gruel/sys_pri.h
===================================================================
--- gnuradio/trunk/gruel/src/include/gruel/sys_pri.h                            
(rev 0)
+++ gnuradio/trunk/gruel/src/include/gruel/sys_pri.h    2008-09-19 21:47:55 UTC 
(rev 9607)
@@ -0,0 +1,41 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef INCLUDED_GRUEL_SYS_PRI_H
+#define INCLUDED_GRUEL_SYS_PRI_H
+
+#include <gruel/realtime.h>
+
+/*
+ * A single place to define real-time priorities used by the system itself
+ */
+namespace gruel {
+
+  struct sys_pri {
+    static rt_sched_param python();              // python code
+    static rt_sched_param normal();              // normal blocks
+    static rt_sched_param gcell_event_handler();
+    static rt_sched_param usrp2_backend();       // thread that services the 
ethernet
+  };
+
+}
+
+#endif /* INCLUDED_GRUEL_SYS_PRI_H */


Property changes on: gnuradio/trunk/gruel/src/include/gruel/sys_pri.h
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: gnuradio/trunk/gruel/src/lib/Makefile.am
===================================================================
--- gnuradio/trunk/gruel/src/lib/Makefile.am    2008-09-19 01:44:34 UTC (rev 
9606)
+++ gnuradio/trunk/gruel/src/lib/Makefile.am    2008-09-19 21:47:55 UTC (rev 
9607)
@@ -31,11 +31,11 @@
 # These are the source files that go into the gruel shared library
 libgruel_la_SOURCES = \
        realtime.cc \
+       sys_pri.cc \
        thread_body_wrapper.cc \
        thread_group.cc
 
 libgruel_la_LIBADD = \
        $(BOOST_THREAD_LIB)
-       -lstdc++                        
 
 noinst_HEADERS =

Modified: gnuradio/trunk/gruel/src/lib/realtime.cc
===================================================================
--- gnuradio/trunk/gruel/src/lib/realtime.cc    2008-09-19 01:44:34 UTC (rev 
9606)
+++ gnuradio/trunk/gruel/src/lib/realtime.cc    2008-09-19 21:47:55 UTC (rev 
9607)
@@ -30,94 +30,107 @@
 #include <sched.h>
 #endif
 
+#include <algorithm>
+#include <math.h>
 #include <string.h>
 #include <errno.h>
 #include <stdio.h>
 
-#if defined(HAVE_SCHED_SETSCHEDULER)
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
+#include <pthread.h>
 
 namespace gruel {
 
+  /*!
+   * 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 gruel
+
+#endif
+
+
+#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
+
+namespace gruel {
+
   rt_status_t
-  enable_realtime_scheduling()
+  enable_realtime_scheduling(rt_sched_param p)
   {
-    int policy = SCHED_FIFO;
-    int pri = (sched_get_priority_max (policy) + sched_get_priority_min 
(policy)) / 2;
-    int pid = 0;  // this process
+    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_min(policy);
+    int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
 
-    if (0){
-      fprintf(stderr, "sched_setscheduler version\n");
-      fprintf(stderr, "pri_min(SCHED_FIFO) = %d\n", 
sched_get_priority_min(SCHED_FIFO));
-      fprintf(stderr, "pri_max(SCHED_FIFO) = %d\n", 
sched_get_priority_max(SCHED_FIFO));
-      fprintf(stderr, "pri = %d\n", pri);
-    }
-
+    pthread_t this_thread = pthread_self ();  // this process
     struct sched_param param;
-    memset(&param, 0, sizeof(param));
+    memset (&param, 0, sizeof (param));
     param.sched_priority = pri;
-    int result = sched_setscheduler(pid, policy, &param);
-    if (result != 0){
+    int result = pthread_setschedparam (this_thread, policy, &param);
+    if (result != 0) {
       if (errno == EPERM)
         return RT_NO_PRIVS;
       else {
-        perror ("sched_setscheduler: failed to set real time priority");
+        perror ("pthread_setschedparam: failed to set real time priority");
         return RT_OTHER_ERROR;
       }
     }
-    
+  
     //printf("SCHED_FIFO enabled with priority = %d\n", pri);
     return RT_OK;
   }
-
 } // namespace gruel
 
-#elif defined(HAVE_PTHREAD_SETSCHEDPARAM)
 
-#include <pthread.h>
-#include <stdio.h>
+#elif defined(HAVE_SCHED_SETSCHEDULER)
 
 namespace gruel {
 
   rt_status_t
-  enable_realtime_scheduling()
+  enable_realtime_scheduling(rt_sched_param p)
   {
-    int policy = SCHED_FIFO;
-    int pri = (sched_get_priority_max (policy) + sched_get_priority_min 
(policy)) / 2;
+    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_min(policy);
+    int pri = rescale_virtual_pri(p.priority, min_real_pri, max_real_pri);
 
-    if (0){
-      fprintf(stderr, "pthread_setschedparam version\n");
-      fprintf(stderr, "pri_min(SCHED_FIFO) = %d\n", 
sched_get_priority_min(SCHED_FIFO));
-      fprintf(stderr, "pri_max(SCHED_FIFO) = %d\n", 
sched_get_priority_max(SCHED_FIFO));
-      fprintf(stderr, "pri = %d\n", pri);
-    }
-
-    pthread_t this_thread = pthread_self ();  // this process
+    int pid = 0;  // this process
     struct sched_param param;
-    memset (&param, 0, sizeof (param));
+    memset(&param, 0, sizeof(param));
     param.sched_priority = pri;
-    int result = pthread_setschedparam (this_thread, policy, &param);
-    if (result != 0) {
+    int result = sched_setscheduler(pid, policy, &param);
+    if (result != 0){
       if (errno == EPERM)
         return RT_NO_PRIVS;
       else {
-        perror ("pthread_setschedparam: failed to set real time priority");
+        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;
   }
+
 } // namespace gruel
 
-// #elif // could try negative niceness
-
 #else
 
 namespace gruel {
 
   rt_status_t
-  enable_realtime_scheduling()
+  enable_realtime_scheduling(rt_sched_param p)
   {
     return RT_NOT_IMPLEMENTED;
   }

Added: gnuradio/trunk/gruel/src/lib/sys_pri.cc
===================================================================
--- gnuradio/trunk/gruel/src/lib/sys_pri.cc                             (rev 0)
+++ gnuradio/trunk/gruel/src/lib/sys_pri.cc     2008-09-19 21:47:55 UTC (rev 
9607)
@@ -0,0 +1,61 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2008 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio 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, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio 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, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <gruel/sys_pri.h>
+
+namespace gruel {
+
+  /*
+   * These may need per-OS tweaking.
+   *
+   * Under linux virtual_pri ->        system_pri
+   *   0 ->   0
+   *   1 ->   5
+   *   2 ->  10
+   *   3 ->  15
+   *   4 ->  20                // typically used by jack and pulse audio
+   *   5 ->  25
+   *   6 ->  30
+   *   7 ->  35
+   *   8 ->  40
+   *   9 ->  45
+   *  10 ->  50
+   *  11 ->  54
+   *  12 ->  59
+   *  13 ->  64
+   *  14 ->  69
+   *  15 ->  74
+   */
+  rt_sched_param
+  sys_pri::python()            { return rt_sched_param(0, RT_SCHED_RR); }
+
+  rt_sched_param
+  sys_pri::normal()            { return rt_sched_param(2, RT_SCHED_RR); }
+
+  rt_sched_param
+  sys_pri::gcell_event_handler(){ return rt_sched_param(5, RT_SCHED_FIFO); }
+
+  rt_sched_param
+  sys_pri::usrp2_backend()     { return rt_sched_param(6, RT_SCHED_FIFO); }
+}


Property changes on: gnuradio/trunk/gruel/src/lib/sys_pri.cc
___________________________________________________________________
Name: svn:eol-style
   + native





reply via email to

[Prev in Thread] Current Thread [Next in Thread]