Linux ядра реального времени

Правда и вымысел об операционных системах Linux реального времени

В данной статье рассмотрены основные различия между операционными системами реального времени (ОСРВ) и операционными системами общего назначения (ОСОН). В частности отмечены некоторые распространенные заблуждения, касающиеся вопросов быстродействия, задержки, выбора типа системы, интерфейсов программирования API и ядра ОСРВ. В дополнение кратко рассмотрены некоторые методы адаптации ядра Linux к работе в реальном времени.

Операционные системы реального времени (ОСРВ) — это ОС, гарантирующая выполнение задачи в течение заданного интервала времени. Правильность вычислений в ОСРВ определяется не только логической корректностью результата, но и временем выполнения. Так, при выполнении операции «2+2» система должна не просто выдать «4», а успеть сделать это в течение установленного интервала времени, например 20 мкс. Иначе результат не считается верным, а в системе происходит сбой или отказ.
В качестве аналогии можно привести систему безопасности в автомобиле. В аварийной ситуации должна сработать подушка безопасности, причем в течение, скажем, 0,5 с, иначе она все равно не успеет спасти жизнь пассажиру.
Главная характеристика ОСРВ — задержка. Задержка — это промежуток времени между воздействием и реакцией на него. Существует несколько видов задержек:
1) Задержка прерывания (interrupt latency) — время, которое проходит с момента генерации прерывания до запуска соответствующей подпрограммы обработки прерывания.
2) Задержка планировщика (scheduling latency) — промежуток времени между «пробуждением» (воздействием), возвещающим о том, что событие произошло, и моментом, когда у планировщика появляется возможность запланировать выполнение соответствующего потока, который ожидает это событие. Воздействие может быть вызвано аппаратным прерыванием или другими потоками. Задержку планировщика часто называют задержкой реакции (task-response latency) или задержкой диспетчеризации (dispatch latency). Нередко при планировании происходит перераспределение приоритетов и может возникнуть инверсия приоритетов — ситуация, когда какая-либо задача вытесняется другими, изначально имеющими более низкий приоритет.
3) Задержка для худшего случая (worst-case latency) — это наибольшее время, которое может пройти до наступления ожидаемого события. Этот термин имеет отношение к случаю, когда система загружена. В системах реального времени необходимо предусмотреть не только средние параметры, но и наихудшие сценарии. Вспоминая аналогию с системой безопасности автомобиля, 0,5 с — это как раз наихудший случай. При нормальных условиях подушка срабатывает за 0,2 с. Однако в случае перегрузки системы выброс подушки должен быть произведен по крайней мере через 0,5 с.

ОСРВ делятся на жесткие и мягкие. В жестких ОСРВ выход за отведенный временной интервал приводит к неминуемому сбою или отказу. Примерами таких систем являются система безопасности автомобиля и система управления самолетом.
Системы мягкого реального времени допускают временные отклонения. Они нежелательны, но к отказу не приводят. Таким образом, они не гарантируют максимальное время выполнения задачи, поэтому их нельзя использовать на важных участках.
В отличие от мягкой ОСРВ, операционные системы общего назначения принципиально не гарантируют детерминизм. Они нацелены на обеспечение общего быстродействия, а не приоритетное выполнение выбранных приоритетных задач. При этом в среднем ОСОН могут оказаться гораздо более быстрыми по сравнению с ОСРВ. Именно в этом и кроется суть ОСРВ: она не обеспечивает высокое быстродействие как таковое, но гарантирует своевременное выполнение выделенных потоков. На практике эти понятия путают, и в большинстве случаев, когда разработчик уверен, что ему нужна ОСРВ для повышения быстродействия, то на самом деле ему нужно модернизировать старую систему, например увеличив объем памяти, заменив процессор на более быстрый и т.п.
Тем не менее, путаница возникла не на пустом месте. Действительно, качественная ОСРВ обеспечивает достаточно высокое общее быстродействие системы, однако она всегда жертвует быстродействием ради обеспечения детерминизма (предсказуемости).

Читайте также:  Изменить атрибуты папки в линукс

Как было сказано выше, ОСРВ не всегда имеют высокое быстродействие. И не всегда выбор в сторону ОСРВ обоснован. Правильнее сказать, что в подавляющем большинстве случаев встраиваемые приложения не требуют жесткой ОСРВ. В конечном счете это зависит от наличия специфических требований и скрытых инструкций. Чтобы понять, нужна ли ОСРВ в конкретной системе, необходимо ответить на следующие вопросы:
1) Какие требуются задержки? Какова максимальная задержка, которую может выждать приложение, не вызвав сбой в системе?
2) Выполняет ли приложение жизненно важные задачи? Другими словами, должно ли приложение успеть выполнить задачу в течение определенного срока, по истечении которого эта задача потеряет смысл? Например, подушка безопасности в автомобиле должна успеть сработать в течение 0,5 с, иначе она все равно не спасет жизнь пассажиру.
3) Должна ли система обрабатывать абсолютно все поступающие данные, или же некоторые из них могут остаться необработанными?
4) Зависит ли работа приложения от внешнего устройства, которое должно ответить в течение отведенного интервала, по истечении которого произойдет сбой системы?
В таблице 1 приведены примеры использования мягких и жестких ОСРВ.

Жесткая ОСРВ

Мягкая ОСРВ

Источник

Realtime kernel patchset

This article describes the Linux kernel realtime patch set, and some utilities useful for trouble shooting scheduling latencies.

What is realtime?

