1 /**************************************************************************/
2 /* ---------------------------------------------------------------------- */
3 /* Copyright (C) 2006 - 2008 FRESCOR consortium partners: */
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 */
12 /* Thales Communication S.A. FRANCE */
13 /* Visual Tools S.A. SPAIN */
14 /* Rapita Systems Ltd UK */
17 /* See http://www.frescor.org for a link to partners' websites */
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. */
25 /* This file is part of FRSH (FRescor ScHeduler) */
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. */
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 /**************************************************************************/
49 * @author Michal Sojka <sojkam1@fel.cvut.cz>
50 * @date Tue Nov 11 08:34:34 2008
52 * @brief Generic resource manager implementation.
56 #include <frm_generic.h>
59 #include <fres_sa_scenario.h>
62 UL_LOG_CUST(ulogd_frm);
63 ul_log_domain_t ulogd_frm = {UL_LOGL_MSG, "frm"};
65 #define object_to_frm(o) (struct frm_data*)forb_instance_data(o)
66 #define save_errno(cmd) do { int _e = errno; cmd; errno = _e; } while(0)
68 void fres_sa_scenario_reserve_new(struct fres_sa_scenario *scenario)
70 struct fres_sa_contract *c;
71 fres_sa_scenario_for_each_contract(scenario, c) {
74 fres_contract_destroy(c->reserved);
82 void fres_sa_scenario_rollback(struct fres_sa_scenario *scenario)
84 struct fres_sa_contract *c, *c_next;
86 /* Deleteion safe scenario traverse */
87 for(c=fres_sa_scenario_contract_first(scenario),
88 c_next=c?fres_sa_scenario_contract_next(scenario,c):NULL;
90 c=c_next,c_next=c?fres_sa_scenario_contract_next(scenario,c):NULL) {
92 if (c->committed || c->reserved) {
93 fres_contract_destroy(c->new);
95 c->contract = c->reserved ? c->reserved : c->committed;
97 fres_sa_scenario_del_contract(scenario, c);
98 fres_sa_contract_destroy(c);
105 static CORBA_long reserve_contracts(fres_resource_manager obj,
106 const fres_contract_ptr_seq* contracts,
107 CORBA_Environment *ev)
109 struct frm_data *frm = object_to_frm(obj);
110 struct fres_sa_scenario *scenario = frm->scenario;
111 bool schedulable =false;
113 struct fres_sa_contract *c;
115 ul_logmsg("reserve_contracts\n");
117 for (i=0; i<contracts->_length; i++) {
118 struct fres_contract *cin = contracts->_buffer[i];
119 c = fres_sa_scenario_find_contract(scenario, &cin->id);
121 c = fres_sa_contract_new();
124 fres_sa_scenario_add_contract(scenario, c);
128 assert(c->new == NULL);
129 c->new = fres_contract_duplicate(cin);
130 c->contract = c->new;
131 if (!c->new) goto err;
134 ret = frm->desc->admission_test(scenario, frm->desc->priv, &schedulable);
136 ul_logerr("admission_test failed: %d\n", ret);
141 fres_sa_scenario_reserve_new(scenario);
143 fres_sa_scenario_rollback(scenario);
145 return schedulable ? 0 : 1;
147 fres_sa_scenario_rollback(scenario);
151 static void commit_contracts(fres_resource_manager obj,
152 const fres_contract_id_seq* ids,
153 fres_contract_ptr_seq** contracts_with_scheduling_data,
154 CORBA_Environment *ev)
156 struct frm_data *frm = object_to_frm(obj);
158 struct fres_sa_contract *c;
159 fres_contract_ptr_seq *contracts;
161 ul_logmsg("commit_contracts\n");
163 contracts = forb_malloc(sizeof(*contracts));
165 ev->major = FORB_EX_NO_MEMORY;
169 contracts->_buffer = CORBA_sequence_fres_contract_ptr_allocbuf(num);
170 CORBA_sequence_set_release(contracts, CORBA_TRUE);
171 contracts->_maximum = contracts->_length = num;
173 /* TODO: Add also the changed contracts (e.g. because of
174 * priorities). Question: How to recognize which contracts are
175 * changed because of this commit? */
176 for (i=0; i < num; i++) {
177 c = fres_sa_scenario_find_contract(frm->scenario, &ids->_buffer[i]);
178 if (c && c->reserved) {
179 if (c->committed) fres_contract_destroy(c->committed);
180 c->committed = c->reserved;
182 contracts->_buffer[i] = fres_contract_duplicate(c->committed);
184 contracts->_buffer[i] = NULL;
185 if (!c) ul_logerr("Commit to unknown contract ID\n");
186 else if (!c->reserved) ul_logerr("Commit to not reserved contract\n");
190 *contracts_with_scheduling_data = contracts;
194 static void cancel_contracts(fres_resource_manager obj,
195 const fres_contract_id_seq* ids,
196 CORBA_Environment *ev)
199 struct frm_data *frm = object_to_frm(obj);
201 ul_logmsg("cancel_contracts\n");
203 for (i=0; i<ids->_length; i++) {
204 struct fres_sa_contract *c;
205 c = fres_sa_scenario_find_contract(frm->scenario, &ids->_buffer[i]);
208 fres_sa_scenario_del_contract(frm->scenario, c);
209 fres_sa_contract_destroy(c);
214 static void get_contracts(fres_resource_manager obj,
215 fres_contract_ptr_seq** contracts_out,
216 CORBA_long* utilization, CORBA_Environment *ev)
218 struct frm_data *frm = object_to_frm(obj);
219 struct fres_sa_contract *c;
220 fres_contract_ptr_seq *contracts;
222 contracts = forb_malloc(sizeof(*contracts));
224 ev->major = FORB_EX_NO_MEMORY;
227 contracts->_buffer = CORBA_sequence_fres_contract_ptr_allocbuf(frm->scenario->num_contracts);
228 CORBA_sequence_set_release(contracts, CORBA_TRUE);
229 contracts->_maximum = frm->scenario->num_contracts;
230 contracts->_length = 0;
232 fres_sa_scenario_for_each_contract(frm->scenario, c) {
234 int i = contracts->_length;
235 contracts->_buffer[i] = fres_contract_duplicate(c->committed);
236 contracts->_length++;
240 *contracts_out = contracts;
241 *utilization = frm->scenario->utilization;
246 static const struct forb_fres_resource_manager_impl frm_impl = {
247 .reserve_contracts = reserve_contracts,
248 .commit_contracts = commit_contracts,
249 .cancel_contracts = cancel_contracts,
250 .get_contracts = get_contracts,
254 * Initializes generic resource manager. The only thing a
255 * caller has to supply is admission test function, which is passed in
256 * @a frm_data->admission_test.
258 * @param orb FORB object used to communicate with other components.
259 * @param[out] frm_data Pointer to frm_data structure
260 * @param executor Executor used for this resource manager.
261 * @param desc Description on the resource manager
263 * @return Resource manager object reference of NULL in case of error.
265 fres_resource_manager frm_register(forb_orb orb, struct frm_data *frm_data,
266 forb_executor_t *executor,
267 const struct fres_res_manager *desc)
269 fres_contract_broker fcb;
270 fres_resource_desc res_desc;
271 fres_resource_manager frm;
275 memset(frm_data, 0, sizeof(*frm_data));
276 frm_data->desc = desc;
277 frm_data->scenario = fres_sa_scenario_new();
278 if (!frm_data->scenario) {
279 save_errno(ul_logerr("fres_sa_scenario_new failed\n"));
283 fcb = forb_resolve_reference(orb, fres_contract_broker_reg_name);
285 save_errno(ul_logerr("Could not find contract broker\n"));
289 frm = forb_fres_resource_manager_new(orb, &frm_impl, frm_data);
291 save_errno(ul_logerr("forb_fres_resource_manager_new error\n"));
292 goto err_release_fcb;
295 ret = forb_executor_register_object(executor, frm);
297 save_errno(ul_logerr("forb_executor_register_object failed\n"));
301 /* Register resource manager */
302 res_desc.manager = frm;
303 ret = fres_contract_broker_register_resource(fcb,
307 if (forb_exception_occurred(&env) || ret != 0) {
308 save_errno(ul_logerr("fres_contract_broker_register_resource exception\n"));
312 forb_object_release(fcb);
315 forb_executor_unregister_object(executor, frm);
317 forb_object_release(frm);
319 forb_object_release(fcb);
325 * Initializes and runs a generic resource manager. The only thing a
326 * caller has to supply is admission test function, which is passed in
327 * @a frm_data->admission_test.
329 * @param orb FORB object used to communicate with other components.
330 * @param desc Description on the resource manager
334 int frm_register_and_run(forb_orb orb, const struct fres_res_manager *desc)
336 fres_resource_manager frm;
337 struct frm_data frm_data;
338 forb_executor_t executor;
341 /* Prepare executor before we register the resource manager
342 * with contract broker */
343 ret = forb_executor_init(&executor);
345 save_errno(ul_logerr("forb_executor_init failed\n"));
349 frm = frm_register(orb, &frm_data, &executor, desc);
352 save_errno(ul_logerr("frm_register failed\n"));
353 goto err_destroy_executor;
356 /* Start request processing */
357 ul_logmsg("Waiting for requests\n");
358 ret = forb_executor_run(&executor);
359 if (ret) goto err_release_frm;
362 forb_object_release(frm);
363 err_destroy_executor:
364 forb_executor_destroy(&executor);