OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium 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. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/leveldb/env_mojo.h" | 5 #include "components/leveldb/env_mojo.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 | 77 |
78 Status Read(size_t n, Slice* result, char* scratch) override { | 78 Status Read(size_t n, Slice* result, char* scratch) override { |
79 int bytes_read = file_.ReadAtCurrentPosNoBestEffort( | 79 int bytes_read = file_.ReadAtCurrentPosNoBestEffort( |
80 scratch, | 80 scratch, |
81 static_cast<int>(n)); | 81 static_cast<int>(n)); |
82 if (bytes_read == -1) { | 82 if (bytes_read == -1) { |
83 base::File::Error error = LastFileError(); | 83 base::File::Error error = LastFileError(); |
84 uma_logger_->RecordOSError(leveldb_env::kSequentialFileRead, error); | 84 uma_logger_->RecordOSError(leveldb_env::kSequentialFileRead, error); |
85 return MakeIOError(filename_, base::File::ErrorToString(error), | 85 return MakeIOError(filename_, base::File::ErrorToString(error), |
86 leveldb_env::kSequentialFileRead, error); | 86 leveldb_env::kSequentialFileRead, error); |
87 } else { | |
88 *result = Slice(scratch, bytes_read); | |
89 return Status::OK(); | |
90 } | 87 } |
88 if (bytes_read > 0) | |
89 uma_logger_->RecordBytesRead(bytes_read); | |
90 *result = Slice(scratch, bytes_read); | |
91 return Status::OK(); | |
91 } | 92 } |
92 | 93 |
93 Status Skip(uint64_t n) override { | 94 Status Skip(uint64_t n) override { |
94 if (file_.Seek(base::File::FROM_CURRENT, n) == -1) { | 95 if (file_.Seek(base::File::FROM_CURRENT, n) == -1) { |
95 base::File::Error error = LastFileError(); | 96 base::File::Error error = LastFileError(); |
96 uma_logger_->RecordOSError(leveldb_env::kSequentialFileSkip, error); | 97 uma_logger_->RecordOSError(leveldb_env::kSequentialFileSkip, error); |
97 return MakeIOError(filename_, base::File::ErrorToString(error), | 98 return MakeIOError(filename_, base::File::ErrorToString(error), |
98 leveldb_env::kSequentialFileSkip, error); | 99 leveldb_env::kSequentialFileSkip, error); |
99 } else { | 100 } else { |
100 return Status::OK(); | 101 return Status::OK(); |
(...skipping 13 matching lines...) Expand all Loading... | |
114 MojoRandomAccessFile(const std::string& fname, | 115 MojoRandomAccessFile(const std::string& fname, |
115 base::File file, | 116 base::File file, |
116 const UMALogger* uma_logger) | 117 const UMALogger* uma_logger) |
117 : filename_(fname), file_(std::move(file)), uma_logger_(uma_logger) {} | 118 : filename_(fname), file_(std::move(file)), uma_logger_(uma_logger) {} |
118 ~MojoRandomAccessFile() override {} | 119 ~MojoRandomAccessFile() override {} |
119 | 120 |
120 Status Read(uint64_t offset, | 121 Status Read(uint64_t offset, |
121 size_t n, | 122 size_t n, |
122 Slice* result, | 123 Slice* result, |
123 char* scratch) const override { | 124 char* scratch) const override { |
124 Status s; | 125 int bytes_read = file_.Read(offset, scratch, static_cast<int>(n)); |
125 int r = file_.Read(offset, scratch, static_cast<int>(n)); | 126 *result = Slice(scratch, (bytes_read < 0) ? 0 : bytes_read); |
126 *result = Slice(scratch, (r < 0) ? 0 : r); | 127 if (bytes_read < 0) { |
127 if (r < 0) { | |
128 // An error: return a non-ok status | |
129 s = MakeIOError(filename_, "Could not perform read", | |
130 leveldb_env::kRandomAccessFileRead); | |
131 uma_logger_->RecordOSError(leveldb_env::kRandomAccessFileRead, | 128 uma_logger_->RecordOSError(leveldb_env::kRandomAccessFileRead, |
132 LastFileError()); | 129 LastFileError()); |
130 return MakeIOError(filename_, "Could not perform read", | |
131 leveldb_env::kRandomAccessFileRead); | |
133 } | 132 } |
134 return s; | 133 if (bytes_read > 0) |
134 uma_logger_->RecordBytesRead(bytes_read); | |
135 return Status::OK(); | |
135 } | 136 } |
136 | 137 |
137 private: | 138 private: |
138 std::string filename_; | 139 std::string filename_; |
139 mutable base::File file_; | 140 mutable base::File file_; |
140 const UMALogger* uma_logger_; | 141 const UMALogger* uma_logger_; |
141 | 142 |
142 DISALLOW_COPY_AND_ASSIGN(MojoRandomAccessFile); | 143 DISALLOW_COPY_AND_ASSIGN(MojoRandomAccessFile); |
143 }; | 144 }; |
144 | 145 |
(...skipping 17 matching lines...) Expand all Loading... | |
162 } else if (path.MatchesExtension(table_extension)) { | 163 } else if (path.MatchesExtension(table_extension)) { |
163 file_type_ = kTable; | 164 file_type_ = kTable; |
164 } | 165 } |
165 parent_dir_ = | 166 parent_dir_ = |
166 base::FilePath::FromUTF8Unsafe(fname).DirName().AsUTF8Unsafe(); | 167 base::FilePath::FromUTF8Unsafe(fname).DirName().AsUTF8Unsafe(); |
167 } | 168 } |
168 | 169 |
169 ~MojoWritableFile() override {} | 170 ~MojoWritableFile() override {} |
170 | 171 |
171 leveldb::Status Append(const leveldb::Slice& data) override { | 172 leveldb::Status Append(const leveldb::Slice& data) override { |
172 size_t bytes_written = file_.WriteAtCurrentPos( | 173 int bytes_written = |
173 data.data(), static_cast<int>(data.size())); | 174 file_.WriteAtCurrentPos(data.data(), static_cast<int>(data.size())); |
174 if (bytes_written != data.size()) { | 175 if (bytes_written != static_cast<int>(data.size())) { |
175 base::File::Error error = LastFileError(); | 176 base::File::Error error = LastFileError(); |
176 uma_logger_->RecordOSError(leveldb_env::kWritableFileAppend, error); | 177 uma_logger_->RecordOSError(leveldb_env::kWritableFileAppend, error); |
177 return MakeIOError(filename_, base::File::ErrorToString(error), | 178 return MakeIOError(filename_, base::File::ErrorToString(error), |
178 leveldb_env::kWritableFileAppend, error); | 179 leveldb_env::kWritableFileAppend, error); |
179 } | 180 } |
180 | 181 if (bytes_written > 0) |
182 uma_logger_->RecordBytesWritten(bytes_written); | |
181 return Status::OK(); | 183 return Status::OK(); |
182 } | 184 } |
183 | 185 |
184 leveldb::Status Close() override { | 186 leveldb::Status Close() override { |
185 file_.Close(); | 187 file_.Close(); |
186 return Status::OK(); | 188 return Status::OK(); |
187 } | 189 } |
188 | 190 |
189 leveldb::Status Flush() override { | 191 leveldb::Status Flush() override { |
190 // base::File doesn't do buffered I/O (i.e. POSIX FILE streams) so nothing | 192 // base::File doesn't do buffered I/O (i.e. POSIX FILE streams) so nothing |
191 // to | 193 // to flush. |
192 // flush. | |
193 return Status::OK(); | 194 return Status::OK(); |
194 } | 195 } |
195 | 196 |
196 leveldb::Status Sync() override { | 197 leveldb::Status Sync() override { |
197 TRACE_EVENT0("leveldb", "MojoWritableFile::Sync"); | 198 TRACE_EVENT0("leveldb", "MojoWritableFile::Sync"); |
198 | 199 |
199 if (!file_.Flush()) { | 200 if (!file_.Flush()) { |
200 base::File::Error error = LastFileError(); | 201 base::File::Error error = LastFileError(); |
201 uma_logger_->RecordOSError(leveldb_env::kWritableFileSync, error); | 202 uma_logger_->RecordOSError(leveldb_env::kWritableFileSync, error); |
202 return MakeIOError(filename_, base::File::ErrorToString(error), | 203 return MakeIOError(filename_, base::File::ErrorToString(error), |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
448 } | 449 } |
449 | 450 |
450 void MojoEnv::RecordOSError(leveldb_env::MethodID method, | 451 void MojoEnv::RecordOSError(leveldb_env::MethodID method, |
451 base::File::Error error) const { | 452 base::File::Error error) const { |
452 RecordErrorAt(method); | 453 RecordErrorAt(method); |
453 std::string uma_name = | 454 std::string uma_name = |
454 std::string("MojoLevelDBEnv.IOError.BFE.") + MethodIDToString(method); | 455 std::string("MojoLevelDBEnv.IOError.BFE.") + MethodIDToString(method); |
455 base::UmaHistogramExactLinear(uma_name, -error, -base::File::FILE_ERROR_MAX); | 456 base::UmaHistogramExactLinear(uma_name, -error, -base::File::FILE_ERROR_MAX); |
456 } | 457 } |
457 | 458 |
459 void MojoEnv::RecordBytesRead(int amount) const { | |
460 base::UmaHistogramCounts10M("Storage.BytesRead.MojoLevelDBEnv", amount); | |
Marijn Kruisselbrink
2017/05/19 21:28:28
nit: since these histogram names are (currently) a
michaeln
2017/05/19 21:37:54
Done.
| |
461 } | |
462 | |
463 void MojoEnv::RecordBytesWritten(int amount) const { | |
464 base::UmaHistogramCounts10M("Storage.BytesWritten.MojoLevelDBEnv", amount); | |
465 } | |
466 | |
458 void MojoEnv::RecordFileError(leveldb_env::MethodID method, | 467 void MojoEnv::RecordFileError(leveldb_env::MethodID method, |
459 FileError error) const { | 468 FileError error) const { |
460 RecordOSError(method, static_cast<base::File::Error>(error)); | 469 RecordOSError(method, static_cast<base::File::Error>(error)); |
461 } | 470 } |
462 | 471 |
463 uint64_t MojoEnv::NowMicros() { | 472 uint64_t MojoEnv::NowMicros() { |
464 return base::TimeTicks::Now().ToInternalValue(); | 473 return base::TimeTicks::Now().ToInternalValue(); |
465 } | 474 } |
466 | 475 |
467 void MojoEnv::SleepForMicroseconds(int micros) { | 476 void MojoEnv::SleepForMicroseconds(int micros) { |
468 // Round up to the next millisecond. | 477 // Round up to the next millisecond. |
469 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(micros)); | 478 base::PlatformThread::Sleep(base::TimeDelta::FromMicroseconds(micros)); |
470 } | 479 } |
471 | 480 |
472 void MojoEnv::Schedule(void (*function)(void* arg), void* arg) { | 481 void MojoEnv::Schedule(void (*function)(void* arg), void* arg) { |
473 base::PostTaskWithTraits(FROM_HERE, | 482 base::PostTaskWithTraits(FROM_HERE, |
474 {base::MayBlock(), base::WithBaseSyncPrimitives(), | 483 {base::MayBlock(), base::WithBaseSyncPrimitives(), |
475 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, | 484 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}, |
476 base::Bind(function, arg)); | 485 base::Bind(function, arg)); |
477 } | 486 } |
478 | 487 |
479 void MojoEnv::StartThread(void (*function)(void* arg), void* arg) { | 488 void MojoEnv::StartThread(void (*function)(void* arg), void* arg) { |
480 new Thread(function, arg); // Will self-delete. | 489 new Thread(function, arg); // Will self-delete. |
481 } | 490 } |
482 | 491 |
483 } // namespace leveldb | 492 } // namespace leveldb |
OLD | NEW |