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