]> rtime.felk.cvut.cz Git - frescor/fna.git/blobdiff - src_frescan/frescan_servers_replenishments.c
add group negotiations to frescan and change all the requests and messages to map...
[frescor/fna.git] / src_frescan / frescan_servers_replenishments.c
index c47fb1846d3e09c4a77d1e970b1841d61f4d544a..b2e75f44045b8dfa88f85d5b1927a9e89cfaf567 100644 (file)
  *
  * @license
  *
- * See MaRTE OS license
+ * -----------------------------------------------------------------------
+ *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
+ *
+ *    Universidad de Cantabria,              SPAIN
+ *    University of York,                    UK
+ *    Scuola Superiore Sant'Anna,            ITALY
+ *    Kaiserslautern University,             GERMANY
+ *    Univ. Politécnica  Valencia,           SPAIN
+ *    Czech Technical University in Prague,  CZECH REPUBLIC
+ *    ENEA                                   SWEDEN
+ *    Thales Communication S.A.              FRANCE
+ *    Visual Tools S.A.                      SPAIN
+ *    Rapita Systems Ltd                     UK
+ *    Evidence                               ITALY
+ *
+ *    See http://www.frescor.org for a link to partners' websites
+ *
+ *           FRESCOR project (FP6/2005/IST/5-034026) is funded
+ *        in part by the European Union Sixth Framework Programme
+ *        The European Union is not liable of any use that may be
+ *        made of this code.
+ *
+ *  This file is part of FRESCAN
+ *
+ *  FRESCAN 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 2, or (at  your option)
+ *  any later version.
+ *
+ *  FRESCAN  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
+ *  distributed  with  FRESCAN;  see file COPYING.   If not,  write to the
+ *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
+ *  02111-1307, USA.
+ *
+ * As a special exception, including FRESCAN header files in a file,
+ * instantiating FRESCAN generics or templates, or linking other files
+ * with FRESCAN objects to produce an executable application, does not
+ * by itself cause the resulting executable application to be covered
+ * by the GNU General Public License. This exception does not
+ * however invalidate any other reasons why the executable file might be
+ * covered by the GNU Public License.
+ * -----------------------------------------------------------------------
  *
  */
 
 #include <time.h>            // clock_gettime
+#include <assert.h>          // assert
 
 #include <misc/freelist.h>   // freelist_t
 #include <misc/linux_list.h> // list_add_tail
-#include <misc/timespec_operations.h>
 
 #include "frescan_servers_replenishments.h"
 #include "frescan_config.h"  // FRESCAN_MX_REPL_OPS
-#include "frescan_debug.h"   // ERROR
+#include "frescan_debug.h"   // FRESCAN_ERROR
 #include "frescan_data.h"    // frescan_repl_op_t
