hrpEC-common.cpp
Go to the documentation of this file.
1 #include "hrpEC.h"
2 #include "hrpsys/io/iob.h"
3 #ifdef OPENRTM_VERSION_TRUNK
4 #include <rtm/RTObjectStateMachine.h>
5 #endif
6 
7 //#define ENABLE_DEBUG_PRINT (true)
8 #define ENABLE_DEBUG_PRINT (false)
9 
10 namespace RTC
11 {
13  {
14  wait();
15  if (m_thread_pending)
16  abort ();
17  }
19  {
20  int ret = svc_wrapped ();
21  m_thread_pending = false;
22  return ret;
23  }
25  {
26  if (open_iob() == FALSE){
27  std::cerr << "open_iob: failed to open" << std::endl;
28  return 0;
29  }
30  if (lock_iob() == FALSE){
31  std::cerr << "failed to lock iob" << std::endl;
32  close_iob();
33  return 0;
34  }
35 #ifndef OPENRTM_VERSION_TRUNK
36  long period_nsec = (m_period.sec()*1e9+m_period.usec()*1e3);
37  double period_sec = period_nsec/1e9;
38 #else
39  coil::TimeValue period(getPeriod());
40  double period_sec = (double)period;
41  long period_nsec = period_sec*1e9;
42 #endif
43  int nsubstep = number_of_substeps();
44  set_signal_period(period_nsec/nsubstep);
45  std::cout << "period = " << get_signal_period()*nsubstep/1e6
46  << "[ms], priority = " << m_priority << ", cpu = " << m_cpu << std::endl;
47  struct timeval debug_tv1, debug_tv2, debug_tv3, debug_tv4, debug_tv5;
48  int loop = 0;
49  int debug_count = 5000.0/(get_signal_period()*nsubstep/1e6); // Loop count for debug print. Once per 5000.0 [ms].
50 
51  if (!enterRT()){
52  unlock_iob();
53  close_iob();
54  return 0;
55  }
56  do{
57  loop++;
58  if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv1, NULL);
59  if (!waitForNextPeriod()){
60  unlock_iob();
61  close_iob();
62  return 0;
63  }
64  struct timeval tv;
65  gettimeofday(&tv, NULL);
66  if (m_profile.count > 0){
67 #define DELTA_SEC(start, end) (end.tv_sec - start.tv_sec + (end.tv_usec - start.tv_usec)/1e6)
68  double dt = DELTA_SEC(m_tv, tv);
69  if (dt > m_profile.max_period) m_profile.max_period = dt;
70  if (dt < m_profile.min_period) m_profile.min_period = dt;
71  m_profile.avg_period = (m_profile.avg_period*m_profile.count + dt)/(m_profile.count+1);
72  }
73  m_profile.count++;
74  m_tv = tv;
75  if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv2, NULL);
76 
77 #ifndef OPENRTM_VERSION_TRUNK
78  invoke_worker iw;
79  struct timeval tbegin, tend;
80  std::vector<double> processes(m_comps.size());
81  gettimeofday(&tbegin, NULL);
82  for (unsigned int i=0; i< m_comps.size(); i++){
83  iw(m_comps[i]);
84  gettimeofday(&tend, NULL);
85  double dt = DELTA_SEC(tbegin, tend);
86  processes[i] = dt;
87  tbegin = tend;
88  }
89 #else
90  struct timeval tbegin, tend;
91  const RTCList& list = getComponentList();
92  std::vector<double> processes(list.length());
93  gettimeofday(&tbegin, NULL);
94  for (unsigned int i=0; i< list.length(); i++){
95  RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
96  rtobj->workerDo();
97  gettimeofday(&tend, NULL);
98  double dt = DELTA_SEC(tbegin, tend);
99  processes[i] = dt;
100  tbegin = tend;
101  }
102 #endif
103  if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT &&
104  rtc_names.size() == processes.size()) {
105  printRTCProcessingTime(processes);
106  }
107  if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv3, NULL);
108 
109  gettimeofday(&tv, NULL);
110  double dt = DELTA_SEC(m_tv, tv);
111  if (dt > m_profile.max_process) m_profile.max_process = dt;
112  if (m_profile.profiles.length() != processes.size()){
113  m_profile.profiles.length(processes.size());
114  for (unsigned int i=0; i<m_profile.profiles.length(); i++){
115  m_profile.profiles[i].count = 0;
116  m_profile.profiles[i].avg_process = 0;
117  m_profile.profiles[i].max_process = 0;
118  }
119  }
120  for (unsigned int i=0; i<m_profile.profiles.length(); i++){
121 #ifndef OPENRTM_VERSION_TRUNK
123 #else
124  RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
125  LifeCycleState lcs = rtobj->getState();
126 #endif
127  OpenHRP::ExecutionProfileService::ComponentProfile &prof
128  = m_profile.profiles[i];
129  double dt = processes[i];
130  if (lcs == ACTIVE_STATE){
131  prof.avg_process = (prof.avg_process*prof.count + dt)/(++prof.count);
132  }
133  if (prof.max_process < dt) prof.max_process = dt;
134  }
135  if (dt > period_sec*nsubstep){
136  m_profile.timeover++;
137 #ifdef NDEBUG
138  fprintf(stderr, "[hrpEC][%d.%6.6d] Timeover: processing time = %4.2f[ms]\n",
139  tv.tv_sec, tv.tv_usec, dt*1e3);
140  // Update rtc_names only when rtcs length change.
141  if (processes.size() != rtc_names.size()){
142  rtc_names.clear();
143  for (unsigned int i=0; i< processes.size(); i++){
144 #ifndef OPENRTM_VERSION_TRUNK
145  RTC::RTObject_var rtc = RTC::RTObject::_narrow(m_comps[i]._ref);
146 #else
147  RTC::RTObject_var rtc = list[i];
148 #endif
149  rtc_names.push_back(std::string(RTC::ComponentProfile_var(rtc->get_component_profile())->instance_name));
150  }
151  }
152  printRTCProcessingTime(processes);
153 #endif
154  }
155 
156 #ifndef OPENRTM_VERSION_TRUNK
157  if (loop % debug_count == 0 && ENABLE_DEBUG_PRINT) {
158  gettimeofday(&debug_tv4, NULL);
159  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",
160  tv.tv_sec, tv.tv_usec,
161  DELTA_SEC(debug_tv1, debug_tv2)*1e3,
162  DELTA_SEC(debug_tv2, debug_tv3)*1e3,
163  DELTA_SEC(debug_tv3, debug_tv4)*1e3,
164  DELTA_SEC(debug_tv5, debug_tv1)*1e3);
165  }
166  if (loop % debug_count == (debug_count-1) && ENABLE_DEBUG_PRINT) gettimeofday(&debug_tv5, NULL);
167  } while (m_running);
168 #else
169  } while (isRunning());
170 #endif
171  exitRT();
172  unlock_iob();
173  close_iob();
174 
175  return 0;
176  }
177 
178  OpenHRP::ExecutionProfileService::Profile *hrpExecutionContext::getProfile()
179  {
180  OpenHRP::ExecutionProfileService::Profile *ret
181  = new OpenHRP::ExecutionProfileService::Profile;
182  *ret = m_profile;
183  return ret;
184  }
185 
186  OpenHRP::ExecutionProfileService::ComponentProfile hrpExecutionContext::getComponentProfile(RTC::LightweightRTObject_ptr obj)
187  {
188 #ifndef OPENRTM_VERSION_TRUNK
189  for (size_t i=0; i<m_comps.size(); i++){
190  if (m_comps[i]._ref->_is_equivalent(obj)){
191 #else
192  const RTCList& list = getComponentList();
193  for(size_t i=0; i<list.length(); i++){
194  RTC_impl::RTObjectStateMachine* rtobj = m_worker.findComponent(list[i]);
195  if(rtobj->isEquivalent(obj)){
196 #endif
197  return m_profile.profiles[i];
198  }
199  }
200  throw OpenHRP::ExecutionProfileService::ExecutionProfileServiceException("no such component");
201  }
202 
204  {
205  m_thread_pending = true;
207  }
208 
210  {
211  m_profile.max_period = m_profile.avg_period = 0;
212  m_profile.min_period = 1.0; // enough long
213  m_profile.max_process = 0.0;
214  for( unsigned int i = 0 ; i < m_profile.profiles.length() ; i++ ){
215  m_profile.profiles[i].count = 0;
216  m_profile.profiles[i].avg_process = 0;
217  m_profile.profiles[i].max_process = 0;
218  }
219  m_profile.count = m_profile.timeover = 0;
220  }
221 };
OpenHRP::ExecutionProfileService::Profile * getProfile()
#define FALSE
Definition: iob.h:19
std::vector< Comp > m_comps
std::vector< std::string > rtc_names
Definition: hrpEC.h:74
#define DELTA_SEC(start, end)
int close_iob(void)
close connection with joint servo process
Definition: iob.cpp:413
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
void printRTCProcessingTime(std::vector< double > &processes)
Definition: hrpEC.h:60
struct timeval m_tv
Definition: hrpEC.h:71
png_uint_32 i
int open_iob(void)
open connection with joint servo process
Definition: iob.cpp:401
volatile bool m_thread_pending
Definition: hrpEC.h:75
virtual int svc(void)
virtual int wait(void)
int gettimeofday(struct timeval *tv, struct timezone *tz)
#define ENABLE_DEBUG_PRINT
Definition: hrpEC-common.cpp:8
int set_signal_period(long period_ns)
set the period of signals issued by wait_for_iob_signal()
Definition: iob.cpp:697
int unlock_iob()
unlock access to iob
Definition: iob.cpp:500
ACTIVE_STATE
long get_signal_period()
get the period of signals issued by wait_for_iob_signal()
Definition: iob.cpp:703
long int sec() const
long int usec() const
LifeCycleState
int number_of_substeps()
Definition: iob.cpp:558
OpenHRP::ExecutionProfileService::ComponentProfile getComponentProfile(RTC::LightweightRTObject_ptr obj)
std::vector< IRTObject *> RTCList
virtual LifeCycleState get_component_state(LightweightRTObject_ptr comp)
int lock_iob()
lock access to iob
Definition: iob.cpp:493
OpenHRP::ExecutionProfileService::Profile m_profile
Definition: hrpEC.h:70
virtual void activate()


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Sat Dec 17 2022 03:52:20