]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/cpu_aquosa/lib/aqcpu_fra.c
Allow specification of the local CPU ID using environment variable
[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         ul_logdeb("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         ul_logdeb("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         vres->perceived = vres->new;
169         
170         ul_logmsg("AQCPU VRES(sid=%d) params changed(period=%lld us,"
171                         "budget=%lld us)\n",sid, cpu_params.P,cpu_params.Q);
172
173         return 0;
174 }
175
176 /*
177  * installed as an exit handler (with 'atexit()') by the initialization
178  * code... For now it only calls the cleanup function of the AQuoSA
179  * Framework
180  */
181 static inline void aqcpu_cleanup_wrapper() {
182         qres_cleanup();
183 }
184
185 #if 0
186 static
187 int aqcpu_fra_exit()
188 {
189         qos_rv rv;
190
191         rv = qres_cleanup();
192         return qos_rv_int(rv);
193 }
194 #endif
195
196 static
197 int fra_CPU_bind_thread
198   (fres_vres_t *vres,
199    const fosa_thread_id_t thread)
200 {
201         qos_rv  qrv;
202         qres_sid_t sid = *((qres_sid_t*)vres->priv);
203
204         if ((qrv = qres_attach_thread(sid, thread.linux_pid,
205             thread.linux_tid)) != QOS_OK)
206                 goto err;
207
208         return 0;
209 err:
210         return -1;
211 }
212
213 static
214 int fra_CPU_unbind_thread(const fosa_thread_id_t thread)
215 {
216         qos_rv  qrv;
217         qres_sid_t  sid;
218
219         if (qres_get_sid(thread.linux_pid, thread.linux_tid, &sid) != QOS_OK)
220                 goto err;
221
222         if ((qrv = qres_detach_thread(sid, thread.linux_pid,
223             thread.linux_tid)) != QOS_OK)
224                 goto err;
225
226         return 0;
227 err:
228         return -1;
229 }
230
231 static
232 int fra_CPU_get_usage
233   (const fres_vres_t *vres,
234    fosa_rel_time_t *spent)
235 {
236         qres_sid_t sid = *((qres_sid_t*)vres->priv);
237         qres_time_t exec_budget;
238         qos_rv rv;
239
240         if (!spent)
241                 return FRSH_ERR_BAD_ARGUMENT;
242
243         rv = qres_get_exec_time(sid, &exec_budget, NULL);
244         if (rv != QOS_OK) return EINVAL;
245
246         *spent = fosa_usec_to_rel_time(exec_budget);
247
248         return 0;
249 }
250
251 static
252 int fra_CPU_get_job_usage
253   (const fres_vres_t *vres,
254    fosa_rel_time_t *spent)
255 {
256         qres_sid_t sid = *((qres_sid_t*)vres->priv);
257         qres_params_t q_params;
258         qres_time_t curr_budget;
259         qos_rv rv;
260
261         if (!spent)
262                 return FRSH_ERR_BAD_ARGUMENT;
263
264         rv = qres_get_params(sid, &q_params);
265         if (rv != QOS_OK) return EINVAL;
266         rv = qres_get_curr_budget(sid, &curr_budget);
267         if (rv != QOS_OK) return EINVAL;
268
269         *spent = fosa_usec_to_rel_time(q_params.Q - curr_budget);
270
271         return 0;
272 }
273
274 static
275 int fra_CPU_get_remaining_budget
276   (const fres_vres_t *vres,
277    fosa_rel_time_t *budget)
278 {
279         qres_sid_t sid = *((qres_sid_t*)vres->priv);
280         qres_time_t curr_budget;
281         qos_rv rv;
282
283         if (!budget)
284                 return FRSH_ERR_BAD_ARGUMENT;
285
286         rv = qres_get_curr_budget(sid, &curr_budget);
287         if (rv != QOS_OK) return EINVAL;
288
289         *budget = fosa_usec_to_rel_time(curr_budget);
290
291         return 0;
292 }
293
294 static
295 int fra_CPU_set_spare_bandwidth(fres_vres_t *vres)
296 {
297         qres_sid_t fake_sid = (qres_sid_t) -1;
298         aqcpu_params_t cpu_params;
299         qos_rv rv;
300
301         /* get aqcpu params from contract */
302         get_aqcpu_params(vres, &cpu_params);
303
304         
305         /* reserve CPU bandwidth for feedback */
306         rv = qsup_reserve_spare(r2bw(cpu_params.Q, cpu_params.P));
307         if (rv != QOS_OK) return qos_rv_int(rv);
308
309         ul_logdeb("Created AQCPU spare (period=%lld us, budget=%lld us)\n",
310                         cpu_params.P, cpu_params.Q);
311         if ((vres->priv = malloc(sizeof(qres_sid_t)))) {
312                 memcpy(vres->priv, &fake_sid, sizeof(qres_sid_t));
313         }
314
315         return 0;
316 }
317
318 static
319 int fra_CPU_get_desired_budget
320   (fres_vres_t *vres,
321    frsh_rel_time_t *p_budget_out)
322 {
323         qres_sid_t sid = *((qres_sid_t*)vres->priv);
324         qres_params_t q_params;
325         qos_rv rv;
326
327         if (!p_budget_out)
328                 return FRSH_ERR_BAD_ARGUMENT;
329
330         rv = qres_get_params(sid, &q_params);
331         if (rv != QOS_OK) return EINVAL;
332
333         *p_budget_out = fosa_usec_to_rel_time(q_params.Q);
334
335         return 0;
336 }
337
338 static
339 int fra_CPU_set_desired_budget
340   (fres_vres_t *vres,
341    fosa_rel_time_t *p_budget_in)
342 {
343         qres_sid_t sid = *((qres_sid_t*)vres->priv);
344         qres_params_t q_params;
345         qos_rv rv;
346
347         if (!p_budget_in)
348                 return FRSH_ERR_BAD_ARGUMENT;
349
350         rv = qres_get_params(sid, &q_params);
351         if (rv != QOS_OK) return EINVAL;
352
353         q_params.Q = fosa_rel_time_to_usec(*p_budget_in);
354
355         rv = qres_set_params(sid, &q_params);
356         if (rv != QOS_OK) return EINVAL;
357
358         return 0;
359 }
360
361 static
362 int fra_CPU_get_actual_budget
363   (fres_vres_t *vres,
364    fosa_rel_time_t *budget)
365 {
366         qres_sid_t sid = *((qres_sid_t*)vres->priv);
367         qres_time_t appr_budget;
368         qos_rv rv;
369
370         if (!budget)
371                 return FRSH_ERR_BAD_ARGUMENT;
372
373         rv = qres_get_appr_budget(sid, &appr_budget);
374         if (rv != QOS_OK) return EINVAL;
375
376         *budget = fosa_usec_to_rel_time(appr_budget);
377
378         return 0;
379 }
380
381 int aqcpu_fra_activate(forb_orb orb)
382 {
383         qos_rv qrv;
384         if ((qrv = qres_init()) != QOS_OK) {
385                 if (qrv == QOS_E_MISSING_COMPONENT) {
386                         return FRES_ERR_KERNEL_SUPPORT_MISSING;
387                 } else
388                         return FRSH_ERR_INTERNAL_ERROR;
389         }
390
391         /* install the cleanup function of th AQuoSA framework as an exit
392          * handler function (quite futile but, for now, it's sufficent) */
393         if (atexit(aqcpu_cleanup_wrapper)) {
394                 qres_cleanup();
395                 return(FRSH_ERR_INTERNAL_ERROR);
396         }
397
398         aqcpu_initialized = 1;
399
400         return 0;
401 }
402
403 static struct fres_allocator aqcpu_allocator = {
404         .res_type = FRSH_RT_PROCESSOR,
405         .res_id = FRSH_CPU_ID_DEFAULT,  /* CPU ID 0 */
406         .create_vres = aqcpu_create_vres,
407         .cancel_vres = aqcpu_cancel_vres,
408         .change_vres = aqcpu_change_vres,
409
410         .bind_thread = fra_CPU_bind_thread,
411         .unbind_thread = fra_CPU_unbind_thread,
412
413         .vres_get_usage = fra_CPU_get_usage,
414         .vres_get_job_usage = fra_CPU_get_job_usage,
415         .vres_get_remaining_budget = fra_CPU_get_remaining_budget,
416
417         .set_spare_bandwidth = fra_CPU_set_spare_bandwidth,
418         .get_desired_budget = fra_CPU_get_desired_budget,
419         .set_desired_budget = fra_CPU_set_desired_budget,
420         .get_actual_budget = fra_CPU_get_actual_budget,
421
422         .activate_callback = aqcpu_fra_activate,
423         
424         .priv = NULL
425 };
426
427 /*
428  * aqcpu_fra_init(), initialize FRSH for the calling process
429  *
430  * Must be called before starting using the framework.
431  * No FRSH call will be successful if this routine is not invoked
432  *
433  * Note that no BACKGROUND is created and negotiated and the caller thread
434  * is bound to no BACKGROUND vres, since no way is provided in order of
435  * specifying the contract label and get back the vres id!
436  *
437  * Note also that, since in this implementation the threads/processes with
438  * backgound contracts are left into the default Linux scheduler hands' and
439  * not attached to any AQuoSA server, while we're violating what D-AC2v1
440  * (pag. 14) says, we achieve exactly the same behaviour!!
441  */
442 int aqcpu_fra_init(void)
443 {
444         int rv;
445
446         aqcpu_allocator.res_id = frsh_get_local_cpu_id();
447                 
448         if ((rv = fra_register(&aqcpu_allocator))) {
449                 return rv;
450         }
451         
452         return 0;
453 }