2 * @file frescan_servers_replenishments.c
4 * @brief the replenishment data and thread for the servers
15 * This module contains the thread that waits for server's replenishment
16 * timer signals and perform the necessary replenishments.
20 * -----------------------------------------------------------------------
21 * Copyright (C) 2006 - 2008 FRESCOR consortium partners:
23 * Universidad de Cantabria, SPAIN
24 * University of York, UK
25 * Scuola Superiore Sant'Anna, ITALY
26 * Kaiserslautern University, GERMANY
27 * Univ. Politécnica Valencia, SPAIN
28 * Czech Technical University in Prague, CZECH REPUBLIC
30 * Thales Communication S.A. FRANCE
31 * Visual Tools S.A. SPAIN
32 * Rapita Systems Ltd UK
35 * See http://www.frescor.org for a link to partners' websites
37 * FRESCOR project (FP6/2005/IST/5-034026) is funded
38 * in part by the European Union Sixth Framework Programme
39 * The European Union is not liable of any use that may be
42 * This file is part of FRESCAN
44 * FRESCAN is free software; you can redistribute it and/or modify
45 * it under the terms of the GNU General Public License as published by
46 * the Free Software Foundation; either version 2, or (at your option)
49 * FRESCAN is distributed in the hope that it will be useful, but
50 * WITHOUT ANY WARRANTY; without even the implied warranty of
51 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
52 * General Public License for more details.
54 * You should have received a copy of the GNU General Public License
55 * distributed with FRESCAN; see file COPYING. If not, write to the
56 * Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
59 * As a special exception, including FRESCAN header files in a file,
60 * instantiating FRESCAN generics or templates, or linking other files
61 * with FRESCAN objects to produce an executable application, does not
62 * by itself cause the resulting executable application to be covered
63 * by the GNU General Public License. This exception does not
64 * however invalidate any other reasons why the executable file might be
65 * covered by the GNU Public License.
66 * -----------------------------------------------------------------------
70 #include <time.h> // clock_gettime
71 #include <assert.h> // assert
73 #include <misc/freelist.h> // freelist_t
74 #include <misc/linux_list.h> // list_add_tail
76 #include "frescan_servers_replenishments.h"
77 #include "frescan_config.h" // FRESCAN_MX_REPL_OPS
78 #include "frescan_debug.h" // FRESCAN_ERROR
79 #include "frescan_data.h" // frescan_repl_op_t
80 #include "fosa.h" // fosa_thread_attr_init, smaller_timespec, incr_timespec
82 #if (FRESCAN_MEASURE_REPL_TH || FRESCAN_MEASURE_REPL_PROGRAM)
83 #include <misc/time_measurement_posix.h>
84 #include <misc/logger.h>
85 static time_measure_id_t measure_id;
89 * the_repl_op_pool - pool of replenishment operations
91 * We have a pool of replenishment operation structures and an associated
92 * freelist where we can get/put replenishment operations in O(1) time
94 * @the_repl_op_pool: array with the replenishment operations allocated
95 * @the_repl_op_pool_freelist: freelist for the_repl_op_pool
96 * @frescan_repl_op_init: initializes the freelist
97 * @frescan_repl_op_alloc: get a free replenishment operation structure
98 * @frescan_repl_op_free: free a replenishment operation structure
102 static frescan_repl_op_t the_repl_op_pool[FRESCAN_MX_REPL_OPS];
103 static freelist_t the_repl_op_pool_freelist;
105 static int frescan_repl_op_init()
107 return freelist_init(&the_repl_op_pool_freelist, FRESCAN_MX_REPL_OPS);
110 frescan_repl_op_t *frescan_repl_op_alloc()
114 pos = freelist_alloc(&the_repl_op_pool_freelist);
116 FRESCAN_ERROR("could not allocate repl op\n");
119 the_repl_op_pool[pos].pool_pos = pos; // to know how to free it
120 return &the_repl_op_pool[pos];
123 int frescan_repl_op_free(frescan_repl_op_t *repl_op)
125 return freelist_free(&the_repl_op_pool_freelist, repl_op->pool_pos);
129 * frescan_repl_thread - the thread that executes the replenishments
132 static void *frescan_repl_thread(void *arg)
138 frescan_network_t net;
139 frescan_server_data_t *server;
140 struct itimerspec timerdata;
142 net = (frescan_network_t)(uint32_t)arg;
143 timerdata.it_interval.tv_sec = 0;
144 timerdata.it_interval.tv_nsec = 0;
147 sigaddset(&set, FRESCAN_REPL_SIGNAL_NUM);
150 #if FRESCAN_MEASURE_REPL_TH
151 ret = time_measure_posix_begin(measure_id);
154 ret = sigwaitinfo(&set, &siginfo);
157 if (siginfo.si_signo != FRESCAN_REPL_SIGNAL_NUM) continue;
159 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
160 "net:%u signal:%d code:%d value(server_id):%d\n",
162 siginfo.si_signo, // FRESCAN_REPL_SIGNAL_NUM
163 siginfo.si_code, // SI_TIMER
164 siginfo.si_value.sival_int); // the server id
166 id = siginfo.si_value.sival_int;
167 server = &the_servers_pool[net][id];
169 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
170 "id:%u current_prio:%u\n", id, server->current_priority);
172 server->current_priority = server->params.prio;
174 if (!list_empty(&server->packet_list.fifo_list)) {
175 clock_gettime (CLOCK_MONOTONIC, &server->act_time);
177 #if FRESCAN_MEASURE_REPL_TH
178 ret = time_measure_posix_end(measure_id, "thread");
181 while (logger_manual_call() > 0);
189 * frescan_replenishments_init - init the replenishment structures and thread
191 * @net: the network instance
193 * Initialize the repl_op pool, set the mask for the timer signals and create
194 * the thread that will await for those signals and replenish the appropiate
198 int frescan_replenishments_init(frescan_network_t net)
201 fosa_signal_t signal_set[1];
202 fosa_thread_attr_t attr;
204 ret = frescan_repl_op_init();
206 FRESCAN_ERROR("could not init repl_op pool\n");
210 signal_set[0] = FRESCAN_REPL_SIGNAL_NUM;
212 ret = fosa_set_accepted_signals(signal_set, 1);
214 FRESCAN_ERROR("could not set the repl signal\n");
218 // create the replenishment thread
219 #if (FRESCAN_MEASURE_REPL_TH || FRESCAN_MEASURE_REPL_PROGRAM)
220 ret = logger_init(LOG_ETHERNET);
223 ret = time_measure_posix_create("repl",
224 CLOCK_THREAD_CPUTIME_ID,
229 ret = fosa_thread_attr_init(&attr);
231 FRESCAN_ERROR("could not init thread attributes\n");
235 ret = fosa_thread_attr_set_prio(&attr, FRESCAN_REPL_THREAD_PRIO);
237 FRESCAN_ERROR("could not set repl thread prio %d\n",
238 FRESCAN_REPL_THREAD_PRIO);
242 ret = fosa_thread_create(&the_networks[net].repl_thread_id,
245 (void *)(uint32_t)net);
247 FRESCAN_ERROR("could not create the replenishment thread\n");
251 ret = fosa_thread_attr_destroy(&attr);
253 FRESCAN_ERROR("could not destroy thread attributes\n");
261 * frescan_replenishment_program - set a replenishment operation
263 * @net: the network instance
267 int frescan_replenishment_program(frescan_network_t net,
269 const struct timespec *timestamp)
272 frescan_repl_op_t *repl = NULL;
273 struct itimerspec timerdata;
274 frescan_server_data_t *server;
275 struct list_head *pos;
278 #if FRESCAN_MEASURE_REPL_PROGRAM
279 ret = time_measure_posix_begin(measure_id);
283 server = &the_servers_pool[net][ss];
286 list_for_each(pos, &server->replenishments.repl_list) {
287 repl = list_entry(pos, frescan_repl_op_t, repl_list);
290 list_del(&repl->repl_list);
292 // move to tail with new repl value
293 repl->when = *timestamp;
294 incr_timespec(repl->when, server->params.period);
297 list_add_tail(&repl->repl_list,
298 &server->replenishments.repl_list);
300 // check the new head
301 list_for_each(pos, &server->replenishments.repl_list) {
302 repl = list_entry(pos, frescan_repl_op_t, repl_list);
306 clock_gettime (CLOCK_MONOTONIC, &now);
308 if (smaller_timespec(now, repl->when)) {
309 server->current_priority = FRESCAN_BACKGROUND_PRIO;
311 timerdata.it_interval.tv_sec = 0;
312 timerdata.it_interval.tv_nsec = 0;
313 timerdata.it_value = repl->when;
315 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
316 "set timer to %d sec, %d nsec\n",
317 repl->when.tv_sec, repl->when.tv_nsec);
319 ret = timer_settime(server->repl_timer,
320 TIMER_ABSTIME, &timerdata, NULL);
322 FRESCAN_ERROR("could not set the replenishment timer\n");
327 #if FRESCAN_MEASURE_REPL_PROGRAM
328 ret = time_measure_posix_end(measure_id, "program");
331 while (logger_manual_call() > 0);