1 // Copyright (c) 2017-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 #include "utilities/env_timed.h"
7 #include "env/composite_env_wrapper.h"
8 #include "monitoring/perf_context_imp.h"
9 #include "rocksdb/env.h"
10 #include "rocksdb/file_system.h"
11 #include "rocksdb/status.h"
13 namespace ROCKSDB_NAMESPACE
{
16 TimedFileSystem::TimedFileSystem(const std::shared_ptr
<FileSystem
>& base
)
17 : FileSystemWrapper(base
) {}
18 IOStatus
TimedFileSystem::NewSequentialFile(
19 const std::string
& fname
, const FileOptions
& options
,
20 std::unique_ptr
<FSSequentialFile
>* result
, IODebugContext
* dbg
) {
21 PERF_TIMER_GUARD(env_new_sequential_file_nanos
);
22 return FileSystemWrapper::NewSequentialFile(fname
, options
, result
, dbg
);
25 IOStatus
TimedFileSystem::NewRandomAccessFile(
26 const std::string
& fname
, const FileOptions
& options
,
27 std::unique_ptr
<FSRandomAccessFile
>* result
, IODebugContext
* dbg
) {
28 PERF_TIMER_GUARD(env_new_random_access_file_nanos
);
29 return FileSystemWrapper::NewRandomAccessFile(fname
, options
, result
, dbg
);
32 IOStatus
TimedFileSystem::NewWritableFile(
33 const std::string
& fname
, const FileOptions
& options
,
34 std::unique_ptr
<FSWritableFile
>* result
, IODebugContext
* dbg
) {
35 PERF_TIMER_GUARD(env_new_writable_file_nanos
);
36 return FileSystemWrapper::NewWritableFile(fname
, options
, result
, dbg
);
39 IOStatus
TimedFileSystem::ReuseWritableFile(
40 const std::string
& fname
, const std::string
& old_fname
,
41 const FileOptions
& options
, std::unique_ptr
<FSWritableFile
>* result
,
42 IODebugContext
* dbg
) {
43 PERF_TIMER_GUARD(env_reuse_writable_file_nanos
);
44 return FileSystemWrapper::ReuseWritableFile(fname
, old_fname
, options
, result
,
48 IOStatus
TimedFileSystem::NewRandomRWFile(
49 const std::string
& fname
, const FileOptions
& options
,
50 std::unique_ptr
<FSRandomRWFile
>* result
, IODebugContext
* dbg
) {
51 PERF_TIMER_GUARD(env_new_random_rw_file_nanos
);
52 return FileSystemWrapper::NewRandomRWFile(fname
, options
, result
, dbg
);
55 IOStatus
TimedFileSystem::NewDirectory(const std::string
& name
,
56 const IOOptions
& options
,
57 std::unique_ptr
<FSDirectory
>* result
,
58 IODebugContext
* dbg
) {
59 PERF_TIMER_GUARD(env_new_directory_nanos
);
60 return FileSystemWrapper::NewDirectory(name
, options
, result
, dbg
);
63 IOStatus
TimedFileSystem::FileExists(const std::string
& fname
,
64 const IOOptions
& options
,
65 IODebugContext
* dbg
) {
66 PERF_TIMER_GUARD(env_file_exists_nanos
);
67 return FileSystemWrapper::FileExists(fname
, options
, dbg
);
70 IOStatus
TimedFileSystem::GetChildren(const std::string
& dir
,
71 const IOOptions
& options
,
72 std::vector
<std::string
>* result
,
73 IODebugContext
* dbg
) {
74 PERF_TIMER_GUARD(env_get_children_nanos
);
75 return FileSystemWrapper::GetChildren(dir
, options
, result
, dbg
);
78 IOStatus
TimedFileSystem::GetChildrenFileAttributes(
79 const std::string
& dir
, const IOOptions
& options
,
80 std::vector
<FileAttributes
>* result
, IODebugContext
* dbg
) {
81 PERF_TIMER_GUARD(env_get_children_file_attributes_nanos
);
82 return FileSystemWrapper::GetChildrenFileAttributes(dir
, options
, result
,
86 IOStatus
TimedFileSystem::DeleteFile(const std::string
& fname
,
87 const IOOptions
& options
,
88 IODebugContext
* dbg
) {
89 PERF_TIMER_GUARD(env_delete_file_nanos
);
90 return FileSystemWrapper::DeleteFile(fname
, options
, dbg
);
93 IOStatus
TimedFileSystem::CreateDir(const std::string
& dirname
,
94 const IOOptions
& options
,
95 IODebugContext
* dbg
) {
96 PERF_TIMER_GUARD(env_create_dir_nanos
);
97 return FileSystemWrapper::CreateDir(dirname
, options
, dbg
);
100 IOStatus
TimedFileSystem::CreateDirIfMissing(const std::string
& dirname
,
101 const IOOptions
& options
,
102 IODebugContext
* dbg
) {
103 PERF_TIMER_GUARD(env_create_dir_if_missing_nanos
);
104 return FileSystemWrapper::CreateDirIfMissing(dirname
, options
, dbg
);
107 IOStatus
TimedFileSystem::DeleteDir(const std::string
& dirname
,
108 const IOOptions
& options
,
109 IODebugContext
* dbg
) {
110 PERF_TIMER_GUARD(env_delete_dir_nanos
);
111 return FileSystemWrapper::DeleteDir(dirname
, options
, dbg
);
114 IOStatus
TimedFileSystem::GetFileSize(const std::string
& fname
,
115 const IOOptions
& options
,
117 IODebugContext
* dbg
) {
118 PERF_TIMER_GUARD(env_get_file_size_nanos
);
119 return FileSystemWrapper::GetFileSize(fname
, options
, file_size
, dbg
);
122 IOStatus
TimedFileSystem::GetFileModificationTime(const std::string
& fname
,
123 const IOOptions
& options
,
124 uint64_t* file_mtime
,
125 IODebugContext
* dbg
) {
126 PERF_TIMER_GUARD(env_get_file_modification_time_nanos
);
127 return FileSystemWrapper::GetFileModificationTime(fname
, options
, file_mtime
,
131 IOStatus
TimedFileSystem::RenameFile(const std::string
& src
,
132 const std::string
& dst
,
133 const IOOptions
& options
,
134 IODebugContext
* dbg
) {
135 PERF_TIMER_GUARD(env_rename_file_nanos
);
136 return FileSystemWrapper::RenameFile(src
, dst
, options
, dbg
);
139 IOStatus
TimedFileSystem::LinkFile(const std::string
& src
,
140 const std::string
& dst
,
141 const IOOptions
& options
,
142 IODebugContext
* dbg
) {
143 PERF_TIMER_GUARD(env_link_file_nanos
);
144 return FileSystemWrapper::LinkFile(src
, dst
, options
, dbg
);
147 IOStatus
TimedFileSystem::LockFile(const std::string
& fname
,
148 const IOOptions
& options
, FileLock
** lock
,
149 IODebugContext
* dbg
) {
150 PERF_TIMER_GUARD(env_lock_file_nanos
);
151 return FileSystemWrapper::LockFile(fname
, options
, lock
, dbg
);
154 IOStatus
TimedFileSystem::UnlockFile(FileLock
* lock
, const IOOptions
& options
,
155 IODebugContext
* dbg
) {
156 PERF_TIMER_GUARD(env_unlock_file_nanos
);
157 return FileSystemWrapper::UnlockFile(lock
, options
, dbg
);
160 IOStatus
TimedFileSystem::NewLogger(const std::string
& fname
,
161 const IOOptions
& options
,
162 std::shared_ptr
<Logger
>* result
,
163 IODebugContext
* dbg
) {
164 PERF_TIMER_GUARD(env_new_logger_nanos
);
165 return FileSystemWrapper::NewLogger(fname
, options
, result
, dbg
);
168 std::shared_ptr
<FileSystem
> NewTimedFileSystem(
169 const std::shared_ptr
<FileSystem
>& base
) {
170 return std::make_shared
<TimedFileSystem
>(base
);
173 // An environment that measures function call times for filesystem
174 // operations, reporting results to variables in PerfContext.
175 Env
* NewTimedEnv(Env
* base_env
) {
176 std::shared_ptr
<FileSystem
> timed_fs
=
177 NewTimedFileSystem(base_env
->GetFileSystem());
178 return new CompositeEnvWrapper(base_env
, timed_fs
);
181 #else // ROCKSDB_LITE
183 Env
* NewTimedEnv(Env
* /*base_env*/) { return nullptr; }
185 #endif // !ROCKSDB_LITE
187 } // namespace ROCKSDB_NAMESPACE