Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

broker_allup.cc

Go to the documentation of this file.
00001 /*
00002  * broker_allup.cc
00003  *
00004  * Copyright (c) 2003, 2004 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file broker_allup.cc
00014  *
00015  * Main file for an edu::utah::pces::Allup server.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <stdlib.h>
00021 #include <signal.h>
00022 #include <unistd.h>
00023 
00024 #include <sys/time.h>
00025 
00026 #include <assert.h>
00027 #include <assert_pp.h>
00028 #include <instrumentation.h>
00029 
00030 #include <ltdl.h>
00031 
00032 #if defined(HAVE_LIBRK)
00033 #include <rk/rk.h>
00034 #include <rk/rk_error.h>
00035 #endif
00036 
00037 #include <fstream>
00038 #include <iostream>
00039 
00040 #include <AllupImpl.hh>
00041 
00042 using namespace std;
00043 
00044 /**
00045  * Flag bit numbers for the executable as a whole.
00046  */
00047 enum {
00048     BAB_DAEMON, /**< Fork into the background. */
00049 };
00050 
00051 /**
00052  * Flags bits derived from the bit numbers above.
00053  */
00054 enum {
00055     BAF_DAEMON = (1L << BAB_DAEMON),
00056 };
00057 
00058 /**
00059  * Global data for the server.
00060  *
00061  * ba_ResourceSet - The resource set for this process.
00062  */
00063 static struct {
00064     CORBA::ORB_var ba_ORB;
00065     unsigned long ba_Flags;
00066     const char *ba_IORFileName;
00067     const char *ba_PIDFileName;
00068 #if defined(HAVE_LIBRK)
00069     rk_resource_set_t ba_ResourceSet;
00070 #endif
00071 } ba_data;
00072 
00073 /**
00074  * Signal handler for SIGINT/SIGTERM.
00075  *
00076  * @param sig The actual signal number received.
00077  */
00078 static void sig_exit_handler(int sig)
00079 {
00080     ba_data.ba_ORB->shutdown(0); /* Break out of orb->run(). */
00081 }
00082 
00083 /**
00084  * Print out the usage statement to standard error.
00085  *
00086  * @param prog_name The program name.
00087  */
00088 static void baUsage(const char *prog_name)
00089 {
00090     require(prog_name != NULL);
00091 
00092     cerr << "Server for CPU broker objects." << endl
00093          << "Usage: "
00094          << prog_name
00095          << " [options]\n"
00096          << endl
00097          << "Options:\n"
00098          << "  -h\t\tThis help message.\n"
00099          << "  -V\t\tShow the version number.\n"
00100 
00101          << "  -f\t\tFork into the background.\n"
00102          << "  -p <file>\tThe PID file name.\n"
00103          << "  -o <file>\tThe IOR file name. (Default: "
00104          << ba_data.ba_IORFileName << ")" << endl
00105 
00106          << endl
00107         
00108          << "Package: " << PACKAGE_STRING << endl
00109          << "Contact: " << PACKAGE_BUGREPORT << endl
00110 
00111         ;
00112 }
00113 
00114 /**
00115  * Process the command line options.
00116  *
00117  * @param argc_inout Reference to main's argc variable.  On return, the
00118  * variable will contain the number of arguments remaining after option
00119  * processing.
00120  * @param argv_inout Reference to main's argv variable.  On return, the
00121  * variable will contain the remaining argument values.
00122  * @return True if the options were processed correctly, false otherwise.
00123  */
00124 static int baProcessOptions(int &argc_inout, char **&argv_inout)
00125 {
00126     int ch, retval = 0;
00127     char *prog_name;
00128     char **argv;
00129     int argc;
00130     
00131     argc = argc_inout;
00132     argv = argv_inout;
00133     prog_name = argv[0];
00134     while( ((ch = getopt(argc, argv, "hVfp:o:")) != -1) && (retval == 0) )
00135     {
00136         switch( ch )
00137         {
00138         case 'f':
00139             ba_data.ba_Flags |= BAF_DAEMON;
00140             break;
00141         case 'p':
00142             if( strlen(optarg) == 0 )
00143             {
00144                 cerr << "Empty PID file name" << endl;
00145                 retval = 1;
00146             }
00147             else
00148             {
00149                 ba_data.ba_PIDFileName = optarg;
00150             }
00151             break;
00152         case 'o':
00153             if( strlen(optarg) == 0 )
00154             {
00155                 cerr << "Empty IOR file name" << endl;
00156                 retval = 1;
00157             }
00158             else
00159             {
00160                 ba_data.ba_IORFileName = optarg;
00161             }
00162             break;
00163         case 'V':
00164             cerr << PACKAGE_VERSION << endl;
00165             retval = -1;
00166             break;
00167         case 'h':
00168         case '?':
00169         default:
00170             retval = 1;
00171             break;
00172         }
00173     }
00174     argc_inout -= optind;
00175     argv_inout += optind;
00176     return( retval );
00177 }
00178 
00179 int main(int argc, char *argv[])
00180 {
00181     const char *prog_name = argv[0];
00182     int retval = EXIT_FAILURE;
00183 
00184     /* Default values. */
00185     ba_data.ba_IORFileName = "allup.ior";
00186 
00187     /* Do not let them kill us until we are ready. */
00188     signal(SIGINT, SIG_IGN);
00189     signal(SIGTERM, SIG_IGN);
00190 
00191     atexit(iPrintPointsAtExit);
00192     
00193 #if defined(HAVE_ACE)
00194     ACE_Log_Msg::instance()->priority_mask(0, ACE_Log_Msg::PROCESS);
00195 #endif
00196 
00197     try
00198     {
00199         int rc;
00200         
00201         /* ORB/POA setup */
00202         ba_data.ba_ORB = CORBA::ORB_init(argc, argv);
00203 
00204         try
00205         {
00206             CORBA::Object_var obj = ba_data.ba_ORB->
00207                 resolve_initial_references("RootPOA");
00208             
00209             PortableServer::POA_var root_poa =
00210                 PortableServer::POA::_narrow(obj.in());
00211             PortableServer::POAManager_var mgr = root_poa->the_POAManager();
00212             
00213             PortableServer::ThreadPolicy_var thread = root_poa->
00214                 create_thread_policy(PortableServer::SINGLE_THREAD_MODEL);
00215             
00216             CORBA::PolicyList policy_list;
00217             policy_list.length(1);
00218             policy_list[0] =
00219                 PortableServer::ThreadPolicy::_duplicate(thread.in());
00220             
00221             PortableServer::POA_var st_poa = root_poa->
00222                 create_POA("SingleThread", mgr.in(), policy_list);
00223             
00224             thread->destroy();
00225             thread = PortableServer::ThreadPolicy::_nil();
00226             
00227             mgr->activate();
00228             
00229             rc = baProcessOptions(argc, argv);
00230             if( rc > 0 )
00231             {
00232                 baUsage(prog_name);
00233             }
00234             else if( rc == 0 )
00235             {
00236                 PortableServer::ObjectId_var allup_oid;
00237                 edu::utah::pces::Allup_var allup;
00238                 struct sigaction sa;
00239                 sigset_t sigmask;
00240                 AllupImpl *ai;
00241                 
00242 #if defined(HAVE_LIBRK)
00243                 {
00244                     char set_name[RK_NAME_LEN];
00245                     rk_resource_set_t rs;
00246                     
00247                     snprintf(set_name, sizeof(set_name), "allup.%d", getpid());
00248                     if( rk_proc_get_rset(getpid()) != NULL_RESOURCE_SET )
00249                     {
00250                         /* Already have a resource set. */
00251                     }
00252                     else if( (rs = rk_resource_set_create(set_name)) != NULL )
00253                     {
00254                         struct cpu_reserve_attr cra;
00255                         rk_reserve_t cr;
00256                         
00257                         memset(&cra, 0, sizeof(cra));
00258                         cra.compute_time.tv_sec = 0;
00259                         cra.compute_time.tv_nsec = 50000;
00260                         cra.period.tv_sec = 0;
00261                         cra.period.tv_nsec = 10000000;
00262                         cra.deadline = cra.period;
00263                         cra.reserve_type.sch_mode = RSV_SOFT;
00264                         cra.reserve_type.enf_mode = RSV_SOFT;
00265                         cra.reserve_type.rep_mode = RSV_SOFT;
00266                         cra.processor = RK_ANY_CPU;
00267                         if( (rk_cpu_reserve_create(rs, &cr, &cra) ==
00268                              RK_SUCCESS) &&
00269                             (rk_resource_set_attach_process(rs, getpid()) ==
00270                              RK_SUCCESS) )
00271                         {
00272                             ba_data.ba_ResourceSet = rs;
00273                         }
00274                         else
00275                         {
00276                             rk_resource_set_destroy(rs);
00277                         }
00278                     }
00279                     else
00280                     {
00281                         perror("rk_resource_set_create");
00282                     }
00283                 }
00284 #endif
00285     
00286                 ai = new AllupImpl(st_poa.in());
00287                 allup_oid = st_poa->activate_object(ai);
00288                 allup = edu::utah::pces::Allup::
00289                     _narrow(st_poa->id_to_reference(allup_oid.in()));
00290                 
00291                 {
00292                     ofstream ostr(ba_data.ba_IORFileName);
00293                     
00294                     ostr << ba_data.ba_ORB->object_to_string(allup.in())
00295                          << endl;
00296                     ostr.close();
00297                 }
00298 
00299                 /* Install our signal handlers, */
00300                 sigemptyset(&sigmask);
00301                 sigaddset(&sigmask, SIGINT);
00302                 sigaddset(&sigmask, SIGTERM);
00303 
00304                 sa.sa_mask = sigmask;
00305                 sa.sa_flags = 0;
00306 #if defined(SA_RESTART)
00307                 sa.sa_flags |= SA_RESTART;
00308 #endif
00309                 sa.sa_handler = sig_exit_handler;
00310                 sigaction(SIGINT, &sa, NULL);
00311                 sigaction(SIGTERM, &sa, NULL);
00312 
00313                 /* ... try to become a daemon, */
00314                 if( ba_data.ba_Flags & BAF_DAEMON )
00315                 {
00316                     daemon(1, 1);
00317                     /* Parent exits here. */
00318                 }
00319 
00320                 /* ... try to write out our pid, */
00321                 if( ba_data.ba_PIDFileName != NULL )
00322                 {
00323                     FILE *file;
00324 
00325                     if( (file = fopen(ba_data.ba_PIDFileName, "w")) != NULL )
00326                     {
00327                         fprintf(file, "%d", getpid());
00328                         fclose(file);
00329                         file = NULL;
00330                     }
00331                 }
00332                 
00333                 /* ... handle events, and then */
00334                 ba_data.ba_ORB->run();
00335 
00336                 /* ... uninstall our signal handlers. */
00337                 signal(SIGINT, SIG_IGN);
00338                 signal(SIGTERM, SIG_IGN);
00339                 
00340 #if defined(HAVE_LIBRK)
00341                 if( ba_data.ba_ResourceSet != NULL_RESOURCE_SET )
00342                 {
00343                     rk_resource_set_destroy(ba_data.ba_ResourceSet);
00344                     ba_data.ba_ResourceSet = NULL_RESOURCE_SET;
00345                 }
00346 #endif
00347             }
00348         }
00349         catch(const CORBA::SystemException &e)
00350         {
00351             cerr << "Caught CORBA exception: " << e << endl;
00352         }
00353         catch(...)
00354         {
00355             cerr << "Caught unknown exception..." << endl;
00356         }
00357         
00358         ba_data.ba_ORB->destroy();
00359         ba_data.ba_ORB = CORBA::ORB::_nil();
00360     }
00361     catch(const CORBA::SystemException &e)
00362     {
00363         cerr << "Caught CORBA exception: " << e << endl;
00364     }
00365     catch(...)
00366     {
00367         cerr << "Caught unknown exception..." << endl;
00368     }
00369     
00370     return( retval );
00371 }

Generated on Fri Oct 22 07:50:24 2004 for CPU Broker by  doxygen 1.3.9.1