Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 7d3f006

Browse files
jmgaoGerrit Code Review
authored andcommitted
Merge changes Ifed3b97a,I09aacb94,I9afedd7b
* changes: adb: fix NonblockingFdConnection's behavior with large writes. adb: fix zero-initialization in Block. adb: make benchmarks build on the host.
2 parents 02ac44c + bc4dbfa commit 7d3f006

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

adb/Android.bp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ cc_test_host {
213213
cc_benchmark {
214214
name: "adb_benchmark",
215215
defaults: ["adb_defaults"],
216+
host_supported: true,
216217

217218
srcs: ["transport_benchmark.cpp"],
218219
target: {
@@ -226,6 +227,9 @@ cc_benchmark {
226227
"libadb_host",
227228
],
228229
},
230+
darwin: {
231+
enabled: false,
232+
}
229233
},
230234

231235
static_libs: [

adb/transport_benchmark.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::SameThread);
183183
ADB_CONNECTION_BENCHMARK(BM_Connection_Echo, ThreadPolicy::MainThread);
184184

185185
int main(int argc, char** argv) {
186+
#if defined(__BIONIC__)
186187
// Set M_DECAY_TIME so that our allocations aren't immediately purged on free.
187188
mallopt(M_DECAY_TIME, 1);
189+
#endif
188190

189191
android::base::SetMinimumLogSeverity(android::base::WARNING);
190192
adb_trace_init(argv);

adb/transport_fd.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,9 @@ struct NonblockingFdConnection : public Connection {
8585
if (pfds[0].revents) {
8686
if ((pfds[0].revents & POLLOUT)) {
8787
std::lock_guard<std::mutex> lock(this->write_mutex_);
88-
WriteResult result = DispatchWrites();
89-
switch (result) {
90-
case WriteResult::Error:
91-
*error = "write failed";
92-
return;
93-
94-
case WriteResult::Completed:
95-
writable_ = true;
96-
break;
97-
98-
case WriteResult::TryAgain:
99-
break;
88+
if (DispatchWrites() == WriteResult::Error) {
89+
*error = "write failed";
90+
return;
10091
}
10192
}
10293

@@ -179,13 +170,14 @@ struct NonblockingFdConnection : public Connection {
179170

180171
WriteResult DispatchWrites() REQUIRES(write_mutex_) {
181172
CHECK(!write_buffer_.empty());
182-
if (!writable_) {
183-
return WriteResult::TryAgain;
184-
}
185-
186173
auto iovs = write_buffer_.iovecs();
187174
ssize_t rc = adb_writev(fd_.get(), iovs.data(), iovs.size());
188175
if (rc == -1) {
176+
if (errno == EAGAIN || errno == EWOULDBLOCK) {
177+
writable_ = false;
178+
return WriteResult::TryAgain;
179+
}
180+
189181
return WriteResult::Error;
190182
} else if (rc == 0) {
191183
errno = 0;
@@ -194,6 +186,7 @@ struct NonblockingFdConnection : public Connection {
194186

195187
// TODO: Implement a more efficient drop_front?
196188
write_buffer_.take_front(rc);
189+
writable_ = write_buffer_.empty();
197190
if (write_buffer_.empty()) {
198191
return WriteResult::Completed;
199192
}
@@ -211,7 +204,12 @@ struct NonblockingFdConnection : public Connection {
211204
if (!packet->payload.empty()) {
212205
write_buffer_.append(std::make_unique<IOVector::block_type>(std::move(packet->payload)));
213206
}
214-
return DispatchWrites() != WriteResult::Error;
207+
208+
WriteResult result = DispatchWrites();
209+
if (result == WriteResult::TryAgain) {
210+
WakeThread();
211+
}
212+
return result != WriteResult::Error;
215213
}
216214

217215
std::thread thread_;

adb/types.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ struct Block {
108108
CHECK_EQ(0ULL, capacity_);
109109
CHECK_EQ(0ULL, size_);
110110
if (size != 0) {
111-
data_ = std::make_unique<char[]>(size);
111+
// This isn't std::make_unique because that's equivalent to `new char[size]()`, which
112+
// value-initializes the array instead of leaving it uninitialized. As an optimization,
113+
// call new without parentheses to avoid this costly initialization.
114+
data_.reset(new char[size]);
112115
capacity_ = size;
113116
size_ = size;
114117
}

0 commit comments

Comments
 (0)