如何“实时”是Linux 2.6吗?

发布于 2024-08-03 08:19:29 字数 153 浏览 5 评论 0原文

我正在考虑将我的产品从 RTOS 迁移到嵌入式 Linux。我对实时性的要求并不高,少数的 RT 要求都是 10 毫秒左右。

有人可以给我指出一个参考资料,告诉我当前版本的 Linux 的实时性如何吗?

从商业 RTOS 迁移到 Linux 还存在其他问题吗?

I am looking at moving my product from an RTOS to embedded Linux. I don't have many real-time requirements, and the few RT requirements I have are on the order of 10s of milliseconds.

Can someone point me to a reference that will tell me how Real-Time the current version of Linux is?

Are there any other gotchas from moving to a commercial RTOS to Linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有你我更好 2024-08-10 08:19:29

您可以从 Real Time Linux wiki常见问题解答

现有 2.6 Linux 内核的实时功能是什么?

传统上,Linux 内核仅在某些情况下才允许一个进程抢占另一个进程:

  • 当 CPU 运行用户模式代码时
  • 当内核代码从系统调用或中断返回到用户空间时
  • 当内核代码代码在互斥锁上阻塞,或显式将控制权交给另一个进程时

如果内核代码正在执行时发生某些需要高优先级线程开始执行的事件,则高优先级线程无法抢占正在运行的内核代码,直到内核代码显式让出控制权。在最坏的情况下,延迟可能会达到数百毫秒或更长。

Linux 2.6 配置选项 CONFIG_PREEMPT_VOLUNTARY 引入了对导致长时间延迟的最常见原因的检查,以便内核可以自愿将控制权交给等待执行的更高优先级任务。这可能会有所帮助,但是虽然它减少了长延迟(数百毫秒到可能数秒或更长时间)的发生,但并不能消除它们。然而,与 CONFIG_PREEMPT(下面讨论)不同,CONFIG_PREEMPT_VOLUNTARY 对系统整体吞吐量的影响要小得多。 (与往常一样,吞吐量(系统的整体效率)和延迟之间存在经典的权衡。随着现代系统的 CPU 速度更快,通常需要权衡吞吐量以降低延迟,但服务器不需要最小延迟保证的类系统很可能选择使用 CONFIG_PREEMPT_VOLUNTARY,或者坚持使用传统的非抢占式内核设计。)

2.6 Linux 内核有一个额外的配置选项 CONFIG_PREEMPT,它使自旋锁保护区域和中断处理程序之外的所有内核代码都有资格被更高优先级的内核线程非自愿抢占。使用此选项,最坏情况下的延迟会下降到(大约)个位数毫秒,尽管某些设备驱动程序可能具有中断处理程序,这会导致比这更糟糕的延迟。如果实时 Linux 应用程序需要小于个位数毫秒的延迟,强烈建议使用 CONFIG_PREEMPT_RT 补丁。

他们还有一个“陷阱”列表,正如您在常见问题解答中所说的那样。

需要保留哪些重要内容
实时写作时的思维
应用程序?

期间注意以下事项
初始启动阶段:

  • 尽快从 main() 调用 mlockall()。
  • 在应用程序启动时创建所有线程,并触摸每个线程的整个堆栈的每个页面。切勿在 RT 显示期间动态启动线程,这会破坏 RT 行为。
  • 切勿使用已知会产生页面错误的系统调用,例如
    fopen()。 (打开文件会
    mmap() 系统调用,它生成一个
    页面错误)。
  • 如果您使用“编译时全局变量”和/或“编译时全局变量”
    数组',然后使用 mlockall() 来
    防止访问时出现页面错误
    他们。

更多信息:HOWTO:构建一个
RT-应用

他们还有一个您可能需要的大型出版物页面去结帐。

You can get most of your answers from the Real Time Linux wiki and FAQ

What are real-time capabilities of the stock 2.6 linux kernel?

Traditionally, the Linux kernel will only allow one process to preempt another only under certain circumstances:

  • When the CPU is running user-mode code
  • When kernel code returns from a system call or an interrupt back to user space
  • When kernel code code blocks on a mutex, or explicitly yields control to another process

If kernel code is executing when some event takes place that requires a high priority thread to start executing, the high priority thread can not preempt the running kernel code, until the kernel code explicitly yields control. In the worst case, the latency could potentially be hundreds milliseconds or more.