Realtime applications have operational deadlines between some triggering event and the application’s response to that event. To meet these operational deadlines, programmers use realtime operating systems (RTOS) on which the maximum response time can be calculated or measured reliably for the given application and environment. A typical RTOS uses priorities. The highest priority task wanting the CPU always gets the CPU within a fixed amount of time after the event waking the task has taken place. On such an RTOS the latency of a task only depends on the tasks running at equal or higher priorities; tasks running at lower priorities may be ignored. On a non-realtime OS (most GNU/Linux distributions running their default kernels), since latencies depend on each process running on the system, it is obviously much harder to ensure deadlines will be met every time, and this difficulty scales nonlinearly with system complexity. Determinism in scheduling becomes yet more difficult to achieve because preemption can be switched off for an arbitrary amount of time. A high priority task wanting to run can thus be delayed indefinitely by lower priority tasks with preemption disabled.

Читайте также:  Настройка переменных окружения линукс

How does the realtime patch work

The RT-Preempt patch converts Linux into a fully preemptible kernel. This is done through:

  • Making in-kernel locking-primitives (using spinlocks) preemptible by reimplementation with rtmutexes.
  • Critical sections protected by i.e. spinlock_t and rwlock_t are now preemptible. The creation of non-preemptible sections (in kernel) is still possible with raw_spinlock_t (same APIs like spinlock_t).
  • Implementing priority inheritance for in-kernel spinlocks and semaphores.
  • Converting interrupt handlers into preemptible kernel threads: The RT-Preempt patch treats soft interrupt handlers in kernel thread context, which is represented by a task_struct like a common user space process. However it is also possible to register an IRQ in kernel context.
  • Converting the old Linux timer API into separate infrastructures for high resolution kernel timers plus one for timeouts, leading to user space POSIX timers with high resolution.

Installation

There are two realtime patched kernels available: linux-rt and linux-rt-lts , which both have a configuration based on the main linux kernel package. linux-rt follows the development branch of the -rt patch, while linux-rt-lts tracks a stable branch of the rt patchset.

Scheduling latency

In the context of the scheduler, latency is the time that passes from the occurrence of an event until the handling of said event. Often the delay from the firing of an interrupt until the interrupt handler starts running, but could also be from the expiration of a timer, etc.

There can be many varied causes for high scheduling latencies. Some worth mentioning (in no particular order) are: a misconfigured system, bad hardware, badly programmed kernel modules, CPU power management, faulty hardware timers, SMIs and SMT.

When trying to determine a system’s maximum scheduling latency, the system needs to be put under load. A busy system will tend to experience greater latencies than an idle one. To sufficiently characterize latencies of interest, it would be prudent to run tests for a long time and under a variety of nominal and worst-case load conditions. Further, since many subsystems such as disks, network devices, USB and graphics may be used sparsely after a system is brought online, care should be taken to characterize latency with these subsystems active as well.

Latency testing utilities

Understanding latency is non-intuitive. In measuring and interpreting latency, errors are common and very likely to happen even with experienced computer scientists. Popular tools are often incorrect. This talk explains some common pitfalls. There are several tools available to check kernel scheduling latencies, and to track down the causes of latency spikes. One set of tools comes in a package called rt-tests .

Читайте также:  Изменить пароль пользователя mysql linux

cyclictest

One of the programs in rt-tests is called cyclictest, which can be used to verify the maximum scheduling latency, and for tracking down the causes of latency spikes. cyclictest works by measuring the time between the expiration of a timer a thread sets and when the thread starts running again.

Here is the result of a typical test run:

# /dev/cpu_dma_latency set to 0us policy: fifo: loadavg: 239.09 220.49 134.53 142/1304 23799 T: 0 (23124) P:98 I:1000 C: 645663 Min: 2 Act: 4 Avg: 4 Max: 23 T: 1 (23125) P:98 I:1500 C: 430429 Min: 2 Act: 5 Avg: 3 Max: 23 T: 2 (23126) P:98 I:2000 C: 322819 Min: 2 Act: 4 Avg: 3 Max: 15 T: 3 (23127) P:98 I:2500 C: 258247 Min: 2 Act: 5 Avg: 4 Max: 32 ^C

It shows a four CPU core system running one thread (SCHED_FIFO) per core at priority 98, with memory locked, the system is also under a high load due to running hackbench in a separate terminal. What is most interesting is the max schedling latency detected, in this case 32 usecs on core 3.

hackbench

An idle kernel will tend to show much lower scheduling latencies, it is essential to put some load on it to get a realistic result. This can be done with another utility in the rt-tests package called hackbench. It works by creating multiple pairs of threads or processes, that pass data between themselves either over sockets or pipes. To make it run longer add the -l parameter: hackbench -l 1000000 .

hwlatdetect

hwlatdetect can be used to detect SMIs taking an inordinate time, thus introducing latency by blocking normal kernel execution. It consists of a kernel module (present in both linux-rt and linux-rt-lts), and a python script to launch the process and report the results back to the user. To check if the system uses NMIs run the following command:

NMI: 3335 3336 3335 3335 Non-maskable interrupts

The hwlatdetect kernel module works by turning everything running on the CPUs off through the stop_machine() call. It then polls the TSC (Time Stamp Counter) looking for gaps in the generated data stream. Any gaps indicates that it was interrupted by a NMI, as they are the only possible mechanism (apart from a broken TSC implementation). To run the program for 120 secs, with a detection threshold of 15 usecs, execute the following:

# hwlatdetect --duration=120 --threshold=15
hwlatdetect: test duration 120 seconds parameters: Latency threshold: 15us Sample window: 1000000us Sample width: 500000us Non-sampling period: 500000us Output File: None Starting test test finished Max Latency: 21us Samples recorded: 16 Samples exceeding threshold: 16 1408928107.0286324723 18 17 . . 1408928180.0296881126 15 21 . . 1408928212.0300332889 18 18

The result shows 16 NMIs detected that exceeded the 15 usecs threshold specified, the maximum latency detected was 21 usecs.

See also

Источник

Оцените статью
Adblock
detector