OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavyai_fs.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "OSDependent/heavyai_fs.h"
18 
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 
22 #include <fcntl.h>
23 #include <io.h>
24 
25 #include "Shared/clean_windows.h"
26 
27 #include <memoryapi.h>
28 
29 #include "Logger/Logger.h"
30 
31 namespace heavyai {
32 
33 size_t file_size(const int fd) {
34  struct _stat64i32 buf;
35  const auto err = _fstat64i32(fd, &buf);
36  CHECK_EQ(0, err);
37  return buf.st_size;
38 }
39 
40 void* checked_mmap(const int fd, const size_t sz) {
41  auto handle = _get_osfhandle(fd);
42  HANDLE map_handle =
43  CreateFileMapping(reinterpret_cast<HANDLE>(handle), NULL, PAGE_READWRITE, 0, 0, 0);
44  CHECK(map_handle);
45  auto map_ptr = MapViewOfFile(map_handle, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, sz);
46  CHECK(map_ptr);
47  CHECK(CloseHandle(map_handle) != 0);
48  return map_ptr;
49 }
50 
51 void checked_munmap(void* addr, size_t length) {
52  CHECK(UnmapViewOfFile(addr) != 0);
53 }
54 
55 int msync(void* addr, size_t length, bool async) {
56  auto err = FlushViewOfFile(addr, length);
57  return err != 0 ? 0 : -1;
58 }
59 
60 int fsync(int fd) {
61  // TODO: FlushFileBuffers
62  auto file = _fdopen(fd, "a+");
63  return fflush(file);
64 }
65 
66 int open(const char* path, int flags, int mode) {
67  return _open(path, flags, mode);
68 }
69 
70 void close(const int fd) {
71  _close(fd);
72 }
73 
74 ::FILE* fopen(const char* filename, const char* mode) {
75  FILE* f;
76  auto err = fopen_s(&f, filename, mode);
77  // Handle 'too many open files' error
78  if (err == EMFILE) {
79  auto max_handles = _getmaxstdio();
80  if (max_handles < 8192) {
81  auto res = _setmaxstdio(8192);
82  if (res < 0) {
83  LOG(FATAL) << "Cannot increase maximum number of open files";
84  }
85  err = fopen_s(&f, filename, mode);
86  }
87  }
88  CHECK(!err) << "ERROR [" << filename << ":" << mode << "[" << err << "]";
89  return f;
90 }
91 
92 ::FILE* popen(const char* command, const char* type) {
93  return _popen(command, type);
94 }
95 
96 int32_t pclose(::FILE* fh) {
97  return _pclose(fh);
98 }
99 
100 int get_page_size() {
101  return 4096; // TODO: reasonable guess for now
102 }
103 
104 int32_t ftruncate(const int32_t fd, int64_t length) {
105  return _chsize_s(fd, length);
106 }
107 } // namespace heavyai
#define CHECK_EQ(x, y)
Definition: Logger.h:301
#define LOG(tag)
Definition: Logger.h:285
int32_t pclose(::FILE *fh)
Definition: heavyai_fs.cpp:82
::FILE * popen(const char *command, const char *type)
Definition: heavyai_fs.cpp:78
future< Result > async(Fn &&fn, Args &&...args)
int open(const char *path, int flags, int mode)
Definition: heavyai_fs.cpp:66
::FILE * fopen(const char *filename, const char *mode)
Definition: heavyai_fs.cpp:74
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
int msync(void *addr, size_t length, bool async)
Definition: heavyai_fs.cpp:57
int fsync(int fd)
Definition: heavyai_fs.cpp:62
#define CHECK(condition)
Definition: Logger.h:291
void checked_munmap(void *addr, size_t length)
Definition: heavyai_fs.cpp:53
int32_t ftruncate(const int32_t fd, int64_t length)
Definition: heavyai_fs.cpp:86
void close(const int fd)
Definition: heavyai_fs.cpp:70
int get_page_size()
Definition: heavyai_fs.cpp:29
size_t file_size(const int fd)
Definition: heavyai_fs.cpp:33
void * checked_mmap(const int fd, const size_t sz)
Definition: heavyai_fs.cpp:40