/*! * @file frescan_servers_replenishments.c * * @brief the replenishment data and thread for the servers * * @version 0.01 * * @date 12-Mar-2008 * * @author * Daniel Sangorrin * * @comments * * This module contains the thread that waits for server's replenishment * timer signals and perform the necessary replenishments. * * @license * * See MaRTE OS license * */ #include // clock_gettime #include // freelist_t #include // list_add_tail #include #include "frescan_servers_replenishments.h" #include "frescan_config.h" // FRESCAN_MX_REPL_OPS #include "frescan_debug.h" // ERROR #include "frescan_data.h" // frescan_repl_op_t #include "fosa_threads_and_signals.h" // fosa_thread_attr_init... /** * the_repl_op_pool - pool of replenishment operations * * We have a pool of replenishment operation structures and an associated * freelist where we can get/put replenishment operations in O(1) time * * @the_repl_op_pool: array with the replenishment operations allocated * @the_repl_op_pool_freelist: freelist for the_repl_op_pool * @frescan_repl_op_init: initializes the freelist * @frescan_repl_op_alloc: get a free replenishment operation structure * @frescan_repl_op_free: free a replenishment operation structure * */ static frescan_repl_op_t the_repl_op_pool[FRESCAN_MX_REPL_OPS]; static freelist_t the_repl_op_pool_freelist; 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() { int pos; pos = freelist_alloc(&the_repl_op_pool_freelist); if (pos == -1) { 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) { return freelist_free(&the_repl_op_pool_freelist, repl_op->pool_pos); } /** * frescan_repl_thread - the thread that executes the replenishments */ static void *frescan_repl_thread(void *arg) { int ret; sigset_t set; 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; struct itimerspec timerdata; net = (frescan_network_t)(uint32_t)arg; timerdata.it_interval.tv_sec = 0; timerdata.it_interval.tv_nsec = 0; sigemptyset(&set); sigaddset(&set, FRESCAN_REPL_SIGNAL_NUM); while (1) { ret = sigwaitinfo(&set, &siginfo); if (ret == -1) { ERROR("sigwaitinfo failed\n"); return NULL; } if (siginfo.si_signo != FRESCAN_REPL_SIGNAL_NUM) continue; DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG, "net:%u signal:%d code:%d value(server_id):%d\n", net, siginfo.si_signo, // FRESCAN_REPL_SIGNAL_NUM siginfo.si_code, // SI_TIMER siginfo.si_value.sival_int); // the server id id = siginfo.si_value.sival_int; server = &the_servers_pool[net][id]; DEBUG(FRESCAN_REPLENSH_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; if (!list_empty(&server->packet_list.fifo_list)) { clock_gettime (CLOCK_MONOTONIC, &server->act_time); } } DEBUG(FRESCAN_REPLENSH_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; } // check if there are pending replenishments if (list_empty(&server->replenishments.repl_list)) continue; list_for_each(pos, &server->replenishments.repl_list) { repl = list_entry(pos, frescan_repl_op_t, repl_list); break; } timerdata.it_value = repl->when; DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG, "set timer to (%d, %d)\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; } } return NULL; } /** * frescan_replenishments_init - init the replenishment structures and thread * * @net: the network instance * * Initialize the repl_op pool, set the mask for the timer signals and create * the thread that will await for those signals and replenish the appropiate * sporadic server. */ int frescan_replenishments_init(frescan_network_t net) { int ret; fosa_signal_t signal_set[1]; fosa_thread_attr_t attr; ret = frescan_repl_op_init(); if (ret != 0) { ERROR("could not init repl_op pool\n"); return ret; } signal_set[0] = FRESCAN_REPL_SIGNAL_NUM; ret = fosa_set_accepted_signals(signal_set, 1); if (ret != 0) { ERROR("could not set the repl signal\n"); return ret; } // create the replenishment thread ret = fosa_thread_attr_init(&attr); if (ret != 0) { 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_REPL_THREAD_PRIO); return ret; } ret = fosa_thread_create(&the_networks[net].repl_thread_id, &attr, frescan_repl_thread, (void *)(uint32_t)net); if (ret != 0) { 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"); return ret; } return 0; } /** * frescan_replenishment_program - set a replenishment operation * * @net: the network instance * @ss: the server */ int frescan_replenishment_program(frescan_network_t net, frescan_ss_t ss, const struct timespec *timestamp) { int ret; frescan_repl_op_t *repl; bool empty; struct itimerspec timerdata; frescan_server_data_t *server; server = &the_servers_pool[net][ss]; repl = frescan_repl_op_alloc(); if (repl == NULL) { ERROR("could not allocate a repl operation\n"); return -1; } repl->when = *timestamp; incr_timespec (&repl->when, &server->params.values.period); repl->amount = 1; empty = list_empty(&server->replenishments.repl_list); DEBUG(FRESCAN_REPLENSH_ENABLE_DEBUG, "ss:%u, empty:%u\n", ss, empty); list_add_tail(&repl->repl_list, &server->replenishments.repl_list); if (empty) { timerdata.it_interval.tv_sec = 0; timerdata.it_interval.tv_nsec = 0; timerdata.it_value = repl->when; 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"); return ret; } } return 0; }