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