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 Mon Oct 20 18:00:18 2008
52 * @brief FRESCOR Contract Broker
56 #include <semaphore.h>
59 #include <forb/config.h>
61 #include <fcb_contact_info.h>
65 #include <ul_gavlcust.h>
68 #include <ul_logreg.h>
69 #include <forb/server_id.h>
70 #include <fres_contract.h>
71 #include "fcb_config.h"
72 #include <fosa_clocks_and_timers.h>
73 #include "contract_log.h"
74 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
75 #include <forb/proto_inet.h>
78 UL_LOG_CUST(ulogd_fcb);
79 ul_log_domain_t ulogd_fcb = {UL_LOGL_MSG, "main"};
80 UL_LOGREG_SINGLE_DOMAIN_INIT_FUNCTION(init_ulogd_fcb, ulogd_fcb);
82 bool opt_daemon = false;
83 char *opt_pidfile = NULL;
84 fosa_abs_time_t start_time;
88 * Resource identification
91 frsh_resource_type_t type;
92 frsh_resource_id_t id;
102 fres_resource_manager mng; /**< Object reference of the resource manager */
103 gavl_cust_root_field_t allocators; /**< Registered allocators for this resource (from multiple applications/nodes) */
104 ul_list_head_t sc_contracts; /**< Negotiated contracts with spare capacity for this resource */
108 * Resource allocator registered in different nodes/applications
113 fres_resource_allocator ra;
116 struct fcb_contract {
117 fres_contract_id_t id;
118 struct res_alloc *ra; /**< Allocator for this contract TODO: Remove contract if allocator is destroyed */
119 gavl_node_t node_fcb;
120 ul_list_node_t node_sc;
121 ul_list_node_t node_reservation;
126 fosa_abs_time_t end_of_stability_period;
127 struct fres_contract *user_contract; /* The contract sent by user. Set after sucessfull neotiation. */
128 struct fres_contract *requested_contract; /* User request for the contract. Set by prepare_reservation_requests() in the beginning of negotiation. */
129 struct fres_contract *to_be_reserved_contract;
130 struct fres_contract *schedulable_contract;
134 * Contract broker data
137 gavl_cust_root_field_t resources; /**< Registered resources */
138 gavl_cust_root_field_t contracts; /**< Contracts negotiated by this FCB */
141 /** List of contracts to be reserved during spare capacity rebalancing */
142 struct reservation_list {
143 ul_list_head_t fcb_contracts;
147 struct fcb_contract *fcb_contract_new(fres_contract_id_t *id)
149 struct fcb_contract *fcb_contract;
151 fcb_contract = malloc(sizeof(*fcb_contract));
155 memset(fcb_contract, 0, sizeof(*fcb_contract));
156 fcb_contract->id = *id;
161 void fcb_contract_destroy(struct fcb_contract *fcb_contract)
163 if (fcb_contract->user_contract) {
164 fres_contract_destroy(fcb_contract->user_contract);
166 if (fcb_contract->to_be_reserved_contract) {
167 fres_contract_destroy(fcb_contract->to_be_reserved_contract);
169 if (fcb_contract->requested_contract) {
170 fres_contract_destroy(fcb_contract->requested_contract);
172 if (fcb_contract->schedulable_contract) {
173 fres_contract_destroy(fcb_contract->requested_contract);
178 static inline int res_key_cmp(const struct res_key *a,
179 const struct res_key *b)
181 if (a->type < b->type) {
183 } else if (a->type > b->type) {
185 } else if (a->id < b->id) {
187 } else if (a->id > b->id) {
194 /* Container for registered resources */
195 GAVL_CUST_NODE_INT_DEC(fcb_resource /* cust_prefix */, \
196 struct fcb /* cust_root_t */, \
197 struct resource/* cust_item_t */, \
198 struct res_key /* cust_key_t */, \
199 resources /* cust_root_node */, \
200 node /* cust_item_node */, \
201 key /* cust_item_key */, \
202 res_key_cmp /* cust_cmp_fnc */);
204 GAVL_CUST_NODE_INT_IMP(fcb_resource /* cust_prefix */, \
205 struct fcb /* cust_root_t */, \
206 struct resource/* cust_item_t */, \
207 struct res_key /* cust_key_t */, \
208 resources /* cust_root_node */, \
209 node /* cust_item_node */, \
210 key /* cust_item_key */, \
211 res_key_cmp /* cust_cmp_fnc */);
213 /* Container for allocators registered for a given resource */
214 GAVL_CUST_NODE_INT_DEC(fcb_alloc /* cust_prefix */, \
215 struct resource /* cust_root_t */, \
216 struct res_alloc /* cust_item_t */, \
217 forb_server_id /* cust_key_t */, \
218 allocators /* cust_root_node */, \
219 node /* cust_item_node */, \
220 app /* cust_item_key */, \
221 fres_contract_id_cmp /* cust_cmp_fnc */);
223 GAVL_CUST_NODE_INT_IMP(fcb_alloc /* cust_prefix */, \
224 struct resource /* cust_root_t */, \
225 struct res_alloc /* cust_item_t */, \
226 forb_server_id /* cust_key_t */, \
227 allocators /* cust_root_node */, \
228 node /* cust_item_node */, \
229 app /* cust_item_key */, \
230 forb_server_id_cmp /* cust_cmp_fnc */);
232 /* Container for contracts with spare capacity negotiated for a given resource */
233 UL_LIST_CUST_DEC(sc_contracts /* cust_prefix */, \
234 struct resource /* cust_head_t */, \
235 struct fcb_contract /* cust_item_t */, \
236 sc_contracts /* cust_head_field */, \
237 node_sc /* cust_node_field */);
239 UL_LIST_CUST_DEC(reservation_list /* cust_prefix */, \
240 struct reservation_list /* cust_head_t */, \
241 struct fcb_contract /* cust_item_t */, \
242 fcb_contracts /* cust_head_field */, \
243 node_reservation /* cust_node_field */);
245 /* Container for negotiated contracts */
246 GAVL_CUST_NODE_INT_DEC(fcb_contract /* cust_prefix */, \
247 struct fcb /* cust_root_t */, \
248 struct fcb_contract /* cust_item_t */, \
249 fres_contract_id_t /* cust_key_t */, \
250 contracts /* cust_root_node */, \
251 node_fcb /* cust_item_node */, \
252 id /* cust_item_key */, \
253 fres_contract_id_cmp /* cust_cmp_fnc */);
256 GAVL_CUST_NODE_INT_IMP(fcb_contract /* cust_prefix */, \
257 struct fcb /* cust_root_t */, \
258 struct fcb_contract /* cust_item_t */, \
259 fres_contract_id_t /* cust_key_t */, \
260 contracts /* cust_root_node */, \
261 node_fcb /* cust_item_node */, \
262 id /* cust_item_key */, \
263 fres_contract_id_cmp /* cust_cmp_fnc */);
265 #include "fcb_contract_gavl.inc"
269 #define o2fcb(o) (struct fcb*)forb_instance_data(o)
272 get_res_key(const struct fres_contract *contract, struct res_key *key)
274 fres_block_resource *block_res;
276 block_res = fres_contract_get_resource(contract);
278 ul_logerr("No resource specified\n");
281 key->type = block_res->resource_type;
282 key->id = block_res->resource_id;
288 * Fills in an array of fcb_contracts according to requests submited
289 * to negotiate_contracts(). For every contract in @a contracts, there
290 * is one fcb_contract in @a fcb_contracts array.
293 * @param fcb_contracts Where to store pointers to fcb_contracts
294 * @param resource Resource for which negotiation is being done.
295 * @param app Application which requests negotiation
296 * @param contracts Contracts submited to negotiate_contracts()
297 * @param num Number of contracts in contracts (which is the same as the number in fcb_contracts).
299 * @return Zero on success, non-zero error code on error.
302 prepare_fcb_contracts(struct fcb *fcb, struct fcb_contract *fcb_contracts[],
303 struct resource **resource,
305 struct fres_contract *contracts[], int num)
308 struct fcb_contract *fc;
309 struct res_alloc *ra;
310 struct res_key key, key2 = {-1,-1};
312 for (i=0; i<num; i++) {
313 struct fres_contract *c = contracts[i];
315 if (fres_contract_id_is_empty(&c->id)) {
316 /* Normal negotiation request */
317 forb_uuid_generate((forb_uuid_t *)&c->id);
318 fc = fcb_contract_new(&c->id);
321 fcb_contracts[i] = fc;
322 if (!get_res_key(c, &key)) {
323 return FRSH_ERR_RESOURCE_ID_INVALID;
325 log_contract("Negotiation request", i, c);
327 fc = fcb_contract_find(fcb, &c->id);
330 fres_contract_id_to_string(str, &c->id, sizeof(str));
331 ul_logerr("Attempt to change unknown contract %s\n", str);
332 return FRES_ERR_NOTHING_TO_RENEGOTIATE;
334 fcb_contracts[i] = fc;
335 if (fres_contract_get_num_blocks(c) == 0) {
337 get_res_key(fc->user_contract, &key);
338 log_contract("Cancelation request", i, fc->user_contract);
341 if (!get_res_key(c, &key)) {
342 return FRSH_ERR_RESOURCE_ID_INVALID;
344 log_contract("Renegotiation request", i, fc->user_contract);
348 fc->requested_contract = c;
350 /* Check that all contracts are for the same resource */
353 *resource = fcb_resource_find(fcb, &key);
355 ul_logerr("No resource manager for %d.%d registered\n",
357 return FRES_ERR_NO_RESOURCE_MANAGER;
360 else if (key.type != key2.type ||
362 ul_logerr("Contract #%d differs in resource!\n", i);
363 return FRSH_ERR_RESOURCE_ID_INVALID;
366 /* Find allocator for this request */
367 ra = fcb_alloc_find(*resource, app);
370 forb_server_id_to_string(str, app, sizeof(str));
371 ul_logerr("No resource allocator found for %d.%d and %s\n",
372 (*resource)->key.type, (*resource)->key.id, str);
373 return FRES_ERR_NO_RESOURCE_ALLOCATOR;
381 * @param resource Resource for which to rebalance capacity and negotiate new contracts
382 * @param fcb_contract New requests to negotiate
383 * @param num The number of elements in @a fcb_contract
384 * @param[out] rl List with changed contracts to be commited
387 prepare_reservation_list(struct resource *resource,
388 struct fcb_contract *fcb_contract[], int num,
389 struct reservation_list *rl)
393 struct fcb_contract *fc;
395 reservation_list_init_head(rl);
397 for (i=0; i<num; i++) {
398 assert(fcb_contract[i]->requested_contract != NULL);
399 reservation_list_insert(rl, fcb_contract[i]);
402 fosa_clock_get_time(CLOCK_REALTIME, &now);
403 ul_list_for_each(sc_contracts, resource, fc) {
404 if (fosa_abs_time_smaller(fc->end_of_stability_period, now) &&
405 fc->requested_contract == NULL) /* Do not insert contract inserted above */
407 reservation_list_insert(rl, fc);
416 * @param resource Resource for which to rebalance capacity and negotiate new contracts
417 * @param rl List with changed contracts to be commited
419 * @return Zero on success, non-zero error code on error
422 rebalance_spare_capacity_and_reserve(struct resource *resource,
423 struct reservation_list *rl)
427 struct fcb_contract *fc;
428 fres_block_spare_capacity *s;
430 fres_contract_ptr_seq contracts;
431 if (!forb_sequence_alloc_buf(&contracts, rl->length)) {
434 contracts._length = rl->length;
436 /* Initialize optimization */
437 ul_list_for_each(reservation_list, rl, fc) {
438 fc->to_be_reserved_contract =
439 fc->requested_contract ? fres_contract_duplicate(fc->requested_contract) :
440 fc->user_contract ? fres_contract_duplicate(fc->user_contract) :
442 assert(fc->to_be_reserved_contract != NULL);
444 forb_sequence_elem(&contracts, i) = fc->to_be_reserved_contract;
447 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
448 if (s && s->granularity == FRSH_GR_DISCRETE) {
449 fc->sc_variant.initial = s->variants._length - 1;
450 fc->sc_variant.try = fc->sc_variant.initial;
454 bool all_combinations_tried;
455 int criterion, best_criterion = -1;
456 struct fcb_contract *fcb_changed;
457 /* Exhaustive search. Try all combinations of spare capacity
458 * variants and find the one with best creterion. */
460 all_combinations_tried = true;
463 /* Prepare spare capacity variant */
464 ul_list_for_each(reservation_list, rl, fc) {
465 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
466 /* TODO: Simulate continuous granularity by discretization */
467 if (s && s->granularity == FRSH_GR_DISCRETE) {
468 fres_container_copy(fc->to_be_reserved_contract->container,
469 forb_sequence_elem(&s->variants, fc->sc_variant.try));
470 criterion += fc->sc_variant.try;
472 if (fcb_changed == NULL) {
473 /* Chnage tried variant for the next round */
474 fc->sc_variant.try = fc->sc_variant.try > 0 ?
475 fc->sc_variant.try - 1 :
476 s->variants._length - 1;
477 /* Do not change other
478 * contracts unless we have
479 * tried all variants */
480 if (fc->sc_variant.try != fc->sc_variant.initial) {
484 if (fc->sc_variant.try != fc->sc_variant.initial)
485 all_combinations_tried = false;
489 if (criterion > best_criterion) {
490 CORBA_Environment ev;
491 /* Reserve contract */
492 ret = fres_resource_manager_reserve_contracts(resource->mng, &contracts, &ev);
493 if (forb_exception_occurred(&ev)) {
494 ret = fres_forbex2err(&ev);
495 ul_logerr("FORB exception when reserving contracts\n");
499 ul_logerr("Contract reservation error %d\n", ret);
500 ret = FRES_ERR_ADMISSION_TEST;
503 if (ret == 0) { /* negotiation succeeded */
504 best_criterion = criterion;
507 } while (!all_combinations_tried);
509 if (best_criterion == -1) {
510 ret = FRSH_ERR_CONTRACT_REJECTED;
512 /* At least some variant succeeded */
514 sc_contracts_init_head(resource);
515 ul_list_for_each(reservation_list, rl, fc) {
516 s = fres_contract_get_spare_capacity(fc->to_be_reserved_contract);
517 if (s && s->granularity == FRSH_GR_DISCRETE) {
518 sc_contracts_insert(resource, fc);
523 ul_list_for_each(reservation_list, rl, fc) {
524 fres_contract_destroy(fc->to_be_reserved_contract);
525 fc->to_be_reserved_contract = NULL;
527 forb_sequence_free_buf(&contracts, forb_no_destructor);
532 * Create/change VReses according to @a schedulable_contracts.
534 * There might be more allocators for schedulable contracts, so merge
535 * adjacent vreses with the same allocator together and create/change
536 * them in one step. Also the order of schedulable contracts might be
537 * different from the initial order od ids in commit_contracts()
538 * therefore we have to search for every contract.
541 change_vreses(struct fcb *fcb, fres_contract_ptr_seq *schedulable_contracts)
543 struct res_alloc *last_ra = NULL;
544 fres_contract_ptr_seq vreses;
546 CORBA_Environment ev;
548 if (!forb_sequence_alloc_buf(&vreses, schedulable_contracts->_length)) {
553 for (i=0; i<schedulable_contracts->_length; i++) {
554 struct fcb_contract *fc;
555 struct fres_contract *sc = schedulable_contracts->_buffer[i];
557 log_contract("Changing VRES", i, sc);
559 fc = fcb_contract_find(fcb, &sc->id);
562 if (true /* TODO: if the schedulable contract is changed */) {
563 if (last_ra != fc->ra) {
564 if (vreses._length) {
565 ret = fres_resource_allocator_change_vreses(last_ra->ra,
567 if (forb_exception_occurred(&ev)) {
568 ret = fres_forbex2err(&ev);
575 vreses._buffer[vreses._length] = sc;
577 fres_contract_destroy(fc->schedulable_contract);
578 fc->schedulable_contract = fres_contract_duplicate(sc);
584 ret = fres_resource_allocator_change_vreses(last_ra->ra,
586 if (forb_exception_occurred(&ev))
587 ret = fres_forbex2err(&ev);
590 forb_sequence_free_buf(&vreses, forb_no_destructor);
595 negotiate_contracts(fres_contract_broker obj,
596 const fres_contract_ptr_seq* contracts,
597 fres_contract_id_seq** ids_out,
598 CORBA_Environment *ev)
600 struct fcb *fcb = o2fcb(obj);
601 struct resource *resource = NULL;
604 fres_contract_ptr_seq *schedulable_contracts;
605 struct fcb_contract **fcb_contracts, *fc;
607 fres_contract_id_seq commit_ids;
609 /* Prepare output sequence for the case we return eariler with
611 forb_sequence_alloc(*ids_out, 0);
613 ev->major = FORB_EX_NO_MEMORY;
616 CORBA_sequence_set_release(*ids_out, CORBA_TRUE);
618 forb_get_req_source(obj, &app);
620 fcb_contracts = malloc(sizeof(fcb_contracts[0])*contracts->_length);
621 if (!fcb_contracts) {
625 memset(fcb_contracts, 0, sizeof(fcb_contracts[0])*contracts->_length);
627 ret = prepare_fcb_contracts(fcb, fcb_contracts, &resource,
628 &app, contracts->_buffer, contracts->_length);
630 goto err_free_fcb_contracts;
632 struct reservation_list rl;
633 prepare_reservation_list(resource,
634 fcb_contracts, contracts->_length,
637 /* Allocate all the needed memory before doing reservation. If
638 * there is no enough memory, it has no sense to call resource
640 if (!forb_sequence_alloc_buf(&commit_ids, rl.length)) {
642 goto err_free_fcb_contracts;
645 /* Reserve contracts */
646 ret = rebalance_spare_capacity_and_reserve(resource, &rl);
648 if (ret == FRSH_ERR_CONTRACT_REJECTED) {
649 ul_logmsg("Contract(s) was/were rejected\n");
652 fres_strerror(ret, msg, sizeof(msg));
653 ul_logerr("Reservation error: %s\n", msg);
655 goto err_free_fcb_contracts;
658 /* Commit contracts */
659 commit_ids._length = rl.length;
661 ul_list_for_each(reservation_list, &rl, fc) {
662 forb_sequence_elem(&commit_ids, i) = fc->id;
666 fres_resource_manager_commit_contracts(resource->mng, &commit_ids,
667 &schedulable_contracts, ev);
668 if (forb_exception_occurred(ev)) {
669 ret = fres_forbex2err(ev);
670 goto err_cancel_reservation;
673 /* Add new contracts to our fcb database for later
674 * reference. Canceled contracts are removed below. */
675 for (i=0; i<contracts->_length; i++) {
676 fc = fcb_contracts[i];
678 if (fc->user_contract) {
679 if (fres_contract_get_num_blocks(fc->requested_contract) > 0) {
681 fres_contract_destroy(fc->user_contract);
682 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
683 /* Note: requested_contract is also
684 * pointed by contracts parameter and
685 * will be freed by FORB. */
686 fc->requested_contract = NULL;
689 /* Insert new contracts */
690 fcb_contract_insert(fcb, fcb_contracts[i]);
691 fc->user_contract = fres_contract_duplicate(fc->requested_contract);
692 fc->requested_contract = NULL;
693 /* See the note above. */
697 ret = change_vreses(fcb, schedulable_contracts);
699 goto err_cancel_reservation;
701 forb_sequence_free(schedulable_contracts, fres_contract_ptr_destroy);
703 ul_logerr("VRes was not created\n");
704 goto err_cancel_contracts;
708 /* Return IDs and delete canceled contracts from out database */
709 if (!forb_sequence_alloc_buf(*ids_out, contracts->_length)) {
710 ev->major = FORB_EX_NO_MEMORY;
711 goto err_cancel_contracts;
713 (*ids_out)->_length = contracts->_length;
714 for (i=0; i<contracts->_length; i++) {
715 struct fcb_contract *fc = fcb_contracts[i];
716 forb_sequence_elem(*ids_out, i) = fc->id;
718 if (fc->requested_contract &&
719 fres_contract_get_num_blocks(fc->requested_contract) == 0) {
720 fcb_contract_delete(fcb, fc);
721 /* Note: requested_contract is also pointed by
722 * contracts parameter and will be freed by FORB. */
723 fc->requested_contract = NULL;
724 fcb_contract_destroy(fc);
729 err_cancel_contracts:
731 goto err_free_fcb_contracts;
732 err_cancel_reservation:
733 fres_resource_manager_cancel_reservations(resource->mng, &commit_ids, ev);
734 err_free_fcb_contracts:
735 for (i=0; i<contracts->_length; i++) {
736 fc = fcb_contracts[i];
737 if (fc && !fc->user_contract) {
738 fcb_contracts[i]->requested_contract = NULL; /* Destroyed by FORB */
739 fcb_contract_destroy(fcb_contracts[i]);
747 void redistribute_spare_capacity(fres_contract_broker obj,
748 const frsh_resource_type_t restype,
749 const frsh_resource_id_t resid,
750 CORBA_Environment *ev)
752 struct fcb *fcb = o2fcb(obj);
753 struct res_key key = {restype, resid };
754 struct resource *resource;
755 struct reservation_list rl;
757 resource = fcb_resource_find(fcb, &key);
759 prepare_reservation_list(resource, NULL, 0, &rl);
761 /* forb_sequence_alloc(ids, rl.length); */
762 /* if (!ids || !ids->_buffer) { */
763 /* ev->major = FORB_EX_NO_MEMORY; */
764 /* goto err_free_fcb_contracts; */
766 /* CORBA_sequence_set_release(ids, CORBA_TRUE); */
767 /* *ids_out = ids; /\* ids is freed by FORB *\/ */
770 rebalance_spare_capacity_and_reserve(resource, &rl);
774 CORBA_long register_resource(fres_contract_broker obj,
775 const frsh_resource_type_t restype,
776 const frsh_resource_id_t resid,
777 const fres_resource_desc *desc,
778 CORBA_Environment *ev)
780 struct fcb *fcb = o2fcb(obj);
781 struct resource *res, *res2;
783 res = malloc(sizeof(*res));
785 memset(res, 0, sizeof(*res));
786 res->key.type = restype;
788 res2 = fcb_resource_find(fcb, &res->key);
790 if (forb_object_is_stale(res2->mng)) {
791 ul_logmsg("Removing stale manager for resource %d.%d\n",
793 forb_object_release(res2->mng);
794 fcb_resource_delete(fcb, res2);
795 /* TODO: Delete also all allocators associated
796 * with this stale resource manager. */
800 ul_logerr("Resource manager %d.%d already registered\n",
805 res->mng = forb_object_duplicate(desc->manager);
806 res->name = desc->name;
808 fcb_alloc_init_root_field(res);
809 sc_contracts_init_head(res);
810 ul_logmsg("Registering manager for resource \"%s\" (%d.%d)\n",
811 res->name, restype, resid);
812 fcb_resource_insert(fcb, res);
821 CORBA_long register_allocator(fres_contract_broker obj,
822 const frsh_resource_type_t restype,
823 const frsh_resource_id_t resid,
824 const fres_resource_allocator ra_obj,
825 CORBA_Environment *ev)
827 struct fcb *fcb = o2fcb(obj);
828 struct resource *res;
829 struct res_alloc *ra;
830 struct res_key resource;
831 forb_server_id server_id;
832 char server_id_str[40];
835 forb_get_server_id(ra_obj, &server_id);
836 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
837 ul_logmsg("Registering allocator for resource %d.%d in app %s\n",
838 restype, resid, server_id_str);
840 resource.type = restype;
842 res = fcb_resource_find(fcb, &resource);
844 ul_logerr("No manager found for %d.%d. Unable to register the allocator!\n",
846 ret = FRES_ERR_NO_RESOURCE_MANAGER;
849 ra = fcb_alloc_find(res, &server_id);
851 char *str = forb_object_to_string(ra_obj);
852 ul_logerr("Allocator from already registered (%s)\n",
855 ret = FRES_ERR_ALLOCATOR_ALREADY_REGISTERED;
858 ra = malloc(sizeof(*ra));
864 ra->ra = forb_object_duplicate(ra_obj);
865 fcb_alloc_insert(res, ra);
871 void get_resources(fres_contract_broker obj, fres_resource_seq** resources, CORBA_Environment *ev)
873 struct fcb *fcb = o2fcb(obj);
874 fres_resource_seq *seq;
875 struct resource *res;
878 seq = malloc(sizeof(*seq));
880 ev->major = FORB_EX_NO_MEMORY;
883 memset(seq, 0, sizeof(*seq));
886 gavl_cust_for_each(fcb_resource, fcb, res) {
890 seq->_buffer = CORBA_sequence_fres_resource_allocbuf(n);
895 gavl_cust_for_each(fcb_resource, fcb, res) {
896 seq->_buffer[n].restype = res->key.type;
897 seq->_buffer[n].resid = res->key.id;
898 seq->_buffer[n].desc.manager = res->mng;
899 seq->_buffer[n].desc.name = res->name;
906 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
907 static int register_inet_port(forb_orb orb)
909 forb_port_t *port = forb_malloc(sizeof(*port));
911 struct in_addr listen_on;
915 memset(port, 0, sizeof(*port));
916 listen_on.s_addr = INADDR_ANY;
917 ret = forb_inet_port_init(&port->desc, listen_on, 0);
920 ret = forb_register_port(orb, port);
925 struct forb_fres_contract_broker_impl impl = {
926 .negotiate_contracts = negotiate_contracts,
927 .register_resource = register_resource,
928 .register_allocator = register_allocator,
929 .redistribute_spare_capacity = redistribute_spare_capacity,
930 .get_resources = get_resources,
933 void peer_discovery_callback(const forb_orb peer_orb, const char *orb_id)
935 forb_server_id server_id;
936 char server_id_str[sizeof(forb_server_id)*2+1];
938 forb_get_server_id(peer_orb, &server_id);
939 forb_server_id_to_string(server_id_str, &server_id, sizeof(server_id_str));
941 ul_logmsg("peer discovered: %s (orb_id '%s')\n", server_id_str, orb_id);
947 if (strcmp(orb_id, "org.frescor.fcb") == 0) {
949 fosa_rel_time_t delay;
950 fosa_clock_get_time(CLOCK_REALTIME, &now);
951 delay = fosa_abs_time_extract_interval(start_time, now);
952 ul_logmsg("node joined: %s (time %ld ms)\n", server_id_str,
953 fosa_rel_time_to_msec(delay));
957 void peer_dead_callback(const forb_orb peer_orb, const char *orb_id)
961 static struct option long_opts[] = {
962 { "daemon", optional_argument, NULL, 'd' },
963 { "loglevel", required_argument, NULL, 'l' },
970 printf("usage: fcb [ options ]\n");
971 printf(" -d, --daemon [pid-file] go to background after FORB initialization\n");
972 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
975 int print_log_domain(ul_log_domain_t *domain, void *context)
977 printf("%s = %d\n", domain->name, domain->level);
981 int main(int argc, char *argv[])
985 fres_contract_broker fcb;
986 forb_executor_t executor;
988 forb_init_attr_t attr = {
989 .orb_id = "org.frescor.fcb",
990 .peer_discovery_callback = peer_discovery_callback,
991 .peer_dead_callback = peer_dead_callback,
992 .fixed_tcp_port = FCB_TCP_PORT,
993 #ifdef CONFIG_FORB_PROTO_INET_DEFAULT
994 .fixed_server_id = FCB_SERVER_ID,
995 .redistribute_hellos = true,
1000 while ((opt = getopt_long(argc, argv, "d::l:", &long_opts[0], NULL)) != EOF) {
1003 if (*optarg == '?') {
1004 ul_logreg_for_each_domain(print_log_domain, NULL);
1009 ret = ul_log_domain_arg2levels(optarg);
1011 error(1, EINVAL, "Error parsing -l argument at char %d\n", ret);
1016 opt_pidfile = optarg;
1021 exit(opt == 'h' ? 0 : 1);
1025 fosa_clock_get_time(CLOCK_REALTIME, &start_time);
1028 forb_daemon_prepare(opt_pidfile);
1030 orb = forb_init(&argc, &argv, &attr);
1031 if (!orb) error(1, errno, "FORB initialization failed");
1033 #if CONFIG_FCB_INET && !CONFIG_FORB_PROTO_INET_DEFAULT
1034 ret = register_inet_port(orb);
1035 if (ret) error(0, errno, "INET port registration failed");
1038 fcb_resource_init_root_field(&fcb_data);
1039 fcb_contract_init_root_field(&fcb_data);
1041 fcb = forb_fres_contract_broker_new(orb, &impl, &fcb_data);
1042 if (!fcb) error(1, errno, "forb_fres_contract_broker_new failed");
1044 /* Prepare executor before we register the fcb reference,
1045 * so that no reuqests are lost */
1046 ret = forb_executor_init(&executor);
1047 if (ret) error(1, errno, "forb_executor_init failed");
1049 ret = forb_executor_register_object(&executor, fcb);
1050 if (ret) error(1, errno, "forb_executor_register_object failed");
1052 ret = forb_register_reference(fcb, fres_contract_broker_reg_name);
1053 if (ret) error(1, errno, "forb_register_reference() failed");
1055 ul_logmsg("Waiting for requests\n");
1057 forb_daemon_ready();
1059 ret = forb_executor_run(&executor);
1060 if (ret) error(1, errno, "forb_executor_run failed");