OLD | NEW |
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | 1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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. | 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. |
4 | 4 |
5 #include "third_party/leveldatabase/env_chromium.h" | 5 #include "third_party/leveldatabase/env_chromium.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
10 #include <dirent.h> | 10 #include <dirent.h> |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 result->push_back(FilePath::FromUTF8Unsafe(dent->d_name)); | 95 result->push_back(FilePath::FromUTF8Unsafe(dent->d_name)); |
96 } | 96 } |
97 int saved_errno = errno; | 97 int saved_errno = errno; |
98 closedir(dir); | 98 closedir(dir); |
99 if (readdir_result != 0) | 99 if (readdir_result != 0) |
100 return base::File::OSErrorToFileError(saved_errno); | 100 return base::File::OSErrorToFileError(saved_errno); |
101 return base::File::FILE_OK; | 101 return base::File::FILE_OK; |
102 #endif | 102 #endif |
103 } | 103 } |
104 | 104 |
| 105 // To avoid a dependency on storage_histograms.h and the storageLib, |
| 106 // we re-implement the BytesCountHistogram functions here. |
| 107 base::HistogramBase* GetBytesCountHistogram(const std::string& name) { |
| 108 const int kMin = 1; |
| 109 const int kMax = 10000000; |
| 110 const int kNumBuckets = 50; |
| 111 return base::Histogram::FactoryGet( |
| 112 name, kMin, kMax, kNumBuckets, |
| 113 base::Histogram::kUmaTargetedHistogramFlag); |
| 114 } |
| 115 |
| 116 void RecordStorageBytesWritten(const char* label, int amount) { |
| 117 std::string name("Storage.BytesWritten."); |
| 118 name.append(label); |
| 119 GetBytesCountHistogram(name)->Add(amount); |
| 120 } |
| 121 |
| 122 void RecordStorageBytesRead(const char* label, int amount) { |
| 123 std::string name("Storage.BytesRead."); |
| 124 name.append(label); |
| 125 GetBytesCountHistogram(name)->Add(amount); |
| 126 } |
| 127 |
105 class ChromiumFileLock : public FileLock { | 128 class ChromiumFileLock : public FileLock { |
106 public: | 129 public: |
107 ChromiumFileLock(base::File file, const std::string& name) | 130 ChromiumFileLock(base::File file, const std::string& name) |
108 : file_(std::move(file)), name_(name) {} | 131 : file_(std::move(file)), name_(name) {} |
109 | 132 |
110 base::File file_; | 133 base::File file_; |
111 std::string name_; | 134 std::string name_; |
112 | 135 |
113 private: | 136 private: |
114 DISALLOW_COPY_AND_ASSIGN(ChromiumFileLock); | 137 DISALLOW_COPY_AND_ASSIGN(ChromiumFileLock); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 193 |
171 Status Read(size_t n, Slice* result, char* scratch) override { | 194 Status Read(size_t n, Slice* result, char* scratch) override { |
172 TRACE_EVENT1("leveldb", "ChromiumSequentialFile::Read", "size", n); | 195 TRACE_EVENT1("leveldb", "ChromiumSequentialFile::Read", "size", n); |
173 int bytes_read = file_.ReadAtCurrentPosNoBestEffort(scratch, n); | 196 int bytes_read = file_.ReadAtCurrentPosNoBestEffort(scratch, n); |
174 if (bytes_read == -1) { | 197 if (bytes_read == -1) { |
175 base::File::Error error = LastFileError(); | 198 base::File::Error error = LastFileError(); |
176 uma_logger_->RecordErrorAt(kSequentialFileRead); | 199 uma_logger_->RecordErrorAt(kSequentialFileRead); |
177 return MakeIOError(filename_, base::File::ErrorToString(error), | 200 return MakeIOError(filename_, base::File::ErrorToString(error), |
178 kSequentialFileRead, error); | 201 kSequentialFileRead, error); |
179 } else { | 202 } else { |
| 203 if (bytes_read > 0) |
| 204 uma_logger_->RecordBytesRead(bytes_read); |
180 *result = Slice(scratch, bytes_read); | 205 *result = Slice(scratch, bytes_read); |
181 return Status::OK(); | 206 return Status::OK(); |
182 } | 207 } |
183 } | 208 } |
184 | 209 |
185 Status Skip(uint64_t n) override { | 210 Status Skip(uint64_t n) override { |
186 if (file_.Seek(base::File::FROM_CURRENT, n) == -1) { | 211 if (file_.Seek(base::File::FROM_CURRENT, n) == -1) { |
187 base::File::Error error = LastFileError(); | 212 base::File::Error error = LastFileError(); |
188 uma_logger_->RecordErrorAt(kSequentialFileSkip); | 213 uma_logger_->RecordErrorAt(kSequentialFileSkip); |
189 return MakeIOError(filename_, base::File::ErrorToString(error), | 214 return MakeIOError(filename_, base::File::ErrorToString(error), |
(...skipping 18 matching lines...) Expand all Loading... |
208 const UMALogger* uma_logger) | 233 const UMALogger* uma_logger) |
209 : filename_(fname), file_(std::move(file)), uma_logger_(uma_logger) {} | 234 : filename_(fname), file_(std::move(file)), uma_logger_(uma_logger) {} |
210 virtual ~ChromiumRandomAccessFile() {} | 235 virtual ~ChromiumRandomAccessFile() {} |
211 | 236 |
212 Status Read(uint64_t offset, | 237 Status Read(uint64_t offset, |
213 size_t n, | 238 size_t n, |
214 Slice* result, | 239 Slice* result, |
215 char* scratch) const override { | 240 char* scratch) const override { |
216 TRACE_EVENT2("leveldb", "ChromiumRandomAccessFile::Read", "offset", offset, | 241 TRACE_EVENT2("leveldb", "ChromiumRandomAccessFile::Read", "offset", offset, |
217 "size", n); | 242 "size", n); |
218 Status s; | 243 int bytes_read = file_.Read(offset, scratch, n); |
219 int r = file_.Read(offset, scratch, n); | 244 *result = Slice(scratch, (bytes_read < 0) ? 0 : bytes_read); |
220 *result = Slice(scratch, (r < 0) ? 0 : r); | 245 if (bytes_read < 0) { |
221 if (r < 0) { | |
222 // An error: return a non-ok status | |
223 s = MakeIOError(filename_, "Could not perform read", | |
224 kRandomAccessFileRead); | |
225 uma_logger_->RecordErrorAt(kRandomAccessFileRead); | 246 uma_logger_->RecordErrorAt(kRandomAccessFileRead); |
| 247 return MakeIOError(filename_, "Could not perform read", |
| 248 kRandomAccessFileRead); |
226 } | 249 } |
227 return s; | 250 if (bytes_read > 0) |
| 251 uma_logger_->RecordBytesRead(bytes_read); |
| 252 return Status::OK(); |
228 } | 253 } |
229 | 254 |
230 private: | 255 private: |
231 std::string filename_; | 256 std::string filename_; |
232 mutable base::File file_; | 257 mutable base::File file_; |
233 const UMALogger* uma_logger_; | 258 const UMALogger* uma_logger_; |
234 | 259 |
235 DISALLOW_COPY_AND_ASSIGN(ChromiumRandomAccessFile); | 260 DISALLOW_COPY_AND_ASSIGN(ChromiumRandomAccessFile); |
236 }; | 261 }; |
237 | 262 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 Status ChromiumWritableFile::Append(const Slice& data) { | 321 Status ChromiumWritableFile::Append(const Slice& data) { |
297 DCHECK(file_.IsValid()); | 322 DCHECK(file_.IsValid()); |
298 DCHECK(uma_logger_); | 323 DCHECK(uma_logger_); |
299 int bytes_written = file_.WriteAtCurrentPos(data.data(), data.size()); | 324 int bytes_written = file_.WriteAtCurrentPos(data.data(), data.size()); |
300 if (bytes_written != data.size()) { | 325 if (bytes_written != data.size()) { |
301 base::File::Error error = LastFileError(); | 326 base::File::Error error = LastFileError(); |
302 uma_logger_->RecordOSError(kWritableFileAppend, error); | 327 uma_logger_->RecordOSError(kWritableFileAppend, error); |
303 return MakeIOError(filename_, base::File::ErrorToString(error), | 328 return MakeIOError(filename_, base::File::ErrorToString(error), |
304 kWritableFileAppend, error); | 329 kWritableFileAppend, error); |
305 } | 330 } |
306 | 331 if (bytes_written > 0) |
| 332 uma_logger_->RecordBytesWritten(bytes_written); |
307 return Status::OK(); | 333 return Status::OK(); |
308 } | 334 } |
309 | 335 |
310 Status ChromiumWritableFile::Close() { | 336 Status ChromiumWritableFile::Close() { |
311 file_.Close(); | 337 file_.Close(); |
312 return Status::OK(); | 338 return Status::OK(); |
313 } | 339 } |
314 | 340 |
315 Status ChromiumWritableFile::Flush() { | 341 Status ChromiumWritableFile::Flush() { |
316 // base::File doesn't do buffered I/O (i.e. POSIX FILE streams) so nothing to | 342 // base::File doesn't do buffered I/O (i.e. POSIX FILE streams) so nothing to |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 | 931 |
906 void ChromiumEnv::SleepForMicroseconds(int micros) { | 932 void ChromiumEnv::SleepForMicroseconds(int micros) { |
907 // Round up to the next millisecond. | 933 // Round up to the next millisecond. |
908 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(micros)); | 934 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(micros)); |
909 } | 935 } |
910 | 936 |
911 void ChromiumEnv::RecordErrorAt(MethodID method) const { | 937 void ChromiumEnv::RecordErrorAt(MethodID method) const { |
912 GetMethodIOErrorHistogram()->Add(method); | 938 GetMethodIOErrorHistogram()->Add(method); |
913 } | 939 } |
914 | 940 |
915 void ChromiumEnv::RecordLockFileAncestors(int num_missing_ancestors) const { | |
916 GetLockFileAncestorHistogram()->Add(num_missing_ancestors); | |
917 } | |
918 | |
919 void ChromiumEnv::RecordOSError(MethodID method, | 941 void ChromiumEnv::RecordOSError(MethodID method, |
920 base::File::Error error) const { | 942 base::File::Error error) const { |
921 DCHECK_LT(error, 0); | 943 DCHECK_LT(error, 0); |
922 RecordErrorAt(method); | 944 RecordErrorAt(method); |
923 GetOSErrorHistogram(method, -base::File::FILE_ERROR_MAX)->Add(-error); | 945 GetOSErrorHistogram(method, -base::File::FILE_ERROR_MAX)->Add(-error); |
924 } | 946 } |
925 | 947 |
| 948 void ChromiumEnv::RecordBytesRead(int amount) const { |
| 949 RecordStorageBytesRead(name_.c_str(), amount); |
| 950 } |
| 951 |
| 952 void ChromiumEnv::RecordBytesWritten(int amount) const { |
| 953 RecordStorageBytesWritten(name_.c_str(), amount); |
| 954 } |
| 955 |
| 956 void ChromiumEnv::RecordLockFileAncestors(int num_missing_ancestors) const { |
| 957 GetLockFileAncestorHistogram()->Add(num_missing_ancestors); |
| 958 } |
| 959 |
926 base::HistogramBase* ChromiumEnv::GetOSErrorHistogram(MethodID method, | 960 base::HistogramBase* ChromiumEnv::GetOSErrorHistogram(MethodID method, |
927 int limit) const { | 961 int limit) const { |
928 std::string uma_name; | 962 std::string uma_name; |
929 base::StringAppendF(&uma_name, "%s.%s", uma_ioerror_base_name_.c_str(), | 963 base::StringAppendF(&uma_name, "%s.%s", uma_ioerror_base_name_.c_str(), |
930 MethodIDToString(method)); | 964 MethodIDToString(method)); |
931 return base::LinearHistogram::FactoryGet(uma_name, 1, limit, limit + 1, | 965 return base::LinearHistogram::FactoryGet(uma_name, 1, limit, limit + 1, |
932 base::Histogram::kUmaTargetedHistogramFlag); | 966 base::Histogram::kUmaTargetedHistogramFlag); |
933 } | 967 } |
934 | 968 |
935 base::HistogramBase* ChromiumEnv::GetMethodIOErrorHistogram() const { | 969 base::HistogramBase* ChromiumEnv::GetMethodIOErrorHistogram() const { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1073 | 1107 |
1074 } // namespace leveldb_env | 1108 } // namespace leveldb_env |
1075 | 1109 |
1076 namespace leveldb { | 1110 namespace leveldb { |
1077 | 1111 |
1078 Env* Env::Default() { | 1112 Env* Env::Default() { |
1079 return leveldb_env::default_env.Pointer(); | 1113 return leveldb_env::default_env.Pointer(); |
1080 } | 1114 } |
1081 | 1115 |
1082 } // namespace leveldb | 1116 } // namespace leveldb |
OLD | NEW |