]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_bwres_analysis.c
make a simplified utilization analysis for the moment
[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  * This module contains the scheduling analysis data and functions for the
7  * admission tests and spare capacity distribution of the negotiation layer.
8  * It provides an easy API easy to understand and in the implementation part
9  * it makes call to the general sched analysis module of FRSH which is shared
10  * with the CPU contracts layer in FRSH.
11  *
12  * EXAMPLE of utilization:
13  *
14  * INITIALIZATION
15  * --------------
16  * params.min_prio = 0;
17  * params.max_prio = 16;
18  * params.overhead = ...;
19  * frescan_sa_init(&scenario, &params);
20  *
21  * NEGOTIATE
22  * ---------
23  * fadt_freelist_alloc(&freelist_contracts, &id);
24  * frescan_sa_add_contract(&scenario, &contracts[id], id);
25  * frescan_sa_sched_test(&scenario, &success);
26  *
27  * if (!success) {
28  *      frescan_sa_remove_contract(&scenario, id);
29  * } else {
30  *      frescan_sa_spare_capacity(&scenario);
31  *      vres_id = to_vres(resource_type, resource_id, id);
32  *      create vres runtime structures;
33  *
34  *      for vres_id in active_vres_id {
35  *                frescan_sa_get_final_values(&scenario,
36  *                                            to_index(vres_id),
37  *                                            &final_values);
38  *                update vres runtime structures if necessary;
39  *      }
40  * }
41  *
42  * RENEGOTIATE
43  * -----------
44  * copy old_contract
45  * frescan_sa_update_contract(&scenario, to_index(vres_id), &contract);
46  * frescan_sa_sched_test(&scenario, &success);
47  *
48  * if (!success) {
49  *         frescan_sa_update_contract(&sa_data,
50  *                                    to_index(vres_id),
51  *                                    &old_contract);
52  * } else {
53  *      frescan_sa_spare_capacity(&scenario);
54  *      for vres_id in active_vres_id {
55  *              frescan_sa_get_final_values(&scenario,
56  *                                          to_index(vres_id),
57  *                                          &final_values);
58  *              update vres runtime structures if necessary;
59  *      }
60  * }
61  *
62  * CANCEL
63  * ------
64  * frescan_sa_remove_contract(&scenario, to_index(vres_id));
65  * frescan_sa_spare_capacity(&scenario);
66  *
67  * @version 0.01
68  *
69  * @date 15-Apr-2008
70  *
71  * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
72  *
73  */
74
75 #include <math.h>
76 #include <misc/timespec_operations.h>
77 #include "frescan_bwres_analysis.h"
78 #undef ERROR
79 #include "frescan_debug.h"
80
81 /**
82  * frescan_sa_init() - init the scenario
83  *
84  * @scenario: the scenario (in out)
85  * @params: init params (in)
86  */
87
88 int frescan_sa_init(frescan_sa_scenario_t *scenario,
89                     const frescan_sa_init_params_t *params)
90 {
91 //         int ret;
92 //
93 //         ret = frsh_sa_scenario_init(scenario, params);
94 //         return ret;
95
96         INIT_LIST_HEAD(&scenario->contracts_head.list);
97         scenario->init_params = *params;
98         return 0;
99 }
100
101 /**
102  * frescan_sa_add_contract() - add a contract to the scenario
103  *
104  * @scenario: the scenario (in out)
105  * @contract: the new contract (in)
106  * @ss: the preallocated ss identificator (in)
107  * @node: the node this contract belongs to (in)
108  */
109
110 int frescan_sa_add_contract(frescan_sa_scenario_t    *scenario,
111                             const frescan_contract_t *contract,
112                             frescan_ss_t             ss,
113                             frescan_node_t           node)
114 {
115 //         int ret;
116 //
117 //         ret = frsh_sa_scenario_add_vres(scenario, contract, id);
118 //         return ret;
119         frescan_sa_contract_t *sa_contract;
120
121         sa_contract = &scenario->contracts[node][ss];
122
123         sa_contract->contract = *contract;
124         sa_contract->node     = node;
125         sa_contract->ss       = ss;
126
127         list_add_tail(&sa_contract->list,
128                       &scenario->contracts_head.list);
129
130         return 0;
131 }
132
133 /**
134  * frescan_sa_update_contract() - update a contract in the scenario
135  *
136  * @scenario: the scenario (in out)
137  * @ss: the ss identificator (in)
138  * @contract: the values to update the contract (in)
139  * @node: the node this contract belongs to (in)
140  */
141
142 int frescan_sa_update_contract(frescan_sa_scenario_t   *scenario,
143                                frescan_ss_t             ss,
144                                const frescan_contract_t *contract,
145                                frescan_node_t           node)
146 {
147 //         int ret;
148 //
149 //         ret = frsh_sa_scenario_modify_vres(scenario, id, *contract);
150 //         return ret;
151         frescan_sa_contract_t *sa_contract;
152
153         sa_contract = &scenario->contracts[node][ss];
154
155         sa_contract->contract = *contract;
156         sa_contract->node     = node;
157         sa_contract->ss       = ss;
158
159         return 0;
160 }
161
162 /**
163  * frescan_sa_remove_contract() - remove a contract from the scenario
164  *
165  * @scenario: the scenario (in out)
166  * @ss: the ss to remove (in)
167  * @node: the node this contract belongs to (in)
168  */
169
170 int frescan_sa_remove_contract(frescan_sa_scenario_t *scenario,
171                                frescan_ss_t          ss,
172                                frescan_node_t        node)
173 {
174 //         int ret;
175 //
176 //         ret = frsh_sa_scenario_del_vres(scenario, id);
177 //         return ret;
178         frescan_sa_contract_t *sa_contract;
179
180         sa_contract = &scenario->contracts[node][ss];
181         list_del(&sa_contract->list);
182
183         return 0;
184 }
185
186 /**
187  * frescan_sa_sched_test() - perform a scheduling test on the scenario
188  *
189  * @scenario: the scenario (in out)
190  * @success: if the scenario is schedulable or not (out)
191  */
192
193 int frescan_sa_sched_test(frescan_sa_scenario_t *scenario,
194                           bool *success)
195 {
196 //         int ret;
197 //
198 //         ret = frsh_sa_scenario_reset_to_min(scenario, NULL, NULL);
199 //         if (ret != 0) goto error;
200 //
201 //         ret = frsh_sa_assign_priorities(scenario, NULL, NULL, NULL, NULL);
202 //         if (ret != 0) goto error;
203 //
204 //         ret = frsh_sa_ceilings_ok(scenario);
205 //         if (ret != 0) goto error;
206 //
207 //         ret = frsh_sa_calculate_blockings(scenario);
208 //         if (ret != 0) goto error;
209 //
210 //         ret = frsh_sa_sched_test(scenario, success);
211 //         if (ret != 0) goto error;
212 //
213 //         return 0;
214 //
215 // error:
216 //         *success = false;
217 //         return ret;
218
219         struct list_head *pos;
220         frescan_sa_contract_t *sa_contract;
221         int num_contracts;
222         double utilization, max_utilization, budget, period;
223
224         WARNING("simplified ub test (no blockings, prio ordered)\n");
225
226         utilization = 0.0;
227         num_contracts = 0;
228
229         list_for_each(pos, &scenario->contracts_head.list) {
230                 sa_contract = list_entry(pos, frescan_sa_contract_t, list);
231                 sa_contract->final_values.server_prio = sa_contract->contract.prio;
232
233                 budget = (double)sa_contract->contract.min_values.budget *
234                          (double)FRESCAN_FRAME_TX_TIME;
235
236                 period = timespec_to_double
237                                 (&sa_contract->contract.min_values.period);
238
239                 utilization = utilization + (budget / period);
240
241                 num_contracts++;
242
243                 DEBUG(FRESCAN_SA_ENABLE_DEBUG,
244                       "sa_contract, node:%d ss:%d, c:%d t:(%d,%d) p:%d\n",
245                       sa_contract->node, sa_contract->ss,
246                       sa_contract->contract.min_values.budget,
247                       sa_contract->contract.min_values.period.tv_sec,
248                       sa_contract->contract.min_values.period.tv_nsec,
249                       sa_contract->contract.prio);
250         }
251
252         max_utilization = num_contracts *
253                           (pow(2.0, 1.0/(double)num_contracts) - 1);
254
255         DEBUG(FRESCAN_SA_ENABLE_DEBUG, "u:%f n:%d u_max:%f %s\n",
256               utilization, num_contracts, max_utilization,
257               (utilization < max_utilization) ? "accepted" : "not accepted");
258
259         if (utilization < max_utilization) {
260                 *success = true;
261         } else {
262                 *success = false;
263         }
264
265         return 0;
266 }
267
268 /**
269  * frescan_sa_spare_capacity() - distribute the remaining spare capacity
270  *
271  * @scenario: the scenario (in out)
272  */
273
274 int frescan_sa_spare_capacity(frescan_sa_scenario_t *scenario)
275 {
276 //         int ret;
277 //
278 //         ret = frsh_sa_distribute_spare(scenario,
279 //                                        NULL, NULL, NULL, NULL, NULL, NULL);
280 //         return ret;
281         WARNING("not implemented, returning 0\n");
282         return 0;
283 }
284
285 /**
286  * frescan_sa_get_final_values() - get the final values
287  *
288  * @scenario: the scenario (in)
289  * @ss: the ss from which we want the final values (in)
290  * @node: the node this contract belongs to (in)
291  * @final_values: the final values (out)
292  */
293
294
295 int frescan_sa_get_final_values(const frescan_sa_scenario_t *scenario,
296                                 frescan_ss_t              ss,
297                                 frescan_node_t            node,
298                                 frescan_sa_final_values_t *final_values)
299 {
300 //         final_values->budget   = scenario->sa_vres_alloc[id].c;
301 //         final_values->period   = scenario->sa_vres_alloc[id].t;
302 //         final_values->deadline = scenario->sa_vres_alloc[id].d;
303 //         final_values->priority = scenario->sa_vres_alloc[id].p;
304 //         return 0;
305
306         *final_values = scenario->contracts[node][ss].final_values;
307
308         return 0;
309 }