简体   繁体   English

实时应用中malloc的使用

[英]Use of malloc in Real Time application

We had an issue with one of our real time application.我们的一个实时应用程序有问题。 The idea was to run one of the threads every 2ms (500 Hz).这个想法是每 2 毫秒(500 赫兹)运行一个线程。 After the application ran for half or an hour so.. we noticed that the thread is falling behind.应用程序运行了半小时或一个小时后……我们注意到线程落后了。

After a few discussions, people complain about the malloc allocations in the real time thread or root cause is malloc allocations.经过几次讨论,人们抱怨实时线程中的 malloc 分配或根本原因是 malloc 分配。

I am wondering that, is it always a good idea to avoid all dynamic memory allocations in the real time threads?我想知道,在实时线程中避免所有动态内存分配总是一个好主意吗?

Internet has very few resource on this ?互联网上很少有这方面的资源? If you can point to some discussion that would we great too..如果你能指出一些讨论,我们也会很棒..

Thanks谢谢

First step is to profile the code and make sure you understand exactly where the bottleneck is.第一步是分析代码并确保您准确了解瓶颈所在。 People are often bad at guessing bottlenecks in code, and you might be surprised with the findings.人们通常不擅长猜测代码中的瓶颈,您可能会对这些发现感到惊讶。 You can simply instrument several parts of this routine yourself and dump min/avg/max durations in regular intervals.您可以自己简单地检测此例程的几个部分,并定期转储最小/平均/最大持续时间。 You want to see the worst case (max), and if the average duration increases as the time goes by.您希望看到最坏的情况(最大值),以及平均持续时间是否随着时间的推移而增加。

I doubt that malloc will take any significant portion of these 2ms on a reasonable microcontroller capable of running Linux;我怀疑malloc是否会在能够运行 Linux 的合理微控制器上占用这 2ms 的任何重要部分; I'd say it's more likely you would run out of memory due to fragmentation, than having performance issues.我想说的是,与性能问题相比,由于碎片而导致内存不足的可能性更大。 If you have any other syscalls in your function, they will easily take an order of magnitude more than malloc .如果您的函数中有任何其他系统调用,它们将很容易比malloc一个数量级。

But if malloc is really the problem, depending on how short-lived your objects are, how much memory you can afford to waste, and how much your requirements are known in advance, there are several approaches you can take:但是,如果malloc确实是问题所在,取决于您的对象的生命周期有多短、您可以浪费多少内存以及您提前知道多少您的需求,您可以采取以下几种方法:

  1. General purpose allocation ( malloc from your standard library, or any third party implementation): best approach if you have "more than enough" RAM, many short-lived objects, and no strict latency requirements通用分配(来自您的标准库或任何第三方实现的malloc ):如果您拥有“足够多”的 RAM、许多短期对象且没有严格的延迟要求,则是最佳方法

    • PROS: works for any object size out of the box, familiar interface, memory is shared dynamically, no need to "plan ahead" if memory is not an issue优点:适用于任何开箱即用的对象大小,熟悉的界面,动态共享内存,如果内存不是问题,则无需“提前计划”
    • CONS: slight performance penalty during allocation and/or deallocation, memory fragmentation issues when doing lots of allocations/deallocations of objects of different sizes, whether a run-time allocation will fail is less deterministic and cannot be easily mitigated at runtime缺点:分配和/或解除分配期间的轻微性能损失,对不同大小的对象进行大量分配/解除分配时的内存碎片问题,运行时分配是否会失败不太确定并且在运行时不能轻易缓解
  2. Memory pool : best approach in most cases where memory is limited, low latency is required, and the object needs to live longer than a single block scope内存池:大多数情况下内存有限、需要低延迟并且对象需要比单个块作用域存活时间更长的最佳方法

    • PROS: allocation/deallocation time is guaranteed to be O(1) in any reasonable implementation, does not suffer from fragmentation, easier to plan its size in advance, failure to allocate at run-time is (likely) easier to mitigate优点:在任何合理的实现中,分配/释放时间都保证为O(1) ,不会受到碎片的影响,更容易提前计划其大小,在运行时分配失败(可能)更容易缓解
    • CONS: works for a single specific object size - memory is not shared between other parts of the program, requires a planning for the right size of the pool or risking potential waste of memory缺点:适用于单个特定对象大小 - 程序的其他部分之间不共享内存,需要规划正确大小的池或存在潜在的内存浪费风险
  3. Stack based (automatic-duration) objects : best for smaller, short-lived objects (single block scope)基于堆栈的(自动持续时间)对象:最适合更小、寿命短的对象(单块范围)

    • PROS: allocation and deallocation is done automatically, allows having optimum usage of RAM for the object's lifetime, there are tools which can sometimes do a static analysis of your code and estimate the stack size优点:分配和释放是自动完成的,允许在对象的生命周期内优化使用 RAM,有一些工具有时可以对代码进行静态分析并估计堆栈大小
    • CONS: objects limited to a single block scope - cannot share objects between interrupt invocations缺点:对象仅限于单个块范围 - 不能在中断调用之间共享对象
  4. Individual statically allocated objects : best approach for long lived objects单个静态分配的对象:长寿命对象的最佳方法

    • PROS: no allocation whatsoever - all needed objects exist throughout the application life-cycle, no problems with allocation/deallocation优点:没有任何分配——所有需要的对象在整个应用程序生命周期中都存在,分配/释放没有问题
    • CONS: wastes memory if the objects should be short-lived缺点:如果对象应该是短暂的,则会浪费内存

Even if you decide to go for memory pools all over the program, make sure you add profiling/instrumentation to your code.即使您决定在整个程序中使用内存池,也要确保将分析/检测添加到您的代码中。 And then leave it there forever to see how the performance changes over time.然后将它永远留在那里,看看性能如何随时间变化。

Being a realtime software engineer in the aerospace industry we see this question a lot.作为航空航天行业的实时软件工程师,我们经常看到这个问题。 Even within our own engineers, we see that software engineers attempt to use non-realtime programming techniques they learned elsewhere or to use open-source code in their programs.即使在我们自己的工程师中,我们也看到软件工程师试图使用他们在别处学到的非实时编程技术或在他们的程序中使用开源代码。 Never allocate from the heap during realtime.切勿在实时期间从堆分配。 One of our engineers created a tool that intercepts the malloc and records the overhead.我们的一位工程师创建了一个工具来拦截 malloc 并记录开销。 You can see in the numbers that you cannot predict when the allocation attempt will take a long time.您可以在数字中看到,您无法预测分配尝试何时需要很长时间。 Even on very high end computers (72 cores, 256 GB RAM servers) running a realtime hybrid of Linux we record mallocs taking 100's of milliseconds.即使在运行实时混合 Linux 的非常高端的计算机(72 核,256 GB RAM 服务器)上,我们也记录了花费 100 毫秒的 malloc。 It is a system call which is cross-ring, so high overhead, and you don't know when you will get hit by garbage collection, or when it decides it must request another large chunk or memory for the task from the OS.这是一个跨环的系统调用,开销如此之大,而且您不知道什么时候会被垃圾回收击中,或者什么时候它决定必须为操作系统的任务请求另一个大块或内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM