Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blob622cae5977cb2a9a53826cea1bbcf9ff53a2a270
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
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
5 #ifndef THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
6 #define THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_
8 #include <deque>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/files/file.h"
14 #include "base/files/file_path.h"
15 #include "base/metrics/histogram.h"
16 #include "leveldb/env.h"
17 #include "port/port_chromium.h"
18 #include "util/mutexlock.h"
20 namespace leveldb_env {
22 // These entries map to values in tools/metrics/histograms/histograms.xml. New
23 // values should be appended at the end.
24 enum MethodID {
25 kSequentialFileRead,
26 kSequentialFileSkip,
27 kRandomAccessFileRead,
28 kWritableFileAppend,
29 kWritableFileClose,
30 kWritableFileFlush,
31 kWritableFileSync,
32 kNewSequentialFile,
33 kNewRandomAccessFile,
34 kNewWritableFile,
35 kDeleteFile,
36 kCreateDir,
37 kDeleteDir,
38 kGetFileSize,
39 kRenameFile,
40 kLockFile,
41 kUnlockFile,
42 kGetTestDirectory,
43 kNewLogger,
44 kSyncParent,
45 kGetChildren,
46 kNewAppendableFile,
47 kNumEntries
50 // The default value for leveldb::Options::reuse_logs. Currently log reuse is an
51 // experimental feature in leveldb. More info at:
52 // https://github.com/google/leveldb/commit/251ebf5dc70129ad3
53 #if defined(OS_CHROMEOS)
54 // Reusing logs on Chrome OS resulted in an unacceptably high leveldb corruption
55 // rate (at least for Indexed DB). More info at https://crbug.com/460568
56 const bool kDefaultLogReuseOptionValue = false;
57 #else
58 const bool kDefaultLogReuseOptionValue = true;
59 #endif
61 const char* MethodIDToString(MethodID method);
63 leveldb::Status MakeIOError(leveldb::Slice filename,
64 const std::string& message,
65 MethodID method,
66 base::File::Error error);
67 leveldb::Status MakeIOError(leveldb::Slice filename,
68 const std::string& message,
69 MethodID method);
71 enum ErrorParsingResult {
72 METHOD_ONLY,
73 METHOD_AND_BFE,
74 NONE,
77 ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
78 MethodID* method,
79 base::File::Error* error);
80 int GetCorruptionCode(const leveldb::Status& status);
81 int GetNumCorruptionCodes();
82 std::string GetCorruptionMessage(const leveldb::Status& status);
83 bool IndicatesDiskFull(const leveldb::Status& status);
85 class UMALogger {
86 public:
87 virtual void RecordErrorAt(MethodID method) const = 0;
88 virtual void RecordOSError(MethodID method,
89 base::File::Error error) const = 0;
90 virtual void RecordBackupResult(bool success) const = 0;
93 class RetrierProvider {
94 public:
95 virtual int MaxRetryTimeMillis() const = 0;
96 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
97 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
98 MethodID method) const = 0;
101 class ChromiumEnv : public leveldb::Env,
102 public UMALogger,
103 public RetrierProvider {
104 public:
105 ChromiumEnv();
107 typedef void(ScheduleFunc)(void*);
109 static bool MakeBackup(const std::string& fname);
110 virtual ~ChromiumEnv();
112 virtual bool FileExists(const std::string& fname);
113 virtual leveldb::Status GetChildren(const std::string& dir,
114 std::vector<std::string>* result);
115 virtual leveldb::Status DeleteFile(const std::string& fname);
116 virtual leveldb::Status CreateDir(const std::string& name);
117 virtual leveldb::Status DeleteDir(const std::string& name);
118 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
119 virtual leveldb::Status RenameFile(const std::string& src,
120 const std::string& dst);
121 virtual leveldb::Status LockFile(const std::string& fname,
122 leveldb::FileLock** lock);
123 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
124 virtual void Schedule(ScheduleFunc*, void* arg);
125 virtual void StartThread(void (*function)(void* arg), void* arg);
126 virtual leveldb::Status GetTestDirectory(std::string* path);
127 virtual uint64_t NowMicros();
128 virtual void SleepForMicroseconds(int micros);
129 virtual leveldb::Status NewSequentialFile(const std::string& fname,
130 leveldb::SequentialFile** result);
131 virtual leveldb::Status NewRandomAccessFile(
132 const std::string& fname,
133 leveldb::RandomAccessFile** result);
134 virtual leveldb::Status NewWritableFile(const std::string& fname,
135 leveldb::WritableFile** result);
136 virtual leveldb::Status NewAppendableFile(const std::string& fname,
137 leveldb::WritableFile** result);
138 virtual leveldb::Status NewLogger(const std::string& fname,
139 leveldb::Logger** result);
141 protected:
142 std::string name_;
143 std::string uma_ioerror_base_name_;
144 bool make_backup_;
146 private:
147 static const char* FileErrorString(base::File::Error error);
149 virtual void RecordErrorAt(MethodID method) const;
150 virtual void RecordOSError(MethodID method,
151 base::File::Error error) const;
152 void RecordOpenFilesLimit(const std::string& type);
153 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
154 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
156 // File locks may not be exclusive within a process (e.g. on POSIX). Track
157 // locks held by the ChromiumEnv to prevent access within the process.
158 class LockTable {
159 public:
160 bool Insert(const std::string& fname) {
161 leveldb::MutexLock l(&mu_);
162 return locked_files_.insert(fname).second;
164 bool Remove(const std::string& fname) {
165 leveldb::MutexLock l(&mu_);
166 return locked_files_.erase(fname) == 1;
168 private:
169 leveldb::port::Mutex mu_;
170 std::set<std::string> locked_files_;
173 const int kMaxRetryTimeMillis;
174 // BGThread() is the body of the background thread
175 void BGThread();
176 static void BGThreadWrapper(void* arg) {
177 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
180 virtual void RecordBackupResult(bool result) const;
181 void RestoreIfNecessary(const std::string& dir,
182 std::vector<std::string>* children);
183 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
184 void RecordLockFileAncestors(int num_missing_ancestors) const;
185 base::HistogramBase* GetMethodIOErrorHistogram() const;
186 base::HistogramBase* GetLockFileAncestorHistogram() const;
188 // RetrierProvider implementation.
189 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
190 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
191 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
192 MethodID method) const;
194 base::FilePath test_directory_;
196 base::Lock mu_;
197 base::ConditionVariable bgsignal_;
198 bool started_bgthread_;
200 // Entry per Schedule() call
201 struct BGItem {
202 void* arg;
203 void (*function)(void*);
205 typedef std::deque<BGItem> BGQueue;
206 BGQueue queue_;
207 LockTable locks_;
210 } // namespace leveldb_env
212 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_