Module 12 · Lesson 49 of 114

The role of ara::per storage

Explain what ara::per provides and choose between KeyValueStorage and FileStorage based on how an application reads and writes its persistent data.

  • Beginner to advanced
  • 7 min read
  • 13 min lab
What you will need

Familiarity with Adaptive AUTOSAR applications and process restarts. Ubuntu 24.04 in WSL2 or a VM for the practice activities. No AUTOSAR SDK or board is required.

Your place in the module
By the end you can 4 objectives
  • Identify application state that must survive a restart or power cycle.
  • Distinguish a saved value from a change held only in process memory.
  • Choose KeyValueStorage or FileStorage based on the application's unit of access.
  • Explain how FileStorage and its file accessors (ReadAccessor and ReadWriteAccessor) relate to an individual file.
Video coming soonThe role of ara::per storage
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from Graceful stops, heartbeats and ara-lite reuse
  • SIGTERM allows application cleanup, while SIGKILL cannot be caught, blocked, or ignored.
  • A heartbeat should report progress from the work the EM intends to supervise.
  • The EM combines the reaped exit status with the current desired state and restart policy.
01

What the application needs after a restart

3 min read

The runtime and communication layers are done. This lesson answers how applications persist data with KeyValueStorage and FileStorage.

Linux makes an important distinction between accepting a write and making the data durable. A program can successfully write bytes, read them back, and still lose the latest change if power goes away before the bytes reach persistent storage. The Linux write(2) and fsync(2) manual pages document this distinction. For an application that remembers a display preference, the requirement is about what a fresh instance will read after power returns. A successful read immediately after changing the preference answers a different question. write(2) · fsync(2)

Consider a driver display program that keeps its selected theme, the latest ambient-light reading, and the brightness it calculated from that reading. When the driver explicitly selects a theme, the program should restore the saved choice on the next startup. It can obtain a fresh light reading and calculate a fresh brightness after startup. All three values may be in process memory while the program runs. Their lifetimes follow from what the program needs to do after startup. The theme is a past decision the program needs to remember. The sensor reading and the calculated brightness describe conditions that may already have changed.

A gives the application a way to retrieve the theme after a power cycle. The Adaptive AUTOSAR functional cluster that supplies this capability is Persistency, accessed through ara::per. A running instance can save application data through this interface. A later instance can open the corresponding storage and retrieve the saved data. The application still decides what is worth saving, when it needs to save it, and how to interpret what it reads. A theme value means something because the display program defines the available themes and knows how to apply the selected one.

A process restart and a power cycle test different conditions. After a process restart, the fresh instance must recreate its own working state. The machine may have stayed powered throughout, and Linux may still have file data in memory. After a power cycle, the program must obtain any remembered state from data retained without power. If a requirement calls for both, a successful process restart is evidence for the first condition. It leaves the second condition unanswered. Likewise, a program can apply a new theme on screen before the new selection has been durably saved. The visible change tells the driver what the current instance is doing.

A saved value across a power cycleSelectthemerunninginstanceSaveselectionnon-volatile storagePower cycleLoad savedthemefreshinstanceSelectthemerunninginstanceSaveselectionnon-volatile storagePower cycleLoad savedthemefreshinstance
A running instance saves a preference so a fresh instance can load it after startup.
02

Choosing a key or a file

3 min read

Start with what the application needs to ask for. If it needs to ask for the current value of display_theme, or update distance_unit by name, those are key-value operations. If it needs to open a named display layout and give its contents to a layout reader, that is a file operation. Both are ways of retaining application data. They give the program different units of access, and that difference is usually enough to make the first design decision.

With , the key identifies an entry in the store. For instance, distance_unit could identify the saved choice between kilometres and miles. The program reads that entry when it builds the display and updates it when the driver changes the setting. A separate key can identify the theme. If the program needs only the distance unit during startup, it can ask for that value directly. The key identifies a value within the store. The application can use that name without making any assumption about where or how the platform keeps the bytes.

