News for hackers. Stuff that matters.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @07:24PM (#19149163)
Reading the article made me think again about realtime software/hardware/OS, which I absolutely never understood.

One one hand Wikipedia says "Real-time programs must guarantee response within specified time constraints, often referred to as "deadlines"." ( https://en.wikipedia.org/wiki/Real-time_computing )

On the other hand when I read Wikipedia's article "Real-time operating system" ( https://en.wikipedia.org/wiki/Real-time_operating_system ) my overall understanding is that there is no feature allowing a program/thread/function to enforce e.g. "I have to be executed within 3 milliseconds" nor that that would anyway be enforced by the OS at any cost (scheduler will prioritize higher-priority tasks).

Am I missing something?

Or maybe the statement "Real-time programs must guarantee response within specified time constraints" must be taken literally and the program/thread/function must be written in a way that when some timer expires (and a "terminate"-event is sent to it), it will stop doing whatever it's doing (and it will potentially send back some kind of (partial) result)?

As you can see, this theme confuses me :)

[EDIT: thank you guys! :) ]

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by gizmo686 on Tuesday February 12, 2019 @07:46PM (#19149301)
Generally real-time systems are realtime at every level. The idea is that you don't have a situation where you need to send a terminate event, because the program would have already terminated.

The point of a realtime OS is not to make your programs run in real-time, but to make it possible for your programs to run in real-time. This is simply not possible on a general OS like Linux, because the kernel can always to do something else while your program is running.

Consider the software that drives the printhead in your printer. The timing here is critical. If a command gets sent too early or too late, the print will not come out right.

You probably don't want the OS to be what tells you to send a command every hundred millisecond. Getting this timing right is a core feature of your application, all you need from the OS is to not get in the way of your getting the timing right.

If you are in a state where you need to send a partial result then the program computing the result is likely not really realtime.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @08:40PM (#19149591)
Thx >>The point of a realtime OS is not to make your programs run in real-time, but to make it possible for your programs to run in real-time.

So basically, if I program something and somehow set it to be "realtime" then I can be sure that it won't be suspended before it finishes whatever it is doing?

Such a condition cannot be kept indefinitely - soon or later there will be activities that will need the OS' attention (therefore my program will have to be suspended), no? (If yes then I have problems understanding the threshold...).

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by gizmo686 on Tuesday February 12, 2019 @10:10PM (#19150026)
>So basically, if I program something and somehow set it to be "realtime" then I can be sure that it won't be suspended before it finishes whatever it is doing?

Not necessarily. The OS will provide guarantees about how when the program will be suspended and for how long. The strength of those guarantees will vary between systems, and it is a fuzzy line to decide at what point you have a realtime system.

One example of this is cheap smartphones. Most phones contain a separate baseband processor to control the phone's radio. Because of how critical timing is to radios, these processors will typically run some sort of realtime system. However, modern processors are powerful enough to run both the baseband code, and Android on the same processor.

The difficulty in doing so is that Android is very much not a realtime system, so trying to run the radio software as, say, a Linux kernel driver would be impossible.

The solution is to run both Android and the baseband under a realtime hypervisor. The hypervisor would then guarantee, for example, that every millisecond it will switch to the baseband code. The baseband still gets suspended while Android is running, but because it knows exactly how long it will be suspended and how long it has to do its work while it is not suspended, it is possible for it to do its timing critical job.

You could view this as the baseband having a lot of short-lived "jobs" that it finishes each time before loosing control. Another way to think about it (that is typically more realistic) is that the baseband has 1 longrunning job that involves a fair bit of waiting; and it can be suspended while it is waiting.

Depending on your architecture, you might have the baseband coorporativly suspend itself, or have the hypervisor preemptively suspend it (with well defined timings that the baseband was designed to work with)

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by kllrnohj on Tuesday February 12, 2019 @10:31PM (#19150119)
> The difficulty in doing so is that Android is very much not a realtime system, so trying to run the radio software as, say, a Linux kernel driver would be impossible.

Not entirely true. Although Linux is not typically real-time you can achieve this by using SCHED_FIFO or SCHED_DEADLINE for a given process/thread.

Android in fact does this for things like audio or display composition.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by ertjaet on Tuesday February 12, 2019 @11:13PM (#19150342)
Setting a Linux process to a "realtime" priority (using SCHED_FIFO, etc.) will put above all non-RT /userspace/ processes. But doing that still means that the kernel can preempt you, if it decides to.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by mdwrigh2 on Wednesday February 13, 2019 @05:05AM (#19151851)
While the kernel theoretically has the ability to preempt you, it's not supposed to when using a realtime priority thread. Per the RH realtime guide[[1]:

""" SCHED_FIFO and SCHED_RR threads will run until one of the following events occurs:

- The thread goes to sleep or begins waiting for an event

- A higher-priority realtime thread becomes ready to run

If one of these events does not occur, the threads will run indefinitely on that processor, and lower-priority threads will not be given a chance to run. This can result in system service threads failing to run, and operations such as memory swapping and filesystem data flushing not occurring as expected. """

SCHED_DEADLINE, which is newer and not mentioned in the guide, can provide a bit stronger guarantees because it can fail if it thinks the requirements you set aren't actually obtainable based on other system load (including high priority kernel tasks that might need to run, etc).

[1]: https://access.redhat.com/documentation/en-US/Red_Hat_Enterp...

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by alfalfasprout on Tuesday February 12, 2019 @08:53PM (#19149653)
This varies. On a multicore CPU it's possible that processes will actually be pinned to a core and those processes will have exclusive access to the CPU core (i.e nothing else will be scheduled on it). Real-time programs tend to avoid syscalls, etc. which cause a context switch so it is indeed possible to run a realtime program indefinitely without the OS stepping in.

It's tricky though because even things like dynamic memory allocation can have non-realtime behavior.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by kllrnohj on Tuesday February 12, 2019 @10:36PM (#19150155)
> Real-time programs tend to avoid syscalls, etc. which cause a context switch

That's not why they avoid syscalls. If you are running on a real time schedule there won't be a context switch in response to a syscall. They may, however, get blocked on kernel mutexes. So syscalls are avoided for the same reason user-space mutexes are avoided - being real-time doesn't help if you're blocked on a mutex.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @09:12PM (#19149753)
>>Real-time programs tend to avoid syscalls, etc. which cause a context switch so it is indeed possible to run a realtime program indefinitely without the OS stepping in.

I guess that context switches would have to happen if e.g. 2 realtime programs, with the same priority etc..., would run concurrently on the same CPU - or would "the running program" (the one that managed to start first) be allowed by the OS(/HW?) to take total ownership of the CPU until it finishes running?

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by wtallis on Tuesday February 12, 2019 @09:43PM (#19149894)
That "until it finishes running" bit is why you can't tackle realtime at just the kernel level or just within the process. You need to also consider the whole system—ensuring that the services you're trying to run as realtime processes actually fit within your hardware's performance envelope. If you have processes that need to be pinned to a CPU core and have exclusive use of that core, then you need to ensure that you don't try to run more of those on the system than you have CPU cores available to dedicate. If you're timesharing cores between multiple processes, you need to know that the processes sharing cores have timing requirements that can be satisfied by your available resources. The human in charge of the overall system design needs to be figuring out things like how many time slices per second each realtime process needs and how many microseconds of CPU time it needs in each time slice to complete its work.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by shaklee3 on Wednesday February 13, 2019 @01:35AM (#19150930)
At least in Linux, there are still os threads that will run on pinned, isolated CPUs. There's work being done to allow only the user thread to run, but last I checked it wasn't complete.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by adrianratnapala on Tuesday February 12, 2019 @07:39PM (#19149247)
OSes and applications that get called real-time vary a lot.

But yes, when it is meant rigorously, you can definitely schedule a thing to run on exactly at particular times, preempting whatever lower priority thing was running before.

In my last embedded project that meant we had thing that got called every 64 microseconds -- and it was our job to ensure it finished in time for the next tick and also left enough CPU for all the other threads.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @08:55PM (#19149663)
Thx >>and it was our job to ensure it finished in time for the next tick and also left enough CPU for all the other threads

Then, for the realtime program to run correctly, it would involve as well an understanding of all other processes/programs that are running at potentially the same time (e.g. "competitors" and "collaborators") and/or with the same or higher priorities, etc.., right?

Therefore, candidate summarized definition:

realtime (HW?/)OSs allow you to execute SW which will run with no interruption, as long as no other SW having the same or higher level of priority interferes?

Does it sound right?

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by FigBug on Tuesday February 12, 2019 @09:12PM (#19149750)
Yes, you need to understand all the other processes, but in most cases, there are no other processes. You are writing everything the system is doing. It's up to you to determine what is running in each thread and set it's priority appropriately so that each thread has enough time to run. Real time OSes give you complete control. What you do with that control is up to you based on your needs.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @09:14PM (#19149759)
>>but in most cases, there are no other processes

Cool (yes, I was wondering about the complexity :) ) Thank you!

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by adrianratnapala on Tuesday February 12, 2019 @10:25PM (#19150097)
> Then, for the realtime program to run correctly, it would involve as well an understanding of all other processes/programs that are running at potentially the same time

Yes, and the best way to understand things is to keep them simple.

For example you can haveone real time thread which gets absolute priority and meets hard deadlines while everything else runs whenever the OS can find time for it. Then your only two rules are (1) don't let the real-time thread starve the others completely, and (2) don't rely on timing from anything except the real-time thread.

> OSs allow you to execute SW which will run with no interruption, as long as no other SW having the same or higher level of priority interferes?

Yep. Any OS meeting that definition is real-time. Probably you could get away with a looser definition, but getting it right would be a rabbit-hole.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by pdkl95 on Wednesday February 13, 2019 @05:20AM (#19151924)
A practical example I once had to solve:

I was writing the BIOS/flash-loader/debugger code for a Z80 clone[1] we made. The compiler send instruction packets over the RS-232 serial port at 115200 kBd (kbaud). Using 8N1, this is 12800 B/s which means we have a deadline to read the current byte out of the serial port register ever 78us or data will be lost as the new incoming byte on the serial port is written to the port's IO register.

This sounded like a lot of time, until I was informed some of the boards we sold ran at (iirc) 128kHz. After accounting for memory access time and the runtime of relevant instructions, it worked out that we had at most a budget of 60-70 instructions/byte, which wasn't anywhere close to enough to process the byte (including a simple checksum). Even using a normal ISR wasn't possible: the usual register push/pop would have a major portion of our instruction budget. I ended up dedicating a set of registers to receiving the bytes in realtime into as buffer, while processing the data non-realtime using the remaining registers.

Depending on how strict your deadlines are, "realtime" can mean accounting for every clock cycle the CPU is running. An OS (realtime or otherwise) is for people with softer deadlines and CPU time to spare.

[1] https://en.wikipedia.org/wiki/Rabbit_2000

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by marcosdumay on Tuesday February 12, 2019 @07:32PM (#19149211)
Real time OSes have an all or nothing response to real time tasks. They either take it to run on the specified time, or they fail with some error.

The OSes also take into their hands to enforce that the tasks will finish at the time they asked for. What means that they either terminate by themselves, or the OS will terminate them, again with some explicit error.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by zepearl on Tuesday February 12, 2019 @09:02PM (#19149698)
Thx but well, this kind of goes frontally against the other 2 answers I got (I understood that nothing ever gets explicitly terminated by the OS and I felt happy with that approach as it kind of made sense to me, but some now strong doubts are coming back again...).
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by gizmo686 on Tuesday February 12, 2019 @09:52PM (#19149945)
Realtime doesn't mean error free. A realtime OS might provide some form of a watchdog feature where it can detect when a program fails to meet its deadline. Think of it like a segfault in a traditional system. If everyone did their job correctly, it would never come up, but people make mistakes and it is better to detect them and fail in a controlled manner than it is not notice and fail in unpredictable ways.
Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by marcosdumay on Wednesday February 13, 2019 @06:05AM (#19152138)
Oh, I've read them now. There are some answers about something we used to call "soft real time". Those OSes (Linux was one, for a while, it's not anymore) will have some processes marked as real time, and change the scheduling so that they will execute right at the correct moments. The gotcha is that there can not be many real time processes, since the OS has no way to resolve a conflict between them.

What I described is called "hard real time". It is indeed a very different thing. Both kinds of OSes are in current use, on different niches.

Re:CPU Clocks and Clock Interrupts, and Their Effects on Sch... by loeg on Wednesday February 13, 2019 @02:27AM (#19151130)
> nor that that would anyway be enforced by the OS at any cost (scheduler will prioritize higher-priority tasks).

When sorted against ordinary time-sharing program priorities, any real-time task is a higher priority.