1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of AQCPU (Aquosa CPU) */
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. */
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 /**************************************************************************/
49 * @author Martin Molnar <molnam1@fel.cvut.cz>
50 * @date Wed Feb 18 16:21:01 2009
59 #include <ul_logreg.h>
60 #include <fra_generic.h>
62 #include "aquosa/qres_lib.h"
63 #include "aquosa/qsup_lib.h"
65 #include "aqcpu_contract.h"
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);
71 static int aqcpu_initialized = 0; /* initialization flag */
74 * Test whether Aquosa Cpu modue is initialized
76 static inline int aqcpu_is_initialized()
78 return (aqcpu_initialized == 1);
83 static int aqcpu_create_vres(fres_vres_t *vres, void *priv)
85 aqcpu_params_t cpu_params;
94 /* get aqcpu params from contract */
95 get_aqcpu_params(vres, &cpu_params);
97 rv = qres_create_server(&cpu_params, &sid);
99 return qos_rv_int(rv);
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));
112 * aqcpu_cancel_vres(), cancels vres
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.
119 static int aqcpu_cancel_vres(fres_vres_t *vres, void *priv)
128 memcpy(&sid, vres->priv, sizeof(qres_sid_t));
130 qrv = qres_destroy_server(sid);
132 return qos_rv_int(qrv);
134 printf("Canceled AQCPU VRES(sid=%d)\n",sid);
140 /* aqcpu_vres_change(), change some parameters of a vres
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
150 int aqcpu_change_vres(fres_vres_t *vres, void *priv)
152 aqcpu_params_t cpu_params;
160 memcpy(&sid, vres->priv, sizeof(qres_sid_t));
162 /* get aqcpu params from contract */
163 get_aqcpu_params(vres, &cpu_params);
166 qrv = qres_set_params(sid, &cpu_params);
168 vres->perceived = vres->new;
170 ul_logmsg("AQCPU VRES(sid=%d) params changed(period=%lld us,"
171 "budget=%lld us)\n",sid, cpu_params.P,cpu_params.Q);
177 * installed as an exit handler (with 'atexit()') by the initialization
178 * code... For now it only calls the cleanup function of the AQuoSA
181 static inline void aqcpu_cleanup_wrapper() {
192 return qos_rv_int(rv);
197 int fra_CPU_bind_thread
199 const fosa_thread_id_t thread)
202 qres_sid_t sid = *((qres_sid_t*)vres->priv);
204 if ((qrv = qres_attach_thread(sid, thread.linux_pid,
205 thread.linux_tid)) != QOS_OK)
214 int fra_CPU_unbind_thread(const fosa_thread_id_t thread)
219 if (qres_get_sid(thread.linux_pid, thread.linux_tid, &sid) != QOS_OK)
222 if ((qrv = qres_detach_thread(sid, thread.linux_pid,
223 thread.linux_tid)) != QOS_OK)
232 int fra_CPU_get_usage
233 (const fres_vres_t *vres,
234 fosa_rel_time_t *spent)
236 qres_sid_t sid = *((qres_sid_t*)vres->priv);
237 qres_time_t exec_budget;
241 return FRSH_ERR_BAD_ARGUMENT;
243 rv = qres_get_exec_time(sid, &exec_budget, NULL);
244 if (rv != QOS_OK) return EINVAL;
246 *spent = fosa_usec_to_rel_time(exec_budget);
252 int fra_CPU_get_job_usage
253 (const fres_vres_t *vres,
254 fosa_rel_time_t *spent)
256 qres_sid_t sid = *((qres_sid_t*)vres->priv);
257 qres_params_t q_params;
258 qres_time_t curr_budget;
262 return FRSH_ERR_BAD_ARGUMENT;
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;
269 *spent = fosa_usec_to_rel_time(q_params.Q - curr_budget);
275 int fra_CPU_get_remaining_budget
276 (const fres_vres_t *vres,
277 fosa_rel_time_t *budget)
279 qres_sid_t sid = *((qres_sid_t*)vres->priv);
280 qres_time_t curr_budget;
284 return FRSH_ERR_BAD_ARGUMENT;
286 rv = qres_get_curr_budget(sid, &curr_budget);
287 if (rv != QOS_OK) return EINVAL;
289 *budget = fosa_usec_to_rel_time(curr_budget);
295 int fra_CPU_set_spare_bandwidth(fres_vres_t *vres)
297 qres_sid_t fake_sid = (qres_sid_t) -1;
298 aqcpu_params_t cpu_params;
301 /* get aqcpu params from contract */
302 get_aqcpu_params(vres, &cpu_params);
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);
309 printf("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));
319 int fra_CPU_get_desired_budget
321 frsh_rel_time_t *p_budget_out)
323 qres_sid_t sid = *((qres_sid_t*)vres->priv);
324 qres_params_t q_params;
328 return FRSH_ERR_BAD_ARGUMENT;
330 rv = qres_get_params(sid, &q_params);
331 if (rv != QOS_OK) return EINVAL;
333 *p_budget_out = fosa_usec_to_rel_time(q_params.Q);
339 int fra_CPU_set_desired_budget
341 fosa_rel_time_t *p_budget_in)
343 qres_sid_t sid = *((qres_sid_t*)vres->priv);
344 qres_params_t q_params;
348 return FRSH_ERR_BAD_ARGUMENT;
350 rv = qres_get_params(sid, &q_params);
351 if (rv != QOS_OK) return EINVAL;
353 q_params.Q = fosa_rel_time_to_usec(*p_budget_in);
355 rv = qres_set_params(sid, &q_params);
356 if (rv != QOS_OK) return EINVAL;
362 int fra_CPU_get_actual_budget
364 fosa_rel_time_t *budget)
366 qres_sid_t sid = *((qres_sid_t*)vres->priv);
367 qres_time_t appr_budget;
371 return FRSH_ERR_BAD_ARGUMENT;
373 rv = qres_get_appr_budget(sid, &appr_budget);
374 if (rv != QOS_OK) return EINVAL;
376 *budget = fosa_usec_to_rel_time(appr_budget);
381 int aqcpu_fra_activate(forb_orb orb)
384 if ((qrv = qres_init()) != QOS_OK) {
385 if (qrv == QOS_E_MISSING_COMPONENT) {
386 return FRES_ERR_KERNEL_SUPPORT_MISSING;
388 return FRSH_ERR_INTERNAL_ERROR;
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)) {
395 return(FRSH_ERR_INTERNAL_ERROR);
398 aqcpu_initialized = 1;
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,
410 .bind_thread = fra_CPU_bind_thread,
411 .unbind_thread = fra_CPU_unbind_thread,
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,
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,
422 .activate_callback = aqcpu_fra_activate,
428 * aqcpu_fra_init(), initialize FRSH for the calling process
430 * Must be called before starting using the framework.
431 * No FRSH call will be successful if this routine is not invoked
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!
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!!
442 int aqcpu_fra_init(void)
446 if ((rv = fra_register(&aqcpu_allocator))) {