From 10673e56ee3f2b7206c0949480085920cf1d721c Mon Sep 17 00:00:00 2001 From: ppisa Date: Wed, 16 Aug 2006 12:50:45 +0000 Subject: [PATCH] Program canping extended to provide option for selection of real-time priorities. --- src/canping.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/canping.c b/src/canping.c index 10c605e..eb829b3 100644 --- a/src/canping.c +++ b/src/canping.c @@ -20,6 +20,16 @@ //#define DEBUG 1 //#define DEBUG 2 +#define WITH_RTPRIO + +#ifdef WITH_RTPRIO +#include + +int sched_policy = SCHED_OTHER; +int sched_rtprio; + +#endif + #ifndef DEBUG #define dbg(level, fmt, arg...) do {} while (0) @@ -445,6 +455,28 @@ void start_slaves(int slaves, int first_id) } } +#ifdef WITH_RTPRIO +int set_sched_policy_and_prio(int policy, int rtprio) +{ + struct sched_param scheduling_parameters; + int maxprio=sched_get_priority_max(policy); + int minprio=sched_get_priority_min(policy); + + if((rtprio < minprio) || (rtprio > maxprio)) { + fprintf(stderr, "The priority for requested policy is out of <%d, %d> range\n", + minprio, maxprio); + return -1; + } + + scheduling_parameters.sched_priority = rtprio; + + if (0 != pthread_setschedparam(pthread_self(), policy, &scheduling_parameters)) { + perror("pthread_setschedparam error"); + } + return 0; +} +#endif + void print_help(void) { printf("Usage: canping -m [other options]\n" @@ -461,6 +493,10 @@ void print_help(void) " -w ms wait ms miliseconds between sending pings\n" " -y synchronize threads before start (doesn't test race conditions\n" " between open and read/write)\n" +#ifdef WITH_RTPRIO + " -r run at realtime priority\n" + " -R P:prio run at realtime priority prio, policy RR or FF\n" +#endif "\n" "Example: canping -m 10 -vv\n" ); @@ -471,7 +507,7 @@ int parse_options(int argc, char *argv[]) int c; opterr = 0; - while ((c = getopt (argc, argv, "c:d:hi:l:m:os:t:vw:y")) != -1) + while ((c = getopt (argc, argv, "c:d:hi:l:m:os:t:vw:yrR:")) != -1) switch (c) { case 'c': @@ -513,6 +549,28 @@ int parse_options(int argc, char *argv[]) case 'y': option_synch_start = 1; break; +#ifdef WITH_RTPRIO + case 'r': + sched_policy = SCHED_FIFO; + sched_rtprio = (sched_get_priority_min(SCHED_FIFO) + + sched_get_priority_max(SCHED_FIFO)) / 2; + break; + case 'R': + if(!isalpha(*optarg)) { + sched_policy = SCHED_FIFO; + } else if(!strncmp(optarg,"FF:",3)) { + sched_policy = SCHED_FIFO; + optarg += 3; + } else if(!strncmp(optarg,"RR:",3)) { + sched_policy = SCHED_RR; + optarg += 3; + } else { + fprintf (stderr, "Unknown policy %s\n", optarg); + exit(EXIT_BAD_PARAM); + } + sched_rtprio = atoi(optarg); + break; +#endif case '?': if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); @@ -606,6 +664,13 @@ int main(int argc, char *argv[]) { parse_options(argc, argv); +#ifdef WITH_RTPRIO + if(sched_policy != SCHED_OTHER) + if(set_sched_policy_and_prio(sched_policy, sched_rtprio) <0) + exit(EXIT_BAD_PARAM); +#endif + + thread_list_init_head(&master_threads); thread_list_init_head(&slave_threads); -- 2.39.2