With , the program selects a file within the storage. Opening a file returns a . The accessor provides access to that file's contents (AUTOSAR Adaptive Platform R24-11, Persistency specification; older releases called this interface FileProxy). Think of a display layout that a reader expects as a complete document. The program selects the layout file and reads its contents through the accessor. The layout reader interprets the representation and works out which controls to display. FileStorage supplies access to the named file, the accessor gives access to the opened file, and the reader supplies the meaning of its contents.

The two ara::per storage interfacesKeyValueStorageFileStorageSelect byEntry keyFile nameAccessStored valueContentsthrough anaccessorExampleDistance unitLayoutdocument
KeyValueStorage selects an entry by key. FileStorage selects a named file and returns an accessor for its contents.

File contents can themselves have named fields. A layout document might include a field called theme. If the application reads and replaces the layout as a document, the storage operation is still a file operation. The parser finds the theme field inside that document. If the program separately looks up the driver's theme preference by the key display_theme, the named value itself is the storage entry. The same word can appear in both designs. Follow the operation the caller needs to perform to tell which one it is.

Size alone is a poor way to choose. Even a short document can belong in file storage if an existing reader consumes it in that format. Several settings can belong in key-value storage if the program looks them up and changes them individually. A single application can use both interfaces. The display program can load a layout file, then read the saved distance unit and theme by key. That design follows the way each item is consumed. It also lets you describe a change precisely in a review. Changing the distance unit means updating one named value. Installing a replacement layout means saving the contents of a named file.

03

Describing the state a fresh instance should see

2 min read

A persistence requirement becomes testable when it describes what a newly started program should see. For the display program, begin with a known saved theme, such as light. Select dark, have the application save that selection, and start a fresh instance under the condition named by the requirement. If the condition is a power cycle, verify the result after power returns. At startup the new instance reads the saved selection and applies it. This gives you a more specific test than looking for a dark screen before shutdown. The old instance could have applied dark using the value in its own memory.

A first startup needs its own expected result. Suppose the design has no saved user selection yet and the program displays its built-in theme. In that case the screen looks just like it would if the user had selected that same theme and saved it. The two states differ in what they represent. One is the program's default and the other is a stored user choice. If the display must distinguish them, the test has to establish whether a saved selection exists as well as what appears on screen. The application defines the default and what counts as a valid choice.

Use the same reasoning for a layout file. Suppose the application is supposed to reopen a saved replacement layout on startup. An acceptance test can check whether the fresh instance reads that named file and whether the layout reader can use its contents. Merely knowing that a file exists leaves open whether it contains the saved layout the program expects. The storage interface supplies the data. The code that consumes it determines whether it can build the screen from it. For a preference, the corresponding question is whether the value retrieved for the key is the expected choice.

Verify saved state at startupSave newdataPerformrequiredrestartpower cycleifspecifiedRetrievesaved datafreshinstanceCheckexpectedresultSave newdataPerformrequiredrestartpower cycleifspecifiedRetrievesaved datafreshinstanceCheckexpectedresult
Verify the saved data by retrieving and using it in a fresh application instance.

When you review a storage design, state the data, how the program will ask for it, and when it must be available again. For the display example, the saved distance unit is a value requested by key on startup. The replacement layout is a document requested by file name on startup and then interpreted by the layout reader. The live ambient-light reading comes from a fresh sample. Those descriptions are enough to choose the storage interface before writing the implementation.

The next lesson, KeyValueStorage and FileStorage, shows how the program works with each interface once you know whether it needs a named value or the contents of a named file.

Key takeaways

  • ara::per is the Adaptive AUTOSAR Persistency interface for non-volatile application data.
  • KeyValueStorage gives the application access to values by key.
  • FileStorage gives access to named files, and opening one returns a ReadAccessor or ReadWriteAccessor for its contents.
  • Reading a change back during the same run does not establish that it will survive a power cycle.
Reflect
  1. Explain to a colleague, in two sentences: identify application state that must survive a restart or power cycle.
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close