在这种情况下我应该在单独的线程中读取文件吗?

发布于 2024-08-12 01:20:41 字数 252 浏览 11 评论 0原文

我正在为嵌入式 Linux 编写一个应用程序,其中 5% 的处理器时间用于读取文件,95% 的处理器时间用于处理文件。如果我在一个线程中读取文件并在另一个线程中继续处理,是否可以提高性能?

我正在从支持 DMA 的 mmc 卡读取数据。文件大小为 20mb,分为 2kb 的块。我将对来自读取器线程的块进行排队并在处理器线程中处理它。因此,仅在从队列中插入和删除时才需要线程同步。

我正在为 ARM9 编程。

什么应该是快速的单线程/多线程。

I am writing an application for embedded linux where 5% of processor time is going in reading a file and 95% on processing it. Can I get some performance improvement if I read file in one thread and keeps on processing in another thread?

I am reading from mmc card which has DMA support. Filesize is of 20mb and it is devided in chunks of 2 kb. I will queue chunks from reader thread and process it in processor thread. So thread sync is needed while inserting and deleting from queue only.

I am programming for ARM9.

What should be fast single threaded / multi threaded.

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

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

发布评论

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

评论(7

云柯 2024-08-19 01:20:41

我建议不要使用其他线程。相反,使用 posix_fadvise() 告诉 Linux 提前读取更多文件。当您的程序处理数据时,内核可以通过 DMA 读取文件。

这假设内核有足够的可用内存用于数据缓冲。如果您的数据处理使用了所有内存,那么内核将忽略 posix_fadvise()。

您需要的确切调用将如下所示:

while( 1 ) {
  ret = read(fd, buffer, 2*1024);
  if( ret < 0 ) abort();
  if( ret == 0 ) break;
  if( ret != 2*1024 ) abort();
  pos += ret;
  ret = posix_fadvise(fd, pos, 8*1024, POSIX_FADV_WILLNEED);
  if( ret ) abort();
  process(buffer);
}

I recommend not using another thread. Instead use posix_fadvise() to tell Linux to read more of your file in advance. The kernel can be reading the file via DMA while your program is processing data.

This assumes that the kernel has enough free memory for data buffering. If your data processing is using all of the memory then the kernel will ignore posix_fadvise().

The exact call that you need would look something like this:

while( 1 ) {
  ret = read(fd, buffer, 2*1024);
  if( ret < 0 ) abort();
  if( ret == 0 ) break;
  if( ret != 2*1024 ) abort();
  pos += ret;
  ret = posix_fadvise(fd, pos, 8*1024, POSIX_FADV_WILLNEED);
  if( ret ) abort();
  process(buffer);
}
扶醉桌前 2024-08-19 01:20:41

唯一能确定的方法就是尝试一下。但听起来好像您需要处理器根据处理器的需要读取文件块。由于您受到处理器限制,因此您可以预期的最大改进是读取时间减少 5%。

两个线程需要内存缓冲区来保存下一个文件块,以便立即可用于处理,并且许多嵌入式系统的可用内存极其有限。

The only way to know for sure is to try it. But it sounds as if you need your processor to read chunks of the file as it is needed by the processor. Since you're processor bound, the most improvement you could expect is the 5% time it takes to read.

Two threads would require an in-memory buffer to hold the next chunk of file so that it's immediately available for processing, and many embedded systems are extremely limited in available memory.

年少掌心 2024-08-19 01:20:41

现在,当您调用 read 时,您的程序会在读取数据时阻塞。然后它在完成后再次启动,我猜你的处理代码会接管。被阻塞的时间不会通过“time”显示为“cpu time”,因为在此期间进程处于睡眠状态。 (这取决于 DMA 是否可用)。

您可能会显示整个程序读取该文件所需的时间有所增加,但您的 cpu 时间不会减少(并且可能会由于同步而增加)。

Right now, when you make the call to read, your program blocks while the data is read. Then it starts up again when it's done and I presume your processing code takes over. The time when it's blocked won't show up as "cpu time" via "time" because the process is in a sleep state during this period. (This depends on DMA being available which it is).

You will probably show a wall-clock increase over the whole program of the time it takes to read in that file, but your cpu time will not go down (and will probably go up due to synchronization).

醉态萌生 2024-08-19 01:20:41

您需要确定几件事。

  1. 这两项活动可以并行进行吗?
    如果硬件/架构会导致处理线程
    阻塞另一个线程,那么就没有任何好处。

  2. 您可以预期的最大增益为 5%(根据阿姆达尔定律)。
    编码的复杂性值得吗?

我建议寻找更有效的处理文件的方法。仔细观察处理线程正在做什么并查看。

There are a couple of things you will want to make sure of.

  1. Can both activities be done in parallel?
    If the hardware/architecture is going to cause the processing thread
    to block the other thread then there will be no gain.

  2. The maximum gain you can expect is 5%, (based on Amdhal's law).
    is the complexity in coding worth that?

I would recommend looking at more efficient ways of processing the file. Look closely at what the processing thread is doing and see.

回忆那么伤 2024-08-19 01:20:41

如果能够在读取过程中处理数据,您可能会得到一些改进,但也必然会产生一些开销。与任何优化问题一样,测量是关键。

真正的问题是是否值得实施某些措施来衡量差异。对于 5% 的最大收益,我怀疑答案是否定的,但这取决于您相对于您的时间而言,这 5% 中的某些潜力的价值。

您的平台支持内存映射文件吗?这将允许你将读数留给操作系统,它可能做得很好。

You would probably get some improvement from being able to process data while the read going, but there will necessarily be some overhead as well. As with any optimization problem measurement is the key.

The real question is whether it's worth implementing something in order to measure the difference. For a 5% maximum gain, I suspect the answer is no, but it's up to you how much the potential for some of that 5% is worth versus your time.

Does your platform support memory-mapped files? That would allow you to leave the reading up to the O/S, which it probably does pretty well.

装纯掩盖桑 2024-08-19 01:20:41

如果您按顺序读取数据,则额外的线程可能不值得,因为内核将提前读取文件并将内容缓存在内存中。内存映射文件,除非您正在为嵌入式系统(其中 MMC 是内存映射的)编写内容,否则变化很小(文件有时必须加载到内存中,并且这些加载将仅由尝试读取而不是显式调用触发)。

If you read the data sequentially the additional thread probably is not worth it, because the kernel will read the file ahead and cache contents in memory. Memory mapping the file, unless you are writing for an embedded system (one where MMC is memory-mapped), changes little (the file has to be loaded in memory sometime and these loads will just be trigerred by attempted reads and not by explicit call).

好听的两个字的网名 2024-08-19 01:20:41

我写了一篇关于

多线程文件访问

在 ddj.com 上。它可能回答了你的部分问题。

I wrote an article about

Multithreaded File Access

on ddj.com. It probably answers a part of your question.

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