One Behavior, Out of Order: Deterministic Execution and Parallel Simulation of ROS 2 Applications
System-level simulation is widely used to validate robotic applications before hardware deployment. However, Robot Operating System 2 (ROS 2), a popular middleware for such applications, uses asynchronous publish-subscribe communication, which can make application behavior depend on scheduling and the platform.
From the abstract. Our summary is in progress.
Full paper
One Behavior, Out of Order: Deterministic Execution and Parallel Simulation of ROS 2 Applications
Published under CC BY 4.0. Reproduced with attribution; original at arXiv:2611.26676 (PDF).
Abstract
System-level simulation is widely used to validate robotic applications before hardware deployment. However, Robot Operating System 2 (ROS 2), a popular middleware for such applications, uses asynchronous publish-subscribe communication, which can make application behavior depend on scheduling and the platform. The behavior can then vary across runs and machines even with the same inputs. This nondeterminism makes faults hard to reproduce and tests unreliable. In this paper, we first give ROS 2 applications a lightweight coordination scheme in which every component, including event-driven ones, reads its inputs and writes its outputs at predefined time instants. By decoupling computation and communication, the scheme makes an application deterministic, that is, it always has one behavior for given inputs. Tests are thus repeatable, but for large applications they still take a long time. To speed up these tests, we then present an out-of-order parallel simulation algorithm. From the predefined read and write instants, it knows before the run which activations of components depend on which. It starts each activation as soon as those it depends on have finished. Finally, we implement the algorithm in a simulator that runs existing ROS 2 applications without any change to them or to ROS 2. We evaluate the simulator on industrial use cases and synthetic graphs. On the industrial use cases, it is up to 2.6 times faster than synchronous parallel simulation and needs an order of magnitude less memory for messages than asynchronous parallel simulation.
I Introduction
Robot Operating System 2 (ROS 2) [1] is widely used in research and industry to build robotic systems, from mobile robots to automated vehicles. System-level simulation plays a pivotal role in their development, since developers can validate a whole application across many scenarios without the costly target hardware. However, the components of a ROS 2 application execute asynchronously and communicate with each other. The behavior of the application therefore depends on the execution times of its components, the communication delays and the scheduling. Their variation makes simulation results differ across runs, even when the application code and inputs remain unchanged [2, 3]. In the example of Fig. 1, Planner computes a command every 20 ms from the latest objects that Detector found in the camera images. In run 2, a variation in the execution time of Detector leads Planner to compute the command at 40 ms from image 2 instead of image 3. If the command computed from image 2 causes a fault, the fault may not appear when the test is repeated, which makes it hard to reproduce. Developers may therefore need to rerun the same test many times to reproduce and diagnose the fault. This additional debugging effort slows development.
AUTOSAR solves this problem for automotive software with a rule by which every component reads its inputs and writes its outputs at predefined time instants, no matter when the computations run in between [4]. This separates computation from communication [5, 6], and the behavior of an application becomes independent of the scheduling and the platform. Following the same principle, our first step gives ROS 2 a lightweight coordination scheme that fixes when each component reads and writes, including event-driven components. Whenever a time-triggered or event-driven component is activated, it reads its inputs at that instant and writes its outputs after a fixed computation delay, even if they are empty. Every output arrives after a fixed transport delay. Since time-triggered components run periodically and the delays are fixed, all reads and writes happen at predefined time instants, as in AUTOSAR. The application therefore becomes deterministic and exhibits one behavior. Tests become repeatable, and application behavior can be validated independently of the target hardware.
However, system-level simulation still becomes time-consuming as applications and scenarios grow, slowing design iteration [7]. Even with one behavior, a recent simulator for ROS 2 applications runs all activations on a single thread, which its authors note increases the run time of complex simulations [8]. To further reduce the validation time, we therefore aim to exploit parallelism within a single simulation while preserving the application’s deterministic behavior. Yet general parallel discrete-event simulation cannot assume that event dependencies are known in advance, so it must preserve causality during the run, either synchronously or asynchronously. Synchronous parallel simulation stops all workers at a global barrier after each time instant or window [9], forcing fast workers to wait for the slowest one. Asynchronous parallel simulation avoids global barriers, either through optimistic execution, which needs state saving and rollback [10], or through conservative execution with null messages [11]. When fast producers run far ahead of their consumers, it can accumulate large buffers of unconsumed messages.
Out-of-order simulation needs none of these mechanisms if the dependencies between events are known. Existing out-of-order simulators derive them from a static analysis of the model or track them during the run [12, 13, 14]. Our key observation is that the coordination scheme already makes these dependencies known in advance, without any such analysis or tracking. The predefined read and write instants determine which outputs each activation reads. An activation therefore need not wait for all earlier activations. Its computation depends only on the activations whose outputs it reads and on the previous activation of the same component, whose updated state it needs. This allows activations of different components to run out of order and in parallel while preserving the one behavior of the application. Building on this observation, we present an out-of-order parallel simulation algorithm for ROS 2 applications. It computes the dependencies on demand from the application graph and the timing parameters, without constructing the full dependency graph. Because a producer may run ahead of its consumers, several outputs may need to be retained at the same time. To bound output buffering, the algorithm uses cyclic storage and allows a slot to be reused only after all readers of its previous contents have finished.
Together, the coordination scheme and the simulator make simulation-based validation of ROS 2 applications both repeatable and fast, which shortens each design iteration. The main contributions of this paper are as follows.
- A lightweight coordination scheme that makes ROS 2 applications deterministic, so that their behavior does not depend on the scheduling or the platform (Section II).
- An efficient out-of-order parallel simulation algorithm that still computes the one deterministic behavior of an application (Section III). We show how to integrate it into ROS 2 so that it simulates existing applications without changing their source code or the libraries of ROS 2 (Section IV).
- We evaluate our simulator on industrial use cases and synthetic graphs. On 256 cores, it is up to
228\timesfaster than sequential simulation, which is 91 % of the theoretical speedup bound. Compared with synchronous parallel simulation, it is up to2.6\timesfaster on the industrial use cases and about2\timesfaster on the synthetic graphs. Its message buffers stay much smaller than those of asynchronous parallel simulation (Section V).
II System Model and Execution Semantics
We model a ROS 2 application as a graph and define our coordination scheme for deterministic execution.
II-A System Model
From a logical perspective, a ROS 2 application is a set of nodes that communicate using the publish-subscribe paradigm [1]. Nodes publish messages on named topics and subscribe to topics to receive messages. A node maintains state in member variables and may have timers and subscriptions, each associated with a callback, a function invoked in response to an event. Timer callbacks are time-triggered, activated periodically, whereas subscription callbacks are event-driven, activated by incoming messages. Callbacks may update node state and publish messages that activate other callbacks, forming a network of topics and callbacks.1 In Fig. 1a, capture and plan are time-triggered, and detect and store are event-driven.
We model an application as a finite directed graph G=(V,E). Each vertex z\in V is a callback, and each edge in E\subseteq V\times V is a communication channel. An event-driven callback u has one input topic, the topic of its subscription. Every callback z has a fixed finite output set O_{z}, which contains the topics on which z may publish. A pair e=(z,u) is in E if and only if u is event-driven and its input topic is in O_{z}.
The callbacks of one node form a task. Each task \tau_{i} has a state s_{i} with initial value s_{i,0}, a list X_{i} of time-triggered callbacks and a list U_{i} of event-driven callbacks. The state models the member variables of the node, and only the callbacks of this task can read and update it. Each time-triggered callback x\in X_{i} has a period T_{x}\in\mathbb{Z}_{>0} and an offset \phi_{x}\in\mathbb{Z}_{\geq 0}, and it is activated at the instants \phi_{x}+mT_{x} for m\in\mathbb{Z}_{\geq 0}. A callback z of \tau_{i} computes a function f_{z}. Its inputs are the state s_{i}, the current time and, if z is event-driven, the incoming message. Its outputs are the new value of s_{i} and the messages that z publishes on the topics in O_{z}. We assume that f_{z} is deterministic, that is, its outputs depend only on its inputs.
\tau_{3}. (b) A light bar spans from the release instant to the write instant of a job, and an arrow goes from the write of a record to its arrival. Darker boxes show when the jobs run in one run. The dashed record is empty and releases the skipped job J_{3,6}. (c) Grey arrows are state dependencies and black arrows are record dependencies.Example 1.
Fig. 2a shows the graph of the application of Fig. 1a, with x_{1}, u_{2}, u_{3} and x_{3} for capture, detect, store and plan. The output sets are O_{x_{1}}=\{\texttt{/img}\}, O_{u_{2}}=\{\texttt{/obj}\}, O_{x_{3}}=\{\texttt{/cmd}\} and O_{u_{3}}=\emptyset. The input topics of u_{2} and u_{3} are /img and /obj, so E has only e_{1}=(x_{1},u_{2}) and e_{2}=(u_{2},u_{3}). The state s_{3} holds latest, which u_{3} writes and x_{3} reads. \diamond
II-B Execution Semantics
To make the behavior of an application independent of the scheduling and the platform, we separate computation from communication by fixing the instants at which callbacks read their input messages and write their output messages. For this, every task \tau_{i} has a computation delay D_{i}\in\mathbb{Z}_{>0} and every channel e has a transport delay \delta_{e}\in\mathbb{Z}_{\geq 0}, both predefined and fixed. Channels from callbacks of the same task to the same callback have the same transport delay.
Every callback releases a sequence of jobs. A job of a callback of \tau_{i}, which we also call a job of \tau_{i}, has a release instant and a write instant, which is D_{i} later. It reads its input messages at its release instant and writes its output messages at its write instant, no matter when it runs in between. It writes them in records, one on every topic in the output set of its callback. A record holds the messages that the job publishes on its topic, in the order in which it publishes them. It is empty if the job publishes nothing on the topic, for example because its callback publishes only for some inputs.
A time-triggered callback releases a job at every instant at which it is activated. A record that a job of z writes on the input topic of u enters the channel e=(z,u) when it is written and arrives at u at its arrival instant, which is \delta_{e} later. At this instant it releases a job of u, and only this job consumes it, that is, removes it from the channel and reads its messages. Since \delta_{e} is fixed, every channel is a first-in first-out queue of records. An empty record releases a job like any other record, so whether a job is released does not depend on what the callbacks compute.
In ROS 2, the executor decides in which order the ready callbacks of a node run, and the default executor runs timer callbacks first [16, 17]. We fix this order by a rule of our own. All jobs of \tau_{i} form one sequence in the order of their release instants, and J_{i,k} denotes the k-th of them. For jobs with the same release instant, jobs of time-triggered callbacks come first, in the order of X_{i}, and jobs of event-driven callbacks follow, in the order of U_{i}. Jobs of the same event-driven callback are ordered by the jobs that wrote their records, first by the index of the task and then by the index of the job.
The jobs of \tau_{i} run one after the other in this sequence. Each job starts from the state that the previous job left, even if it is released before the previous job writes, and the first job starts from s_{i,0}. Whenever a job reads the current time, it gets its release instant. A job of a time-triggered callback x calls f_{x} once. A job of an event-driven callback u calls f_{u} once for each message of the record that it consumes, in the order of the messages. If this record is empty, the job is skipped, that is, it calls nothing, keeps the state and writes only empty records. So an empty record comes from a job that publishes nothing on a topic or from a skipped job.
Example 2.
Fig. 2b shows the jobs of the running example with the parameters of Fig. 2a. Job J_{2,2}, released at 30 ms, runs in two pieces but still writes its record on /obj at 30+D_{2}=55 ms. This record arrives at 55+\delta_{e_{2}}=59 ms and releases J_{3,4}, which stores the detection in s_{3}. The next job J_{3,5} of \tau_{3} is released at 60 ms, so it computes its command from this detection in every run. Job J_{2,3} finds no object, so its record is empty and the job J_{3,6} that it releases is skipped. \diamond
II-C Determinism
Theorem 1.
The jobs, their release and write instants, their order in each task and which record each job reads depend only on the application graph model, the tasks, the periods, the offsets and the delays. The states and the records depend in addition only on the initial states and the functions f_{z}. So the behavior of the application, that is, its non-empty records with their write instants, depends neither on the scheduling nor on the platform.
Jobs are released only at predefined instants, so we use induction on the release instant t. Since D_{i}>0, a job released at t is released by a time-triggered callback or by a record of a job released before t, and this record is written whether it is empty or not. So the jobs released at t, their order and the records that they read do not depend on what the callbacks compute. We take the jobs of each task in the order of its sequence. Their inputs then depend only on the initial states and on the outputs of jobs that come before them, and so do their outputs, since every f_{z} is deterministic. ∎
II-D Discussion
The main benefit of empty records is that the jobs and their dependencies are known before the run, as Theorem 1 states. Without them, a callback that publishes only for some inputs would release jobs of other callbacks only for these inputs, so the jobs would depend on the computed values. With them, every job depends only on the previous job of its task and on the job that wrote the record that it consumes, and both come before it. So the dependencies form a directed acyclic graph, even if the application graph model has cycles. Fig. 2c shows this graph for the running example. Section III shows how to run jobs out of order and in parallel by computing their dependencies from the parameters, without building the graph.
III Out-of-Order Parallel Simulation
As discussed in Section II-D, the dependencies between jobs form a directed acyclic graph. A simulation that runs the jobs in a topological order of this graph is correct, that is, every job writes the same records as in the semantics of Section II-B. So a simulator can run jobs out of the order of their release instants and in parallel, as long as every job starts only after the jobs that it depends on have finished.
Example 3.
In Fig. 2c, J_{3,5} depends, directly or through other jobs, only on J_{1,1}, J_{1,2}, J_{2,1}, J_{2,2} and J_{3,1},\dots,J_{3,4}. So the simulator can run J_{3,5} in parallel with J_{1,3}, or even finish it before J_{1,3} starts, although J_{3,5} is released at 60 ms and J_{1,3} at 30 ms. \diamond
III-A Activation Streams
To execute jobs in a topological order, the simulator needs to identify their dependencies. We can compute these dependencies on demand using only the application graph model parameters, without constructing or storing the full job dependency graph. To this end, we group the jobs into periodic sequences, in which the record and state dependencies of a job (Fig. 2c) follow from its position.
A time-triggered callback releases a job in every period. Each of these jobs writes a record on every topic in its output set, empty or not, and after fixed delays each record releases one job of every callback that receives it. So these jobs are periodic as well, with the same period and a later offset, and so are the jobs that their records release. We call such a periodic sequence of jobs of one callback an activation stream. A stream \sigma has a period T_{\sigma} and an offset \phi_{\sigma}, and its m-th job J(\sigma,m), with m\in\mathbb{Z}_{\geq 0}, is released at r_{\sigma}(m)=\phi_{\sigma}+mT_{\sigma}. Every time-triggered callback x has a root stream with period T_{x} and offset \phi_{x}. For each channel e=(z,u), every stream \sigma of callback z in task \tau_{j} has a child stream \nu of callback u. The child stream has period T_{\nu}=T_{\sigma} and offset \phi_{\nu}=\phi_{\sigma}+D_{j}+\delta_{e}. We call \sigma the parent of \nu. Job J(\nu,m) consumes the record written by J(\sigma,m), so J(\sigma,m) is its record dependency.
Assume that we simulate the jobs released in the interval [0,H), for a given H\in\mathbb{Z}_{>0}. Since D_{j}>0, a child stream has a later offset than its parent, so only finitely many streams have an offset before H. Such a stream \sigma has M_{\sigma}=\lceil(H-\phi_{\sigma})/T_{\sigma}\rceil jobs in [0,H).
The state dependency of a job is the previous job in the sequence of its task. This sequence merges the jobs of all streams of the task and orders them by release instant, which r_{\sigma}(m) gives. Jobs of a task with the same release instant belong to different streams. To order them, we give every stream \sigma of \tau_{i} with an offset before H a rank q_{\sigma}, its position in the following order, which follows the rule of Section II-B. Root streams come first, in the order of X_{i}. Child streams follow, ordered by the position of their callback in U_{i}, then by the index of the task of their parent, and then by the rank of their parent. This is not circular, since a parent has an earlier offset than its child. The key of J(\sigma,m) is \kappa_{\sigma}(m)=(r_{\sigma}(m),q_{\sigma}), compared first by release instant and then by rank. By the following lemma, the keys give the sequence of the task, so the state dependency of a job is the job of the same task with the next smaller key.
Lemma 1.
Every job released in [0,H) is the job J(\sigma,m) of one stream \sigma and one m<M_{\sigma}. In the order of their keys, the jobs of \tau_{i} released in [0,H) are the first jobs J_{i,1},J_{i,2},\dots of its sequence.
By induction on the release instant, every job is in one stream, since it is released by a time-triggered callback or by the record of one earlier job. The ranks follow the rule of Section II-B, with the parents of two streams in place of the jobs that wrote the records. When these jobs are in the same task, their channels have the same transport delay, so they have the same release instant, and by induction their order is the order of the ranks of the parents. ∎
Example 4.
With H=70 ms, the running example has four streams. The root stream \sigma_{1} of x_{1} has period and offset 10 ms, and its child \sigma_{2} is a stream of u_{2} with offset 20 ms. The child \sigma_{3} of \sigma_{2} is a stream of u_{3} with offset 20+D_{2}+\delta_{e_{2}}=49 ms. The root stream \sigma_{4} of x_{3} has period and offset 20 ms. In \tau_{3}, \sigma_{4} has rank 1 and \sigma_{3} has rank 2, so the keys (20,1), (40,1), (49,2), (59,2), (60,1), (69,2) give J_{3,1},\dots,J_{3,6}. \diamond
III-B Bounded Record Storage
With the dependencies of Section III-A, the simulator can run jobs out of order. The remaining issue is storage. A stream can then run far ahead of its children, that is, many of its jobs finish before the jobs that consume their records start. The simulator must store these records in the meantime, and their number is bounded only by the number of jobs of the stream, which grows with H. To bound it, every stream keeps its records in a ring with a fixed number of slots, which its jobs reuse in turn. A job that reuses a slot must then wait until the records in it have been consumed. This adds a third kind of dependency. If the dependencies had a cycle, the jobs on it would wait for each other, and none of them could start. So we first define the rings and then choose their size such that the dependencies still form a directed acyclic graph.
Let the ring of a stream \sigma have K_{\sigma} slots. Each slot holds the records of one job. Job J(\sigma,m) writes its records into slot m\bmod K_{\sigma}. For m\geq K_{\sigma}, it replaces the records of J(\sigma,m-K_{\sigma}). These records are consumed by the jobs J(\nu,m-K_{\sigma}) of the children \nu of \sigma, which we call the slot dependencies of J(\sigma,m). So J(\sigma,m) can start only after its slot dependencies have finished.
The dependencies stay acyclic if every slot dependency of a job is released before the job. Let \sigma be a stream of a callback of \tau_{j}, and let \delta_{\sigma} be the largest transport delay of the channels to its children, or 0 if it has no children. The records of J(\sigma,m-K_{\sigma}) arrive at most D_{j}+\delta_{\sigma} after its release, and they release the slot dependencies of J(\sigma,m) when they arrive. Job J(\sigma,m) is released K_{\sigma}T_{\sigma} after J(\sigma,m-K_{\sigma}), so we need K_{\sigma}T_{\sigma}>D_{j}+\delta_{\sigma} and choose
K_{\sigma}=\max\left(2,\ \left\lfloor\frac{D_{j}+\delta_{\sigma}}{T_{\sigma}}\right\rfloor+1\right).The second term is the smallest such number, and the term 2 lets a stream run at least one job ahead of its children.
Lemma 2.
Every slot dependency of a job is released before the job.
The slot dependency J(\nu,m-K_{\sigma}) of J(\sigma,m), with \nu on a channel e, is released at r_{\sigma}(m)-K_{\sigma}T_{\sigma}+D_{j}+\delta_{e}, which is before r_{\sigma}(m) by (1). ∎
So every dependency of a job is released before it or comes before it in its task, and the dependencies still form a directed acyclic graph.
Example 5.
In Example 4, K_{\sigma_{1}}=\max(2,\lfloor 10/10\rfloor+1)=2 and K_{\sigma_{2}}=\max(2,\lfloor(25+4)/10\rfloor+1)=3. So J(\sigma_{2},3), released at 50 ms, reuses the slot of J(\sigma_{2},0), and its slot dependency is J(\sigma_{3},0), released at 49 ms. Job J(\sigma_{1},4), also released at 50 ms, waits for no job of \tau_{3}, directly or through other jobs, so it can run before J(\sigma_{4},0), which is released at 20 ms. \diamond
III-C The Simulation Algorithm
By Sections III-A and III-B, a job can start when its state, record and slot dependencies have finished. Algorithm 1 runs the jobs in this way on a pool of worker threads. It has to find the jobs that can start and to make sure that each of them runs only once. It does both without a list of jobs.
Every child stream has one parent, so the streams form trees whose roots are the root streams. Before the run, the simulator searches these trees breadth first and keeps the streams with an offset before H. It skips the children of all other streams, so the search always ends. The simulator then computes the ranks and the ring sizes. To find the next job of each task efficiently during the run, each task keeps the next job of each of its streams in a heap ordered by key. By Lemma 1, the first job in the heap is the next job of the task. The heap has at most one entry per stream, so no list of jobs is stored.
Each task \tau_{i} also has three values. Workers read and write them and the next job of the task atomically, in one total order. The completion count c_{i} is the number of its finished jobs, and the completion key \lambda_{i} is the key of its last finished job. The jobs of a task finish in the order of their keys, so a job of \tau_{i} has finished if and only if its key is at most \lambda_{i}. So one comparison of keys tells whether a dependency has finished. To run every job only once, a worker must claim a job before it runs it, and the claim count a_{i} is the number of claimed jobs of \tau_{i}. TryEnqueue(\tau_{i}) looks only at the next job J(\sigma,m) of \tau_{i}, since the jobs of a task run in the order of its sequence. If a_{i}=c_{i}, no job of \tau_{i} is claimed and unfinished, so the state dependency of J(\sigma,m) has finished (line 3). Lines 5 and 6 check its record dependency J(\pi,m) and its slot dependencies J(\nu,m-K_{\sigma}). If all have finished, the worker claims J(\sigma,m) by a compare-and-swap (CAS) that changes a_{i} from c_{i} to c_{i}+1 and fails if another worker has claimed a job of \tau_{i} in the meantime (line 7). It then pushes the job onto its local double-ended queue (deque).
Each worker takes the jobs from its local deque one at a time. When its deque is empty, it steals a job from the deque of another worker, which balances the load among the workers (work stealing [18]). It runs a job J(\sigma,m) of \tau_{i} as in the semantics, with its release instant as the current time, and writes its records into slot m\bmod K_{\sigma}. It then replaces J(\sigma,m) in the heap by J(\sigma,m+1) if m+1<M_{\sigma}, sets the next job of \tau_{i}, sets \lambda_{i} to \kappa_{\sigma}(m) and increases c_{i}, in this order, so that a worker that reads the new c_{i} also reads the new next job. After that, the next job of \tau_{i}, a job that consumes a record of J(\sigma,m), or a job that reuses a slot whose records J(\sigma,m) consumed may be able to start. So the worker calls TryEnqueue for \tau_{i} and for every task that has a child or a parent of a stream of \tau_{i}. Because of the total order, the worker that finishes the last dependency of a job sees that all of them have finished, so no job is missed. The dependencies form a directed acyclic graph, so every job then runs once, and the simulation cannot deadlock.
Complexity. Let n_{i} be the number of streams of \tau_{i}, and let d be the largest number of channels that enter or leave the callbacks of one task. After a job of \tau_{i} has run, the worker performs one heap operation in O(\log n_{i}) time and calls TryEnqueue for at most d+1 tasks. Each call compares one pair of keys per dependency of the next job, in O(1+d) time.
IV ROS 2 Integration
We implemented the simulator of Section III as a lightweight layer for ROS 2 Jazzy.2
ROS 2 is organized in layers [1]. Fig. 3 shows them together with our simulator. The nodes are written with a client library, which is rclcpp for C++ [17]. It provides the timers, the subscriptions, the publishers and the ROS clock of a node, and its executors decide when the callbacks run [16]. Below it, the C library rcl passes a published message through the interface rmw to the middleware, which delivers it to the subscriptions of the topic [19]. The middleware is usually an implementation of the Data Distribution Service (DDS) [20].
Our simulator is directly below rclcpp. To simulate the application as in the semantics of Section II-B, it runs the callbacks, carries the messages and gives the time itself. To run a job, a worker of Algorithm 1 calls its callback through the timer, or through the subscription once for every message of the consumed record, so no executor is started. To take the published messages and to give the time, the simulator defines its own versions of the functions of rcl that publish a message and that read the ROS clock. They are part of the executable that loads the nodes, so rclcpp calls them in place of the ones in rcl, which stays unchanged. A published message is then stored in serialized form in the slot of the running job, and the ROS clock returns the release instant of this job. The simulator also turns off the direct delivery of messages inside one process, so that every message goes through a record. The rest of ROS 2 is used as it is, without any change.
Using the simulator takes little effort. The user does not write the application graph model of Section II-A, because the simulator reads it from the nodes themselves. It finds the timers and subscriptions of every node and connects publishers and subscriptions by topic name and message type. It loads the nodes from their existing shared libraries, so they need no recompilation, or it creates them by the functions that construct them. Besides the application, the simulator only needs the delays, which the user gives in files.
V Evaluation
V-A Setup
Workloads and simulators. We evaluate our simulator on three industrial use cases, the Autoware reference system (RS) [21], fleets of vehicles built from RS (Fleet-N), and a stereo vision pipeline (Stereo) [22, 23]. RS is a ROS 2 benchmark modeled on the autonomous driving software Autoware. Fleet-N runs N copies of RS as vehicles that exchange detected objects, with different timer offsets. Stereo runs the stereo vision nodes of image_pipeline on recorded camera frames. We also evaluate 1385 synthetic graphs, including randomly generated ROS 2 graphs and those derived from automotive benchmarks [24, 6].
We compare our simulator with sequential simulation, with the synchronous parallel simulations Sync-I and Sync-W, and with the asynchronous parallel simulation Async. Sequential simulation runs one job at a time. Sync-I and Sync-W run the jobs released in a time window in parallel and wait at a global barrier at its end [9]. Sync-I runs in parallel only the jobs released at the same instant, and waits at a barrier before the next instant. Sync-W has windows as long as the smallest computation delay, so that the jobs of different tasks in a window do not depend on each other. Async is a conservative simulation with null messages and without global barriers [11, 25]. In Async, a task runs its next job as soon as it knows all records that the job reads, and an optional lead bounds how far a task may run ahead of its readers.
Parameters and measurements. We ran all simulators on ROS 2 Jazzy on a machine with two AMD EPYC 9755 processors and 256 cores, with each worker pinned to one core. The computation delay of a node is its smallest timer period, or, if it has no timers, the smallest computation delay of the nodes that publish to it.
We measure how fast each simulator is and how many messages it holds. The speedup of a simulator is the time of sequential simulation divided by its time. To see how much of the parallelism of a workload a simulator uses, we compare its speedup with a bound that depends only on the workload. Let W be the total computation time of all jobs and S that of the longest chain of jobs in which each job needs the state or a record of the job before it, both measured in sequential simulation. The jobs of such a chain run one after another, so any simulator takes at least S, and p workers take at least W/p. The speedup on p workers is therefore at most \min(p,W/S), which we call the bound. The ratio W/S is the average number of jobs that the workload can run at the same time. This ratio is 3.7 for RS and 7.9 for Stereo, and it grows in proportion to the number of vehicles in the fleets, up to 252 for Fleet-64. The message bytes of a simulator are the most bytes of messages that it holds at one time.
V-B Results
Speedup. On Fleet-8, our simulator follows the bound and is 29.1 times faster than sequential simulation on 32 workers, which is 93 % of the bound (Fig. 4a). Sync-W reaches 14.6, and Sync-I stays below 3, because the vehicles have different offsets and few jobs share an instant. When all vehicles have offset 0, Sync-I and Sync-W reach speedups of 15.1 and 18.0, and our simulator reaches 28.9. Aligning the release instants therefore improves the speedup of synchronous simulation, while the speedup of our simulator stays almost the same. With transport delays of 0 to 100 ms between the vehicles of Fleet-8, our simulator stays between 29.0 and 30.9 times faster than sequential simulation on 32 workers. RS and Stereo have bounds of only 3.7 and 7.9 (Fig. 4b). Our simulator reaches the bound of RS on 4 workers, where Async and Sync-W reach 2.8 and 2.5. On Stereo, it reaches 6.7, and Async reaches 7.4.
Scaling. On the whole machine, Fleet-64, with 1600 nodes and 566 353 jobs in 10 s, takes 228 s in sequential simulation (Fig. 4c). Our simulator takes 1.0 s on 256 workers, so it is 228 times faster, which is 91 % of the bound of 252. Sync-W reaches 89, so our simulator is 2.6 times faster. In every window, Sync-W waits for the task with the most work, and our simulator does not.
Message bytes. Sync-W holds as many message bytes as our simulator, or 1.5 times as many on Stereo, but Async holds 12 to 42 times as many, since a task of Async can run far ahead of its readers and its records then wait in queues (Fig. 4d). On Stereo, Async is about as fast, but it holds up to 6.3 GB of messages at one time, while our simulator holds at most 0.5 GB. A lead bounds the queues, but it has to be chosen for each workload (Fig. 5b). On Fleet-8, Async without a lead is 1.4 times slower and holds 35 times as many message bytes, while leads from 25 to 200 ms make it as fast as our simulator with 1.4 to 2.9 times its message bytes.
Synthetic graphs. Fig. 5a shows the speedups of our simulator and Sync-W on the synthetic graphs with 32 workers, plotted against the average number x of jobs in a nonempty window of Sync-W. If all jobs take the same time, the speedup of Sync-W is at most \min(x,32). Across all 1385 synthetic graphs, our simulator is 1.98 times as fast as Sync-W on average (geometric mean). Async is about as fast as our simulator but holds 45 times as many message bytes. With leads of 0.25 to 8 times the longest timer period, Async still holds 2.2 to 27 times as many message bytes (Fig. 5b).
VI Related Work
Safety-critical embedded systems need behavior that is deterministic and predictable [3]. The time-triggered architecture was designed for this, with computation and communication at predefined time instants [26]. Henzinger et al. developed this idea into the logical execution time (LET) [27], which our coordination scheme follows. AUTOSAR supports LET [4]. LET fixes the instants of reads and writes on one machine, but messages between machines take a varying time. System-level LET [28] therefore gives the communication between machines a fixed delay, as our transport delays do. The Adaptive Platform of AUTOSAR [29] comes closest to ROS 2, since its applications also run on POSIX and communicate by publish and subscribe. Menard et al. show that it is not deterministic by itself and add a coordination layer to it [30]. Bellassai et al. instead bring system-level LET to it [31] and to its publish-subscribe communication [32]. In both works, LET applies only to time-triggered tasks. Our scheme also applies to event-driven callbacks. An empty record tells such a callback that no message comes. In this way, the absence of a message also carries information, as Lamport observed for bounded message delays and synchronized clocks [33].
Three recent approaches make the simulation or the execution of ROS 2 applications deterministic. Sagmeister et al. run the callbacks of all nodes sequentially on a single thread with a simulated clock [8]. Otto uses an orchestrator that forwards the messages between nodes in an order that it builds during the run [34]. Teper et al. map a ROS 2 application to Lingua Franca [35], a coordination language based on the deterministic reactor model [36]. Each node becomes a reactor, a component that reacts to timestamped events, and each callback becomes one of its reactions. The runtime of Lingua Franca then calls the unmodified callbacks in timestamp order. Kwok et al. instead build a robotic middleware on Lingua Franca to replace ROS 2, and show how to move ROS 2 programs to it [37]. Although Lingua Franca can also express LET [38], its runtime is synchronous by default and completes all reactions at one timestamp before it advances to the next. By default, none of the three runs callbacks of different timestamps in parallel, since a callback may or may not publish and which callback runs next is known only during the run. With empty records, the jobs and their dependencies are known before the run, so our simulator runs jobs out of order and in parallel. An empty record plays the role of a null message in conservative parallel simulation [11, 25], but it is part of our coordination scheme and not an extra message of the simulator.
Henzinger and Kirsch describe semantics-preserving compilation of LET programs for a single processor [39]. Biondi and Di Natale implement LET on multicore platforms [40]. We use LET to coordinate ROS 2 callbacks, defining the application behavior that our simulator reproduces. The final step would be to generate code that preserves validated behavior on the target platform.
VII Conclusion
We have shown that fixing when every callback reads and writes makes a ROS 2 application deterministic. With empty records, it also fixes the jobs and their dependencies before the run. Based on this, our simulator runs the jobs out of release order and in parallel. It reached 91 % of the theoretical speedup bound on 256 cores. Our coordination scheme currently does not support several publishers of one node on the same topic or applications whose structure changes during a run, and both are worth addressing in the future.