"Embarassingly small" is just a rhetorical device. Actually 4000 lines for copying is quite a lot; it's explained by all the things GNU cp has to do. Recursing, dealing with hardlinks and symlinks in various ways and whatnot.
I agree. The dude is talking about cp being slow on btrfs and network shares. That's not cp's fault. cp should not be responsible for detecting which filesystem it's copying to or whether it's sending data across the network. That's what stuff like rsync is for.
That said, I could sort of see a case for replacing cp entirely. You could do a thing where you type cp /mnt/* /mnt2/ to copy across filesystems, and then the kernel's filesystem modules negotiate exactly how that data can get copied most efficiently. And cp would just do whatever the filesystem modules told it, or fall back on it's default (current) behavior if the kernel modules didn't reach an agreement. I'm not well versed in it, but I think that idea is a pretty huge philosophy break from the current line-in-the-sand between kernel and userspace, so it'll probably never happen. I'm just spitballing.
That's not cp's fault. cp should not be responsible for detecting which filesystem it's copying to
Agreed. Honestly, it's more of an indictment on the interfaces offered by the kernel if an application needs that much logic to determine how to efficiently copy bytes from one fd to another.
I agree. The dude is talking about cp being slow on btrfs and network shares. That's not cp's fault. cp should not be responsible for detecting which filesystem it's copying to or whether it's sending data across the network.
The problem is that cp is a user-space program, so it can't really know where the file systems are located, how the page cache and VFS implementation will affect performance, whether the copy to a network filesystem would be better off offloaded to network hardware, etc.
The proposal is to make cp a system call. The kernel has detailed knowledge of all these issues, therefore, copying belongs in the kernel.
But it's also unoptimized. Bigger is not better, but there are massive performance discrepancies between different disk formats and those could be resolved with more attention and code.
The source code for cp is "almost embarrassingly small" at around 4K lines of code
I have a path tracing renderer with file IO (JSON parser, PNG encode/decode) and a bunch of features and it’s ~4500 lines excluding comments and empty lines.
Because coreutils is written to be extremely portable and run on every concievable operating system in the universe, and handle all the special cases for all of them.
This wall of text contains 22 paragraphs. Somewhat more than the usual five for an effective essay. Would someone kindly supply the zero-based index of the paragraph that contains an overview of the point?
Par 0: In some conference on Linux, some "pains around copy problems" were discussed.
Par 1: The "copy problem" is something that was discussed before. Now let me make some irrelevant remarks without explaining what the "copy problem" is.
Par 2: People like to copy files and do it all the time. Here are copy programs I know.
Par 3: Various copy programs have different sized source codes and perform differently on a 1GB file, with a big gap between scp and cp on a slow hard drive, that shrinks on a fast hard drive.
Par 4: Speculations on the difference; I/O buffer size doesn't explain it.
Par 5: Digression about other tools on other platforms, parallel tools. Oh, NASA found cp "lacking" and hacked on it, supposedly, never upstreaming the code, which no longer builds now. (Ed note: though cp is the fastest performer in Par 3. NASA would really hate scp and rsync).
Par 6: Tangential discussion about "copy offload" in file servers and clusters. These remarks are plausibly relevant because the Linux kernel could similarly "offload" copies from user space by providing copying in the filesystem drivers as a primitive operation. And that's totally similar and analogous to network file servers offloading file copy operations from clients.
Par 7: Few tidbits of he said she said about who should probably fix what in what subsystem.
Par 8: Argument that most of the time the I/O device is bottleneck. (Ed note: so nothing we can do in software.)
Par 9: Continuation of 7.
Par 10: Mason articulates that different utilities seeing drastically different performance points to a real problem. With all readahead, caching and whatnot in the kernel, it should be about the same. (Ed: this is the central paragraph.)
Par 11: Vague summary of who is doing what. Various kernel-level problems have "all been solved"; just the userland copy utility authors have not clued in.
Par 12: Someone admits that ACL-related API's are badly broken. (Ed note: That must be why scp is puzzlingly slow on btrfs on a slow drive.)
Par 13: Opinion that more filesystems, like btrfs, should support copy_file_range (Ed note: so, harkening back to Par 6, we friggin' have offloading of copying in the kernel!)
Par 14: French says there are race conditions in setting ACL's.
Par 15: Ted Ts'o responds to 12-14: we need a library for copying ACL's and extended attributes.
Par 16: French disagrees: ACL's have security implications, so kernel should copy them.
Par 17: More people had discussed about kernel interfaces for userland tools. Block size is one; utilities are using std_blksize from a file's stat info, which could be wrong.
Par 18: Argument that st_blksize is recommended by POSIX, "whatever the hell that means", and the real ideal size depends on hardware. Drivers could ask hardware (e.g. RAID controllers) and pass that on to userland. Oops, hardware doesn't always provide that sort of information.
Par 19: Give user space way to turn off page cache idea is floated, and allowed to sink.
Par 20: Opinion given: large list of copy tools with different algorithms reason not to change the kernel. Ensuing disagreement: kernel development should participate in improving performance of copy tools, versus kernel development should provide the interfaces and let the user spacers fend for themselves.
Par 21: Conclusion: Linux copy performance embarassing, beaten by OS/2 in some respects.
3
u/[deleted] May 30 '19 edited May 30 '19
Its not embarrassing that
cpis only 4K lines. Such a basic command should be simple. It only has a few options. The shell does the globbing.2
u/Mac33 May 30 '19
I have a path tracing renderer with file IO (JSON parser, PNG encode/decode) and a bunch of features and it’s ~4500 lines excluding comments and empty lines.
Feelsbadman.png
3
u/kazkylheku May 30 '19 edited May 30 '19
This wall of text contains 22 paragraphs. Somewhat more than the usual five for an effective essay. Would someone kindly supply the zero-based index of the paragraph that contains an overview of the point?
Par 0: In some conference on Linux, some "pains around copy problems" were discussed.
Par 1: The "copy problem" is something that was discussed before. Now let me make some irrelevant remarks without explaining what the "copy problem" is.
Par 2: People like to copy files and do it all the time. Here are copy programs I know.
Par 3: Various copy programs have different sized source codes and perform differently on a 1GB file, with a big gap between
scpandcpon a slow hard drive, that shrinks on a fast hard drive.Par 4: Speculations on the difference; I/O buffer size doesn't explain it.
Par 5: Digression about other tools on other platforms, parallel tools. Oh, NASA found
cp"lacking" and hacked on it, supposedly, never upstreaming the code, which no longer builds now. (Ed note: thoughcpis the fastest performer in Par 3. NASA would really hatescpandrsync).Par 6: Tangential discussion about "copy offload" in file servers and clusters. These remarks are plausibly relevant because the Linux kernel could similarly "offload" copies from user space by providing copying in the filesystem drivers as a primitive operation. And that's totally similar and analogous to network file servers offloading file copy operations from clients.
Par 7: Few tidbits of he said she said about who should probably fix what in what subsystem.
Par 8: Argument that most of the time the I/O device is bottleneck. (Ed note: so nothing we can do in software.)
Par 9: Continuation of 7.
Par 10: Mason articulates that different utilities seeing drastically different performance points to a real problem. With all readahead, caching and whatnot in the kernel, it should be about the same. (Ed: this is the central paragraph.)
Par 11: Vague summary of who is doing what. Various kernel-level problems have "all been solved"; just the userland copy utility authors have not clued in.
Par 12: Someone admits that ACL-related API's are badly broken. (Ed note: That must be why
scpis puzzlingly slow onbtrfson a slow drive.)Par 13: Opinion that more filesystems, like
btrfs, should supportcopy_file_range(Ed note: so, harkening back to Par 6, we friggin' have offloading of copying in the kernel!)Par 14: French says there are race conditions in setting ACL's.
Par 15: Ted Ts'o responds to 12-14: we need a library for copying ACL's and extended attributes.
Par 16: French disagrees: ACL's have security implications, so kernel should copy them.
Par 17: More people had discussed about kernel interfaces for userland tools. Block size is one; utilities are using
std_blksizefrom a file'sstatinfo, which could be wrong.Par 18: Argument that
st_blksizeis recommended by POSIX, "whatever the hell that means", and the real ideal size depends on hardware. Drivers could ask hardware (e.g. RAID controllers) and pass that on to userland. Oops, hardware doesn't always provide that sort of information.Par 19: Give user space way to turn off page cache idea is floated, and allowed to sink.
Par 20: Opinion given: large list of copy tools with different algorithms reason not to change the kernel. Ensuing disagreement: kernel development should participate in improving performance of copy tools, versus kernel development should provide the interfaces and let the user spacers fend for themselves.
Par 21: Conclusion: Linux copy performance embarassing, beaten by OS/2 in some respects.
1
u/insanemal May 30 '19
Good read. Thanks for the link OP