OmniSciDB  85c2d10cdc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
omnisci_fs.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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/omnisci_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 <windows.h>
26 
27 #include <memoryapi.h>
28 
29 #include "Logger/Logger.h"
30 
31 namespace omnisci {
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);
89  return f;
90 }
91 
92 int get_page_size() {
93  return 4096; // TODO: reasonable guess for now
94 }
95 
96 } // namespace omnisci
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::string filename(char const *path)
Definition: Logger.cpp:62
int open(const char *path, int flags, int mode)
Definition: omnisci_fs.cpp:64
void * checked_mmap(const int fd, const size_t sz)
Definition: omnisci_fs.cpp:38
::FILE * fopen(const char *filename, const char *mode)
Definition: omnisci_fs.cpp:72
#define LOG(tag)
Definition: Logger.h:188
void close(const int fd)
Definition: omnisci_fs.cpp:68
int fsync(int fd)
Definition: omnisci_fs.cpp:60
int msync(void *addr, size_t length, bool async)
Definition: omnisci_fs.cpp:55
void checked_munmap(void *addr, size_t length)
Definition: omnisci_fs.cpp:51
#define CHECK(condition)
Definition: Logger.h:197
char * f
int get_page_size()
Definition: omnisci_fs.cpp:27
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31