adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define TRACE_TAG USB
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
2020-02-19 22:50:57 +01:00
|
|
|
#include <inttypes.h>
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <linux/usb/functionfs.h>
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
|
2019-02-27 02:53:52 +01:00
|
|
|
#include <algorithm>
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
#include <array>
|
|
|
|
#include <future>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <asyncio/AsyncIO.h>
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
#include <android-base/macros.h>
|
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/thread_annotations.h>
|
|
|
|
|
|
|
|
#include "adb_unique_fd.h"
|
|
|
|
#include "adb_utils.h"
|
2020-03-28 02:09:56 +01:00
|
|
|
#include "daemon/usb_ffs.h"
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
#include "sysdeps/chrono.h"
|
|
|
|
#include "transport.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2019-04-16 20:20:04 +02:00
|
|
|
// Not all USB controllers support operations larger than 16k, so don't go above that.
|
2019-04-24 23:28:25 +02:00
|
|
|
// Also, each submitted operation does an allocation in the kernel of that size, so we want to
|
|
|
|
// minimize our queue depth while still maintaining a deep enough queue to keep the USB stack fed.
|
|
|
|
static constexpr size_t kUsbReadQueueDepth = 8;
|
2019-04-16 20:20:04 +02:00
|
|
|
static constexpr size_t kUsbReadSize = 4 * PAGE_SIZE;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
2019-04-24 23:28:25 +02:00
|
|
|
static constexpr size_t kUsbWriteQueueDepth = 8;
|
2019-04-16 20:20:04 +02:00
|
|
|
static constexpr size_t kUsbWriteSize = 4 * PAGE_SIZE;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
2019-06-24 23:35:35 +02:00
|
|
|
static const char* to_string(enum usb_functionfs_event_type type) {
|
|
|
|
switch (type) {
|
|
|
|
case FUNCTIONFS_BIND:
|
|
|
|
return "FUNCTIONFS_BIND";
|
|
|
|
case FUNCTIONFS_UNBIND:
|
|
|
|
return "FUNCTIONFS_UNBIND";
|
|
|
|
case FUNCTIONFS_ENABLE:
|
|
|
|
return "FUNCTIONFS_ENABLE";
|
|
|
|
case FUNCTIONFS_DISABLE:
|
|
|
|
return "FUNCTIONFS_DISABLE";
|
|
|
|
case FUNCTIONFS_SETUP:
|
|
|
|
return "FUNCTIONFS_SETUP";
|
|
|
|
case FUNCTIONFS_SUSPEND:
|
|
|
|
return "FUNCTIONFS_SUSPEND";
|
|
|
|
case FUNCTIONFS_RESUME:
|
|
|
|
return "FUNCTIONFS_RESUME";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
enum class TransferDirection : uint64_t {
|
|
|
|
READ = 0,
|
|
|
|
WRITE = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TransferId {
|
|
|
|
TransferDirection direction : 1;
|
|
|
|
uint64_t id : 63;
|
|
|
|
|
|
|
|
TransferId() : TransferId(TransferDirection::READ, 0) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TransferId(TransferDirection direction, uint64_t id) : direction(direction), id(id) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit operator uint64_t() const {
|
|
|
|
uint64_t result;
|
|
|
|
static_assert(sizeof(*this) == sizeof(result));
|
|
|
|
memcpy(&result, this, sizeof(*this));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TransferId read(uint64_t id) { return TransferId(TransferDirection::READ, id); }
|
|
|
|
static TransferId write(uint64_t id) { return TransferId(TransferDirection::WRITE, id); }
|
|
|
|
|
|
|
|
static TransferId from_value(uint64_t value) {
|
|
|
|
TransferId result;
|
|
|
|
memcpy(&result, &value, sizeof(value));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
template <class Payload>
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
struct IoBlock {
|
2019-03-18 22:11:28 +01:00
|
|
|
bool pending = false;
|
2019-05-16 03:45:01 +02:00
|
|
|
struct iocb control = {};
|
2019-07-12 23:11:54 +02:00
|
|
|
Payload payload;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
|
|
|
TransferId id() const { return TransferId::from_value(control.aio_data); }
|
|
|
|
};
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
using IoReadBlock = IoBlock<Block>;
|
|
|
|
using IoWriteBlock = IoBlock<std::shared_ptr<Block>>;
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
struct ScopedAioContext {
|
|
|
|
ScopedAioContext() = default;
|
|
|
|
~ScopedAioContext() { reset(); }
|
|
|
|
|
|
|
|
ScopedAioContext(ScopedAioContext&& move) { reset(move.release()); }
|
|
|
|
ScopedAioContext(const ScopedAioContext& copy) = delete;
|
|
|
|
|
|
|
|
ScopedAioContext& operator=(ScopedAioContext&& move) {
|
|
|
|
reset(move.release());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ScopedAioContext& operator=(const ScopedAioContext& copy) = delete;
|
|
|
|
|
|
|
|
static ScopedAioContext Create(size_t max_events) {
|
|
|
|
aio_context_t ctx = 0;
|
|
|
|
if (io_setup(max_events, &ctx) != 0) {
|
|
|
|
PLOG(FATAL) << "failed to create aio_context_t";
|
|
|
|
}
|
|
|
|
ScopedAioContext result;
|
|
|
|
result.reset(ctx);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
aio_context_t release() {
|
|
|
|
aio_context_t result = context_;
|
|
|
|
context_ = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset(aio_context_t new_context = 0) {
|
|
|
|
if (context_ != 0) {
|
|
|
|
io_destroy(context_);
|
|
|
|
}
|
|
|
|
|
|
|
|
context_ = new_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
aio_context_t get() { return context_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
aio_context_t context_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UsbFfsConnection : public Connection {
|
2019-06-24 23:35:35 +02:00
|
|
|
UsbFfsConnection(unique_fd control, unique_fd read, unique_fd write,
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
std::promise<void> destruction_notifier)
|
2019-03-27 02:47:45 +01:00
|
|
|
: worker_started_(false),
|
|
|
|
stopped_(false),
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
destruction_notifier_(std::move(destruction_notifier)),
|
2019-06-24 23:35:35 +02:00
|
|
|
control_fd_(std::move(control)),
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
read_fd_(std::move(read)),
|
|
|
|
write_fd_(std::move(write)) {
|
|
|
|
LOG(INFO) << "UsbFfsConnection constructed";
|
2019-02-14 00:27:28 +01:00
|
|
|
worker_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
|
|
|
|
if (worker_event_fd_ == -1) {
|
|
|
|
PLOG(FATAL) << "failed to create eventfd";
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:35:35 +02:00
|
|
|
monitor_event_fd_.reset(eventfd(0, EFD_CLOEXEC));
|
|
|
|
if (monitor_event_fd_ == -1) {
|
|
|
|
PLOG(FATAL) << "failed to create eventfd";
|
|
|
|
}
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
aio_context_ = ScopedAioContext::Create(kUsbReadQueueDepth + kUsbWriteQueueDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
~UsbFfsConnection() {
|
|
|
|
LOG(INFO) << "UsbFfsConnection being destroyed";
|
|
|
|
Stop();
|
|
|
|
monitor_thread_.join();
|
2019-02-14 00:27:28 +01:00
|
|
|
|
|
|
|
// We need to explicitly close our file descriptors before we notify our destruction,
|
|
|
|
// because the thread listening on the future will immediately try to reopen the endpoint.
|
2019-03-27 02:47:45 +01:00
|
|
|
aio_context_.reset();
|
2019-06-24 23:35:35 +02:00
|
|
|
control_fd_.reset();
|
2019-02-14 00:27:28 +01:00
|
|
|
read_fd_.reset();
|
|
|
|
write_fd_.reset();
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
destruction_notifier_.set_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Write(std::unique_ptr<apacket> packet) override final {
|
|
|
|
LOG(DEBUG) << "USB write: " << dump_header(&packet->msg);
|
2019-07-12 23:11:54 +02:00
|
|
|
auto header = std::make_shared<Block>(sizeof(packet->msg));
|
|
|
|
memcpy(header->data(), &packet->msg, sizeof(packet->msg));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(write_mutex_);
|
2019-07-12 23:11:54 +02:00
|
|
|
write_requests_.push_back(
|
|
|
|
CreateWriteBlock(std::move(header), 0, sizeof(packet->msg), next_write_id_++));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (!packet->payload.empty()) {
|
2019-02-27 02:53:52 +01:00
|
|
|
// The kernel attempts to allocate a contiguous block of memory for each write,
|
|
|
|
// which can fail if the write is large and the kernel heap is fragmented.
|
|
|
|
// Split large writes into smaller chunks to avoid this.
|
2019-07-12 23:11:54 +02:00
|
|
|
auto payload = std::make_shared<Block>(std::move(packet->payload));
|
2019-02-27 02:53:52 +01:00
|
|
|
size_t offset = 0;
|
|
|
|
size_t len = payload->size();
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
size_t write_size = std::min(kUsbWriteSize, len);
|
|
|
|
write_requests_.push_back(
|
|
|
|
CreateWriteBlock(payload, offset, write_size, next_write_id_++));
|
|
|
|
len -= write_size;
|
|
|
|
offset += write_size;
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
2020-05-20 00:57:06 +02:00
|
|
|
|
|
|
|
// Wake up the worker thread to submit writes.
|
|
|
|
uint64_t notify = 1;
|
|
|
|
ssize_t rc = adb_write(worker_event_fd_.get(), ¬ify, sizeof(notify));
|
|
|
|
if (rc < 0) {
|
|
|
|
PLOG(FATAL) << "failed to notify worker eventfd to submit writes";
|
|
|
|
}
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Start() override final { StartMonitor(); }
|
|
|
|
|
|
|
|
virtual void Stop() override final {
|
|
|
|
if (stopped_.exchange(true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stopped_ = true;
|
|
|
|
uint64_t notify = 1;
|
2019-02-14 00:27:28 +01:00
|
|
|
ssize_t rc = adb_write(worker_event_fd_.get(), ¬ify, sizeof(notify));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (rc < 0) {
|
2019-02-14 00:27:28 +01:00
|
|
|
PLOG(FATAL) << "failed to notify worker eventfd to stop UsbFfsConnection";
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
|
2019-06-24 23:35:35 +02:00
|
|
|
|
|
|
|
rc = adb_write(monitor_event_fd_.get(), ¬ify, sizeof(notify));
|
|
|
|
if (rc < 0) {
|
|
|
|
PLOG(FATAL) << "failed to notify monitor eventfd to stop UsbFfsConnection";
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(static_cast<size_t>(rc), sizeof(notify));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
[adbwifi] Add A_STLS command.
This command will be sent by adbd to notify the client that the
connection will be over TLS.
When client connects, it will send the CNXN packet, as usual. If the
server connection has TLS enabled, it will send the A_STLS packet
(regardless of whether auth is required). At this point, the client's
only valid response is to send a A_STLS packet. Once both sides have
exchanged the A_STLS packet, both will start the TLS handshake.
If auth is required, then the client will receive a CertificateRequest
with a list of known public keys (SHA256 hash) that it can use in its
certificate. Otherwise, the list will be empty and the client can assume
that either any key will work, or none will work.
If the handshake was successful, the server will send the CNXN packet
and the usual adb protocol is resumed over TLS. If the handshake failed,
both sides will disconnect, as there's no point to retry because the
server's known keys have already been communicated.
Bug: 111434128
Test: WIP; will add to adb_test.py/adb_device.py.
Enable wireless debugging in the Settings, then 'adb connect
<ip>:<port>'. Connection should succeed if key is in keystore. Used
wireshark to check for packet encryption.
Change-Id: I3d60647491c6c6b92297e4f628707a6457fa9420
2020-01-21 22:19:42 +01:00
|
|
|
virtual bool DoTlsHandshake(RSA* key, std::string* auth_key) override final {
|
|
|
|
// TODO: support TLS for usb connections.
|
|
|
|
LOG(FATAL) << "Not supported yet.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
private:
|
|
|
|
void StartMonitor() {
|
|
|
|
// This is a bit of a mess.
|
|
|
|
// It's possible for io_submit to end up blocking, if we call it as the endpoint
|
|
|
|
// becomes disabled. Work around this by having a monitor thread to listen for functionfs
|
|
|
|
// lifecycle events. If we notice an error condition (either we've become disabled, or we
|
|
|
|
// were never enabled in the first place), we send interruption signals to the worker thread
|
|
|
|
// until it dies, and then report failure to the transport via HandleError, which will
|
|
|
|
// eventually result in the transport being destroyed, which will result in UsbFfsConnection
|
|
|
|
// being destroyed, which unblocks the open thread and restarts this entire process.
|
|
|
|
static std::once_flag handler_once;
|
|
|
|
std::call_once(handler_once, []() { signal(kInterruptionSignal, [](int) {}); });
|
|
|
|
|
|
|
|
monitor_thread_ = std::thread([this]() {
|
|
|
|
adb_thread_setname("UsbFfs-monitor");
|
2020-02-27 00:44:30 +01:00
|
|
|
LOG(INFO) << "UsbFfs-monitor thread spawned";
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
2019-06-24 23:35:35 +02:00
|
|
|
bool bound = false;
|
2019-03-26 21:21:42 +01:00
|
|
|
bool enabled = false;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
bool running = true;
|
|
|
|
while (running) {
|
2019-02-14 00:27:28 +01:00
|
|
|
adb_pollfd pfd[2] = {
|
2019-06-24 23:35:35 +02:00
|
|
|
{ .fd = control_fd_.get(), .events = POLLIN, .revents = 0 },
|
|
|
|
{ .fd = monitor_event_fd_.get(), .events = POLLIN, .revents = 0 },
|
2019-02-14 00:27:28 +01:00
|
|
|
};
|
2019-03-27 02:47:45 +01:00
|
|
|
|
2019-06-24 23:35:35 +02:00
|
|
|
// If we don't see our first bind within a second, try again.
|
|
|
|
int timeout_ms = bound ? -1 : 1000;
|
|
|
|
|
|
|
|
int rc = TEMP_FAILURE_RETRY(adb_poll(pfd, 2, timeout_ms));
|
2019-02-14 00:27:28 +01:00
|
|
|
if (rc == -1) {
|
|
|
|
PLOG(FATAL) << "poll on USB control fd failed";
|
2019-06-24 23:35:35 +02:00
|
|
|
} else if (rc == 0) {
|
|
|
|
LOG(WARNING) << "timed out while waiting for FUNCTIONFS_BIND, trying again";
|
|
|
|
break;
|
2019-02-14 00:27:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pfd[1].revents) {
|
2019-06-24 23:35:35 +02:00
|
|
|
// We were told to die.
|
|
|
|
break;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
struct usb_functionfs_event event;
|
2019-06-24 23:35:35 +02:00
|
|
|
rc = TEMP_FAILURE_RETRY(adb_read(control_fd_.get(), &event, sizeof(event)));
|
2019-05-10 20:37:34 +02:00
|
|
|
if (rc == -1) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
PLOG(FATAL) << "failed to read functionfs event";
|
2019-05-10 20:37:34 +02:00
|
|
|
} else if (rc == 0) {
|
|
|
|
LOG(WARNING) << "hit EOF on functionfs control fd";
|
|
|
|
break;
|
|
|
|
} else if (rc != sizeof(event)) {
|
|
|
|
LOG(FATAL) << "read functionfs event of unexpected size, expected "
|
|
|
|
<< sizeof(event) << ", got " << rc;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "USB event: "
|
2019-06-24 23:35:35 +02:00
|
|
|
<< to_string(static_cast<usb_functionfs_event_type>(event.type));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
case FUNCTIONFS_BIND:
|
2019-06-24 23:35:35 +02:00
|
|
|
if (bound) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_BIND while already bound?";
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabled) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_BIND while already enabled?";
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bound = true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNCTIONFS_ENABLE:
|
2019-06-24 23:35:35 +02:00
|
|
|
if (!bound) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_ENABLE while not bound?";
|
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:05:53 +01:00
|
|
|
if (enabled) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_ENABLE while already enabled?";
|
|
|
|
running = false;
|
2019-05-02 01:53:53 +02:00
|
|
|
break;
|
2019-03-28 19:05:53 +01:00
|
|
|
}
|
2019-03-26 21:21:42 +01:00
|
|
|
|
2019-03-28 19:05:53 +01:00
|
|
|
enabled = true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
StartWorker();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNCTIONFS_DISABLE:
|
2019-06-24 23:35:35 +02:00
|
|
|
if (!bound) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_DISABLE while not bound?";
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:05:53 +01:00
|
|
|
if (!enabled) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_DISABLE while not enabled?";
|
|
|
|
}
|
2019-03-26 21:21:42 +01:00
|
|
|
|
2019-03-28 19:05:53 +01:00
|
|
|
enabled = false;
|
2019-03-26 21:21:42 +01:00
|
|
|
running = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNCTIONFS_UNBIND:
|
2019-03-28 19:05:53 +01:00
|
|
|
if (enabled) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_UNBIND while still enabled?";
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:35:35 +02:00
|
|
|
if (!bound) {
|
|
|
|
LOG(WARNING) << "received FUNCTIONFS_UNBIND when not bound?";
|
|
|
|
}
|
|
|
|
|
|
|
|
bound = false;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
running = false;
|
|
|
|
break;
|
2019-05-16 03:03:29 +02:00
|
|
|
|
|
|
|
case FUNCTIONFS_SETUP: {
|
2019-06-24 23:35:35 +02:00
|
|
|
LOG(INFO) << "received FUNCTIONFS_SETUP control transfer: bRequestType = "
|
|
|
|
<< static_cast<int>(event.u.setup.bRequestType)
|
|
|
|
<< ", bRequest = " << static_cast<int>(event.u.setup.bRequest)
|
|
|
|
<< ", wValue = " << static_cast<int>(event.u.setup.wValue)
|
|
|
|
<< ", wIndex = " << static_cast<int>(event.u.setup.wIndex)
|
|
|
|
<< ", wLength = " << static_cast<int>(event.u.setup.wLength);
|
|
|
|
|
|
|
|
if ((event.u.setup.bRequestType & USB_DIR_IN)) {
|
|
|
|
LOG(INFO) << "acking device-to-host control transfer";
|
|
|
|
ssize_t rc = adb_write(control_fd_.get(), "", 0);
|
|
|
|
if (rc != 0) {
|
|
|
|
PLOG(ERROR) << "failed to write empty packet to host";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::string buf;
|
|
|
|
buf.resize(event.u.setup.wLength + 1);
|
|
|
|
|
|
|
|
ssize_t rc = adb_read(control_fd_.get(), buf.data(), buf.size());
|
|
|
|
if (rc != event.u.setup.wLength) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "read " << rc
|
|
|
|
<< " bytes when trying to read control request, expected "
|
|
|
|
<< event.u.setup.wLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(INFO) << "control request contents: " << buf;
|
|
|
|
break;
|
|
|
|
}
|
2019-05-16 03:03:29 +02:00
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:29:32 +01:00
|
|
|
StopWorker();
|
2019-03-27 02:47:45 +01:00
|
|
|
HandleError("monitor thread finished");
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartWorker() {
|
2019-03-27 02:47:45 +01:00
|
|
|
CHECK(!worker_started_);
|
|
|
|
worker_started_ = true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
worker_thread_ = std::thread([this]() {
|
|
|
|
adb_thread_setname("UsbFfs-worker");
|
2020-02-27 00:44:30 +01:00
|
|
|
LOG(INFO) << "UsbFfs-worker thread spawned";
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
for (size_t i = 0; i < kUsbReadQueueDepth; ++i) {
|
|
|
|
read_requests_[i] = CreateReadBlock(next_read_id_++);
|
2019-02-14 00:27:28 +01:00
|
|
|
if (!SubmitRead(&read_requests_[i])) {
|
|
|
|
return;
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!stopped_) {
|
|
|
|
uint64_t dummy;
|
2019-02-14 00:27:28 +01:00
|
|
|
ssize_t rc = adb_read(worker_event_fd_.get(), &dummy, sizeof(dummy));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (rc == -1) {
|
|
|
|
PLOG(FATAL) << "failed to read from eventfd";
|
|
|
|
} else if (rc == 0) {
|
|
|
|
LOG(FATAL) << "hit EOF on eventfd";
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:21:42 +01:00
|
|
|
ReadEvents();
|
2020-05-20 00:57:06 +02:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(write_mutex_);
|
|
|
|
SubmitWrites();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:29:32 +01:00
|
|
|
void StopWorker() {
|
2019-03-27 02:47:45 +01:00
|
|
|
if (!worker_started_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 22:29:32 +01:00
|
|
|
pthread_t worker_thread_handle = worker_thread_.native_handle();
|
|
|
|
while (true) {
|
|
|
|
int rc = pthread_kill(worker_thread_handle, kInterruptionSignal);
|
|
|
|
if (rc != 0) {
|
|
|
|
LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(100ms);
|
|
|
|
|
|
|
|
rc = pthread_kill(worker_thread_handle, 0);
|
|
|
|
if (rc == 0) {
|
|
|
|
continue;
|
|
|
|
} else if (rc == ESRCH) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "failed to send interruption signal to worker: " << strerror(rc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
worker_thread_.join();
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
void PrepareReadBlock(IoReadBlock* block, uint64_t id) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
block->pending = false;
|
2019-07-12 23:11:54 +02:00
|
|
|
if (block->payload.capacity() >= kUsbReadSize) {
|
|
|
|
block->payload.resize(kUsbReadSize);
|
|
|
|
} else {
|
|
|
|
block->payload = Block(kUsbReadSize);
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
block->control.aio_data = static_cast<uint64_t>(TransferId::read(id));
|
2019-07-12 23:11:54 +02:00
|
|
|
block->control.aio_buf = reinterpret_cast<uintptr_t>(block->payload.data());
|
|
|
|
block->control.aio_nbytes = block->payload.size();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
IoReadBlock CreateReadBlock(uint64_t id) {
|
|
|
|
IoReadBlock block;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
PrepareReadBlock(&block, id);
|
|
|
|
block.control.aio_rw_flags = 0;
|
|
|
|
block.control.aio_lio_opcode = IOCB_CMD_PREAD;
|
|
|
|
block.control.aio_reqprio = 0;
|
|
|
|
block.control.aio_fildes = read_fd_.get();
|
|
|
|
block.control.aio_offset = 0;
|
|
|
|
block.control.aio_flags = IOCB_FLAG_RESFD;
|
2019-02-14 00:27:28 +01:00
|
|
|
block.control.aio_resfd = worker_event_fd_.get();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:21:42 +01:00
|
|
|
void ReadEvents() {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
static constexpr size_t kMaxEvents = kUsbReadQueueDepth + kUsbWriteQueueDepth;
|
|
|
|
struct io_event events[kMaxEvents];
|
|
|
|
struct timespec timeout = {.tv_sec = 0, .tv_nsec = 0};
|
|
|
|
int rc = io_getevents(aio_context_.get(), 0, kMaxEvents, events, &timeout);
|
|
|
|
if (rc == -1) {
|
|
|
|
HandleError(StringPrintf("io_getevents failed while reading: %s", strerror(errno)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int event_idx = 0; event_idx < rc; ++event_idx) {
|
|
|
|
auto& event = events[event_idx];
|
|
|
|
TransferId id = TransferId::from_value(event.data);
|
|
|
|
|
|
|
|
if (event.res < 0) {
|
|
|
|
std::string error =
|
|
|
|
StringPrintf("%s %" PRIu64 " failed with error %s",
|
|
|
|
id.direction == TransferDirection::READ ? "read" : "write",
|
|
|
|
id.id, strerror(-event.res));
|
|
|
|
HandleError(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id.direction == TransferDirection::READ) {
|
2020-02-28 23:53:56 +01:00
|
|
|
if (!HandleRead(id, event.res)) {
|
|
|
|
return;
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
} else {
|
|
|
|
HandleWrite(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 23:53:56 +01:00
|
|
|
bool HandleRead(TransferId id, int64_t size) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
uint64_t read_idx = id.id % kUsbReadQueueDepth;
|
2019-07-12 23:11:54 +02:00
|
|
|
IoReadBlock* block = &read_requests_[read_idx];
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
block->pending = false;
|
2019-07-12 23:11:54 +02:00
|
|
|
block->payload.resize(size);
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
|
|
|
// Notification for completed reads can be received out of order.
|
|
|
|
if (block->id().id != needed_read_id_) {
|
|
|
|
LOG(VERBOSE) << "read " << block->id().id << " completed while waiting for "
|
|
|
|
<< needed_read_id_;
|
2020-02-28 23:53:56 +01:00
|
|
|
return true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint64_t id = needed_read_id_;; ++id) {
|
|
|
|
size_t read_idx = id % kUsbReadQueueDepth;
|
2019-07-12 23:11:54 +02:00
|
|
|
IoReadBlock* current_block = &read_requests_[read_idx];
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (current_block->pending) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-28 23:53:56 +01:00
|
|
|
if (!ProcessRead(current_block)) {
|
|
|
|
return false;
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
++needed_read_id_;
|
|
|
|
}
|
2020-02-28 23:53:56 +01:00
|
|
|
|
|
|
|
return true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:53:56 +01:00
|
|
|
bool ProcessRead(IoReadBlock* block) {
|
2019-07-12 23:11:54 +02:00
|
|
|
if (!block->payload.empty()) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (!incoming_header_.has_value()) {
|
2020-02-28 23:53:56 +01:00
|
|
|
if (block->payload.size() != sizeof(amessage)) {
|
|
|
|
HandleError("received packet of unexpected length while reading header");
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-12 23:11:54 +02:00
|
|
|
amessage& msg = incoming_header_.emplace();
|
|
|
|
memcpy(&msg, block->payload.data(), sizeof(msg));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
LOG(DEBUG) << "USB read:" << dump_header(&msg);
|
|
|
|
incoming_header_ = msg;
|
|
|
|
} else {
|
|
|
|
size_t bytes_left = incoming_header_->data_length - incoming_payload_.size();
|
2020-02-28 23:53:56 +01:00
|
|
|
if (block->payload.size() > bytes_left) {
|
|
|
|
HandleError("received too many bytes while waiting for payload");
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-11 20:40:00 +02:00
|
|
|
incoming_payload_.append(std::move(block->payload));
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (incoming_header_->data_length == incoming_payload_.size()) {
|
|
|
|
auto packet = std::make_unique<apacket>();
|
|
|
|
packet->msg = *incoming_header_;
|
|
|
|
|
|
|
|
// TODO: Make apacket contain an IOVector so we don't have to coalesce.
|
2019-07-12 23:11:54 +02:00
|
|
|
packet->payload = std::move(incoming_payload_).coalesce();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
read_callback_(this, std::move(packet));
|
|
|
|
|
|
|
|
incoming_header_.reset();
|
2019-07-12 23:11:54 +02:00
|
|
|
// reuse the capacity of the incoming payload while we can.
|
|
|
|
auto free_block = incoming_payload_.clear();
|
|
|
|
if (block->payload.capacity() == 0) {
|
|
|
|
block->payload = std::move(free_block);
|
|
|
|
}
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrepareReadBlock(block, block->id().id + kUsbReadQueueDepth);
|
|
|
|
SubmitRead(block);
|
2020-02-28 23:53:56 +01:00
|
|
|
return true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
bool SubmitRead(IoReadBlock* block) {
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
block->pending = true;
|
|
|
|
struct iocb* iocb = &block->control;
|
|
|
|
if (io_submit(aio_context_.get(), 1, &iocb) != 1) {
|
|
|
|
HandleError(StringPrintf("failed to submit read: %s", strerror(errno)));
|
2019-02-14 00:27:28 +01:00
|
|
|
return false;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
2019-02-14 00:27:28 +01:00
|
|
|
|
|
|
|
return true;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HandleWrite(TransferId id) {
|
|
|
|
std::lock_guard<std::mutex> lock(write_mutex_);
|
|
|
|
auto it =
|
|
|
|
std::find_if(write_requests_.begin(), write_requests_.end(), [id](const auto& req) {
|
2019-07-12 23:11:54 +02:00
|
|
|
return static_cast<uint64_t>(req.id()) == static_cast<uint64_t>(id);
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
});
|
|
|
|
CHECK(it != write_requests_.end());
|
|
|
|
|
|
|
|
write_requests_.erase(it);
|
|
|
|
size_t outstanding_writes = --writes_submitted_;
|
|
|
|
LOG(DEBUG) << "USB write: reaped, down to " << outstanding_writes;
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
IoWriteBlock CreateWriteBlock(std::shared_ptr<Block> payload, size_t offset, size_t len,
|
|
|
|
uint64_t id) {
|
|
|
|
auto block = IoWriteBlock();
|
|
|
|
block.payload = std::move(payload);
|
|
|
|
block.control.aio_data = static_cast<uint64_t>(TransferId::write(id));
|
|
|
|
block.control.aio_rw_flags = 0;
|
|
|
|
block.control.aio_lio_opcode = IOCB_CMD_PWRITE;
|
|
|
|
block.control.aio_reqprio = 0;
|
|
|
|
block.control.aio_fildes = write_fd_.get();
|
|
|
|
block.control.aio_buf = reinterpret_cast<uintptr_t>(block.payload->data() + offset);
|
|
|
|
block.control.aio_nbytes = len;
|
|
|
|
block.control.aio_offset = 0;
|
|
|
|
block.control.aio_flags = IOCB_FLAG_RESFD;
|
|
|
|
block.control.aio_resfd = worker_event_fd_.get();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
IoWriteBlock CreateWriteBlock(Block&& payload, uint64_t id) {
|
|
|
|
size_t len = payload.size();
|
|
|
|
return CreateWriteBlock(std::make_shared<Block>(std::move(payload)), 0, len, id);
|
2019-02-27 02:53:52 +01:00
|
|
|
}
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
void SubmitWrites() REQUIRES(write_mutex_) {
|
|
|
|
if (writes_submitted_ == kUsbWriteQueueDepth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t writes_to_submit = std::min(kUsbWriteQueueDepth - writes_submitted_,
|
|
|
|
write_requests_.size() - writes_submitted_);
|
|
|
|
CHECK_GE(writes_to_submit, 0);
|
|
|
|
if (writes_to_submit == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct iocb* iocbs[kUsbWriteQueueDepth];
|
|
|
|
for (int i = 0; i < writes_to_submit; ++i) {
|
2019-07-12 23:11:54 +02:00
|
|
|
CHECK(!write_requests_[writes_submitted_ + i].pending);
|
|
|
|
write_requests_[writes_submitted_ + i].pending = true;
|
|
|
|
iocbs[i] = &write_requests_[writes_submitted_ + i].control;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
LOG(VERBOSE) << "submitting write_request " << static_cast<void*>(iocbs[i]);
|
|
|
|
}
|
|
|
|
|
2019-03-26 21:06:38 +01:00
|
|
|
writes_submitted_ += writes_to_submit;
|
|
|
|
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
int rc = io_submit(aio_context_.get(), writes_to_submit, iocbs);
|
|
|
|
if (rc == -1) {
|
|
|
|
HandleError(StringPrintf("failed to submit write requests: %s", strerror(errno)));
|
|
|
|
return;
|
|
|
|
} else if (rc != writes_to_submit) {
|
|
|
|
LOG(FATAL) << "failed to submit all writes: wanted to submit " << writes_to_submit
|
|
|
|
<< ", actually submitted " << rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HandleError(const std::string& error) {
|
|
|
|
std::call_once(error_flag_, [&]() {
|
|
|
|
error_callback_(this, error);
|
|
|
|
if (!stopped_) {
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::thread monitor_thread_;
|
2019-03-27 02:47:45 +01:00
|
|
|
|
|
|
|
bool worker_started_;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
std::thread worker_thread_;
|
|
|
|
|
|
|
|
std::atomic<bool> stopped_;
|
|
|
|
std::promise<void> destruction_notifier_;
|
|
|
|
std::once_flag error_flag_;
|
|
|
|
|
2019-02-14 00:27:28 +01:00
|
|
|
unique_fd worker_event_fd_;
|
2019-06-24 23:35:35 +02:00
|
|
|
unique_fd monitor_event_fd_;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
|
|
|
|
ScopedAioContext aio_context_;
|
2019-06-24 23:35:35 +02:00
|
|
|
unique_fd control_fd_;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
unique_fd read_fd_;
|
|
|
|
unique_fd write_fd_;
|
|
|
|
|
|
|
|
std::optional<amessage> incoming_header_;
|
|
|
|
IOVector incoming_payload_;
|
|
|
|
|
2019-07-12 23:11:54 +02:00
|
|
|
std::array<IoReadBlock, kUsbReadQueueDepth> read_requests_;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
IOVector read_data_;
|
|
|
|
|
|
|
|
// ID of the next request that we're going to send out.
|
|
|
|
size_t next_read_id_ = 0;
|
|
|
|
|
|
|
|
// ID of the next packet we're waiting for.
|
|
|
|
size_t needed_read_id_ = 0;
|
|
|
|
|
|
|
|
std::mutex write_mutex_;
|
2019-07-12 23:11:54 +02:00
|
|
|
std::deque<IoWriteBlock> write_requests_ GUARDED_BY(write_mutex_);
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
size_t next_write_id_ GUARDED_BY(write_mutex_) = 0;
|
|
|
|
size_t writes_submitted_ GUARDED_BY(write_mutex_) = 0;
|
2019-02-28 22:29:32 +01:00
|
|
|
|
|
|
|
static constexpr int kInterruptionSignal = SIGUSR1;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void usb_ffs_open_thread() {
|
|
|
|
adb_thread_setname("usb ffs open");
|
|
|
|
|
|
|
|
while (true) {
|
2019-06-24 23:35:35 +02:00
|
|
|
unique_fd control;
|
|
|
|
unique_fd bulk_out;
|
|
|
|
unique_fd bulk_in;
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
if (!open_functionfs(&control, &bulk_out, &bulk_in)) {
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
atransport* transport = new atransport();
|
|
|
|
transport->serial = "UsbFfs";
|
|
|
|
std::promise<void> destruction_notifier;
|
|
|
|
std::future<void> future = destruction_notifier.get_future();
|
|
|
|
transport->SetConnection(std::make_unique<UsbFfsConnection>(
|
2019-06-24 23:35:35 +02:00
|
|
|
std::move(control), std::move(bulk_out), std::move(bulk_in),
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
std::move(destruction_notifier)));
|
|
|
|
register_transport(transport);
|
|
|
|
future.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_init() {
|
2020-03-28 02:09:56 +01:00
|
|
|
std::thread(usb_ffs_open_thread).detach();
|
adbd: implement a nonblocking USB Connection.
Implement a Connection that implements a nonblocking interface to
functionfs, to replace the existing implementation that uses two
threads that loop and call read and write respectively. The existing
implementation is vulnerable to a race condition that can occur when a
connection is terminated, where one thread can notice failure and
complete reinitialization of the USB endpoints before the other thread
noticed anything went wrong, resulting in either the first packet
coming from the other end disappearing in to the void, or the other end
getting a packet of garbage.
As a side benefit, this improves performance on walleye from:
push 100MiB: 10 runs: median 49.48 MiB/s, mean 50.00 MiB/s, stddev: 2.77 MiB/s
pull 100MiB: 10 runs: median 75.82 MiB/s, mean 76.18 MiB/s, stddev: 6.60 MiB/s
to:
push 100MiB: 10 runs: median 73.90 MiB/s, mean 73.51 MiB/s, stddev: 5.26 MiB/s
pull 100MiB: 10 runs: median 105.90 MiB/s, mean 107.19 MiB/s, stddev: 6.10 MiB/s
Test: python test_device.py
Change-Id: I9b77c1057965edfef739ed9736e5d76613adf60a
2018-10-12 01:33:05 +02:00
|
|
|
}
|