-#include "fosa_threads_and_signals.h" // fosa_thread_attr_init...
+#include "fosa.h" // fosa_thread_attr_init, smaller_timespec, incr_timespec
+
+#if (FRESCAN_MEASURE_REPL_TH || FRESCAN_MEASURE_REPL_PROGRAM)
+#include <misc/time_measurement_posix.h>
+#include <misc/logger.h>
+static time_measure_id_t measure_id;
+#endif
 
 /**
  * the_repl_op_pool - pool of replenishment operations
@@ -55,20 +107,20 @@ static int frescan_repl_op_init()
         return freelist_init(&the_repl_op_pool_freelist, FRESCAN_MX_REPL_OPS);
 }
 
-static frescan_repl_op_t *frescan_repl_op_alloc()
+frescan_repl_op_t *frescan_repl_op_alloc()
 {
         int pos;
 
         pos = freelist_alloc(&the_repl_op_pool_freelist);
         if (pos == -1) {
-                ERROR("could not allocate repl op\n");
+                FRESCAN_ERROR("could not allocate repl op\n");
                 return NULL;
         }
         the_repl_op_pool[pos].pool_pos = pos; // to know how to free it
         return &the_repl_op_pool[pos];
 }
 
-static int frescan_repl_op_free(frescan_repl_op_t *repl_op)
+int frescan_repl_op_free(frescan_repl_op_t *repl_op)
 {
         return freelist_free(&the_repl_op_pool_freelist, repl_op->pool_pos);
 }
@@ -84,9 +136,7 @@ static void *frescan_repl_thread(void *arg)
         siginfo_t siginfo;
         frescan_ss_t id;
         frescan_network_t net;
-        struct list_head *pos;
-        frescan_repl_op_t *repl = NULL;
-        frescan_server_data_t *server;
+        frescan_ss_data_t *server;
         struct itimerspec timerdata;
 
         net = (frescan_network_t)(uint32_t)arg;
@@ -97,15 +147,16 @@ static void *frescan_repl_thread(void *arg)
         sigaddset(&set, FRESCAN_REPL_SIGNAL_NUM);
 
         while (1) {
+#if FRESCAN_MEASURE_REPL_TH
+                ret = time_measure_posix_begin(measure_id);
+                assert(ret == 0);
+#endif
                 ret = sigwaitinfo(&set, &siginfo);
-                if (ret == -1) {
-                        ERROR("sigwaitinfo failed\n");
-                        return NULL;
-                }
+                assert(ret != -1);
 
                 if (siginfo.si_signo != FRESCAN_REPL_SIGNAL_NUM) continue;
 
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG,
+                DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
                       "net:%u signal:%d code:%d value(server_id):%d\n",
                       net,
                       siginfo.si_signo,            // FRESCAN_REPL_SIGNAL_NUM
@@ -113,60 +164,22 @@ static void *frescan_repl_thread(void *arg)
                       siginfo.si_value.sival_int); // the server id
 
                 id = siginfo.si_value.sival_int;
-                server = &the_servers_pool[net][id];
-
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG,
-                      "id:%u, current_budget:%u, budget:%u, current_prio:%u\n",
-                      id,
-                      server->current_budget,
-                      server->params.values.budget,
-                      server->current_priority);
-
-                server->current_budget++;
-
-                if (server->current_priority == FRESCAN_BACKGROUND_PRIO) {
-                        server->current_priority = server->params.prio;
-                }
+                server = &frescan_data[net].ss_data[id];
 
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG,
-                      "now... current_budget:%u, current_prio:%u\n",
-                      server->current_budget,
-                      server->current_priority);
-
-                // delete the replenishment of this call
-                list_for_each(pos, &server->replenishments.repl_list) {
-                        repl = list_entry(pos, frescan_repl_op_t, repl_list);
-                        break;
-                }
-
-                list_del(&repl->repl_list);
-
-                ret = frescan_repl_op_free(repl);
-                if (ret != 0) {
-                        ERROR("could not free replenishment op\n");
-                        return NULL;
-                }
+                DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
+                      "id:%u current_prio:%u\n", id, server->current_priority);
 
-                // check if there are pending replenishments
-                if (list_empty(&server->replenishments.repl_list)) continue;
+                server->current_priority = server->committed_params.prio;
 
-                list_for_each(pos, &server->replenishments.repl_list) {
-                        repl = list_entry(pos, frescan_repl_op_t, repl_list);
-                        break;
+                if (!list_empty(&server->packet_list.fifo_list)) {
+                        clock_gettime (CLOCK_MONOTONIC, &server->act_time);
                 }
+#if FRESCAN_MEASURE_REPL_TH
+                ret = time_measure_posix_end(measure_id, "thread");
+                assert(ret == 0);
 
-                timerdata.it_value = repl->when;
-
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG,
-                      "set timer to %d sec, %d nsec\n",
-                      repl->when.tv_sec, repl->when.tv_nsec);
-
-                ret = timer_settime(server->repl_timer,
-                                    TIMER_ABSTIME, &timerdata, NULL);
-                if (ret != 0) {
-                        ERROR("could not set replenishment timer\n");
-                        return NULL;
-                }
+                while (logger_manual_call() > 0);
+#endif
         }
 
         return NULL;
@@ -190,7 +203,7 @@ int frescan_replenishments_init(frescan_network_t net)
 
         ret = frescan_repl_op_init();
         if (ret != 0) {
-                ERROR("could not init repl_op pool\n");
+                FRESCAN_ERROR("could not init repl_op pool\n");
                 return ret;
         }
 
@@ -198,37 +211,46 @@ int frescan_replenishments_init(frescan_network_t net)
 
         ret = fosa_set_accepted_signals(signal_set, 1);
         if (ret != 0) {
-                ERROR("could not set the repl signal\n");
+                FRESCAN_ERROR("could not set the repl signal\n");
                 return ret;
         }
 
         // create the replenishment thread
+#if (FRESCAN_MEASURE_REPL_TH || FRESCAN_MEASURE_REPL_PROGRAM)
+        ret = logger_init(LOG_ETHERNET);
+        assert(ret == 0);
+
+        ret = time_measure_posix_create("repl",
+                                        CLOCK_THREAD_CPUTIME_ID,
+                                        &measure_id);
+        assert(ret == 0);
+#endif
 
         ret = fosa_thread_attr_init(&attr);
         if (ret != 0) {
-                ERROR("could not init thread attributes\n");
+                FRESCAN_ERROR("could not init thread attributes\n");
                 return ret;
         }
 
         ret = fosa_thread_attr_set_prio(&attr, FRESCAN_REPL_THREAD_PRIO);
         if (ret != 0) {
-                ERROR("could not set repl thread prio %d\n",
+                FRESCAN_ERROR("could not set repl thread prio %d\n",
                       FRESCAN_REPL_THREAD_PRIO);
                 return ret;
         }
 
-        ret = fosa_thread_create(&the_networks[net].repl_thread_id,
+        ret = fosa_thread_create(&frescan_data[net].repl_thread_id,
                                  &attr,
                                  frescan_repl_thread,
                                  (void *)(uint32_t)net);
         if (ret != 0) {
-                ERROR("could not create the replenishment thread\n");
+                FRESCAN_ERROR("could not create the replenishment thread\n");
                 return ret;
         }
 
         ret = fosa_thread_attr_destroy(&attr);
         if (ret != 0) {
-                ERROR("could not destroy thread attributes\n");
+                FRESCAN_ERROR("could not destroy thread attributes\n");
                 return ret;
         }
 
@@ -243,53 +265,71 @@ int frescan_replenishments_init(frescan_network_t net)
  */
 
 int frescan_replenishment_program(frescan_network_t net,
-                                  frescan_ss_t ss)
+                                  frescan_ss_t id,
+                                  const struct timespec *timestamp)
 {
         int ret;
-        frescan_repl_op_t *repl;
-        bool empty;
+        frescan_repl_op_t *repl = NULL;
         struct itimerspec timerdata;
-        frescan_server_data_t *server;
+        frescan_ss_data_t *server;
+        struct list_head *pos;
+        struct timespec now;
 
-        server = &the_servers_pool[net][ss];
+#if FRESCAN_MEASURE_REPL_PROGRAM
+        ret = time_measure_posix_begin(measure_id);
+        assert(ret == 0);
+#endif
 
-        if (server->current_priority == FRESCAN_BACKGROUND_PRIO) {
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG, "ss in background\n");
-                return 0;
-        }
+        server = &frescan_data[net].ss_data[id];
 
