Repository navigation
Support CopyFile with streaming - #12658
Conversation
|
@yongtang, thanks for your PR! By analyzing the history of the files in this pull request, we identified @keveman, @rohan100jain and @josh11b to be potential reviewers. |
|
Can one of the admins verify this patch? |
0066554 to
db86391
Compare
|
/cc @rohan100jain @jart |
rohan100jain
left a comment
There was a problem hiding this comment.
Thanks for the change! Please add tests for the new functionality added!
|
|
||
| Status FileSystem::CopyFile(const string& src, const string& target) { | ||
| uint64 size; | ||
| Status s = GetFileSize(src, &size); |
There was a problem hiding this comment.
Use TF_RETURN_IF_ERROR(GetFileSize(src, &size)); instead. Here and elsewhere...
| while (offset < size) { | ||
| char scratch[kCopyFileBufferSize]; | ||
| StringPiece result; | ||
| size_t chunk = |
| return s; | ||
| } | ||
| if (offset != size) { | ||
| return errors::Aborted("File ", src, " changed while reading: ", size, |
There was a problem hiding this comment.
In this case, we'll end up copying something into target_file. Do those semantics make sense? Or should we delete target_file? What are the semantics in Unix for instance? Either way, can we document this in the header file?
There was a problem hiding this comment.
@rohan100jain That was to follow the old semantics (where no partial copy in memory). Now as we use streaming, it is not possible to rollback any way. The cp in Linux is not impacted by the file change so I just removed the check at the end.
| return src_fs->RenameFile(src, target); | ||
| } | ||
|
|
||
| Status Env::CopyFile(const string& src, const string& target) { |
There was a problem hiding this comment.
the logic for the same FS and different FS seem very similar... would it make sense to create a utility method that takes in FileSystem* src_fs and FileSystem* target_fs and does the copy. For the Filesystem::CopyFile method pass in this, this to that method?
| const StringPiece& data); | ||
|
|
||
| /// A utility routine: copy contents of `oldpath` to `newpath` | ||
| Status CopyFile(Env* env, const string& oldpath, const string& newpath, |
There was a problem hiding this comment.
I don't see where this is implemented?
There was a problem hiding this comment.
Sorry, that was left over when check in. It has been cleaned.
| } | ||
|
|
||
| Status PosixFileSystem::CopyFile(const string& src, const string& target) { | ||
| #if defined(PLATFORM_WINDOWS) |
There was a problem hiding this comment.
Why do you need to handle this case? From what I understand this case should be handled by platform/windows/windows_file_system
| } | ||
| string translated_target = TranslateName(target); | ||
| int target_fd = | ||
| open64(translated_target.c_str(), O_WRONLY | O_CREAT, |
There was a problem hiding this comment.
Add a comment here explaining all the flags turned on.
|
@rohan100jain will this make the 1.4 release? |
db86391 to
72b848e
Compare
|
Thanks @rohan100jain for the review. The PR has been updated. Please take a look. |
|
Jenkins, test this please. |
|
@yongtang can you look into the failing tests |
|
On Mac sendfile is not defined in Will take a look and update the PR. |
|
@yongtang It seems like there is also an issue with the Linux ARM build: 11:44:48 /android/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++ --std=c++11 -DIS_SLIM_BUILD -fno-exceptions -DNDEBUG -O2 --sysroot /android/ndk/platforms/android-21/arch-arm -Wno-narrowing -fomit-frame-pointer -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fPIE -MT /workspace/tensorflow/contrib/makefile/gen/obj/tensorflow/core/platform/posix/posix_file_system.o -MMD -MP -MF /workspace/tensorflow/contrib/makefile/gen/dep//tensorflow/core/platform/posix/posix_file_system.Td -I/android/ndk/sources/android/support/include -I/android/ndk/sources/cxx-stl/gnu-libstdc++/4.9/include -I/android/ndk/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include -I. -I/workspace/tensorflow/contrib/makefile/downloads/ -I/workspace/tensorflow/contrib/makefile/downloads/eigen -I/workspace/tensorflow/contrib/makefile/downloads/gemmlowp -I/workspace/tensorflow/contrib/makefile/downloads/nsync/public -I/workspace/tensorflow/contrib/makefile/downloads/fft2d -I/workspace/tensorflow/contrib/makefile/gen/protobuf/include -I/workspace/tensorflow/contrib/makefile/gen/proto/ -I/workspace/tensorflow/contrib/makefile/gen/proto_text/ -c tensorflow/core/platform/posix/posix_file_system.cc -o
11:44:50 tensorflow/core/platform/posix/posix_file_system.cc: In member function 'virtual tensorflow::Status tensorflow::PosixFileSystem::CopyFile(const string&, const string&)':
11:44:50 tensorflow/core/platform/posix/posix_file_system.cc:307:54: error: cannot convert 'off_t* {aka long int*}' to 'off64_t* {aka long long int*}' for argument '3' to 'ssize_t sendfile64(int, int, off64_t*, size_t)'
11:44:50 rc = sendfile64(target_fd, src_fd, &offset, chunk);
11:44:50 ^ |
72b848e to
242a202
Compare
|
@quaeler Thanks and sorry for the late reply (was out of town). The Android failure could be fixed with |
|
@drpngx could you kick off a build please |
|
Jenkins, test this please. |
|
Jenkins seems troubled again - https://ci.tensorflow.org/job/tensorflow-pull-requests-mac/6766/console (Thanks for the kickoff, @drpngx!) |
6865bd4 to
76ac19b
Compare
|
Jenkins, test this please. |
jart
left a comment
There was a problem hiding this comment.
I am so excited about this change. I really can't thank you enough for taking the initiative on this suggestion.
|
|
||
| uint64 offset = 0; | ||
| while (offset < size) { | ||
| char scratch[kCopyFileBufferSize]; |
There was a problem hiding this comment.
One fabulous fact about Google is stacks are 64kB and individual frames can't exceed 16kB. Please use heap memory here.
| Status CopyFile(FileSystem* src_fs, const string& src, FileSystem* target_fs, | ||
| const string& target) { | ||
| uint64 size; | ||
| TF_RETURN_IF_ERROR(src_fs->GetFileSize(src, &size)); |
There was a problem hiding this comment.
From RandomAccessFile:
/// Returns `OUT_OF_RANGE` if fewer than n bytes were stored in `*result`
/// because of EOF.So you could possibly be able to avoid the round trip of fetching the length, if you check the status returned by Read.
There was a problem hiding this comment.
Thanks. The PR has been updated and size has been removed.
| TF_RETURN_IF_ERROR(src_fs->GetFileSize(src, &size)); | ||
|
|
||
| std::unique_ptr<RandomAccessFile> src_file; | ||
| TF_RETURN_IF_ERROR(src_fs->NewRandomAccessFile(src, &src_file)); |
There was a problem hiding this comment.
Do we want copies to be atomic? I just noticed our GCS RandomAccessFile impl doesn't lock-in the generation. Can we do that @dbcode? If that can happen soon, I don't see any reason to block this PR.
| // O_WRONLY | O_CREAT: | ||
| // Open file for write and if file does not exist, create the file. | ||
| // S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH: | ||
| // Create the file with permission of 0666 |
| char scratch[kCopyFileBufferSize]; | ||
| StringPiece result; | ||
| size_t bytes_to_read = | ||
| offset + sizeof(scratch) < size ? sizeof(scratch) : size - offset; |
There was a problem hiding this comment.
This is fine, but can we have an if statement instead? I always get scared when I have to search Google to make sure unsafe code is correct. Same goes for that other ternary.
|
|
||
| namespace tensorflow { | ||
|
|
||
| constexpr size_t kPosixCopyFileBufferSize = 1024 * 1024; |
There was a problem hiding this comment.
128kB is the optimal according to this guy: https://eklitzke.org/efficient-file-copying-on-linux
| break; | ||
| } | ||
| } | ||
| close(target_fd); |
There was a problem hiding this comment.
Please check for close errors.
| size_t chunk = (sbuf.st_size - offset) < SSIZE_MAX ? (sbuf.st_size - offset) | ||
| : SSIZE_MAX; | ||
| #if !defined(__APPLE__) | ||
| rc = sendfile(target_fd, src_fd, &offset, chunk); |
| #if !defined(__APPLE__) | ||
| rc = sendfile(target_fd, src_fd, &offset, chunk); | ||
| #else | ||
| char buffer[kPosixCopyFileBufferSize]; |
| int rc = 0; | ||
| off_t offset = 0; | ||
| while (offset < sbuf.st_size) { | ||
| size_t chunk = (sbuf.st_size - offset) < SSIZE_MAX ? (sbuf.st_size - offset) |
There was a problem hiding this comment.
Mixing signed and unsigned with integer limits like this scares me a little bit because I can't tell if the code is correct. Would it be possible to write this in a way there the code is obviously correct? Even if it results in a ton of static_cast<> boilerplate? I'll also note that I would personally feel completely fine if sendfile() context-switched back into userspace every 128kB. What matters IIRC is that the memory itself isn't being copied between the two.
f62de77 to
1e559be
Compare
| while (offset < sbuf.st_size) { | ||
| size_t chunk = (sbuf.st_size - offset) < SSIZE_MAX ? (sbuf.st_size - offset) | ||
| : SSIZE_MAX; | ||
| #if !defined(__APPLE__) |
There was a problem hiding this comment.
Having done more homework, I would say change this to #if defined(__linux__) && !defined(__ANDROID__) because it won't work on FreeBSD and you never know with Android. I've confirmed it will work on Linux kernels ≥2.6.33 (published in 2010.) TensorFlow supports as far back as CentOS 7, which uses Linux 3.10.0, so this should be fine. If we want to be even more conservative, we could use the splice system call instead, which became available in 2.6.17 in 2006. It's Linux-only, but we've constrained ourselves to Linux anyway.
1e559be to
b34003a
Compare
|
Thanks @jart for the review. The PR has been updated. The Windows build might fail as there was a linkage error before and I haven't been able to find out the reason. Unfortunately I don't have a Windows dev machine available at the moment. I will see what I could do to resolve the Windows build error. |
|
|
||
| /// A utility routine: copy contents of `src` in file system `src_fs` | ||
| /// to `target` in file system `target_fs`. | ||
| Status CopyFile(FileSystem* src_fs, const string& src, FileSystem* target_fs, |
There was a problem hiding this comment.
It might confuse users if env.h provides two APIs for CopyFile. The CMake error also points to this helper being the linker error. I'm not entirely sure why, but if the helper is removed it might fix the problem.
There was a problem hiding this comment.
Thanks @jart for the help and sorry for the late reply. I made a change to the name CopyFile -> FileSystemCopyFile and it seems that Windows build is successful. Please take a look.
This fix tries to address the issue raised in 12641 where it was not possible to have CopyFile with streaming. The original implementation copies the whole content of the file to a string buffer and write to the file. This could be an issue if the file size is large (than memory of the host). This fix streams the CopyFile operation. This fix fixes 12641. Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
This commit adds sendfile processing for OSX Darwin. Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
Signed-off-by: Yong Tang <[email protected]>
015abf2 to
b87e4c4
Compare
|
@jart The PR has been updated. Now all build passes (I think |
|
@jart good to go? |
|
Nagging Assignee: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly. |
1 similar comment
|
Nagging Assignee: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly. |
|
@jart Can you take a look and see if the PR is ready to go? I think the build |
jart
left a comment
There was a problem hiding this comment.
Oh I didn't realize I was the blocker. Thanks for the ping.
This change is both fabulous and difficult. Something nice was accomplished here. I think it'll have impact, yet poses a tiny risk for bugs. If we keep our eyes peeled, and maybe attract a few more looks at this impl after we submit it, I think we're going to be fine.
LGTM
|
Yup, it's unrelated. |
This fix tries to address the issue raised in #12641 where it was not possible to have CopyFile with streaming. The original implementation copies the whole content of the file to a string
buffer and write to the file. This could be an issue if the file size is too large (than the memory of the host).
This fix streams the CopyFile operation.
Also, sendfile is used if the file system is posix
This fix fixes #12641.
Signed-off-by: Yong Tang [email protected]