]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/cpucg/lib/cpucg_fra.c
cd1da37afc7ea2a9a4594f479beede53836331f8
[frescor/frsh.git] / resources / cpucg / lib / cpucg_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 FRESCOR CPUCG (cpu control group)                */
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 #include <ul_log.h>
47 #include <ul_logreg.h>
48 #include <fra_generic.h>
49 #include <cpucg_res.h>
50
51 #include "cpucg_contract.h"
52 #include <stdio.h>
53 #include <libcgroup.h>
54
55 UL_LOG_CUST(ulogd_fra_cpucg);
56 ul_log_domain_t ulogd_fra_cpucg = {UL_LOGL_MSG, "fra_cpucg"};
57 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(fra_cpucg_logreg_domains, ulogd_fra_cpucg);
58
59 static int cpucg_initialized = 0; /* initialization flag */
60
61 #define CGNAME 20
62 /* 
63  * Permissions
64  */
65 static uid_t tuid = 0;
66 static gid_t tgid = 0;
67 static uid_t cuid = 0;
68 static gid_t cgid = 0;
69
70 static int vres_inc = 0;
71
72 /*
73  * Test whether Aquosa Cpu modue is initialized 
74  */
75 static inline int cpucg_is_initialized()
76 {
77         return (cpucg_initialized == 1);
78 }
79
80 static inline char* cpucg_get_name(void)
81 {
82         char* cgname = malloc(CGNAME);
83         snprintf(cgname, CGNAME, "%d", getpid() + vres_inc++);
84         
85         return cgname; 
86 }
87
88 static int cpucg_create_vres(fres_vres_t *vres, void *priv)
89 {
90         cpucg_params_t cpu_params;
91         struct cgroup_controller *cpucntl;
92         struct cgroup* ccg;
93         long int period;
94         long int budget;
95         char* cgroup_name;
96         int ret;
97
98         /* get aqcpu params from contract */
99         get_cpucg_params(vres, &cpu_params);
100         period = cpu_params.P;
101         budget = cpu_params.Q;
102         
103         /* create cpu vres */
104         cgroup_name = cpucg_get_name();
105         ccg = cgroup_new_cgroup(cgroup_name);
106         cgroup_set_uid_gid(ccg, tuid, tgid, cuid, cgid);
107         cpucntl = cgroup_add_controller(ccg, "cpu");
108         printf("To be Created CGCPU VRES(period=%ld us, budget=%ld us)\n",
109                         period, budget);
110         cgroup_add_value_uint64(cpucntl, "cpu.rt_period_us", period);
111         cgroup_add_value_uint64(cpucntl, "cpu.rt_runtime_us", budget);
112         
113         if ((ret = cgroup_create_cgroup(ccg, 1))) {
114                 return ret;
115         }
116         
117         printf("Created CGCPU VRES(period=%lld us, budget=%lld us)\n",
118                         period, budget);
119         vres->priv = ccg;
120         
121         return 0;
122 }
123
124 /*
125  * aqcpu_cancel_vres(), cancels vres 
126  *
127  * The thread bound to the vres are unbound, and so, detached from their
128  * AQuoSA resource reservation servers and continue their execution according
129  * to the standard Linux scheduler policies.
130  *
131  */
132 static int cpucg_cancel_vres(fres_vres_t *vres, void *priv)
133 {
134         struct cgroup *cgroup;
135
136         if (vres->priv) {
137                 errno = -EINVAL;
138                 return -1;
139         }
140         
141         cgroup = (struct cgroup*) vres->priv;
142         /* TODO: Solve migration 
143         cgroup_delete_cgroup(cgroup, 1);*/
144         printf("Canceled CGCPU VRES\n");
145         cgroup_free(&cgroup);   
146         
147         return 0;
148 }
149
150 /* aqcpu_vres_change(), change some parameters of a vres
151  *
152  * In fact, since that AQuoSA call doesn't deal with _its_ Q_min (budget_min
153  * for in FRSH semantic) parameter all the renegotiation will be accepted but
154  * the new temporal behaviour is not guaranteed!
155  * Obviously this is a bug and should/will be corrected in AQuoSA as soon as
156  * possible.
157  *
158  */
159 int cpucg_change_vres(fres_vres_t *vres, void *priv)
160 {
161         struct cgroup *cgroup;
162         cpucg_params_t cpu_params;
163         
164         if (vres->priv) {
165                 errno = -EINVAL;
166                 return -1;
167         }
168         
169         cgroup = (struct cgroup*) vres->priv;
170         
171         /* get aqcpu params from contract */
172         get_cpucg_params(vres, &cpu_params);
173
174         /* TODO:
175          * cgroup->controller[0]->values[0]->value = cpu_params.P;      
176         cgroup->controller[0]->values[1]->value = cpu_params.Q; */
177         
178         ul_logmsg("AQCPU VRES params changed(period=%lld us,"
179                         "budget=%lld us)\n", cpu_params.P, cpu_params.Q);
180
181         return 0;
182 }
183
184 static int fra_CPU_bind_thread
185   (fres_vres_t *vres,
186    const fosa_thread_id_t thread)
187 {
188         struct cgroup* cgroup = (struct cgroup*)vres->priv;
189         struct sched_param param;
190         int p, policy, ret;
191
192         /* if (!aqcpu_is_initialized) {
193          *      return(FRSH_ERR_NOT_INITIALIZED);
194          * }
195          */
196
197         pthread_getschedparam(pthread_self(), &policy, &param);
198         p = param.sched_priority;
199         printf("policy = %d, priority = %d\n", policy, p);
200         if (p == 0) {
201                 param.sched_priority = 1;
202                 pthread_setschedparam(pthread_self(), SCHED_RR, &param);
203                 pthread_getschedparam(pthread_self(), &policy, &param);
204                 printf("changed policy = %d, changed priority = %d\n",
205                         policy, param.sched_priority);
206         }
207
208         if (cgroup_attach_task_pid(cgroup, thread.linux_tid))
209                 goto err;
210
211         return 0;
212 err:
213         return -1;
214 }
215
216 static int fra_CPU_unbind_thread(const fosa_thread_id_t thread)
217 {
218         int ret;
219
220         /* TODO: solve thread -> vres mapping */
221 #if 0
222         char path[FILENAME_MAX];
223         struct cgroup* cgroup = (struct cgroup*)vres->priv;
224         FILE* taskf;
225
226         /* if (!aqcpu_is_initialized) {
227          *      return(FRSH_ERR_NOT_INITIALIZED);
228          * }
229          */
230
231         if (!cg_build_path(cgroup->name, path, cgroup->controller[i]->name)){
232                 return FRSH_ERR_INTERNAL_ERROR;
233         }
234
235         strcat(path, "tasks");
236         taskf = fopen(path, "w");
237         if (!taskf) {
238                 return FRSH_ERR_INTERNAL_ERROR;
239         }
240         fclose(taskf);
241 #endif
242
243         return 0;
244 err:
245         return -1;
246 }
247
248 static int fra_CPU_get_usage
249   (const fres_vres_t *vres,
250    fosa_rel_time_t *spent)
251 {
252         return FRSH_ERR_NOT_IMPLEMENTED;
253 }
254
255 static int fra_CPU_get_job_usage
256   (const fres_vres_t *vres,
257    fosa_rel_time_t *spent)
258 {
259         return FRSH_ERR_NOT_IMPLEMENTED;
260 }
261
262 static int fra_CPU_get_remaining_budget
263   (const fres_vres_t *vres,
264    fosa_rel_time_t *budget)
265 {
266         return FRSH_ERR_NOT_IMPLEMENTED;
267 }
268
269 static
270 int fra_CPU_set_spare_bandwidth(fres_vres_t *vres)
271 {
272         return FRSH_ERR_NOT_IMPLEMENTED;
273 }
274
275 static int fra_CPU_get_desired_budget
276   (fres_vres_t *vres,
277    frsh_rel_time_t *p_budget_out)
278 {
279         return FRSH_ERR_NOT_IMPLEMENTED;
280 }
281
282 static int fra_CPU_set_desired_budget
283   (fres_vres_t *vres,
284    fosa_rel_time_t *p_budget_in)
285 {
286         return FRSH_ERR_NOT_IMPLEMENTED;
287 }
288
289 static int fra_CPU_get_actual_budget
290   (fres_vres_t *vres,
291    fosa_rel_time_t *budget)
292 {
293         return FRSH_ERR_NOT_IMPLEMENTED;
294 }
295
296 int cpucg_fra_activate(forb_orb orb)
297 {
298         if (cpucg_is_initialized()) {
299                 return FRSH_ERR_SYSTEM_ALREADY_INITIALIZED;
300         }
301
302         /* TODO: tets whether cpu controller is mounted */
303         //if (cgroup_init() || !cgroup_test_subsys_mounted("cpu")) {
304         if (cgroup_init()) {
305                 return FRES_ERR_KERNEL_SUPPORT_MISSING;
306         }
307         
308         cpucg_initialized = 1;
309         return 0;
310
311 }
312
313 static struct fres_allocator cpucg_allocator = {
314         .res_type = FRSH_RT_PROCESSOR,
315         .res_id = 0,  /* CPU ID 0 */
316         .create_vres = cpucg_create_vres,
317         .cancel_vres = cpucg_cancel_vres,
318         .change_vres = cpucg_change_vres,
319
320         .bind_thread = fra_CPU_bind_thread,
321         .unbind_thread = fra_CPU_unbind_thread,
322
323         .vres_get_usage = fra_CPU_get_usage,
324         .vres_get_job_usage = fra_CPU_get_job_usage,
325         .vres_get_remaining_budget = fra_CPU_get_remaining_budget,
326
327         .set_spare_bandwidth = fra_CPU_set_spare_bandwidth,
328         .get_desired_budget = fra_CPU_get_desired_budget,
329         .set_desired_budget = fra_CPU_set_desired_budget,
330         .get_actual_budget = fra_CPU_get_actual_budget,
331
332         .activate_callback = cpucg_fra_activate,
333
334         .priv = NULL,
335 };
336
337 /*
338  * cpucg_fra_init()
339  *
340  * initialize FRSH for the calling process
341  *
342  * Must be called before starting using the framework.
343  * No FRSH call will be successful if this routine is not invoked
344  *
345  * Note that no BACKGROUND is created and negotiated and the caller thread
346  * is bound to no BACKGROUND vres, since no way is provided in order of
347  * specifying the contract label and get back the vres id!
348  *
349  * Note also that, since in this implementation the threads/processes with
350  * backgound contracts are left into the default Linux scheduler hands' and
351  * not attached to any AQuoSA server, while we're violating what D-AC2v1
352  * (pag. 14) says, we achieve exactly the same behaviour!!
353  *
354  * possible return values:
355  *  FRSH_NO_ERROR
356  *  FRSH_ERR_ALREADY_INITIALIZED
357  *  FRSH_ERR_INTERNAL_ERROR (something, different from the previous case, gone wrong)
358  */
359 int cpucg_fra_init(void)
360 {
361         int rv;
362
363         if ((rv = fra_register(&cpucg_allocator))) {
364                 return rv;
365         }
366         
367         return 0;
368 }
369
370 int cpucg_fra_exit()
371 {
372         return 0;
373 }