InputTracer: Use InputThread instead of std::thread
We need to query PackageManager from the trace instace, so we will need
access to the JVM from the tracer thread. Since std::thread does not
have access to the JVM, use InputThread instead.
Bug: 210460522
Test: manual with perfetto
Change-Id: Ifb618765a269ae928d2310fc6af3daf55bf10dc9
diff --git a/services/inputflinger/dispatcher/trace/ThreadedBackend.cpp b/services/inputflinger/dispatcher/trace/ThreadedBackend.cpp
index a58d52a..25bc227 100644
--- a/services/inputflinger/dispatcher/trace/ThreadedBackend.cpp
+++ b/services/inputflinger/dispatcher/trace/ThreadedBackend.cpp
@@ -21,7 +21,6 @@
#include "InputTracingPerfettoBackend.h"
#include <android-base/logging.h>
-#include <utils/AndroidThreads.h>
namespace android::inputdispatcher::trace::impl {
@@ -39,7 +38,10 @@
template <typename Backend>
ThreadedBackend<Backend>::ThreadedBackend(Backend&& innerBackend)
- : mTracerThread(&ThreadedBackend::threadLoop, this), mBackend(std::move(innerBackend)) {}
+ : mTracerThread(
+ "InputTracer", [this]() { threadLoop(); },
+ [this]() { mThreadWakeCondition.notify_all(); }),
+ mBackend(std::move(innerBackend)) {}
template <typename Backend>
ThreadedBackend<Backend>::~ThreadedBackend() {
@@ -48,7 +50,6 @@
mThreadExit = true;
}
mThreadWakeCondition.notify_all();
- mTracerThread.join();
}
template <typename Backend>
@@ -74,38 +75,33 @@
template <typename Backend>
void ThreadedBackend<Backend>::threadLoop() {
- androidSetThreadName("InputTracer");
+ std::vector<TraceEntry> entries;
- std::vector<std::variant<TracedKeyEvent, TracedMotionEvent, WindowDispatchArgs>> events;
+ { // acquire lock
+ std::unique_lock lock(mLock);
+ base::ScopedLockAssertion assumeLocked(mLock);
- while (true) {
- { // acquire lock
- std::unique_lock lock(mLock);
- base::ScopedLockAssertion assumeLocked(mLock);
-
- // Wait until we need to process more events or exit.
- mThreadWakeCondition.wait(lock, [&]() REQUIRES(mLock) {
- return mThreadExit || !mQueue.empty();
- });
- if (mThreadExit) {
- return;
- }
-
- mQueue.swap(events);
- } // release lock
-
- // Trace the events into the backend without holding the lock to reduce the amount of
- // work performed in the critical section.
- for (const auto& event : events) {
- std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend.traceMotionEvent(e); },
- [&](const TracedKeyEvent& e) { mBackend.traceKeyEvent(e); },
- [&](const WindowDispatchArgs& args) {
- mBackend.traceWindowDispatch(args);
- }},
- event);
+ // Wait until we need to process more events or exit.
+ mThreadWakeCondition.wait(lock,
+ [&]() REQUIRES(mLock) { return mThreadExit || !mQueue.empty(); });
+ if (mThreadExit) {
+ return;
}
- events.clear();
+
+ mQueue.swap(entries);
+ } // release lock
+
+ // Trace the events into the backend without holding the lock to reduce the amount of
+ // work performed in the critical section.
+ for (const auto& entry : entries) {
+ std::visit(Visitor{[&](const TracedMotionEvent& e) { mBackend.traceMotionEvent(e); },
+ [&](const TracedKeyEvent& e) { mBackend.traceKeyEvent(e); },
+ [&](const WindowDispatchArgs& args) {
+ mBackend.traceWindowDispatch(args);
+ }},
+ entry);
}
+ entries.clear();
}
// Explicit template instantiation for the PerfettoBackend.
diff --git a/services/inputflinger/dispatcher/trace/ThreadedBackend.h b/services/inputflinger/dispatcher/trace/ThreadedBackend.h
index c42f896..5776cf9 100644
--- a/services/inputflinger/dispatcher/trace/ThreadedBackend.h
+++ b/services/inputflinger/dispatcher/trace/ThreadedBackend.h
@@ -16,11 +16,11 @@
#pragma once
+#include "InputThread.h"
#include "InputTracingPerfettoBackend.h"
#include <android-base/thread_annotations.h>
#include <mutex>
-#include <thread>
#include <variant>
#include <vector>
@@ -44,12 +44,12 @@
private:
std::mutex mLock;
- std::thread mTracerThread;
+ InputThread mTracerThread;
bool mThreadExit GUARDED_BY(mLock){false};
std::condition_variable mThreadWakeCondition;
Backend mBackend;
- std::vector<std::variant<TracedKeyEvent, TracedMotionEvent, WindowDispatchArgs>> mQueue
- GUARDED_BY(mLock);
+ using TraceEntry = std::variant<TracedKeyEvent, TracedMotionEvent, WindowDispatchArgs>;
+ std::vector<TraceEntry> mQueue GUARDED_BY(mLock);
using WindowDispatchArgs = InputTracingBackendInterface::WindowDispatchArgs;