]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - resources/dummy/fra_dummy.c
dummy: Enable use of multiple dummy resources
[frescor/frsh.git] / resources / dummy / fra_dummy.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 FRSH (FRescor ScHeduler)                         */
26 /*                                                                        */
27 /* FRSH 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.  FRSH 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 FRSH; 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 FRSH header files in a file,         */
39 /* instantiating FRSH generics or templates, or linking other files       */
40 /* with FRSH 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   fra_dummy.c
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Wed Feb 18 16:06:34 2009
51  * 
52  * @brief  Dummy resource allocator implementation
53  * 
54  * 
55  */
56
57
58 #include <ul_log.h>
59 #include <ul_logreg.h>
60 #include <fra_generic.h>
61 #include "res_dummy.h"
62 #include <stdio.h>
63 #include "dummy_config.h"
64
65
66 UL_LOG_CUST(ulogd_frs_dummy);
67 ul_log_domain_t ulogd_fra_dummy = {UL_LOGL_MSG, "fra_dummy"};
68 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(fra_dummy_logreg_domains, ulogd_fra_dummy);
69
70 struct dummy_vres_priv {
71         int num;
72 };
73
74 static int create_vres(fres_vres_t *vres, void *priv)
75 {
76         int *my_data = priv;
77         struct dummy_vres_priv *vres_priv;
78         fres_block_basic *basic;
79         fres_block_dummy_sched *dummy_sched;
80         char id[40];
81         
82         (*my_data)++;
83
84         fres_contract_id_to_string(id, &vres->id, sizeof(id));
85         basic = fres_contract_get_basic(vres->new);
86         dummy_sched = fres_contract_get_dummy_sched(vres->new);
87         vres_priv = malloc(sizeof(vres_priv));
88         vres_priv->num = *my_data;
89         vres->priv = vres_priv;
90 #ifdef CONFIG_RESOURCE_DUMMY_VERBOSE
91         printf("Creating VRes #%d (id=%s, period=%ld ms, budget=%ld ms, priority=%d)\n",
92                vres_priv->num, id,
93                fosa_rel_time_to_msec(basic->period),
94                fosa_rel_time_to_msec(basic->budget),
95                dummy_sched ? dummy_sched->priority : -1);
96 #endif
97         return 0;
98 }
99
100 static int cancel_vres(fres_vres_t *vres, void *priv)
101 {
102         int *my_data = priv;
103         fres_block_basic *basic;
104         fres_block_dummy_sched *dummy_sched;
105         char id[40];
106
107         fres_contract_id_to_string(id, &vres->id, sizeof(id));
108         basic = fres_contract_get_basic(vres->allocated);
109         dummy_sched = fres_contract_get_dummy_sched(vres->allocated);
110 #ifdef CONFIG_RESOURCE_DUMMY_VERBOSE    
111         struct dummy_vres_priv *vres_priv = vres->priv;
112         printf("Canceling VRes #%d (id=%s, period=%ld ms, budget=%ld ms, priority=%d)\n",
113                vres_priv->num, id,
114                fosa_rel_time_to_msec(basic->period),
115                fosa_rel_time_to_msec(basic->budget),
116                dummy_sched ? dummy_sched->priority : 999);
117 #endif
118         (*my_data)--;
119         return 0;
120 }
121
122 static int change_vres(fres_vres_t *vres, void *priv)
123 {
124         fres_block_basic *basic[2];
125         fres_block_dummy_sched *dummy_sched[2];
126         char id[40];
127
128         fres_contract_id_to_string(id, &vres->id, sizeof(id));
129         basic[0] = fres_contract_get_basic(vres->allocated);
130         basic[1] = fres_contract_get_basic(vres->new);
131         dummy_sched[0] = fres_contract_get_dummy_sched(vres->allocated);
132         dummy_sched[1] = fres_contract_get_dummy_sched(vres->new);
133
134 #ifdef CONFIG_RESOURCE_DUMMY_VERBOSE    
135         struct dummy_vres_priv *vres_priv = vres->priv;
136         printf("Changing VRes #%d (id=%s, period: %ld -> %ld ms, budget: %ld -> %ld ms, priority: %d -> %d)\n",
137                vres_priv->num, id,
138                fosa_rel_time_to_msec(basic[0]->period),
139                fosa_rel_time_to_msec(basic[1]->period),
140                fosa_rel_time_to_msec(basic[0]->budget),
141                fosa_rel_time_to_msec(basic[1]->budget),
142                dummy_sched[0] ? dummy_sched[0]->priority : 999,
143                dummy_sched[1] ? dummy_sched[1]->priority : 999);
144 #endif
145
146         vres->perceived = vres->new;
147
148         return 0;
149 }
150
151
152
153 static int my_data;
154
155 static struct fres_allocator dummy_allocator = {
156         .res_type = DUMMY_RESOURCE_TYPE,
157         .res_id = DUMMY_RESOURCE_ID,
158         /* Here we are using the "simple interface" */
159         .create_vres = create_vres,
160         .cancel_vres = cancel_vres,
161         .change_vres = change_vres,
162         .priv = &my_data
163 };
164
165 int fra_dummy_init(void)
166 {
167         fres_block_register_dummy();
168         return fra_register(&dummy_allocator);
169 }
170
171 /** 
172  * Initializes addition dummy resource allocator.
173  *
174  * This is intended for the use in automatic tests which need to work
175  * with multiple resources. The test can initialize several dummy
176  * resources, not just the default one with id 255.
177  * 
178  * @param id 
179  * 
180  * @return Zero on success, non-zero error code on error.
181  */
182 int fra_dummy_init_and_activate_id(frsh_resource_id_t res_id)
183 {
184         struct fres_allocator *fra;
185         int ret;
186         fres_block_register_dummy();
187         fra = malloc(sizeof(*fra));
188         if (!fra)
189                 goto err;
190         *fra = dummy_allocator;
191         fra->res_id = res_id;
192         fra->priv = malloc(sizeof(int));
193         ret = fra_register(fra);
194         if (ret)
195                 goto err;
196         ret = fra_activate(fra->res_type, fra->res_id);
197         return ret;
198 err:
199         return -1;
200 }