alp@vilnius: ~ up since 2019 · Vilnius Download cv.pdf

← ALP-OS

alp ~ $ work ram-budget

Three features against one RAM budget

400 geofences, 10,000 driver IDs and two data connections at once, on a tracker already short on RAM. I estimated each before writing code, and each hit a different limit: free RAM, boot time, or room for one TLS session.

24 kBfree RAM with 400 geofences

Tracker firmware runs on a fixed amount of RAM, and most of it is already taken. Three requests each needed more of it. A client wanted 400 geofences, each a 15-point polygon. Another project needed the driver ID list to hold 10,000 entries. A customer needed two data connections on different APNs, open at the same time.

I wrote an estimate for each before writing any code: 154 h of firmware work plus 40 h for the configuration tool, 60 h, and 116 h. Each one hit a different wall: free RAM, boot time, and room for only one TLS session.

Geofences: measure, then cut

Every polygon point lives in RAM. With 400 zones, I measured 42 kB of free RAM at 10 points per polygon, and 24 kB at 15. That was an early measurement, and it was dangerously low.

Free RAM with 400 zones, preliminary measurement

Coordinates come with six decimal places, about 10 cm, and were stored as doubles. I pushed for 32-bit floats instead: half the bytes per coordinate, and a little less precision. I also pushed back on per-zone names as scope creep. It still didn’t fit, so I removed reserved features the build never used, and shortened a rarely used list to free 2.4 kB.

What the cuts broke

Trimming had removed some base parameters by mistake, so the immobilizer re-locked after unlock. I restored them. The #ifndef blocks I added to save RAM overlapped and changed an IO default, so an alarm event was always on. I patched that. And the configuration tool still treated coordinates as doubles, so every config showed unsaved changes. I updated it to float.

The geofence part: where the RAM for 400 polygons came from, and what the cuts broke Top, bytes drawn to scale: one polygon point is a latitude and a longitude. Stored as doubles it takes 8 bytes per coordinate, 16 bytes per point. Stored as 32-bit floats it takes 4 bytes per coordinate, 8 bytes per point. Coordinates arrive with six decimal places, about 10 cm; floats lose a little of that precision, which a geofence edge can afford. Every point of 400 zones with 15 points each is held in RAM. Middle, schematic: in that build, a rarely used list was shortened, freeing 2.4 kB. Removed reserved functionality the build never used also made room. Bottom: three things the cuts broke, each with its fix. The trimming removed some base parameters by mistake, so the immobilizer re-locked after unlock; restoring the parameters fixed it. #ifndef blocks added to save RAM overlapped and changed an IO default, so an alarm event was always present; it was patched in a new revision. The configuration tool still treated coordinates as doubles while the firmware used floats, so every config showed unsaved changes; the configuration tool was updated to float. QA confirmed 400 zones configurable and polygons working, and the build fits in RAM. ONE POLYGON POINT · latitude + longitude · bytes to scale lat lon double 16 bytes, 8 per coordinate float 8 bytes, 4 per coordinate a little less precise, fine for a geofence edge Coordinates arrive with six decimals, about 10 cm. Every point of 400 zones × 15 points is held in RAM. A RARELY USED LIST, SHORTENED · in that build · schematic before full list after kept removed · 2.4 kB freed Reserved functionality the build never used was removed too. WHAT THE RAM CUTS BROKE, AND THE FIX cause symptom fix trimming removed some base parameters immobilizer re-locked after unlock restored the parameters #ifndef blocks overlapped and changed an IO default alarm event always present patched in a new revision configuration tool kept doubles, firmware used floats every config showed unsaved changes configuration tool updated to float QA: “400 zones configurable and polygons working”, and the build fits in RAM.
The geofence part: where the RAM for 400 polygons came from, and what the cuts broke

10,000 driver IDs: boot was the problem

A driver touches an ID key to the 1-Wire reader, and the firmware checks it against the list for the immobilizer. With 10,000 entries the feature worked, but boot took about 100 s, against a 30 s limit. I made the list load faster at boot, and cut RAM and BSS use so the list fit.

Two connections, one TLS session

The firmware assumed there was only ever one data connection. The estimate is where the real limit showed up: there was RAM for one TLS session, not two. So I reworked the core network API to handle two PDP contexts. Both connections stay up, and secure traffic goes to one server at a time, so one TLS session is enough.

In testing, the backup connection was torn down and rebuilt on every send. The network rejected that, and the modem got stuck in a restart loop. I fixed the backup handling in several places and removed the settings-refresh loop.

Conclusion

Geofences: QA confirmed “400 zones configurable and polygons working”, and the build fits in RAM. Driver IDs: boot went from about 100 s to under 30 s, on two tracker models. Two connections: the customer accepted the design, which touches about 30 files across the network API and two modem drivers.

On a device this full, every feature costs something else. Here it was precision, unused features and a second TLS session. And cutting RAM breaks things too, so cuts need testing like any new feature.

Tags memory network tls security