MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
RTAISolver Class Reference

#include <rtaisolver.h>

Inheritance diagram for RTAISolver:
Collaboration diagram for RTAISolver:

Public Member Functions

 RTAISolver (Solver *pS, RTMode eRTMode, long long lRTPeriod, unsigned long RTStackSize, bool bRTAllowNonRoot, int RTCpuMap, bool bRTHard, bool bRTlog, const std::string &LogProcName)
 
 ~RTAISolver (void)
 
std::ostream & Restart (std::ostream &out) const
 
void Setup (void)
 
void Init (void)
 
bool IsStopCommanded (void)
 
void StopCommanded (void)
 
void Log (void)
 
void Wait (void)
 
- Public Member Functions inherited from RTSolverBase
 RTSolverBase (Solver *pS, RTMode eRTMode, unsigned long lRTPeriod, unsigned long RTStackSize, bool bRTAllowNonRoot, int RTCpuMap, bool bNoOutput=true)
 
virtual ~RTSolverBase (void)
 

Private Attributes

bool bRTHard
 
bool bRTlog
 
const std::string LogProcName
 
long long lRTPeriod
 
void * RTSemPtr_in
 
void * RTSemPtr_out
 
void * mbxlog
 
int RTStpFlag
 
int t_tot
 
long long t0
 
long long t1
 
int or_counter
 

Additional Inherited Members

- Public Types inherited from RTSolverBase
enum  RTMode {
  MBRT_UNKNOWN = -1, MBRT_WAITPERIOD, MBRT_SEMAPHORE, MBRT_IO,
  MBRT_LASTMODE
}
 
- Protected Member Functions inherited from RTSolverBase
bool RTWaitPeriod (void) const
 
bool RTSemWait (void) const
 
bool RTBlockingIO (void) const
 
- Protected Attributes inherited from RTSolverBase
SolverpS
 
RTMode eRTMode
 
unsigned long lRTPeriod
 
unsigned long RTStackSize
 
bool bRTAllowNonRoot
 
int RTCpuMap
 
bool bNoOutput
 
volatile int RTSteps
 

Detailed Description

Definition at line 39 of file rtaisolver.h.

Constructor & Destructor Documentation

RTAISolver::RTAISolver ( Solver pS,
RTMode  eRTMode,
long long  lRTPeriod,
unsigned long  RTStackSize,
bool  bRTAllowNonRoot,
int  RTCpuMap,
bool  bRTHard,
bool  bRTlog,
const std::string &  LogProcName 
)

Definition at line 50 of file rtaisolver.cc.

References ASSERT.

60  RTStackSize, bRTAllowNonRoot, RTCpuMap),
62 bRTlog(bRTlog),
65 RTSemPtr_in(0),
66 RTSemPtr_out(0),
67 mbxlog(0),
68 RTStpFlag(0),
69 t_tot(0),
70 t0(0),
71 t1(0),
72 or_counter(0)
73 {
74  ASSERT(lRTPeriod > 0);
75  ASSERT(!bRTlog || !LogProcName.empty());
76 }
void * RTSemPtr_in
Definition: rtaisolver.h:48
unsigned long RTStackSize
Definition: rtsolver.h:59
void * mbxlog
Definition: rtaisolver.h:51
long long lRTPeriod
Definition: rtaisolver.h:45
long long t0
Definition: rtaisolver.h:54
RTMode eRTMode
Definition: rtsolver.h:54
#define ASSERT(expression)
Definition: colamd.c:977
bool bRTHard
Definition: rtaisolver.h:41
const std::string LogProcName
Definition: rtaisolver.h:43
RTSolverBase(Solver *pS, RTMode eRTMode, unsigned long lRTPeriod, unsigned long RTStackSize, bool bRTAllowNonRoot, int RTCpuMap, bool bNoOutput=true)
Definition: rtsolver.cc:44
void * RTSemPtr_out
Definition: rtaisolver.h:49
int or_counter
Definition: rtaisolver.h:55
int RTStpFlag
Definition: rtaisolver.h:52
bool bRTlog
Definition: rtaisolver.h:42
bool bRTAllowNonRoot
Definition: rtsolver.h:60
long long t1
Definition: rtaisolver.h:54
RTAISolver::~RTAISolver ( void  )

