 Chromium Code Reviews
 Chromium Code Reviews Issue 7583049:
  Record UMA statistics for file_stream operations.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 7583049:
  Record UMA statistics for file_stream operations.  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: net/base/file_stream_win.cc | 
| diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc | 
| index c08521e913a47c1ee3adcf9b77ea969365d28f54..b320672dbb0410da13851091b0381231f844d81e 100644 | 
| --- a/net/base/file_stream_win.cc | 
| +++ b/net/base/file_stream_win.cc | 
| @@ -11,6 +11,7 @@ | 
| #include "base/message_loop.h" | 
| #include "base/metrics/histogram.h" | 
| #include "base/threading/thread_restrictions.h" | 
| +#include "net/base/file_stream_metrics.h" | 
| #include "net/base/net_errors.h" | 
| namespace net { | 
| @@ -53,7 +54,8 @@ static int MapErrorCode(DWORD err) { | 
| class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 
| public: | 
| AsyncContext(FileStream* owner) | 
| - : owner_(owner), context_(), callback_(NULL), is_closing_(false) { | 
| + : owner_(owner), context_(), callback_(NULL), is_closing_(false), | 
| + record_uma_(false) { | 
| context_.handler = this; | 
| } | 
| ~AsyncContext(); | 
| @@ -63,6 +65,10 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 
| OVERLAPPED* overlapped() { return &context_.overlapped; } | 
| CompletionCallback* callback() const { return callback_; } | 
| + void EnableRecording() { | 
| + record_uma_ = true; | 
| + } | 
| + | 
| private: | 
| virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 
| DWORD bytes_read, DWORD error); | 
| @@ -71,6 +77,7 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 
| MessageLoopForIO::IOContext context_; | 
| CompletionCallback* callback_; | 
| bool is_closing_; | 
| + bool record_uma_; | 
| }; | 
| FileStream::AsyncContext::~AsyncContext() { | 
| @@ -105,8 +112,11 @@ void FileStream::AsyncContext::OnIOCompleted( | 
| } | 
| int result = static_cast<int>(bytes_read); | 
| - if (error && error != ERROR_HANDLE_EOF) | 
| + if (error && error != ERROR_HANDLE_EOF) { | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_READ); | 
| 
cbentzel
2011/08/16 13:36:02
This could be done for both reads and writes. You
 
ahendrickson
2011/08/17 20:12:04
Done.
 | 
| result = MapErrorCode(error); | 
| + } | 
| if (bytes_read) | 
| IncrementOffset(&context->overlapped, bytes_read); | 
| @@ -121,13 +131,15 @@ void FileStream::AsyncContext::OnIOCompleted( | 
| FileStream::FileStream() | 
| : file_(INVALID_HANDLE_VALUE), | 
| open_flags_(0), | 
| - auto_closed_(true) { | 
| + auto_closed_(true), | 
| + record_uma_(false) { | 
| } | 
| FileStream::FileStream(base::PlatformFile file, int flags) | 
| : file_(file), | 
| open_flags_(flags), | 
| - auto_closed_(false) { | 
| + auto_closed_(false), | 
| + record_uma_(false) { | 
| // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 
| // make sure we will perform asynchronous File IO to it. | 
| if (flags & base::PLATFORM_FILE_ASYNC) { | 
| @@ -164,11 +176,15 @@ int FileStream::Open(const FilePath& path, int open_flags) { | 
| if (file_ == INVALID_HANDLE_VALUE) { | 
| DWORD error = GetLastError(); | 
| LOG(WARNING) << "Failed to open file: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_OPEN); | 
| return MapErrorCode(error); | 
| } | 
| if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 
| async_context_.reset(new AsyncContext(this)); | 
| + if (record_uma_) | 
| + async_context_->EnableRecording(); | 
| MessageLoopForIO::current()->RegisterIOHandler(file_, | 
| async_context_.get()); | 
| } | 
| @@ -181,8 +197,12 @@ bool FileStream::IsOpen() const { | 
| } | 
| int64 FileStream::Seek(Whence whence, int64 offset) { | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| 
cbentzel
2011/08/16 13:36:02
Again - in this file I would remove the recording
 
ahendrickson
2011/08/17 20:12:04
Done.
 | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| + | 
| DCHECK(!async_context_.get() || !async_context_->callback()); | 
| LARGE_INTEGER distance, result; | 
| @@ -191,6 +211,8 @@ int64 FileStream::Seek(Whence whence, int64 offset) { | 
| if (!SetFilePointerEx(file_, distance, &result, move_method)) { | 
| DWORD error = GetLastError(); | 
| LOG(WARNING) << "SetFilePointerEx failed: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_SEEK); | 
| return MapErrorCode(error); | 
| } | 
| if (async_context_.get()) | 
| @@ -201,8 +223,11 @@ int64 FileStream::Seek(Whence whence, int64 offset) { | 
| int64 FileStream::Available() { | 
| base::ThreadRestrictions::AssertIOAllowed(); | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| int64 cur_pos = Seek(FROM_CURRENT, 0); | 
| if (cur_pos < 0) | 
| @@ -212,6 +237,8 @@ int64 FileStream::Available() { | 
| if (!GetFileSizeEx(file_, &file_size)) { | 
| DWORD error = GetLastError(); | 
| LOG(WARNING) << "GetFileSizeEx failed: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_GET_SIZE); | 
| return MapErrorCode(error); | 
| } | 
| @@ -220,8 +247,12 @@ int64 FileStream::Available() { | 
| int FileStream::Read( | 
| char* buf, int buf_len, CompletionCallback* callback) { | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| + | 
| DCHECK(open_flags_ & base::PLATFORM_FILE_READ); | 
| OVERLAPPED* overlapped = NULL; | 
| @@ -246,6 +277,8 @@ int FileStream::Read( | 
| rv = 0; // Report EOF by returning 0 bytes read. | 
| } else { | 
| LOG(WARNING) << "ReadFile failed: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_READ); | 
| rv = MapErrorCode(error); | 
| } | 
| } else if (overlapped) { | 
| @@ -280,8 +313,11 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { | 
| int FileStream::Write( | 
| const char* buf, int buf_len, CompletionCallback* callback) { | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 
| OVERLAPPED* overlapped = NULL; | 
| @@ -303,6 +339,8 @@ int FileStream::Write( | 
| rv = ERR_IO_PENDING; | 
| } else { | 
| LOG(WARNING) << "WriteFile failed: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_WRITE); | 
| rv = MapErrorCode(error); | 
| } | 
| } else if (overlapped) { | 
| @@ -317,8 +355,11 @@ int FileStream::Write( | 
| int FileStream::Flush() { | 
| base::ThreadRestrictions::AssertIOAllowed(); | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 
| if (FlushFileBuffers(file_)) { | 
| @@ -327,6 +368,8 @@ int FileStream::Flush() { | 
| int rv; | 
| DWORD error = GetLastError(); | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_FLUSH); | 
| rv = MapErrorCode(error); | 
| return rv; | 
| } | 
| @@ -334,8 +377,11 @@ int FileStream::Flush() { | 
| int64 FileStream::Truncate(int64 bytes) { | 
| base::ThreadRestrictions::AssertIOAllowed(); | 
| - if (!IsOpen()) | 
| + if (!IsOpen()) { | 
| + if (record_uma_) | 
| + RecordFileError(ERROR_INVALID_HANDLE, FILE_ERROR_SOURCE_IS_NOT_OPEN); | 
| return ERR_UNEXPECTED; | 
| + } | 
| // We better be open for reading. | 
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 
| @@ -350,6 +396,8 @@ int64 FileStream::Truncate(int64 bytes) { | 
| if (!result) { | 
| DWORD error = GetLastError(); | 
| LOG(WARNING) << "SetEndOfFile failed: " << error; | 
| + if (record_uma_) | 
| + RecordFileError(error, FILE_ERROR_SOURCE_SET_EOF); | 
| return MapErrorCode(error); | 
| } | 
| @@ -357,4 +405,11 @@ int64 FileStream::Truncate(int64 bytes) { | 
| return seek_position; | 
| } | 
| +void FileStream::EnableRecording() { | 
| + record_uma_ = true; | 
| + | 
| + if (async_context_.get()) | 
| + async_context_->EnableRecording(); | 
| +} | 
| + | 
| } // namespace net |