]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/port/win/env_win.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / port / win / env_win.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
8 //
9 // An Env is an interface used by the rocksdb implementation to access
10 // operating system functionality like the filesystem etc. Callers
11 // may wish to provide a custom Env object when opening a database to
12 // get fine gain control; e.g., to rate limit file system operations.
13 //
14 // All Env implementations are safe for concurrent access from
15 // multiple threads without any external synchronization.
16
17 #pragma once
18
19 #include "port/win/win_thread.h"
20 #include <rocksdb/env.h>
21 #include "util/threadpool_imp.h"
22
23 #include <stdint.h>
24 #include <windows.h>
25
26 #include <mutex>
27 #include <vector>
28 #include <string>
29
30
31 #undef GetCurrentTime
32 #undef DeleteFile
33 #undef GetTickCount
34
35 namespace ROCKSDB_NAMESPACE {
36 namespace port {
37
38 // Currently not designed for inheritance but rather a replacement
39 class WinEnvThreads {
40 public:
41
42 explicit WinEnvThreads(Env* hosted_env);
43
44 ~WinEnvThreads();
45
46 WinEnvThreads(const WinEnvThreads&) = delete;
47 WinEnvThreads& operator=(const WinEnvThreads&) = delete;
48
49 void Schedule(void(*function)(void*), void* arg, Env::Priority pri,
50 void* tag, void(*unschedFunction)(void* arg));
51
52 int UnSchedule(void* arg, Env::Priority pri);
53
54 void StartThread(void(*function)(void* arg), void* arg);
55
56 void WaitForJoin();
57
58 unsigned int GetThreadPoolQueueLen(Env::Priority pri) const;
59
60 static uint64_t gettid();
61
62 uint64_t GetThreadID() const;
63
64 void SleepForMicroseconds(int micros);
65
66 // Allow increasing the number of worker threads.
67 void SetBackgroundThreads(int num, Env::Priority pri);
68 int GetBackgroundThreads(Env::Priority pri);
69
70 void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri);
71
72 private:
73
74 Env* hosted_env_;
75 mutable std::mutex mu_;
76 std::vector<ThreadPoolImpl> thread_pools_;
77 std::vector<WindowsThread> threads_to_join_;
78
79 };
80
81 // Designed for inheritance so can be re-used
82 // but certain parts replaced
83 class WinEnvIO {
84 public:
85 explicit WinEnvIO(Env* hosted_env);
86
87 virtual ~WinEnvIO();
88
89 virtual Status DeleteFile(const std::string& fname);
90
91 Status Truncate(const std::string& fname, size_t size);
92
93 virtual Status GetCurrentTime(int64_t* unix_time);
94
95 virtual Status NewSequentialFile(const std::string& fname,
96 std::unique_ptr<SequentialFile>* result,
97 const EnvOptions& options);
98
99 // Helper for NewWritable and ReopenWritableFile
100 virtual Status OpenWritableFile(const std::string& fname,
101 std::unique_ptr<WritableFile>* result,
102 const EnvOptions& options,
103 bool reopen);
104
105 virtual Status NewRandomAccessFile(const std::string& fname,
106 std::unique_ptr<RandomAccessFile>* result,
107 const EnvOptions& options);
108
109 // The returned file will only be accessed by one thread at a time.
110 virtual Status NewRandomRWFile(const std::string& fname,
111 std::unique_ptr<RandomRWFile>* result,
112 const EnvOptions& options);
113
114 virtual Status NewMemoryMappedFileBuffer(
115 const std::string& fname,
116 std::unique_ptr<MemoryMappedFileBuffer>* result);
117
118 virtual Status NewDirectory(const std::string& name,
119 std::unique_ptr<Directory>* result);
120
121 virtual Status FileExists(const std::string& fname);
122
123 virtual Status GetChildren(const std::string& dir,
124 std::vector<std::string>* result);
125
126 virtual Status CreateDir(const std::string& name);
127
128 virtual Status CreateDirIfMissing(const std::string& name);
129
130 virtual Status DeleteDir(const std::string& name);
131
132 virtual Status GetFileSize(const std::string& fname, uint64_t* size);
133
134 static uint64_t FileTimeToUnixTime(const FILETIME& ftTime);
135
136 virtual Status GetFileModificationTime(const std::string& fname,
137 uint64_t* file_mtime);
138
139 virtual Status RenameFile(const std::string& src, const std::string& target);
140
141 virtual Status LinkFile(const std::string& src, const std::string& target);
142
143 virtual Status NumFileLinks(const std::string& /*fname*/,
144 uint64_t* /*count*/);
145
146 virtual Status AreFilesSame(const std::string& first,
147 const std::string& second, bool* res);
148
149 virtual Status LockFile(const std::string& lockFname, FileLock** lock);
150
151 virtual Status UnlockFile(FileLock* lock);
152
153 virtual Status GetTestDirectory(std::string* result);
154
155 virtual Status NewLogger(const std::string& fname,
156 std::shared_ptr<Logger>* result);
157
158 virtual Status IsDirectory(const std::string& path, bool* is_dir);
159
160 virtual uint64_t NowMicros();
161
162 virtual uint64_t NowNanos();
163
164 virtual Status GetHostName(char* name, uint64_t len);
165
166 virtual Status GetAbsolutePath(const std::string& db_path,
167 std::string* output_path);
168
169 // This seems to clash with a macro on Windows, so #undef it here
170 #undef GetFreeSpace
171
172 // Get the amount of free disk space
173 virtual Status GetFreeSpace(const std::string& path, uint64_t* diskfree);
174
175 virtual std::string TimeToString(uint64_t secondsSince1970);
176
177 virtual EnvOptions OptimizeForLogWrite(const EnvOptions& env_options,
178 const DBOptions& db_options) const;
179
180 virtual EnvOptions OptimizeForManifestWrite(
181 const EnvOptions& env_options) const;
182
183 virtual EnvOptions OptimizeForManifestRead(
184 const EnvOptions& env_options) const;
185
186 size_t GetPageSize() const { return page_size_; }
187
188 size_t GetAllocationGranularity() const { return allocation_granularity_; }
189
190 uint64_t GetPerfCounterFrequency() const { return perf_counter_frequency_; }
191
192 static size_t GetSectorSize(const std::string& fname);
193
194 private:
195 // Returns true iff the named directory exists and is a directory.
196 virtual bool DirExists(const std::string& dname);
197
198 typedef VOID(WINAPI * FnGetSystemTimePreciseAsFileTime)(LPFILETIME);
199
200 Env* hosted_env_;
201 size_t page_size_;
202 size_t allocation_granularity_;
203 uint64_t perf_counter_frequency_;
204 uint64_t nano_seconds_per_period_;
205 FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_;
206 };
207
208 class WinEnv : public Env {
209 public:
210 WinEnv();
211
212 ~WinEnv();
213
214 Status DeleteFile(const std::string& fname) override;
215
216 Status Truncate(const std::string& fname, size_t size) override;
217
218 Status GetCurrentTime(int64_t* unix_time) override;
219
220 Status NewSequentialFile(const std::string& fname,
221 std::unique_ptr<SequentialFile>* result,
222 const EnvOptions& options) override;
223
224 Status NewRandomAccessFile(const std::string& fname,
225 std::unique_ptr<RandomAccessFile>* result,
226 const EnvOptions& options) override;
227
228 Status NewWritableFile(const std::string& fname,
229 std::unique_ptr<WritableFile>* result,
230 const EnvOptions& options) override;
231
232 // Create an object that writes to a new file with the specified
233 // name. Deletes any existing file with the same name and creates a
234 // new file. On success, stores a pointer to the new file in
235 // *result and returns OK. On failure stores nullptr in *result and
236 // returns non-OK.
237 //
238 // The returned file will only be accessed by one thread at a time.
239 Status ReopenWritableFile(const std::string& fname,
240 std::unique_ptr<WritableFile>* result,
241 const EnvOptions& options) override;
242
243 // The returned file will only be accessed by one thread at a time.
244 Status NewRandomRWFile(const std::string& fname,
245 std::unique_ptr<RandomRWFile>* result,
246 const EnvOptions& options) override;
247
248 Status NewMemoryMappedFileBuffer(
249 const std::string& fname,
250 std::unique_ptr<MemoryMappedFileBuffer>* result) override;
251
252 Status NewDirectory(const std::string& name,
253 std::unique_ptr<Directory>* result) override;
254
255 Status FileExists(const std::string& fname) override;
256
257 Status GetChildren(const std::string& dir,
258 std::vector<std::string>* result) override;
259
260 Status CreateDir(const std::string& name) override;
261
262 Status CreateDirIfMissing(const std::string& name) override;
263
264 Status DeleteDir(const std::string& name) override;
265
266 Status GetFileSize(const std::string& fname,
267 uint64_t* size) override;
268
269 Status GetFileModificationTime(const std::string& fname,
270 uint64_t* file_mtime) override;
271
272 Status RenameFile(const std::string& src,
273 const std::string& target) override;
274
275 Status LinkFile(const std::string& src,
276 const std::string& target) override;
277
278 Status NumFileLinks(const std::string& fname, uint64_t* count) override;
279
280 Status AreFilesSame(const std::string& first,
281 const std::string& second, bool* res) override;
282
283 Status LockFile(const std::string& lockFname, FileLock** lock) override;
284
285 Status UnlockFile(FileLock* lock) override;
286
287 Status GetTestDirectory(std::string* result) override;
288
289 Status NewLogger(const std::string& fname,
290 std::shared_ptr<Logger>* result) override;
291
292 Status IsDirectory(const std::string& path, bool* is_dir) override;
293
294 uint64_t NowMicros() override;
295
296 uint64_t NowNanos() override;
297
298 Status GetHostName(char* name, uint64_t len) override;
299
300 Status GetAbsolutePath(const std::string& db_path,
301 std::string* output_path) override;
302
303 std::string TimeToString(uint64_t secondsSince1970) override;
304
305 Status GetThreadList(std::vector<ThreadStatus>* thread_list) override;
306
307 void Schedule(void(*function)(void*), void* arg, Env::Priority pri,
308 void* tag, void(*unschedFunction)(void* arg)) override;
309
310 int UnSchedule(void* arg, Env::Priority pri) override;
311
312 void StartThread(void(*function)(void* arg), void* arg) override;
313
314 void WaitForJoin() override;
315
316 unsigned int GetThreadPoolQueueLen(Env::Priority pri) const override;
317
318 uint64_t GetThreadID() const override;
319
320 // This seems to clash with a macro on Windows, so #undef it here
321 #undef GetFreeSpace
322
323 // Get the amount of free disk space
324 Status GetFreeSpace(const std::string& path, uint64_t* diskfree) override;
325
326 void SleepForMicroseconds(int micros) override;
327
328 // Allow increasing the number of worker threads.
329 void SetBackgroundThreads(int num, Env::Priority pri) override;
330 int GetBackgroundThreads(Env::Priority pri) override;
331
332 void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) override;
333
334 EnvOptions OptimizeForManifestRead(
335 const EnvOptions& env_options) const override;
336
337 EnvOptions OptimizeForLogWrite(const EnvOptions& env_options,
338 const DBOptions& db_options) const override;
339
340 EnvOptions OptimizeForManifestWrite(
341 const EnvOptions& env_options) const override;
342
343
344 private:
345
346 WinEnvIO winenv_io_;
347 WinEnvThreads winenv_threads_;
348 };
349
350 } // namespace port
351 } // namespace ROCKSDB_NAMESPACE