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

Commit b43ad44f authored by Josh Gao's avatar Josh Gao
Browse files

adb: fdevent: add fdevent_context_epoll.

Implement an epoll-backed fdevent_context, that reduces overhead when
we're polling many file descriptors. FdeventTest.smoke goes from ~5.2s
to ~3.3s when run on the host (after this patch's modification to change
it from chaining 10 file descriptors together to 512).

Test: adb_test on host
Test: adbd_test on blueline
Test: test_adb.py
Test: test_device.py
Change-Id: Iacf0093aa7bebea31e447c2cb012af72d8c3297e
parent e22cb9f3
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -142,6 +142,10 @@ libadb_posix_srcs = [
    "sysdeps/posix/network.cpp",
]

libadb_linux_srcs = [
    "fdevent/fdevent_epoll.cpp",
]

libadb_test_srcs = [
    "adb_io_test.cpp",
    "adb_listeners_test.cpp",
@@ -170,12 +174,11 @@ cc_library_host_static {

    target: {
        linux: {
            srcs: ["client/usb_linux.cpp"],
            srcs: ["client/usb_linux.cpp"] + libadb_linux_srcs,
        },
        darwin: {
            srcs: ["client/usb_osx.cpp"],
        },

        not_windows: {
            srcs: libadb_posix_srcs,
        },
@@ -342,7 +345,7 @@ cc_library_static {
    // libminadbd wants both, as it's used to build native tests.
    compile_multilib: "both",

    srcs: libadb_srcs + libadb_posix_srcs + [
    srcs: libadb_srcs + libadb_linux_srcs + libadb_posix_srcs + [
        "daemon/auth.cpp",
        "daemon/jdwp_service.cpp",
    ],
+11 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@

#include "adb_utils.h"
#include "fdevent.h"
#include "fdevent_epoll.h"
#include "fdevent_poll.h"

using namespace std::chrono_literals;
@@ -185,8 +186,16 @@ void fdevent_context::TerminateLoop() {
    Interrupt();
}

static std::unique_ptr<fdevent_context> fdevent_create_context() {
#if defined(__linux__)
    return std::make_unique<fdevent_context_epoll>();
#else
    return std::make_unique<fdevent_context_poll>();
#endif
}

static auto& g_ambient_fdevent_context =
        *new std::unique_ptr<fdevent_context>(new fdevent_context_poll());
        *new std::unique_ptr<fdevent_context>(fdevent_create_context());

static fdevent_context* fdevent_get_ambient() {
    return g_ambient_fdevent_context.get();
@@ -247,5 +256,5 @@ size_t fdevent_installed_count() {
}

void fdevent_reset() {
    g_ambient_fdevent_context.reset(new fdevent_context_poll());
    g_ambient_fdevent_context = fdevent_create_context();
}
+2 −0
Original line number Diff line number Diff line
@@ -60,6 +60,8 @@ struct fdevent_context {
    fdevent* Create(unique_fd fd, std::variant<fd_func, fd_func2> func, void* arg);

    // Deallocate an fdevent object, returning the file descriptor that was owned by it.
    // Note that this calls Set, which is a virtual method, so destructors that call this must be
    // final.
    unique_fd Destroy(fdevent* fde);

  protected:
+200 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 "fdevent_epoll.h"

#if defined(__linux__)

#include <sys/epoll.h>
#include <sys/eventfd.h>

#include <android-base/logging.h>
#include <android-base/threads.h>

#include "adb_unique_fd.h"
#include "fdevent.h"

static void fdevent_interrupt(int fd, unsigned, void*) {
    uint64_t buf;
    ssize_t rc = TEMP_FAILURE_RETRY(adb_read(fd, &buf, sizeof(buf)));
    if (rc == -1) {
        PLOG(FATAL) << "failed to read from fdevent interrupt fd";
    }
}

fdevent_context_epoll::fdevent_context_epoll() {
    epoll_fd_.reset(epoll_create1(EPOLL_CLOEXEC));

    unique_fd interrupt_fd(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK));
    if (interrupt_fd == -1) {
        PLOG(FATAL) << "failed to create fdevent interrupt eventfd";
    }

    unique_fd interrupt_fd_dup(fcntl(interrupt_fd.get(), F_DUPFD_CLOEXEC, 3));
    if (interrupt_fd_dup == -1) {
        PLOG(FATAL) << "failed to dup fdevent interrupt eventfd";
    }

    this->interrupt_fd_ = std::move(interrupt_fd_dup);
    fdevent* fde = this->Create(std::move(interrupt_fd), fdevent_interrupt, nullptr);
    CHECK(fde != nullptr);
    this->Add(fde, FDE_READ);
}

fdevent_context_epoll::~fdevent_context_epoll() {
    // Destroy calls virtual methods, but this class is final, so that's okay.
    this->Destroy(this->interrupt_fde_);
}

static epoll_event calculate_epoll_event(fdevent* fde) {
    epoll_event result;
    result.events = 0;
    if (fde->state & FDE_READ) {
        result.events |= EPOLLIN;
    }
    if (fde->state & FDE_WRITE) {
        result.events |= EPOLLOUT;
    }
    if (fde->state & FDE_ERROR) {
        result.events |= EPOLLERR;
    }
    result.events |= EPOLLRDHUP;
    result.data.ptr = fde;
    return result;
}

void fdevent_context_epoll::Register(fdevent* fde) {
    epoll_event ev = calculate_epoll_event(fde);
    if (epoll_ctl(epoll_fd_.get(), EPOLL_CTL_ADD, fde->fd.get(), &ev) != 0) {
        PLOG(FATAL) << "failed to register fd " << fde->fd.get() << " with epoll";
    }
}

void fdevent_context_epoll::Unregister(fdevent* fde) {
    if (epoll_ctl(epoll_fd_.get(), EPOLL_CTL_DEL, fde->fd.get(), nullptr) != 0) {
        PLOG(FATAL) << "failed to unregister fd " << fde->fd.get() << " with epoll";
    }
}

void fdevent_context_epoll::Set(fdevent* fde, unsigned events) {
    unsigned previous_state = fde->state;
    fde->state = events;

    // If the state is the same, or only differed by FDE_TIMEOUT, we don't need to modify epoll.
    if ((previous_state & ~FDE_TIMEOUT) == (events & ~FDE_TIMEOUT)) {
        return;
    }

    epoll_event ev = calculate_epoll_event(fde);
    if (epoll_ctl(epoll_fd_.get(), EPOLL_CTL_MOD, fde->fd.get(), &ev) != 0) {
        PLOG(FATAL) << "failed to modify fd " << fde->fd.get() << " with epoll";
    }
}

void fdevent_context_epoll::Loop() {
    main_thread_id_ = android::base::GetThreadId();

    std::vector<fdevent_event> fde_events;
    std::vector<epoll_event> epoll_events;
    epoll_events.resize(this->installed_fdevents_.size());

    while (true) {
        if (terminate_loop_) {
            break;
        }

        int rc = -1;
        while (rc == -1) {
            std::optional<std::chrono::milliseconds> timeout = CalculatePollDuration();
            int timeout_ms;
            if (!timeout) {
                timeout_ms = -1;
            } else {
                timeout_ms = timeout->count();
            }

            rc = epoll_wait(epoll_fd_.get(), epoll_events.data(), epoll_events.size(), timeout_ms);
            if (rc == -1 && errno != EINTR) {
                PLOG(FATAL) << "epoll_wait failed";
            }
        }

        auto post_poll = std::chrono::steady_clock::now();
        std::unordered_map<fdevent*, unsigned> event_map;
        for (int i = 0; i < rc; ++i) {
            fdevent* fde = static_cast<fdevent*>(epoll_events[i].data.ptr);

            unsigned events = 0;
            if (epoll_events[i].events & EPOLLIN) {
                CHECK(fde->state & FDE_READ);
                events |= FDE_READ;
            }
            if (epoll_events[i].events & EPOLLOUT) {
                CHECK(fde->state & FDE_WRITE);
                events |= FDE_WRITE;
            }
            if (epoll_events[i].events & (EPOLLERR | EPOLLHUP | EPOLLRDHUP)) {
                // We fake a read, as the rest of the code assumes that errors will
                // be detected at that point.
                events |= FDE_READ | FDE_ERROR;
            }

            event_map[fde] = events;
        }

        for (const auto& [fd, fde] : installed_fdevents_) {
            unsigned events = 0;
            if (auto it = event_map.find(fde); it != event_map.end()) {
                events = it->second;
            }

            if (events == 0) {
                if (fde->timeout) {
                    auto deadline = fde->last_active + *fde->timeout;
                    if (deadline < post_poll) {
                        events |= FDE_TIMEOUT;
                    }
                }
            }

            if (events != 0) {
                LOG(DEBUG) << dump_fde(fde) << " got events " << std::hex << std::showbase
                           << events;
                fde_events.push_back({fde, events});
                fde->last_active = post_poll;
            }
        }
        this->HandleEvents(std::move(fde_events));
        fde_events.clear();
    }

    main_thread_id_.reset();
}

size_t fdevent_context_epoll::InstalledCount() {
    // We always have an installed fde for interrupt.
    return this->installed_fdevents_.size() - 1;
}

void fdevent_context_epoll::Interrupt() {
    uint64_t i = 1;
    ssize_t rc = TEMP_FAILURE_RETRY(adb_write(this->interrupt_fd_, &i, sizeof(i)));
    if (rc != sizeof(i)) {
        PLOG(FATAL) << "failed to write to fdevent interrupt eventfd";
    }
}

#endif  // defined(__linux__)
+61 −0
Original line number Diff line number Diff line
#pragma once

/*
 * Copyright (C) 2019 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.
 */

#if defined(__linux__)

#include "sysdeps.h"

#include <sys/epoll.h>

#include <deque>
#include <list>
#include <mutex>
#include <unordered_map>

#include <android-base/thread_annotations.h>

#include "adb_unique_fd.h"
#include "fdevent.h"

struct fdevent_context_epoll final : public fdevent_context {
    fdevent_context_epoll();
    virtual ~fdevent_context_epoll();

    virtual void Register(fdevent* fde) final;
    virtual void Unregister(fdevent* fde) final;

    virtual void Set(fdevent* fde, unsigned events) final;

    virtual void Loop() final;
    size_t InstalledCount() final;

  protected:
    virtual void Interrupt() final;

  public:
    // All operations to fdevent should happen only in the main thread.
    // That's why we don't need a lock for fdevent.
    std::unordered_map<int, fdevent*> epoll_node_map_;
    std::list<fdevent*> pending_list_;

    unique_fd epoll_fd_;
    unique_fd interrupt_fd_;
    fdevent* interrupt_fde_ = nullptr;
};

#endif  // defined(__linux__)
Loading