What is an MCAP file? How to open, read and convert it
What an MCAP file is, how ROS 2 records it, and how to inspect, read in Python, convert from .db3, compress and recover MCAP files on real robots.
By Eitan Chudnovsky →How to measure control-loop rate, jitter and late iterations on a robot running a model, find the line that's slow, and structure loops that hold.

"The robot feels slow." I hear that sentence a lot. Sometimes it means cycle time went from 14 seconds to 17. Sometimes it means the arm hesitates before every grasp. Sometimes it means a motion that used to be smooth now has a small shake in it.
Almost every time, the cause is the control loop.
Control loops used to be boring in the good way. A fixed program read sensors, computed a command, wrote it, slept. Same work every iteration, same time every iteration. Now there is a learned model in the loop. It decides from what the cameras see, many times a second, and its compute time moves around: the first call is slow, a thermal throttle makes it slower, a bigger checkpoint makes it slower still. The model changes with every release and the robot is in a factory, not on your desk. A loop that held 30 Hz in the lab can quietly run at 22 on the floor, and the robot's behavior changes with it.
This post is about measuring that properly, finding the cause, and structuring loops so they hold up.
The short answer: Usually it's the control loop, not the model. Measure the achieved loop rate, p99 iteration time, jitter and the share of late iterations, then find the slow line with a stack sampler. The usual causes are blocking serial or camera reads, model time that moves around, garbage-collection pauses, logging inside the loop and other processes on the same cores.
Most teams measure inference time. That's one part of the iteration. The robot cares about the whole iteration. Measure these per loop:
hz=30 and the loop does 24, the robot is running at 24.Starting points for thresholds, to adjust for your robot:
Three mistakes I see in almost every hand-rolled loop timer:
time.time() jumps when NTP corrects the clock. On a robot that just got network after an hour offline, that can be seconds. Use a monotonic clock (time.monotonic() or time.perf_counter() in Python).sleep(1/30) after the work makes every iteration take work plus 33 ms. Sleep until the next deadline instead, and if you are already past it, don't sleep at all and count the iteration as late. If you are far past it, skip ahead rather than trying to catch up with a burst of fast iterations.When a loop is slow or jittery in production, it's almost always one of these.
read() on a camera at the top of the loop often blocks until the next frame arrives. A camera at 30 fps can add up to 33 ms to any iteration, and at 15 fps in low light (many webcams drop frame rate when it gets dark) it can add up to 66. Grab frames in their own thread and have the loop take the latest one, with its timestamp..item(), .cpu(), printing a tensor) stalls the loop until the GPU finishes. Warm the model up before the robot moves, watch device temperature next to inference time, and keep sync points to one per iteration.gc.freeze() after startup so the collector stops rescanning everything you loaded at boot.You know the loop is slow. Now you need to know where. Adding timers around every block works, but it's slow to iterate on, especially on a robot you can't easily redeploy.
Stack sampling is better. A sampler looks at the loop thread's call stack many times a second and counts which function and line it's in. After a minute you get a ranking: "read_encoders (robot.py:40), waiting, 92% of the time the loop was slow". That one line tells you it's the serial bus, not the model, and not your code's math.
Two things to know about sampling in Python:
Out-of-process samplers like py-spy work the same way and are great on a bench. In production, you want the sampler already running when the slowdown happens, because by the time someone opens a terminal, it has usually stopped.
Here is how I'd structure a robot app with a model in it. This is opinionated, on purpose.
read() that blocks. Everything goes through queues and threads that can be slow without slowing the loop.Some timing problems take hours or days to show.
try: read_gripper() except TimeoutError: pass. It was written for the rare timeout. Now the gripper's cable is loose and it times out on every iteration. Each timeout costs the full timeout period. The loop slows down, nothing is logged, and nobody looks at the gripper. Count caught exceptions per place in the code, and flag a place that suddenly catches far more than it used to. Exceptions you catch all the time by design, like an empty queue, are normal and should be ignored.One last thing. In a fleet, the most useful question is often "why is this robot slower than the others running the same code?" If eight arms run the loop at 30 Hz and one runs at 12, the problem is almost never the software. It's that robot's SD card, its USB hub, a servo with a bad connector, a fan that stopped and a board that throttles. Compare each robot's loop rate and jitter with its peers and with its own history. We wrote more about this in finding the robot that's different.
And loop timing is only one part of watching a policy. For what to check on the model's inputs and outputs, see how to monitor a robot policy in production.
When your Python app runs under pf run or imports physicalfleet, PhysicalFleet measures the loop around the model with no code changes: rate, median and p99 iteration time, jitter, late iterations, and the split between the model and the rest. When a loop slows down, a stack sampler running in the app names the function and line taking the time and whether it was waiting. It also watches memory and GPU memory trends with time until out of memory, thread growth, error log lines and exceptions the app catches. For an exact target and phases, pf.loop("control", hz=30) makes the loop explicit. More on the observability page.
If you have a timing problem that took weeks to find, I'd really like to hear about it.
Jitter is how much the loop's period varies from one iteration to the next. A practical measure is the p99 of the period minus the median, in milliseconds: most iterations take 31 ms, the bad ones take 48. Watch it together with the share of late iterations, because a loop with a fine median and many late iterations has a stutter.
Use a monotonic clock like time.monotonic() or time.perf_counter(), never time.time(), which jumps when NTP corrects the clock. Measure from the start of one iteration to the start of the next, so time spent waiting on sensor reads is included. Sleep until the next deadline instead of sleeping a fixed period after the work.
Allocate less inside the loop: preallocate buffers, reuse arrays and avoid creating many small objects every iteration. Calling gc.freeze() after startup stops the collector from rescanning everything you loaded at boot. GC pauses usually show up as periodic late iterations while the median looks fine.
No. Split it into a fast control loop that follows the current target and writes commands, and a slower policy loop that runs the model and hands over new targets. The control loop never waits for the model. Give every target the time of the observation it came from, and hold position or stop if the newest one is too old.
Use a stack sampler on the loop thread. It looks at which function and line the thread is in many times a second, and after a minute it ranks where the time went, including whether the thread was waiting or running. In production you want the sampler already running, because a slowdown has often stopped by the time someone logs in.

What an MCAP file is, how ROS 2 records it, and how to inspect, read in Python, convert from .db3, compress and recover MCAP files on real robots.
By Eitan Chudnovsky →
Why a ROS 2 node can look alive while its topic goes quiet, how to catch it by hand and in code, and how to alert on silence across a robot fleet.
By Amit Ezer →Connect your first machine in minutes, develop it in the lab and run your whole fleet in production from one place.
Free for up to 3 machines · Then pay as you go