]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - frsh_api/frsh_spare_capacity.c
77b5bb113fd31cdcb22e16e0d76d89d0e0e657e1
[frescor/frsh.git] / frsh_api / frsh_spare_capacity.c
1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                 */
4 /*                                                                        */
5 /*   Universidad de Cantabria,              SPAIN                         */
6 /*   University of York,                    UK                            */
7 /*   Scuola Superiore Sant'Anna,            ITALY                         */
8 /*   Kaiserslautern University,             GERMANY                       */
9 /*   Univ. Politécnica  Valencia,           SPAIN                        */
10 /*   Czech Technical University in Prague,  CZECH REPUBLIC                */
11 /*   ENEA                                   SWEDEN                        */
12 /*   Thales Communication S.A.              FRANCE                        */
13 /*   Visual Tools S.A.                      SPAIN                         */
14 /*   Rapita Systems Ltd                     UK                            */
15 /*   Evidence                               ITALY                         */
16 /*                                                                        */
17 /*   See http://www.frescor.org for a link to partners' websites          */
18 /*                                                                        */
19 /*          FRESCOR project (FP6/2005/IST/5-034026) is funded             */
20 /*       in part by the European Union Sixth Framework Programme          */
21 /*       The European Union is not liable of any use that may be          */
22 /*       made of this code.                                               */
23 /*                                                                        */
24 /*                                                                        */
25 /*  This file is part of FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH is free software; you can redistribute it and/or modify it        */
28 /* under terms of the GNU General Public License as published by the      */
29 /* Free Software Foundation; either version 2, or (at your option) any    */
30 /* later version.  FRSH is distributed in the hope that it will be        */
31 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
32 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
33 /* General Public License for more details. You should have received a    */
34 /* copy of the GNU General Public License along with FRSH; see file       */
35 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
36 /* Cambridge, MA 02139, USA.                                              */
37 /*                                                                        */
38 /* As a special exception, including FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH objects to produce an executable application, does not       */
41 /* by itself cause the resulting executable application to be covered     */
42 /* by the GNU General Public License. This exception does not             */
43 /* however invalidate any other reasons why the executable file might be  */
44 /* covered by the GNU Public License.                                     */
45 /**************************************************************************/
46
47 /**
48  * @file   frsh_spare_capacity.c
49  * @author Dario Faggioli <faggioli@gandalf.sssup.it>
50  * @author Michal Sojka <sojkam1@fel.cvut.cz>
51  * 
52  * @brief  Implementation of FRSH contract API on top of FRES contracts.
53  * 
54  * 
55  */
56 #include <fres_contract.h>
57 #include <fres_contract_idl.h>
58 #include <frsh_core.h>
59 #include <frsh_error.h>
60 #include <fres_blocks.h>
61 #include <string.h>
62 #include <fcb.h>
63 #include "frsh_forb.h"
64 #include <fra_generic.h>
65
66 int frsh_contract_set_reclamation_params
67   (frsh_contract_t *contract,
68    const struct timespec *stability_time,
69    const struct timespec *budget_max,
70    const struct timespec *period_min,
71    frsh_granularity_t granularity,
72    const frsh_utilization_set_t *utilization_set,
73    int importance,
74    int weight)
75 {
76         fres_block_spare_capacity *s;
77         int ret;
78
79         if (!contract || !*contract ||
80             !stability_time || !budget_max || !period_min)
81                 return FRSH_ERR_BAD_ARGUMENT;
82
83         if (fosa_rel_time_is_null(*budget_max) ||
84             fosa_rel_time_is_null(*period_min) ||
85             fosa_rel_time_smaller(*period_min, *budget_max))
86                 return FRSH_ERR_BAD_ARGUMENT;
87
88         if ((granularity == FRSH_GR_CONTINUOUS && utilization_set != NULL) ||
89             (granularity == FRSH_GR_DISCRETE && utilization_set == NULL))
90                 return FRSH_ERR_BAD_ARGUMENT;
91
92         if (granularity != FRSH_GR_CONTINUOUS &&
93             granularity != FRSH_GR_DISCRETE)
94                 return FRSH_ERR_NOT_IMPLEMENTED;
95
96         s = malloc(sizeof(*s));
97         if (!s) return ENOMEM; 
98         memset(s, 0, sizeof(*s));
99
100         s->budget = *budget_max;
101         s->period = *period_min;
102         s->granularity = granularity;
103
104         if (utilization_set) {
105                 int i;
106                 if (!forb_sequence_alloc_buf(s->variants, utilization_set->size)) {
107                         goto err;
108                 }
109                 for (i=0; i<utilization_set->size; i++) {
110                         struct fres_container *c = NULL;
111                         fres_block_basic *b = NULL;
112                         fres_block_timing_reqs *t = NULL;
113                         c = fres_container_new();
114                         if (!c)
115                                 goto err;
116                         b = malloc(sizeof(*b));
117                         if (!b)
118                                 goto err_ut;
119                         t = malloc(sizeof(*t));
120                         if (!t)
121                                 goto err_ut;
122
123                         b->budget = utilization_set->utilizations[i].budget;
124                         b->period = utilization_set->utilizations[i].period;
125                         /* FIXME: workload and contract type should be
126                          * the same as in basic_params. The easyest
127                          * solution would be to split basic block to
128                          * two blocks - (budget, period) and
129                          * (workload, contract_type).
130                          * Note: frsh_contract_set_reclamation_params()
131                          * should be defined differently. */
132                         b->workload = FRSH_WT_BOUNDED;
133                         b->contract_type = FRSH_CT_REGULAR;
134                         ret = fres_container_add_basic(c, b);
135
136                         t->d_equals_t = true; /* FIXME: the same as above */
137                         t->deadline = utilization_set->utilizations[i].deadline;
138
139                         ret = fres_container_add_timing_reqs(c, t);
140
141                         forb_sequence_elem(s->variants, i) = c;
142                         continue;
143                 err_ut:
144                         fres_container_destroy(c);
145                         free(b); /* FIXME: Possible double free */
146                         free(t);
147                         goto err;
148                 }
149                 s->variants._length = utilization_set->size;
150         }
151         /* TODO: In case of CONTINUOUS granularity copy min and max
152          * requirements to the variants. */
153         s->importance = importance;
154         s->weight = weight;
155         s->stability_time = *stability_time;
156
157         fres_contract_del_spare_capacity(*contract);
158         ret = fres_contract_add_spare_capacity(*contract, s);
159
160         if (ret)
161                 goto err;
162
163         return FRSH_NO_ERROR;
164 err:
165         forb_sequence_free_buf(s->variants, forb_no_destructor);
166         free(s);
167         return errno;
168 }
169
170 int frsh_contract_get_reclamation_params
171   (const frsh_contract_t *contract,
172    struct timespec *stability_time,
173    struct timespec *budget_max,
174    struct timespec *period_min,
175    frsh_granularity_t *granularity,
176    frsh_utilization_set_t *utilization_set,
177    int *importance,
178    int *weight)
179 {
180         fres_block_spare_capacity *s;
181
182         if (!contract || !*contract)
183                 return FRSH_ERR_BAD_ARGUMENT;
184
185         s = fres_contract_get_spare_capacity(*contract);
186
187         if (budget_max)
188                 *budget_max = s->budget;
189         if (period_min)
190                 *period_min = s->period;
191         if (granularity)
192                 *granularity = s->granularity;
193         /*
194          * TODO:
195          *  support utilization-set for discrete spare
196          *  capacity distribution.
197          */
198         if (importance)
199                 *importance = s->importance;
200         if (weight)
201                 *weight = s->weight;
202         if (stability_time)
203                 *stability_time = s->stability_time;
204
205         return FRSH_NO_ERROR;
206 }
207
208 int frsh_vres_get_remaining_stability_time
209   (frsh_vres_id_t vres,
210    struct timespec *stability_time)
211 {
212         return FRSH_ERR_NOT_IMPLEMENTED;
213 }
214
215 int frsh_resource_get_capacity
216   (const frsh_resource_type_t resource_type,
217    const frsh_resource_id_t resource_id,
218    const int importance,
219    uint32_t *capacity)
220 {
221         return FRSH_ERR_NOT_IMPLEMENTED;
222 }
223
224 int frsh_resource_get_total_weight
225   (const frsh_resource_type_t resource_type,
226    const frsh_resource_id_t resource_id,
227    const int importance,
228    uint32_t *total_weight)
229 {
230         return FRSH_ERR_NOT_IMPLEMENTED;
231 }
232
233 int frsh_vres_decrease_capacity
234   (frsh_vres_id_t vres_id,
235    struct timespec new_budget,
236    struct timespec new_period)
237 {
238         return FRSH_ERR_NOT_IMPLEMENTED;
239 }
240