deployer.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Jul 3 15:30:14 CEST 2008  deployer.cpp
00003 
00004                         deployer.cpp -  description
00005                            -------------------
00006     begin                : Thu July 03 2008
00007     copyright            : (C) 2008 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This program is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU Lesser General Public            *
00013  *   License as published by the Free Software Foundation; either          *
00014  *   version 2.1 of the License, or (at your option) any later version.    *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00019  *   Lesser General Public License for more details.                       *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU Lesser General Public      *
00022  *   License along with this program; if not, write to the Free Software   *
00023  *   Foundation, Inc., 59 Temple Place,                                    *
00024  *   Suite 330, Boston, MA  02111-1307  USA                                *
00025  ***************************************************************************/
00026 
00027 #include <rtt/rtt-config.h>
00028 #ifdef OS_RT_MALLOC
00029 // need access to all TLSF functions embedded in RTT
00030 #define ORO_MEMORY_POOL
00031 #include <rtt/os/tlsf/tlsf.h>
00032 #endif
00033 #include <rtt/os/main.h>
00034 #include <rtt/RTT.hpp>
00035 #include <rtt/Logger.hpp>
00036 
00037 #ifndef RTT_SCRIPT_PROGRAM
00038 #define USE_TASKBROWSER
00039 #endif
00040 
00041 #ifdef USE_TASKBROWSER
00042 #include <taskbrowser/TaskBrowser.hpp>
00043 #endif
00044 #include <deployment/DeploymentComponent.hpp>
00045 #include <iostream>
00046 #include <string>
00047 #include "deployer-funcs.hpp"
00048 
00049 #ifdef  ORO_BUILD_LOGGING
00050 #   ifndef OS_RT_MALLOC
00051 #   warning "Logging needs rtalloc!"
00052 #   endif
00053 #include <log4cpp/HierarchyMaintainer.hh>
00054 #include "logging/Category.hpp"
00055 #endif
00056 
00057 using namespace RTT;
00058 namespace po = boost::program_options;
00059 using namespace std;
00060 
00061 int main(int argc, char** argv)
00062 {
00063         std::string                 siteFile;      // "" means use default in DeploymentComponent.cpp
00064     std::vector<std::string>    scriptFiles;
00065         std::string                 name("Deployer");
00066     bool                        requireNameService = false;         // not used
00067     bool                        deploymentOnlyChecked = false;
00068         int                                                     minNumberCPU = 0;
00069     po::variables_map           vm;
00070         po::options_description     otherOptions;
00071 
00072 #ifdef  ORO_BUILD_RTALLOC
00073     OCL::memorySize             rtallocMemorySize   = ORO_DEFAULT_RTALLOC_SIZE;
00074         po::options_description     rtallocOptions      = OCL::deployerRtallocOptions(rtallocMemorySize);
00075         otherOptions.add(rtallocOptions);
00076 #endif
00077 
00078 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00079     // to support RTT's logging to log4cpp
00080     std::string                 rttLog4cppConfigFile;
00081     po::options_description     rttLog4cppOptions = OCL::deployerRttLog4cppOptions(rttLog4cppConfigFile);
00082     otherOptions.add(rttLog4cppOptions);
00083 #endif
00084 
00085     // were we given non-deployer options? ie find "--"
00086     int     optIndex    = 0;
00087     bool    found       = false;
00088 
00089     while(!found && optIndex<argc)
00090     {
00091         found = (0 == strcmp("--", argv[optIndex]));
00092         if(!found) optIndex++;
00093     }
00094 
00095     if (found) {
00096         argv[optIndex] = argv[0];
00097     }
00098 
00099     // if extra options not found then process all command line options,
00100     // otherwise process all options up to but not including "--"
00101     int rc = OCL::deployerParseCmdLine(!found ? argc : optIndex, argv,
00102                                        siteFile, scriptFiles, name, requireNameService,deploymentOnlyChecked,
00103                                                                            minNumberCPU,
00104                                        vm, &otherOptions);
00105 
00106     if (0 != rc)
00107         {
00108                 return rc;
00109         }
00110 
00111         // check system capabilities
00112         rc = OCL::enforceMinNumberCPU(minNumberCPU);
00113         if (0 != rc)
00114         {
00115                 return rc;
00116         }
00117 
00118 #if     defined(ORO_BUILD_LOGGING) && defined(OROSEM_LOG4CPP_LOGGING)
00119     if (!OCL::deployerConfigureRttLog4cppCategory(rttLog4cppConfigFile))
00120     {
00121         return -1;
00122     }
00123 #endif
00124 
00125 #ifdef  ORO_BUILD_RTALLOC
00126     size_t                  memSize     = rtallocMemorySize.size;
00127     void*                   rtMem       = 0;
00128     size_t                  freeMem     = 0;
00129     if (0 < memSize)
00130     {
00131         // don't calloc() as is first thing TLSF does.
00132         rtMem = malloc(memSize);
00133         assert(0 != rtMem);
00134         freeMem = init_memory_pool(memSize, rtMem);
00135         if ((size_t)-1 == freeMem)
00136         {
00137             cerr << "Invalid memory pool size of " << memSize 
00138                           << " bytes (TLSF has a several kilobyte overhead)." << endl;
00139             free(rtMem);
00140             return -1;
00141         }
00142         cout << "Real-time memory: " << freeMem << " bytes free of "
00143                   << memSize << " allocated." << endl;
00144     }
00145 #endif  // ORO_BUILD_RTALLOC
00146 
00147 #ifdef  ORO_BUILD_LOGGING
00148     log4cpp::HierarchyMaintainer::set_category_factory(
00149         OCL::logging::Category::createOCLCategory);
00150 #endif
00151 
00152 
00153     /******************** WARNING ***********************
00154      *   NO log(...) statements before __os_init() !!!!! 
00155      ***************************************************/
00156 
00157     // start Orocos _AFTER_ setting up log4cpp
00158         if (0 == __os_init(argc - optIndex, &argv[optIndex]))
00159     {
00160 #ifdef  ORO_BUILD_LOGGING
00161         log(Info) << "OCL factory set for real-time logging" << endlog();
00162 #endif
00163         // scope to force dc destruction prior to memory free
00164         {
00165             OCL::DeploymentComponent dc( name, siteFile );
00166 
00167             /* Only start the scripts after the Orb was created. Processing of
00168                scripts stops after the first failed script, and -1 is returned.
00169                Whether a script failed or all scripts succeeded, in non-daemon
00170                and non-checking mode the TaskBrowser will be run to allow
00171                inspection.
00172              */
00173             bool result = true;
00174             for (std::vector<std::string>::const_iterator iter=scriptFiles.begin();
00175                  iter!=scriptFiles.end() && result;
00176                  ++iter)
00177             {
00178                 if ( !(*iter).empty() )
00179                 {
00180                     if ( (*iter).rfind(".xml",std::string::npos) == (*iter).length() - 4 || (*iter).rfind(".cpf",std::string::npos) == (*iter).length() - 4) {
00181                         if ( deploymentOnlyChecked ) {
00182                             result = dc.loadComponents( (*iter) ) && dc.configureComponents();
00183                         } else {
00184                             result = dc.kickStart( (*iter) );
00185                         }
00186                         continue;
00187                     } if ( (*iter).rfind(".ops",std::string::npos) == (*iter).length() - 4 || (*iter).rfind(".osd",std::string::npos) == (*iter).length() - 4) {
00188                         result = dc.runScript( (*iter) ) && result;
00189                         continue;
00190                     }
00191                     log(Error) << "Unknown extension of file: '"<< (*iter) <<"'. Must be xml, cpf for XML files or, ops or osd for script files."<<endlog();
00192                 }
00193             }
00194             if (result == false)
00195                 rc = -1;
00196 #ifdef USE_TASKBROWSER
00197             // We don't start an interactive console when we're a daemon
00198             if ( !deploymentOnlyChecked && !vm.count("daemon") ) {
00199                 OCL::TaskBrowser tb( &dc );
00200 
00201                 tb.loop();
00202 
00203                 dc.shutdownDeployment();
00204             }
00205 #endif
00206         }
00207 #ifdef  ORO_BUILD_RTALLOC
00208         if (0 != rtMem)
00209             {
00210                 // print statistics after deployment finished, but before os_exit() (needs Logger):
00211                 log(Debug) << "TLSF bytes allocated=" << memSize
00212                            << " overhead=" << (memSize - freeMem)
00213                            << " max-used=" << get_max_size(rtMem)
00214                            << " currently-used=" << get_used_size(rtMem)
00215                            << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00216                            << endlog();
00217             }
00218 #endif
00219 
00220         __os_exit();
00221         }
00222         else
00223         {
00224                 std::cerr << "Unable to start Orocos" << std::endl;
00225         rc = -1;
00226         }
00227 
00228 #ifdef  ORO_BUILD_LOGGING
00229     log4cpp::HierarchyMaintainer::getDefaultMaintainer().shutdown();
00230     log4cpp::HierarchyMaintainer::getDefaultMaintainer().deleteAllCategories();
00231 #endif
00232 
00233 #ifdef  ORO_BUILD_RTALLOC
00234     if (0 != rtMem)
00235     {
00236         std::cout << "TLSF bytes allocated=" << memSize
00237                   << " overhead=" << (memSize - freeMem)
00238                   << " max-used=" << get_max_size(rtMem)
00239                   << " currently-used=" << get_used_size(rtMem)
00240                   << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00241                   << "\n";
00242 
00243         destroy_memory_pool(rtMem);
00244         free(rtMem);
00245     }
00246 #endif
00247 
00248     return rc;
00249 }


ocl
Author(s): OCL Development Team
autogenerated on Mon Sep 14 2015 14:21:46