Windows/Linux下直接访问内存设备

发布于 2024-09-28 23:40:32 字数 1161 浏览 2 评论 0原文

你好
我目前正在编写一个软件来测试闪存设备的完整性。 (使用c++ iostreams)

为了实现这个目标,具有随机内容的文件被写入设备,读回,复制,移动,比较(我将设备置于压力条件下,并不时检查数据是否有效)。 .
一切表面上看起来都很好,但幕后却存在问题。

当我分析通过 ATA / SATA 电缆(使用总线医生)发送到设备的命令时,我发现(尤其是)读取命令通常不会被执行。尽管如此,数据仍可供程序使用。所以它们必须来自缓冲区,这对我来说是不可接受的。每当我读取数据时,它都必须直接来自磁盘,而不是来自缓存。读/写性能根本不重要。

但我还没有找到一种方法来告诉 Windows 不要缓冲任何内容(设备管理器中有一个选项可以停用特定设备的写入缓存,但没有达到预期的效果)。
如果有人知道如何做到这一点:请告诉我
(对于任何有关 Linux 的建议也是如此。我想我也会遇到同样的问题)

到目前为止我能想到的唯一解决方案是直接 I/O。
我在 Windows 上找到的是这样的:

http://www.internals.com/utilities_main.htm

WinIo 库允许 32 位 Windows 应用程序直接访问 I/O 端口和物理内存。它通过结合使用内核模式设备驱动程序和多种低级编程技术来绕过 Windows 保护机制。

http://www.direct-io.com/

Direct I/O 是世界上第一个适用于 Microsoft Windows NT、Windows 2000、Windows XP 和 Windows Server 2003 的设备驱动程序,它允许您直接访问现有软件的硬件,而无需您进行任何编程工作

问题:
有没有人遇到过类似的问题,可以给我一些建议吗?
我真的必须使用直接 I/O 还是有一些我没有找到的操作系统选项/设置(Windows 或 Linux)
您知道其他直接 I/O 库吗(Windows 或 Linux 甚至更好:两者都)

Hello
I'm currently writing a software to test the integrity of flash-memory devices. (using c++ iostreams)

To achieve this goal files with random content are written to the device, read back, copied, moved, compared ( I put the device under stress condition, and check if the data is valid from time to time)...
Everything looks great on the outside, but there is a problem behind the curtains.

When I analyzed the commands that are sent to the device over the ATA / SATA Cable (with a Bus Doctor), I discovered that (especially) read commands are often not executed. Nevertheless the data is availible to the program. So they have to come from a buffer, which is not acceptable in my case. Whenever I read Data it has to come straight from the disk, not from the cache. Read / Write Performance isn't important at all.

But I haven't found a way to tell Windows not to buffer anything (There is the option to deactivate write caching for a specific device in the device manager, but doesn't have the desired effect).
If anyone knows how to do this: Please tell me
( same goes for any advice concerning Linux. I think I will run into the same problems there )

Only solution I could come up with so far is Direct I/O.
What I found for Windows is this:

http://www.internals.com/utilities_main.htm

The WinIo library allows 32-bit Windows applications to directly access I/O ports and physical memory. It bypasses Windows protection mechanisms by using a combination of a kernel-mode device driver and several low-level programming techniques.

and
http://www.direct-io.com/

Direct I/O is the world's first device driver for Microsoft Windows NT, Windows 2000, Windows XP and Windows Server 2003 which enables the direct hardware access for your existing software without any programming efforts on your side

Questions:
Has somebody ever had a similar problem, and can give me some advice?
Do I really have to go with Direct I/O or is there some OS option/setting I haven't found (Windows or Linux)
Do you know other libraries for Direct I/O (Windows or Linux or even better: both)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对于 Windows,您可以使用 Win32 API 禁用缓冲 直接。

打开或创建文件时
创建文件函数,
FILE_FLAG_NO_BUFFERING 标志可以是
指定禁用系统缓存
正在读取或写入的数据
文件。虽然这给出了完整且
直接控制数据 I/O
缓冲,对于文件和
类似设备有数据
必须符合的对齐要求
已考虑。

不确定这是否可以在 C++ 标准库中完成。您可以在 iostream 级别禁用缓冲,但这可能不会影响操作系统缓冲。

For Windows you can disable buffering using the Win32 API directly.

When opening or creating a file with
the CreateFile function, the
FILE_FLAG_NO_BUFFERING flag can be
specified to disable system caching of
data being read from or written to the
file. Although this gives complete and
direct control over data I/O
buffering, in the case of files and
similar devices there are data
alignment requirements that must be
considered.

Not sure if this can be done in the C++ standard library. You can disable buffering at the iostream level but that may not affect the O/S buffering.

半世晨晓 2024-10-05 23:40:32

在Linux下,您可以在打开设备时使用O_DIRECT标志,然后所有访问都会绕过页面缓存,直接访问设备。

请注意,O_DIRECT 读取和写入必须与内存地址、文件偏移和读/写请求大小中的设备块大小(通常为 512 字节)对齐。

Under Linux you can use the O_DIRECT flag when opening the device, and then all the accesses will bypass the page cache and go directly to the device.

Note that O_DIRECT reads and writes must be aligned to the device's block size (usually 512 bytes) in memory addresses, file offsets and read/write request sizes.

寂寞笑我太脆弱 2024-10-05 23:40:32

您可以访问 Linux 内存中 /proc/kcore 下的任何位置。因此,如果您知道设备的内存映射到哪个地址(您可以通过查看 /proc 目录来了解这一点),您可以直接对其进行读/写。

You can access any place in memory in Linux under /proc/kcore. So if you know which address you device's memory is mapped to (something you can figure out by looking more through the /proc directory), you can read/write directly to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文