]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - fres/resalloc/fra_generic.h
Fixed race condition in allocator registration
[frescor/frsh.git] / fres / resalloc / fra_generic.h
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_generic.h
49  * @author Michal Sojka <sojkam1@fel.cvut.cz>
50  * @date   Mon Nov  3 15:32:54 2008
51  * 
52  * @brief  FORB interface of FRES Resource Allocator
53  * 
54  * 
55  */
56 #ifndef FRA_GENERIC_H
57 #define FRA_GENERIC_H
58 #include <fcb.h>
59 #include <fra.h>
60 #include <fres_contract.h>
61 #include <ul_gavlcust.h>
62 #include <fosa.h>
63 #include <forb/executor.h>
64 #include <fres_contract_idl.h>
65 #include <fres_vres.h>
66
67 /** Registry of all virtual resources in an application. */
68 struct fres_vreses {
69         fosa_mutex_t mutex;     /**< Mutex for manipulation with vreses tree. If all allocators are executed from one executor, locking can be avoided. */
70         gavl_cust_root_field_t vreses; /**< Container of all virtual resources */
71 };
72
73 GAVL_CUST_NODE_INT_DEC(fres_vreses_nolock /* cust_prefix */,
74                        struct fres_vreses /* cust_root_t */,
75                        struct fres_vres   /* cust_item_t */,
76                        fres_contract_id_t /* cust_key_t */,
77                        vreses             /* cust_root_node */,
78                        node               /* cust_item_node */,
79                        id                 /* cust_item_key */,
80                        fres_contract_id_cmp /* cust_cmp_fnc */)
81
82 extern struct fres_vreses fres_vreses;
83
84 struct fres_vres *
85 fres_vreses_find_label(fres_block_label *label, fres_block_resource *res);
86
87 /** Registry of all threads in an application that are bound to a VRES. */
88 struct fres_threads_vres {
89         fosa_mutex_t mutex;     /**< Mutex for manipulation with vreses tree. If all allocators are executed from one executor, locking can be avoided. */
90         gavl_cust_root_field_t threads; /**< Container of all virtual resources */
91 };
92
93 GAVL_CUST_NODE_INT_DEC(fres_threads_vres_nolock /* cust_prefix */,
94                        struct fres_threads_vres /* cust_root_t */,
95                        struct fres_thread_vres  /* cust_item_t */,
96                        fres_thread_vres_key_t   /* cust_key_t */,
97                        threads                  /* cust_root_node */,
98                        node                     /* cust_item_node */,
99                        thread_vres              /* cust_item_key */,
100                        fres_thread_vres_cmp     /* cust_cmp_fnc */)
101
102 extern struct fres_threads_vres fres_threads_vres;
103
104
105 /**
106  * Structure describing FRES allocator. It is used as a parameter to
107  * fra_register(), which registeres this allocator with contract
108  * broker and provides FORB interface to the real allocator.
109  * 
110  */
111 struct fres_allocator {
112         frsh_resource_type_t res_type; /**< Resource type */
113         frsh_resource_id_t res_id;     /**< Resource ID */
114         /** @name Simple interface
115          * The allocator cannot influence the order of applying changes. */
116         /*@{*/
117         /** 
118          * Should create the VRES according to the parameters in
119          * fres_vres::new.
120          * 
121          * @param vres VRES to create.
122          * @param priv User supplied pointer registered by fra_register()
123          * 
124          * @return Zero in case of success, nonzero error code on error.
125          */
126         int (*create_vres)(fres_vres_t *vres, void *priv);
127         /** 
128          * Should change the already crated VRES according to the
129          * parameters in fres_vres::new. The currenlty allocated
130          * parameters are in fres_vres::allocated. This function
131          * should change the fres_vres::perceived to @a fres_vres::new
132          * at some point in time (depending on the kind of the change).
133          * The fres_vres::allocated parameters will be freed after
134          * the return of this function and then fres_vres::allocated
135          * will be set to fres_vres::new.
136          * 
137          * @param vres VRES to change.
138          * @param priv User supplied pointer registered by fra_register()
139          * 
140          * @return Zero in case of success, nonzero error code on error.
141          */
142         int (*change_vres)(fres_vres_t *vres, void *priv);
143         /** 
144          * Cancel the CRES
145          * 
146          * @param cancel_vres VRES to cancel
147          * 
148          * @return Zero on succaess, non-zero error code on error.
149          */
150         int (*cancel_vres)(fres_vres_t *vres, void *priv);
151         /*@}*/
152
153         /** @name Full interface
154          * 
155          * Full interface provides a way for the allocator to
156          * influence the order of applying changes.
157          */
158         /*@{*/
159         /**
160          * A more general (and more compilcated) allocator
161          * interface. If this field is non-NULL, the simple interface
162          * is not used.
163          *
164          * The advantage of this method is that it can apply the
165          * changes in whatever order and even in parallel. The new
166          * version of VRES parameters is stored in fres_vres::new,
167          * whereas the previous version (if any) is stored in
168          * fres_vres::allocated. This function should change the
169          * fres_vres::perceived to @a fres_vres::new at some point in
170          * time (depending on the kind of the change) as the
171          * fres_vres::allocated parameters will be freed after the
172          * return of this function. When the fres_vres::new contains
173          * no blocks, the VRES should be canceled.
174          * 
175          * @param vreses Array of pointers to VRESes.
176          * @param length The number of elements in @a vreses.
177          * @param priv Value of @a priv field in this structure.
178          * 
179          * @return Zero in case of success, non-zero error code otherwise.
180          */
181         int (*apply_vres_changes)(fres_vres_t *vreses[], unsigned length, void *priv);
182         /*@}*/
183
184         /** @name Feedback module support */
185         /*@{*/
186         int (*set_spare_bandwidth)(fres_vres_t *vres);
187         int (*get_desired_budget)(fres_vres_t *vres, frsh_rel_time_t *p_budget_out);
188         int (*set_desired_budget)(fres_vres_t *vres_id, frsh_rel_time_t *p_budget_in);
189         int (*get_actual_budget)(fres_vres_t *vres_id, frsh_rel_time_t *budget);
190         /*@}*/
191
192         int (*bind_thread)(fres_vres_t *vres, fosa_thread_id_t thread);
193         int (*unbind_thread)(fosa_thread_id_t thread);
194
195         int (*vres_get_job_usage)(const fres_vres_t *vres, frsh_rel_time_t *spent);
196         int (*vres_get_remaining_budget)(const fres_vres_t *vres, frsh_rel_time_t *budget);
197         int (*vres_get_usage)(const fres_vres_t *vres, frsh_rel_time_t *spent);
198
199         void *priv;             /**< Pointer to allocator's private data */
200
201         /**
202          * A callback called whenever the resource allocator is to be
203          * activated by fra_activate().
204          */
205         int (*activate_callback)(forb_orb orb);
206 };
207
208 fres_resource_allocator fra_new(forb_orb orb,
209                                 forb_executor_t *executor,
210                                 struct fres_allocator *allocator);
211
212 int fra_registry_init(forb_orb             orb,
213                       fres_contract_broker fcb,
214                       forb_executor_t      *executor);
215 int fra_register(struct fres_allocator *allocator);
216 int fra_activate(frsh_resource_type_t res_type,
217                  frsh_resource_id_t   res_id);
218 struct fres_allocator *fra_get(frsh_resource_type_t res_type,
219                                frsh_resource_id_t   res_id);
220
221 fres_vres_t *fra_get_vres(fres_contract_id_t *id);
222
223 int fra_insert_thread_vres(const fosa_thread_id_t *id,
224                            int vres_type,
225                            fres_vres_t *vres);
226 int fra_delete_thread_vres(const fosa_thread_id_t *id,
227                            int vres_type);
228 fres_thread_vres_t *fra_get_thread_vres(const fosa_thread_id_t *id,
229                                         int vres_type);
230 fres_vres_t *fra_get_vres_thread_vres(const fosa_thread_id_t *id,
231                                       int vres_type);
232
233 #endif
234