summaryrefslogtreecommitdiffstats
path: root/src/rocksdb/port/win/env_win.h
blob: 8fbfb8246cdf6a7296529002ffccfbef2b083ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// An Env is an interface used by the rocksdb implementation to access
// operating system functionality like the filesystem etc.  Callers
// may wish to provide a custom Env object when opening a database to
// get fine gain control; e.g., to rate limit file system operations.
//
// All Env implementations are safe for concurrent access from
// multiple threads without any external synchronization.

#pragma once
#include <stdint.h>
#include <windows.h>

#include <mutex>
#include <string>
#include <vector>

#include "env/composite_env_wrapper.h"
#include "port/port.h"
#include "rocksdb/env.h"
#include "rocksdb/file_system.h"
#include "rocksdb/system_clock.h"
#include "util/threadpool_imp.h"

#undef GetCurrentTime
#undef DeleteFile
#undef LoadLibrary

namespace ROCKSDB_NAMESPACE {
namespace port {

// Currently not designed for inheritance but rather a replacement
class WinEnvThreads {
 public:
  explicit WinEnvThreads(Env* hosted_env);

  ~WinEnvThreads();

  WinEnvThreads(const WinEnvThreads&) = delete;
  WinEnvThreads& operator=(const WinEnvThreads&) = delete;

  void Schedule(void (*function)(void*), void* arg, Env::Priority pri,
                void* tag, void (*unschedFunction)(void* arg));

  int UnSchedule(void* arg, Env::Priority pri);

  void StartThread(void (*function)(void* arg), void* arg);

  void WaitForJoin();

  unsigned int GetThreadPoolQueueLen(Env::Priority pri) const;

  int ReserveThreads(int threads_to_be_reserved, Env::Priority pri);

  int ReleaseThreads(int threads_to_be_released, Env::Priority pri);

  static uint64_t gettid();

  uint64_t GetThreadID() const;

  // Allow increasing the number of worker threads.
  void SetBackgroundThreads(int num, Env::Priority pri);
  int GetBackgroundThreads(Env::Priority pri);

  void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri);

 private:
  Env* hosted_env_;
  mutable std::mutex mu_;
  std::vector<ThreadPoolImpl> thread_pools_;
  std::vector<Thread> threads_to_join_;
};

class WinClock : public SystemClock {
 public:
  WinClock();
  virtual ~WinClock() {}

  static const char* kClassName() { return "WindowsClock"; }
  const char* Name() const override { return kDefaultName(); }
  const char* NickName() const override { return kClassName(); }

  uint64_t NowMicros() override;

  uint64_t NowNanos() override;

  // 0 indicates not supported
  uint64_t CPUMicros() override { return 0; }
  void SleepForMicroseconds(int micros) override;

  Status GetCurrentTime(int64_t* unix_time) override;
  // Converts seconds-since-Jan-01-1970 to a printable string
  virtual std::string TimeToString(uint64_t time);

  uint64_t GetPerfCounterFrequency() const { return perf_counter_frequency_; }

 private:
  using FnGetSystemTimePreciseAsFileTime = VOID(WINAPI*)(LPFILETIME);

  uint64_t perf_counter_frequency_;
  uint64_t nano_seconds_per_period_;
  FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_;
};

class WinFileSystem : public FileSystem {
 public:
  static const std::shared_ptr<WinFileSystem>& Default();
  WinFileSystem(const std::shared_ptr<SystemClock>& clock);
  ~WinFileSystem() {}
  static const char* kClassName() { return "WinFS"; }
  const char* Name() const override { return kClassName(); }
  const char* NickName() const { return kDefaultName(); }

  static size_t GetSectorSize(const std::string& fname);
  size_t GetPageSize() const { return page_size_; }
  size_t GetAllocationGranularity() const { return allocation_granularity_; }

  IOStatus DeleteFile(const std::string& fname, const IOOptions& options,
                      IODebugContext* dbg) override;

  // Truncate the named file to the specified size.
  IOStatus Truncate(const std::string& /*fname*/, size_t /*size*/,
                    const IOOptions& /*options*/,
                    IODebugContext* /*dbg*/) override;
  IOStatus NewSequentialFile(const std::string& fname,
                             const FileOptions& file_opts,
                             std::unique_ptr<FSSequentialFile>* result,
                             IODebugContext* dbg) override;

  IOStatus NewRandomAccessFile(const std::string& fname,
                               const FileOptions& options,
                               std::unique_ptr<FSRandomAccessFile>* result,
                               IODebugContext* /*dbg*/) override;
  IOStatus NewWritableFile(const std::string& f, const FileOptions& file_opts,
                           std::unique_ptr<FSWritableFile>* r,
                           IODebugContext* dbg) override;
  IOStatus ReopenWritableFile(const std::string& fname,
                              const FileOptions& options,
                              std::unique_ptr<FSWritableFile>* result,
                              IODebugContext* dbg) override;

  IOStatus NewRandomRWFile(const std::string& fname,
                           const FileOptions& file_opts,
                           std::unique_ptr<FSRandomRWFile>* result,
                           IODebugContext* dbg) override;
  IOStatus NewMemoryMappedFileBuffer(
      const std::string& fname,
      std::unique_ptr<MemoryMappedFileBuffer>* result) override;

  IOStatus NewDirectory(const std::string& name, const IOOptions& io_opts,
                        std::unique_ptr<FSDirectory>* result,
                        IODebugContext* dbg) override;
  IOStatus FileExists(const std::string& f, const IOOptions& io_opts,
                      IODebugContext* dbg) override;
  IOStatus GetChildren(const std::string& dir, const IOOptions& io_opts,
                       std::vector<std::string>* r,
                       IODebugContext* dbg) override;
  IOStatus CreateDir(const std::string& dirname, const IOOptions& options,
                     IODebugContext* dbg) override;

