]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/disk_bfq/lib/diskbfq_fra.c
disk_bfq: Set ioprio directly without regard to the contract
[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         ul_logmsg("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         ul_logmsg("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         ul_logmsg("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->allocated;
158
159         b = fres_contract_get_basic(contract);
160         if (b->contract_type == FRSH_CT_REGULAR)
161                 /* Hack: frm_diskbfq calculates wrong ioprio (-1) - override it here */
162                 ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT | 7;
163         else if (b->contract_type == FRSH_CT_BACKGROUND)
164                  ioprio |= IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT;
165         else
166                 goto err;
167
168         /* ret = ioprio_set(IOPRIO_WHO_PROCESS, gettid(), ioprio); */
169         ret = syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS,
170                       syscall(__NR_gettid), ioprio);
171         if (ret) goto err;
172
173         return 0;
174 err:
175         ul_logerr("ioprio(): %s\n", strerror(errno));
176         return -1;
177 }
178
179 static
180 int fra_DISK_unbind_thread(const fosa_thread_id_t thread)
181 {
182         int ret;
183
184         /* ret = ioprio_set(IOPRIO_WHO_PROCESS, gettid(), IOPRIO_DEFAULT); */
185         ret = syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS,
186                       syscall(__NR_gettid), IOPRIO_PRIO_DEFAULT);
187         if (ret)
188                 return -1;
189
190         return 0;
191 }
192
193 static
194 int fra_DISK_get_usage
195   (const fres_vres_t *vres,
196    fosa_rel_time_t *spent)
197 {
198         return -1;
199 }
200
201 static
202 int fra_DISK_get_job_usage
203   (const fres_vres_t *vres,
204    fosa_rel_time_t *spent)
205 {
206         return -1;
207 }
208
209 static
210 int fra_DISK_get_remaining_budget
211   (const fres_vres_t *vres,
212    fosa_rel_time_t *budget)
213 {
214         return -1;
215 }
216
217 static
218 int fra_DISK_set_spare_bandwidth
219   (fres_vres_t *vres)
220 {
221         return -1;
222 }
223
224 static
225 int fra_DISK_get_desired_budget
226   (fres_vres_t *vres,
227    frsh_rel_time_t *p_budget_out)
228 {
229         return -1;
230 }
231
232 static
233 int fra_DISK_set_desired_budget
234   (fres_vres_t *vres,
235    fosa_rel_time_t *p_budget_in)
236 {
237         return -1;
238 }
239
240 static
241 int fra_DISK_get_actual_budget
242   (fres_vres_t *vres,
243    fosa_rel_time_t *budget)
244 {
245         return -1;
246 }
247
248 static struct fres_allocator diskbfq_allocator = {
249         .res_type = FRSH_RT_DISK,
250         .res_id = 0,  /* DISK ID 0 */
251         .create_vres = diskbfq_create_vres,
252         .cancel_vres = diskbfq_cancel_vres,
253         .change_vres = diskbfq_change_vres,
254
255         .bind_thread = fra_DISK_bind_thread,
256         .unbind_thread = fra_DISK_unbind_thread,
257
258         .vres_get_usage = fra_DISK_get_usage,
259         .vres_get_job_usage = fra_DISK_get_job_usage,
260         .vres_get_remaining_budget = fra_DISK_get_remaining_budget,
261
262         .set_spare_bandwidth = fra_DISK_set_spare_bandwidth,
263         .get_desired_budget = fra_DISK_get_desired_budget,
264         .set_desired_budget = fra_DISK_set_desired_budget,
265         .get_actual_budget = fra_DISK_get_actual_budget,
266
267         .priv = NULL
268 };
269
270 /*
271  * diskbfq_fra_init()
272  *
273  * initialize FRSH for the calling process
274  *
275  * Must be called before starting using the framework.
276  * No FRSH call will be successful if this routine is not invoked
277  *
278  * Note that no BACKGROUND is created and negotiated and the caller thread
279  * is bound to no BACKGROUND vres, since no way is provided in order of
280  * specifying the contract label and get back the vres id!
281  *
282  * Note also that, since in this implementation the threads/processes with
283  * backgound contracts are left into the default Linux scheduler hands' and
284  * not attached to any AQuoSA server, while we're violating what D-AC2v1
285  * (pag. 14) says, we achieve exactly the same behaviour!!
286  *
287  * possible return values:
288  *  FRSH_NO_ERROR
289  *  FRSH_ERR_ALREADY_INITIALIZED
290  *  FRSH_ERR_INTERNAL_ERROR (something, different from the previous case, gone wrong)
291  */
292 int diskbfq_fra_init(void)
293 {
294         if (diskbfq_is_initialized()) {
295                 return -1;
296         }
297
298         fres_block_register_disk_sched();
299         if (fra_register(&diskbfq_allocator)) {
300                 return -1;
301         }
302         
303         diskbfq_initialized = 1;
304         return 0;
305 }
306