2017-04-04 20:12:47 +02:00
|
|
|
// Copyright (c) 2017-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).
|
2017-04-04 20:12:47 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
#include "env/composite_env_wrapper.h"
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
2017-04-04 20:12:47 +02:00
|
|
|
#include "rocksdb/env.h"
|
2021-03-16 03:48:36 +01:00
|
|
|
#include "rocksdb/file_system.h"
|
2017-04-04 20:12:47 +02:00
|
|
|
#include "rocksdb/status.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-04-04 20:12:47 +02:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2021-03-16 03:48:36 +01:00
|
|
|
namespace {
|
|
|
|
class TimedFileSystem : public FileSystemWrapper {
|
2017-04-04 20:12:47 +02:00
|
|
|
public:
|
2021-03-16 03:48:36 +01:00
|
|
|
explicit TimedFileSystem(const std::shared_ptr<FileSystem>& base)
|
|
|
|
: FileSystemWrapper(base) {}
|
|
|
|
|
|
|
|
const char* Name() const override { return "TimedFS"; }
|
|
|
|
IOStatus NewSequentialFile(const std::string& fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSSequentialFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_sequential_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewSequentialFile(fname, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewRandomAccessFile(const std::string& fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_random_access_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewRandomAccessFile(fname, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewWritableFile(const std::string& fname, const FileOptions& options,
|
|
|
|
std::unique_ptr<FSWritableFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_writable_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewWritableFile(fname, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus ReuseWritableFile(const std::string& fname,
|
|
|
|
const std::string& old_fname,
|
|
|
|
const FileOptions& options,
|
|
|
|
std::unique_ptr<FSWritableFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_reuse_writable_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::ReuseWritableFile(fname, old_fname, options,
|
|
|
|
result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewRandomRWFile(const std::string& fname, const FileOptions& options,
|
|
|
|
std::unique_ptr<FSRandomRWFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_random_rw_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewRandomRWFile(fname, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewDirectory(const std::string& name, const IOOptions& options,
|
|
|
|
std::unique_ptr<FSDirectory>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_directory_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewDirectory(name, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus FileExists(const std::string& fname, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_file_exists_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::FileExists(fname, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetChildren(const std::string& dir, const IOOptions& options,
|
|
|
|
std::vector<std::string>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_get_children_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::GetChildren(dir, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetChildrenFileAttributes(const std::string& dir,
|
|
|
|
const IOOptions& options,
|
|
|
|
std::vector<FileAttributes>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_get_children_file_attributes_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::GetChildrenFileAttributes(dir, options, result,
|
|
|
|
dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus DeleteFile(const std::string& fname, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_delete_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::DeleteFile(fname, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateDir(const std::string& dirname, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_create_dir_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::CreateDir(dirname, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus CreateDirIfMissing(const std::string& dirname,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_create_dir_if_missing_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::CreateDirIfMissing(dirname, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus DeleteDir(const std::string& dirname, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_delete_dir_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::DeleteDir(dirname, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetFileSize(const std::string& fname, const IOOptions& options,
|
|
|
|
uint64_t* file_size, IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_get_file_size_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::GetFileSize(fname, options, file_size, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus GetFileModificationTime(const std::string& fname,
|
|
|
|
const IOOptions& options,
|
|
|
|
uint64_t* file_mtime,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_get_file_modification_time_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::GetFileModificationTime(fname, options,
|
|
|
|
file_mtime, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus RenameFile(const std::string& src, const std::string& dst,
|
|
|
|
const IOOptions& options, IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_rename_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::RenameFile(src, dst, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus LinkFile(const std::string& src, const std::string& dst,
|
|
|
|
const IOOptions& options, IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_link_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::LinkFile(src, dst, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus LockFile(const std::string& fname, const IOOptions& options,
|
|
|
|
FileLock** lock, IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_lock_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::LockFile(fname, options, lock, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus UnlockFile(FileLock* lock, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_unlock_file_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::UnlockFile(lock, options, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
IOStatus NewLogger(const std::string& fname, const IOOptions& options,
|
|
|
|
std::shared_ptr<Logger>* result,
|
|
|
|
IODebugContext* dbg) override {
|
2017-04-04 20:12:47 +02:00
|
|
|
PERF_TIMER_GUARD(env_new_logger_nanos);
|
2021-03-16 03:48:36 +01:00
|
|
|
return FileSystemWrapper::NewLogger(fname, options, result, dbg);
|
2017-04-04 20:12:47 +02:00
|
|
|
}
|
|
|
|
};
|
2021-03-16 03:48:36 +01:00
|
|
|
} // namespace
|
2017-04-04 20:12:47 +02:00
|
|
|
|
2021-03-16 03:48:36 +01:00
|
|
|
std::shared_ptr<FileSystem> NewTimedFileSystem(
|
|
|
|
const std::shared_ptr<FileSystem>& base) {
|
|
|
|
return std::make_shared<TimedFileSystem>(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// An environment that measures function call times for filesystem
|
|
|
|
// operations, reporting results to variables in PerfContext.
|
|
|
|
Env* NewTimedEnv(Env* base_env) {
|
|
|
|
std::shared_ptr<FileSystem> timed_fs =
|
|
|
|
NewTimedFileSystem(base_env->GetFileSystem());
|
|
|
|
return new CompositeEnvWrapper(base_env, timed_fs);
|
|
|
|
}
|
2017-04-04 20:12:47 +02:00
|
|
|
|
|
|
|
#else // ROCKSDB_LITE
|
|
|
|
|
2018-04-13 02:55:14 +02:00
|
|
|
Env* NewTimedEnv(Env* /*base_env*/) { return nullptr; }
|
2017-04-04 20:12:47 +02:00
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|