Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1ce99576 authored by Josh Gao's avatar Josh Gao
Browse files

adb: switch apacket payload to a type that doesn't initialize its contents.

Switch from using std::string as the type we use to hold our payload in
apacket to a custom reimplementation that doesn't zero initialize. This
improves bulk transfer throughput in the adb_benchmark microbenchmark
on walleye by ~20%.

Test: adb shell taskset f0 /data/benchmarktest64/adb_benchmark/adb_benchmark
Change-Id: Ibad797701eb1460c9321b0400c5b167b89b2b4d0
parent 1b86d41c
Loading
Loading
Loading
Loading
+11 −5
Original line number Diff line number Diff line
@@ -257,7 +257,7 @@ void send_connect(atransport* t) {
                   << connection_str.length() << ")";
    }

    cp->payload = std::move(connection_str);
    cp->payload.assign(connection_str.begin(), connection_str.end());
    cp->msg.data_length = cp->payload.size();

    send_packet(cp, t);
@@ -329,7 +329,8 @@ static void handle_new_connection(atransport* t, apacket* p) {
    handle_offline(t);

    t->update_version(p->msg.arg0, p->msg.arg1);
    parse_banner(p->payload, t);
    std::string banner(p->payload.begin(), p->payload.end());
    parse_banner(banner, t);

#if ADB_HOST
    handle_online(t);
@@ -369,8 +370,10 @@ void handle_packet(apacket *p, atransport *t)
                send_auth_response(p->payload.data(), p->msg.data_length, t);
                break;
#else
            case ADB_AUTH_SIGNATURE:
                if (adbd_auth_verify(t->token, sizeof(t->token), p->payload)) {
            case ADB_AUTH_SIGNATURE: {
                // TODO: Switch to string_view.
                std::string signature(p->payload.begin(), p->payload.end());
                if (adbd_auth_verify(t->token, sizeof(t->token), signature)) {
                    adbd_auth_verified(t);
                    t->failed_auth_attempts = 0;
                } else {
@@ -378,6 +381,7 @@ void handle_packet(apacket *p, atransport *t)
                    send_auth_request(t);
                }
                break;
            }

            case ADB_AUTH_RSAPUBLICKEY:
                adbd_auth_confirm_key(p->payload.data(), p->msg.data_length, t);
@@ -392,7 +396,9 @@ void handle_packet(apacket *p, atransport *t)

    case A_OPEN: /* OPEN(local-id, 0, "destination") */
        if (t->online && p->msg.arg0 != 0 && p->msg.arg1 == 0) {
            asocket* s = create_local_service_socket(p->payload.c_str(), t);
            // TODO: Switch to string_view.
            std::string address(p->payload.begin(), p->payload.end());
            asocket* s = create_local_service_socket(address.c_str(), t);
            if (s == nullptr) {
                send_close(0, p->msg.arg0, t);
            } else {
+1 −14
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "adb_trace.h"
#include "fdevent.h"
#include "socket.h"
#include "types.h"
#include "usb.h"

constexpr size_t MAX_PAYLOAD_V1 = 4 * 1024;
@@ -63,20 +64,6 @@ std::string adb_version();
using TransportId = uint64_t;
class atransport;

struct amessage {
    uint32_t command;     /* command identifier constant      */
    uint32_t arg0;        /* first argument                   */
    uint32_t arg1;        /* second argument                  */
    uint32_t data_length; /* length of payload (0 is allowed) */
    uint32_t data_check;  /* checksum of data payload         */
    uint32_t magic;       /* command ^ 0xffffffff             */
};

struct apacket {
    amessage msg;
    std::string payload;
};

uint32_t calculate_apacket_checksum(const apacket* packet);

/* the adisconnect structure is used to record a callback that
+2 −4
Original line number Diff line number Diff line
@@ -454,10 +454,8 @@ static void send_auth_publickey(atransport* t) {
    p->msg.command = A_AUTH;
    p->msg.arg0 = ADB_AUTH_RSAPUBLICKEY;

    p->payload = std::move(key);

    // adbd expects a null-terminated string.
    p->payload.push_back('\0');
    p->payload.assign(key.data(), key.data() + key.size() + 1);
    p->msg.data_length = p->payload.size();
    send_packet(p, t);
}
@@ -482,7 +480,7 @@ void send_auth_response(const char* token, size_t token_size, atransport* t) {

    p->msg.command = A_AUTH;
    p->msg.arg0 = ADB_AUTH_SIGNATURE;
    p->payload = std::move(result);
    p->payload.assign(result.begin(), result.end());
    p->msg.data_length = p->payload.size();
    send_packet(p, t);
}
+6 −5
Original line number Diff line number Diff line
@@ -459,7 +459,7 @@ static void jdwp_socket_close(asocket* s) {
    delete s;
}

static int jdwp_socket_enqueue(asocket* s, std::string) {
static int jdwp_socket_enqueue(asocket* s, apacket::payload_type) {
    /* you can't write to this asocket */
    D("LS(%d): JDWP socket received data?", s->id);
    s->peer->close(s->peer);
@@ -474,7 +474,7 @@ static void jdwp_socket_ready(asocket* s) {
     * on the second one, close the connection
     */
    if (!jdwp->pass) {
        std::string data;
        apacket::payload_type data;
        data.resize(s->get_max_payload());
        size_t len = jdwp_process_list(&data[0], data.size());
        data.resize(len);
@@ -521,7 +521,8 @@ static void jdwp_process_list_updated(void) {
    for (auto& t : _jdwp_trackers) {
        if (t->peer) {
            // The tracker might not have been connected yet.
            t->peer->enqueue(t->peer, data);
            apacket::payload_type payload(data.begin(), data.end());
            t->peer->enqueue(t->peer, std::move(payload));
        }
    }
}
@@ -547,7 +548,7 @@ static void jdwp_tracker_ready(asocket* s) {
    JdwpTracker* t = (JdwpTracker*)s;

    if (t->need_initial) {
        std::string data;
        apacket::payload_type data;
        data.resize(s->get_max_payload());
        data.resize(jdwp_process_list_msg(&data[0], data.size()));
        t->need_initial = false;
@@ -555,7 +556,7 @@ static void jdwp_tracker_ready(asocket* s) {
    }
}

static int jdwp_tracker_enqueue(asocket* s, std::string) {
static int jdwp_tracker_enqueue(asocket* s, apacket::payload_type) {
    /* you can't write to this socket */
    D("LS(%d): JDWP tracker received data?", s->id);
    s->peer->close(s->peer);

adb/range.h

deleted100644 → 0
+0 −65
Original line number Diff line number Diff line
#pragma once

/*
 * 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.
 */

#include <string>

#include <android-base/logging.h>

struct Range {
    explicit Range(std::string data) : data_(std::move(data)) {}

    Range(const Range& copy) = delete;
    Range& operator=(const Range& copy) = delete;

    Range(Range&& move) = default;
    Range& operator=(Range&& move) = default;

    bool empty() const {
        return size() == 0;
    }

    size_t size() const {
        return data_.size() - begin_offset_ - end_offset_;
    };

    void drop_front(size_t n) {
        CHECK_GE(size(), n);
        begin_offset_ += n;
    }

    void drop_end(size_t n) {
        CHECK_GE(size(), n);
        end_offset_ += n;
    }

    char* data() {
        return &data_[0] + begin_offset_;
    }

    std::string::iterator begin() {
        return data_.begin() + begin_offset_;
    }

    std::string::iterator end() {
        return data_.end() - end_offset_;
    }

    std::string data_;
    size_t begin_offset_ = 0;
    size_t end_offset_ = 0;
};
Loading