BLOG
Is Nerves a Real-Time Embedded System?
For now, we’re going to give a brief overview of Dr. Takase’s findings and what the implications are for the growing number of possibilities we’re seeing from Nerves.
Before we get to that, let’s give a quick overview of what “real-time performance” actually means. It’s easy to confuse this term with speed, but real-time just means that the device will complete processes in a specific time window.
For instance, if we compare system A, which can finish processes in as low as 10ms, but sometimes takes 20ms, 50ms, 100ms, or even longer, to system B, which finishes every process in 30-40ms, it’s easy to see why B is a better real-time embedded system.
Basically, predictability is more important than speed. The time that it takes to go from making a request to receiving a response should be stable and relatively constant.
There’s also a distinction between hard and soft real-time that we need to keep in mind. For hard real-time, we have timing constraints where all requests MUST be satisfied within a deadline-and that’s why we depend on it for applications like braking systems, where response time is critical. Soft real-time, on the other hand, lets some requests miss the deadline, but the variance in time that it takes must be within acceptable bounds.
Erlang is a soft real-time language, and so, since Nerves is built in Elixir, which in turn is built on top of Erlang, it’s no surprise that Nerves has soft real-time capabilities. Dr. Takase’s research sought to figure out just how close to hard real-time we can get and if there are any optimizations that we can make to improve real-time functionality.
Here’s what he found.
Nerves is a Real-Time Embedded System and Can Be Optimized for RTC
Jumping to the good stuff, the conclusion is that Nerves provides enough real-time performance within its minimal firmware that it’s suitable for real-time embedded devices. He found this by running benchmarks on a variety of hardware platforms and by comparing Nerves to other embedded operating systems like Raspberry Pi OS and Ubuntu MATE.
What’s even cooler is that he took these findings one step further and investigated whether we can improve Nerves’ real-time performance by configuring our system. It turns out the answer is yes.
Disabling Minor Garbage Collection
First, he found disabling minor garbage collection (GC) can improve real-time performance for applications that consume lots of local data. Since garbage collection automatically manages memory, scaling it back reduces overhead and increases consistency.
Prioritizing Processes
Second, Erlang also allows us to set process scheduling according to four priority levels: low, normal, high, and max. Basically, higher priority processes get more attention than lower ones. Dr. Takase found that setting high priority was good for real-time performance.
Optimizing CPUfreq
Third, we can also optimize in cpufreq, which manages the CPU’s clock speed. Specifically, some processors include Dynamic Voltage and Frequency Scaling (DVFS) that lets them automatically switch between different clock speeds, depending on IO and power consumption requirements.
However, since this scaling requires overhead, setting a fixed frequency is desirable for real-time performance. Setting the clock speed to maximum is best for performance—so long as you don’t mind the resulting BBQ—and powersave mode is still better for real-time performance than DVFS, even though it’s the lowest cpufreq.
Exploring the Linux Kernel
The last place Dr. Takase tried to optimize was in the Linux kernel itself. Since Nerves is highly customizable, he bumped the kernel version up from v4.19 to v5.4 and applied a PREEMPT_RT patch, which enables hard real-time features at the operating system level. Unfortunately, he couldn’t find any noticeable effect, but he did speculate that this could be useful for severe processes with high CPU loads and lots of concurrent IOs.
Nerves, Real-Time Performance, and the Future of IoT
The results are in, and Dr. Takase puts it best:
Nerves is a now, young, cool, and real-time framework!”
Note that ‘now’(ナウ) is Japanese slang for cutting-edge. We couldn’t agree more.
While Nerves isn’t hard real-time, it’s good enough for all but the most time-sensitive applications. When we package that together with its small firmware, customizability, and all its other awesome features, we’re confident that Nerves will grow into the #1 choice for IoT development.
Specifically focusing on its real-time capabilities, we can expect to see Nerves’ stock go up for Industrial IoT (IIoT) applications, early warning alert systems, and even medical science applications, such as MRI scans and radiation therapy. Just like we rely on our own nervous system to interact with the world around us in real-time, our IoT devices can count on their Nerves.