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.
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.
14 // All Env implementations are safe for concurrent access from
15 // multiple threads without any external synchronization.
19 #include "port/win/win_thread.h"
20 #include <rocksdb/env.h>
21 #include "util/threadpool_imp.h"
35 namespace ROCKSDB_NAMESPACE
{
38 // Currently not designed for inheritance but rather a replacement
42 explicit WinEnvThreads(Env
* hosted_env
);
46 WinEnvThreads(const WinEnvThreads
&) = delete;
47 WinEnvThreads
& operator=(const WinEnvThreads
&) = delete;
49 void Schedule(void(*function
)(void*), void* arg
, Env::Priority pri
,
50 void* tag
, void(*unschedFunction
)(void* arg
));
52 int UnSchedule(void* arg
, Env::Priority pri
);
54 void StartThread(void(*function
)(void* arg
), void* arg
);
58 unsigned int GetThreadPoolQueueLen(Env::Priority pri
) const;
60 static uint64_t gettid();
62 uint64_t GetThreadID() const;
64 void SleepForMicroseconds(int micros
);
66 // Allow increasing the number of worker threads.
67 void SetBackgroundThreads(int num
, Env::Priority pri
);
68 int GetBackgroundThreads(Env::Priority pri
);
70 void IncBackgroundThreadsIfNeeded(int num
, Env::Priority pri
);
75 mutable std::mutex mu_
;
76 std::vector
<ThreadPoolImpl
> thread_pools_
;
77 std::vector
<WindowsThread
> threads_to_join_
;
81 // Designed for inheritance so can be re-used
82 // but certain parts replaced
85 explicit WinEnvIO(Env
* hosted_env
);
89 virtual Status
DeleteFile(const std::string
& fname
);
91 Status
Truncate(const std::string
& fname
, size_t size
);
93 virtual Status
GetCurrentTime(int64_t* unix_time
);
95 virtual Status
NewSequentialFile(const std::string
& fname
,
96 std::unique_ptr
<SequentialFile
>* result
,
97 const EnvOptions
& options
);
99 // Helper for NewWritable and ReopenWritableFile
100 virtual Status
OpenWritableFile(const std::string
& fname
,
101 std::unique_ptr
<WritableFile
>* result
,
102 const EnvOptions
& options
,
105 virtual Status
NewRandomAccessFile(const std::string
& fname
,
106 std::unique_ptr
<RandomAccessFile
>* result
,
107 const EnvOptions
& options
);
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
);
114 virtual Status
NewMemoryMappedFileBuffer(
115 const std::string
& fname
,
116 std::unique_ptr
<MemoryMappedFileBuffer
>* result
);
118 virtual Status
NewDirectory(const std::string
& name
,
119 std::unique_ptr
<Directory
>* result
);
121 virtual Status
FileExists(const std::string
& fname
);
123 virtual Status
GetChildren(const std::string
& dir
,
124 std::vector
<std::string
>* result
);
126 virtual Status
CreateDir(const std::string
& name
);
128 virtual Status
CreateDirIfMissing(const std::string
& name
);
130 virtual Status
DeleteDir(const std::string
& name
);
132 virtual Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
134 static uint64_t FileTimeToUnixTime(const FILETIME
& ftTime
);
136 virtual Status
GetFileModificationTime(const std::string
& fname
,
137 uint64_t* file_mtime
);
139 virtual Status
RenameFile(const std::string
& src
, const std::string
& target
);
141 virtual Status
LinkFile(const std::string
& src
, const std::string
& target
);
143 virtual Status
NumFileLinks(const std::string
& /*fname*/,
144 uint64_t* /*count*/);
146 virtual Status
AreFilesSame(const std::string
& first
,
147 const std::string
& second
, bool* res
);
149 virtual Status
LockFile(const std::string
& lockFname
, FileLock
** lock
);
151 virtual Status
UnlockFile(FileLock
* lock
);
153 virtual Status
GetTestDirectory(std::string
* result
);
155 virtual Status
NewLogger(const std::string
& fname
,
156 std::shared_ptr
<Logger
>* result
);
158 virtual Status
IsDirectory(const std::string
& path
, bool* is_dir
);
160 virtual uint64_t NowMicros();
162 virtual uint64_t NowNanos();
164 virtual Status
GetHostName(char* name
, uint64_t len
);
166 virtual Status
GetAbsolutePath(const std::string
& db_path
,
167 std::string
* output_path
);
169 // This seems to clash with a macro on Windows, so #undef it here
172 // Get the amount of free disk space
173 virtual Status
GetFreeSpace(const std::string
& path
, uint64_t* diskfree
);
175 virtual std::string
TimeToString(uint64_t secondsSince1970
);
177 virtual EnvOptions
OptimizeForLogWrite(const EnvOptions
& env_options
,
178 const DBOptions
& db_options
) const;
180 virtual EnvOptions
OptimizeForManifestWrite(
181 const EnvOptions
& env_options
) const;
183 virtual EnvOptions
OptimizeForManifestRead(
184 const EnvOptions
& env_options
) const;
186 size_t GetPageSize() const { return page_size_
; }
188 size_t GetAllocationGranularity() const { return allocation_granularity_
; }
190 uint64_t GetPerfCounterFrequency() const { return perf_counter_frequency_
; }
192 static size_t GetSectorSize(const std::string
& fname
);
195 // Returns true iff the named directory exists and is a directory.
196 virtual bool DirExists(const std::string
& dname
);
198 typedef VOID(WINAPI
* FnGetSystemTimePreciseAsFileTime
)(LPFILETIME
);
202 size_t allocation_granularity_
;
203 uint64_t perf_counter_frequency_
;
204 uint64_t nano_seconds_per_period_
;
205 FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_
;
208 class WinEnv
: public Env
{
214 Status
DeleteFile(const std::string
& fname
) override
;
216 Status
Truncate(const std::string
& fname
, size_t size
) override
;
218 Status
GetCurrentTime(int64_t* unix_time
) override
;
220 Status
NewSequentialFile(const std::string
& fname
,
221 std::unique_ptr
<SequentialFile
>* result
,
222 const EnvOptions
& options
) override
;
224 Status
NewRandomAccessFile(const std::string
& fname
,
225 std::unique_ptr
<RandomAccessFile
>* result
,
226 const EnvOptions
& options
) override
;
228 Status
NewWritableFile(const std::string
& fname
,
229 std::unique_ptr
<WritableFile
>* result
,
230 const EnvOptions
& options
) override
;
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
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
;
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
;
248 Status
NewMemoryMappedFileBuffer(
249 const std::string
& fname
,
250 std::unique_ptr
<MemoryMappedFileBuffer
>* result
) override
;
252 Status
NewDirectory(const std::string
& name
,
253 std::unique_ptr
<Directory
>* result
) override
;
255 Status
FileExists(const std::string
& fname
) override
;
257 Status
GetChildren(const std::string
& dir
,
258 std::vector
<std::string
>* result
) override
;
260 Status
CreateDir(const std::string
& name
) override
;
262 Status
CreateDirIfMissing(const std::string
& name
) override
;
264 Status
DeleteDir(const std::string
& name
) override
;
266 Status
GetFileSize(const std::string
& fname
,
267 uint64_t* size
) override
;
269 Status
GetFileModificationTime(const std::string
& fname
,
270 uint64_t* file_mtime
) override
;
272 Status
RenameFile(const std::string
& src
,
273 const std::string
& target
) override
;
275 Status
LinkFile(const std::string
& src
,
276 const std::string
& target
) override
;
278 Status
NumFileLinks(const std::string
& fname
, uint64_t* count
) override
;
280 Status
AreFilesSame(const std::string
& first
,
281 const std::string
& second
, bool* res
) override
;
283 Status
LockFile(const std::string
& lockFname
, FileLock
** lock
) override
;
285 Status
UnlockFile(FileLock
* lock
) override
;
287 Status
GetTestDirectory(std::string
* result
) override
;
289 Status
NewLogger(const std::string
& fname
,
290 std::shared_ptr
<Logger
>* result
) override
;
292 Status
IsDirectory(const std::string
& path
, bool* is_dir
) override
;
294 uint64_t NowMicros() override
;
296 uint64_t NowNanos() override
;
298 Status
GetHostName(char* name
, uint64_t len
) override
;
300 Status
GetAbsolutePath(const std::string
& db_path
,
301 std::string
* output_path
) override
;
303 std::string
TimeToString(uint64_t secondsSince1970
) override
;
305 Status
GetThreadList(std::vector
<ThreadStatus
>* thread_list
) override
;
307 void Schedule(void(*function
)(void*), void* arg
, Env::Priority pri
,
308 void* tag
, void(*unschedFunction
)(void* arg
)) override
;
310 int UnSchedule(void* arg
, Env::Priority pri
) override
;
312 void StartThread(void(*function
)(void* arg
), void* arg
) override
;
314 void WaitForJoin() override
;
316 unsigned int GetThreadPoolQueueLen(Env::Priority pri
) const override
;
318 uint64_t GetThreadID() const override
;
320 // This seems to clash with a macro on Windows, so #undef it here
323 // Get the amount of free disk space
324 Status
GetFreeSpace(const std::string
& path
, uint64_t* diskfree
) override
;
326 void SleepForMicroseconds(int micros
) override
;
328 // Allow increasing the number of worker threads.
329 void SetBackgroundThreads(int num
, Env::Priority pri
) override
;
330 int GetBackgroundThreads(Env::Priority pri
) override
;
332 void IncBackgroundThreadsIfNeeded(int num
, Env::Priority pri
) override
;
334 EnvOptions
OptimizeForManifestRead(
335 const EnvOptions
& env_options
) const override
;
337 EnvOptions
OptimizeForLogWrite(const EnvOptions
& env_options
,
338 const DBOptions
& db_options
) const override
;
340 EnvOptions
OptimizeForManifestWrite(
341 const EnvOptions
& env_options
) const override
;
347 WinEnvThreads winenv_threads_
;
351 } // namespace ROCKSDB_NAMESPACE