env_win.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #pragma once
  17. #include "port/win/win_thread.h"
  18. #include <rocksdb/env.h>
  19. #include "util/threadpool_imp.h"
  20. #include <stdint.h>
  21. #include <windows.h>
  22. #include <mutex>
  23. #include <vector>
  24. #include <string>
  25. #undef GetCurrentTime
  26. #undef DeleteFile
  27. #undef GetTickCount
  28. namespace ROCKSDB_NAMESPACE {
  29. namespace port {
  30. // Currently not designed for inheritance but rather a replacement
  31. class WinEnvThreads {
  32. public:
  33. explicit WinEnvThreads(Env* hosted_env);
  34. ~WinEnvThreads();
  35. WinEnvThreads(const WinEnvThreads&) = delete;
  36. WinEnvThreads& operator=(const WinEnvThreads&) = delete;
  37. void Schedule(void(*function)(void*), void* arg, Env::Priority pri,
  38. void* tag, void(*unschedFunction)(void* arg));
  39. int UnSchedule(void* arg, Env::Priority pri);
  40. void StartThread(void(*function)(void* arg), void* arg);
  41. void WaitForJoin();
  42. unsigned int GetThreadPoolQueueLen(Env::Priority pri) const;
  43. static uint64_t gettid();
  44. uint64_t GetThreadID() const;
  45. void SleepForMicroseconds(int micros);
  46. // Allow increasing the number of worker threads.
  47. void SetBackgroundThreads(int num, Env::Priority pri);
  48. int GetBackgroundThreads(Env::Priority pri);
  49. void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri);
  50. private:
  51. Env* hosted_env_;
  52. mutable std::mutex mu_;
  53. std::vector<ThreadPoolImpl> thread_pools_;
  54. std::vector<WindowsThread> threads_to_join_;
  55. };
  56. // Designed for inheritance so can be re-used
  57. // but certain parts replaced
  58. class WinEnvIO {
  59. public:
  60. explicit WinEnvIO(Env* hosted_env);
  61. virtual ~WinEnvIO();
  62. virtual Status DeleteFile(const std::string& fname);
  63. Status Truncate(const std::string& fname, size_t size);
  64. virtual Status GetCurrentTime(int64_t* unix_time);
  65. virtual Status NewSequentialFile(const std::string& fname,
  66. std::unique_ptr<SequentialFile>* result,
  67. const EnvOptions& options);
  68. // Helper for NewWritable and ReopenWritableFile
  69. virtual Status OpenWritableFile(const std::string& fname,
  70. std::unique_ptr<WritableFile>* result,
  71. const EnvOptions& options,
  72. bool reopen);
  73. virtual Status NewRandomAccessFile(const std::string& fname,
  74. std::unique_ptr<RandomAccessFile>* result,
  75. const EnvOptions& options);
  76. // The returned file will only be accessed by one thread at a time.
  77. virtual Status NewRandomRWFile(const std::string& fname,
  78. std::unique_ptr<RandomRWFile>* result,
  79. const EnvOptions& options);
  80. virtual Status NewMemoryMappedFileBuffer(
  81. const std::string& fname,
  82. std::unique_ptr<MemoryMappedFileBuffer>* result);
  83. virtual Status NewDirectory(const std::string& name,
  84. std::unique_ptr<Directory>* result);
  85. virtual Status FileExists(const std::string& fname);
  86. virtual Status GetChildren(const std::string& dir,
  87. std::vector<std::string>* result);
  88. virtual Status CreateDir(const std::string& name);
  89. virtual Status CreateDirIfMissing(const std::string& name);
  90. virtual Status DeleteDir(const std::string& name);
  91. virtual Status GetFileSize(const std::string& fname, uint64_t* size);
  92. static uint64_t FileTimeToUnixTime(const FILETIME& ftTime);
  93. virtual Status GetFileModificationTime(const std::string& fname,
  94. uint64_t* file_mtime);
  95. virtual Status RenameFile(const std::string& src, const std::string& target);
  96. virtual Status LinkFile(const std::string& src, const std::string& target);
  97. virtual Status NumFileLinks(const std::string& /*fname*/,
  98. uint64_t* /*count*/);
  99. virtual Status AreFilesSame(const std::string& first,
  100. const std::string& second, bool* res);
  101. virtual Status LockFile(const std::string& lockFname, FileLock** lock);
  102. virtual Status UnlockFile(FileLock* lock);
  103. virtual Status GetTestDirectory(std::string* result);
  104. virtual Status NewLogger(const std::string& fname,
  105. std::shared_ptr<Logger>* result);
  106. virtual uint64_t NowMicros();
  107. virtual uint64_t NowNanos();
  108. virtual Status GetHostName(char* name, uint64_t len);
  109. virtual Status GetAbsolutePath(const std::string& db_path,
  110. std::string* output_path);
  111. // This seems to clash with a macro on Windows, so #undef it here
  112. #undef GetFreeSpace
  113. // Get the amount of free disk space
  114. virtual Status GetFreeSpace(const std::string& path, uint64_t* diskfree);
  115. virtual std::string TimeToString(uint64_t secondsSince1970);
  116. virtual EnvOptions OptimizeForLogWrite(const EnvOptions& env_options,
  117. const DBOptions& db_options) const;
  118. virtual EnvOptions OptimizeForManifestWrite(
  119. const EnvOptions& env_options) const;
  120. virtual EnvOptions OptimizeForManifestRead(
  121. const EnvOptions& env_options) const;
  122. size_t GetPageSize() const { return page_size_; }
  123. size_t GetAllocationGranularity() const { return allocation_granularity_; }
  124. uint64_t GetPerfCounterFrequency() const { return perf_counter_frequency_; }
  125. static size_t GetSectorSize(const std::string& fname);
  126. private:
  127. // Returns true iff the named directory exists and is a directory.
  128. virtual bool DirExists(const std::string& dname);
  129. typedef VOID(WINAPI * FnGetSystemTimePreciseAsFileTime)(LPFILETIME);
  130. Env* hosted_env_;
  131. size_t page_size_;
  132. size_t allocation_granularity_;
  133. uint64_t perf_counter_frequency_;
  134. uint64_t nano_seconds_per_period_;
  135. FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_;
  136. };
  137. class WinEnv : public Env {
  138. public:
  139. WinEnv();
  140. ~WinEnv();
  141. Status DeleteFile(const std::string& fname) override;
  142. Status Truncate(const std::string& fname, size_t size) override;
  143. Status GetCurrentTime(int64_t* unix_time) override;
  144. Status NewSequentialFile(const std::string& fname,
  145. std::unique_ptr<SequentialFile>* result,
  146. const EnvOptions& options) override;
  147. Status NewRandomAccessFile(const std::string& fname,
  148. std::unique_ptr<RandomAccessFile>* result,
  149. const EnvOptions& options) override;
  150. Status NewWritableFile(const std::string& fname,
  151. std::unique_ptr<WritableFile>* result,
  152. const EnvOptions& options) override;
  153. // Create an object that writes to a new file with the specified
  154. // name. Deletes any existing file with the same name and creates a
  155. // new file. On success, stores a pointer to the new file in
  156. // *result and returns OK. On failure stores nullptr in *result and
  157. // returns non-OK.
  158. //
  159. // The returned file will only be accessed by one thread at a time.
  160. Status ReopenWritableFile(const std::string& fname,
  161. std::unique_ptr<WritableFile>* result,
  162. const EnvOptions& options) override;
  163. // The returned file will only be accessed by one thread at a time.
  164. Status NewRandomRWFile(const std::string& fname,
  165. std::unique_ptr<RandomRWFile>* result,
  166. const EnvOptions& options) override;
  167. Status NewMemoryMappedFileBuffer(
  168. const std::string& fname,
  169. std::unique_ptr<MemoryMappedFileBuffer>* result) override;
  170. Status NewDirectory(const std::string& name,
  171. std::unique_ptr<Directory>* result) override;
  172. Status FileExists(const std::string& fname) override;
  173. Status GetChildren(const std::string& dir,
  174. std::vector<std::string>* result) override;
  175. Status CreateDir(const std::string& name) override;
  176. Status CreateDirIfMissing(const std::string& name) override;
  177. Status DeleteDir(const std::string& name) override;
  178. Status GetFileSize(const std::string& fname,
  179. uint64_t* size) override;
  180. Status GetFileModificationTime(const std::string& fname,
  181. uint64_t* file_mtime) override;
  182. Status RenameFile(const std::string& src,
  183. const std::string& target) override;
  184. Status LinkFile(const std::string& src,
  185. const std::string& target) override;
  186. Status NumFileLinks(const std::string& fname, uint64_t* count) override;
  187. Status AreFilesSame(const std::string& first,
  188. const std::string& second, bool* res) override;
  189. Status LockFile(const std::string& lockFname, FileLock** lock) override;
  190. Status UnlockFile(FileLock* lock) override;
  191. Status GetTestDirectory(std::string* result) override;
  192. Status NewLogger(const std::string& fname,
  193. std::shared_ptr<Logger>* result) override;
  194. uint64_t NowMicros() override;
  195. uint64_t NowNanos() override;
  196. Status GetHostName(char* name, uint64_t len) override;
  197. Status GetAbsolutePath(const std::string& db_path,
  198. std::string* output_path) override;
  199. std::string TimeToString(uint64_t secondsSince1970) override;
  200. Status GetThreadList(std::vector<ThreadStatus>* thread_list) override;
  201. void Schedule(void(*function)(void*), void* arg, Env::Priority pri,
  202. void* tag, void(*unschedFunction)(void* arg)) override;
  203. int UnSchedule(void* arg, Env::Priority pri) override;
  204. void StartThread(void(*function)(void* arg), void* arg) override;
  205. void WaitForJoin();
  206. unsigned int GetThreadPoolQueueLen(Env::Priority pri) const override;
  207. uint64_t GetThreadID() const override;
  208. // This seems to clash with a macro on Windows, so #undef it here
  209. #undef GetFreeSpace
  210. // Get the amount of free disk space
  211. Status GetFreeSpace(const std::string& path, uint64_t* diskfree) override;
  212. void SleepForMicroseconds(int micros) override;
  213. // Allow increasing the number of worker threads.
  214. void SetBackgroundThreads(int num, Env::Priority pri) override;
  215. int GetBackgroundThreads(Env::Priority pri) override;
  216. void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) override;
  217. EnvOptions OptimizeForManifestRead(
  218. const EnvOptions& env_options) const override;
  219. EnvOptions OptimizeForLogWrite(const EnvOptions& env_options,
  220. const DBOptions& db_options) const override;
  221. EnvOptions OptimizeForManifestWrite(
  222. const EnvOptions& env_options) const override;
  223. private:
  224. WinEnvIO winenv_io_;
  225. WinEnvThreads winenv_threads_;
  226. };
  227. } // namespace port
  228. } // namespace ROCKSDB_NAMESPACE