11
11
#include < sys/stat.h>
12
12
#include < unistd.h> // for close, getpagesize
13
13
#elif defined(_MSC_VER)
14
+ #define WIN32_LEAN_AND_MEAN
14
15
#include < windows.h>
15
16
#endif // defined(__unix__)
16
17
26
27
27
28
#include " io.h"
28
29
#include " xgboost/logging.h"
30
+ #include " xgboost/collective/socket.h"
29
31
30
32
namespace xgboost {
31
33
namespace common {
@@ -170,7 +172,8 @@ std::size_t GetPageSize() {
170
172
#if defined(_MSC_VER)
171
173
SYSTEM_INFO sys_info;
172
174
GetSystemInfo (&sys_info);
173
- return sys_info.dwPageSize ;
175
+ // During testing, `sys_info.dwPageSize` is of size 4096 while `dwAllocationGranularity` is of size 65536.
176
+ return sys_info.dwAllocationGranularity ;
174
177
#else
175
178
return getpagesize ();
176
179
#endif
@@ -190,18 +193,21 @@ std::size_t PadPageForMmap(std::size_t file_bytes, dmlc::Stream* fo) {
190
193
191
194
struct PrivateMmapStream ::MMAPFile {
192
195
#if defined(_MSC_VER)
193
- HANDLE fd;
196
+ HANDLE fd{ INVALID_HANDLE_VALUE } ;
194
197
#else
195
- std::int32_t fd;
198
+ std::int32_t fd { 0 } ;
196
199
#endif
197
200
std::string path;
198
201
};
199
202
200
203
PrivateMmapStream::PrivateMmapStream (std::string path, bool read_only, std::size_t offset,
201
204
std::size_t length)
202
- : MemoryFixSizeBuffer{Open (std::move (path), read_only, offset, length), length} {}
205
+ : MemoryFixSizeBuffer{} {
206
+ this ->p_buffer_ = Open (std::move (path), read_only, offset, length);
207
+ this ->buffer_size_ = length;
208
+ }
203
209
204
- void * PrivateMmapStream::Open (std::string path, bool read_only, std::size_t offset,
210
+ char * PrivateMmapStream::Open (std::string path, bool read_only, std::size_t offset,
205
211
std::size_t length) {
206
212
#if defined(_MSC_VER)
207
213
HANDLE fd = CreateFile (path.c_str (), GENERIC_READ, FILE_SHARE_READ, nullptr , OPEN_EXISTING,
@@ -211,7 +217,8 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
211
217
auto fd = open (path.c_str (), O_RDONLY);
212
218
CHECK_GE (fd, 0 ) << " Failed to open:" << path << " . " << strerror (errno);
213
219
#endif
214
- handle_.reset (new MMAPFile{fd, std::move (path)});
220
+ handle_ = nullptr ;
221
+ handle_.reset (new MMAPFile{fd, path});
215
222
216
223
void * ptr{nullptr };
217
224
#if defined(__linux__) || defined(__GLIBC__)
@@ -228,8 +235,12 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
228
235
access = read_only ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
229
236
std::uint32_t loff = static_cast <std::uint32_t >(offset);
230
237
std::uint32_t hoff = offset >> 32 ;
238
+ CHECK (map_file) << " Failed to map: " << handle_->path << " . " << GetLastError ();;
231
239
ptr = MapViewOfFile (map_file, access, hoff, loff, length);
232
- CHECK_NE (ptr, nullptr ) << " Failed to map: " << handle_->path << " . " << GetLastError ();
240
+ if (ptr == nullptr ) {
241
+ system::ThrowAtError (" MapViewOfFile" );
242
+ }
243
+ CHECK_NE (ptr, nullptr ) << " Failed to map: " << handle_->path << " . " << GetLastError () ;
233
244
#else
234
245
CHECK_LE (offset, std::numeric_limits<off_t >::max ())
235
246
<< " File size has exceeded the limit on the current system." ;
@@ -240,18 +251,22 @@ void* PrivateMmapStream::Open(std::string path, bool read_only, std::size_t offs
240
251
ptr = reinterpret_cast <char *>(mmap (nullptr , length, prot, MAP_PRIVATE, fd_, offset));
241
252
CHECK_NE (ptr, MAP_FAILED) << " Failed to map: " << handle_->path << " . " << strerror (errno);
242
253
#endif // defined(__linux__)
243
- return ptr;
254
+ return reinterpret_cast < char *>( ptr) ;
244
255
}
245
256
246
257
PrivateMmapStream::~PrivateMmapStream () {
247
258
CHECK (handle_);
248
259
#if defined(_MSC_VER)
249
- CHECK (UnmapViewOfFile (p_buffer_)) " Faled to munmap." << handle_->path << " . " << GetLastError ();
250
- CloseHandle (handle_->fd );
260
+ if (p_buffer_) {
261
+ CHECK (UnmapViewOfFile (p_buffer_)) " Faled to munmap." << GetLastError ();
262
+ }
263
+ if (handle_->fd != INVALID_HANDLE_VALUE) {
264
+ CHECK (CloseHandle (handle_->fd ));
265
+ }
251
266
#else
252
267
CHECK_NE (munmap (p_buffer_, buffer_size_), -1 )
253
268
<< " Faled to munmap." << handle_->path << " . " << strerror (errno);
254
- CHECK_NE (close (fd_ ), -1 ) << " Faled to close: " << handle_->path << " . " << strerror (errno);
269
+ CHECK_NE (close (handle_-> fd ), -1 ) << " Faled to close: " << handle_->path << " . " << strerror (errno);
255
270
#endif
256
271
}
257
272
} // namespace common
0 commit comments