]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_bwres_analysis.c
f8105b30c00bc0f53b15d161bcdddfdcb4db4a6e
[frescor/fna.git] / src_frescan / frescan_bwres_analysis.c
1 /*!
2  * @file frescan_bwres_analysis.c
3  *
4  * @brief FRESCAN bandwith reservation layer: sched analysis
5  *
6  * @version 0.02
7  *
8  * @date 3-Jul-2008
9  *
10  * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
11  *
12  * @comments
13  *
14  * This module contains the scheduling analysis functions for the
15  * admission tests and spare capacity distribution of the bandwith
16  * reservation (bwres) layer.
17  *
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
20  * change protocol.
21  *
22  * @license
23  *
24  * -----------------------------------------------------------------------
25  *  Copyright (C) 2006 - 2008 FRESCOR consortium partners:
26  *
27  *    Universidad de Cantabria,              SPAIN
28  *    University of York,                    UK
29  *    Scuola Superiore Sant'Anna,            ITALY
30  *    Kaiserslautern University,             GERMANY
31  *    Univ. Politécnica  Valencia,           SPAIN
32  *    Czech Technical University in Prague,  CZECH REPUBLIC
33  *    ENEA                                   SWEDEN
34  *    Thales Communication S.A.              FRANCE
35  *    Visual Tools S.A.                      SPAIN
36  *    Rapita Systems Ltd                     UK
37  *    Evidence                               ITALY
38  *
39  *    See http://www.frescor.org for a link to partners' websites
40  *
41  *           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
44  *        made of this code.
45  *
46  *  This file is part of FRESCAN
47  *
48  *  FRESCAN is free software; you can  redistribute it and/or  modify
49  *  it under the terms of  the GNU General Public License as published by
50  *  the Free Software Foundation;  either  version 2, or (at  your option)
51  *  any later version.
52  *
53  *  FRESCAN  is distributed  in  the hope  that  it  will  be useful,  but
54  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
55  *  MERCHANTABILITY  or  FITNESS FOR  A  PARTICULAR PURPOSE. See  the  GNU
56  *  General Public License for more details.
57  *
58  *  You should have  received a  copy of  the  GNU  General Public License
59  *  distributed  with  FRESCAN;  see file COPYING.   If not,  write to the
60  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
61  *  02111-1307, USA.
62  *
63  * As a special exception, including FRESCAN header files in a file,
64  * instantiating FRESCAN generics or templates, or linking other files
65  * with FRESCAN objects to produce an executable application, does not
66  * by itself cause the resulting executable application to be covered
67  * by the GNU General Public License. This exception does not
68  * however invalidate any other reasons why the executable file might be
69  * covered by the GNU Public License.
70  * -----------------------------------------------------------------------
71  *
72  */
73
74 #include <math.h>
75 #include <misc/timespec_operations.h>
76 #include <misc/linux_list.h>
77 #include <misc/freelist.h>
78 #include "frescan_bwres_analysis.h"
79 #include "frescan_debug.h"
80
81 /**
82  * frescan_bwres_sa_init() - init the scenario
83  *
84  * Initialize the vres list and copy the initialization parameters
85  */
86
87 int frescan_bwres_sa_init(frescan_bwres_sa_scenario_t          *scenario,
88                           const frescan_bwres_sa_init_params_t *params)
89 {
90         int ret;
91         frsh_sa_scenario_init_data_t fsa_scenario_init_data;
92
93         INIT_LIST_HEAD(&scenario->vres_head.list);
94         scenario->init_params = *params;
95
96         ret = freelist_init(&scenario->fsa_vres_global_id_freelist,
97                             FRESCAN_MX_NODES*FRESCAN_MX_IDS);
98         if (ret != 0) return ret;
99
100         fsa_scenario_init_data.min_priority = params->min_prio;
101         fsa_scenario_init_data.max_priority = params->max_prio;
102         fsa_scenario_init_data.ovhd_data.np =
103                         frsh_rel_time_to_sa_time(frsh_usec_to_rel_time(0));
104
105         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
106               "init the scenario min_prio:%d max_prio:%d\n",
107               params->min_prio, params->max_prio);
108
109         ret = frsh_sa_scenario_init(&scenario->fsa_scenario,
110                                     &fsa_scenario_init_data);
111         if (ret != 0) return -1;
112
113         return 0;
114 }
115
116 /**
117  * frescan_bwres_sa_add_contract() - add a contract to the scenario
118  *
119  * create the corresponding vres structure and set its values, and add the
120  * vres to the vres list
121  */
122
123 int frescan_bwres_sa_add_contract(frescan_bwres_sa_scenario_t *scenario,
124                                   frescan_ss_t          ss,
125                                   frescan_node_t        node,
126                                   const frsh_contract_t *contract)
127 {
128         int ret;
129         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
130
131         sa_vres->contract = *contract;
132         sa_vres->node     = node;
133         sa_vres->ss       = ss;
134         sa_vres->old_c    = 0;
135         sa_vres->old_t    = 0;
136         sa_vres->old_p    = 0;
137
138         ret = freelist_alloc(&scenario->fsa_vres_global_id_freelist);
139         if (ret < 0) return -1;
140
141         sa_vres->fsa_vres_global_id = (frsh_sa_vres_id_t)ret;
142
143         list_add_tail(&sa_vres->list,
144                       &scenario->vres_head.list);
145
146         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
147               "add contract, node:%d ss:%d globalid:%d\n",
148               node, ss, sa_vres->fsa_vres_global_id);
149
150         ret = frsh_sa_scenario_add_vres(&scenario->fsa_scenario,
151                                         &sa_vres->contract,
152                                         sa_vres->fsa_vres_global_id);
153         if (ret != 0) return -1;
154
155         return 0;
156 }
157
158 /**
159  * frescan_bwres_sa_update_contract() - update a contract in the scenario
160  */
161
162 int frescan_bwres_sa_update_contract(frescan_bwres_sa_scenario_t  *scenario,
163                                      frescan_ss_t           ss,
164                                      frescan_node_t         node,
165                                      const frsh_contract_t  *contract,
166                                      frsh_contract_t        *old_contract)
167 {
168         int ret;
169         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
170
171         if (old_contract != NULL) {
172                 *old_contract = sa_vres->contract;
173         }
174
175         sa_vres->contract = *contract;
176
177         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
178               "update contract node:%d ss:%d globalid:%d\n",
179               node, ss, sa_vres->fsa_vres_global_id);
180
181         ret = frsh_sa_scenario_modify_vres(&scenario->fsa_scenario,
182                                            sa_vres->fsa_vres_global_id,
183                                            &sa_vres->contract);
184         if (ret != 0) return -1;
185
186         return 0;
187 }
188
189 /**
190  * frescan_bwres_sa_remove_contract() - remove a contract from the scenario
191  */
192
193 int frescan_bwres_sa_remove_contract(frescan_bwres_sa_scenario_t *scenario,
194                                      frescan_ss_t                ss,
195                                      frescan_node_t              node,
196                                      frsh_contract_t             *contract)
197 {
198         int ret;
199         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
200
201         if (contract != NULL) {
202                 *contract = sa_vres->contract;
203         }
204
205         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG,
206               "remove contract, node:%d ss:%d globalid:%d\n",
207               node, ss, sa_vres->fsa_vres_global_id);
208
209         ret = frsh_sa_scenario_del_vres(&scenario->fsa_scenario,
210                                         sa_vres->fsa_vres_global_id);
211         if (ret != 0) return -1;
212
213         ret = freelist_free(&scenario->fsa_vres_global_id_freelist,
214                             sa_vres->fsa_vres_global_id);
215         if (ret < 0) return -1;
216
217         list_del(&sa_vres->list);
218
219         return 0;
220 }
221
222 /**
223  * frescan_bwres_sa_sched_test() - perform a scheduling test on the scenario
224  *
225  */
226
227 int frescan_bwres_sa_sched_test(frescan_bwres_sa_scenario_t *scenario,
228                                 bool                        *is_schedulable)
229 {
230         int ret;
231
232         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "init the fsa scenario\n");
233
234         ret = frsh_sa_init_analysis(&scenario->fsa_scenario);
235         if (ret != 0) return -1;
236
237         ret = frsh_sa_assign_priorities(&scenario->fsa_scenario);
238         if (ret != 0) return -1;
239
240         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "do scheduling test\n");
241
242         ret = frsh_sa_sched_test(&scenario->fsa_scenario, is_schedulable);
243         if (ret != 0) return -1;
244
245         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "%s\n",
246               (*is_schedulable) ? "OK" : "FAILED");
247
248         return 0;
249 }
250
251 /**
252  * frescan_bwres_sa_spare_capacity() - distribute the spare capacity
253  */
254
255 int frescan_bwres_sa_spare_capacity(frescan_bwres_sa_scenario_t *scenario)
256 {
257         int ret;
258
259         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "distribute sc\n");
260
261         ret = frsh_sa_distribute_spare(&scenario->fsa_scenario);
262         if (ret != 0) return -1;
263
264         return 0;
265 }