]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/disk_bfq/lib/diskbfq_fra.c
1c3a7e51581987d0e20e715500c5cbfc4417ba5b
[frescor/frsh.git] / resources / disk_bfq / lib / diskbfq_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
47 /**
48  * @file   diskbfq_fra.c
49  * @author Dario Faggiolir <faggioli@gandalf.sssup.it>
50  * 
51  * @brief
52  *
53  *
54  */
55
56 #include <stdio.h>
57 #include <ul_log.h>
58 #include <ul_logreg.h>
59 #include <fra_generic.h>
60 #include "diskbfq_contract.h"
61 #include "res_disk.h"
62
63 UL_LOG_CUST(ulogd_fra_diskbfq);
64 ul_log_domain_t ulogd_fra_diskbfq = {UL_LOGL_MSG, "fra_diskbfq"};
65 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(fra_diskbfq_logreg_domains, ulogd_fra_diskbfq);
66
67 static int diskbfq_initialized = 0; /* initialization flag */
68
69 /*
70  * Test whether BFQ disk scheduler modue is initialized 
71  */
72 static inline int diskbfq_is_initialized()
73 {
74         return (diskbfq_initialized == 1);
75 }
76
77 static int diskbfq_create_vres(fres_vres_t *vres, void *priv)
78 {
79         fres_block_basic *basic;
80         fres_block_disk_sched *disk_sched;
81         char id[40];
82
83         /*
84          * for disk, reserving the bandwidth does not
85          * make any sense.
86          *
87          * All that we must do is prepare the
88          * vres storing the I/O priority the thread that
89          * will be bound to it will acquire.
90          */
91         fres_contract_id_to_string(id, &vres->id, sizeof(id));
92         basic = fres_contract_get_basic(vres->new);
93         disk_sched = fres_contract_get_disk_sched(vres->new);
94         if (!(vres->priv = malloc(sizeof(int))))
95                 return -1;
96
97         memcpy(vres->priv, &disk_sched->ioprio, sizeof(int));
98         printf("Created BFQ VRES (ioprio=%d budget=%ld ms, period=%ld ms)\n",
99                disk_sched->ioprio,
100                fosa_rel_time_to_msec(basic->budget),
101                fosa_rel_time_to_msec(basic->period));
102
103         return 0;
104 }
105
106 static int diskbfq_cancel_vres(fres_vres_t *vres, void *priv)
107 {
108         if (!vres->priv) {
109                 errno = -EINVAL;
110                 return -1;
111         }
112
113         /*
114          * As before, it is not possible to "free" disk
115          * bandwidth.
116          *
117          * All that we do is free vres memory, the bandwidth
118          * is automatically released when the thread is unbound
119          * from the vres itself.
120          */
121         printf("Canceled DISK VRES (ioprio=%d)\n", *((int*)vres->priv));
122         free(vres->priv);
123
124         return 0;
125 }
126
127 static
128 int diskbfq_change_vres(fres_vres_t *vres, void *priv)
129 {
130         fres_block_disk_sched *disk_sched;
131
132         if (!vres->priv) {
133                 errno = -EINVAL;
134                 return -1;
135         }
136
137         disk_sched = fres_contract_get_disk_sched(vres->new);
138         printf("Changed BFQ VRES from ioprio=%d to ioprio=%d\n",
139                *((int*)vres->priv), (int) disk_sched->ioprio);
140         memcpy(vres->priv, &disk_sched->ioprio, sizeof(int));
141
142         return 0;
143 }
144
145 int diskbfq_fra_exit()
146 {
147         return 0;
148 }
149
150 static
151 int fra_DISK_bind_thread
152   (fres_vres_t *vres,
153    const fosa_thread_id_t thread)
154 {
155         fres_block_basic *b;
156         int ret, ioprio = *((int*)vres->priv);
157         struct fres_contract *contract = vres->perceived;
158
159         b = fres_contract_get_basic(contract);
160         if (b->contract_type == FRSH_CT_REGULAR)
161             ioprio = DISKBFQ_IOPRIO_DEFAULT;
162         else if (b->contract_type == FRSH_CT_BACKGROUND)
163                  ioprio |= IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT;
164         else
165                 goto err;
166
167         /* ret = ioprio_set(IOPRIO_WHO_PROCESS, gettid(), ioprio); */
168         ret = syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS,
169                       syscall(__NR_gettid), ioprio);
170         if (ret) goto err;
171
172         return 0;
173 err:
174         return -1;
175 }
176
177 static
178 int fra_DISK_unbind_thread(const fosa_thread_id_t thread)
179 {
180         int ret;
181
182         /* ret = ioprio_set(IOPRIO_WHO_PROCESS, gettid(), IOPRIO_DEFAULT); */
183         ret = syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS,
184                       syscall(__NR_gettid), IOPRIO_PRIO_DEFAULT);
185         if (ret)
186                 return -1;
187
188         return 0;
189 }
190
191 static
192 int fra_DISK_get_usage
193   (const fres_vres_t *vres,
194    fosa_rel_time_t *spent)
195 {
196         return -1;
197 }
198
199 static
200 int fra_DISK_get_job_usage
201   (const fres_vres_t *vres,
202    fosa_rel_time_t *spent)
203 {
204         return -1;
205 }
206
207 static
208 int fra_DISK_get_remaining_budget
209   (const fres_vres_t *vres,
210    fosa_rel_time_t *budget)
211 {
212         return -1;
213 }
214
215 static
216 int fra_DISK_set_spare_bandwidth
217   (fres_vres_t *vres)
218 {
219         return -1;
220 }
221
222 static
223 int fra_DISK_get_desired_budget
224   (fres_vres_t *vres,
225    frsh_rel_time_t *p_budget_out)
226 {
227         return -1;
228 }
229
230 static
231 int fra_DISK_set_desired_budget
232   (fres_vres_t *vres,
233    fosa_rel_time_t *p_budget_in)
234 {
235         return -1;
236 }
237
238 static
239 int fra_DISK_get_actual_budget
240   (fres_vres_t *vres,
241    fosa_rel_time_t *budget)
242 {
243         return -1;
244 }
245
246 static struct fres_allocator diskbfq_allocator = {
247         .res_type = FRSH_RT_DISK,
248         .res_id = 0,  /* DISK ID 0 */
249         .create_vres = diskbfq_create_vres,
250         .cancel_vres = diskbfq_cancel_vres,
251         .change_vres = diskbfq_change_vres,
252
253         .bind_thread = fra_DISK_bind_thread,
254         .unbind_thread = fra_DISK_unbind_thread,
255
256         .vres_get_usage = fra_DISK_get_usage,
257         .vres_get_job_usage = fra_DISK_get_job_usage,
258         .vres_get_remaining_budget = fra_DISK_get_remaining_budget,
259
260         .set_spare_bandwidth = fra_DISK_set_spare_bandwidth,
261         .get_desired_budget = fra_DISK_get_desired_budget,
262         .set_desired_budget = fra_DISK_set_desired_budget,
263         .get_actual_budget = fra_DISK_get_actual_budget,
264
265         .priv = NULL
266 };
267
268 /*
269  * diskbfq_fra_init()
270  *
271  * initialize FRSH for the calling process
272  *
273  * Must be called before starting using the framework.
274  * No FRSH call will be successful if this routine is not invoked
275  *
276  * Note that no BACKGROUND is created and negotiated and the caller thread
277  * is bound to no BACKGROUND vres, since no way is provided in order of
278  * specifying the contract label and get back the vres id!
279  *
280  * Note also that, since in this implementation the threads/processes with
281  * backgound contracts are left into the default Linux scheduler hands' and
282  * not attached to any AQuoSA server, while we're violating what D-AC2v1
283  * (pag. 14) says, we achieve exactly the same behaviour!!
284  *
285  * possible return values:
286  *  FRSH_NO_ERROR
287  *  FRSH_ERR_ALREADY_INITIALIZED
288  *  FRSH_ERR_INTERNAL_ERROR (something, different from the previous case, gone wrong)
289  */
290 int diskbfq_fra_init(void)
291 {
292         if (diskbfq_is_initialized()) {
293                 return -1;
294         }
295
296         fres_block_register_disk_sched();
297         if (fra_register(&diskbfq_allocator)) {
298                 return -1;
299         }
300         
301         diskbfq_initialized = 1;
302         return 0;
303 }
304