Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/src/man/man1/rtapi_app.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 26 additions & 3 deletions src/rtapi/uspace_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdexcept>
#include <atomic>
#ifdef HAVE_SYS_IO_H
#include <sys/io.h>
#endif
Expand All @@ -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);
Expand Down Expand Up @@ -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<void *>(task))) != 0)
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -203,7 +219,7 @@ struct PosixApp : RtapiApp {

void task_self_resync() {
RtapiTask *task = reinterpret_cast<RtapiTask *>(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
Expand All @@ -212,6 +228,8 @@ struct PosixApp : RtapiApp {
}

bool do_thread_lock;
const bool virtual_time;
static std::atomic<long long> vtime; // start of the current cycle [ns], 0 before the first one

static pthread_once_t key_once;
static pthread_key_t key;
Expand All @@ -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;
Expand All @@ -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<long long> PosixApp::vtime(0);

} // namespace

Expand All @@ -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");
}
Expand Down
8 changes: 8 additions & 0 deletions src/rtapi/uspace_rtapi_app.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sys/fsuid.h>
#endif
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <rtapi_atomic.h>
#include <rtapi_stdint.h>
Expand All @@ -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();
Expand Down
18 changes: 16 additions & 2 deletions src/rtapi/uspace_rtapi_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -1237,14 +1237,28 @@ 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");
abort();
}
#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<std::string> args;
for (int i = 1; i < argc; i++) {
Expand Down
1 change: 1 addition & 0 deletions tests/virtual-time/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RTAPI_VIRTUAL_TIME=1 runs a single realtime thread without sleeping and on tick-based time.
4 changes: 4 additions & 0 deletions tests/virtual-time/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1000000
1000000
TRUE
second thread refused
15 changes: 15 additions & 0 deletions tests/virtual-time/setup.hal
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions tests/virtual-time/test.sh
Original file line number Diff line number Diff line change
@@ -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"