Skip to main content

Some notes on soft real-time under Linux 2,6...

Linux 2.6 Kernel:

With the 2.6 kernel, a large step was taken to improve Linux's real-time capabilities. The improvements are a result of two major changes in the kernel (as well as many minor changes):

1. Kernel preemption. Before 2.6 the scheduler was able to preempt threads running in user mode, but when the thread made a system call that caused a context switch to kernel mode there was no way for the thread to be preempted. This situation could cause a high-priority thread that was ready to run to be blocked by a lower priority thread inside a system call. With 2.6, the kernel can now be preempted.

2. Constant time scheduler. Prior to version 2.6, the time that it took for the scheduler to decide which thread to run depended on how many threads that were currently running on the system. The more threads on the system, the more time it took the scheduler to make a decision. In version 2.6, the scheduler makes the decision in the same amount of time whether there are 1 thread or 100 threads on the system. This makes the system more predictable.

Scheduling Priorities:

Most processes are started with the default scheduler: SCHED_NORMAL. There are two other schedulers that can be used for real time threads:

1. SCHED_FIFO. This is a first-in first-out scheduler. The highest priority SCHED_FIFO thread ready to run will be scheduled. Once the thread starts running it will only be preempted if a higher priority real time thread becomes runnable or if the thread yields the processor.

2. SCHED_RR. This is a round-robbin scheduler. Unlike SCHED_FIFO, the SCHED_RR scheduler does not allow a thread to run indefinitely unless it is preempted by a higher priority thread. If there are multiple threads of the same priority and they are runnable, the scheduler will alow each thread to run for a specific time before it is preempted (quantum).

To start a process with one of the real time schedulers you have to use the system call sched_setscheduler(pid_t pid, int policy, const struct sched_param *p). Once a thread is running under a real time scheduler you can adjust its priority with a call to sched_setparam().

Linux 2.6 Kernel Timers:

nanosleep(): In Linux 2.6 the kernel only checks on timers once every "jiffie" which defaults to 1ms (it was 10ms before 2.6). Even though you can specify times with nanosecond resolution in the call to nanosleep() don't expect that kind of time resolution. nanosleep() is good for a minimum of 2ms - meaning the timer will finish within 2ms of the programmed interval.

If more accurate timing is needed, /dev/rtc can be used. If you do a read() of /dev/rtc it will block until the rtc timer tick is hit. You can program the rtc's tick rate from 2 Hz to 8192 Hz.

Locking memory:

To keep the memory allocated for a process from being swapped out to disk, you can lock all or part of its memory in RAM using the mlock() call. You can unlock the memory by calling munlock(). If you want to lock the entire memory of a process, a call to mlockall(int flags) will lock the entire address space of a process into RAM. The flags are MCL_CURRENT and MCL_FUTURE. MCL_CURRENT will lock the current memory allocated to the process into RAM. MCL_FUTURE will lock any future allocation of memory by the process into physical RAM. You will usually want to or MCL_CURRENT and MCL_FUTURE together.

Comments

Popular posts from this blog

History of HPSDR Mercury and Quick Silver

History of HPSDR Mercury and Quick Silver Philip Covington, N8VB Early HPSDR and XYLO In 2005 I started a High Performance SDR (HPSDR) project which was to consist of a motherboard carrying a FPGA/USB 2.0 interface and power supply with the provision for plug in modules through 40 pin headers. I had planned a narrow band high dynamic range module based on a QSD/DDS/PCM4202 audio ADC and a wide bandwidth module based on a high speed 16 bit ADC: http://www.philcovington.com/SDR/PICS/HPSDR_FPGA_USB_Board_top1_800600.jpg http://www.philcovington.com/SDR/PICS/HPSDR_FPGA_USB_Board_top4.jpg I soon selected the LTC2208 ADC from Linear Technology. A representative from Linear Technology came across my blog ( http://pcovington.blogspot.com/ ) and offered evaluation boards and samples to support the project. At about the same time my HPSDR project came about, Phil Harman, VK6APH and Bill Tracey, KD5TFD were interested developing a sound card replacement to be used with the SD...

2323 Wilt

RFFE1 Why you may (or may not) need it

RFFE stands for "Radio Frequency Front End" and the "1" stands for the first version (0-62.5 MHz coverage). When I was designing the QS1R board, I had to decide whether to include bandpass filtering and RF amplification on the board. In fact the initial prototypes "RevA" included RF amplification on the QS1R board. Unlike another DDC based direct sampling receiver "Perseus", QS1R was designed to be more than a SW receiver. In addition to a SW receiver, QS1R was meant to facilitate experimentation in the RF spectrum up to at least 300 MHz. I finally settled on a 55 MHz low pass filter (which can be bypassed) and no active components in front of the ADC on QS1R. Any active devices, bandpass filters, or attenuation would be added by a separate board such as the RFFE1. The antenna that I use for my QS1R is a center fed, non-resonant dipole at 50 feet. The total wire length is about 240 feet with only about 100 feet of that running horizontally...