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_
11 #include "base/metrics/histogram.h"
12 #include "base/platform_file.h"
13 #include "base/synchronization/condition_variable.h"
14 #include "leveldb/env.h"
15 #include "leveldb/slice.h"
16 #include "leveldb/status.h"
18 namespace leveldb_env
{
23 kRandomAccessFileRead
,
44 const char* MethodIDToString(MethodID method
);
46 leveldb::Status
MakeIOError(leveldb::Slice filename
,
50 leveldb::Status
MakeIOError(leveldb::Slice filename
,
53 base::PlatformFileError error
);
54 leveldb::Status
MakeIOError(leveldb::Slice filename
,
58 enum ErrorParsingResult
{
65 ErrorParsingResult
ParseMethodAndError(const char* string
,
68 std::string
FilePathToString(const base::FilePath
& file_path
);
72 virtual void RecordErrorAt(MethodID method
) const = 0;
73 virtual void RecordOSError(MethodID method
, int saved_errno
) const = 0;
74 virtual void RecordOSError(MethodID method
,
75 base::PlatformFileError error
) const = 0;
78 class RetrierProvider
{
80 virtual int MaxRetryTimeMillis() const = 0;
81 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const = 0;
82 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
83 MethodID method
) const = 0;
88 virtual void DidCreateNewFile(const std::string
& fname
) = 0;
89 virtual bool DoesDirNeedSync(const std::string
& fname
) = 0;
90 virtual void DidSyncDir(const std::string
& fname
) = 0;
93 class ChromiumWritableFile
: public leveldb::WritableFile
{
95 ChromiumWritableFile(const std::string
& fname
,
97 const UMALogger
* uma_logger
,
98 WriteTracker
* tracker
);
99 virtual ~ChromiumWritableFile();
100 virtual leveldb::Status
Append(const leveldb::Slice
& data
);
101 virtual leveldb::Status
Close();
102 virtual leveldb::Status
Flush();
103 virtual leveldb::Status
Sync();
106 leveldb::Status
SyncParent();
108 std::string filename_
;
110 const UMALogger
* uma_logger_
;
111 WriteTracker
* tracker_
;
113 std::string parent_dir_
;
116 class ChromiumEnv
: public leveldb::Env
,
118 public RetrierProvider
,
119 public WriteTracker
{
122 virtual ~ChromiumEnv();
124 virtual leveldb::Status
NewSequentialFile(const std::string
& fname
,
125 leveldb::SequentialFile
** result
);
126 virtual leveldb::Status
NewRandomAccessFile(
127 const std::string
& fname
,
128 leveldb::RandomAccessFile
** result
);
129 virtual leveldb::Status
NewWritableFile(const std::string
& fname
,
130 leveldb::WritableFile
** result
);
131 virtual bool FileExists(const std::string
& fname
);
132 virtual leveldb::Status
GetChildren(const std::string
& dir
,
133 std::vector
<std::string
>* result
);
134 virtual leveldb::Status
DeleteFile(const std::string
& fname
);
135 virtual leveldb::Status
CreateDir(const std::string
& name
);
136 virtual leveldb::Status
DeleteDir(const std::string
& name
);
137 virtual leveldb::Status
GetFileSize(const std::string
& fname
, uint64_t* size
);
138 virtual leveldb::Status
RenameFile(const std::string
& src
,
139 const std::string
& dst
);
140 virtual leveldb::Status
LockFile(const std::string
& fname
,
141 leveldb::FileLock
** lock
);
142 virtual leveldb::Status
UnlockFile(leveldb::FileLock
* lock
);
143 virtual void Schedule(void (*function
)(void*), void* arg
);
144 virtual void StartThread(void (*function
)(void* arg
), void* arg
);
145 virtual leveldb::Status
GetTestDirectory(std::string
* path
);
146 virtual leveldb::Status
NewLogger(const std::string
& fname
,
147 leveldb::Logger
** result
);
148 virtual uint64_t NowMicros();
149 virtual void SleepForMicroseconds(int micros
);
152 virtual void DidCreateNewFile(const std::string
& fname
);
153 virtual bool DoesDirNeedSync(const std::string
& fname
);
154 virtual void DidSyncDir(const std::string
& fname
);
159 std::map
<std::string
, bool> needs_sync_map_
;
160 base::Lock map_lock_
;
162 const int kMaxRetryTimeMillis
;
163 // BGThread() is the body of the background thread
165 static void BGThreadWrapper(void* arg
) {
166 reinterpret_cast<ChromiumEnv
*>(arg
)->BGThread();
169 virtual void RecordErrorAt(MethodID method
) const;
170 virtual void RecordOSError(MethodID method
, int saved_errno
) const;
171 virtual void RecordOSError(MethodID method
,
172 base::PlatformFileError error
) const;
173 void RecordOpenFilesLimit(const std::string
& type
);
174 void RecordLockFileAncestors(int num_missing_ancestors
) const;
175 base::HistogramBase
* GetOSErrorHistogram(MethodID method
, int limit
) const;
176 base::HistogramBase
* GetMethodIOErrorHistogram() const;
177 base::HistogramBase
* GetMaxFDHistogram(const std::string
& type
) const;
178 base::HistogramBase
* GetLockFileAncestorHistogram() const;
180 // RetrierProvider implementation.
181 virtual int MaxRetryTimeMillis() const { return kMaxRetryTimeMillis
; }
182 virtual base::HistogramBase
* GetRetryTimeHistogram(MethodID method
) const;
183 virtual base::HistogramBase
* GetRecoveredFromErrorHistogram(
184 MethodID method
) const;
186 base::FilePath test_directory_
;
189 ::base::ConditionVariable bgsignal_
;
190 bool started_bgthread_
;
192 // Entry per Schedule() call
195 void (*function
)(void*);
197 typedef std::deque
<BGItem
> BGQueue
;
201 } // namespace leveldb_env