简体   繁体   English

动态内存访问是否对实时程序有害?

[英]Is dynamic memory access detrimental in real-time programs?

I'm working on a project that contains a real-time software component, using the RT PREEMPT patch on Linux. 我正在使用Linux上的RT PREEMPT补丁来开发一个包含实时软件组件的项目。

During "idle" operation the software just sits waiting for incoming TCP connections and requests. 在“空闲”操作过程中,软件只是坐在等待传入的TCP连接和请求。 Depending on the request, the software may create a real-time thread that runs for a period of time. 根据请求,软件可能会创建一个运行一段时间的实时线程。 So the entire application doesn't need to operate in real-time, only this one thread from time to time. 因此,整个应用程序不需要实时运行,只需不时地运行一个线程。

My question is this: I'm well aware that dynamic memory allocation is non-deterministic and is detrimental to real-time code. 我的问题是:我很清楚动态内存分配是不确定性的,并且不利于实时代码。 However, is accessing existing memory on the heap also detrimental to real-time constraints? 但是,访问堆上的现有内存是否也不利于实时限制?

I ask because I'm considering a situation where the program starts up, allocates any required structures on the heap, then triggers a real-time thread that accesses the heap. 我问是因为我正在考虑程序启动,在堆上分配所有必需的结构,然后触发访问堆的实时线程的情况。

EDIT: Once the real-time thread has started, other threads are prevented from writing to variables the real-time thread needs to access using locks (well, except for one variable that must be updated, but access is still restricted using a separate lock). 编辑:实时线程启动后,将阻止其他线程向实时线程需要使用锁访问的变量写入变量(嗯,除了必须更新的一个变量外,访问仍然受到使用单独锁的限制。 )。

EDIT2: I forgot to mention that the program will ultimately be deployed on a system that doesn't have any swap space, so I don't think the paging of memory should be an issue. EDIT2:我忘了提到程序最终将部署在没有任何交换空间的系统上,因此我认为内存分页不应该成为问题。 (Though of course this doesn't avoid the issue of page-faults through memory that hasn't yet been provisioned by the OS.) (当然,这不能避免操作系统尚未提供的内存导致的页面错误问题。)

It is possible that the virtual memory manager might move your memory to swap making your thread generate a major page fault when it runs. 虚拟内存管理器可能会移动您的内存以进行交换,从而使线程在运行时生成主要的页面错误。 You need to lock the memory pages using mlock(). 您需要使用mlock()锁定内存页面。 I also recommend allocating memory in chunks and writing to all of it with memset() before using it to avoid minor page faults at run time and use placement new instead of the regular one to create your objects in the already allocated memory. 我还建议先分块分配内存,然后使用memset()将其全部写入,以免在运行时出现较小的页面错误,并使用placement new而不是常规的placement在已分配的内存中创建对象。

is accessing existing memory on the heap also detrimental to real-time constraints? 访问堆上的现有内存是否也不利于实时限制?

No, unless your system is thrashing . 不,除非你的系统是颠簸

BTW, you could consider writing your own allocation (eg above mmap(2) ...) and using mlock(2) for the memory that ought to be in RAM. 顺便说一句,您可以考虑编写自己的分配(例如,在mmap(2)以上),并将mlock(2)用于应该在RAM中的内存。

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

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