| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/profiling/memlog_receiver_pipe_win.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "chrome/profiling/memlog_stream_receiver.h" |
| 14 |
| 15 namespace profiling { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Use a large buffer for our pipe. We don't want the sender to block |
| 20 // if at all possible since it will slow the app down quite a bit. Windows |
| 21 // seems to max out a 64K per read so we use that (larger would be better if it |
| 22 // worked). |
| 23 const int kReadBufferSize = 1024 * 64; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 MemlogReceiverPipe::CompletionThunk::CompletionThunk(HANDLE handle, Callback cb) |
| 28 : handle_(handle), callback_(cb) { |
| 29 base::MessageLoopForIO::current()->RegisterIOHandler(handle, this); |
| 30 ZeroOverlapped(); |
| 31 } |
| 32 |
| 33 MemlogReceiverPipe::CompletionThunk::~CompletionThunk() { |
| 34 if (handle_ != INVALID_HANDLE_VALUE) |
| 35 ::CloseHandle(handle_); |
| 36 } |
| 37 |
| 38 void MemlogReceiverPipe::CompletionThunk::ZeroOverlapped() { |
| 39 memset(overlapped(), 0, sizeof(OVERLAPPED)); |
| 40 } |
| 41 |
| 42 void MemlogReceiverPipe::CompletionThunk::OnIOCompleted( |
| 43 base::MessagePumpForIO::IOContext* context, |
| 44 DWORD bytes_transfered, |
| 45 DWORD error) { |
| 46 // Note: any crashes with this on the stack are likely a result of destroying |
| 47 // a relevant class while there is I/O pending. |
| 48 callback_.Run(static_cast<size_t>(bytes_transfered), error); |
| 49 } |
| 50 |
| 51 MemlogReceiverPipe::MemlogReceiverPipe(std::unique_ptr<CompletionThunk> thunk) |
| 52 : thunk_(std::move(thunk)), read_buffer_(new char[kReadBufferSize]) { |
| 53 // Need Unretained to avoid a reference cycle. |
| 54 thunk_->set_callback(base::BindRepeating(&MemlogReceiverPipe::OnIOCompleted, |
| 55 base::Unretained(this))); |
| 56 } |
| 57 |
| 58 MemlogReceiverPipe::~MemlogReceiverPipe() {} |
| 59 |
| 60 void MemlogReceiverPipe::StartReadingOnIOThread() { |
| 61 ReadUntilBlocking(); |
| 62 } |
| 63 |
| 64 int MemlogReceiverPipe::GetRemoteProcessID() { |
| 65 ULONG id = 0; |
| 66 ::GetNamedPipeClientProcessId(thunk_->handle(), &id); |
| 67 return static_cast<int>(id); |
| 68 } |
| 69 |
| 70 void MemlogReceiverPipe::SetReceiver( |
| 71 scoped_refptr<base::TaskRunner> task_runner, |
| 72 scoped_refptr<MemlogStreamReceiver> receiver) { |
| 73 receiver_task_runner_ = task_runner; |
| 74 receiver_ = receiver; |
| 75 } |
| 76 |
| 77 void MemlogReceiverPipe::OnIOCompleted(size_t bytes_transfered, DWORD error) { |
| 78 DCHECK(read_outstanding_); |
| 79 read_outstanding_ = false; |
| 80 |
| 81 // This will get called both for async completion of ConnectNamedPipe as well |
| 82 // as async read completions. |
| 83 if (bytes_transfered && receiver_) { |
| 84 receiver_task_runner_->PostTask( |
| 85 FROM_HERE, |
| 86 base::BindOnce(&MemlogStreamReceiver::OnStreamData, receiver_, |
| 87 std::move(read_buffer_), bytes_transfered)); |
| 88 read_buffer_.reset(new char[kReadBufferSize]); |
| 89 } |
| 90 ReadUntilBlocking(); |
| 91 } |
| 92 |
| 93 void MemlogReceiverPipe::ReadUntilBlocking() { |
| 94 // TODO(brettw) note that the IO completion callback will always be issued, |
| 95 // even for sync returns of ReadFile. If there is a lot of data ready to be |
| 96 // read, it would be nice to process them all in this loop rather than having |
| 97 // to go back to the message loop for each block, but that will require |
| 98 // different IOContext structures for each one. |
| 99 DWORD bytes_read = 0; |
| 100 thunk_->ZeroOverlapped(); |
| 101 |
| 102 DCHECK(!read_outstanding_); |
| 103 read_outstanding_ = true; |
| 104 if (!::ReadFile(thunk_->handle(), read_buffer_.get(), kReadBufferSize, |
| 105 &bytes_read, thunk_->overlapped())) { |
| 106 if (GetLastError() == ERROR_IO_PENDING) { |
| 107 return; |
| 108 } else { |
| 109 if (receiver_) { |
| 110 receiver_task_runner_->PostTask( |
| 111 FROM_HERE, |
| 112 base::BindOnce(&MemlogStreamReceiver::OnStreamComplete, receiver_)); |
| 113 } |
| 114 return; |
| 115 } |
| 116 } |
| 117 } |
| 118 |
| 119 } // namespace profiling |
| OLD | NEW |