-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure that LoadSequentialFile() actually read the whole file #5831
Changes from all commits
6810799
5a5f99c
469705b
c8fabb2
5083ca2
efc6f47
d57e0ab
4782c7d
2685ce6
29708e9
7bb99c6
4faae5b
c4b7ffa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,10 @@ | |
#include <unistd.h> | ||
#endif // defined(__unix__) | ||
#include <algorithm> | ||
#include <cstdio> | ||
#include <fstream> | ||
#include <string> | ||
#include <utility> | ||
#include <cstdio> | ||
|
||
#include "xgboost/logging.h" | ||
#include "io.h" | ||
|
@@ -108,39 +109,17 @@ std::string LoadSequentialFile(std::string fname) { | |
}; | ||
|
||
std::string buffer; | ||
#if defined(__unix__) | ||
struct stat fs; | ||
if (stat(fname.c_str(), &fs) != 0) { | ||
OpenErr(); | ||
} | ||
|
||
size_t f_size_bytes = fs.st_size; | ||
buffer.resize(f_size_bytes + 1); | ||
int32_t fd = open(fname.c_str(), O_RDONLY); | ||
#if defined(__linux__) | ||
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); | ||
#endif // defined(__linux__) | ||
ssize_t bytes_read = read(fd, &buffer[0], f_size_bytes); | ||
if (bytes_read < 0) { | ||
close(fd); | ||
ReadErr(); | ||
} | ||
close(fd); | ||
#else // defined(__unix__) | ||
FILE *f = fopen(fname.c_str(), "r"); | ||
if (f == NULL) { | ||
std::string msg; | ||
OpenErr(); | ||
} | ||
fseek(f, 0, SEEK_END); | ||
auto fsize = ftell(f); | ||
fseek(f, 0, SEEK_SET); | ||
Comment on lines
-135
to
-137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: According to the MSDN documentation, Fix. We do away with On Linux, we use |
||
|
||
buffer.resize(fsize + 1); | ||
fread(&buffer[0], 1, fsize, f); | ||
fclose(f); | ||
#endif // defined(__unix__) | ||
// Open in binary mode so that correct file size can be computed with seekg(). | ||
// This accommodates Windows platform: | ||
// https://docs.microsoft.com/en-us/cpp/standard-library/basic-istream-class?view=vs-2019#seekg | ||
std::ifstream ifs(fname, std::ios_base::binary | std::ios_base::in); | ||
ifs.seekg(0, std::ios_base::end); | ||
const size_t file_size = static_cast<size_t>(ifs.tellg()); | ||
ifs.seekg(0, std::ios_base::beg); | ||
buffer.resize(file_size + 1); | ||
ifs.read(&buffer[0], file_size); | ||
buffer.back() = '\0'; | ||
|
||
return buffer; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trivialfis Previously, we called
read()
only once. For a file containing 2896075944 bytes (2.69 GB),read()
only reads 2147479552 bytes (1.99 GB), and the variables would be set as follows:This PR ensures that
bytes_read
would be identical tof_size_bytes
, by callingread()
multiple times in awhile
loop.