env_timed.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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"
  6. #include "env/composite_env_wrapper.h"
  7. #include "monitoring/perf_context_imp.h"
  8. #include "rocksdb/env.h"
  9. #include "rocksdb/file_system.h"
  10. #include "rocksdb/status.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. TimedFileSystem::TimedFileSystem(const std::shared_ptr<FileSystem>& base)
  13. : FileSystemWrapper(base) {}
  14. IOStatus TimedFileSystem::NewSequentialFile(
  15. const std::string& fname, const FileOptions& options,
  16. std::unique_ptr<FSSequentialFile>* result, IODebugContext* dbg) {
  17. PERF_TIMER_GUARD(env_new_sequential_file_nanos);
  18. return FileSystemWrapper::NewSequentialFile(fname, options, result, dbg);
  19. }
  20. IOStatus TimedFileSystem::NewRandomAccessFile(
  21. const std::string& fname, const FileOptions& options,
  22. std::unique_ptr<FSRandomAccessFile>* result, IODebugContext* dbg) {
  23. PERF_TIMER_GUARD(env_new_random_access_file_nanos);
  24. return FileSystemWrapper::NewRandomAccessFile(fname, options, result, dbg);
  25. }
  26. IOStatus TimedFileSystem::NewWritableFile(
  27. const std::string& fname, const FileOptions& options,
  28. std::unique_ptr<FSWritableFile>* result, IODebugContext* dbg) {
  29. PERF_TIMER_GUARD(env_new_writable_file_nanos);
  30. return FileSystemWrapper::NewWritableFile(fname, options, result, dbg);
  31. }
  32. IOStatus TimedFileSystem::ReuseWritableFile(
  33. const std::string& fname, const std::string& old_fname,
  34. const FileOptions& options, std::unique_ptr<FSWritableFile>* result,
  35. IODebugContext* dbg) {
  36. PERF_TIMER_GUARD(env_reuse_writable_file_nanos);
  37. return FileSystemWrapper::ReuseWritableFile(fname, old_fname, options, result,
  38. dbg);
  39. }
  40. IOStatus TimedFileSystem::NewRandomRWFile(
  41. const std::string& fname, const FileOptions& options,
  42. std::unique_ptr<FSRandomRWFile>* result, IODebugContext* dbg) {
  43. PERF_TIMER_GUARD(env_new_random_rw_file_nanos);
  44. return FileSystemWrapper::NewRandomRWFile(fname, options, result, dbg);
  45. }
  46. IOStatus TimedFileSystem::NewDirectory(const std::string& name,
  47. const IOOptions& options,
  48. std::unique_ptr<FSDirectory>* result,
  49. IODebugContext* dbg) {
  50. PERF_TIMER_GUARD(env_new_directory_nanos);
  51. return FileSystemWrapper::NewDirectory(name, options, result, dbg);
  52. }
  53. IOStatus TimedFileSystem::FileExists(const std::string& fname,
  54. const IOOptions& options,
  55. IODebugContext* dbg) {
  56. PERF_TIMER_GUARD(env_file_exists_nanos);
  57. return FileSystemWrapper::FileExists(fname, options, dbg);
  58. }
  59. IOStatus TimedFileSystem::GetChildren(const std::string& dir,
  60. const IOOptions& options,
  61. std::vector<std::string>* result,
  62. IODebugContext* dbg) {
  63. PERF_TIMER_GUARD(env_get_children_nanos);
  64. return FileSystemWrapper::GetChildren(dir, options, result, dbg);
  65. }
  66. IOStatus TimedFileSystem::GetChildrenFileAttributes(
  67. const std::string& dir, const IOOptions& options,
  68. std::vector<FileAttributes>* result, IODebugContext* dbg) {
  69. PERF_TIMER_GUARD(env_get_children_file_attributes_nanos);
  70. return FileSystemWrapper::GetChildrenFileAttributes(dir, options, result,
  71. dbg);
  72. }
  73. IOStatus TimedFileSystem::DeleteFile(const std::string& fname,
  74. const IOOptions& options,
  75. IODebugContext* dbg) {
  76. PERF_TIMER_GUARD(env_delete_file_nanos);
  77. return FileSystemWrapper::DeleteFile(fname, options, dbg);
  78. }
  79. IOStatus TimedFileSystem::CreateDir(const std::string& dirname,
  80. const IOOptions& options,
  81. IODebugContext* dbg) {
  82. PERF_TIMER_GUARD(env_create_dir_nanos);
  83. return FileSystemWrapper::CreateDir(dirname, options, dbg);
  84. }
  85. IOStatus TimedFileSystem::CreateDirIfMissing(const std::string& dirname,
  86. const IOOptions& options,
  87. IODebugContext* dbg) {
  88. PERF_TIMER_GUARD(env_create_dir_if_missing_nanos);
  89. return FileSystemWrapper::CreateDirIfMissing(dirname, options, dbg);
  90. }
  91. IOStatus TimedFileSystem::DeleteDir(const std::string& dirname,
  92. const IOOptions& options,
  93. IODebugContext* dbg) {
  94. PERF_TIMER_GUARD(env_delete_dir_nanos);
  95. return FileSystemWrapper::DeleteDir(dirname, options, dbg);
  96. }
  97. IOStatus TimedFileSystem::GetFileSize(const std::string& fname,
  98. const IOOptions& options,
  99. uint64_t* file_size,
  100. IODebugContext* dbg) {
  101. PERF_TIMER_GUARD(env_get_file_size_nanos);
  102. return FileSystemWrapper::GetFileSize(fname, options, file_size, dbg);
  103. }
  104. IOStatus TimedFileSystem::GetFileModificationTime(const std::string& fname,
  105. const IOOptions& options,
  106. uint64_t* file_mtime,
  107. IODebugContext* dbg) {
  108. PERF_TIMER_GUARD(env_get_file_modification_time_nanos);
  109. return FileSystemWrapper::GetFileModificationTime(fname, options, file_mtime,
  110. dbg);
  111. }
  112. IOStatus TimedFileSystem::RenameFile(const std::string& src,
  113. const std::string& dst,
  114. const IOOptions& options,
  115. IODebugContext* dbg) {
  116. PERF_TIMER_GUARD(env_rename_file_nanos);
  117. return FileSystemWrapper::RenameFile(src, dst, options, dbg);
  118. }
  119. IOStatus TimedFileSystem::LinkFile(const std::string& src,
  120. const std::string& dst,
  121. const IOOptions& options,
  122. IODebugContext* dbg) {
  123. PERF_TIMER_GUARD(env_link_file_nanos);
  124. return FileSystemWrapper::LinkFile(src, dst, options, dbg);
  125. }
  126. IOStatus TimedFileSystem::LockFile(const std::string& fname,
  127. const IOOptions& options, FileLock** lock,
  128. IODebugContext* dbg) {
  129. PERF_TIMER_GUARD(env_lock_file_nanos);
  130. return FileSystemWrapper::LockFile(fname, options, lock, dbg);
  131. }
  132. IOStatus TimedFileSystem::UnlockFile(FileLock* lock, const IOOptions& options,
  133. IODebugContext* dbg) {
  134. PERF_TIMER_GUARD(env_unlock_file_nanos);
  135. return FileSystemWrapper::UnlockFile(lock, options, dbg);
  136. }
  137. IOStatus TimedFileSystem::NewLogger(const std::string& fname,
  138. const IOOptions& options,
  139. std::shared_ptr<Logger>* result,
  140. IODebugContext* dbg) {
  141. PERF_TIMER_GUARD(env_new_logger_nanos);
  142. return FileSystemWrapper::NewLogger(fname, options, result, dbg);
  143. }
  144. std::shared_ptr<FileSystem> NewTimedFileSystem(
  145. const std::shared_ptr<FileSystem>& base) {
  146. return std::make_shared<TimedFileSystem>(base);
  147. }
  148. // An environment that measures function call times for filesystem
  149. // operations, reporting results to variables in PerfContext.
  150. Env* NewTimedEnv(Env* base_env) {
  151. std::shared_ptr<FileSystem> timed_fs =
  152. NewTimedFileSystem(base_env->GetFileSystem());
  153. return new CompositeEnvWrapper(base_env, timed_fs);
  154. }
  155. } // namespace ROCKSDB_NAMESPACE