> For the complete documentation index, see [llms.txt](https://www.opal-rt.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.opal-rt.com/docs/hypersim/model-and-simulation/create-oem-controller-code/real-time-performance-considerations-for-controller-code.md).

# Real-Time Performance Considerations for Controller Code

Controller code intended for real-time execution in HYPERSIM must be designed to meet strict timing requirements. Because all computations must complete within a fixed time step, non-deterministic behavior can lead to missed deadlines and reduced simulation fidelity. To ensure reliable real-time performance, developers should follow real-time programming best practices and avoid OS functions that introduce unpredictable execution delays.

Here are common categories of system calls and programming practices that should be avoided, or used with extreme caution, when executing controller code in a real-time context.

<table><thead><tr><th width="140"></th><th>Description</th></tr></thead><tbody><tr><td><strong>Dynamic Memory Allocation</strong></td><td><p>Functions such as malloc() and free() introduce non-determinism and memory fragmentation, which can cause sporadic delays.</p><p><strong>Recommendation</strong>: Use fixed-size memory pools or pre-allocate memory at startup to ensure predictable timing and avoid runtime allocation.</p></td></tr><tr><td><strong>File I/O Operations</strong></td><td><p>Calls like fopen(), fread(), fwrite(), and fclose() are inherently non-deterministic, as their execution time depends on system state and file system buffering.</p><p><strong>Recommendation</strong>: Avoid file operations during the simulation. Use HYPERSIM’s built-in recording and logging tools instead.</p></td></tr><tr><td><strong>Unbounded for or while loops</strong></td><td><p>A loop that does not have a clearly defined, finite number of iterations or that depends on dynamic conditions that may vary during execution.</p><p><strong>Recommendation</strong>: Always limit loop iteration counts explicitly, and avoid convergence-based logic unless it is guaranteed to complete within a fixed number of steps.</p></td></tr><tr><td><strong>Standard Library Functions with Variable Timing</strong></td><td><p>Common functions like printf(), strcpy(), or strcat() may have unpredictable timing depending on string size or internal buffering.</p><p><strong>Recommendation</strong>: Avoid printing to the console in real time and minimize use of string operations, especially with variable-length buffers.</p></td></tr><tr><td><strong>Thread Priority Manipulation</strong></td><td><p>Modifying thread priorities dynamically can lead to priority inversion and hard-to-detect scheduling anomalies.</p><p><strong>Recommendation</strong>: Define static priorities if required and avoid runtime adjustments. It is also possible to use HYPERSIM threading functions.</p></td></tr><tr><td><strong>Complex Synchronization Primitives</strong></td><td><p>Mechanisms such as semaphores, mutexes, and locks can introduce variable wait times and are sensitive to contention.</p><p><strong>Recommendation</strong>: Design your code to minimize inter-thread synchronization and avoid deep locking hierarchies. It is also possible to use HYPERSIM primitives.</p></td></tr><tr><td><strong>Heavy Interrupt Service Routines (ISRs)</strong></td><td><p>Performing complex computations inside ISRs increases interrupt latency and can block other time-sensitive operations.</p><p><strong>Recommendation</strong>: Avoid ISRs.</p></td></tr><tr><td><strong>System Calls with Variable Execution Time</strong></td><td><p>Some system calls may behave differently based on CPU load, I/O activity, or kernel state, affecting their execution time.</p><p><strong>Recommendation</strong>: Carefully evaluate any system-level call used during real-time simulation and profile its behavior under load.</p></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.opal-rt.com/docs/hypersim/model-and-simulation/create-oem-controller-code/real-time-performance-considerations-for-controller-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
