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 const char* MethodIDToString(MethodID method
);
52 leveldb::Status
MakeIOError(leveldb::Slice filename
,
53 const std::string
& message
,
55 base::File::Error error
);
56 leveldb::Status
MakeIOError(leveldb::Slice filename
,
57 const std::string
& message
,
60 enum ErrorParsingResult
{
67 ErrorParsingResult
ParseMethodAndError(const leveldb::Status
& status
,
70 int GetCorruptionCode(const leveldb::Status
& status
);
71 int GetNumCorruptionCodes();
72 std::string
GetCorruptionMessage(const leveldb::Status
& status
);
73 bool IndicatesDiskFull(const leveldb::Status
& status
);
77 virtual void RecordErrorAt(MethodID method
) const = 0;
78 virtual void RecordOSError(MethodID method
,
79 base::File::Error error
) const = 0;
80 virtual void RecordBackupResult(bool success
) const = 0;
83 class RetrierProvider
{
85 virtual int MaxRetryTimeMillis() const = 0;
86 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const = 0;
87 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
88 MethodID method
) const = 0;
93 virtual void DidCreateNewFile(const std::string
& fname
) = 0;
94 virtual bool DoesDirNeedSync(const std::string
& fname
) = 0;
95 virtual void DidSyncDir(const std::string
& fname
) = 0;
98 class ChromiumEnv
: public leveldb::Env
,
100 public RetrierProvider
,
101 public WriteTracker
{
105 typedef void(ScheduleFunc
)(void*);
107 static bool MakeBackup(const std::string
& fname
);
108 virtual ~ChromiumEnv();
110 virtual bool FileExists(const std::string
& fname
);
111 virtual leveldb::Status
GetChildren(const std::string
& dir
,
112 std::vector
<std::string
>* result
);
113 virtual leveldb::Status
DeleteFile(const std::string
& fname
);
114 virtual leveldb::Status
CreateDir(const std::string
& name
);
115 virtual leveldb::Status
DeleteDir(const std::string
& name
);
116 virtual leveldb::Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
117 virtual leveldb::Status
RenameFile(const std::string
& src
,
118 const std::string
& dst
);
119 virtual leveldb::Status
LockFile(const std::string
& fname
,
120 leveldb::FileLock
** lock
);
121 virtual leveldb::Status
UnlockFile(leveldb::FileLock
* lock
);
122 virtual void Schedule(ScheduleFunc
*, void* arg
);
123 virtual void StartThread(void (*function
)(void* arg
), void* arg
);
124 virtual leveldb::Status
GetTestDirectory(std::string
* path
);
125 virtual uint64_t NowMicros();
126 virtual void SleepForMicroseconds(int micros
);
127 virtual leveldb::Status
NewSequentialFile(const std::string
& fname
,
128 leveldb::SequentialFile
** result
);
129 virtual leveldb::Status
NewRandomAccessFile(
130 const std::string
& fname
,
131 leveldb::RandomAccessFile
** result
);
132 virtual leveldb::Status
NewWritableFile(const std::string
& fname
,
133 leveldb::WritableFile
** result
);
134 virtual leveldb::Status
NewAppendableFile(const std::string
& fname
,
135 leveldb::WritableFile
** result
);
136 virtual leveldb::Status
NewLogger(const std::string
& fname
,
137 leveldb::Logger
** result
);
140 virtual void DidSyncDir(const std::string
& fname
);
146 static const char* FileErrorString(base::File::Error error
);
148 virtual void DidCreateNewFile(const std::string
& fname
);
149 virtual bool DoesDirNeedSync(const std::string
& fname
);
150 virtual void RecordErrorAt(MethodID method
) const;
151 virtual void RecordOSError(MethodID method
,
152 base::File::Error error
) const;
153 void RecordOpenFilesLimit(const std::string
& type
);
154 base::HistogramBase
* GetMaxFDHistogram(const std::string
& type
) const;
155 base::HistogramBase
* GetOSErrorHistogram(MethodID method
, int limit
) const;
157 // File locks may not be exclusive within a process (e.g. on POSIX). Track
158 // locks held by the ChromiumEnv to prevent access within the process.
161 bool Insert(const std::string
& fname
) {
162 leveldb::MutexLock
l(&mu_
);
163 return locked_files_
.insert(fname
).second
;
165 bool Remove(const std::string
& fname
) {
166 leveldb::MutexLock
l(&mu_
);
167 return locked_files_
.erase(fname
) == 1;
170 leveldb::port::Mutex mu_
;
171 std::set
<std::string
> locked_files_
;
174 std::set
<std::string
> directories_needing_sync_
;
175 base::Lock directory_sync_lock_
;
177 const int kMaxRetryTimeMillis
;
178 // BGThread() is the body of the background thread
180 static void BGThreadWrapper(void* arg
) {
181 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
184 virtual void RecordBackupResult(bool result
) const;
185 void RestoreIfNecessary(const std::string
& dir
,
186 std::vector
<std::string
>* children
);
187 base::FilePath
RestoreFromBackup(const base::FilePath
& base_name
);
188 void RecordLockFileAncestors(int num_missing_ancestors
) const;
189 base::HistogramBase
* GetMethodIOErrorHistogram() const;
190 base::HistogramBase
* GetLockFileAncestorHistogram() const;
192 // RetrierProvider implementation.
193 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
194 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
195 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
196 MethodID method
) const;
198 base::FilePath test_directory_
;
201 base::ConditionVariable bgsignal_
;
202 bool started_bgthread_
;
204 // Entry per Schedule() call
207 void (*function
)(void*);
209 typedef std::deque
<BGItem
> BGQueue
;
214 } // namespace leveldb_env
216 #endif // THIRD_PARTY_LEVELDATABASE_ENV_CHROMIUM_H_