| OLD | NEW |
| 1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved. | 1 // Copyright (c) 2013 The LevelDB Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. | 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 | 4 |
| 5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ | 5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ |
| 6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ | 6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 14 #include "base/synchronization/condition_variable.h" |
| 14 #include "leveldb/env.h" | 15 #include "leveldb/env.h" |
| 16 #include "leveldb/slice.h" |
| 17 #include "leveldb/status.h" |
| 15 #include "port/port_chromium.h" | 18 #include "port/port_chromium.h" |
| 16 #include "util/mutexlock.h" | 19 #include "util/mutexlock.h" |
| 17 | 20 |
| 18 namespace leveldb_env { | 21 namespace leveldb_env { |
| 19 | 22 |
| 20 enum MethodID { | 23 enum MethodID { |
| 21 kSequentialFileRead, | 24 kSequentialFileRead, |
| 22 kSequentialFileSkip, | 25 kSequentialFileSkip, |
| 23 kRandomAccessFileRead, | 26 kRandomAccessFileRead, |
| 24 kWritableFileAppend, | 27 kWritableFileAppend, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 MethodID method) const = 0; | 94 MethodID method) const = 0; |
| 92 }; | 95 }; |
| 93 | 96 |
| 94 class WriteTracker { | 97 class WriteTracker { |
| 95 public: | 98 public: |
| 96 virtual void DidCreateNewFile(const std::string& fname) = 0; | 99 virtual void DidCreateNewFile(const std::string& fname) = 0; |
| 97 virtual bool DoesDirNeedSync(const std::string& fname) = 0; | 100 virtual bool DoesDirNeedSync(const std::string& fname) = 0; |
| 98 virtual void DidSyncDir(const std::string& fname) = 0; | 101 virtual void DidSyncDir(const std::string& fname) = 0; |
| 99 }; | 102 }; |
| 100 | 103 |
| 104 class ChromiumWritableFile : public leveldb::WritableFile { |
| 105 public: |
| 106 ChromiumWritableFile(const std::string& fname, |
| 107 FILE* f, |
| 108 const UMALogger* uma_logger, |
| 109 WriteTracker* tracker, |
| 110 bool make_backup); |
| 111 virtual ~ChromiumWritableFile(); |
| 112 virtual leveldb::Status Append(const leveldb::Slice& data); |
| 113 virtual leveldb::Status Close(); |
| 114 virtual leveldb::Status Flush(); |
| 115 virtual leveldb::Status Sync(); |
| 116 |
| 117 private: |
| 118 enum Type { |
| 119 kManifest, |
| 120 kTable, |
| 121 kOther |
| 122 }; |
| 123 leveldb::Status SyncParent(); |
| 124 |
| 125 std::string filename_; |
| 126 FILE* file_; |
| 127 const UMALogger* uma_logger_; |
| 128 WriteTracker* tracker_; |
| 129 Type file_type_; |
| 130 std::string parent_dir_; |
| 131 bool make_backup_; |
| 132 }; |
| 133 |
| 101 class ChromiumEnv : public leveldb::Env, | 134 class ChromiumEnv : public leveldb::Env, |
| 102 public UMALogger, | 135 public UMALogger, |
| 103 public RetrierProvider, | 136 public RetrierProvider, |
| 104 public WriteTracker { | 137 public WriteTracker { |
| 105 public: | 138 public: |
| 106 static bool MakeBackup(const std::string& fname); | 139 ChromiumEnv(); |
| 107 static base::FilePath CreateFilePath(const std::string& file_path); | |
| 108 static const char* PlatformFileErrorString( | |
| 109 const ::base::PlatformFileError& error); | |
| 110 static bool HasTableExtension(const base::FilePath& path); | |
| 111 virtual ~ChromiumEnv(); | 140 virtual ~ChromiumEnv(); |
| 112 | 141 |
| 142 virtual leveldb::Status NewSequentialFile(const std::string& fname, |
| 143 leveldb::SequentialFile** result); |
| 144 virtual leveldb::Status NewRandomAccessFile( |
| 145 const std::string& fname, |
| 146 leveldb::RandomAccessFile** result); |
| 147 virtual leveldb::Status NewWritableFile(const std::string& fname, |
| 148 leveldb::WritableFile** result); |
| 113 virtual bool FileExists(const std::string& fname); | 149 virtual bool FileExists(const std::string& fname); |
| 114 virtual leveldb::Status GetChildren(const std::string& dir, | 150 virtual leveldb::Status GetChildren(const std::string& dir, |
| 115 std::vector<std::string>* result); | 151 std::vector<std::string>* result); |
| 116 virtual leveldb::Status DeleteFile(const std::string& fname); | 152 virtual leveldb::Status DeleteFile(const std::string& fname); |
| 117 virtual leveldb::Status CreateDir(const std::string& name); | 153 virtual leveldb::Status CreateDir(const std::string& name); |
| 118 virtual leveldb::Status DeleteDir(const std::string& name); | 154 virtual leveldb::Status DeleteDir(const std::string& name); |
| 119 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size); | 155 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size); |
| 120 virtual leveldb::Status RenameFile(const std::string& src, | 156 virtual leveldb::Status RenameFile(const std::string& src, |
| 121 const std::string& dst); | 157 const std::string& dst); |
| 122 virtual leveldb::Status LockFile(const std::string& fname, | 158 virtual leveldb::Status LockFile(const std::string& fname, |
| 123 leveldb::FileLock** lock); | 159 leveldb::FileLock** lock); |
| 124 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock); | 160 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock); |
| 125 virtual void Schedule(void (*function)(void*), void* arg); | 161 virtual void Schedule(void (*function)(void*), void* arg); |
| 126 virtual void StartThread(void (*function)(void* arg), void* arg); | 162 virtual void StartThread(void (*function)(void* arg), void* arg); |
| 127 virtual leveldb::Status GetTestDirectory(std::string* path); | 163 virtual leveldb::Status GetTestDirectory(std::string* path); |
| 164 virtual leveldb::Status NewLogger(const std::string& fname, |
| 165 leveldb::Logger** result); |
| 128 virtual uint64_t NowMicros(); | 166 virtual uint64_t NowMicros(); |
| 129 virtual void SleepForMicroseconds(int micros); | 167 virtual void SleepForMicroseconds(int micros); |
| 130 | 168 |
| 131 protected: | 169 protected: |
| 132 ChromiumEnv(); | |
| 133 | |
| 134 virtual void DidCreateNewFile(const std::string& fname); | 170 virtual void DidCreateNewFile(const std::string& fname); |
| 135 virtual bool DoesDirNeedSync(const std::string& fname); | 171 virtual bool DoesDirNeedSync(const std::string& fname); |
| 136 virtual void DidSyncDir(const std::string& fname); | 172 virtual void DidSyncDir(const std::string& fname); |
| 137 virtual base::PlatformFileError GetDirectoryEntries( | |
| 138 const base::FilePath& dir_param, | |
| 139 std::vector<base::FilePath>* result) const = 0; | |
| 140 virtual void RecordErrorAt(MethodID method) const; | |
| 141 virtual void RecordOSError(MethodID method, int saved_errno) const; | |
| 142 virtual void RecordOSError(MethodID method, | |
| 143 base::PlatformFileError error) const; | |
| 144 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; | |
| 145 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; | |
| 146 | 173 |
| 147 std::string name_; | 174 std::string name_; |
| 148 bool make_backup_; | 175 bool make_backup_; |
| 149 | 176 |
| 150 private: | 177 private: |
| 151 // File locks may not be exclusive within a process (e.g. on POSIX). Track | 178 // File locks may not be exclusive within a process (e.g. on POSIX). Track |
| 152 // locks held by the ChromiumEnv to prevent access within the process. | 179 // locks held by the ChromiumEnv to prevent access within the process. |
| 153 class LockTable { | 180 class LockTable { |
| 154 public: | 181 public: |
| 155 bool Insert(const std::string& fname) { | 182 bool Insert(const std::string& fname) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 168 std::map<std::string, bool> needs_sync_map_; | 195 std::map<std::string, bool> needs_sync_map_; |
| 169 base::Lock map_lock_; | 196 base::Lock map_lock_; |
| 170 | 197 |
| 171 const int kMaxRetryTimeMillis; | 198 const int kMaxRetryTimeMillis; |
| 172 // BGThread() is the body of the background thread | 199 // BGThread() is the body of the background thread |
| 173 void BGThread(); | 200 void BGThread(); |
| 174 static void BGThreadWrapper(void* arg) { | 201 static void BGThreadWrapper(void* arg) { |
| 175 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); | 202 reinterpret_cast<ChromiumEnv*>(arg)->BGThread(); |
| 176 } | 203 } |
| 177 | 204 |
| 205 virtual void RecordErrorAt(MethodID method) const; |
| 206 virtual void RecordOSError(MethodID method, int saved_errno) const; |
| 207 virtual void RecordOSError(MethodID method, |
| 208 base::PlatformFileError error) const; |
| 178 virtual void RecordBackupResult(bool result) const; | 209 virtual void RecordBackupResult(bool result) const; |
| 179 void RestoreIfNecessary(const std::string& dir, | 210 void RestoreIfNecessary(const std::string& dir, |
| 180 std::vector<std::string>* children); | 211 std::vector<std::string>* children); |
| 181 base::FilePath RestoreFromBackup(const base::FilePath& base_name); | 212 base::FilePath RestoreFromBackup(const base::FilePath& base_name); |
| 213 void RecordOpenFilesLimit(const std::string& type); |
| 182 void RecordLockFileAncestors(int num_missing_ancestors) const; | 214 void RecordLockFileAncestors(int num_missing_ancestors) const; |
| 215 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const; |
| 183 base::HistogramBase* GetMethodIOErrorHistogram() const; | 216 base::HistogramBase* GetMethodIOErrorHistogram() const; |
| 217 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const; |
| 184 base::HistogramBase* GetLockFileAncestorHistogram() const; | 218 base::HistogramBase* GetLockFileAncestorHistogram() const; |
| 185 | 219 |
| 186 // RetrierProvider implementation. | 220 // RetrierProvider implementation. |
| 187 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; } | 221 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; } |
| 188 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const; | 222 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const; |
| 189 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( | 223 virtual base::HistogramBase* GetRecoveredFromErrorHistogram( |
| 190 MethodID method) const; | 224 MethodID method) const; |
| 191 | 225 |
| 192 base::FilePath test_directory_; | 226 base::FilePath test_directory_; |
| 193 | 227 |
| 194 ::base::Lock mu_; | 228 ::base::Lock mu_; |
| 195 ::base::ConditionVariable bgsignal_; | 229 ::base::ConditionVariable bgsignal_; |
| 196 bool started_bgthread_; | 230 bool started_bgthread_; |
| 197 | 231 |
| 198 // Entry per Schedule() call | 232 // Entry per Schedule() call |
| 199 struct BGItem { | 233 struct BGItem { |
| 200 void* arg; | 234 void* arg; |
| 201 void (*function)(void*); | 235 void (*function)(void*); |
| 202 }; | 236 }; |
| 203 typedef std::deque<BGItem> BGQueue; | 237 typedef std::deque<BGItem> BGQueue; |
| 204 BGQueue queue_; | 238 BGQueue queue_; |
| 205 LockTable locks_; | 239 LockTable locks_; |
| 206 }; | 240 }; |
| 207 | 241 |
| 208 } // namespace leveldb_env | 242 } // namespace leveldb_env |
| 209 | 243 |
| 210 #endif | 244 #endif |
| OLD | NEW |