Tell the build what your system creates
Needs first: Parameters and launch files
Status: this chapter runs on
nros-board-mps2-an385-freertos: Verified in emulator (daily). From the board support registry; Board Support Levels explains the words.
Every node in the project creates publishers, subscriptions, timers, servers and clients when it starts. On this board, the memory for all of them is set aside when the image is built, so the build must size it before any node has run. Until now it has not known what the nodes create, so it sized every pool for a generous default. In this chapter you write the contract: a file beside the launch file that states what the system creates. You measure the image’s static RAM before and after, and see what the contract decides that the build could not guess, such as how many samples a subscription keeps:
(text 661372 + data 16848 in flash; data 16848 + bss 1318724 in RAM, at link time) before
(text 665124 + data 14968 in flash; data 14968 + bss 1321396 in RAM, at link time) after
Then you break the contract on purpose and read the refusal, which shows what the build does when a statement and the system disagree.
Run every step from the root of sensor_ws. The chapter writes a contract
beside the launch file and adds a table to system.toml:
sensor_ws/
└── src/
└── sensor_node_bringup/
├── system.toml (changed)
└── launch/
└── system.contract.yaml (new)
Measure the image as it is
Build the image:
nros build
It ends with the image’s size, read from the linked image, and the image’s
path after artifact: (a Rust project’s, trimmed):
nros build: size of `sensor_node_bringup:freertos`:
flash 662.3 KiB (678220 B) of 4096.0 KiB (16.2 %)
RAM 1304.3 KiB (1335572 B) of 4096.0 KiB (31.8 %)
(text 661372 + data 16848 in flash; data 16848 + bss 1318724 in RAM, at link time)
nros build: next for `sensor_node_bringup:freertos`:
artifact: /home/you/sensor_ws/build/freertos-zenoh/freertos_entry/target/thumbv7m-none-eabi/release/freertos_entry
Those are the numbers of a Rust project; a C or C++ project’s differ.
text is the code and its constants. data is RAM with initial values, so
its first values live in flash too. bss is RAM zeroed at boot. The
percentages are of the memory the board states it has. This image claims
1,318,724 bytes of zeroed RAM before its first instruction. Most of it is
FreeRTOS’s heap; the rest is what nano-ros sizes.
Where does it go? Each node’s slots, the executor’s storage for every callback, the middleware’s sessions and receive buffers, and a parameter store. The parameter store is sized from the parameters the code declares. The rest is sized for the most the system might create: four samples per subscription, a copy of each topic’s last sample kept for late joiners, and so on. The build warned about some of it on every run, in lines like this one:
warning: nros-rmw-zenoh@0.6.0: the parameter family's requests are not priced: the image declares no `params:`. Its inbox slot keeps 1024 B; a larger `set_parameters` is dropped with no reply. Declaring the parameters in the contract derives it ...
Write the contract
A contract is a YAML file with the launch file’s name and .contract.yaml,
in the same directory. Write launch/system.contract.yaml:
src/sensor_node_bringup/launch/system.contract.yaml# What the system in system.launch.xml creates, so the build can size the
# image for exactly that. Names are the names the code and the launch file
# give: local endpoint names under nodes:, resolved names under topics:.
version: 1
nodes:
sensor_node:
params:
period_ms: { type: integer }
paths:
on_tick:
trigger: { timer: { rate: 2Hz } }
output: [reading]
pub:
reading: { qos: { depth: 1, durability: volatile } }
monitor_node:
params: {}
sub:
reading: { qos: { depth: 1, durability: volatile } }
server_node:
params: {}
srv:
add_two_ints: {}
client_node:
params: {}
paths:
issue_call:
trigger: { timer: { rate: 1Hz } }
output: []
cli:
add_two_ints: {}
action_server_node:
params: {}
action_client_node:
params: {}
topics:
/sensor/reading:
type: tutorial_msgs/msg/Reading
pub: [sensor_node/reading]
sub: [monitor_node/reading]
services:
/add_two_ints:
type: example_interfaces/srv/AddTwoInts
server: [server_node/add_two_ints]
client: [client_node/add_two_ints]
actions:
/fibonacci:
type: example_interfaces/action/Fibonacci
server: [action_server_node/fibonacci]
client: [action_client_node/fibonacci]
src/sensor_node_bringup/launch/system.contract.yaml# What the system in system.launch.xml creates, so the build can size the
# image for exactly that. Names are the names the code and the launch file
# give: local endpoint names under nodes:, resolved names under topics:.
version: 1
nodes:
sensor_node:
params:
period_ms: { type: integer }
paths:
on_tick:
trigger: { timer: { rate: 2Hz } }
output: [reading]
pub:
reading: { qos: { depth: 1, durability: volatile } }
monitor_node:
params: {}
sub:
reading: { qos: { depth: 1, durability: volatile } }
server_node:
params: {}
srv:
add_two_ints: {}
client_node:
params: {}
paths:
issue_call:
trigger: { timer: { rate: 1Hz } }
output: []
cli:
add_two_ints: {}
action_server_node:
params: {}
paths:
tick:
trigger: { timer: { rate: 2Hz } }
output: []
action_client_node:
params: {}
paths:
tick:
trigger: { timer: { rate: 1Hz } }
output: []
topics:
/sensor/reading:
type: tutorial_msgs/msg/Reading
pub: [sensor_node/reading]
sub: [monitor_node/reading]
services:
/add_two_ints:
type: example_interfaces/srv/AddTwoInts
server: [server_node/add_two_ints]
client: [client_node/add_two_ints]
actions:
/fibonacci:
type: example_interfaces/action/Fibonacci
server: [action_server_node/fibonacci]
client: [action_client_node/fibonacci]
src/sensor_node_bringup/launch/system.contract.yaml# What the system in system.launch.xml creates, so the build can size the
# image for exactly that. Names are the names the code and the launch file
# give: local endpoint names under nodes:, resolved names under topics:.
version: 1
nodes:
sensor_node:
params:
period_ms: { type: integer }
paths:
on_tick:
trigger: { timer: { rate: 2Hz } }
output: [reading]
pub:
reading: { qos: { depth: 1, durability: volatile } }
monitor_node:
params: {}
sub:
reading: { qos: { depth: 1, durability: volatile } }
server_node:
params: {}
srv:
add_two_ints: {}
client_node:
params: {}
paths:
issue_call:
trigger: { timer: { rate: 1Hz } }
output: []
cli:
add_two_ints: {}
action_server_node:
params: {}
paths:
tick:
trigger: { timer: { rate: 2Hz } }
output: []
action_client_node:
params: {}
paths:
tick:
trigger: { timer: { rate: 1Hz } }
output: []
topics:
/sensor/reading:
type: tutorial_msgs/msg/Reading
pub: [sensor_node/reading]
sub: [monitor_node/reading]
services:
/add_two_ints:
type: example_interfaces/srv/AddTwoInts
server: [server_node/add_two_ints]
client: [client_node/add_two_ints]
actions:
/fibonacci:
type: example_interfaces/action/Fibonacci
server: [action_server_node/fibonacci]
client: [action_client_node/fibonacci]
Read it from the top:
nodes:has one entry per node, under the name the launch file gives it. Under each, the endpoints the node creates are keyed by their LOCAL name:reading, not/sensor/reading.pub:,sub:,srv:andcli:are publishers, subscriptions, service servers and service clients.params:lists the parameters a node declares. The talker declaresperiod_ms, the one chapter 7’s launch file sets; every other node declares none,params: {}. That is not the same as leavingparams:out, which says nothing.paths:is where timers live. A path with atimertrigger and no input is a periodic callback: the talker’son_tickat 2 Hz (the 500 ms of chapter 7) and the client’sissue_callat 1 Hz. The C and C++ action nodes do their work from a timer too, so their contract adds atickpath for each: the server at 2 Hz, the client at 1 Hz.qos:states the quality of service an endpoint uses.depth: 1keeps one sample, anddurability: volatilekeeps none for late joiners.topics:,services:andactions:wire the endpoints together under their RESOLVED names, and carry their types./sensor/readingis the name after chapter 7’s remap, which is the name the board uses.
The build reads this file every time it builds the image, folds it into the
system it resolved from the launch file, and sizes every pool from it.
Nothing in a node package changes. The contract describes this launch file’s
system, so a second launch file in the bringup gets its own contract, with
its own name before .contract.yaml.
Mark the contract as unchecked
A contract is a statement beside the code, and the build can check it against the code by building the same launch file for your host, running that host program once in a recording mode, and comparing what the nodes created with what the contract says. That check is the census. It needs a host image of the same launch file in the bringup, and this one has only the board’s, so tell the build to warn rather than refuse when no census exists:
src/sensor_node_bringup/system.toml
[census]
on_missing = "warn"
Every build of the image now says, in its output, that its pools come from a contract nothing has compared with the code. Treat that line as a reminder: the contract is yours to keep right until a census checks it.
Measure it again
Build again:
nros build
nros build: size of `sensor_node_bringup:freertos`:
flash 664.2 KiB (680092 B) of 4096.0 KiB (16.2 %)
RAM 1305.0 KiB (1336364 B) of 4096.0 KiB (31.9 %)
(text 665124 + data 14968 in flash; data 14968 + bss 1321396 in RAM, at link time)
The totals barely moved: data fell by 1,880 bytes and bss grew by
2,672. What changed is what the image keeps. /sensor/reading’s receive
buffers hold one sample instead of four, and no copy is kept for late
joiners, because the contract says so. The parameter store was already
sized from the code, contract or not. On a C or C++ image bss grows by more
today, because the build reserves executor capacity the image does not use.
Run it
The image runs as before:
source /opt/ros/humble/setup.bash
nros run
I heard: reading 3
The console says where the depth now comes from, and Ctrl-C stops it:
[WARN] nros: [ 2.125169] declared depth: taking KEEP_LAST(1) on publisher `/sensor/reading`; the call site asked 10 and the build reserved 1. Raise the contract row to keep more (type `tutorial_msgs::msg::dds_::Reading_`).
[INFO] sensor_node: [ 2.132168] Publishing every 500 ms
The talker’s code asks for a history of 10, the ROS 2 default; the image reserved 1, because the contract says the system keeps 1, and the board takes what was reserved. The talker publishes every 500 ms and the listener reads each sample before the next arrives, so one is enough. A node that needs to keep more says so in the contract, and the build reserves it.
Break it on purpose
Rename the talker’s parameter in the contract, from period_ms to period:
src/sensor_node_bringup/launch/system.contract.yaml, change the marked lines sensor_node:
params:
- period_ms: { type: integer }
+ period: { type: integer }
and build:
nros build
The build refuses, before it compiles anything:
Error: 1 launch parameter value(s) disagree with the contract's declarations:
node `/sensor_node`: parameter `period_ms`, set by launch file system.launch.xml, is not declared in its contract (…); it declares: period
The launch file sets period_ms, and the contract says the talker declares
only period. The build does not guess which one is right, and it does not
size the parameter store for the names that matched and ignore the rest. It
stops, naming the node, the parameter and both files. The model it resolved
is set aside as refused, so nothing builds from it until a resolve succeeds.
Put the name back, and the build succeeds again:
src/sensor_node_bringup/launch/system.contract.yaml, change the marked lines sensor_node:
params:
- period: { type: integer }
+ period_ms: { type: integer }
nros build
What just happened
You gave the build the contract, and it used it for static sizing: every pool of the image sized from what the system states it creates, rather than from a default large enough for any system.
A contract states, ahead of the build, what only exists after the program
runs. A node creates its publishers and timers in register or configure,
at boot, but the pools they live in are static arrays whose sizes are
compiled in before any node’s code is. Somebody must state those numbers
before the build, and the contract is where the whole system states them
once, under the same names the launch file and the ROS 2 graph use.
Three rules make it safe to size from a statement:
- Names are resolved names. A contract row matches an endpoint, a parameter or a node by the name the system actually uses, after remaps and namespaces. A row that matches nothing is not used, and a value the launch file sets that the contract does not declare is refused, as you saw.
- All or nothing, per image. If one node of an image is missing from the contract, that image derives nothing and keeps its configured defaults. A count taken over only the nodes the contract describes would be smaller than the image needs, and a pool one slot short fails at boot, so the build refuses to guess “some of them”.
- Checked against the code when it can be. The census compares the
contract with what the nodes actually create. An endpoint the code creates
and the contract omits is refused, because it is the pool that would be
short; one the contract declares and the code never creates is refused
too, unless the bringup waives it under
[census.waive]. A census is fresh by content: touching a file changes nothing, adding a subscription does. This Tutorial’s bringup has no host image to take a census with, so it warns instead;[census] on_stale = "warn"does the same for a census that no longer matches the code.
The same contract works for every language. A C or C++ node package has no
ENTITY_BOUNDS: its counts come only from the contract, which is why a
contract matters most for a C or C++ image. A bringup with no launch/
directory writes the same file as system.contract.yaml beside its
system.toml, and the build carries it to where the launch file is
generated.
Static memory and pools explains what each pool holds and why the sizes derived from messages are upper bounds.
Next
- Same project, another target: the same nodes, the same contract, on a second board.
- Contract format: every key a contract accepts, generated from the format’s own definition.
- Measure static memory: RAM broken down by symbol and by owner.
Design background
The contract and the sizing it drives follow RFC-0124 D6 for this walkthrough;
the census follows the contract’s retirement of the per-component ENTITIES
list, which declared counts beside the code with nothing comparing the two.