]> rtime.felk.cvut.cz Git - frescor/frsh.git/blobdiff - resources/dummy/frm_dummy.c
forb: executable resources and fcb changed into dynamic libs
[frescor/frsh.git] / resources / dummy / frm_dummy.c
index 4d75b278ef278996bf61a8b15acbb4ae4e79ab2c..1314d03ff5c46701b27b3cd0344a072c0993a2ea 100644 (file)
-#include <frm.h>
+/**************************************************************************/
+/* ---------------------------------------------------------------------- */
+/* Copyright (C) 2006 - 2008 FRESCOR consortium partners:                */
+/*                                                                       */
+/*   Universidad de Cantabria,              SPAIN                        */
+/*   University of York,                    UK                           */
+/*   Scuola Superiore Sant'Anna,            ITALY                        */
+/*   Kaiserslautern University,             GERMANY                      */
+/*   Univ. Politécnica  Valencia,           SPAIN                       */
+/*   Czech Technical University in Prague,  CZECH REPUBLIC               */
+/*   ENEA                                   SWEDEN                       */
+/*   Thales Communication S.A.              FRANCE                       */
+/*   Visual Tools S.A.                      SPAIN                        */
+/*   Rapita Systems Ltd                     UK                           */
+/*   Evidence                               ITALY                        */
+/*                                                                       */
+/*   See http://www.frescor.org for a link to partners' websites         */
+/*                                                                       */
+/*          FRESCOR project (FP6/2005/IST/5-034026) is funded            */
+/*       in part by the European Union Sixth Framework Programme         */
+/*       The European Union is not liable of any use that may be         */
+/*       made of this code.                                              */
+/*                                                                       */
+/*                                                                       */
+/*  This file is part of FRSH (FRescor ScHeduler)                        */
+/*                                                                       */
+/* FRSH is free software; you can redistribute it and/or modify it       */
+/* under terms of the GNU General Public License as published by the     */
+/* Free Software Foundation; either version 2, or (at your option) any   */
+/* later version.  FRSH is distributed in the hope that it will be       */
+/* useful, but WITHOUT ANY WARRANTY; without even the implied warranty   */
+/* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   */
+/* General Public License for more details. You should have received a   */
+/* copy of the GNU General Public License along with FRSH; see file      */
+/* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
+/* Cambridge, MA 02139, USA.                                             */
+/*                                                                       */
+/* As a special exception, including FRSH header files in a file,        */
+/* instantiating FRSH generics or templates, or linking other files      */
+/* with FRSH objects to produce an executable application, does not      */
+/* by itself cause the resulting executable application to be covered    */
+/* by the GNU General Public License. This exception does not            */
+/* however invalidate any other reasons why the executable file might be  */
+/* covered by the GNU Public License.                                    */
+/**************************************************************************/
+
+/**
+ * @file   frm_dummy.c
+ * @author Michal Sojka <sojkam1@fel.cvut.cz>
+ * @date   Wed Feb 18 16:06:04 2009
+ * 
+ * @brief  Dummy resource manager implementation
+ * 
+ * 
+ */
+
+
+#include <frm_generic.h>
 #include <forb.h>
 #include <error.h>
 #include <errno.h>
+#include <getopt.h>
 #include <fres_sa_scenario.h>
 #include <stdbool.h>
-#include <fcb.h>
 #include <ul_log.h>
