2 * Copyright (c) 2012, Intel Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <murphy/common/mm.h>
36 #include <murphy/common/hashtbl.h>
37 #include <murphy/common/utils.h>
38 #include <murphy/common/log.h>
40 #include <murphy-db/mqi.h>
42 #include <murphy/resource/client-api.h>
43 #include <murphy/resource/config-api.h>
45 #include "resource-owner.h"
46 #include "application-class.h"
47 #include "resource-set.h"
50 #include "resource-lua.h"
52 #define NAME_LENGTH 24
55 #define ZONE_NAME_IDX 1
56 #define CLASS_NAME_IDX 2
57 #define FIRST_ATTRIBUTE_IDX 3
61 const char *zone_name;
62 const char *class_name;
63 mrp_attr_value_t attrs[MQI_COLUMN_MAX];
66 static mrp_resource_owner_t resource_owners[MRP_ZONE_MAX * MRP_RESOURCE_MAX];
67 static mqi_handle_t owner_tables[MRP_RESOURCE_MAX];
69 static mrp_resource_owner_t *get_owner(uint32_t, uint32_t);
70 static void reset_owners(uint32_t, mrp_resource_owner_t *);
71 static bool grant_ownership(mrp_resource_owner_t *, mrp_zone_t *,
72 mrp_application_class_t *, mrp_resource_set_t *,
74 static bool advice_ownership(mrp_resource_owner_t *, mrp_zone_t *,
75 mrp_application_class_t *, mrp_resource_set_t *,
78 static void manager_start_transaction(mrp_zone_t *);
79 static void manager_end_transaction(mrp_zone_t *);
81 static void delete_resource_owner(mrp_zone_t *, mrp_resource_t *);
82 static void insert_resource_owner(mrp_zone_t *, mrp_application_class_t *,
84 static void update_resource_owner(mrp_zone_t *, mrp_application_class_t *,
86 static void set_attr_descriptors(mqi_column_desc_t *, mrp_resource_t *);
89 int mrp_resource_owner_create_database_table(mrp_resource_def_t *rdef)
91 MQI_COLUMN_DEFINITION_LIST(base_coldefs,
92 MQI_COLUMN_DEFINITION( "zone_id" , MQI_UNSIGNED ),
93 MQI_COLUMN_DEFINITION( "zone_name" , MQI_VARCHAR(NAME_LENGTH) ),
94 MQI_COLUMN_DEFINITION( "application_class", MQI_VARCHAR(NAME_LENGTH) )
97 MQI_INDEX_DEFINITION(indexdef,
98 MQI_INDEX_COLUMN( "zone_id" )
101 static bool initialized = false;
104 mqi_column_def_t coldefs[MQI_COLUMN_MAX + 1];
105 mqi_column_def_t *col;
113 for (i = 0; i < MRP_RESOURCE_MAX; i++)
114 owner_tables[i] = MQI_HANDLE_INVALID;
118 MRP_ASSERT(sizeof(base_coldefs) < sizeof(coldefs),"too many base columns");
119 MRP_ASSERT(rdef, "invalid argument");
120 MRP_ASSERT(rdef->id < MRP_RESOURCE_MAX, "confused with data structures");
121 MRP_ASSERT(owner_tables[rdef->id] == MQI_HANDLE_INVALID,
122 "owner table already exist");
124 snprintf(name, sizeof(name), "%s_owner", rdef->name);
125 for (p = name; (c = *p); p++) {
126 if (!isascii(c) || (!isalnum(c) && c != '_'))
130 j = MQI_DIMENSION(base_coldefs) - 1;
131 memcpy(coldefs, base_coldefs, j * sizeof(mqi_column_def_t));
133 for (i = 0; i < rdef->nattr && j < MQI_COLUMN_MAX; i++, j++) {
135 atd = rdef->attrdefs + i;
137 col->name = atd->name;
138 col->type = atd->type;
139 col->length = (col->type == mqi_string) ? NAME_LENGTH : 0;
143 memset(coldefs + j, 0, sizeof(mqi_column_def_t));
145 table = MQI_CREATE_TABLE(name, MQI_TEMPORARY, coldefs, indexdef);
147 if (table == MQI_HANDLE_INVALID) {
148 mrp_log_error("Can't create table '%s': %s", name, strerror(errno));
152 owner_tables[rdef->id] = table;
158 void mrp_resource_owner_update_zone(uint32_t zoneid,
159 mrp_resource_set_t *reqset,
164 mrp_resource_set_t *rset;
168 mrp_resource_owner_t oldowners[MRP_RESOURCE_MAX];
169 mrp_resource_owner_t backup[MRP_RESOURCE_MAX];
171 mrp_application_class_t *class;
172 mrp_resource_set_t *rset;
174 mrp_resource_def_t *rdef;
175 mrp_resource_mgr_ftbl_t *ftbl;
176 mrp_resource_owner_t *owner, *old, *owners;
177 mrp_resource_mask_t mask;
178 mrp_resource_mask_t mandatory;
179 mrp_resource_mask_t grant;
180 mrp_resource_mask_t advice;
181 void *clc, *rsc, *rc;
187 mrp_resource_event_t notify;
189 uint32_t nevent, maxev;
190 event_t *events, *ev, *lastev;
192 MRP_ASSERT(zoneid < MRP_ZONE_MAX, "invalid argument");
194 zone = mrp_zone_find_by_id(zoneid);
196 MRP_ASSERT(zone, "zone is not defined");
198 if (!(maxev = mrp_get_resource_set_count()))
202 events = mrp_alloc(sizeof(event_t) * maxev);
204 MRP_ASSERT(events, "Memory alloc failure. Can't update zone");
206 reset_owners(zoneid, oldowners);
207 manager_start_transaction(zone);
209 rcnt = mrp_resource_definition_count();
212 while ((class = mrp_application_class_iterate_classes(&clc))) {
215 while ((rset=mrp_application_class_iterate_rsets(class,zoneid,&rsc))) {
216 force_release = false;
217 mandatory = rset->resource.mask.mandatory;
222 switch (rset->state) {
224 case mrp_resource_acquire:
225 while ((res = mrp_resource_set_iterate_resources(rset, &rc))) {
228 owner = get_owner(zoneid, rid);
230 backup[rid] = *owner;
232 if (grant_ownership(owner, zone, class, rset, res))
233 grant |= ((mrp_resource_mask_t)1 << rid);
235 if (owner->rset != rset)
236 force_release |= owner->modal;
239 owners = get_owner(zoneid, 0);
240 if ((grant & mandatory) == mandatory &&
241 mrp_resource_lua_veto(zone, rset, owners, grant))
246 /* rollback, ie. restore the backed up state */
248 while ((res=mrp_resource_set_iterate_resources(rset,&rc))){
251 mask = (mrp_resource_mask_t)1 << rid;
252 owner = get_owner(zoneid, rid);
253 *owner = backup[rid];
255 if ((grant & mask)) {
256 if ((ftbl = rdef->manager.ftbl) && ftbl->free)
257 ftbl->free(zone, res, rdef->manager.userdata);
260 if (advice_ownership(owner, zone, class, rset, res))
266 if ((advice & mandatory) != mandatory)
269 mrp_resource_lua_set_owners(zone, owners);
273 case mrp_resource_release:
274 while ((res = mrp_resource_set_iterate_resources(rset, &rc))) {
277 owner = get_owner(zoneid, rid);
279 if (advice_ownership(owner, zone, class, rset, res))
280 advice |= ((mrp_resource_mask_t)1 << rid);
282 if ((advice & mandatory) != mandatory)
293 replyid = (reqset == rset && reqid == rset->request.id) ? reqid:0;
297 move = (rset->state != mrp_resource_release);
298 notify = move ? MRP_RESOURCE_EVENT_RELEASE : 0;
299 changed = move || rset->resource.mask.grant;
300 rset->state = mrp_resource_release;
301 rset->resource.mask.grant = 0;
304 if (grant == rset->resource.mask.grant) {
305 if (rset->state == mrp_resource_acquire &&
306 !grant && rset->dont_wait)
308 notify = MRP_RESOURCE_EVENT_RELEASE;
309 rset->state = mrp_resource_release;
314 rset->resource.mask.grant = grant;
317 if (!grant && rset->auto_release) {
318 if (rset->state != mrp_resource_release)
319 notify = MRP_RESOURCE_EVENT_RELEASE;
320 rset->state = mrp_resource_release;
327 mrp_resource_set_notify(rset, notify);
330 if (advice != rset->resource.mask.advice) {
331 rset->resource.mask.advice = advice;
335 if (replyid || changed) {
336 ev = events + nevent++;
338 ev->replyid = replyid;
345 manager_end_transaction(zone);
347 for (lastev = (ev = events) + nevent; ev < lastev; ev++) {
351 mrp_application_class_move_resource_set(rset);
353 mrp_resource_set_updated(rset);
356 rset->event(ev->replyid, rset, rset->user_data);
361 for (rid = 0; rid < rcnt; rid++) {
362 owner = get_owner(zoneid, rid);
363 old = oldowners + rid;
365 if (owner->class != old->class ||
366 owner->rset != old->rset ||
367 owner->res != old->res )
370 delete_resource_owner(zone, old->res);
372 insert_resource_owner(zone, owner->class, owner->res);
374 update_resource_owner(zone, owner->class, owner->res);
379 int mrp_resource_owner_print(char *buf, int len)
381 #define PRINT(fmt, args...) if (p<e) { p += snprintf(p, e-p, fmt , ##args); }
384 mrp_resource_owner_t *owner;
385 mrp_application_class_t *class;
386 mrp_resource_set_t *rset;
388 mrp_resource_def_t *rdef;
393 MRP_ASSERT(buf && len > 0, "invalid argument");
395 rcnt = mrp_resource_definition_count();
396 zcnt = mrp_zone_count();
400 PRINT("Resource owners:\n");
402 for (zid = 0; zid < zcnt; zid++) {
403 zone = mrp_zone_find_by_id(zid);
406 PRINT(" Zone %u:\n", zid);
409 PRINT(" Zone %s:", zone->name);
410 p += mrp_zone_attribute_print(zone, p, e-p);
414 for (rid = 0; rid < rcnt; rid++) {
415 if (!(rdef = mrp_resource_definition_find_by_id(rid)))
418 PRINT(" %-15s: ", rdef->name);
420 owner = get_owner(zid, rid);
422 if (!(class = owner->class) ||
423 !(rset = owner->rset ) ||
424 !(res = owner->res ) )
429 MRP_ASSERT(rdef == res->def, "confused with data structures");
431 PRINT("%-15s", class->name);
433 p += mrp_resource_attribute_print(res, p, e-p);
446 static mrp_resource_owner_t *get_owner(uint32_t zone, uint32_t resid)
448 MRP_ASSERT(zone < MRP_ZONE_MAX && resid < MRP_RESOURCE_MAX,
451 return resource_owners + (zone * MRP_RESOURCE_MAX + resid);
454 static void reset_owners(uint32_t zone, mrp_resource_owner_t *oldowners)
456 mrp_resource_owner_t *owners = get_owner(zone, 0);
457 size_t size = sizeof(mrp_resource_owner_t) * MRP_RESOURCE_MAX;
461 memcpy(oldowners, owners, size);
463 memset(owners, 0, size);
465 for (i = 0; i < MRP_RESOURCE_MAX; i++)
466 owners[i].share = true;
469 static bool grant_ownership(mrp_resource_owner_t *owner,
471 mrp_application_class_t *class,
472 mrp_resource_set_t *rset,
475 mrp_resource_def_t *rdef = res->def;
476 mrp_resource_mgr_ftbl_t *ftbl = rdef->manager.ftbl;
477 bool set_owner = false;
487 do { /* not a loop */
488 if (!owner->class && !owner->rset) {
489 /* nobody owns this, so grab it */
494 if (owner->class == class && owner->rset == rset) {
495 /* we happen to already own it */
499 if (rdef->shareable && owner->share) {
500 /* OK, someone else owns it but
501 the owner is ready to share it with us */
509 if (ftbl && ftbl->allocate) {
510 if (!ftbl->allocate(zone, res, rdef->manager.userdata))
515 owner->class = class;
518 owner->modal = class->modal;
521 owner->share = class->share && res->shared;
526 static bool advice_ownership(mrp_resource_owner_t *owner,
528 mrp_application_class_t *class,
529 mrp_resource_set_t *rset,
532 mrp_resource_def_t *rdef = res->def;
533 mrp_resource_mgr_ftbl_t *ftbl = rdef->manager.ftbl;
545 do { /* not a loop */
546 if (!owner->class && !owner->rset)
547 /* nobody owns this */
551 /* someone else owns it but it can be shared */
555 if (owner->class == class) {
556 if (owner->rset->class.priority == rset->class.priority)
564 if (ftbl && ftbl->advice) {
565 if (!ftbl->advice(zone, res, rdef->manager.userdata))
572 static void manager_start_transaction(mrp_zone_t *zone)
574 mrp_resource_def_t *rdef;
575 mrp_resource_mgr_ftbl_t *ftbl;
578 while ((rdef = mrp_resource_definition_iterate_manager(&cursor))) {
579 ftbl = rdef->manager.ftbl;
581 MRP_ASSERT(ftbl, "confused with data structures");
584 ftbl->init(zone, rdef->manager.userdata);
588 static void manager_end_transaction(mrp_zone_t *zone)
590 mrp_resource_def_t *rdef;
591 mrp_resource_mgr_ftbl_t *ftbl;
594 while ((rdef = mrp_resource_definition_iterate_manager(&cursor))) {
595 ftbl = rdef->manager.ftbl;
597 MRP_ASSERT(ftbl, "confused with data structures");
600 ftbl->commit(zone, rdef->manager.userdata);
605 static void delete_resource_owner(mrp_zone_t *zone, mrp_resource_t *res)
607 static uint32_t zone_id;
609 MQI_WHERE_CLAUSE(where,
610 MQI_EQUAL( MQI_COLUMN(0), MQI_UNSIGNED_VAR(zone_id) )
613 mrp_resource_def_t *rdef;
616 MRP_ASSERT(res, "invalid argument");
621 if ((n = MQI_DELETE(owner_tables[rdef->id], where)) != 1)
622 mrp_log_error("Could not delete resource owner");
625 static void insert_resource_owner(mrp_zone_t *zone,
626 mrp_application_class_t *class,
629 mrp_resource_def_t *rdef = res->def;
633 owner_row_t *rows[2];
634 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
636 MRP_ASSERT(FIRST_ATTRIBUTE_IDX + rdef->nattr <= MQI_COLUMN_MAX,
637 "too many attributes for a table");
639 row.zone_id = zone->id;
640 row.zone_name = zone->name;
641 row.class_name = class->name;
642 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
645 cdsc[i].cindex = ZONE_ID_IDX;
646 cdsc[i].offset = MQI_OFFSET(owner_row_t, zone_id);
649 cdsc[i].cindex = ZONE_NAME_IDX;
650 cdsc[i].offset = MQI_OFFSET(owner_row_t, zone_name);
653 cdsc[i].cindex = CLASS_NAME_IDX;
654 cdsc[i].offset = MQI_OFFSET(owner_row_t, class_name);
656 set_attr_descriptors(cdsc + (i+1), res);
661 if ((n = MQI_INSERT_INTO(owner_tables[rdef->id], cdsc, rows)) != 1)
662 mrp_log_error("can't insert row into owner table");
665 static void update_resource_owner(mrp_zone_t *zone,
666 mrp_application_class_t *class,
669 static uint32_t zone_id;
671 MQI_WHERE_CLAUSE(where,
672 MQI_EQUAL( MQI_COLUMN(0), MQI_UNSIGNED_VAR(zone_id) )
675 mrp_resource_def_t *rdef = res->def;
679 mqi_column_desc_t cdsc[FIRST_ATTRIBUTE_IDX + MQI_COLUMN_MAX + 1];
683 MRP_ASSERT(1 + rdef->nattr <= MQI_COLUMN_MAX,
684 "too many attributes for a table");
686 row.class_name = class->name;
687 memcpy(row.attrs, res->attrs, rdef->nattr * sizeof(mrp_attr_value_t));
690 cdsc[i].cindex = CLASS_NAME_IDX;
691 cdsc[i].offset = MQI_OFFSET(owner_row_t, class_name);
693 set_attr_descriptors(cdsc + (i+1), res);
696 if ((n = MQI_UPDATE(owner_tables[rdef->id], cdsc, &row, where)) != 1)
697 mrp_log_error("can't update row in owner table");
701 static void set_attr_descriptors(mqi_column_desc_t *cdsc, mrp_resource_t *res)
703 mrp_resource_def_t *rdef = res->def;
707 for (i = j = 0; j < rdef->nattr; j++) {
708 switch (rdef->attrdefs[j].type) {
709 case mqi_string: o = MQI_OFFSET(owner_row_t,attrs[j].string); break;
710 case mqi_integer: o = MQI_OFFSET(owner_row_t,attrs[j].integer); break;
711 case mqi_unsignd: o = MQI_OFFSET(owner_row_t,attrs[j].unsignd); break;
712 case mqi_floating: o = MQI_OFFSET(owner_row_t,attrs[j].floating);break;
713 default: /* skip this */ continue;
716 cdsc[i].cindex = FIRST_ATTRIBUTE_IDX + j;
729 * indent-tabs-mode: nil