如何更快地访问System V共享内存?
我正在使用 System V Shared Memory 在两个 PHP 应用程序之间进行通信。在单个共享内存段
上,一个应用程序是只写的(Writer),另一个应用程序是只读的(Reader)(数据约为900-1000 字节
)。
两个应用程序都使用无限循环运行,并具有 1μs
睡眠,以防止 100% CPU 使用率(请参阅下面的代码示例)
while(true) {
shm_get_var($shm, 1);
usleep(1);
}
读取器需要 ~1ms
才能获取由来自共享内存段
的Writer。有趣的是,如果我仅从 Writer 中删除 usleep
,Reader 会在大约 500μs
内获取其数据。如果我仅从 Reader 中删除 usleep
,Reader 会在大约 200μs
内获取其数据。删除 usleep
的问题是 100% 的核心使用率。
我尝试将每个应用程序分配给同一个核心(使用taskset
),两个应用程序在同一核心上使用usleep
,Reader在大约300中获取其数据-400μs
(有时峰值接近900μs
)。如果我从 Reader 中删除 usleep
,并且两个应用程序位于同一内核上,则 Reader 获取数据的速度不会快得多,约为 300-390μs
。
对于某些人来说,半毫秒可能听起来很荒谬,但速度是我的应用程序中最重要的事情,对我来说有很大的不同。
如果有人能帮助我理解为什么我在上面的例子之间有这么大的差异,那就太好了。任何有关更快地执行此操作的想法都将受到极大的赞赏。
规格
- 内存: 12GB DDR3 (6x2GB),运行频率为 1066 MHz
- CPU: 双 Intel Xeon E5620 @ 2.40GHz(四核,超线程关闭)
更新
#1 我尝试使用time_nanosleep
将睡眠时间减少到纳秒而不是微秒,Reader 仍然需要 ~1ms
。
#2 我在 Python 中也遇到了同样的问题,只不过它需要 ~1-2ms
而不是像 PHP 中的 ~1ms
。我尝试使用ctypes模块
和clock_nanosleep
,我能够降低到大约~300μs
,这还不错,但我'我正在寻找更快的东西。
I'm using System V Shared Memory
to communicate between my two PHP applications. One application is write only (Writer) and the other one is read only (Reader) on a single Shared Memory Segment
(the data is about 900-1000 bytes
).
Both applications run using an infinite loop with a 1μs
sleep to prevent 100% CPU usage (see code sample below)
while(true) {
shm_get_var($shm, 1);
usleep(1);
}
It takes ~1ms
for the Reader to get the data written by the Writer from the Shared Memory Segment
. Interestingly enough, if I remove the usleep
from the Writer only, Reader get its data in around 500μs
. If I remove the usleep
from the Reader only, Reader get its data in around 200μs
. The problem with removing the usleep
is the 100% core usage.
I tried is to assign each application to the same core (using taskset
) with both applications using usleep
on the same core, the Reader get its data in around 300-400μs
(sometimes peaks near 900μs
). If I remove the usleep
from the Reader and both applications are on the same core, the Reader won't get its data much faster, around 300-390μs
.
Half of a millisecond might sound ridiculous to some but speed is the most important thing in my application and makes a huge difference to me.
If someone could help me understand why I get that much of difference between my examples above that would great. Any idea about executing this faster would be tremendously appreciated.
Specs
- Memory: 12GB DDR3 (6x2GB) running at 1066 MHz
- CPU: Dual Intel Xeon E5620 @ 2.40GHz (Quad core, HyperThreading off)
Updates
#1 I tried to use time_nanosleep
to reduce the sleep time to nanoseconds instead of microseconds and Reader is still taking ~1ms
.
#2 I have the same problem in Python as well except that it takes ~1-2ms
instead of ~1ms
like in PHP. I tried to use the ctypes module
and clock_nanosleep
and I'm able to get down to about ~300μs
which is not too bad but I'm looking for something even faster.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该尝试 zeromq
You should try zeromq
如果您的软件仅限于单个类 UNIX 操作系统,则也可以使用 POSIX 消息队列。请在此处查找概述:https://linux.die.net/man/7/mq_overview< /a>
If your software is limited to a single UNIX-like OS also the POSIX message queues could be used. Please find an overview here: https://linux.die.net/man/7/mq_overview