OmniSciDB  fe05a0c208
 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>
27 
28 #include <memoryapi.h>
29 
30 #include "Logger/Logger.h"
31 
32 namespace omnisci {
33 
34 size_t file_size(const int fd) {
35  struct _stat64i32 buf;
36  const auto err = _fstat64i32(fd, &buf);
37  CHECK_EQ(0, err);
38  return buf.st_size;
39 }
40 
41 void* checked_mmap(const int fd, const size_t sz) {
42  auto handle = _get_osfhandle(fd);
43  HANDLE map_handle =
44  CreateFileMapping(reinterpret_cast<HANDLE>(handle), NULL, PAGE_READWRITE, 0, 0, 0);
45  CHECK(map_handle);
46  auto map_ptr = MapViewOfFile(map_handle, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, sz);
47  CHECK(map_ptr);
48  CHECK(CloseHandle(map_handle) != 0);
49  return map_ptr;
50 }
51 
52 void checked_munmap(void* addr, size_t length) {
53  CHECK(UnmapViewOfFile(addr) != 0);
54 }
55 
56 int msync(void* addr, size_t length, bool async) {
57  auto err = FlushViewOfFile(addr, length);
58  return err != 0 ? 0 : -1;
59 }
60 
61 int fsync(int fd) {
62  // TODO: FlushFileBuffers
63  auto file = _fdopen(fd, "a+");
64  return fflush(file);
65 }
66 
67 int open(const char* path, int flags, int mode) {
68  return _open(path, flags, mode);
69 }
70 
71 void close(const int fd) {
72  _close(fd);
73 }
74 
75 ::FILE* fopen(const char* filename, const char* mode) {
76  FILE* f;
77  auto err = fopen_s(&f, filename, mode);
78  // Handle 'too many open files' error
79  if (err == EMFILE) {
80  auto max_handles = _getmaxstdio();
81  if (max_handles < 8192) {
82  auto res = _setmaxstdio(8192);
83  if (res < 0) {
84  LOG(FATAL) << "Cannot increase maximum number of open files";
85  }
86  err = fopen_s(&f, filename, mode);
87  }
88  }
89  CHECK(!err);
90  return f;
91 }
92 
93 int get_page_size() {
94  return 4096; // TODO: reasonable guess for now
95 }
96 
97 } // namespace omnisci
#define CHECK_EQ(x, y)
Definition: Logger.h:211
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:194
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:203
char * f
int get_page_size()
Definition: omnisci_fs.cpp:27
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31