forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemlog_receiver_pipe_win.cc
93 lines (78 loc) · 3.08 KB
/
memlog_receiver_pipe_win.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/profiling/memlog_receiver_pipe_win.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "chrome/profiling/memlog_receiver_pipe.h"
#include "chrome/profiling/memlog_stream_receiver.h"
namespace profiling {
namespace {
// Use a large buffer for our pipe. We don't want the sender to block
// if at all possible since it will slow the app down quite a bit. Windows
// seems to max out a 64K per read so we use that (larger would be better if it
// worked).
const int kReadBufferSize = 1024 * 64;
} // namespace
MemlogReceiverPipe::MemlogReceiverPipe(mojo::edk::ScopedPlatformHandle handle)
: MemlogReceiverPipeBase(std::move(handle)),
read_buffer_(new char[kReadBufferSize]) {
ZeroOverlapped();
base::MessageLoopForIO::current()->RegisterIOHandler(handle_.get().handle,
this);
}
MemlogReceiverPipe::~MemlogReceiverPipe() {
}
void MemlogReceiverPipe::StartReadingOnIOThread() {
ReadUntilBlocking();
}
void MemlogReceiverPipe::ReadUntilBlocking() {
// TODO(brettw) note that the IO completion callback will always be issued,
// even for sync returns of ReadFile. If there is a lot of data ready to be
// read, it would be nice to process them all in this loop rather than having
// to go back to the message loop for each block, but that will require
// different IOContext structures for each one.
DWORD bytes_read = 0;
ZeroOverlapped();
DCHECK(!read_outstanding_);
read_outstanding_ = true;
if (!::ReadFile(handle_.get().handle, read_buffer_.get(), kReadBufferSize,
&bytes_read, &context_.overlapped)) {
if (GetLastError() == ERROR_IO_PENDING) {
return;
} else {
if (receiver_) {
receiver_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&MemlogStreamReceiver::OnStreamComplete, receiver_));
}
return;
}
}
}
void MemlogReceiverPipe::ZeroOverlapped() {
memset(&context_.overlapped, 0, sizeof(OVERLAPPED));
}
void MemlogReceiverPipe::OnIOCompleted(
base::MessagePumpForIO::IOContext* context,
DWORD bytes_transfered,
DWORD error) {
// Note: any crashes with this on the stack are likely a result of destroying
// a relevant class while there is I/O pending.
DCHECK(read_outstanding_);
read_outstanding_ = false;
if (bytes_transfered && receiver_) {
receiver_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&MemlogReceiverPipe::OnStreamDataThunk, this,
base::MessageLoop::current()->task_runner(),
std::move(read_buffer_),
static_cast<size_t>(bytes_transfered)));
read_buffer_.reset(new char[kReadBufferSize]);
}
ReadUntilBlocking();
}
} // namespace profiling