-
-UL_LOG_CUST(ulogd_frm_dummy);
-ul_log_domain_t ulogd_frm_dummy = {UL_LOGL_MSG, "frm_dummy"};
-
-
-bool admission_test(struct fres_sa_scenario *scenario)
-{
-       bool schedulable;
-
-       ul_logmsg("Admission test");
-       schedulable = scenario->num_contracts <= 3;
-               
-       return schedulable;
-}
-
-struct frm_data {
-       bool (*admission_test)(struct fres_sa_scenario *scenario);
-       struct fres_sa_scenario *running;
+#include <ul_logreg.h>
+#include "res_dummy.h"
+#include <stdio.h>
+#include "dummy_config.h"
+
+bool opt_accept_all = false;
+bool opt_daemon = false;
+char *opt_pidfile = NULL;
+int opt_res_id = DUMMY_RESOURCE_ID;
+
+struct dummy_data {
+       int some_data;
 };
 
-#define object_to_frm(o) (struct frm_data*)forb_instance_data(o)
-
-CORBA_long reserve_contracts(fres_resource_manager obj,
-                            const fres_contract_ptr_seq* contracts,
-                            CORBA_Environment *ev)
+int admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedulable)
 {
-       struct frm_data *frm = object_to_frm(obj);
-       struct fres_sa_scenario *prospective;
-       struct fres_contract *c;
-       bool schedulable;
-       int i;
-
-       ul_logdeb("reserve_contracts\n");
-       
-       prospective = fres_sa_scenario_duplicate(frm->running);
-       for (i=0; i<contracts->_length; i++) {
-               c = fres_contract_duplicate(contracts->_buffer[i]);
-               fres_sa_scenario_add_contract(prospective, c);
-       }
+       struct dummy_data *data = priv;
+       struct fres_sa_contract *c;
+       int ret;
+       long int utilization = 0;
 
-       schedulable = frm->admission_test(prospective);
+#ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
+       printf("Admission test:\n");
+#endif
 
-       if (schedulable) {
-               fres_sa_scenario_destroy(frm->running);
-               frm->running = prospective;
-       } else {
-               fres_sa_scenario_destroy(prospective);
+       if (opt_accept_all) {
+               *schedulable = true;
+               return 0;
        }
-       return schedulable ? 0 : 1;
-}
 
-void commit_contracts(fres_resource_manager obj,
-                     const fres_contract_id_seq* ids,
-                     fres_contract_ptr_seq** contract_with_scheduler_data,
-                     CORBA_Environment *ev)
-{
-       struct frm_data *frm = object_to_frm(obj);
-       int i, num;
-       struct fres_sa_contract *c;
-       fres_contract_ptr_seq *contracts;
-
-       ul_logdeb("commit_contracts\n");
+       data->some_data++;
+
+       fres_sa_scenario_for_each_no_cancel_contract(scenario, c) {
+               fres_block_basic *basic;
+               fres_block_dummy_sched *dummy_sched;
+               char id[40];
+               fres_contract_id_to_string(id, &c->id, sizeof(id));
+#ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
+               printf("  %s contract: id=%s num_blocks=%d\n",
+                      c->contract == c->new ? "new" : "old", id,
+                      fres_contract_get_num_blocks(c->contract));
+#endif
+               
+               basic = fres_contract_get_basic(c->contract);
+               if (!basic) {
+                       fprintf(stderr, "No basic block present\n");
+                       return -1;
+               }
 
-       contracts = forb_malloc(sizeof(*contracts));
-       if (!contracts) {
-               ev->major = FORB_EX_NO_MEMORY;
-               goto err;
+               if (c->contract == c->new) {
+                       /* Add data for scheduler to the new contracts */
+                       dummy_sched = malloc(sizeof(*dummy_sched));
+                       if (!dummy_sched) return -1;
+                       dummy_sched->priority = 100 - fosa_rel_time_to_msec(basic->budget);
+                       ret = fres_contract_add_dummy_sched(c->contract, dummy_sched);
+                       if (ret) {
+                               fprintf(stderr, "Cannot add dummy_sched block\n");
+                               return -1;
+                       }
+               } else {
+                       dummy_sched = fres_contract_get_dummy_sched(c->contract);
+                       if (!dummy_sched) {
+                               fprintf(stderr, "Dummy_sched is not present\n");
+                               return -1;
+                       }
+               }
+#ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
+               printf("                period=%ld ms, budget=%ld ms, priority=%d\n",
+                      fosa_rel_time_to_msec(basic->period),
+                      fosa_rel_time_to_msec(basic->budget), dummy_sched->priority);
+#endif
+               utilization +=
+                       1000*fosa_rel_time_to_msec(basic->budget) /
+                       fosa_rel_time_to_msec(basic->period);
        }
-       num = ids->_length;
-       contracts->_buffer = CORBA_sequence_fres_contract_ptr_allocbuf(num);
-       contracts->_maximum = contracts->_length = num;
-       
-       for (i=0; i < num; i++) {
-               c = fres_sa_scenario_find_contract(frm->running, &ids->_buffer[i]);
+       *schedulable = utilization < 1000;
+       if (*schedulable) {
+               scenario->utilization = utilization/10;
        }
        
-       *contract_with_scheduler_data = contracts;
-err:;
+#ifdef CONFIG_RESOURCE_DUMMY_VERBOSE   
+       printf("utilization=%ld.%03ld => %s\n", utilization/1000, utilization%1000,
+              *schedulable?"schedulable":"not schedulable");
+#endif
+               
+       return 0;
 }
 
-void cancel_contracts(fres_resource_manager obj,
-                     const fres_contract_id_seq* ids,
-                     CORBA_Environment *ev)
-{
-       int i;
-       struct frm_data *frm = object_to_frm(obj);
-
-       ul_logdeb("cancel_contracts\n");
-
-       for (i=0; i<ids->_length; i++) {
-               struct fres_sa_contract *c;
-               c = fres_sa_scenario_find_contract(frm->running, &ids->_buffer[i]);
-
-               if (c) {
-                       fres_sa_scenario_del_contract(frm->running, c);
-               }
-       }
-}
+struct dummy_data dummy_data;
 
+static struct fres_res_manager frm = {
+       .res_type = DUMMY_RESOURCE_TYPE,
+       .res_id = DUMMY_RESOURCE_ID,
+       .admission_test = admission_test,
+       .priv = &dummy_data,
+       .name = "Dummy resource",
+};
 
-const struct forb_fres_resource_manager_impl dummy_impl = {
-       .reserve_contracts = reserve_contracts,
-       .commit_contracts  = commit_contracts,
-       .cancel_contracts  = cancel_contracts,
+static struct option long_opts[] = {
+    { "loglevel", 1, 0, 'l' },
+//     { "id",           required_argument, 0, 'i' },
+    { "accept-all", 0, 0, 'a' },
+    { "help", 0, 0, 'h' },
+    { 0, 0, 0, 0}
 };
 
-int main(int argc, char *argv[])
+static void
+usage(void)
 {
-       forb_orb orb;
-       fres_contract_broker fcb;
-       fres_resource_manager frm;
-       struct forb_env env;
+       printf("usage: frm_dummy [ options ]\n");
+       printf("  -a, --accept-all   Accepts all contracts\n");
+       printf("  -d, --daemon [pid-file]   go to background after FORB initialization\n");
+/*     printf("  -i, --id <resource id>    Use a different id that the default (%d)\n", DUMMY_RESOURCE_ID);*/
+       printf("  -h, --help         Display this help\n");
+       printf("  -l, --loglevel <number>|<domain>=<number>,...\n");
+}
 
-       struct frm_data dummy_data;
+int forb_main(forb_orb orb, int argc, char *argv[])
+{
+       int ret;
+       forb_init_attr_t attr = { .orb_id = "org.frescor.frm.dummy" };
+       int  opt;
+
+       while ((opt = getopt_long(argc, argv, "ai:l:h", &long_opts[0], NULL)) != EOF) {
+               switch (opt) {
+                       case 'a':
+                               opt_accept_all = true;
+                       case 'd':
+                               opt_daemon = true;
+                               opt_pidfile = optarg;
+                               break;
+//                     case 'i':
+//                             opt_res_id = atoi(optarg);
+//                             break;
+                       case 'l':
+                               ul_log_domain_arg2levels(optarg);
+                               break;
+                       case 'h':
+                       /*default:*/
+                               usage();
+                               exit(opt == 'h' ? 0 : 1);
+               }
+       }
 
-       orb = forb_init(&argc, &argv, "frm_dummy");
-       if (!orb) error(1, errno, "forb_init");
+       if (opt_daemon)
+               forb_daemon_prepare(opt_pidfile);
 
-       fcb = forb_resolve_reference(orb, fres_contract_broker_reg_name);
-       if (!fcb) error(1, 0, "Could not find contract broker");
+       /* Register fres_block_dummy_sched to contract handling
+        * functions */
+       fres_block_register_dummy();
 
-       frm = forb_fres_resource_manager_new(orb, &dummy_impl, &dummy_data);
-       if (!frm) error(1, errno, "forb_fres_resource_manager_new");
+#ifndef CONFIG_RESOURCE_DUMMY_VERBOSE  
+       ulogd_frm_generic.level = UL_LOGL_ERR;
+#endif
 
-       fres_contract_broker_register_manager(fcb, FRSH_RT_PROCESSOR, 0,
-                                             frm, &env);
+       frm.res_id = opt_res_id;
+       ret = frm_register_and_run(orb, &frm);
 
-       forb_execute_object(frm);       
+       if (ret != 0) {
+               error(1, errno, "frm_generic_run failed");
+       }
        
        return 0;
 }