diff --git a/docs/src/man/man1/rtapi_app.1.adoc b/docs/src/man/man1/rtapi_app.1.adoc index 88215e89b6c..dc6fc800711 100644 --- a/docs/src/man/man1/rtapi_app.1.adoc +++ b/docs/src/man/man1/rtapi_app.1.adoc @@ -14,6 +14,17 @@ rtapi_app - creates a simulated real time environment Used for loading real time modules on systems without real time (for simulation). +== ENVIRONMENT + +*RTAPI_VIRTUAL_TIME*:: + When set to a non-zero number, run in accelerated simulation: the + non-realtime POSIX backend is used, the realtime thread does not sleep + between cycles, and *rtapi_get_time*(3) advances by exactly one thread + period per cycle. Only a single realtime thread is supported. + *rtapi_app* gives up root and all capabilities in this mode, so + hardware drivers that need privileged access fail to load. Never use + it with a real machine. + == SEE ALSO linuxcnc(1) diff --git a/src/rtapi/uspace_posix.cc b/src/rtapi/uspace_posix.cc index e7d0b88bb30..2d35666ae68 100644 --- a/src/rtapi/uspace_posix.cc +++ b/src/rtapi/uspace_posix.cc @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef HAVE_SYS_IO_H #include #endif @@ -35,7 +36,8 @@ struct PosixTask : RtapiTask { }; struct PosixApp : RtapiApp { - PosixApp(int policy = SCHED_FIFO) : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO) { + PosixApp(int policy = SCHED_FIFO) + : RtapiApp(policy), do_thread_lock(policy != SCHED_FIFO), virtual_time(rtapi_virtual_time()) { pthread_once(&key_once, init_key); if (do_thread_lock) { pthread_once(&lock_once, init_lock); @@ -102,6 +104,15 @@ struct PosixApp : RtapiApp { return -ret; } } + // shortcut: one thread only, interleaving several threads needs a + // virtual-time scheduler; upgrade when a base-thread must be simulated. + for (int i = 0; virtual_time && i < MAX_TASKS; i++) { + RtapiTask *other = task_array[i]; + if (other && other != TASK_MAGIC_INIT && other != task && other->period) { + rtapi_print_msg(RTAPI_MSG_ERR, "RTAPI_VIRTUAL_TIME supports a single realtime thread only\n"); + return -EBUSY; + } + } if (do_thread_lock) pthread_mutex_lock(&thread_lock); if ((ret = pthread_create(&task->thr, &attr, &wrapper, reinterpret_cast(task))) != 0) @@ -119,6 +130,8 @@ struct PosixApp : RtapiApp { struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); rtapi_timespec_advance(task->nextstart, now, task->period + task->pll_correction); + if (rtapi_virtual_time()) + vtime = now.tv_sec * 1000000000LL + now.tv_nsec; /* call the task function with the task argument */ (task->taskcode)(task->arg); @@ -164,7 +177,10 @@ struct PosixApp : RtapiApp { rtapi_timespec_advance(task->nextstart, task->nextstart, task->period + task->pll_correction); struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now); - if (rtapi_timespec_less(task->nextstart, now)) { + if (virtual_time) { + // No sleep: the next cycle starts at once, one period later in virtual time. + vtime = task->nextstart.tv_sec * 1000000000LL + task->nextstart.tv_nsec; + } else if (rtapi_timespec_less(task->nextstart, now)) { if (policy == SCHED_FIFO) unexpected_realtime_delay(task); } else { @@ -203,7 +219,7 @@ struct PosixApp : RtapiApp { void task_self_resync() { RtapiTask *task = reinterpret_cast(pthread_getspecific(key)); - if (!task) + if (!task || virtual_time) return; /* Set nextstart = now. The next rtapi_wait() advances it by (period + pll_correction) and sleeps until then, giving exactly one @@ -212,6 +228,8 @@ struct PosixApp : RtapiApp { } bool do_thread_lock; + const bool virtual_time; + static std::atomic vtime; // start of the current cycle [ns], 0 before the first one static pthread_once_t key_once; static pthread_key_t key; @@ -226,6 +244,8 @@ struct PosixApp : RtapiApp { } long long do_get_time() { + if (virtual_time && vtime) + return vtime; struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts.tv_sec * 1000000000LL + ts.tv_nsec; @@ -241,6 +261,7 @@ pthread_once_t PosixApp::key_once = PTHREAD_ONCE_INIT; pthread_once_t PosixApp::lock_once = PTHREAD_ONCE_INIT; pthread_key_t PosixApp::key; pthread_mutex_t PosixApp::thread_lock; +std::atomic PosixApp::vtime(0); } // namespace @@ -249,6 +270,8 @@ extern "C" RtapiApp *make(int policy); RtapiApp *make(int policy) { if (policy == SCHED_OTHER) { rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX non-realtime\n"); + if (rtapi_virtual_time()) + rtapi_print_msg(RTAPI_MSG_ERR, "Note: RTAPI_VIRTUAL_TIME: threads run without sleeping, simulation only\n"); } else { rtapi_print_msg(RTAPI_MSG_ERR, "Note: Using POSIX realtime\n"); } diff --git a/src/rtapi/uspace_rtapi_app.hh b/src/rtapi/uspace_rtapi_app.hh index 20e1ab1871c..bcc8302ed86 100644 --- a/src/rtapi/uspace_rtapi_app.hh +++ b/src/rtapi/uspace_rtapi_app.hh @@ -21,6 +21,7 @@ #include #endif #include +#include #include #include #include @@ -44,6 +45,13 @@ static inline bool rtapi_timespec_less(const struct timespec &ta, const struct t void rtapi_timespec_advance(struct timespec &result, const struct timespec &src, unsigned long nsec); +// Opt-in accelerated simulation: RTAPI_VIRTUAL_TIME=1 makes the POSIX +// non-realtime backend skip the period sleep and report tick-based time. +static inline bool rtapi_virtual_time() { + const char *e = getenv("RTAPI_VIRTUAL_TIME"); + return e && atoi(e) != 0; +} + struct WithRoot { WithRoot(); ~WithRoot(); diff --git a/src/rtapi/uspace_rtapi_main.cc b/src/rtapi/uspace_rtapi_main.cc index f23772ab474..256a837f535 100644 --- a/src/rtapi/uspace_rtapi_main.cc +++ b/src/rtapi/uspace_rtapi_main.cc @@ -347,7 +347,7 @@ rtapi_realtime_type_t rtapi_get_realtime_type(void){ return cached; } - if(!detect_force() && !can_set_sched_fifo()){ + if(rtapi_virtual_time() || (!detect_force() && !can_set_sched_fifo())){ cached = REALTIME_TYPE_NONE; return cached; } @@ -1237,6 +1237,11 @@ int main(int argc, char **argv) { } uid_t ruid = getuid(); uid_t euid = geteuid(); + if (rtapi_virtual_time()) { + // Virtual time must never drive hardware: give up root for good, so + // drivers that need privileged access fail to load. + euid = ruid; + } WithRoot::init(ruid, euid); if (setresuid(euid, euid, ruid) != 0) { perror("setresuid"); @@ -1244,7 +1249,16 @@ int main(int argc, char **argv) { } #ifdef __linux__ setfsuid(ruid); - raise_net_admin_ambient(); + if (rtapi_virtual_time()) { + cap_t none = cap_init(); // file capabilities ('make setcap') go too + if (!none || cap_set_proc(none) != 0) { + perror("RTAPI_VIRTUAL_TIME: cannot drop capabilities"); + exit(1); + } + cap_free(none); + } else { + raise_net_admin_ambient(); + } #endif std::vector args; for (int i = 1; i < argc; i++) { diff --git a/tests/virtual-time/README b/tests/virtual-time/README new file mode 100644 index 00000000000..a3c0b5a60e7 --- /dev/null +++ b/tests/virtual-time/README @@ -0,0 +1 @@ +RTAPI_VIRTUAL_TIME=1 runs a single realtime thread without sleeping and on tick-based time. diff --git a/tests/virtual-time/expected b/tests/virtual-time/expected new file mode 100644 index 00000000000..62d01d8b12e --- /dev/null +++ b/tests/virtual-time/expected @@ -0,0 +1,4 @@ +1000000 +1000000 +TRUE +second thread refused diff --git a/tests/virtual-time/setup.hal b/tests/virtual-time/setup.hal new file mode 100644 index 00000000000..c8074426892 --- /dev/null +++ b/tests/virtual-time/setup.hal @@ -0,0 +1,15 @@ +loadrt threads name1=t period1=1000000 +loadrt timedelta +loadrt integ +loadrt comp +addf timedelta.0 t +addf integ.0 t +addf comp.0 t +setp integ.0.in 1 +setp comp.0.in0 10 +net elapsed integ.0.out comp.0.in1 +start +loadusr -w sleep 1 +getp timedelta.0.min +getp timedelta.0.max +getp comp.0.out diff --git a/tests/virtual-time/test.sh b/tests/virtual-time/test.sh new file mode 100755 index 00000000000..4b18e191743 --- /dev/null +++ b/tests/virtual-time/test.sh @@ -0,0 +1,8 @@ +#!/bin/sh +# One real second must cover far more than ten seconds of virtual time, +# and every cycle must see exactly one period on the clock. +RTAPI_VIRTUAL_TIME=1 halrun setup.hal +# A second realtime thread would need a virtual-time scheduler: refused. +echo loadrt threads name1=a period1=1000000 name2=b period2=2000000 > "${TMPDIR:-/tmp}/virtual-time-two.hal" +RTAPI_VIRTUAL_TIME=1 halrun "${TMPDIR:-/tmp}/virtual-time-two.hal" >/dev/null 2>&1 || echo second thread refused +rm -f "${TMPDIR:-/tmp}/virtual-time-two.hal"