Skip to content

Support CopyFile with streaming - #12658

Merged
jhseu merged 10 commits into
tensorflow:masterfrom
yongtang:12641-CopyFile-stream
Feb 8, 2018
Merged

jhseu merged 10 commits into
tensorflow:masterfrom
yongtang:12641-CopyFile-stream

Conversation

@yongtang

@yongtang yongtang commented Aug 28, 2017 •

Copy link
Copy Markdown
Member

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]

@mention-bot

Copy link
Copy Markdown

@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.

@tensorflow-jenkins

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@yongtang

Copy link
Copy Markdown
Member Author

/cc @rohan100jain @jart

@yifeif yifeif added the awaiting review Pull request awaiting review label Sep 7, 2017

@rohan100jain rohan100jain left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change! Please add tests for the new functionality added!

Comment thread tensorflow/core/platform/file_system.cc Outdated

Status FileSystem::CopyFile(const string& src, const string& target) {
uint64 size;
Status s = GetFileSize(src, &size);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TF_RETURN_IF_ERROR(GetFileSize(src, &size)); instead. Here and elsewhere...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rohan100jain, done.

Comment thread tensorflow/core/platform/file_system.cc Outdated
while (offset < size) {
char scratch[kCopyFileBufferSize];
StringPiece result;
size_t chunk =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name this bytes_to_read?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread tensorflow/core/platform/file_system.cc Outdated
return s;
}
if (offset != size) {
return errors::Aborted("File ", src, " changed while reading: ", size,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread tensorflow/core/platform/env.h Outdated
const StringPiece& data);

/// A utility routine: copy contents of `oldpath` to `newpath`
Status CopyFile(Env* env, const string& oldpath, const string& newpath,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see where this is implemented?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to handle this case? From what I understand this case should be handled by platform/windows/windows_file_system

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. Thanks.

}
string translated_target = TranslateName(target);
int target_fd =
open64(translated_target.c_str(), O_WRONLY | O_CREAT,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here explaining all the flags turned on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thanks.

@quaeler

quaeler commented Sep 8, 2017

Copy link
Copy Markdown
Contributor

@rohan100jain will this make the 1.4 release?

@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch from db86391 to 72b848e Compare September 16, 2017 02:19
@yongtang

Copy link
Copy Markdown
Member Author

Thanks @rohan100jain for the review. The PR has been updated. Please take a look.

@drpngx

drpngx commented Sep 17, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@sb2nov

sb2nov commented Sep 27, 2017

Copy link
Copy Markdown
Contributor

@yongtang can you look into the failing tests

@yongtang

yongtang commented Oct 1, 2017

Copy link
Copy Markdown
Member Author

On Mac sendfile is not defined in <sys/sendfile.h>. Though it looks like there is a Darwin version of sendfile:
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man2/sendfile.2.html

Will take a look and update the PR.

@quaeler

quaeler commented Oct 1, 2017

Copy link
Copy Markdown
Contributor

@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                                                       ^

@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch from 72b848e to 242a202 Compare October 5, 2017 21:48
@yongtang

yongtang commented Oct 5, 2017

Copy link
Copy Markdown
Member Author

@quaeler Thanks and sorry for the late reply (was out of town). The Android failure could be fixed with sendfile64 -> sendfile. I also updated the Mac OS X implementation so that appropriate sendfile API could be used.

@quaeler

quaeler commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

@drpngx could you kick off a build please

@drpngx

drpngx commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

Jenkins, test this please.

@quaeler

quaeler commented Oct 6, 2017

Copy link
Copy Markdown
Contributor

@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch 2 times, most recently from 6865bd4 to 76ac19b Compare October 6, 2017 04:59
@yongtang

yongtang commented Oct 6, 2017

Copy link
Copy Markdown
Member Author

Thanks @quaeler @drpngx, the PR has been updated with Darwin OS X fix.

@quaeler

quaeler commented Oct 7, 2017

Copy link
Copy Markdown
Contributor

Thanks @yongtang - your branch now builds the libtensorflow.so target fine for me on OS X 10.12.6.

@drpngx could you pass this on to Jenkins please?

@martinwicke

Copy link
Copy Markdown
Member

Jenkins, test this please.

@martinwicke martinwicke added awaiting testing (then merge) and removed awaiting review Pull request awaiting review labels Nov 7, 2017

@jart jart left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so excited about this change. I really can't thank you enough for taking the initiative on this suggestion.

Comment thread tensorflow/core/platform/env.cc Outdated

uint64 offset = 0;
while (offset < size) {
char scratch[kCopyFileBufferSize];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One fabulous fact about Google is stacks are 64kB and individual frames can't exceed 16kB. Please use heap memory here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks.

Comment thread tensorflow/core/platform/env.cc Outdated
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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 0644?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Thanks.

Comment thread tensorflow/core/platform/env.cc Outdated
char scratch[kCopyFileBufferSize];
StringPiece result;
size_t bytes_to_read =
offset + sizeof(scratch) < size ? sizeof(scratch) : size - offset;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


namespace tensorflow {

constexpr size_t kPosixCopyFileBufferSize = 1024 * 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

break;
}
}
close(target_fd);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check for close errors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kernelspace! :D

#if !defined(__APPLE__)
rc = sendfile(target_fd, src_fd, &offset, chunk);
#else
char buffer[kPosixCopyFileBufferSize];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on heap memory.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch from f62de77 to 1e559be Compare November 11, 2017 21:24
while (offset < sbuf.st_size) {
size_t chunk = (sbuf.st_size - offset) < SSIZE_MAX ? (sbuf.st_size - offset)
: SSIZE_MAX;
#if !defined(__APPLE__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jart. Done.

@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch from 1e559be to b34003a Compare November 12, 2017 20:10
@yongtang

Copy link
Copy Markdown
Member Author

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.

Comment thread tensorflow/core/platform/env.h Outdated

/// 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
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]>
@yongtang
yongtang force-pushed the 12641-CopyFile-stream branch from 015abf2 to b87e4c4 Compare December 12, 2017 00:28
@yongtang

Copy link
Copy Markdown
Member Author

@jart The PR has been updated. Now all build passes (I think Ubuntu contrib failure is unrelated). Please take a look and let me know if there are any issues.

@drpngx drpngx added stat:awaiting tensorflower Status - Awaiting response from tensorflower and removed stat:awaiting response Status - Awaiting response from author labels Dec 29, 2017
@drpngx

drpngx commented Dec 29, 2017

Copy link
Copy Markdown
Contributor

@jart good to go?

@tensorflowbutler

Copy link
Copy Markdown
Member

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
@tensorflowbutler

Copy link
Copy Markdown
Member

Nagging Assignee: It has been 14 days with no activity and this issue has an assignee. Please update the label and/or status accordingly.

@yongtang

yongtang commented Feb 6, 2018

Copy link
Copy Markdown
Member Author

@jart Can you take a look and see if the PR is ready to go? I think the build Ubuntu contrib is unrelated.

@jart jart left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@martinwicke martinwicke added awaiting testing (then merge) and removed stat:awaiting tensorflower Status - Awaiting response from tensorflower labels Feb 6, 2018
@martinwicke

Copy link
Copy Markdown
Member

Yup, it's unrelated.

@jhseu
jhseu merged commit b1f5f43 into tensorflow:master Feb 8, 2018
@yongtang
yongtang deleted the 12641-CopyFile-stream branch February 8, 2018 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve all-in-memory file copy architecture (Python at least)