简体   繁体   English

非阻塞mlock()

[英]Non-blocking mlock()

Is there such a thing as non-blocking mlock()? 是否有非阻塞mlock()之类的东西? Under heavy traffic, I don't want my threads block waiting for I/O. 在通信繁忙的情况下,我不想让线程阻塞等待I / O。 I'd rather just tell the Linux kernel what region I need from a mmap()'d file using mlock() and then get notified when the pages has been fetched. 我宁愿只是使用mlock()告诉Linux内核我需要从mmap()文件中的哪个区域,然后在提取页面时得到通知。 (As far as I know, the standard mlock() call is blocking.) (据我所知,标准的mlock()调用正在阻止。)

The mlock interface does not seem to have any built-in functionality like what you want, so I think the only way to achieve it is to use a separate thread to perform the mlock and have that thread notify you (via a condition variable, semaphore, or other mechanism) when the mlock has returned. mlock接口似乎没有您想要的任何内置功能,因此我认为实现此功能的唯一方法是使用单独的线程执行mlock并使该线程通知您(通过条件变量,信号量)或其它机构),当mlock已经返回。 Obviously this is going to incur some overhead, but if your goal is to get realtime latency guarantees rather than improving overall runtime/average latency, it's still an obvious win. 显然,这会产生一些开销,但是如果您的目标是获得实时延迟保证,而不是改善总体运行时/平均延迟,那仍然是一个明显的胜利。

Of course it's hard to make any realtime assumptions unless you use mlockall since your code could get swapped out. 当然,除非使用mlockall否则很难做出任何实时假设,因为代码可能会被换出。 So it might make more sense to use mlockall and POSIX AIO (or a similar but cleaner-API system implemented yourself in terms of threads) to do reads rather than using mmap . 因此,使用mlockall和POSIX AIO(或类似的但更清晰的API系统,根据线程自己实现)可能更有意义,而不是使用mmap Then you have a hard guarantee that, once your data is fetched, it can't be swapped out. 然后,您将坚决保证,一旦提取了数据,就无法将其交换出去。

I believe that you want a combination of madvise() or posix_madvise() and mincore() . 我相信您想要madvise()posix_madvise()mincore()

You would use a madvise call to ask the kernel for MADV_WILLNEED . 您将使用madvise调用向内核询问MADV_WILLNEED Then you would have to poll using mincore to check if the pages had been read into memory. 然后,您将不得不使用mincore进行轮询,以检查是否已将页面读入内存。

If the system is under heavy memory load it is possible for the pages to never be read in by the madvise call, so you would need a timeout and a fallback to a blocking read mode. 如果系统承受沉重的内存负载,则可能无法通过madvise调用来读取页面,因此您将需要超时并回退到阻塞读取模式。

If you mmap() a file, then you treat the file as memory, the VM pages in/out file blocks depending on your usage, which may benefit from read ahead. 如果您对文件进行mmap()处理,则将该文件视为内存,VM会根据您的使用情况来分页输入/输出文件,这可能会受益于预读。

There are various ways to do non-blocking I/O (asyncio, poll, select, epoll) but mlock() is about holding a memory region in RAM, not allowing it to be paged out to the swap partition; 有多种方法可以执行非阻塞I / O(异步,轮询,选择,epoll),但是mlock()涉及在RAM中保存一个内存区域,不允许将其分页到交换分区。 though under severe memory pressure the kernel may not honour that. 尽管在严重的内存压力下,内核可能无法兑现。

Most likely, mmap(2) will suffice as if you are using memory pages, they will not be selected for page out, but kept in memory anyway, so consider if this question is premature optomisation, the kernel tries hard to provide (by default) good performance. mmap(2)最有可能就好像您正在使用内存页面一样,它们不会被选择用于页面输出,但是无论如何都会保留在内存中,因此请考虑是否这个问题是过早的优化,内核会尽力提供(默认情况下) ) 很好的表现。

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

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