Definition at line 78 of file rtaisolver.cc.

References mbxlog.

79 {
80  if (mbxlog) {
81  rtmbdyn_rt_mbx_delete(&mbxlog);
82  mbxlog = 0;
83  }
84 }
void * mbxlog
Definition: rtaisolver.h:51

Member Function Documentation

void RTAISolver::Init ( void  )
virtual

Reimplemented from RTSolverBase.

Definition at line 148 of file rtaisolver.cc.

References ASSERT, RTSolverBase::bRTAllowNonRoot, bRTlog, RTSolverBase::Init(), LogProcName, lRTPeriod, MBDYN_EXCEPT_ARGS, mbxlog, msg, RTSolverBase::RTCpuMap, RTSemPtr_in, RTSemPtr_out, RTSolverBase::RTSemWait(), and RTSolverBase::RTWaitPeriod().

149 {
150  /* Need timer */
151  if (!rtmbdyn_rt_is_hard_timer_running()) {
152  /* FIXME: ??? */
153  silent_cout("RTAISolver: Hard timer is started by MBDyn"
154  << std::endl);
155  rtmbdyn_rt_set_oneshot_mode();
156  rtmbdyn_start_rt_timer(rtmbdyn_nano2count(1000000));
157  }
158 
159  /*
160  * MBDyn can work in two ways:
161  * - internal timer
162  * - scheduled by an external signal
163  * only the first case is currently implemented
164  */
165  if (RTWaitPeriod()) {
166  long long t = rtmbdyn_rt_get_time();
167  int r;
168 
169  /* Timer should be init'ed */
170  ASSERT(t > 0);
171 
172  silent_cout("RTAISolver: Task: " << ::rtmbdyn_rtai_task
173  << "; time: " << t
174  << "; period: " << lRTPeriod
175  << std::endl);
176 
177  // NOTE: the period was in nanoseconds until now.
178  lRTPeriod = rtmbdyn_nano2count(lRTPeriod);
179 
180  r = rtmbdyn_rt_task_make_periodic(::rtmbdyn_rtai_task,
181  t, lRTPeriod);
182 
183  if (r) {
184  silent_cerr("RTAISolver: "
185  "rtmbdyn_rt_task_make_periodic() failed "
186  "(" << r << ")" << std::endl);
188  }
189 
190 #if 0
191  } else if (RTSemWait()) {
192  int r;
193 
194  /* FIXME: check args
195  * name should be configurable?
196  * initial value 0: non-blocking
197  */
198  r = rtmbdyn_rt_sem_init("MBDSMI", 0, &RTSemPtr_in);
199  if (r) {
200  silent_cerr("rt_sem_init() failed ("
201  << r << ")" << std::endl);
203  }
204 
205  /* FIXME: check args
206  * name should be configurable?
207  * initial value 0: non-blocking
208  */
209  r = rtmbdyn_rt_sem_init("MBDSMO", 0, &RTSemPtr_out);
210  if (r) {
211  silent_cerr("rt_sem_init() failed ("
212  << r << ")" << std::endl);
214  }
215 #endif
216  }
217 
218  /* FIXME: should check whether RTStackSize is correctly set? */
219  if (bRTlog) {
220  silent_cout("RTAISolver: MBDyn starts overruns monitor "
221  "(proc: \"" << LogProcName << "\")"
222  << std::endl);
223 
224  const char *mbxlogname = "logmb";
225  if (rtmbdyn_rt_mbx_init(mbxlogname, sizeof(msg)*16, &mbxlog)) {
226  bRTlog = false;
227  silent_cerr("RTAISolver: cannot init log mailbox "
228  "\"" << mbxlogname << "\""
229  << std::endl);
230 
231  } else {
232  const char *nonroot =
233  bRTAllowNonRoot ? "TRUE" : "FALSE";
234 
235  switch (fork()) {
236  case 0: {
237  char LogCpuMap[] = "0xFF";
238 
239  if (RTCpuMap != 0xFF) {
240  /* MBDyn can use any cpu
241  * The overruns monitor will use any free cpu */
242  snprintf(LogCpuMap, sizeof(LogCpuMap),
243  "0x%02X", ~RTCpuMap);
244  }
245 
246  if (strcmp(LogProcName.c_str(), "logproc") != 0) {
247  if (execl(LogProcName.c_str(), LogProcName.c_str(),
248  "MBDTSK", mbxlogname,
249  LogCpuMap, nonroot, NULL) == 0)
250  {
251  break;
252  }
253 
254  /* error */
255  silent_cout("RTAISolver: cannot start "
256  "log procedure "
257  "\"" << LogProcName << "\"; "
258  "using default" << std::endl);
259  }
260 
261 #ifdef HAVE_SETENV
262  /* sets new path */
263  /* BINPATH is the ${bindir} variable
264  * at configure time, defined in
265  * include/mbdefs.h.in */
266  char *origpath = getenv("PATH");
267  if (origpath == 0) {
268  /* ?!? */
269  setenv("PATH", ".:" BINPATH, 1);
270 
271  } else {
272  std::string newpath = ".:" BINPATH ":";
273  newpath += origpath;
274  setenv("PATH", newpath.c_str(), 1);
275  }
276 #endif // HAVE_SETENV
277 
278  /* start logger */
279  if (execlp("logproc", "logproc", "MBDTSK",
280  mbxlogname, LogCpuMap, nonroot, NULL)
281  == -1)
282  {
283  silent_cout("RTAISolver: cannot start default "
284  "log procedure \"logproc\""
285  << std::endl);
286  /* FIXME: better give up logging? */
287  bRTlog = false;
288  }
289  break;
290  }
291 
292  case -1:
293  silent_cerr("Cannot init log procedure" << std::endl);
294  bRTlog = false;
295  break;
296 
297  default:
298  rtmbdyn_rt_sleep(rtmbdyn_nano2count(1000000000));
299  break;
300  }
301  }
302  }
303 
305 }
void * RTSemPtr_in
Definition: rtaisolver.h:48
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual void Init(void)
Definition: rtsolver.cc:69
void * mbxlog
Definition: rtaisolver.h:51
long long lRTPeriod
Definition: rtaisolver.h:45
bool RTSemWait(void) const
Definition: rtsolver.h:69
bool RTWaitPeriod(void) const
Definition: rtsolver.h:65
#define ASSERT(expression)
Definition: colamd.c:977
const std::string LogProcName
Definition: rtaisolver.h:43
struct mbrtai_msg_t msg
void * RTSemPtr_out
Definition: rtaisolver.h:49
int RTCpuMap
Definition: rtsolver.h:61
bool bRTlog
Definition: rtaisolver.h:42
bool bRTAllowNonRoot
Definition: rtsolver.h:60

