OmniSciDB  2e3a973ef4
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/fcntl.h>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 
23 #include "Logger/Logger.h"
24 
25 namespace omnisci {
26 
28  return getpagesize();
29 }
30 
31 size_t file_size(const int fd) {
32  struct stat buf;
33  int err = fstat(fd, &buf);
34  CHECK_EQ(0, err);
35  return buf.st_size;
36 }
37 
38 void* checked_mmap(const int fd, const size_t sz) {
39  auto ptr = mmap(nullptr, sz, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
40  CHECK(ptr != reinterpret_cast<void*>(-1));
41 #ifdef __linux__
42 #ifdef MADV_HUGEPAGE
43  madvise(ptr, sz, MADV_RANDOM | MADV_WILLNEED | MADV_HUGEPAGE);
44 #else
45  madvise(ptr, sz, MADV_RANDOM | MADV_WILLNEED);
46 #endif
47 #endif
48  return ptr;
49 }
50 
51 void checked_munmap(void* addr, size_t length) {
52  CHECK_EQ(0, munmap(addr, length));
53 }
54 
55 int msync(void* addr, size_t length, bool async) {
56  // TODO: support MS_INVALIDATE?
57  return ::msync(addr, length, async ? MS_ASYNC : MS_SYNC);
58 }
59 
60 int fsync(int fd) {
61  return ::fsync(fd);
62 }
63 
64 int open(const char* path, int flags, int mode) {
65  return ::open(path, flags, mode);
66 }
67 
68 void close(const int fd) {
69  ::close(fd);
70 }
71 
72 } // namespace omnisci
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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
int64_t const int32_t sz
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
int get_page_size()
Definition: omnisci_fs.cpp:27
size_t file_size(const int fd)
Definition: omnisci_fs.cpp:31