2 * @file frescan_bwres_analysis.c
4 * @brief FRESCAN bandwith reservation layer: sched analysis
10 * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
14 * This module contains the scheduling analysis functions for the
15 * admission tests and spare capacity distribution of the bandwith
16 * reservation (bwres) layer.
18 * The module "frescan_bwres_mode_change" must be used to apply finally this
19 * new parameters to the sporadic servers following an appropriate mode
24 //----------------------------------------------------------------------
25 // Copyright (C) 2006 - 2009 by the FRESCOR consortium:
27 // Universidad de Cantabria, SPAIN
28 // University of York, UK
29 // Scuola Superiore Sant'Anna, ITALY
30 // Kaiserslautern University, GERMANY
31 // Univ. Politecnica Valencia, SPAIN
32 // Czech Technical University in Prague, CZECH REPUBLIC
34 // Thales Communication S.A. FRANCE
35 // Visual Tools S.A. SPAIN
36 // Rapita Systems Ltd UK
39 // See http://www.frescor.org
41 // The FRESCOR project (FP6/2005/IST/5-034026) is funded
42 // in part by the European Union Sixth Framework Programme
43 // The European Union is not liable of any use that may be
47 // based on previous work (FSF) done in the FIRST project
49 // Copyright (C) 2005 Mälardalen University, SWEDEN
50 // Scuola Superiore S.Anna, ITALY
51 // Universidad de Cantabria, SPAIN
52 // University of York, UK
54 // This file is part of FNA (Frescor Network Adaptation)
56 // FNA is free software; you can redistribute it and/or modify it
57 // under terms of the GNU General Public License as published by the
58 // Free Software Foundation; either version 2, or (at your option) any
59 // later version. FNA is distributed in the hope that it will be
60 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
61 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 // General Public License for more details. You should have received a
63 // copy of the GNU General Public License along with FNA; see file
64 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
65 // Cambridge, MA 02139, USA.
67 // As a special exception, including FNA header files in a file,
68 // instantiating FNA generics or templates, or linking other files
69 // with FNA objects to produce an executable application, does not
70 // by itself cause the resulting executable application to be covered
71 // by the GNU General Public License. This exception does not
72 // however invalidate any other reasons why the executable file might be
73 // covered by the GNU Public License.
74 // -----------------------------------------------------------------------
79 #include <misc/timespec_operations.h>
80 #include <misc/linux_list.h>
81 #include <misc/freelist.h>
82 #include "frescan_bwres_analysis.h"
83 #include "frescan_debug.h"
86 * frescan_bwres_sa_init() - init the scenario
88 * Initialize the vres list and copy the initialization parameters
91 int frescan_bwres_sa_init(frescan_bwres_sa_scenario_t *scenario,
92 const frescan_bwres_sa_init_params_t *params)
95 frsh_sa_scenario_init_data_t fsa_scenario_init_data;
97 INIT_LIST_HEAD(&scenario->vres_head.list);
98 scenario->init_params = *params;
100 ret = freelist_init(&scenario->fsa_vres_global_id_freelist,
101 FRESCAN_MX_NODES*FRESCAN_MX_IDS);
102 if (ret != 0) return ret;
104 fsa_scenario_init_data.min_priority = params->min_prio;
105 fsa_scenario_init_data.max_priority = params->max_prio;
106 fsa_scenario_init_data.ovhd_data.np =
107 frsh_rel_time_to_sa_time(frsh_usec_to_rel_time(0));
109 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
110 "init the scenario min_prio:%d max_prio:%d\n",
111 params->min_prio, params->max_prio);
113 ret = frsh_sa_scenario_init(&scenario->fsa_scenario,
114 &fsa_scenario_init_data);
115 if (ret != 0) return -1;
121 * frescan_bwres_sa_add_contract() - add a contract to the scenario
123 * create the corresponding vres structure and set its values, and add the
124 * vres to the vres list
127 int frescan_bwres_sa_add_contract(frescan_bwres_sa_scenario_t *scenario,
130 const frsh_contract_t *contract)
133 frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
135 sa_vres->contract = *contract;
136 sa_vres->node = node;
142 ret = freelist_alloc(&scenario->fsa_vres_global_id_freelist);
143 if (ret < 0) return -1;
145 sa_vres->fsa_vres_global_id = (frsh_sa_vres_id_t)ret;
147 list_add_tail(&sa_vres->list,
148 &scenario->vres_head.list);
150 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
151 "add contract, node:%d ss:%d globalid:%d\n",
152 node, ss, sa_vres->fsa_vres_global_id);
154 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
155 "contract bmin=(%u,%u) tmax=(%u,%u) bmax=(%u,%u) tmin=(%u,%u) prio=%u\n",
156 contract->budget_min.tv_sec,
157 contract->budget_min.tv_nsec,
158 contract->period_max.tv_sec,
159 contract->period_max.tv_nsec,
160 contract->budget_max.tv_sec,
161 contract->budget_max.tv_nsec,
162 contract->period_min.tv_sec,
163 contract->period_min.tv_nsec,
164 contract->preemption_level);
166 ret = frsh_sa_scenario_add_vres(&scenario->fsa_scenario,
168 sa_vres->fsa_vres_global_id);
169 if (ret != 0) return -1;
175 * frescan_bwres_sa_update_contract() - update a contract in the scenario
178 int frescan_bwres_sa_update_contract(frescan_bwres_sa_scenario_t *scenario,
181 const frsh_contract_t *contract,
182 frsh_contract_t *old_contract)
185 frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
187 if (old_contract != NULL) {
188 *old_contract = sa_vres->contract;
191 sa_vres->contract = *contract;
193 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
194 "update contract node:%d ss:%d globalid:%d\n",
195 node, ss, sa_vres->fsa_vres_global_id);
197 ret = frsh_sa_scenario_modify_vres(&scenario->fsa_scenario,
198 sa_vres->fsa_vres_global_id,
200 if (ret != 0) return -1;
206 * frescan_bwres_sa_remove_contract() - remove a contract from the scenario
209 int frescan_bwres_sa_remove_contract(frescan_bwres_sa_scenario_t *scenario,
212 frsh_contract_t *contract)
215 frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
217 if (contract != NULL) {
218 *contract = sa_vres->contract;
221 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
222 "remove contract, node:%d ss:%d globalid:%d\n",
223 node, ss, sa_vres->fsa_vres_global_id);
225 ret = frsh_sa_scenario_del_vres(&scenario->fsa_scenario,
226 sa_vres->fsa_vres_global_id);
227 if (ret != 0) return -1;
229 ret = freelist_free(&scenario->fsa_vres_global_id_freelist,
230 sa_vres->fsa_vres_global_id);
231 if (ret < 0) return -1;
233 list_del(&sa_vres->list);
239 * frescan_bwres_sa_sched_test() - perform a scheduling test on the scenario
243 int frescan_bwres_sa_sched_test(frescan_bwres_sa_scenario_t *scenario,
244 bool *is_schedulable)
248 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "init the fsa scenario\n");
250 ret = frsh_sa_init_analysis(&scenario->fsa_scenario);
251 if (ret != 0) return -1;
253 ret = frsh_sa_assign_priorities(&scenario->fsa_scenario);
254 if (ret != 0) return -1;
256 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "do scheduling test\n");
258 ret = frsh_sa_sched_test(&scenario->fsa_scenario, is_schedulable);
259 if (ret != 0) return -1;
261 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "%s\n",
262 (*is_schedulable) ? "OK" : "FAILED");
268 * frescan_bwres_sa_spare_capacity() - distribute the spare capacity
271 int frescan_bwres_sa_spare_capacity(frescan_bwres_sa_scenario_t *scenario)
275 DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "distribute sc\n");
277 ret = frsh_sa_distribute_spare(&scenario->fsa_scenario);
278 if (ret != 0) return -1;