Here is the call graph for this function:

bool RTAISolver::IsStopCommanded ( void  )
virtual

Reimplemented from RTSolverBase.

Definition at line 309 of file rtaisolver.cc.

References RTStpFlag, and StopCommanded().

310 {
311  if (RTStpFlag == 1) {
312  StopCommanded();
313  }
314 
315  return (RTStpFlag != 0);
316 }
void StopCommanded(void)
Definition: rtaisolver.cc:320
int RTStpFlag
Definition: rtaisolver.h:52

Here is the call graph for this function:

void RTAISolver::Log ( void  )
virtual

Implements RTSolverBase.

Definition at line 329 of file rtaisolver.cc.

References or_counter, and t_tot.

330 {
331  silent_cout("total overruns: " << or_counter << std::endl
332  << "total overrun time: " << t_tot << " micros" << std::endl);
333 }
int or_counter
Definition: rtaisolver.h:55
std::ostream & RTAISolver::Restart ( std::ostream &  out) const
virtual

Implements RTSolverBase.

Definition at line 88 of file rtaisolver.cc.

References RTSolverBase::bRTAllowNonRoot, bRTHard, bRTlog, RTSolverBase::eRTMode, LogProcName, lRTPeriod, MBDYN_EXCEPT_ARGS, RTSolverBase::MBRT_SEMAPHORE, RTSolverBase::MBRT_WAITPERIOD, RTSolverBase::RTCpuMap, and RTSolverBase::RTStackSize.

