]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src_frescan/frescan_bwres_analysis.c
Do not enter unnecessary subdirectories
[frescor/frsh-forb.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 - 2009 by the FRESCOR consortium:
26 //
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
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
40 //
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
44 //        made of this code.
45 //
46 //
47 //  based on previous work (FSF) done in the FIRST project
48 //
49 //   Copyright (C) 2005  Mälardalen University, SWEDEN
50 //                       Scuola Superiore S.Anna, ITALY
51 //                       Universidad de Cantabria, SPAIN
52 //                       University of York, UK
53 //
54 // This file is part of FNA (Frescor Network Adaptation)
55 //
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.
66 //
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 // -----------------------------------------------------------------------
75  *
76  */
77
78 #include <math.h>
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"
84
85 /**
86  * frescan_bwres_sa_init() - init the scenario
87  *
88  * Initialize the vres list and copy the initialization parameters
89  */
90
91 int frescan_bwres_sa_init(frescan_bwres_sa_scenario_t          *scenario,
92                           const frescan_bwres_sa_init_params_t *params)
93 {
94         int ret;
95         frsh_sa_scenario_init_data_t fsa_scenario_init_data;
96
97         INIT_LIST_HEAD(&scenario->vres_head.list);
98         scenario->init_params = *params;
99
100         ret = freelist_init(&scenario->fsa_vres_global_id_freelist,
101                             FRESCAN_MX_NODES*FRESCAN_MX_IDS);
102         if (ret != 0) return ret;
103
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));
108
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);
112
113         ret = frsh_sa_scenario_init(&scenario->fsa_scenario,
114                                     &fsa_scenario_init_data);
115         if (ret != 0) return -1;
116
117         return 0;
118 }
119
120 /**
121  * frescan_bwres_sa_add_contract() - add a contract to the scenario
122  *
123  * create the corresponding vres structure and set its values, and add the
124  * vres to the vres list
125  */
126
127 int frescan_bwres_sa_add_contract(frescan_bwres_sa_scenario_t *scenario,
128                                   frescan_ss_t          ss,
129                                   frescan_node_t        node,
130                                   const frsh_contract_t *contract)
131 {
132         int ret;
133         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
134
135         sa_vres->contract = *contract;
136         sa_vres->node     = node;
137         sa_vres->ss       = ss;
138         sa_vres->old_c    = 0;
139         sa_vres->old_t    = 0;
140         sa_vres->old_p    = 0;
141
142         ret = freelist_alloc(&scenario->fsa_vres_global_id_freelist);
143         if (ret < 0) return -1;
144
145         sa_vres->fsa_vres_global_id = (frsh_sa_vres_id_t)ret;
146
147         list_add_tail(&sa_vres->list,
148                       &scenario->vres_head.list);
149
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);
153
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);
165
166         ret = frsh_sa_scenario_add_vres(&scenario->fsa_scenario,
167                                         &sa_vres->contract,
168                                         sa_vres->fsa_vres_global_id);
169         if (ret != 0) return -1;
170
171         return 0;
172 }
173
174 /**
175  * frescan_bwres_sa_update_contract() - update a contract in the scenario
176  */
177
178 int frescan_bwres_sa_update_contract(frescan_bwres_sa_scenario_t  *scenario,
179                                      frescan_ss_t           ss,
180                                      frescan_node_t         node,
181                                      const frsh_contract_t  *contract,
182                                      frsh_contract_t        *old_contract)
183 {
184         int ret;
185         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
186
187         if (old_contract != NULL) {
188                 *old_contract = sa_vres->contract;
189         }
190
191         sa_vres->contract = *contract;
192
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);
196
197         ret = frsh_sa_scenario_modify_vres(&scenario->fsa_scenario,
198                                            sa_vres->fsa_vres_global_id,
199                                            &sa_vres->contract);
200         if (ret != 0) return -1;
201
202         return 0;
203 }
204
205 /**
206  * frescan_bwres_sa_remove_contract() - remove a contract from the scenario
207  */
208
209 int frescan_bwres_sa_remove_contract(frescan_bwres_sa_scenario_t *scenario,
210                                      frescan_ss_t                ss,
211                                      frescan_node_t              node,
212                                      frsh_contract_t             *contract)
213 {
214         int ret;
215         frescan_bwres_vres_t *sa_vres = &scenario->vres_pool[node][ss];
216
217         if (contract != NULL) {
218                 *contract = sa_vres->contract;
219         }
220
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);
224
225         ret = frsh_sa_scenario_del_vres(&scenario->fsa_scenario,
226                                         sa_vres->fsa_vres_global_id);
227         if (ret != 0) return -1;
228
229         ret = freelist_free(&scenario->fsa_vres_global_id_freelist,
230                             sa_vres->fsa_vres_global_id);
231         if (ret < 0) return -1;
232
233         list_del(&sa_vres->list);
234
235         return 0;
236 }
237
238 /**
239  * frescan_bwres_sa_sched_test() - perform a scheduling test on the scenario
240  *
241  */
242
243 int frescan_bwres_sa_sched_test(frescan_bwres_sa_scenario_t *scenario,
244                                 bool                        *is_schedulable)
245 {
246         int ret;
247
248         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "init the fsa scenario\n");
249
250         ret = frsh_sa_init_analysis(&scenario->fsa_scenario);
251         if (ret != 0) return -1;
252
253         ret = frsh_sa_assign_priorities(&scenario->fsa_scenario);
254         if (ret != 0) return -1;
255
256         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "do scheduling test\n");
257
258         ret = frsh_sa_sched_test(&scenario->fsa_scenario, is_schedulable);
259         if (ret != 0) return -1;
260
261         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "%s\n",
262               (*is_schedulable) ? "OK" : "FAILED");
263
264         return 0;
265 }
266
267 /**
268  * frescan_bwres_sa_spare_capacity() - distribute the spare capacity
269  */
270
271 int frescan_bwres_sa_spare_capacity(frescan_bwres_sa_scenario_t *scenario)
272 {
273         int ret;
274
275         DEBUG(FRESCAN_BWRES_SA_ENABLE_DEBUG, "distribute sc\n");
276
277         ret = frsh_sa_distribute_spare(&scenario->fsa_scenario);
278         if (ret != 0) return -1;
279
280         return 0;
281 }