简体   繁体   English

设备驱动程序内存缓冲区处理器高速缓存问题

[英]device driver memory buffer processor cache issue

I have a device which sends image data and video frame using two different bulk channels in USB. 我有一台使用USB中的两个不同的批量通道发送图像数据和视频帧的设备。

My workstation processor cache is little bit large enough to hold around 100 video frames without any issue but not image data. 我的工作站处理器高速缓存有点大,足以容纳大约100个视频帧,但没有图像数据。

I'm using same buffer for image and video data and that buffer have around 50 blocks and one block is 1MB size. 我对图像和视频数据使用相同的缓冲区,该缓冲区大约有50个块,一个块的大小为1MB。 The video frames come quickly and then the image frame. 视频帧很快出现,然后是图像帧。

My question is, is there is a memory corruption issue in the following secnario ? 我的问题是,在以下secnario中是否存在内存损坏问题? Somebody who have knowledge in processor cache could help me. 知道处理器高速缓存的人可以为我提供帮助。

  • Because of video frames are small the pages in the memory buffer which writes video frames are almost in the cache. 由于视频帧较小,因此写入视频帧的内存缓冲区中的页面几乎在高速缓存中。 Since video data comes as a stream it never flushed out. 由于视频数据以流的形式出现,因此它永远不会被冲洗掉。
  • but when the image data comes, the large area of the memory buffer will be used, then video memory pages will be flushed out. 但是当图像数据到来时,将使用大面积的存储缓冲区,然后将刷新视频存储页面。 But scheduled to be flushed but still not written to the physical memory. 但是排定要刷新但仍未写入物理内存。
  • Now image data was written to the memory, I've used volatile there. 现在,图像数据已写入内存,我在那里使用了volatile
  • And that data will be corrupted by the cache flush when they were flushed after the image data write. 并且在写入图像数据后刷新数据时,缓存刷新将破坏该数据。

Can this happen? 这会发生吗? So I applied volatile to video data write too and this issue looks like it disappeared. 因此,我也将volatile应用于视频数据写入,这个问题似乎消失了。 But I need to make a report, so is it possible for this above mentioned scenario to happen? 但是我需要报告,因此上述情况有可能发生吗?

The comments are the giveaway: two threads, and volatile is misused as a threading mechanism. 注释是赠品:两个线程和volatile被滥用为线程机制。

Two threads can run on two CPU cores. 两个线程可以在两个CPU内核上运行。 While the cores usually do share memory, they usually do not share the L1 cache. 尽管内核通常共享内存,但它们通常不共享L1缓存。 Intermediate caches vary. 中间缓存有所不同。 As a result, dereferencing the same pointer on two CPU cores may give different results. 结果,在两个CPU内核上取消引用相同的指针可能会得出不同的结果。 This is not a problem for variables that are properly shared across threads; 对于在线程之间正确共享的变量来说,这不是问题。 the compiler will use the correct instructions. 编译器将使用正确的指令。 But the keyword is properly shared. 但是该关键字已正确共享。

Here we get into the slight problem that you've tagged your question both as C and C++, because the two languages forked before threading was standardized in either language. 在这里,我们遇到了一个小问题,您已经将问题标记为C和C ++,因为在线程标准化之前,这两种语言都以这两种语言进行了标准化。 However, the two threading mechanisms are intentionally similar so that a compiler pair can (as an extension) define how C threading and C++ threading interact. 但是,这两种线程机制故意相似,因此编译器对可以(作为扩展)定义C线程和C ++线程如何交互。 You'll need to consult your documentation for that. 您需要为此查阅文档。

It may be easier to wrap the libusb thread in your own code, so that you receive the data without threading issues, and then dispatch from your code to other threads that are also under your control. libusb线程包装在您自己的代码中可能会更容易,这样您就可以在没有线程问题的情况下接收数据,然后将代码从代码分派到同样受您控制的其他线程。

Back to the memory corruption you're seeing: what you probably see is that one thread is writing out its view of memory, which turns out to be stale data in its cache. 回到您看到的内存损坏:您可能看到的是,一个线程正在写出其内存视图,事实证明这是其缓存中的陈旧数据。 Had you used something like a mutex, this stale data would have been noted and caches synchronized. 如果您使用了互斥锁之类的东西,那么这些过时的数据将被记录下来并缓存同步。

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

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