Revert of ui: Clean up damaged rects and clear them after painting. (patchset #2...
[chromium-blink-merge.git] / third_party / leveldatabase / env_chromium.h
blob5372ac8f9910c5c971dee901576fd2873c73d7cd
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 const bool kDefaultLogReuseOptionValue = true;
55 const char* MethodIDToString(MethodID method);
57 leveldb::Status MakeIOError(leveldb::Slice filename,
58 const std::string& message,
59 MethodID method,
60 base::File::Error error);
61 leveldb::Status MakeIOError(leveldb::Slice filename,
62 const std::string& message,
63 MethodID method);
65 enum ErrorParsingResult {
66 METHOD_ONLY,
67 METHOD_AND_BFE,
68 NONE,
71 ErrorParsingResult ParseMethodAndError(const leveldb::Status& status,
72 MethodID* method,
73 base::File::Error* error);
74 int GetCorruptionCode(const leveldb::Status& status);
75 int GetNumCorruptionCodes();
76 std::string GetCorruptionMessage(const leveldb::Status& status);
77 bool IndicatesDiskFull(const leveldb::Status& status);
79 class UMALogger {
80 public:
81 virtual void RecordErrorAt(MethodID method) const = 0;
82 virtual void RecordOSError(MethodID method,
83 base::File::Error error) const = 0;
84 virtual void RecordBackupResult(bool success) const = 0;
87 class RetrierProvider {
88 public:
89 virtual int MaxRetryTimeMillis() const = 0;
90 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const = 0;
91 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
92 MethodID method) const = 0;
95 class ChromiumEnv : public leveldb::Env,
96 public UMALogger,
97 public RetrierProvider {
98 public:
99 ChromiumEnv();
101 typedef void(ScheduleFunc)(void*);
103 static bool MakeBackup(const std::string& fname);
104 virtual ~ChromiumEnv();
106 virtual bool FileExists(const std::string& fname);
107 virtual leveldb::Status GetChildren(const std::string& dir,
108 std::vector<std::string>* result);
109 virtual leveldb::Status DeleteFile(const std::string& fname);
110 virtual leveldb::Status CreateDir(const std::string& name);
111 virtual leveldb::Status DeleteDir(const std::string& name);
112 virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size);
113 virtual leveldb::Status RenameFile(const std::string& src,
114 const std::string& dst);
115 virtual leveldb::Status LockFile(const std::string& fname,
116 leveldb::FileLock** lock);
117 virtual leveldb::Status UnlockFile(leveldb::FileLock* lock);
118 virtual void Schedule(ScheduleFunc*, void* arg);
119 virtual void StartThread(void (*function)(void* arg), void* arg);
120 virtual leveldb::Status GetTestDirectory(std::string* path);
121 virtual uint64_t NowMicros();
122 virtual void SleepForMicroseconds(int micros);
123 virtual leveldb::Status NewSequentialFile(const std::string& fname,
124 leveldb::SequentialFile** result);
125 virtual leveldb::Status NewRandomAccessFile(
126 const std::string& fname,
127 leveldb::RandomAccessFile** result);
128 virtual leveldb::Status NewWritableFile(const std::string& fname,
129 leveldb::WritableFile** result);
130 virtual leveldb::Status NewAppendableFile(const std::string& fname,
131 leveldb::WritableFile** result);
132 virtual leveldb::Status NewLogger(const std::string& fname,
133 leveldb::Logger** result);
135 protected:
136 std::string name_;
137 std::string uma_ioerror_base_name_;
138 bool make_backup_;
140 private:
141 static const char* FileErrorString(base::File::Error error);
143 virtual void RecordErrorAt(MethodID method) const;
144 virtual void RecordOSError(MethodID method,
145 base::File::Error error) const;
146 void RecordOpenFilesLimit(const std::string& type);
147 base::HistogramBase* GetMaxFDHistogram(const std::string& type) const;
148 base::HistogramBase* GetOSErrorHistogram(MethodID method, int limit) const;
150 // File locks may not be exclusive within a process (e.g. on POSIX). Track
151 // locks held by the ChromiumEnv to prevent access within the process.
152 class LockTable {
153 public:
154 bool Insert(const std::string& fname) {
155 leveldb::MutexLock l(&mu_);
156 return locked_files_.insert(fname).second;
158 bool Remove(const std::string& fname) {
159 leveldb::MutexLock l(&mu_);
160 return locked_files_.erase(fname) == 1;
162 private:
163 leveldb::port::Mutex mu_;
164 std::set<std::string> locked_files_;
167 const int kMaxRetryTimeMillis;
168 // BGThread() is the body of the background thread
169 void BGThread();
170 static void BGThreadWrapper(void* arg) {
171 reinterpret_cast<ChromiumEnv*>(arg)->BGThread();
174 virtual void RecordBackupResult(bool result) const;
175 void RestoreIfNecessary(const std::string& dir,
176 std::vector<std::string>* children);
177 base::FilePath RestoreFromBackup(const base::FilePath& base_name);
178 void RecordLockFileAncestors(int num_missing_ancestors) const;
179 base::HistogramBase* GetMethodIOErrorHistogram() const;
180 base::HistogramBase* GetLockFileAncestorHistogram() const;
182 // RetrierProvider implementation.
183 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis; }
184 virtual base::HistogramBase* GetRetryTimeHistogram(MethodID method) const;
185 virtual base::HistogramBase* GetRecoveredFromErrorHistogram(
186 MethodID method) const;
188 base::FilePath test_directory_;
190 base::Lock mu_;
191 base::ConditionVariable bgsignal_;
192 bool started_bgthread_;
194 // Entry per Schedule() call
195 struct BGItem {
196 void* arg;
197 void (*function)(void*);
199 typedef std::deque<BGItem> BGQueue;
200 BGQueue queue_;
201 LockTable locks_;
204 } // namespace leveldb_env
206 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_