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