简体   繁体   English

为什么 Linux 命令 cp 不是零拷贝?

[英]Why the Linux command cp is not zero-copy?

I use strace to trace command cp a.txt b.txt .我使用 strace 来跟踪命令cp a.txt b.txt

It shows that it copied the file using read and write syscall.它表明它使用读写系统调用复制了文件。

But there are some zero-copy syscall like sendfile, splice.但是有一些零拷贝系统调用,如 sendfile、splice。 Although splice does not support file-to-file data copy.虽然 splice 不支持文件到文件的数据复制。

Why does not the command cp using those syscall (so we don't have to copy data to userspace)?为什么命令 cp 不使用那些系统调用(所以我们不必将数据复制到用户空间)?

You answered your own question: sendfile() does not support file-to-file operation.您回答了自己的问题: sendfile()不支持文件到文件的操作。

Why does not the Linux kernel provide a syscall which performs file to file data copy with zero-copy?为什么 Linux kernel 不提供一个系统调用来执行零拷贝的文件到文件数据拷贝?

Probably because nobody has a use case where it improves performance by that much or in a way that doesn't have bad trade-offs.可能是因为没有人有一个用例可以将性能提高这么多或以一种没有糟糕权衡的方式。 All you can really do is have a single syscall arrange both the DMA from disk to RAM and from RAM to disk, instead of two syscalls as you have now.您真正能做的就是让一个系统调用将 DMA 从磁盘安排到 RAM 以及从 RAM 到磁盘,而不是像现在这样安排两个系统调用。 If this reduction is syscalls matters to you, look at io_uring: https://vorner.github.io/2019/11/03/io-uring-mental-experiments.html (a very new feature, so you can't expect to see it used in cp yet). If this reduction is syscalls matters to you, look at io_uring: https://vorner.github.io/2019/11/03/io-uring-mental-experiments.html (a very new feature, so you can't expect看到它在cp中使用过)。

People who care a lot about file copy performance often use copy-on-write to avoid copying in the first place, and/or break their files into multiple pieces so they only need to copy or modify one piece instead of a huge file.非常关心文件复制性能的人通常使用写时复制来避免首先进行复制,和/或将他们的文件分成多个部分,因此他们只需要复制或修改一个部分而不是一个巨大的文件。

https://man7.org/linux/man-pages/man2/sendfile.2.html https://man7.org/linux/man-pages/man2/sendfile.2.html

In Linux kernels before 2.6.33, out_fd must refer to a socket.在 2.6.33 之前的 Linux 内核中,out_fd 必须引用套接字。 Since Linux 2.6.33 it can be any file .由于 Linux 2.6.33 它可以是任何文件 If it is a regular file, then sendfile() changes the file offset appropriately.如果它是一个常规文件,那么 sendfile() 会适当地更改文件偏移量。

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

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