hrpEC.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #include "hrpEC.h"
4 #include <rtm/ECFactory.h>
5 #include "hrpsys/io/iob.h"
6 
7 #include <iostream>
8 
9 #include <stdio.h>
10 #include <errno.h>
11 #include <sched.h>
12 #include <signal.h>
13 #include <sys/time.h>
14 #include <sys/mman.h>
15 #include <unistd.h>
16 
17 #define MAX_SAFE_STACK (8*1024) /* The maximum stack size which is
18  guranteed safe to access without
19  faulting */
20 void stack_prefault(void) {
21 
22  unsigned char dummy[MAX_SAFE_STACK];
23 
24  memset(&dummy, 0, MAX_SAFE_STACK);
25  return;
26 }
27 
28 namespace RTC
29 {
31 #ifndef OPENRTM_VERSION_TRUNK
33 #else
34  : RTC_exp::PeriodicExecutionContext(),
35 #endif
36  m_priority(49),
37  m_cpu(-1),
38  m_thread_pending (false)
39  {
40  resetProfile();
41  rtclog.setName("hrpEC");
42  coil::Properties& prop(Manager::instance().getConfig());
43 
44  // Priority
45  getProperty(prop, "exec_cxt.periodic.priority", m_priority);
46  getProperty(prop, "exec_cxt.periodic.rtpreempt.priority", m_priority);
47  getProperty(prop, "exec_cxt.periodic.cpu_affinity", m_cpu);
48  RTC_DEBUG(("Priority: %d", m_priority));
49  }
50 
52  {
53  int nsubstep = number_of_substeps();
54  while(1){
55  if (wait_for_iob_signal()){
56  perror("wait_for_iob_signal()");
57  return false;
58  }
59  if (read_iob_frame() % nsubstep == 0) break;
60  }
61  return true;
62  }
63 
65  {
66  struct sched_param param;
67  param.sched_priority = m_priority;
68 
69 #ifndef __APPLE__
70  if (sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
71  perror("sched_setscheduler");
72  std::cerr << "If you are running this program on normal linux kernel for debug purpose, you can ignore the error message displayed above. If not, this program must have superuser privilege." << std::endl;
73  //return -1;
74  }else{
75  /* Lock memory */
76  if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
77  perror("mlockall failed");
78  }
79  }
80 
81  if (m_cpu>=0){
82  cpu_set_t cpu_set;
83  CPU_ZERO(&cpu_set);
84  CPU_SET(m_cpu, &cpu_set);
85  int result = sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set);
86  if (result != 0) {
87  perror("sched_setaffinity():");
88  }
89  }
90 #endif
91  /* Pre-fault our stack */
93 
94  return true;
95  }
97  {
98  return true;
99  }
100 };
101 
102 
103 extern "C"
104 {
105  void hrpECInit(RTC::Manager* manager)
106  {
107 #ifndef OPENRTM_VERSION_TRUNK
108  manager->registerECFactory("hrpExecutionContext",
109  RTC::ECCreate<RTC::hrpExecutionContext>,
110  RTC::ECDelete<RTC::hrpExecutionContext>);
111 #else
112  RTC::ExecutionContextFactory::
113  instance().addFactory("hrpExecutionContext",
118 #endif
119  std::cerr << "hrpExecutionContext is registered" << std::endl;
120  }
121 };
122 
AbstractClass * Creator()
void hrpECInit(RTC::Manager *manager)
Definition: hrpEC.cpp:105
static Manager & instance()
void stack_prefault(void)
Definition: hrpEC.cpp:20
#define RTC_DEBUG(fmt)
unsigned long long read_iob_frame()
Definition: iob.cpp:551
int wait_for_iob_signal()
wait until iob signal is issued
Definition: iob.cpp:671
bool registerECFactory(const char *name, ECNewFunc new_func, ECDeleteFunc delete_func)
prop
void Destructor(AbstractClass *&obj)
int number_of_substeps()
Definition: iob.cpp:558
#define MAX_SAFE_STACK
Definition: hrpEC.cpp:17


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50