/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/io_posix.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | #ifndef YB_ROCKSDB_UTIL_IO_POSIX_H |
24 | | #define YB_ROCKSDB_UTIL_IO_POSIX_H |
25 | | |
26 | | #pragma once |
27 | | #include <unistd.h> |
28 | | #include "yb/rocksdb/env.h" |
29 | | |
30 | | // For non linux platform, the following macros are used only as place |
31 | | // holder. |
32 | | #if !(defined __linux__) && !(defined CYGWIN) |
33 | | #define POSIX_FADV_NORMAL 0 /* [MC1] no further special treatment */ |
34 | | #define POSIX_FADV_RANDOM 1 /* [MC1] expect random page refs */ |
35 | | #define POSIX_FADV_SEQUENTIAL 2 /* [MC1] expect sequential page refs */ |
36 | | #define POSIX_FADV_WILLNEED 3 /* [MC1] will need these pages */ |
37 | | #define POSIX_FADV_DONTNEED 4 /* [MC1] dont need these pages */ |
38 | | #endif |
39 | | |
40 | | namespace rocksdb { |
41 | | |
42 | 0 | #define STATUS_IO_ERROR(context, err_number) STATUS(IOError, (context), strerror(err_number)) |
43 | | |
44 | | class PosixWritableFile : public WritableFile { |
45 | | private: |
46 | | const std::string filename_; |
47 | | int fd_; |
48 | | uint64_t filesize_; |
49 | | #ifdef ROCKSDB_FALLOCATE_PRESENT |
50 | | bool allow_fallocate_; |
51 | | bool fallocate_with_keep_size_; |
52 | | #endif |
53 | | |
54 | | public: |
55 | | PosixWritableFile(const std::string& fname, int fd, |
56 | | const EnvOptions& options); |
57 | | ~PosixWritableFile(); |
58 | | |
59 | | // Means Close() will properly take care of truncate |
60 | | // and it does not need any additional information |
61 | | virtual Status Truncate(uint64_t size) override; |
62 | | virtual Status Close() override; |
63 | | virtual Status Append(const Slice& data) override; |
64 | | virtual Status Flush() override; |
65 | | virtual Status Sync() override; |
66 | | virtual Status Fsync() override; |
67 | | virtual bool IsSyncThreadSafe() const override; |
68 | | virtual uint64_t GetFileSize() override; |
69 | | virtual Status InvalidateCache(size_t offset, size_t length) override; |
70 | | #ifdef ROCKSDB_FALLOCATE_PRESENT |
71 | | virtual Status Allocate(uint64_t offset, uint64_t len) override; |
72 | | virtual Status RangeSync(uint64_t offset, uint64_t nbytes) override; |
73 | | virtual size_t GetUniqueId(char* id) const override; |
74 | | #endif |
75 | | }; |
76 | | |
77 | | class PosixMmapReadableFile : public RandomAccessFile { |
78 | | private: |
79 | | int fd_; |
80 | | std::string filename_; |
81 | | void* mmapped_region_; |
82 | | size_t length_; |
83 | | |
84 | | public: |
85 | | PosixMmapReadableFile(const int fd, const std::string& fname, void* base, |
86 | | size_t length, const EnvOptions& options); |
87 | | ~PosixMmapReadableFile(); |
88 | | CHECKED_STATUS Read(uint64_t offset, size_t n, Slice* result, uint8_t* scratch) const override; |
89 | | CHECKED_STATUS InvalidateCache(size_t offset, size_t length) override; |
90 | | yb::Result<uint64_t> Size() const override; |
91 | | yb::Result<uint64_t> INode() const override; |
92 | | // Doesn't include memory usage by mmap. |
93 | | size_t memory_footprint() const override; |
94 | 0 | const std::string& filename() const override { return filename_; } |
95 | | }; |
96 | | |
97 | | class PosixMmapFile : public WritableFile { |
98 | | private: |
99 | | std::string filename_; |
100 | | int fd_; |
101 | | size_t page_size_; |
102 | | size_t map_size_; // How much extra memory to map at a time |
103 | | char* base_; // The mapped region |
104 | | char* limit_; // Limit of the mapped region |
105 | | char* dst_; // Where to write next (in range [base_,limit_]) |
106 | | char* last_sync_; // Where have we synced up to |
107 | | uint64_t file_offset_; // Offset of base_ in file |
108 | | #ifdef ROCKSDB_FALLOCATE_PRESENT |
109 | | bool allow_fallocate_; // If false, fallocate calls are bypassed |
110 | | bool fallocate_with_keep_size_; |
111 | | #endif |
112 | | |
113 | | // Roundup x to a multiple of y |
114 | 0 | static size_t Roundup(size_t x, size_t y) { return ((x + y - 1) / y) * y; } |
115 | | |
116 | 0 | size_t TruncateToPageBoundary(size_t s) { |
117 | 0 | s -= (s & (page_size_ - 1)); |
118 | 0 | assert((s % page_size_) == 0); |
119 | 0 | return s; |
120 | 0 | } |
121 | | |
122 | | Status MapNewRegion(); |
123 | | Status UnmapCurrentRegion(); |
124 | | Status Msync(); |
125 | | |
126 | | public: |
127 | | PosixMmapFile(const std::string& fname, int fd, size_t page_size, |
128 | | const EnvOptions& options); |
129 | | ~PosixMmapFile(); |
130 | | |
131 | | // Means Close() will properly take care of truncate |
132 | | // and it does not need any additional information |
133 | | Status Truncate(uint64_t size) override; |
134 | | Status Close() override; |
135 | | Status Append(const Slice& data) override; |
136 | | Status Flush() override; |
137 | | Status Sync() override; |
138 | | Status Fsync() override; |
139 | | uint64_t GetFileSize() override; |
140 | | Status InvalidateCache(size_t offset, size_t length) override; |
141 | | #ifdef ROCKSDB_FALLOCATE_PRESENT |
142 | | Status Allocate(uint64_t offset, uint64_t len) override; |
143 | | #endif |
144 | | }; |
145 | | |
146 | | class PosixDirectory : public Directory { |
147 | | public: |
148 | 437k | explicit PosixDirectory(int fd) : fd_(fd) {} |
149 | | ~PosixDirectory(); |
150 | | virtual Status Fsync() override; |
151 | | |
152 | | private: |
153 | | int fd_; |
154 | | }; |
155 | | |
156 | | } // namespace rocksdb |
157 | | |
158 | | #endif // YB_ROCKSDB_UTIL_IO_POSIX_H |