  // Creates directory if missing. Return Ok if it exists, or successful in
  // Creating.
  IOStatus CreateDirIfMissing(const std::string& dirname,
                              const IOOptions& options,
                              IODebugContext* dbg) override;

  // Delete the specified directory.
  IOStatus DeleteDir(const std::string& dirname, const IOOptions& options,
                     IODebugContext* dbg) override;
  // Store the size of fname in *file_size.
  IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
                       uint64_t* file_size, IODebugContext* dbg) override;
  // Store the last modification time of fname in *file_mtime.
  IOStatus GetFileModificationTime(const std::string& fname,
                                   const IOOptions& options,
                                   uint64_t* file_mtime,
                                   IODebugContext* dbg) override;
  // Rename file src to target.
  IOStatus RenameFile(const std::string& src, const std::string& target,
                      const IOOptions& options, IODebugContext* dbg) override;

  // Hard Link file src to target.
  IOStatus LinkFile(const std::string& /*src*/, const std::string& /*target*/,
                    const IOOptions& /*options*/,
                    IODebugContext* /*dbg*/) override;
  IOStatus NumFileLinks(const std::string& /*fname*/,
                        const IOOptions& /*options*/, uint64_t* /*count*/,
                        IODebugContext* /*dbg*/) override;
  IOStatus AreFilesSame(const std::string& /*first*/,
                        const std::string& /*second*/,
                        const IOOptions& /*options*/, bool* /*res*/,
                        IODebugContext* /*dbg*/) override;
  IOStatus LockFile(const std::string& fname, const IOOptions& options,
                    FileLock** lock, IODebugContext* dbg) override;
  IOStatus UnlockFile(FileLock* lock, const IOOptions& options,
                      IODebugContext* dbg) override;
  IOStatus GetTestDirectory(const IOOptions& options, std::string* path,
                            IODebugContext* dbg) override;

  // Create and returns a default logger (an instance of EnvLogger) for storing
  // informational messages. Derived classes can override to provide custom
  // logger.
  IOStatus NewLogger(const std::string& fname, const IOOptions& io_opts,
                     std::shared_ptr<Logger>* result,
                     IODebugContext* dbg) override;
  // Get full directory name for this db.
  IOStatus GetAbsolutePath(const std::string& db_path, const IOOptions& options,
                           std::string* output_path,
                           IODebugContext* dbg) override;
  IOStatus IsDirectory(const std::string& /*path*/, const IOOptions& options,
                       bool* is_dir, IODebugContext* /*dgb*/) override;
  // This seems to clash with a macro on Windows, so #undef it here
#undef GetFreeSpace
  IOStatus GetFreeSpace(const std::string& /*path*/,
                        const IOOptions& /*options*/, uint64_t* /*diskfree*/,
                        IODebugContext* /*dbg*/) override;
  FileOptions OptimizeForLogWrite(const FileOptions& file_options,
                                  const DBOptions& db_options) const override;
  FileOptions OptimizeForManifestRead(
      const FileOptions& file_options) const override;
  FileOptions OptimizeForManifestWrite(
      const FileOptions& file_options) const override;

 protected:
  static uint64_t FileTimeToUnixTime(const FILETIME& ftTime);
  // Returns true iff the named directory exists and is a directory.

  virtual bool DirExists(const std::string& dname);
  // Helper for NewWritable and ReopenWritableFile
  virtual IOStatus OpenWritableFile(const std::string& fname,
                                    const FileOptions& options,
                                    std::unique_ptr<FSWritableFile>* result,
                                    bool reopen);

 private:
  std::shared_ptr<SystemClock> clock_;
  size_t page_size_;
  size_t allocation_granularity_;
};

// Designed for inheritance so can be re-used
// but certain parts replaced
class WinEnvIO {
 public:
  explicit WinEnvIO(Env* hosted_env);

  virtual ~WinEnvIO();

  virtual Status GetHostName(char* name, uint64_t len);

 private:
  Env* hosted_env_;
};

class WinEnv : public CompositeEnv {
 public:
  WinEnv();

  ~WinEnv();
  static const char* kClassName() { return "WinEnv"; }
  const char* Name() const override { return kClassName(); }
  const char* NickName() const override { return kDefaultName(); }

  Status GetHostName(char* name, uint64_t len) override;

  Status GetThreadList(std::vector<ThreadStatus>* thread_list) override;

  void Schedule(void (*function)(void*), void* arg, Env::Priority pri,
                void* tag, void (*unschedFunction)(void* arg)) override;

  int UnSchedule(void* arg, Env::Priority pri) override;

  void StartThread(void (*function)(void* arg), void* arg) override;

  void WaitForJoin() override;

  unsigned int GetThreadPoolQueueLen(Env::Priority pri) const override;

  int ReserveThreads(int threads_to_be_reserved, Env::Priority pri) override;

  int ReleaseThreads(int threads_to_be_released, Env::Priority pri) override;

  uint64_t GetThreadID() const override;

  // Allow increasing the number of worker threads.
  void SetBackgroundThreads(int num, Env::Priority pri) override;
  int GetBackgroundThreads(Env::Priority pri) override;

  void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) override;

 private:
  WinEnvIO winenv_io_;
  WinEnvThreads winenv_threads_;
};

}  // namespace port
}  // namespace ROCKSDB_NAMESPACE