Chromium Code Reviews| 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..e17fdd2bc5d00b5533d61289340aea309547705f 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 { |
| @@ -52,8 +53,16 @@ static int MapErrorCode(DWORD err) { |
| class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| public: |
| + enum AsyncOperation { |
| + ASYNC_OPERATION_NONE = 0, |
| + ASYNC_OPERATION_READ, |
| + ASYNC_OPERATION_WRITE, |
| + ASYNC_OPERATION_SEEK, |
| + }; |
| + |
| AsyncContext(FileStream* owner) |
| - : owner_(owner), context_(), callback_(NULL), is_closing_(false) { |
| + : owner_(owner), context_(), callback_(NULL), is_closing_(false), |
| + record_uma_(false), operation_(ASYNC_OPERATION_NONE) { |
| context_.handler = this; |
| } |
| ~AsyncContext(); |
| @@ -63,6 +72,12 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| OVERLAPPED* overlapped() { return &context_.overlapped; } |
| CompletionCallback* callback() const { return callback_; } |
| + void set_operation(AsyncOperation operation) { operation_ = operation; } |
|
cbentzel
2011/08/18 13:31:28
Why isn't this just FILE_ERROR_SOURCE? That way yo
ahendrickson
2011/08/18 15:56:45
I believe there is 0 or 1 AsyncContext's per FileS
|
| + |
| + void EnableErrorStatistics() { |
| + record_uma_ = true; |
| + } |
| + |
| private: |
| virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| DWORD bytes_read, DWORD error); |
| @@ -71,6 +86,8 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| MessageLoopForIO::IOContext context_; |
| CompletionCallback* callback_; |
| bool is_closing_; |
| + bool record_uma_; |
| + AsyncOperation operation_; |
| }; |
| FileStream::AsyncContext::~AsyncContext() { |
| @@ -99,14 +116,23 @@ void FileStream::AsyncContext::OnIOCompleted( |
| DCHECK_EQ(&context_, context); |
| DCHECK(callback_); |
| + static FileErrorSource source_map[] = { |
| + FILE_ERROR_SOURCE_COUNT, // ASYNC_OPERATION_NONE |
| + FILE_ERROR_SOURCE_READ, // ASYNC_OPERATION_READ |
| + FILE_ERROR_SOURCE_WRITE, // ASYNC_OPERATION_WRITE |
| + FILE_ERROR_SOURCE_SEEK, // ASYNC_OPERATION_SEEK |
| + }; |
| + |
| if (is_closing_) { |
| callback_ = NULL; |
| return; |
| } |
| int result = static_cast<int>(bytes_read); |
| - if (error && error != ERROR_HANDLE_EOF) |
| + if (error && error != ERROR_HANDLE_EOF) { |
| + RecordFileError(error, source_map[operation_], record_uma_); |
| result = MapErrorCode(error); |
| + } |
| if (bytes_read) |
| IncrementOffset(&context->overlapped, bytes_read); |
| @@ -121,13 +147,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 +192,13 @@ int FileStream::Open(const FilePath& path, int open_flags) { |
| if (file_ == INVALID_HANDLE_VALUE) { |
| DWORD error = GetLastError(); |
| LOG(WARNING) << "Failed to open file: " << error; |
| - return MapErrorCode(error); |
| + return RecordAndMapError(error, FILE_ERROR_SOURCE_OPEN); |
| } |
| if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
| async_context_.reset(new AsyncContext(this)); |
| + if (record_uma_) |
| + async_context_->EnableErrorStatistics(); |
| MessageLoopForIO::current()->RegisterIOHandler(file_, |
| async_context_.get()); |
| } |
| @@ -183,6 +213,7 @@ bool FileStream::IsOpen() const { |
| int64 FileStream::Seek(Whence whence, int64 offset) { |
| if (!IsOpen()) |
| return ERR_UNEXPECTED; |
| + |
| DCHECK(!async_context_.get() || !async_context_->callback()); |
| LARGE_INTEGER distance, result; |
| @@ -191,10 +222,12 @@ int64 FileStream::Seek(Whence whence, int64 offset) { |
| if (!SetFilePointerEx(file_, distance, &result, move_method)) { |
| DWORD error = GetLastError(); |
| LOG(WARNING) << "SetFilePointerEx failed: " << error; |
| - return MapErrorCode(error); |
| + return RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK); |
| } |
| - if (async_context_.get()) |
| + if (async_context_.get()) { |
| + async_context_->set_operation(AsyncContext::ASYNC_OPERATION_SEEK); |
| SetOffset(async_context_->overlapped(), result); |
| + } |
| return result.QuadPart; |
| } |
| @@ -212,7 +245,7 @@ int64 FileStream::Available() { |
| if (!GetFileSizeEx(file_, &file_size)) { |
| DWORD error = GetLastError(); |
| LOG(WARNING) << "GetFileSizeEx failed: " << error; |
| - return MapErrorCode(error); |
| + return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE); |
| } |
| return file_size.QuadPart - cur_pos; |
| @@ -222,6 +255,7 @@ int FileStream::Read( |
| char* buf, int buf_len, CompletionCallback* callback) { |
| if (!IsOpen()) |
| return ERR_UNEXPECTED; |
| + |
| DCHECK(open_flags_ & base::PLATFORM_FILE_READ); |
| OVERLAPPED* overlapped = NULL; |
| @@ -229,6 +263,7 @@ int FileStream::Read( |
| DCHECK(callback); |
| DCHECK(!async_context_->callback()); |
| overlapped = async_context_->overlapped(); |
| + async_context_->set_operation(AsyncContext::ASYNC_OPERATION_READ); |
| } else { |
| DCHECK(!callback); |
| base::ThreadRestrictions::AssertIOAllowed(); |
| @@ -246,7 +281,7 @@ int FileStream::Read( |
| rv = 0; // Report EOF by returning 0 bytes read. |
| } else { |
| LOG(WARNING) << "ReadFile failed: " << error; |
| - rv = MapErrorCode(error); |
| + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ); |
| } |
| } else if (overlapped) { |
| async_context_->IOCompletionIsPending(callback); |
| @@ -282,6 +317,7 @@ int FileStream::Write( |
| const char* buf, int buf_len, CompletionCallback* callback) { |
| if (!IsOpen()) |
| return ERR_UNEXPECTED; |
| + |
| DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
| OVERLAPPED* overlapped = NULL; |
| @@ -289,6 +325,7 @@ int FileStream::Write( |
| DCHECK(callback); |
| DCHECK(!async_context_->callback()); |
| overlapped = async_context_->overlapped(); |
| + async_context_->set_operation(AsyncContext::ASYNC_OPERATION_WRITE); |
| } else { |
| DCHECK(!callback); |
| base::ThreadRestrictions::AssertIOAllowed(); |
| @@ -303,7 +340,7 @@ int FileStream::Write( |
| rv = ERR_IO_PENDING; |
| } else { |
| LOG(WARNING) << "WriteFile failed: " << error; |
| - rv = MapErrorCode(error); |
| + rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE); |
| } |
| } else if (overlapped) { |
| async_context_->IOCompletionIsPending(callback); |
| @@ -325,10 +362,7 @@ int FileStream::Flush() { |
| return OK; |
| } |
| - int rv; |
| - DWORD error = GetLastError(); |
| - rv = MapErrorCode(error); |
| - return rv; |
| + return RecordAndMapError(GetLastError(), FILE_ERROR_SOURCE_FLUSH); |
| } |
| int64 FileStream::Truncate(int64 bytes) { |
| @@ -350,11 +384,23 @@ int64 FileStream::Truncate(int64 bytes) { |
| if (!result) { |
| DWORD error = GetLastError(); |
| LOG(WARNING) << "SetEndOfFile failed: " << error; |
| - return MapErrorCode(error); |
| + return RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF); |
| } |
| // Success. |
| return seek_position; |
| } |
| +void FileStream::EnableErrorStatistics() { |
| + record_uma_ = true; |
| + |
| + if (async_context_.get()) |
| + async_context_->EnableErrorStatistics(); |
| +} |
| + |
| +int FileStream::RecordAndMapError(int error, FileErrorSource source) { |
| + RecordFileError(error, source, record_uma_); |
| + return MapErrorCode(error); |
| +} |
| + |
| } // namespace net |