The Linux 2.6 configuration option CONFIG_PREEMPT_VOLUNTARY introduces checks to the most common causes of long latencies, so that the kernel can voluntarily yield control to a higher priority task waiting to execute. This can be helpful, but while it reduces the occurences of long latencies (hundreds of milliseconds to potentially seconds or more), it does not eliminate them. However unlike CONFIG_PREEMPT (discussed below), CONFIG_PREEMPT_VOLUNTARY has a much lower impact on the overall throughput of the system. (As always, there is a classical tradeoff between throughput --- the overall efficiency of the system --- and latency. With the faster CPU's of modern-day systems, it often makes sense to trade off throughput for lower latencies, but server class systems that do not need minimum latency guarantees may very well chose to use either CONFIG_PREEMPT_VOLUNTARY, or to stick with the traditional non-preemptible kernel design.)

The 2.6 Linux kernel has an additional configuration option, CONFIG_PREEMPT, which causes all kernel code outside of spinlock-protected regions and interrupt handlers to be eligible for non-voluntary preemption by higher priority kernel threads. With this option, worst case latency drops to (around) single digit milliseconds, although some device drivers can have interrupt handlers that will introduce latency much worse than that. If a real-time Linux application requires latencies smaller than single-digit milliseconds, use of the CONFIG_PREEMPT_RT patch is highly recommended.

They also have a list of "Gotcha's" as you called them in the FAQ.

What are important things to keep in
mind while writing realtime
applications?

Taking care of the following during
the initial startup phase:

  • Call mlockall() as soon as possible from main().
  • Create all threads at startup time of the application, and touch each page of the entire stack of each thread. Never start threads dynamically during RT show time, this will ruin RT behavior.
  • Never use system calls that are known to generate page faults, such as
    fopen(). (Opening of files does the
    mmap() system call, which generates a
    page-fault).
  • If you use 'compile time global variables' and/or 'compile time global
    arrays', then use mlockall() to
    prevent page faults when accessing
    them.

more information: HOWTO: Build an
RT-application

They also have a large publications page you might want to checkout.

酒解孤独 2024-08-10 08:19:29

您看过Xenomai吗?它将允许您在 Linux 上运行“硬实时”进程,同时仍然允许您访问常规 Linux API 以满足所有非实时需求。

Have you had a look at Xenomai? It will let you run "hard real time" processes above Linux, while still allowing you to access the regular Linux APIs for all the non-real-time needs.

秋日私语 2024-08-10 08:19:29

使用 Linux 实现实时功能有两种根本不同的方法。

  1. 使用 rt-preempt 补丁等修补现有内核。这最终将导致完全抢占式内核

  2. 双内核方法(如 xenomai、RTLinux、RTAI...)

从 RTOS 迁移到 Linux 会遇到很多问题。

也许您真的不需要实时?

我在培训课程中谈论实时 Linux:

https://rlbl.me/elisa

< a href="https://rlbl.me/elisa-en-pdf" rel="nofollow noreferrer">https://rlbl.me/elisa-en-pdf

https://rlbl.me/intely

https://rlbl.me/intely-en-pdf

https: //rlbl.me/entirety-en-all-pdf

There are two fundamentally different approaches to achieve real-time capabilities with Linux.

  1. Patch the existing kernel with things like the rt-preempt patches. This will eventually lead to a fully preemptive kernel

  2. Dual kernel approach (like xenomai, RTLinux, RTAI,...)

There are lots of gotchas moving from a RTOS to Linux.

Maybe you don't really need real-time?

I'm talking about real-time Linux in my training sessions:

https://rlbl.me/elisa

https://rlbl.me/elisa-en-pdf

https://rlbl.me/intely

https://rlbl.me/intely-en-pdf

https://rlbl.me/entirety-en-all-pdf

风月客 2024-08-10 08:19:29

答案可能是“足够好”。

如果您运行的是嵌入式系统,您可能可以控制盒子上的全部或大部分软件。

Stock Linux 2.6 有几个适合低延迟任务的功能 - 主要是:

  • 调度策略
  • 内存锁定

假设您使用的是单核机器,如果您只有一个任务将其调度策略设置为 SCHED_FIFO 或 SCHED_RR(它如果你只有一个任务,那并不重要),并使用 mlockall() 锁定其所有内存,那么一旦准备好运行,它将立即被调度。

那么您唯一需要担心的是内核的某些不可抢占部分需要比您可接受的延迟更长的时间才能完成 - 这在嵌入式系统中不太可能发生,除非发生不好的事情,例如极端的内存压力,或者你们的司机很狡猾。

我想“尝试一下”是一个很好的答案,但在您的情况下这可能相当复杂(并且可能涉及编写设备驱动程序等)。

查看 sched_setscheduler 的文档以获取一些有用的信息。

The answer is probably "good enough".

If you're running an embedded system, you probably have control of all or most of the software on the box.

Stock Linux 2.6 has several features suitable for low-latency tasks - chiefly these are:

  • Scheduling policies
  • Memory locking

Assuming you're using a single-core machine, if you have just one task which has set its scheduling policy to SCHED_FIFO or SCHED_RR (it doesn't matter which if you have just one task), AND locked all its memory in with mlockall(), then it WILL get scheduled as soon as it is ready to run.

Then the only thing you'd have to worry about was some non-preemptable part of the kernel taking longer than your acceptable latency to complete - which is unlikely to happen in an embedded system unless something bad happens, such as extreme memory pressure, or your drivers are dodgy.

I guess "try it and see" is a good answer, but that's probably rather complicated in your case (and might involve writing device drivers etc).

Look at the doc for sched_setscheduler for some good info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文