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                             if (!dc.loadComponents( (*iter) )) {
00183                                 result = false;
00184                                 log(Error) << "Failed to load file: '"<< (*iter) <<"'." << endlog();
00185                             } else if (!dc.configureComponents()) {
00186                                 result = false;
00187                                 log(Error) << "Failed to configure file: '"<< (*iter) <<"'." << endlog();
00188                             }
00189                             // else leave result=true and continue
00190                         } else {
00191                             result = dc.kickStart( (*iter) );
00192                         }
00193                         continue;
00194                     }
00195 
00196                     if ( (*iter).rfind(".ops",std::string::npos) == (*iter).length() - 4 ||
00197                          (*iter).rfind(".osd",std::string::npos) == (*iter).length() - 4 ||
00198                          (*iter).rfind(".lua",std::string::npos) == (*iter).length() - 4) {
00199                         result = dc.runScript( (*iter) ) && result;
00200                         continue;
00201                     }
00202                     log(Error) << "Unknown extension of file: '"<< (*iter) <<"'. Must be xml, cpf for XML files or, ops, osd or lua for script files."<<endlog();
00203                 }
00204             }
00205             rc = (result ? 0 : -1);
00206 #ifdef USE_TASKBROWSER
00207             // We don't start an interactive console when we're a daemon
00208             if ( !deploymentOnlyChecked && !vm.count("daemon") ) {
00209                 OCL::TaskBrowser tb( &dc );
00210 
00211                 tb.loop();
00212 
00213                 dc.shutdownDeployment();
00214             }
00215 #endif
00216         }
00217 #ifdef  ORO_BUILD_RTALLOC
00218         if (0 != rtMem)
00219             {
00220                 // print statistics after deployment finished, but before os_exit() (needs Logger):
00221                 log(Debug) << "TLSF bytes allocated=" << memSize
00222                            << " overhead=" << (memSize - freeMem)
00223                            << " max-used=" << get_max_size(rtMem)
00224                            << " currently-used=" << get_used_size(rtMem)
00225                            << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00226                            << endlog();
00227             }
00228 #endif
00229 
00230         __os_exit();
00231         }
00232         else
00233         {
00234                 std::cerr << "Unable to start Orocos" << std::endl;
00235         rc = -1;
00236         }
00237 
00238 #ifdef  ORO_BUILD_LOGGING
00239     log4cpp::HierarchyMaintainer::getDefaultMaintainer().shutdown();
00240     log4cpp::HierarchyMaintainer::getDefaultMaintainer().deleteAllCategories();
00241 #endif
00242 
00243 #ifdef  ORO_BUILD_RTALLOC
00244     if (0 != rtMem)
00245     {
00246         std::cout << "TLSF bytes allocated=" << memSize
00247                   << " overhead=" << (memSize - freeMem)
00248                   << " max-used=" << get_max_size(rtMem)
00249                   << " currently-used=" << get_used_size(rtMem)
00250                   << " still-allocated=" << (get_used_size(rtMem) - (memSize - freeMem))
00251                   << "\n";
00252 
00253         destroy_memory_pool(rtMem);
00254         free(rtMem);
00255     }
00256 #endif
00257 
00258     return rc;
00259 }


ocl
Author(s): OCL Development Team
autogenerated on Sat Jun 8 2019 18:48:54