hrpEC-common.cpp
Go to the documentation of this file.
00001 #include "hrpEC.h"
00002 #include "hrpsys/io/iob.h"
00003 #ifdef OPENRTM_VERSION_TRUNK
00004 #include <rtm/RTObjectStateMachine.h>
00005 #endif
00006 
00007 //#define ENABLE_DEBUG_PRINT (true)
00008 #define ENABLE_DEBUG_PRINT (false)
00009 
00010 namespace RTC
00011 {
00012     hrpExecutionContext::~hrpExecutionContext()
00013     {
00014     }
00015     int hrpExecutionContext::svc(void)
00016     {
00017         if (open_iob() == FALSE){
00018             std::cerr << "open_iob: failed to open" << std::endl;
00019             return 0;
00020         } 
00021         if (lock_iob() == FALSE){
00022             std::cerr << "failed to lock iob" << std::endl;
00023             close_iob();
00024             return 0;
00025         }
00026 #ifndef OPENRTM_VERSION_TRUNK
00027         long period_nsec = (m_period.sec()*1e9+m_period.usec()*1e3);
00028         double period_sec = period_nsec/1e9;
00029 #else
00030         coil::TimeValue period(getPeriod());
00031         double period_sec = (double)period;
00032         long period_nsec = period_sec*1e9;
00033 #endif
00034             int nsubstep = number_of_substeps();
00035         set_signal_period(period_nsec/nsubstep);
00036         std::cout << "period = " << get_signal_period()*nsubstep/1e6
00037                   << "[ms], priority = " << m_priority << std::endl;
00038         struct timeval debug_tv1, debug_tv2, debug_tv3, debug_tv4, debug_tv5;
00039         int loop = 0;
00040         int debug_count = 5000.0/(get_signal_period()*nsubstep/1e6); // Loop count for debug print. Once per 5000.0 [ms].
00041 
00042         if (!enterRT()){
00043             unlock_iob();
00044             close_iob();
00045             return 0;
00046         }
00047         do{
00048             loop++;
00049             if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv1, NULL);
00050             if (!waitForNextPeriod()){
00051                 unlock_iob();
00052                 close_iob();
00053                 return 0;
00054             }
00055             struct timeval tv;
00056             gettimeofday(&tv, NULL);
00057             if (m_profile.count > 0){
00058 #define DELTA_SEC(start, end) (end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1e6)
00059                 double dt = DELTA_SEC(m_tv, tv);
00060                 if (dt > m_profile.max_period) m_profile.max_period = dt;
00061                 if (dt < m_profile.min_period) m_profile.min_period = dt;
00062                 m_profile.avg_period = (m_profile.avg_period*m_profile.count + dt)/(m_profile.count+1);
00063             }
00064             m_profile.count++;
00065             m_tv = tv;
00066             if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv2, NULL);
00067 
00068 #ifndef OPENRTM_VERSION_TRUNK
00069             invoke_worker iw;
00070             struct timeval tbegin, tend;
00071                 std::vector<double> processes(m_comps.size());
00072             gettimeofday(&tbegin, NULL);
00073             for (unsigned int i=0; i< m_comps.size(); i++){
00074                 iw(m_comps[i]);
00075                 gettimeofday(&tend, NULL);
00076                 double dt = DELTA_SEC(tbegin, tend);
00077                 processes[i] = dt;
00078                 tbegin = tend;
00079             }
00080 #else
00081             struct timeval tbegin, tend;
00082             const RTCList& list = getComponentList();
00083             std::vector<double> processes(list.length());
00084             gettimeofday(&tbegin, NULL);
00085             for (unsigned int i=0; i< list.length(); i++){
00086                 RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
00087                 rtobj->workerDo(); 
00088                 gettimeofday(&tend, NULL);
00089                 double dt = DELTA_SEC(tbegin, tend);
00090                 processes[i] = dt;
00091                 tbegin = tend;
00092             }
00093 #endif
00094             if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT &&
00095                 rtc_names.size() == processes.size()) {
00096               printRTCProcessingTime(processes);
00097             }
00098             if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv3, NULL);
00099 
00100             gettimeofday(&tv, NULL);
00101             double dt = DELTA_SEC(m_tv, tv);
00102             if (dt > m_profile.max_process) m_profile.max_process = dt;
00103             if (m_profile.profiles.length() != processes.size()){
00104                 m_profile.profiles.length(processes.size());
00105                 for (unsigned int i=0; i<m_profile.profiles.length(); i++){
00106                     m_profile.profiles[i].count = 0;
00107                     m_profile.profiles[i].avg_process = 0;
00108                     m_profile.profiles[i].max_process = 0;
00109                 }
00110             }
00111             for (unsigned int i=0; i<m_profile.profiles.length(); i++){
00112 #ifndef OPENRTM_VERSION_TRUNK
00113                 LifeCycleState lcs = get_component_state(m_comps[i]._ref);
00114 #else
00115                 RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
00116                 LifeCycleState lcs = rtobj->getState();
00117 #endif
00118                 OpenHRP::ExecutionProfileService::ComponentProfile &prof 
00119                     = m_profile.profiles[i];
00120                 double dt = processes[i];
00121                 if (lcs == ACTIVE_STATE){
00122                     prof.avg_process = (prof.avg_process*prof.count + dt)/(++prof.count);
00123                 }
00124                 if (prof.max_process < dt) prof.max_process = dt;
00125             }
00126             if (dt > period_sec*nsubstep){
00127                 m_profile.timeover++; 
00128 #ifdef NDEBUG
00129                 fprintf(stderr, "[hrpEC][%d.%6.6d] Timeover: processing time = %4.2f[ms]\n",
00130                         tv.tv_sec, tv.tv_usec, dt*1e3);
00131                 // Update rtc_names only when rtcs length change.
00132                 if (processes.size() != rtc_names.size()){
00133                     rtc_names.clear();
00134                     for (unsigned int i=0; i< processes.size(); i++){
00135 #ifndef OPENRTM_VERSION_TRUNK 
00136                         RTC::RTObject_var rtc = RTC::RTObject::_narrow(m_comps[i]._ref);
00137 #else
00138                         RTC::RTObject_var rtc = list[i];
00139 #endif
00140                         rtc_names.push_back(std::string(rtc->get_component_profile()->instance_name));
00141                     }
00142                 }
00143                 printRTCProcessingTime(processes);
00144 #endif
00145             }
00146 
00147 #ifndef OPENRTM_VERSION_TRUNK
00148             if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) {
00149               gettimeofday(&debug_tv4, NULL);
00150               fprintf(stderr, "[hrpEC] [%d.%6.6d] Processing time breakdown : waitForNextPeriod %f[ms], warker (onExecute) %f[ms], ExecutionProfile %f[ms], time from prev cicle %f[ms]\n",
00151                       tv.tv_sec, tv.tv_usec,
00152                       DELTA_SEC(debug_tv1, debug_tv2)*1e3,
00153                       DELTA_SEC(debug_tv2, debug_tv3)*1e3,
00154                       DELTA_SEC(debug_tv3, debug_tv4)*1e3,
00155                       DELTA_SEC(debug_tv5, debug_tv1)*1e3);
00156             }
00157             if (loop % debug_count == (debug_count-1) && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv5, NULL);
00158         } while (m_running);
00159 #else
00160         } while (isRunning());
00161 #endif
00162         exitRT();
00163         unlock_iob();
00164         close_iob();
00165 
00166         return 0;
00167     }
00168 
00169     OpenHRP::ExecutionProfileService::Profile *hrpExecutionContext::getProfile()
00170     {
00171         OpenHRP::ExecutionProfileService::Profile *ret 
00172             = new OpenHRP::ExecutionProfileService::Profile;
00173         *ret = m_profile;
00174         return ret;
00175     }
00176 
00177     OpenHRP::ExecutionProfileService::ComponentProfile hrpExecutionContext::getComponentProfile(RTC::LightweightRTObject_ptr obj)
00178     {
00179 #ifndef OPENRTM_VERSION_TRUNK
00180         for (size_t i=0; i<m_comps.size(); i++){
00181             if (m_comps[i]._ref->_is_equivalent(obj)){
00182 #else
00183         const RTCList& list = getComponentList();
00184         for(size_t i=0; i<list.length(); i++){
00185             RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
00186             if(rtobj->isEquivalent(obj)){
00187 #endif
00188                 return m_profile.profiles[i];
00189             }
00190         }
00191         throw OpenHRP::ExecutionProfileService::ExecutionProfileServiceException("no such component");
00192     }
00193 
00194     void hrpExecutionContext::resetProfile()
00195     {
00196         m_profile.max_period = m_profile.avg_period = 0;
00197         m_profile.min_period = 1.0; // enough long 
00198         m_profile.max_process = 0.0;
00199         for( unsigned int i = 0 ; i < m_profile.profiles.length() ; i++ ){
00200             m_profile.profiles[i].count       = 0;
00201             m_profile.profiles[i].avg_process = 0;
00202             m_profile.profiles[i].max_process = 0;
00203         }
00204         m_profile.count = m_profile.timeover = 0;
00205     }
00206 };


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:55