Skip to content

Commit d3987e8

Browse files
committed
cleanup.
1 parent 9bbecf5 commit d3987e8

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/common/io.cc

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@
55
#define NOMINMAX
66
#endif // !defined(NOMINMAX)
77

8-
#if defined(__unix__) || defined(__APPLE__)
8+
#if defined(__unix__) || defined(__APPLE__) || defined(__MINGW32__)
99
#include <fcntl.h> // for open, O_RDONLY
1010
#include <sys/mman.h> // for mmap, mmap64, munmap
11-
#include <sys/stat.h>
12-
#include <unistd.h> // for close, getpagesize
11+
#include <unistd.h> // for close, getpagesize
1312
#elif defined(_MSC_VER)
1413
#define WIN32_LEAN_AND_MEAN
1514
#include <windows.h>
16-
#endif // defined(__unix__)
15+
#endif // defined(__unix__)
1716

18-
#include <algorithm>
19-
#include <cerrno> // for errno
20-
#include <cstdio>
21-
#include <fstream>
22-
#include <limits> // for numeric_limits
23-
#include <memory>
24-
#include <string>
25-
#include <utility>
26-
#include <vector> // for vector
17+
#include <algorithm> // for copy, transform
18+
#include <cctype> // for tolower
19+
#include <cerrno> // for errno
20+
#include <cstddef> // for size_t
21+
#include <cstdint> // for int32_t, uint32_t
22+
#include <cstring> // for memcpy
23+
#include <fstream> // for ifstream
24+
#include <iterator> // for distance
25+
#include <limits> // for numeric_limits
26+
#include <memory> // for unique_ptr
27+
#include <string> // for string
28+
#include <system_error> // for error_code, system_category
29+
#include <utility> // for move
30+
#include <vector> // for vector
2731

2832
#include "io.h"
33+
#include "xgboost/collective/socket.h" // for LastError
2934
#include "xgboost/logging.h"
30-
#include "xgboost/collective/socket.h"
3135

3236
namespace xgboost {
3337
namespace common {
@@ -191,6 +195,14 @@ struct PrivateMmapStream::MMAPFile {
191195
std::string path;
192196
};
193197

198+
namespace {
199+
auto SystemErrorMsg() {
200+
std::int32_t errsv = system::LastError();
201+
auto err = std::error_code{errsv, std::system_category()};
202+
return err;
203+
}
204+
} // anonymous namespace
205+
194206
PrivateMmapStream::PrivateMmapStream(std::string path, bool read_only, std::size_t offset,
195207
std::size_t length)
196208
: MemoryFixSizeBuffer{} {
@@ -206,7 +218,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
206218
CHECK_NE(fd, INVALID_HANDLE_VALUE) << "Failed to open:" << path;
207219
#else
208220
auto fd = open(path.c_str(), O_RDONLY);
209-
CHECK_GE(fd, 0) << "Failed to open:" << path << ". " << strerror(errno);
221+
CHECK_GE(fd, 0) << "Failed to open:" << path << ". " << SystemErrorMsg();
210222
#endif
211223

212224
char* ptr{nullptr};
@@ -218,7 +230,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
218230
prot |= PROT_WRITE;
219231
}
220232
ptr = reinterpret_cast<char*>(mmap64(nullptr, view_size, prot, MAP_PRIVATE, fd, view_start));
221-
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << strerror(errno);
233+
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << SystemErrorMsg();
222234
#elif defined(_MSC_VER)
223235
auto file_size = GetFileSize(fd, nullptr);
224236
DWORD access = read_only ? PAGE_READONLY : PAGE_READWRITE;
@@ -228,10 +240,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
228240
std::uint32_t hoff = view_start >> 32;
229241
CHECK(map_file) << "Failed to map: " << path << ". " << GetLastError();
230242
ptr = reinterpret_cast<char*>(MapViewOfFile(map_file, access, hoff, loff, view_size));
231-
if (ptr == nullptr) {
232-
system::ThrowAtError("MapViewOfFile");
233-
}
234-
CHECK_NE(ptr, nullptr) << "Failed to map: " << path << ". " << GetLastError();
243+
CHECK_NE(ptr, nullptr) << "Failed to map: " << path << ". " << SystemErrorMsg();
235244
#else
236245
CHECK_LE(offset, std::numeric_limits<off_t>::max())
237246
<< "File size has exceeded the limit on the current system.";
@@ -240,7 +249,7 @@ char* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
240249
prot |= PROT_WRITE;
241250
}
242251
ptr = reinterpret_cast<char*>(mmap(nullptr, view_size, prot, MAP_PRIVATE, fd, view_start));
243-
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << strerror(errno);
252+
CHECK_NE(ptr, MAP_FAILED) << "Failed to map: " << path << ". " << SystemErrorMsg();
244253
#endif // defined(__linux__)
245254

246255
handle_.reset(new MMAPFile{fd, ptr, view_size, std::move(path)});

tests/python/test_demos.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def test_cross_validation_demo():
103103
subprocess.check_call(cmd)
104104

105105

106-
@pytest.mark.skipif(**tm.no_unix())
107106
def test_external_memory_demo():
108107
script = os.path.join(PYTHON_DEMO_DIR, 'external_memory.py')
109108
cmd = ['python', script]

0 commit comments

Comments
 (0)