]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
Task runs with RT priority, added run script, common code added to common.c.
authorMichal Sojka <sojkam1@fel.cvut.cz>
Sat, 1 Dec 2007 11:46:01 +0000 (12:46 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Sat, 1 Dec 2007 11:46:01 +0000 (12:46 +0100)
wme_test/Makefile
wme_test/common.c [new file with mode: 0644]
wme_test/common.h [new file with mode: 0644]
wme_test/run [new file with mode: 0755]
wme_test/wclient.c
wme_test/wserver.c

index d2ed3d8b69f1e9b92b4b15a6237b010cd167f2d8..f00d9ed255bdbe8ea7c3c02335ef09420e2d39e1 100644 (file)
@@ -3,7 +3,14 @@ PROGS = wserver wclient
 all: $(PROGS) 
 .PHONY:all
 
-CFLAGS = -Wall -D_REENTRANT -lpthread  -lrt -g
+LDFLAGS = -lpthread  -lrt 
+CFLAGS = -Wall -D_REENTRANT -g
+ifdef DEBUG
+CFLAGS += -DDEBUG=1
+endif
+
+wserver: wserver.o common.o
+wclient: wclient.o common.o
 
 clean:
        rm -rf ./*.o
diff --git a/wme_test/common.c b/wme_test/common.c
new file mode 100644 (file)
index 0000000..dc2a539
--- /dev/null
@@ -0,0 +1,54 @@
+#include "common.h"
+#include <sched.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
+const unsigned int ac_to_tos[4] = {224,160,96,64};
+
+void set_rt_prio(int priority)
+{
+       int maxpri, minpri;
+       static struct sched_param param;
+
+       if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1)        {
+               perror("sched_get_priority_max fails");
+               exit(8);
+       }
+
+       if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1)        {
+               perror("sched_get_priority_min fails");
+               exit(10);
+       }
+
+       if (priority > maxpri)  {
+               fprintf(stderr,"maximum priority allowed is %d.\n", maxpri);
+               exit(9);
+       }
+       if (priority < minpri)  {
+               fprintf(stderr,"minimum priority allowed is %d.\n", minpri);
+               exit(11);
+       }
+
+       param.sched_priority = priority;
+
+       if (sched_setscheduler(0, SCHED_FIFO, &param) == -1)    {
+               perror("sched_setscheduler fails");
+               exit(13);
+       }
+}
+
+void block_signals(void)
+{
+       sigset_t sigset;
+       int ret;
+       sigemptyset(&sigset);
+       sigaddset(&sigset, SIGINT);
+       sigaddset(&sigset, SIGTERM);
+       ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+       if (ret != 0) {
+               perror("pthread_sigmask failed");
+               exit(1);
+       }
+}
diff --git a/wme_test/common.h b/wme_test/common.h
new file mode 100644 (file)
index 0000000..0f960b4
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef COMMON_H
+#define COMMON_H
+
+#define BASE_PORT       5100
+#define AC_QUEUES       4
+#define MTU             800 
+enum {
+       AC_VO = 0,
+       AC_VI = 1,
+       AC_BE = 2,
+       AC_BK = 3
+};
+
+extern const int prio_to_ac[8];
+extern const unsigned int ac_to_tos[4];
+
+void block_signals(void);
+void set_rt_prio(int priority);
+
+
+#endif
diff --git a/wme_test/run b/wme_test/run
new file mode 100755 (executable)
index 0000000..dc247eb
--- /dev/null
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+FILE=delay_stats
+
+./wclient $1
+
+cat <<EOF | gnuplot -persist
+set xlabel "Delay [ms]"
+set ylabel "Packet percentage"
+set key right top nobox
+set style data linespoints 
+set grid
+plot [0:*] [0:*] "${FILE}.dat" index 0 title "AC_VO",\
+                "${FILE}.dat" index 1 title "AC_VI",\
+                "${FILE}.dat" index 2 title "AC_BE",\
+                "${FILE}.dat" index 3 title "AC_BK"
+set term postscript color eps size 12cm,9cm
+set output '${FILE}.eps'
+replot
+EOF
index d56873a5854dcc66ada404fb3418b24222a34da4..290db3bc27acc59fe2d050247b56b743e5b65b8d 100644 (file)
@@ -15,6 +15,8 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include "common.h"
+#include <semaphore.h>
 
 /*#define AC_VO 0
 #define AC_VI 1
 */
 
 #define PARAM_SERVERADDR 1
-#define BASE_PORT       5100
-#define AC_QUEUES       4
 #define MAX_SENDENDPOINTS  10
-#define MTU             800 
 
-// Turn on/off debugging
-#define DEBUG 0
-
-enum {  AC_VO = 0,
-       AC_VI = 1,
-       AC_BE = 2,
-       AC_BK = 3
-};
+int ac_sockfd[AC_QUEUES];
 
-const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
-const unsigned int ac_to_tos[4] = {224,160,96,64};
+struct receiver {
+       pthread_t thread;
+} receivers[AC_QUEUES];
 
-int ac_sockfd[AC_QUEUES];
 FILE* logfd;
 char* server_addr; 
 
@@ -96,13 +88,28 @@ struct send_endpoint sepoint[] = {
 
 unsigned int nr_sepoints = sizeof(sepoint)/sizeof(*sepoint);
 
+sem_t sem_thread_finished;
+
+bool exit_flag = false;
+
 void stopper()
+{
+       int i;
+       exit_flag = true;
+
+       /* Interrupt all receivers */
+       for (i=0; i<AC_QUEUES; i++) {
+               pthread_kill(receivers[i].thread, SIGUSR1);
+       }
+}
+
+void save_results()
 {
        int ac, i, maxi;
        bool allzeros;
        unsigned sum[AC_QUEUES];
 
-       printf("\n Writing data to log file...\n");
+       printf("\nWriting data to log file...\n");
 
        allzeros = true;
        for (maxi = MAX_DELAY_US/GRANULARITY - 1; maxi >= 0; maxi--) {
@@ -111,7 +118,7 @@ void stopper()
                }
                if (!allzeros) break;
        }
-       if (maxi < 10000/GRANULARITY) maxi = 10000/GRANULARITY;
+       if (maxi < 3000/GRANULARITY) maxi = 3000/GRANULARITY;
 
        for (ac = 0; ac < AC_QUEUES; ac++) { 
                sum[ac] = 0;
@@ -192,6 +199,9 @@ int create_ac_socket(unsigned int ac)
        return sockfd;
 }
 
+void empty_handler()
+{
+}
 
 void* receiver(void* queue)
 {
@@ -205,14 +215,18 @@ void* receiver(void* queue)
        
        min_trans_time = ~0;
        
+       block_signals();
+       set_rt_prio(99);
+
        ac = (int)queue;
         rem_addr_length = sizeof(rem_addr);
-       while (1) {
-               while ((mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,\
-                       (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
-                           if (errno == EINTR) continue;
-                           perror("Chyba pri prijimani pozadavku");
-                           return NULL;
+       while (!exit_flag) {
+               mlen = recvfrom(ac_sockfd[ac], &msg, sizeof(msg), 0,    \
+                               (struct sockaddr*)&rem_addr, &rem_addr_length);
+               if (mlen < 0) {
+                       if (errno == EINTR) continue;
+                       perror("Chyba pri prijimani pozadavku");
+                       goto out;
                }       
                clock_gettime(CLOCK_MONOTONIC,&recv_timestamp);
                send_timestamp = msg.send_timestamp;
@@ -232,6 +246,9 @@ void* receiver(void* queue)
                         send_timestamp.tv_nsec,recv_timestamp.tv_sec,\
                         recv_timestamp.tv_nsec, trans_time_msec); */
        }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
 }
 
 void* sender(void* endpoint)
@@ -267,7 +284,10 @@ void* sender(void* endpoint)
        period.tv_nsec = spoint->period_nsec;
        period.tv_sec = 0;
 
-       while (1) {
+       block_signals();
+       set_rt_prio(90-ac);
+
+       while (!exit_flag) {
 
                msg.seqn = seqn;
                msg.tos = ac_to_tos[ac];
@@ -277,11 +297,11 @@ void* sender(void* endpoint)
                while (sendto(ac_sockfd[ac], &msg, sizeof(msg), 0,\
                                (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0) {
                                if (errno == EINTR) continue;
-                               perror("Error while sending.");
-                               return NULL;
+                               perror("Error while sending");
+                               goto out;
                }
 
-#if DEBUG
+#ifdef DEBUG
                printf("%d", ac);
                fflush(stdout);
 #endif
@@ -291,7 +311,10 @@ void* sender(void* endpoint)
                clock_gettime(CLOCK_MONOTONIC,&current_time);
                timespec_sub(&interval,&time_to_wait,&current_time);
                nanosleep(&interval,NULL);
-       }       
+       }
+out:
+       sem_post(&sem_thread_finished);
+       return NULL;
 }
 
 int main(int argc, char *argv[])
@@ -341,11 +364,22 @@ int main(int argc, char *argv[])
                perror("Signal handler registration error");
                exit(1);
        }
+
+       struct sigaction sa;
+       sa.sa_handler = empty_handler;
+       sa.sa_flags = 0;        /* don't restart syscalls */
+
+       if (sigaction(SIGUSR1, &sa, NULL) < 0) {
+               perror("sigaction error");
+               exit(1);
+       }
+
+       sem_init(&sem_thread_finished, 0, 0);
        
        /* create four receivers each per AC */
        for (ac = AC_QUEUES - 1; ac >= 0; ac--) {
                ac_sockfd[ac] = create_ac_socket(ac);
-               rc = pthread_create(&thread, &attr, receiver, (void*) ac);
+               rc = pthread_create(&receivers[ac].thread, &attr, receiver, (void*) ac);
                if (rc) {
                        printf("Error while creating receiver %d\n",rc);
                        return 1;
@@ -361,9 +395,17 @@ int main(int argc, char *argv[])
                }
        }
        
-       while (1) {
+       while (!exit_flag) {
                sleep(100000);
        }
-       
+
+       printf("Waiting for threads to finish\n");
+       /* Wait for all threads to finish */
+       for (i=0; i < nr_sepoints + AC_QUEUES; i++) {
+               sem_wait(&sem_thread_finished);
+       }
+
+       save_results();
+
        return 0;
 }
index 93ad509e67a3927bfd56cfcaa1dfa57862c21f80..3c7b78fc55373d150547ce03283dc76fc0d367cc 100644 (file)
 #include <time.h>
 #include <string.h>
 #include <pthread.h>
+#include "common.h"
 
-#define BASE_PORT      5100
 #define        BUFFSIZE        65536
-#define AC_QUEUES      4
 
-/* Turn on/off debugging */
-#define DEBUG 0
-
-const int prio_to_ac[8] = {2,3,3,2,1,1,0,0};
-const unsigned int ac_to_tos[4] = {224,160,96,64};
 int ac_sockfd[AC_QUEUES];
 
 
@@ -87,6 +81,10 @@ void* qhandler(void* queue)
        ac = (int) queue;
        printf("AC= %d\n",ac);
         rem_addr_length=sizeof(rem_addr);
+
+       block_signals();
+       set_rt_prio(90-ac);
+
        while (1) {
                while ((mlen = recvfrom(ac_sockfd[ac], buff, sizeof(buff) , 0, \
                        (struct sockaddr*)&rem_addr, &rem_addr_length)) < 0) {
@@ -95,7 +93,7 @@ void* qhandler(void* queue)
                            return NULL;
                }
 
-#if DEBUG
+#ifdef DEBUG
                printf("%d",ac);
                fflush(stdout);
 #endif