低延迟访问PCI内存BAR(Linux) [英] Access PCI memory BAR with low latency (Linux)

查看:302
本文介绍了低延迟访问PCI内存BAR(Linux)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PCI卡,它基本上是一个时钟.它通过GPS获取时间,并将当前时间保存在某个寄存器中.

I have a PCI card, which is basically a clock. It gets the time by GPS and saves the current time in a certain register.

我想一遍又一遍地读取有限数量的寄存器/字节(例如当前时间),并以尽可能低的延迟. (时钟提供了很高的精度,我认为延迟越高,精度就会越低.)操作系统是RedHat.编程语言是C/C ++.我还想写入设备内存,因此延迟不是问题.

I want to read a limited number of registers/bytes (for example the current time) over and over again, with the lowest possible latency. (The clock provides very high precision and I think I will loose precision the higher the latency is.). The operating system is RedHat. The programming language is C/C++. I also want to write to the device memory, whereby latency is not an issue.

我看到了这些方式.如果您看到另一个,请告诉我:

I see these ways. If you see another, please tell me:

  1. 编写Linux内核模块驱动程序,该驱动程序创建一个字符设备(或每个读取的寄存器一个字符设备).然后,用户空间应用程序可以对/dev/文件进行读取".
  2. DMA
  3. 通过用户空间应用程序(系统调用)将sysfs resourceX文件映射到用户空间. (例如,例如此处)
  4. 编写一个实现mmap文件操作的Linux内核模块驱动程序.
  1. Writing a Linux kernel module driver, which creates a character device (or one character device for each register to read). Then a user space application can do a "read" on the /dev/ file(s).
  2. DMA
  3. mmap the sysfs resourceX file to user space by a user space application (systemcall). (like here for example)
  4. Write a Linux kernel module driver which implements a mmap file operation.

问题:

  1. 在实际读取寄存器时,哪种方法具有最低的延迟?我知道mmap会在内核中造成很多开销,但据我了解,这仅用于初始化.
  2. 方法3是合法的方法吗?对我来说,这似乎是一种hack.如何从应用程序自动确定/sys/路径?
  3. 方法3和方法4有区别吗?我是PCI驱动程序编程的新手,我认为我并不真正了解4的工作方式.我阅读了(以及那本书),但也许您可以给我一个提示或示例.我会很感激的.
  1. Which is the way with the lowest latency when it comes to the actual reading of the register? I am aware that mmap causes a lot of overhead in the kernel, but as far as I understand that is only for initialisation.
  2. Is way 3 a legit way to go? It looks like a hack to me. How can I determine the /sys/ path automatically from the application?
  3. Is there a difference between way 3 and 4? I am new to PCI driver programming and I think I didn't really understand how way 4 works. I read this (and other chapters of that book), but maybe you can give me a hint or an example. I would appreciate that.

推荐答案

方法3或4应该可以正常工作.它们之间在延迟方面没有区别.延迟约为100 ns.

Method 3 or 4 should work fine. There’s no difference between them with respect to latency. Latency would be in the order of 100 ns.

如果您需要初始化设备,或者控制允许哪些应用程序访问该设备,或者一次强制一个阅读器,等等,则需要方法4.方法3看起来有点像hack,因为它跳过了所有内容这个的.但是,如果您不需要这样的东西,则会更简单.

Method 4 would be needed if you need to initialize the device, or control which applications are allowed to access it, or enforce one reader at a time, etc. Method 3 does seem like a bit of a hack because it skips all of this. But it is simpler if you don’t need such things.

字符设备肯定有更高的延迟,因为每次读取设备时都需要内核转换.

A character device is definitely higher latency, because it requires a kernel transition each time the device is read.

DMA方法的延迟时间完全取决于设备将时间写入内存的频率. CPU访问内存的延迟要比MMIO低,但是如果设备仅执行一次毫秒的DMA,那么这就是您的延迟.另外,由于CPU读取值的频率远低于写入值,因此该方法会产生大量无用的DMA流量.

The latency of a DMA method depends entirely on how frequently the device writes the time to memory. It is lower latency for the CPU to access memory than MMIO, but if the device only does DMA once a millisecond, then that would be your latency. Also, that method generates a lot of useless DMA traffic, since the CPU would read the value far less often than it is written.

这篇关于低延迟访问PCI内存BAR(Linux)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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