89 {
90  out << "RTAI";
91 
92  out << ", reserve stack, " << RTStackSize;
93 
94  out << ", mode, ";
95  switch (eRTMode) {
97  out << "period, time step, " << lRTPeriod;
98  break;
99 
101  out << "semaphore";
102  break;
103 
104  default:
106  }
107 
108  if (bRTAllowNonRoot) {
109  out << ", allow nonroot";
110  }
111 
112  if (RTCpuMap != 0xFF) {
113  out << ", cpu map, " << RTCpuMap;
114  }
115 
116  if (bRTHard) {
117  out << ", hard realtime";
118  }
119 
120  if (bRTlog) {
121  out << ", realtime log, \"" << LogProcName << "\"";
122  }
123 
124  return out;
125 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
unsigned long RTStackSize
Definition: rtsolver.h:59
long long lRTPeriod
Definition: rtaisolver.h:45
RTMode eRTMode
Definition: rtsolver.h:54
bool bRTHard
Definition: rtaisolver.h:41
const std::string LogProcName
Definition: rtaisolver.h:43
int RTCpuMap
Definition: rtsolver.h:61
bool bRTlog
Definition: rtaisolver.h:42
bool bRTAllowNonRoot
Definition: rtsolver.h:60
void RTAISolver::Setup ( void  )
virtual

Implements RTSolverBase.

Definition at line 129 of file rtaisolver.cc.

References ASSERT, RTSolverBase::bRTAllowNonRoot, MBDYN_EXCEPT_ARGS, and RTSolverBase::RTCpuMap.

130 {
131  if (bRTAllowNonRoot) {
132  rtmbdyn_rt_allow_nonroot_hrt();
133  }
134 
135  ASSERT(::rtmbdyn_rtai_task == 0);
136 
137  /* Init RTAI; if init'ed, it will be shut down at exit */
138  if (rtmbdyn_rt_task_init("MBDTSK", 1, 0, 0, RTCpuMap,
139  &::rtmbdyn_rtai_task))
140  {
141  silent_cerr("RTAISolver: unable to init RTAI task" << std::endl);
143  }
144 }
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
#define ASSERT(expression)
Definition: colamd.c:977
int RTCpuMap
Definition: rtsolver.h:61
bool bRTAllowNonRoot
Definition: rtsolver.h:60
void RTAISolver::StopCommanded ( void  )
virtual

Implements RTSolverBase.

Definition at line 320 of file rtaisolver.cc.

References bRTHard.

Referenced by IsStopCommanded().

321 {
322  if (bRTHard) {
323  rtmbdyn_rt_make_soft_real_time();
324  }
325 }
bool bRTHard
Definition: rtaisolver.h:41
void RTAISolver::Wait ( void  )
virtual

Implements RTSolverBase.

Definition at line 337 of file rtaisolver.cc.

References bRTHard, bRTlog, lRTPeriod, mbxlog, msg, or_counter, RTSemPtr_in, RTSolverBase::RTSemWait(), RTSolverBase::RTSteps, RTStpFlag, RTSolverBase::RTWaitPeriod(), mbrtai_msg_t::step, t0, t1, t_tot, and mbrtai_msg_t::time.

338 {
339  rtmbdyn_rt_receive_if(NULL, &RTStpFlag);
340 
341  t1 = rtmbdyn_rt_get_time();
342  if (RTSteps >= 2 && t1 > (t0 + lRTPeriod)) {
343  or_counter++;
344  t_tot = t_tot + rtmbdyn_count2nano(t1 - t0 - lRTPeriod)/1000;
345 
346  if (bRTlog) {
347  msg.step = RTSteps;
348  msg.time = (int)rtmbdyn_count2nano(t1 - t0 - lRTPeriod)/1000;
349 
350  rtmbdyn_RT_mbx_send_if(0, 0, mbxlog, &msg, sizeof(msg));
351  }
352  }
353 
354  if (RTWaitPeriod()) {
355  rtmbdyn_rt_task_wait_period();
356 
357 #if 0
358  } else if (RTSemWait()) {
359  /* FIXME: semaphore must be configurable */
360  mbdyn_rt_sem_wait(RTSemPtr_in);
361 #endif
362  } /* else RTBlockingIO(): do nothing */
363 
364  t0 = rtmbdyn_rt_get_time();
365 
366  if (RTSteps == 2 && bRTHard) {
367  /* make hard real time */
368  rtmbdyn_rt_make_hard_real_time();
369  }
370 
371  RTSteps++;
372 }
void * RTSemPtr_in
Definition: rtaisolver.h:48
void * mbxlog
Definition: rtaisolver.h:51
long long lRTPeriod
Definition: rtaisolver.h:45
volatile int RTSteps
Definition: rtsolver.h:75
bool RTSemWait(void) const
Definition: rtsolver.h:69
bool RTWaitPeriod(void) const
Definition: rtsolver.h:65
long long t0
Definition: rtaisolver.h:54
bool bRTHard
Definition: rtaisolver.h:41
struct mbrtai_msg_t msg
int or_counter
Definition: rtaisolver.h:55
int RTStpFlag
Definition: rtaisolver.h:52
bool bRTlog
Definition: rtaisolver.h:42
long long t1
Definition: rtaisolver.h:54

Here is the call graph for this function:

Member Data Documentation

bool RTAISolver::bRTHard
private

Definition at line 41 of file rtaisolver.h.

Referenced by Restart(), StopCommanded(), and Wait().

bool RTAISolver::bRTlog
private

Definition at line 42 of file rtaisolver.h.

Referenced by Init(), Restart(), and Wait().

const std::string RTAISolver::LogProcName
private

Definition at line 43 of file rtaisolver.h.

Referenced by Init(), and Restart().

long long RTAISolver::lRTPeriod
private

Definition at line 45 of file rtaisolver.h.

Referenced by Init(), Restart(), and Wait().

void* RTAISolver::mbxlog
private

Definition at line 51 of file rtaisolver.h.

Referenced by Init(), Wait(), and ~RTAISolver().

int RTAISolver::or_counter
private

Definition at line 55 of file rtaisolver.h.

Referenced by Log(), and Wait().

void* RTAISolver::RTSemPtr_in
private

Definition at line 48 of file rtaisolver.h.

Referenced by Init(), and Wait().

void* RTAISolver::RTSemPtr_out
private

Definition at line 49 of file rtaisolver.h.

Referenced by Init().

int RTAISolver::RTStpFlag
private

Definition at line 52 of file rtaisolver.h.

Referenced by IsStopCommanded(), and Wait().

long long RTAISolver::t0
private

Definition at line 54 of file rtaisolver.h.

Referenced by Wait().

long long RTAISolver::t1
private

Definition at line 54 of file rtaisolver.h.

Referenced by Wait().

int RTAISolver::t_tot
private

Definition at line 53 of file rtaisolver.h.

Referenced by Log(), and Wait().


The documentation for this class was generated from the following files: