]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/cpu_aquosa/lib/aqcpu_fra.c
Aquosa is initialized only when it is really needed
[frescor/frsh.git] / resources / cpu_aquosa / lib / aqcpu_fra.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 AQCPU (Aquosa CPU)                               */
26 /*                                                                        */
27 /* FWP 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.  FWP 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 FWP; 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 AQCPU header files in a file,        */
39 /* instantiating FWP generics or templates, or linking other files        */
40 /* with FWP 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   aqcpu_fra.c
49  * @author Martin Molnar <molnam1@fel.cvut.cz>
50  * @date   Wed Feb 18 16:21:01 2009
51  * 
52  * @brief  
53  * 
54  * 
55  */
56
57
58 #include <ul_log.h>
59 #include <fra_generic.h>
60
61 #include "aquosa/qres_lib.h"
62 #include "aquosa/qsup_lib.h"
63
64 #include "aqcpu_contract.h"
65
66 UL_LOG_CUST(ulogd_fra_aqcpu);
67 //ul_log_domain_t ulogd_fra_aqcpu = {UL_LOGL_MSG, "fra_aqcpu"};
68
69 static int aqcpu_initialized = 0;               /* initialization flag */
70
71 /*
72  * Test whether Aquosa Cpu modue is initialized 
73  */
74 static inline int aqcpu_is_initialized()
75 {
76         return (aqcpu_initialized == 1);
77 }
78
79
80
81 static int aqcpu_create_vres(fres_vres_t *vres, void *priv)
82 {
83         aqcpu_params_t cpu_params;
84         qres_sid_t sid;
85         qos_rv rv;
86
87         if (vres->priv) {
88                 errno = -EINVAL;
89                 return -1;
90         }
91
92         /* get aqcpu params from contract */
93         get_aqcpu_params(vres, &cpu_params);
94         /* create cpu vres */
95         rv = qres_create_server(&cpu_params, &sid);
96         if (rv != QOS_OK) {
97                 return qos_rv_int(rv);  
98         }
99         
100         printf("Created AQCPU VRES(sid=%d period=%lld us, budget=%lld us)\n",
101                         sid, cpu_params.P, cpu_params.Q);
102         if ((vres->priv = malloc(sizeof(qres_sid_t)))) {
103                 memcpy(vres->priv, &sid, sizeof(qres_sid_t));
104         }
105         
106         return 0;
107 }
108
109 /*
110  * aqcpu_cancel_vres(), cancels vres 
111  *
112  * The threads bound to the vres are unbound, and so, detached from their
113  * AQuoSA resource reservation server and continue their execution according
114  * to the standard Linux scheduler policies.
115  *
116  */
117 static int aqcpu_cancel_vres(fres_vres_t *vres, void *priv)
118 {
119         qres_sid_t sid;
120         qos_rv qrv;
121         
122         if (!vres->priv) {
123                 errno = -EINVAL;
124                 return -1;
125         }
126         memcpy(&sid, vres->priv, sizeof(qres_sid_t));
127         
128         qrv = qres_destroy_server(sid);
129         if (qrv != QOS_OK) {
130                 return qos_rv_int(qrv);
131         }
132         printf("Canceled AQCPU VRES(sid=%d)\n",sid);
133         free(vres->priv);
134
135         return 0;
136 }
137
138 /* aqcpu_vres_change(), change some parameters of a vres
139  *
140  * In fact, since that AQuoSA call doesn't deal with _its_ Q_min (budget_min
141  * for in FRSH semantic) parameter all the renegotiation will be accepted but
142  * the new temporal behaviour is not guaranteed!
143  * Obviously this is a bug and should/will be corrected in AQuoSA as soon as
144  * possible.
145  *
146  */
147 static
148 int aqcpu_change_vres(fres_vres_t *vres, void *priv)
149 {
150         aqcpu_params_t cpu_params;
151         qres_sid_t sid;
152         qos_rv qrv;
153         
154         if (!vres->priv) {
155                 errno = -EINVAL;
156                 return -1;
157         }
158         memcpy(&sid, vres->priv, sizeof(qres_sid_t));
159         
160         /* get aqcpu params from contract */
161         get_aqcpu_params(vres, &cpu_params);
162         
163         /* set cpu params */
164         qrv = qres_set_params(sid, &cpu_params);
165         
166         ul_logmsg("AQCPU VRES(sid=%d) params changed(period=%lld us,"
167                         "budget=%lld us)\n",sid, cpu_params.P,cpu_params.Q);
168
169         return 0;
170 }
171
172 /*
173  * installed as an exit handler (with 'atexit()') by the initialization
174  * code... For now it only calls the cleanup function of the AQuoSA
175  * Framework
176  */
177 static inline void aqcpu_cleanup_wrapper() {
178         qres_cleanup();
179 }
180
181 static
182 int aqcpu_fra_exit()
183 {
184         qos_rv rv;
185
186         rv = qres_cleanup();
187         return qos_rv_int(rv);
188 }
189
190 static
191 int fra_CPU_bind_thread
192   (fres_vres_t *vres,
193    const fosa_thread_id_t thread)
194 {
195         qos_rv  qrv;
196         qres_sid_t sid = *((qres_sid_t*)vres->priv);
197
198         if ((qrv = qres_attach_thread(sid, thread.linux_pid,
199             thread.linux_tid)) != QOS_OK)
200                 goto err;
201
202         return 0;
203 err:
204         return -1;
205 }
206
207 static
208 int fra_CPU_unbind_thread(const fosa_thread_id_t thread)
209 {
210         qos_rv  qrv;
211         qres_sid_t  sid;
212
213         if (qres_get_sid(thread.linux_pid, thread.linux_tid, &sid) != QOS_OK)
214                 goto err;
215
216         if ((qrv = qres_detach_thread(sid, thread.linux_pid,
217             thread.linux_tid)) != QOS_OK)
218                 goto err;
219
220         return 0;
221 err:
222         return -1;
223 }
224
225 static
226 int fra_CPU_get_usage
227   (const fres_vres_t *vres,
228    fosa_rel_time_t *spent)
229 {
230         qres_sid_t sid = *((qres_sid_t*)vres->priv);
231         qres_time_t exec_budget;
232         qos_rv rv;
233
234         if (!spent)
235                 return FRSH_ERR_BAD_ARGUMENT;
236
237         rv = qres_get_exec_time(sid, &exec_budget, NULL);
238         if (rv != QOS_OK) return EINVAL;
239
240         *spent = fosa_usec_to_rel_time(exec_budget);
241
242         return 0;
243 }
244
245 static
246 int fra_CPU_get_job_usage
247   (const fres_vres_t *vres,
248    fosa_rel_time_t *spent)
249 {
250         qres_sid_t sid = *((qres_sid_t*)vres->priv);
251         qres_params_t q_params;
252         qres_time_t curr_budget;
253         qos_rv rv;
254
255         if (!spent)
256                 return FRSH_ERR_BAD_ARGUMENT;
257
258         rv = qres_get_params(sid, &q_params);
259         if (rv != QOS_OK) return EINVAL;
260         rv = qres_get_curr_budget(sid, &curr_budget);
261         if (rv != QOS_OK) return EINVAL;
262
263         *spent = fosa_usec_to_rel_time(q_params.Q - curr_budget);
264
265         return 0;
266 }
267
268 static
269 int fra_CPU_get_remaining_budget
270   (const fres_vres_t *vres,
271    fosa_rel_time_t *budget)
272 {
273         qres_sid_t sid = *((qres_sid_t*)vres->priv);
274         qres_time_t curr_budget;
275         qos_rv rv;
276
277         if (!budget)
278                 return FRSH_ERR_BAD_ARGUMENT;
279
280         rv = qres_get_curr_budget(sid, &curr_budget);
281         if (rv != QOS_OK) return EINVAL;
282
283         *budget = fosa_usec_to_rel_time(curr_budget);
284
285         return 0;
286 }
287
288 static
289 int fra_CPU_set_spare_bandwidth(fres_vres_t *vres)
290 {
291         qres_sid_t fake_sid = (qres_sid_t) -1;
292         aqcpu_params_t cpu_params;
293         qos_rv rv;
294
295         /* get aqcpu params from contract */
296         get_aqcpu_params(vres, &cpu_params);
297
298         
299         /* reserve CPU bandwidth for feedback */
300         rv = qsup_reserve_spare(r2bw(cpu_params.Q, cpu_params.P));
301         if (rv != QOS_OK) return qos_rv_int(rv);
302
303         printf("Created AQCPU spare (period=%lld us, budget=%lld us)\n",
304                         cpu_params.P, cpu_params.Q);
305         if ((vres->priv = malloc(sizeof(qres_sid_t)))) {
306                 memcpy(vres->priv, &fake_sid, sizeof(qres_sid_t));
307         }
308
309         return 0;
310 }
311
312 static
313 int fra_CPU_get_desired_budget
314   (fres_vres_t *vres,
315    frsh_rel_time_t *p_budget_out)
316 {
317         qres_sid_t sid = *((qres_sid_t*)vres->priv);
318         qres_params_t q_params;
319         qos_rv rv;
320
321         if (!p_budget_out)
322                 return FRSH_ERR_BAD_ARGUMENT;
323
324         rv = qres_get_params(sid, &q_params);
325         if (rv != QOS_OK) return EINVAL;
326
327         *p_budget_out = fosa_usec_to_rel_time(q_params.Q);
328
329         return 0;
330 }
331
332 static
333 int fra_CPU_set_desired_budget
334   (fres_vres_t *vres,
335    fosa_rel_time_t *p_budget_in)
336 {
337         qres_sid_t sid = *((qres_sid_t*)vres->priv);
338         qres_params_t q_params;
339         qos_rv rv;
340
341         if (!p_budget_in)
342                 return FRSH_ERR_BAD_ARGUMENT;
343
344         rv = qres_get_params(sid, &q_params);
345         if (rv != QOS_OK) return EINVAL;
346
347         q_params.Q = fosa_rel_time_to_usec(*p_budget_in);
348
349         rv = qres_set_params(sid, &q_params);
350         if (rv != QOS_OK) return EINVAL;
351
352         return 0;
353 }
354
355 static
356 int fra_CPU_get_actual_budget
357   (fres_vres_t *vres,
358    fosa_rel_time_t *budget)
359 {
360         qres_sid_t sid = *((qres_sid_t*)vres->priv);
361         qres_time_t appr_budget;
362         qos_rv rv;
363
364         if (!budget)
365                 return FRSH_ERR_BAD_ARGUMENT;
366
367         rv = qres_get_appr_budget(sid, &appr_budget);
368         if (rv != QOS_OK) return EINVAL;
369
370         *budget = fosa_usec_to_rel_time(appr_budget);
371
372         return 0;
373 }
374
375 int aqcpu_fra_activate(forb_orb orb)
376 {
377         qos_rv qrv;
378         if ((qrv = qres_init()) != QOS_OK) {
379                 if (qrv == QOS_E_MISSING_COMPONENT) {
380                         return FRES_ERR_KERNEL_SUPPORT_MISSING;
381                 } else
382                         return FRSH_ERR_INTERNAL_ERROR;
383         }
384
385         /* install the cleanup function of th AQuoSA framework as an exit
386          * handler function (quite futile but, for now, it's sufficent) */
387         if (atexit(aqcpu_cleanup_wrapper))
388                 return(FRSH_ERR_INTERNAL_ERROR);
389
390         aqcpu_initialized = 1;
391
392         return 0;
393 }
394
395 static struct fres_allocator aqcpu_allocator = {
396         .res_type = FRSH_RT_PROCESSOR,
397         .res_id = 0,  /* CPU ID 0 */
398         .create_vres = aqcpu_create_vres,
399         .cancel_vres = aqcpu_cancel_vres,
400         .change_vres = aqcpu_change_vres,
401
402         .bind_thread = fra_CPU_bind_thread,
403         .unbind_thread = fra_CPU_unbind_thread,
404
405         .vres_get_usage = fra_CPU_get_usage,
406         .vres_get_job_usage = fra_CPU_get_job_usage,
407         .vres_get_remaining_budget = fra_CPU_get_remaining_budget,
408
409         .set_spare_bandwidth = fra_CPU_set_spare_bandwidth,
410         .get_desired_budget = fra_CPU_get_desired_budget,
411         .set_desired_budget = fra_CPU_set_desired_budget,
412         .get_actual_budget = fra_CPU_get_actual_budget,
413
414         .activate_callback = aqcpu_fra_activate,
415         
416         .priv = NULL
417 };
418
419 /*
420  * aqcpu_fra_init(), initialize FRSH for the calling process
421  *
422  * Must be called before starting using the framework.
423  * No FRSH call will be successful if this routine is not invoked
424  *
425  * Note that no BACKGROUND is created and negotiated and the caller thread
426  * is bound to no BACKGROUND vres, since no way is provided in order of
427  * specifying the contract label and get back the vres id!
428  *
429  * Note also that, since in this implementation the threads/processes with
430  * backgound contracts are left into the default Linux scheduler hands' and
431  * not attached to any AQuoSA server, while we're violating what D-AC2v1
432  * (pag. 14) says, we achieve exactly the same behaviour!!
433  */
434 int aqcpu_fra_init(void)
435 {
436         int rv;
437
438         if ((rv = fra_register(&aqcpu_allocator))) {
439                 qres_cleanup();
440                 return rv;
441         }
442         
443         return 0;
444 }