QP/C++  8.0.3
Real-Time Event Framework
Loading...
Searching...
No Matches
Event Memory Management

Event Delivery MechanismsTime Management

Concepts & Definitions

In QP Framework, as in any event-driven system, events are frequently passed from producers to consumers. The exchanged event instances can be either immutable or mutable.

Immutable Events

Immutable events are event instances that never change at runtime. Such immutable events can be pre-allocated statically (typically and preferably in ROM) and can be shared safely among any number of concurrent entities (Active Objects, ISRs, "naked" threads, etc.) rather than being created and recycled dynamically every time. QP Framework does not need to manage memory for immutable events, but needs to clearly distinguish them from mutable events, precisely to avoid any memory management for them.

Remarks
Immutability is a desirable property of event instances because exchanging such immutable events is faster and inherently safer compared to mutable events described below. QP Applications should take advantage of immutable events whenever possible.

Mutable Events

Many event instances, especially events with parameters cannot be easily made immutable because their main function is specifically to deliver information produced at runtime. Consequently, such mutable events must be dynamically created, filled with information (mutated), passed around, and eventually recycled. The management of these processes at runtime is one of the most valuable services QP Framework can provide to QP Application.

The main challenge of managing mutable events is to guarantee that once a mutable event gets posted or published (which might involve event multicasting), it does not change and does not get recycled until all Active Objects have finished their Run-to-Completion processing of that event. In fact, changing or premature recycling the current event constitutes a violation of the RTC semantics.

Automatic Event Recycling

Every dynamically created mutable event must be eventually recycled, or the memory would leak. Some event-driven systems leave the event recycling to the application, but in the safety-related context, this is considered unreliable and unsafe. Therefore, the event memory management in QP Framework must also include automatic event recycling.

Zero-Copy Event Management

From the safety point of view, the ideal would be to copy entire mutable events into and out of the event queues, as it is often done with the message queues of a traditional RTOS. Unfortunately, it is prohibitively expensive in RAM and nondeterministic in CPU cycles for larger event instances (events with large parameters). However, an event-driven framework, like QP, can be far more sophisticated than a traditional RTOS because, due to inversion of control, the framework manages an event's whole life cycle. The framework extracts an event from the Active Object's event queue and dispatches it for processing. After the Run-to-Completion processing, the framework regains control of the event and can automatically recycle the event.

An event-driven framework can also easily control the dynamic allocation of mutable events (e.g., the QP Framework provides API for this purpose). All this permits the framework to implement controlled, concurrency-safe sharing of mutable events, which, from the application standpoint, is almost indistinguishable from copying entire events. Such event management is called zero-copy event management.

The whole event memory management must also carefully avoid concurrency hazards around the shared mutable events. Failure in any of those aspects results in defects (bugs) that are the hardest to detect, isolate, and fix.

Event Pools

To manage the memory for mutable events, QP Framework needs deterministic, efficient, and concurrency-safe method of dynamically allocating and recycling the event memory. The general-purpose, variable-block-size heap does not fit this bill and is inappropriate for safety-related applications, anyway. However, simpler, higher-performance, and safer options exist to the general-purpose heap. A well-known alternative, commonly supported by RTOSs, is a fixed-block-size heap, also known as a memory partition or memory pool. Memory pools are a much better choice for a real-time event framework like QP to manage mutable event memory than the general-purpose heap. Unlike the conventional (variable-block-size) heap, a memory pool is deterministic, has guaranteed capacity, and is not subject to fragmentation because all blocks are exactly the same size.

The most obvious drawback of a memory pool is that it does not support variable-sized blocks. Consequently, the blocks have to be oversized to handle the biggest possible allocation. Such a policy is often too wasteful if the actual sizes of allocated objects (mutable events, in this case) vary a lot. A good compromise is often to use not one but multiple memory pools with memory blocks of different sizes. QP Framework chooses that option to implement event pools, which are multiple memory pools specialized to hold mutable events.

Requirements

SRS_QP_EMM_00

QP Framework shall support immutable events.

Description
Support for immutable events means that QP Framework shall provide a way to create and initialize them (including allocation and initialization in ROM). Also, QP Framework must recognize and distinguish immutable events from mutable events and should never attempt to manage immutable events as mutable.
Use Case
QP Framework can fulfill this requirement by embedding a unique immutable-event signature in the internal data of the event (see SRS_QP_EVT_31). QP Framework must then provide a constant initializer for immutable events.
Forward Traceability (truncated to 2 level(s))
  • SAS_QP_EMM: Event memory management policy
    • SAS_QP_FUSA_02: Error detecting codes mechanisms to protect critical variables.
    • SDS_QA_START: QA Application startup sequence
    • SDS_QP_POST: QP event posting sequence
    • SDS_QP_PUB: QP event publishing sequence
    • SDS_QP_MELC: Mutable event ownership rules

SRS_QP_EMM_10

QP Framework shall support mutable events.

Description
Support for mutable events means that QP Framework shall provide a way to create and initialize them. Also, QP Framework must recognize and distinguish mutable events from immutable events and should never attempt to manage mutable events as immutable.
Forward Traceability (truncated to 2 level(s))
  • SAS_QP_EMM: Event memory management policy
    • SAS_QP_FUSA_02: Error detecting codes mechanisms to protect critical variables.
    • SDS_QA_START: QA Application startup sequence
    • SDS_QP_POST: QP event posting sequence
    • SDS_QP_PUB: QP event publishing sequence
    • SDS_QP_MELC: Mutable event ownership rules

SRS_QP_EMM_11

QP Framework shall support up to 15 event pools for mutable events.

Description
Support for mutable events means that QP Framework shall support multiple, deterministic event pools with different block sizes. The maximum number of event pools managed by QP Framework at any given time shall be compile-time configurable with the maximum of 15 event pools.
Use Case
QP Application configures QP for the maximum number of event pools to 5 but initializes only 3 event pools, each for a different event size.
Forward Traceability (truncated to 2 level(s))


SRS_QP_EMM_20

QP Framework shall provide a method of allocating mutable events at runtime.

Description
The allocation shall choose the appropriate event pool from which to allocate the event based on the event size. The method of allocating mutable events shall be available to all event producers, such as Active Objects, but also ISRs, "device drivers", or "naked" thread of an RTOS (if a traditional RTOS is used).
Use Case
QP Framework might meet this requirement by sorting the event pools internally based on their increasing block size. Then any given event is allocated from the smallest block-size event pool that fits the requested event size.

SRS_QP_EMM_30

QP Framework shall support automatic recycling of mutable events according to the "zero-copy" memory management policy.

Description
QP Framework shall keep track of every use of a mutable event, so that it can support the zero-copy event management policy. In particular, QP Framewrok shall automatically recycle an unused event.
Use Case
One way to meet this requirement is for QP Framewrok to use the standard reference-counting algorithm for mutable events.
Forward Traceability (truncated to 2 level(s))


SRS_QP_EMM_40

QP Framework shall provide a method of explicitly recycling mutable events.

Description
The method of recycling mutable events back to the appropriate event pool shall be available to all event producers, such as Active Objects, but also ISRs, "device drivers", or "naked" thread of an RTOS (if a traditional RTOS is used).

Event Delivery MechanismsTime Management