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_
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.
27 kRandomAccessFileRead
,
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
,
60 base::File::Error error
);
61 leveldb::Status
MakeIOError(leveldb::Slice filename
,
62 const std::string
& message
,
65 enum ErrorParsingResult
{
71 ErrorParsingResult
ParseMethodAndError(const leveldb::Status
& status
,
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
);
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
{
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
,
97 public RetrierProvider
{
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
);
137 std::string uma_ioerror_base_name_
;
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.
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;
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
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_
;
191 base::ConditionVariable bgsignal_
;
192 bool started_bgthread_
;
194 // Entry per Schedule() call
197 void (*function
)(void*);
199 typedef std::deque
<BGItem
> BGQueue
;
204 } // namespace leveldb_env
206 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_