如何通过mmap映射的内存指针获取写入以立即刷新? [英] How to get writes via an mmap mapped memory pointer to flush immediately?

查看:754
本文介绍了如何通过mmap映射的内存指针获取写入以立即刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在双ARM处理器系统(准确地说是Xilinx Zynq)上将/dev/mem和mmap一起使用时,出现了缓存问题.我的配置是不对称的,一个处理器运行Linux,另一个处理器运行裸机应用程序.它们通过不在Linux虚拟内存空间中的RAM块进行通信(设备树文件排除了该RAM).当我的用户空间Linux应用程序使用从mmap()返回的指针写入内存时,第二个处理器检测更改的内存内容可能需要100毫秒到一秒钟的时间.

I'm having what appears to be a caching problem when using /dev/mem with mmap on a dual ARM processor system (Xilinx Zynq, to be exact). My configuration is asymmettric, with one processor running Linux and the other processor running a bare metal application. They communicate through a block of RAM that isn't in the Linux virtual memory space (it was excluded by the devicetree file). When my userspace Linux application writes to memory using the pointer returned from mmap(), it can take anywhere from 100 ms to well over a second for the second processor to detect the changed memory content.

在对/dev/mem的open()调用中,我尝试指定O_RDRW,O_SYNC和O_DIRECT,但是O_DIRECT导致打开失败,因此我删除了O_DIRECT.我以为O_SYNC应该保证在write()调用返回之前就已将数据写入内存,但是我使用的是内存指针,而不是通过write()进行写入.我没有在mmap()调用上看到任何似乎可以解决缓存问题的参数.

On the open() call to /dev/mem, I tried to specify O_RDRW, O_SYNC, and O_DIRECT, but the O_DIRECT caused the open to fail, so I removed O_DIRECT. I thought O_SYNC should have guaranteed that data was written to memory before the write() call returned, but I'm using a memory pointer instead of writing through write(). I don't see any parameters on the mmap() call that would seem to address caching issues.

我尝试在写入内存后调用fsync(fd)和fdatasync(),但这并没有改变行为.

I've tried calling fsync(fd) and fdatasync() after writing to memory, but that didn't change the behavior.

似乎DID起作用的是在写入内存后立即生成以下命令: 同步;回声3/proc/sys/vm/drop_caches

What DID seem to work was spawning this command immediately after the memory write: sync; echo 3 /proc/sys/vm/drop_caches

通过映射的内存指针立即进行刷新的最简单方法是什么?

What is the simplest way to get writes via a mapped memory pointer to flush immediately?

推荐答案

fsync等都将内存映射区域同步到后备块设备(例如,文件).

fsync, etc. all synchronize the memory mapped region to the backing block device (e.g., file).

它们不影响CPU数据缓存.您将需要使用显式缓存清理调用将CPU缓存刷新到DRAM,或者必须使用ACP端口.

They do not affect the CPU data cache. You will either need to use explicit cache clean calls to flush the CPU cache to DRAM or you will have to use the ACP port.

ACP端口应该是缓存连贯的,但是我从来没有使它起作用.

The ACP port is supposed to be cache coherent, but I've never gotten it to work.

以下是有关如何刷新缓存的答案.我认为代码需要放入设备驱动程序中.我们将这些代码打包在通用的"portalmem"驱动程序中.它使您的应用程序可以分配可以与硬件共享的内存,并且为应用程序写入缓存后提供了一个用于刷新缓存的ioctl.

Here's an answer for how to flush the cache. I believe that code needs to go in your device driver. We have that code packaged in a generic "portalmem" driver. It enables your application to allocate memory that you can share with your hardware, and it provides an ioctl for flushing the cache after your application writes to it.

这篇关于如何通过mmap映射的内存指针获取写入以立即刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