-        repl = frescan_repl_op_alloc();
-        if (repl == NULL) {
-                ERROR("could not allocate a repl operation\n");
-                return -1;
+        // extract the head
+        list_for_each(pos, &server->replenishments.repl_list) {
+                repl = list_entry(pos, frescan_repl_op_t, repl_list);
+                break;
         }
+        list_del(&repl->repl_list);
 
-        clock_gettime (CLOCK_MONOTONIC, &repl->when);
-        incr_timespec (&repl->when, &server->params.values.period);
+        // move to tail with new repl value
+        repl->when = *timestamp;
+        incr_timespec(repl->when, server->committed_params.period);
         repl->amount = 1;
 
-        empty = list_empty(&server->replenishments.repl_list);
-        DEBUG(FRESCAN_REPL_ENABLE_DEBUG, "ss:%u, empty:%u\n", ss, empty);
-
         list_add_tail(&repl->repl_list,
-                      &server->replenishments.repl_list);
+                       &server->replenishments.repl_list);
+
+        // check the new head
+        list_for_each(pos, &server->replenishments.repl_list) {
+                repl = list_entry(pos, frescan_repl_op_t, repl_list);
+                break;
+        }
+
+        clock_gettime (CLOCK_MONOTONIC, &now);
+
+        if (smaller_timespec(now, repl->when)) {
+                server->current_priority = FRESCAN_BACKGROUND_PRIO;
 
-        if (empty) {
                 timerdata.it_interval.tv_sec  = 0;
                 timerdata.it_interval.tv_nsec = 0;
                 timerdata.it_value = repl->when;
 
-                DEBUG(FRESCAN_REPL_ENABLE_DEBUG,
+                DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
                       "set timer to %d sec, %d nsec\n",
                       repl->when.tv_sec, repl->when.tv_nsec);
 
                 ret = timer_settime(server->repl_timer,
                                     TIMER_ABSTIME, &timerdata, NULL);
                 if (ret != 0) {
-                        ERROR("could not set the replenishment timer\n");
+                        FRESCAN_ERROR("could not set the replenishment timer\n");
                         return ret;
                 }
         }
 
+#if FRESCAN_MEASURE_REPL_PROGRAM
+                ret = time_measure_posix_end(measure_id, "program");
+                assert(ret == 0);
+
+                while (logger_manual_call() > 0);
+#endif
+
         return 0;
 }