| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 | 9 |
| 10 #include "base/at_exit.h" | 10 #include "base/at_exit.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 NOTREACHED(); | 173 NOTREACHED(); |
| 174 return "kNumEntries"; | 174 return "kNumEntries"; |
| 175 } | 175 } |
| 176 NOTREACHED(); | 176 NOTREACHED(); |
| 177 return "Unknown"; | 177 return "Unknown"; |
| 178 } | 178 } |
| 179 | 179 |
| 180 class UMALogger { | 180 class UMALogger { |
| 181 public: | 181 public: |
| 182 virtual void RecordErrorAt(MethodID method) const = 0; | 182 virtual void RecordErrorAt(MethodID method) const = 0; |
| 183 virtual void RecordSpecificError(MethodID method, int saved_errno) const = 0; | 183 virtual void RecordOSError(MethodID method, int saved_errno) const = 0; |
| 184 virtual void RecordSpecificError(MethodID method, | 184 virtual void RecordOSError(MethodID method, base::PlatformFileError error) |
| 185 base::PlatformFileError error) const = 0; | 185 const = 0; |
| 186 }; | 186 }; |
| 187 | 187 |
| 188 } // namespace | 188 } // namespace |
| 189 | 189 |
| 190 namespace leveldb { | 190 namespace leveldb { |
| 191 | 191 |
| 192 namespace { | 192 namespace { |
| 193 | 193 |
| 194 class Thread; | 194 class Thread; |
| 195 | 195 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 if (file_ != NULL) { | 315 if (file_ != NULL) { |
| 316 // Ignoring any potential errors | 316 // Ignoring any potential errors |
| 317 fclose(file_); | 317 fclose(file_); |
| 318 } | 318 } |
| 319 } | 319 } |
| 320 | 320 |
| 321 virtual Status Append(const Slice& data) { | 321 virtual Status Append(const Slice& data) { |
| 322 size_t r = fwrite_unlocked(data.data(), 1, data.size(), file_); | 322 size_t r = fwrite_unlocked(data.data(), 1, data.size(), file_); |
| 323 Status result; | 323 Status result; |
| 324 if (r != data.size()) { | 324 if (r != data.size()) { |
| 325 uma_logger_->RecordSpecificError(kWritableFileAppend, errno); | 325 uma_logger_->RecordOSError(kWritableFileAppend, errno); |
| 326 result = Status::IOError(filename_, strerror(errno)); | 326 result = Status::IOError(filename_, strerror(errno)); |
| 327 } | 327 } |
| 328 return result; | 328 return result; |
| 329 } | 329 } |
| 330 | 330 |
| 331 virtual Status Close() { | 331 virtual Status Close() { |
| 332 Status result; | 332 Status result; |
| 333 if (fclose(file_) != 0) { | 333 if (fclose(file_) != 0) { |
| 334 result = Status::IOError(filename_, strerror(errno)); | 334 result = Status::IOError(filename_, strerror(errno)); |
| 335 uma_logger_->RecordErrorAt(kWritableFileClose); | 335 uma_logger_->RecordErrorAt(kWritableFileClose); |
| 336 } | 336 } |
| 337 file_ = NULL; | 337 file_ = NULL; |
| 338 return result; | 338 return result; |
| 339 } | 339 } |
| 340 | 340 |
| 341 virtual Status Flush() { | 341 virtual Status Flush() { |
| 342 Status result; | 342 Status result; |
| 343 if (HANDLE_EINTR(fflush_unlocked(file_))) { | 343 if (HANDLE_EINTR(fflush_unlocked(file_))) { |
| 344 int saved_errno = errno; | 344 int saved_errno = errno; |
| 345 result = Status::IOError(filename_, strerror(saved_errno)); | 345 result = Status::IOError(filename_, strerror(saved_errno)); |
| 346 uma_logger_->RecordSpecificError(kWritableFileFlush, saved_errno); | 346 uma_logger_->RecordOSError(kWritableFileFlush, saved_errno); |
| 347 } | 347 } |
| 348 return result; | 348 return result; |
| 349 } | 349 } |
| 350 | 350 |
| 351 virtual Status Sync() { | 351 virtual Status Sync() { |
| 352 Status result; | 352 Status result; |
| 353 int error = 0; | 353 int error = 0; |
| 354 | 354 |
| 355 if (HANDLE_EINTR(fflush_unlocked(file_))) | 355 if (HANDLE_EINTR(fflush_unlocked(file_))) |
| 356 error = errno; | 356 error = errno; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 378 virtual ~ChromiumEnv() { | 378 virtual ~ChromiumEnv() { |
| 379 NOTREACHED(); | 379 NOTREACHED(); |
| 380 } | 380 } |
| 381 | 381 |
| 382 virtual Status NewSequentialFile(const std::string& fname, | 382 virtual Status NewSequentialFile(const std::string& fname, |
| 383 SequentialFile** result) { | 383 SequentialFile** result) { |
| 384 FILE* f = fopen_internal(fname.c_str(), "rb"); | 384 FILE* f = fopen_internal(fname.c_str(), "rb"); |
| 385 if (f == NULL) { | 385 if (f == NULL) { |
| 386 *result = NULL; | 386 *result = NULL; |
| 387 int saved_errno = errno; | 387 int saved_errno = errno; |
| 388 RecordSpecificError(kNewSequentialFile, saved_errno); | 388 RecordOSError(kNewSequentialFile, saved_errno); |
| 389 return Status::IOError(fname, strerror(saved_errno)); | 389 return Status::IOError(fname, strerror(saved_errno)); |
| 390 } else { | 390 } else { |
| 391 *result = new ChromiumSequentialFile(fname, f, this); | 391 *result = new ChromiumSequentialFile(fname, f, this); |
| 392 return Status::OK(); | 392 return Status::OK(); |
| 393 } | 393 } |
| 394 } | 394 } |
| 395 | 395 |
| 396 virtual Status NewRandomAccessFile(const std::string& fname, | 396 virtual Status NewRandomAccessFile(const std::string& fname, |
| 397 RandomAccessFile** result) { | 397 RandomAccessFile** result) { |
| 398 int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN; | 398 int flags = ::base::PLATFORM_FILE_READ | ::base::PLATFORM_FILE_OPEN; |
| 399 bool created; | 399 bool created; |
| 400 ::base::PlatformFileError error_code; | 400 ::base::PlatformFileError error_code; |
| 401 ::base::PlatformFile file = ::base::CreatePlatformFile( | 401 ::base::PlatformFile file = ::base::CreatePlatformFile( |
| 402 CreateFilePath(fname), flags, &created, &error_code); | 402 CreateFilePath(fname), flags, &created, &error_code); |
| 403 if (error_code != ::base::PLATFORM_FILE_OK) { | 403 if (error_code != ::base::PLATFORM_FILE_OK) { |
| 404 *result = NULL; | 404 *result = NULL; |
| 405 RecordSpecificError(kNewRandomAccessFile, error_code); | 405 RecordOSError(kNewRandomAccessFile, error_code); |
| 406 return Status::IOError(fname, PlatformFileErrorString(error_code)); | 406 return Status::IOError(fname, PlatformFileErrorString(error_code)); |
| 407 } | 407 } |
| 408 *result = new ChromiumRandomAccessFile(fname, file, this); | 408 *result = new ChromiumRandomAccessFile(fname, file, this); |
| 409 return Status::OK(); | 409 return Status::OK(); |
| 410 } | 410 } |
| 411 | 411 |
| 412 virtual Status NewWritableFile(const std::string& fname, | 412 virtual Status NewWritableFile(const std::string& fname, |
| 413 WritableFile** result) { | 413 WritableFile** result) { |
| 414 *result = NULL; | 414 *result = NULL; |
| 415 FILE* f = fopen_internal(fname.c_str(), "wb"); | 415 FILE* f = fopen_internal(fname.c_str(), "wb"); |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 ::base::PlatformFileError error_code; | 554 ::base::PlatformFileError error_code; |
| 555 ::base::PlatformFile file; | 555 ::base::PlatformFile file; |
| 556 Retrier r(lockfile_time_histogram_, kMaxRenameTimeMillis); | 556 Retrier r(lockfile_time_histogram_, kMaxRenameTimeMillis); |
| 557 do { | 557 do { |
| 558 file = ::base::CreatePlatformFile( | 558 file = ::base::CreatePlatformFile( |
| 559 CreateFilePath(fname), flags, &created, &error_code); | 559 CreateFilePath(fname), flags, &created, &error_code); |
| 560 } while (error_code != ::base::PLATFORM_FILE_OK && r.ShouldKeepTrying()); | 560 } while (error_code != ::base::PLATFORM_FILE_OK && r.ShouldKeepTrying()); |
| 561 | 561 |
| 562 if (error_code != ::base::PLATFORM_FILE_OK) { | 562 if (error_code != ::base::PLATFORM_FILE_OK) { |
| 563 result = Status::IOError(fname, PlatformFileErrorString(error_code)); | 563 result = Status::IOError(fname, PlatformFileErrorString(error_code)); |
| 564 RecordSpecificError(kLockFile, error_code); | 564 RecordOSError(kLockFile, error_code); |
| 565 } else { | 565 } else { |
| 566 ChromiumFileLock* my_lock = new ChromiumFileLock; | 566 ChromiumFileLock* my_lock = new ChromiumFileLock; |
| 567 my_lock->file_ = file; | 567 my_lock->file_ = file; |
| 568 *lock = my_lock; | 568 *lock = my_lock; |
| 569 } | 569 } |
| 570 return result; | 570 return result; |
| 571 } | 571 } |
| 572 | 572 |
| 573 virtual Status UnlockFile(FileLock* lock) { | 573 virtual Status UnlockFile(FileLock* lock) { |
| 574 ChromiumFileLock* my_lock = reinterpret_cast<ChromiumFileLock*>(lock); | 574 ChromiumFileLock* my_lock = reinterpret_cast<ChromiumFileLock*>(lock); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 *path = FilePathToString(test_directory_); | 611 *path = FilePathToString(test_directory_); |
| 612 mu_.Release(); | 612 mu_.Release(); |
| 613 return Status::OK(); | 613 return Status::OK(); |
| 614 } | 614 } |
| 615 | 615 |
| 616 virtual Status NewLogger(const std::string& fname, Logger** result) { | 616 virtual Status NewLogger(const std::string& fname, Logger** result) { |
| 617 FILE* f = fopen_internal(fname.c_str(), "w"); | 617 FILE* f = fopen_internal(fname.c_str(), "w"); |
| 618 if (f == NULL) { | 618 if (f == NULL) { |
| 619 *result = NULL; | 619 *result = NULL; |
| 620 int saved_errno = errno; | 620 int saved_errno = errno; |
| 621 RecordSpecificError(kNewLogger, saved_errno); | 621 RecordOSError(kNewLogger, saved_errno); |
| 622 return Status::IOError(fname, strerror(saved_errno)); | 622 return Status::IOError(fname, strerror(saved_errno)); |
| 623 } else { | 623 } else { |
| 624 if (!sync_parent(fname)) { | 624 if (!sync_parent(fname)) { |
| 625 fclose(f); | 625 fclose(f); |
| 626 return Status::IOError(fname, strerror(errno)); | 626 return Status::IOError(fname, strerror(errno)); |
| 627 } | 627 } |
| 628 *result = new ChromiumLogger(f); | 628 *result = new ChromiumLogger(f); |
| 629 return Status::OK(); | 629 return Status::OK(); |
| 630 } | 630 } |
| 631 } | 631 } |
| 632 | 632 |
| 633 virtual uint64_t NowMicros() { | 633 virtual uint64_t NowMicros() { |
| 634 return ::base::TimeTicks::Now().ToInternalValue(); | 634 return ::base::TimeTicks::Now().ToInternalValue(); |
| 635 } | 635 } |
| 636 | 636 |
| 637 virtual void SleepForMicroseconds(int micros) { | 637 virtual void SleepForMicroseconds(int micros) { |
| 638 // Round up to the next millisecond. | 638 // Round up to the next millisecond. |
| 639 ::base::PlatformThread::Sleep(::base::TimeDelta::FromMicroseconds(micros)); | 639 ::base::PlatformThread::Sleep(::base::TimeDelta::FromMicroseconds(micros)); |
| 640 } | 640 } |
| 641 | 641 |
| 642 void RecordErrorAt(MethodID method) const { | 642 void RecordErrorAt(MethodID method) const { |
| 643 io_error_histogram_->Add(method); | 643 io_error_histogram_->Add(method); |
| 644 } | 644 } |
| 645 | 645 |
| 646 void RecordSpecificError(MethodID method, base::PlatformFileError error) | 646 void RecordOSError(MethodID method, base::PlatformFileError error) const { |
| 647 const { | |
| 648 DCHECK(error < 0); | 647 DCHECK(error < 0); |
| 649 RecordSpecificError(method, -error); | 648 RecordOSError(method, -error); |
| 650 } | 649 } |
| 651 | 650 |
| 652 void RecordSpecificError(MethodID method, int error) const { | 651 void RecordOSError(MethodID method, int error) const { |
| 653 RecordErrorAt(method); | 652 RecordErrorAt(method); |
| 654 if (error_histograms_.find(method) == error_histograms_.end()) { | 653 if (error_histograms_.find(method) == error_histograms_.end()) { |
| 655 NOTREACHED(); | 654 NOTREACHED(); |
| 656 return; | 655 return; |
| 657 } | 656 } |
| 658 error_histograms_.find(method)->second->Add(error); | 657 error_histograms_.find(method)->second->Add(error); |
| 659 } | 658 } |
| 660 | 659 |
| 661 void RecordTimeToRename(base::TimeDelta t) const { | 660 void RecordTimeToRename(base::TimeDelta t) const { |
| 662 rename_time_histogram_->AddTime(t); | 661 rename_time_histogram_->AddTime(t); |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 | 847 |
| 849 Env* IDBEnv() { | 848 Env* IDBEnv() { |
| 850 return idb_env.Pointer(); | 849 return idb_env.Pointer(); |
| 851 } | 850 } |
| 852 | 851 |
| 853 Env* Env::Default() { | 852 Env* Env::Default() { |
| 854 return default_env.Pointer(); | 853 return default_env.Pointer(); |
| 855 } | 854 } |
| 856 | 855 |
| 857 } | 856 } |
| OLD | NEW |