# 2/8 Introduction to Real-time Linux
###### tags: `experience_sharing` `electrical_system` `NTURT`
##### Author: @QuantumSpawner
> Controlling a laser with Linux is crazy, but everyone in this room is crazy in his own way. So if you want to use Linux to control an industrial welding laser, I have no problem with your using PREEMPT_RT.
By Linus Torvalds(2007), [source](https://quotepark.com/quotes/1770416-linus-torvalds-controlling-a-laser-with-linux-is-crazy-but-every/)
## What is real-time?
From [wikipedia](https://en.wikipedia.org/wiki/Real-time_computing),
> Real-time program must guarantee response within specified time constraints, often referred to as 'deadlines'.
But how to define those time constrains?

courtesy: [source](https://shuhaowu.com/blog/2022/01-linux-rt-appdev-part1.html)
As you may see, it depends strongly on the application. For automotive control, the time constrain is about **10ms**.
### Categories of real-time
- Hard: Missing a deadline is a total system failure.
- Soft: The usefulness of a result degrades after its deadline, thereby degrading the system's quality of service.
For automotive control, soft real-time is enough as a missed deadline can be compensated in the future. But it's not the case when it comes to motor control.
Currently, linux is not able to reach hard real-time, but rtos for embedded systems such as FreeRTOS can achieve it.
### Latency sources

courtesy: [source](https://shuhaowu.com/blog/2022/01-linux-rt-appdev-part1.html)
- Hardware latency: Mostly comes from hardware io, and different cpu architectures also differs. But it's comperative lower then the other categories.
- Scheduling latency: Comes from the scheduler, it happens when your program should run but the scheduler let other programs to run instead.
- Application latency: It depends on how well you programm your code, and whether you follow the rules that will be mentioned latter.
## How to achieve real-time programming
- Minimized latency instead of maximize computatuion throughput.
- Bounded execution time: Functions should have bounded executaion time such that the maximum execution time of the program can be estimated. Avoid functions that may take a very long time to execute.(see)
- Non-blocking calls: Similar to the previous point, instead of waiting a function call that takes a long time to execute to finish, a non-blocking call can be implemented to avoid long waiting time.
- Preemption: Tasks with higher priority should be executed first to ensure that the deadline can be met.
## Real-time programming in linux
### Categories of real-time linux
As linux does not native support real-time, there are two main categories to acheive real-time in linux:
- `Hypervisor` approach: Run a hypervisor that directly control the hardware and runs real-time tasks and a linux kernel on top of it to run non-rt task. Best known one is [Xenomai](https://source.denx.de/Xenomai/xenomai/-/wikis/home)
- Pros: The hypervisor can provide less latency then the patched kernel.
- Cons: Since it directly talks to the hardware, drivers for different hardware have to be implemented.
- `Patched-kernel` approach: Patch the linux kernel such that it can achieve very low latency and behaves like a RTOS. Best known one is [PREEMPT_RT](https://wiki.linuxfoundation.org/realtime/start).
- Pros: Works directly in the kernel, hence there's no need to write hardware drivers.
- Cons: Since linux kernel is very complex and there's not a methmatical model of it, theoretical maximum latency cannot be determined.
As `PREEMPT_RT` continues to mature and more and more code are merged to the mainline linux, it may one day be fully supported by the linux out of the box. And implementing hardware support for xenomai is not a small work, it's better to choose `PREEMPT_RT` as the real-time solution.
### Who to install `PREEMPT_RT`
- Build from source: [Linux foundation](https://wiki.linuxfoundation.org/realtime/start)
- Pre-built image: [RPI4 ROS2 real-time image](https://github.com/ros-realtime/ros-realtime-rpi4-image)
### Configure linux for real-time
- Disable swap
- Disable hyper-threading
- Disable dynamic cpu frequency
- Disable RT throttling
> I did not try them before, so just listing.
### Rules in real-time programming
- Avoid using system calls, especially `malloc`, `free` and those take very long time, i.e. disk io, network, etc.
- Lock the application memory to physical memory. See [virtual memory](https://en.wikipedia.org/wiki/Virtual_memory).
- If dynamic memory allocation is necessary during real-time sections, try using a custom memory allocator such as [TLSF](http://www.gii.upv.es/tlsf/) for bounded execution time.
## Reference
- Excellent overview of real-time linux programming: [Real-time programming with Linux](https://shuhaowu.com/blog/2022/01-linux-rt-appdev-part1.html)
- Wikipedia: [Real-time computing](https://en.wikipedia.org/wiki/Real-time_computing)
- Linux foudation: [Real-time Linux](https://wiki.linuxfoundation.org/realtime/start)
- 成大資工wiki: [Xenomai](http://wiki.csie.ncku.edu.tw/embedded/xenomai)
- Hypervized vs pathched kernel: [Real-time Linux explained, and contrasted with Xenomai and RTAI](https://linuxgizmos.com/real-time-linux-explained/)