154 points · by ducktective · 4y ago · 114 comments
If you aren't doing that then it's not hard real time.
Not all aspects of a real time system need latency control like you're imagining. It's entirely possible to imagine, say, an aircraft autopilot or self-driving system (something that needs ~100ms round-trip latencies to approximate a human pilot) being implemented using a garbage collected runtime, as long as you can guarantee those GC pauses are acceptably small (lots of such systems exist).
The same would be a very hard ask of a piston engine controller, where the timing needs are closer to 100us. Nonetheless the (again to pick an arbitrary example) logging/telemetry subsystem of that engine controller doesn't need that. So as long as you have an OS that can partition the engine tasks from the low priority stuff in a reliable way, you can still imagine a Lisp/Java/.NET/Go/whatever runtime being used there.
Real time is a design philosophy, not a tech stack.
Hard real time is not too hard in general as it is usually combined with quite low performance-requirements. Boundedness itself is the important property, as opposed to soft-real time which is much more finicky (video games, audio).
The RTGC applications in military tech that I had heard of were on the soft end of hard real time. It was mission critical, not safety critical. Maybe there’s something newer that I hadn’t heard about though.
Some of the really bad problems are:
- How to make GC operations like allocation have the level of determinism that hard RT programmers want.
- How to prove that your GC can keep up with allocation rate while at the same time also only taking a deterministic amount of your time.
Not saying it can’t be done. It’s just hard.
Hard real time is hard, full stop. If you have malloc and free you will encounter the exact same real-time challenges as you will with GC.
Heck, you don't even need malloc and free. All you need is a WHILE loop to make hard-real-time extremely challenging.
You turn the garbage collector off, tune the allowed step size, and trigger collection in steps when it's allowed. Having the Lua VM and GC around is not the hard part of getting hard real time guarantees out of such a system.
With Lua, those parts get written in C for the most part. With Common Lisp it's possible to use one language for both parts. At least as I'm told, by those who should know, I've not tried writing any CL which wasn't comfortably high level.
Complex configuration files, embedded scripts, even browser scripting should all have been done in Lua.
(Or Lisp, but people have less of a visceral reaction when they see Lua syntax)
Theoretically some Common Lisp implementations could have GC disabled. Like you, I have not tried that in practice.
My gripe with it is: no easy parallelism and I don't mean OS threads. I suppose it's not a priority for them, sadly.
You can build Lua on a 32 bit chip which doesn't have a native float type, without floating point support. Which is handy.
Actually it's trivial: just put a bound on the amount of available memory and do a worst-case analysis to GC it all.
The hard part is not engineering the system to have a bound, it is engineering the system so that the bound is reasonable for practical use.
Also if you ever find yourself needing to write hard real-time code for your arduino, check out my blog post about the arduino-copilot library at https://wjwh.eu/posts/2020-01-30-arduino-copilot.html :)
Even a mere 9kloc L4 kernel (of intentionally not-so-dense C-code) took many man years to prove.
EDIT: Has anyone ever offered such a proof for a program that is just 100kloc? What about 1mloc? Do people actually believe that most non-trivial "realtime" programs are provably realtime?
"All loops shall have a statically determinable upper-bound on the maximum number of loop iterations. It shall be possible for a static compliance checking tool to affirm the existence of the bound. An exception is allowed for the use of a single non-terminating loop per task or thread where requests are received and processed. Such a server loop shall be annotated with the C comment: /* @non-terminating@ */."
Without such a theorem solver, it is all just "best practices" then hoping for the best.
There are a whole class of programs/languages that are decidable (i.e. they will terminate) and that can be proven as well by automated means.
Now this does depend on what you mean with sufficiently complex, but for special programs you can prove a lot of things (by hand); the halting problem does not prevent that. However, a side effect is that you may not be able to prove that property for the program you want.
Can you do this for all possible inputs? If not, then your informal pinky swear that it really will get things done on time isn't worth very much.
Running through every possible x86 instruction is out of the question. Even provably creating every possible instruction scenario would be all but impossible.
Caching is a very hard problem. Add in a bunch of neural networks to control those caches and programmers aren't even sure what's actually happening (let alone actually having formal verification).
Large, Out-of-order chips use millions of lines of code to implement their algorithms in hardware. They do lots of unit testing and some formal verification of the smaller bits, but the sum of the parts isn't formally verified.
It takes a half-decade to create a chip without formal verification. With complete formal verification, we might not even have the first Pentium ready to ship yet (if ever).
While we're on the topic, very few programming language implementations are formally verified. For example, L4 was verified using Isabelle which is written in StandardML and compiled using the PolyML compiler. I don't believe the PolyML compiler is formally verified. This means that Isabelle isn't formally verified when compiled which means that L4 isn't formally verified either (technically speaking). And of course, even if all of these were verified, there's the "trusting trust" issue and the fact that the compiler must be running on a formally verified CPU.
I appreciate striving for formal verification, but code verifiable from the bottom-up is a very long way away from happening.
A large program is itself essentially composed of many subprograms. You must prove every single aspect of those subprograms (hard in and of itself) then you must prove that every possible combination of those subprograms is also provable. As there is no general formula for the halting problem, each combination must be uniquely proven.
This essentially means that if you have N possible branches, then the difficulty proving for each unique case would be the factorial of N.
Proving 9kloc of code for L4 took 5-10 man years. Adding just 1kloc of similarly dense code would likely add decades of man-years. What about 100kloc or 1mloc?
Even if you could find tricks to optimize similar branches, the fundamental factorial problem doesn't go away (as making it go away would mean that you've solved the provably insolvable halting problem).
In practice, only the most trivial of programs are completely provable before the heat death of the universe.
That is correct but also completely irrelevant. In practice all software we actually use and need can be written in languages that are slightly less powerfully than Turing complete languages. Look at CompCert, seL4, Dafny, Coq, LEAN, F-Star etc. etc.
(Doing soft-RT work in Common Lisp though)
What am I missing?
Isn't hard real-time only about upper-bound of tasks? Like if every task could be proven to run under t seconds (not necessary small), it is eligible for being called hard real-time.
symbolic processing - symbolic expressions (s-expressions) being the building blocks of lisp programs
single processor - not considering multitasking or multiprocessor concerns
Lisp was championed by people in AI working on symbolic reasoning back in the 1990s.
The language they're developing in/for is Lisp, which is used (a lot) for symbolic processing. That's the "symbolic processing" part. "real-time" is the constraint they're trying to achieve and relates to system performance. "single processor" is just another constraint. If you permit multiple processors then some of the problems making Lisp real-time go away or are mitigated as you can do things like shove the GC bit into a second processor and execute it in parallel. Being constrained to a single processor means you can't do this, when the GC is going it's using the full CPU and taking time away from other computations (symbolic processing, in their case).
I would guess, the paper cares what the language looks like since it's about Lisp being real-time.
Lisp has been dubbed as a "symbolic processing language" since its early days, since MacCarthy used that language in some papers.*
Basically it means the ability to juggle complex data structures that can represent formulas; e.g. solving algebraic equations (something easily hacked up in Lisp) is an example of symbolic processing.
This is entirely relevant to the topic because the dynamic allocation used in symbolic processing presents challenges to real-time processing, like garbage collection pauses, and whatnot. What if you want asynchronous interrupts to be able to allocate cons cells and other objects?
In a Lisp program, memory-allocating expressions casually appear throughout a program. Something like (list a b c), if we translate it to C, would call a malloc-like routine three times. Some people doing some kinds of embedded work even today eschew dynamic allocation.
---
* Like: Recursive Functions of Symbolic Expressions and Their Computation by Machine. Memo 8, Artificial Intelligence Project, RLE and MIT Computation Center, March 13, 1959, 19 pages. https://dspace.mit.edu/handle/1721.1/6096
Like: (simplify '(2 * a + 4 * a)) -> (6 * a).
An application for symbolic processing could be a rule-based system. The application then consists of a lot of rules and facts. Facts also could be symbols, not just numbers. Like a physical pressure value could be a number, but it could also be a qualitative value like low-pressure.
A relatively old application (originating in the mid 80s) is real-time control with a rule-based system in technical systems. Technical systems could be a fleet of satellites, a chemical plant, a cement production plant, ...
The software might get sensor inputs and based on rules may cause physical actions like opening or closing a valve, while getting more live sensor inputs, then possibly triggering other rules.
Typically Lisp systems have some kind of GC and most of the time this will not allow real-time applications. There are are some specialized Lisp systems which have no GC and the application code is written in a way to not dynamically allocate memory during runtime - one says that the style is non-consing, where CONS is the basic operation to allocate a new list cell. This way they avoid garbage collections. This allows some real-time applications, like the mentioned rule-based process control software. This is rare and exotic stuff. It's also considered 'old AI' technology.
The paper describes also something exotic: a concurrent Garbage Collector for a Japanese Lisp Machine, which runs a Lisp operating system - that was a thing when Japan was investing a lot into Artificial Intelligence in the 'Fifth Generation Project'. Such a GC would be useful so that the machine's network stack could be written directly in Lisp, but also for applications (similar to above control of external physical systems): robots, machines, production plants, ... It was thought that an important application domain for expert systems could be in industrial control systems.
You got any more of these "Real-time Programming" courses? Or books?
In this particular case, latest publish reference is at 1998 and one marked as "to be published" at 2000 so that makes it a bit easier to guess pretty exactly :)
(This is aside from the assertion that "faster than language x" is not the same as "adheres to hard realtime constraints" which is what the overall topic is about.)