]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_servers_replenishments.c
changes to use the FRSH FSA module to do the analysis and spare capacity. TODO: finis...
[frescor/fna.git] / src_frescan / frescan_servers_replenishments.c
1 /*!
2  * @file frescan_servers_replenishments.c
3  *
4  * @brief the replenishment data and thread for the servers
5  *
6  * @version 0.01
7  *
8  * @date 12-Mar-2008
9  *
10  * @author
11  *      Daniel Sangorrin
12  *
13  * @comments
14  *
15  * This module contains the thread that waits for server's replenishment
16  * timer signals and perform the necessary replenishments.
17  *
18  * @license
19  *
20  * -----------------------------------------------------------------------
21  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
22  *
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
29  *    ENEA                                   SWEDEN
30  *    Thales Communication S.A.              FRANCE
31  *    Visual Tools S.A.                      SPAIN
32  *    Rapita Systems Ltd                     UK
33  *    Evidence                               ITALY
34  *
35  *    See http://www.frescor.org for a link to partners' websites
36  *
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
40  *        made of this code.
41  *
42  *  This file is part of FRESCAN
43  *
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)
47  *  any later version.
48  *
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.
53  *
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
57  *  02111-1307, USA.
58  *
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  * -----------------------------------------------------------------------
67  *
68  */
69
70 #include <time.h>            // clock_gettime
71 #include <assert.h>          // assert
72
73 #include <misc/freelist.h>   // freelist_t
74 #include <misc/linux_list.h> // list_add_tail
75
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
81
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;
86 #endif
87
88 /**
89  * the_repl_op_pool - pool of replenishment operations
90  *
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
93  *
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
99  *
100  */
101
102 static frescan_repl_op_t the_repl_op_pool[FRESCAN_MX_REPL_OPS];
103 static freelist_t the_repl_op_pool_freelist;
104
105 static int frescan_repl_op_init()
106 {
107         return freelist_init(&the_repl_op_pool_freelist, FRESCAN_MX_REPL_OPS);
108 }
109
110 frescan_repl_op_t *frescan_repl_op_alloc()
111 {
112         int pos;
113
114         pos = freelist_alloc(&the_repl_op_pool_freelist);
115         if (pos == -1) {
116                 FRESCAN_ERROR("could not allocate repl op\n");
117                 return NULL;
118         }
119         the_repl_op_pool[pos].pool_pos = pos; // to know how to free it
120         return &the_repl_op_pool[pos];
121 }
122
123 int frescan_repl_op_free(frescan_repl_op_t *repl_op)
124 {
125         return freelist_free(&the_repl_op_pool_freelist, repl_op->pool_pos);
126 }
127
128 /**
129  * frescan_repl_thread - the thread that executes the replenishments
130  */
131
132 static void *frescan_repl_thread(void *arg)
133 {
134         int ret;
135         sigset_t set;
136         siginfo_t siginfo;
137         frescan_ss_t id;
138         frescan_network_t net;
139         frescan_server_data_t *server;
140         struct itimerspec timerdata;
141
142         net = (frescan_network_t)(uint32_t)arg;
143         timerdata.it_interval.tv_sec  = 0;
144         timerdata.it_interval.tv_nsec = 0;
145
146         sigemptyset(&set);
147         sigaddset(&set, FRESCAN_REPL_SIGNAL_NUM);
148
149         while (1) {
150 #if FRESCAN_MEASURE_REPL_TH
151                 ret = time_measure_posix_begin(measure_id);
152                 assert(ret == 0);
153 #endif
154                 ret = sigwaitinfo(&set, &siginfo);
155                 assert(ret != -1);
156
157                 if (siginfo.si_signo != FRESCAN_REPL_SIGNAL_NUM) continue;
158
159                 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
160                       "net:%u signal:%d code:%d value(server_id):%d\n",
161                       net,
162                       siginfo.si_signo,            // FRESCAN_REPL_SIGNAL_NUM
163                       siginfo.si_code,             // SI_TIMER
164                       siginfo.si_value.sival_int); // the server id
165
166                 id = siginfo.si_value.sival_int;
167                 server = &the_servers_pool[net][id];
168
169                 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
170                       "id:%u current_prio:%u\n", id, server->current_priority);
171
172                 server->current_priority = server->params.prio;
173
174                 if (!list_empty(&server->packet_list.fifo_list)) {
175                         clock_gettime (CLOCK_MONOTONIC, &server->act_time);
176                 }
177 #if FRESCAN_MEASURE_REPL_TH
178                 ret = time_measure_posix_end(measure_id, "thread");
179                 assert(ret == 0);
180
181                 while (logger_manual_call() > 0);
182 #endif
183         }
184
185         return NULL;
186 }
187
188 /**
189  * frescan_replenishments_init - init the replenishment structures and thread
190  *
191  * @net: the network instance
192  *
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
195  * sporadic server.
196  */
197
198 int frescan_replenishments_init(frescan_network_t net)
199 {
200         int ret;
201         fosa_signal_t signal_set[1];
202         fosa_thread_attr_t attr;
203
204         ret = frescan_repl_op_init();
205         if (ret != 0) {
206                 FRESCAN_ERROR("could not init repl_op pool\n");
207                 return ret;
208         }
209
210         signal_set[0] = FRESCAN_REPL_SIGNAL_NUM;
211
212         ret = fosa_set_accepted_signals(signal_set, 1);
213         if (ret != 0) {
214                 FRESCAN_ERROR("could not set the repl signal\n");
215                 return ret;
216         }
217
218         // create the replenishment thread
219 #if (FRESCAN_MEASURE_REPL_TH || FRESCAN_MEASURE_REPL_PROGRAM)
220         ret = logger_init(LOG_ETHERNET);
221         assert(ret == 0);
222
223         ret = time_measure_posix_create("repl",
224                                         CLOCK_THREAD_CPUTIME_ID,
225                                         &measure_id);
226         assert(ret == 0);
227 #endif
228
229         ret = fosa_thread_attr_init(&attr);
230         if (ret != 0) {
231                 FRESCAN_ERROR("could not init thread attributes\n");
232                 return ret;
233         }
234
235         ret = fosa_thread_attr_set_prio(&attr, FRESCAN_REPL_THREAD_PRIO);
236         if (ret != 0) {
237                 FRESCAN_ERROR("could not set repl thread prio %d\n",
238                       FRESCAN_REPL_THREAD_PRIO);
239                 return ret;
240         }
241
242         ret = fosa_thread_create(&the_networks[net].repl_thread_id,
243                                  &attr,
244                                  frescan_repl_thread,
245                                  (void *)(uint32_t)net);
246         if (ret != 0) {
247                 FRESCAN_ERROR("could not create the replenishment thread\n");
248                 return ret;
249         }
250
251         ret = fosa_thread_attr_destroy(&attr);
252         if (ret != 0) {
253                 FRESCAN_ERROR("could not destroy thread attributes\n");
254                 return ret;
255         }
256
257         return 0;
258 }
259
260 /**
261  * frescan_replenishment_program - set a replenishment operation
262  *
263  * @net: the network instance
264  * @ss: the server
265  */
266
267 int frescan_replenishment_program(frescan_network_t net,
268                                   frescan_ss_t ss,
269                                   const struct timespec *timestamp)
270 {
271         int ret;
272         frescan_repl_op_t *repl = NULL;
273         struct itimerspec timerdata;
274         frescan_server_data_t *server;
275         struct list_head *pos;
276         struct timespec now;
277
278 #if FRESCAN_MEASURE_REPL_PROGRAM
279         ret = time_measure_posix_begin(measure_id);
280         assert(ret == 0);
281 #endif
282
283         server = &the_servers_pool[net][ss];
284
285         // extract the head
286         list_for_each(pos, &server->replenishments.repl_list) {
287                 repl = list_entry(pos, frescan_repl_op_t, repl_list);
288                 break;
289         }
290         list_del(&repl->repl_list);
291
292         // move to tail with new repl value
293         repl->when = *timestamp;
294         incr_timespec(repl->when, server->params.period);
295         repl->amount = 1;
296
297         list_add_tail(&repl->repl_list,
298                        &server->replenishments.repl_list);
299
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);
303                 break;
304         }
305
306         clock_gettime (CLOCK_MONOTONIC, &now);
307
308         if (smaller_timespec(now, repl->when)) {
309                 server->current_priority = FRESCAN_BACKGROUND_PRIO;
310
311                 timerdata.it_interval.tv_sec  = 0;
312                 timerdata.it_interval.tv_nsec = 0;
313                 timerdata.it_value = repl->when;
314
315                 DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG,
316                       "set timer to %d sec, %d nsec\n",
317                       repl->when.tv_sec, repl->when.tv_nsec);
318
319                 ret = timer_settime(server->repl_timer,
320                                     TIMER_ABSTIME, &timerdata, NULL);
321                 if (ret != 0) {
322                         FRESCAN_ERROR("could not set the replenishment timer\n");
323                         return ret;
324                 }
325         }
326
327 #if FRESCAN_MEASURE_REPL_PROGRAM
328                 ret = time_measure_posix_end(measure_id, "program");
329                 assert(ret == 0);
330
331                 while (logger_manual_call() > 0);
332 #endif
333
334         return 0;
335 }