2016-05-20 01:40:54 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2016-05-20 01:40:54 +02:00
|
|
|
// 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
|
2017-02-06 23:43:55 +01:00
|
|
|
#include <stdint.h>
|
2017-05-06 01:09:24 +02:00
|
|
|
#include <windows.h>
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2016-05-20 01:40:54 +02:00
|
|
|
#include <mutex>
|
2017-02-06 23:43:55 +01:00
|
|
|
#include <string>
|
2021-01-06 19:48:24 +01:00
|
|
|
#include <vector>
|
2017-02-06 23:43:55 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
#include "env/composite_env_wrapper.h"
|
2021-06-29 15:51:11 +02:00
|
|
|
#include "port/port.h"
|
2021-01-06 19:48:24 +01:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/file_system.h"
|
2021-01-26 07:07:26 +01:00
|
|
|
#include "rocksdb/system_clock.h"
|
2021-01-06 19:48:24 +01:00
|
|
|
#include "util/threadpool_imp.h"
|
2017-02-06 23:43:55 +01:00
|
|
|
|
|
|
|
#undef GetCurrentTime
|
|
|
|
#undef DeleteFile
|
2021-01-06 19:48:24 +01:00
|
|
|
#undef LoadLibrary
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-05-20 01:40:54 +02:00
|
|
|
namespace port {
|
|
|
|
|
|
|
|
// Currently not designed for inheritance but rather a replacement
|
|
|
|
class WinEnvThreads {
|
2021-01-06 19:48:24 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
explicit WinEnvThreads(Env* hosted_env);
|
|
|
|
|
|
|
|
~WinEnvThreads();
|
|
|
|
|
|
|
|
WinEnvThreads(const WinEnvThreads&) = delete;
|
|
|
|
WinEnvThreads& operator=(const WinEnvThreads&) = delete;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
void Schedule(void (*function)(void*), void* arg, Env::Priority pri,
|
|
|
|
void* tag, void (*unschedFunction)(void* arg));
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
int UnSchedule(void* arg, Env::Priority pri);
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
void StartThread(void (*function)(void* arg), void* arg);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
void WaitForJoin();
|
|
|
|
|
|
|
|
unsigned int GetThreadPoolQueueLen(Env::Priority pri) const;
|
|
|
|
|
|
|
|
static uint64_t gettid();
|
|
|
|
|
|
|
|
uint64_t GetThreadID() const;
|
|
|
|
|
|
|
|
// Allow increasing the number of worker threads.
|
|
|
|
void SetBackgroundThreads(int num, Env::Priority pri);
|
2017-05-23 20:04:25 +02:00
|
|
|
int GetBackgroundThreads(Env::Priority pri);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri);
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
2019-03-28 00:31:30 +01:00
|
|
|
Env* hosted_env_;
|
|
|
|
mutable std::mutex mu_;
|
2016-08-26 19:41:35 +02:00
|
|
|
std::vector<ThreadPoolImpl> thread_pools_;
|
2021-06-29 15:51:11 +02:00
|
|
|
std::vector<Thread> threads_to_join_;
|
2016-05-20 01:40:54 +02:00
|
|
|
};
|
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
class WinClock : public SystemClock {
|
2021-01-06 19:48:24 +01:00
|
|
|
public:
|
|
|
|
WinClock();
|
|
|
|
virtual ~WinClock() {}
|
2020-04-17 23:36:51 +02:00
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
const char* Name() const override { return "WindowsClock"; }
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
uint64_t NowMicros() override;
|
|
|
|
|
|
|
|
uint64_t NowNanos() override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
// 0 indicates not supported
|
|
|
|
uint64_t CPUMicros() override { return 0; }
|
|
|
|
void SleepForMicroseconds(int micros) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-26 07:07:26 +01:00
|
|
|
Status GetCurrentTime(int64_t* unix_time) override;
|
2021-01-06 19:48:24 +01:00
|
|
|
// Converts seconds-since-Jan-01-1970 to a printable string
|
|
|
|
virtual std::string TimeToString(uint64_t time);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
uint64_t GetPerfCounterFrequency() const { return perf_counter_frequency_; }
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
2021-09-07 20:31:12 +02:00
|
|
|
using FnGetSystemTimePreciseAsFileTime = VOID(WINAPI*)(LPFILETIME);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2019-03-28 00:31:30 +01:00
|
|
|
uint64_t perf_counter_frequency_;
|
|
|
|
uint64_t nano_seconds_per_period_;
|
2016-05-20 01:40:54 +02:00
|
|
|
FnGetSystemTimePreciseAsFileTime GetSystemTimePreciseAsFileTime_;
|
|
|
|
};
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinFileSystem : public FileSystem {
|
|
|
|
public:
|
|
|
|
static const std::shared_ptr<WinFileSystem>& Default();
|
2021-01-26 07:07:26 +01:00
|
|
|
WinFileSystem(const std::shared_ptr<SystemClock>& clock);
|
2021-01-06 19:48:24 +01:00
|
|
|
~WinFileSystem() {}
|
|
|
|
const char* Name() const { return "WinFS"; }
|
|
|
|
static size_t GetSectorSize(const std::string& fname);
|
|
|
|
size_t GetPageSize() const { return page_size_; }
|
|
|
|
size_t GetAllocationGranularity() const { return allocation_granularity_; }
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
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(
|
2019-03-28 00:31:30 +01:00
|
|
|
const std::string& fname,
|
|
|
|
std::unique_ptr<MemoryMappedFileBuffer>* result) override;
|
2018-05-25 00:05:00 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
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 overide 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.
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
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);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
2021-01-26 07:07:26 +01:00
|
|
|
std::shared_ptr<SystemClock> clock_;
|
2021-01-06 19:48:24 +01:00
|
|
|
size_t page_size_;
|
|
|
|
size_t allocation_granularity_;
|
|
|
|
};
|
2018-08-09 23:21:35 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
// Designed for inheritance so can be re-used
|
|
|
|
// but certain parts replaced
|
|
|
|
class WinEnvIO {
|
|
|
|
public:
|
|
|
|
explicit WinEnvIO(Env* hosted_env);
|
2018-03-06 20:47:42 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual ~WinEnvIO();
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual Status GetHostName(char* name, uint64_t len);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
|
|
|
Env* hosted_env_;
|
|
|
|
};
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinEnv : public CompositeEnv {
|
|
|
|
public:
|
|
|
|
WinEnv();
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
~WinEnv();
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
Status GetHostName(char* name, uint64_t len) override;
|
|
|
|
|
2019-03-28 00:31:30 +01:00
|
|
|
Status GetThreadList(std::vector<ThreadStatus>* thread_list) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
void Schedule(void (*function)(void*), void* arg, Env::Priority pri,
|
|
|
|
void* tag, void (*unschedFunction)(void* arg)) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
int UnSchedule(void* arg, Env::Priority pri) override;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
void StartThread(void (*function)(void* arg), void* arg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2020-09-23 21:54:29 +02:00
|
|
|
void WaitForJoin() override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
unsigned int GetThreadPoolQueueLen(Env::Priority pri) const override;
|
|
|
|
|
|
|
|
uint64_t GetThreadID() const override;
|
|
|
|
|
|
|
|
// Allow increasing the number of worker threads.
|
|
|
|
void SetBackgroundThreads(int num, Env::Priority pri) override;
|
2017-05-23 20:04:25 +02:00
|
|
|
int GetBackgroundThreads(Env::Priority pri) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
void IncBackgroundThreadsIfNeeded(int num, Env::Priority pri) override;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
2019-03-28 00:31:30 +01:00
|
|
|
WinEnvIO winenv_io_;
|
2016-08-26 19:41:35 +02:00
|
|
|
WinEnvThreads winenv_threads_;
|
2016-05-20 01:40:54 +02:00
|
|
|
};
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
} // namespace port
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|