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

Commit 24878b59 authored by jioana's avatar jioana Committed by Ioana Jianu
Browse files

LatencyAggregatorWithHistograms for InputEventLatencyReported atom.

Currently, input latency metrics cannot be reported on a per-device/per
-source/per-input-event-type granular level. Moreover, regarding
implementation, the input event latencies are aggregated into KLL
sketches (per day) and serialized into the InputEventLatencySketch,
which is a pulled atom, using LatencyAggregator. This means that only
20,000 input event latencies can be recorded per day, the rest are
dropped.

We want to capture latency statistics that can be broken down by per
input device (characterized by its Product ID and Vendor ID), by
source(s) of the input event (such as stylus, touchpad, touchscreen) and
by specific types of input events (such as MOTION_ACTION_DOWN,
MOTION_ACTION_UP, MOTION_ACTION_MOVE, KEY).

The logic for per device input latency metrics lives in
LatencyAggregatorWithHistograms.
Once the flag is rolled out, this will replace LatencyAggregator.

Input events with the same identifier are mapped to an array of 7
histograms. Each histogram's bin sizes are custom to the latency stage
and input event type. When an input event timeline is received, the
relevant bin counter of the corresponding histogram is incremented.

The InputEventLatencyReported atom is pushed every 6 hours by calling
pushLatencyStatistics from InputDispatcher DispatchOnce. After a push,
histograms are cleared.

processSlowEvent was copied from LatencyAggregator to keep logging the
SLOW_iNPUT_EVENT_REPORTED atom.

Tests will be added in a separate CL.

Bug: 270049345
Test: atest inputflinger_tests
Test: manual tests with statsd_testdrive 932
Flag: com.android.input.flags.enable_per_device_input_latency_metrics
Change-Id: I64fb883f4a01889b3600043d21446613a5a5bce7
parent de3019c8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ filegroup {
        "InputState.cpp",
        "InputTarget.cpp",
        "LatencyAggregator.cpp",
        "LatencyAggregatorWithHistograms.cpp",
        "LatencyTracker.cpp",
        "Monitor.cpp",
        "TouchedWindow.cpp",
+32 −3
Original line number Diff line number Diff line
@@ -155,6 +155,10 @@ constexpr std::chrono::milliseconds SLOW_INTERCEPTION_THRESHOLD = 50ms;
// Number of recent events to keep for debugging purposes.
constexpr size_t RECENT_QUEUE_MAX_SIZE = 10;

// Interval at which we should push the atom gathering input event latencies in
// LatencyAggregatorWithHistograms
constexpr nsecs_t LATENCY_STATISTICS_PUSH_INTERVAL = 6 * 3600 * 1000000000LL; // 6 hours

// Event log tags. See EventLogTags.logtags for reference.
constexpr int LOGTAG_INPUT_INTERACTION = 62000;
constexpr int LOGTAG_INPUT_FOCUS = 62001;
@@ -944,8 +948,13 @@ InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
        mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
        mWindowTokenWithPointerCapture(nullptr),
        mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
        mLatencyAggregator(),
        mLatencyTracker(&mLatencyAggregator) {
        mInputEventTimelineProcessor(
                input_flags::enable_per_device_input_latency_metrics()
                        ? std::move(std::unique_ptr<InputEventTimelineProcessor>(
                                  new LatencyAggregatorWithHistograms()))
                        : std::move(std::unique_ptr<InputEventTimelineProcessor>(
                                  new LatencyAggregator()))),
        mLatencyTracker(*mInputEventTimelineProcessor) {
    mLooper = sp<Looper>::make(false);
    mReporter = createInputReporter();

@@ -1017,6 +1026,11 @@ void InputDispatcher::dispatchOnce() {
        const nsecs_t nextAnrCheck = processAnrsLocked();
        nextWakeupTime = std::min(nextWakeupTime, nextAnrCheck);

        if (input_flags::enable_per_device_input_latency_metrics()) {
            const nsecs_t nextStatisticsPush = processLatencyStatisticsLocked();
            nextWakeupTime = std::min(nextWakeupTime, nextStatisticsPush);
        }

        // We are about to enter an infinitely long sleep, because we have no commands or
        // pending or queued events
        if (nextWakeupTime == LLONG_MAX) {
@@ -1097,6 +1111,21 @@ nsecs_t InputDispatcher::processAnrsLocked() {
    return LLONG_MIN;
}

/**
 * Check if enough time has passed since the last latency statistics push.
 * Return the time at which we should wake up next.
 */
nsecs_t InputDispatcher::processLatencyStatisticsLocked() {
    const nsecs_t currentTime = now();
    // Log the atom recording latency statistics if more than 6 hours passed from the last
    // push
    if (currentTime - mLastStatisticPushTime >= LATENCY_STATISTICS_PUSH_INTERVAL) {
        mInputEventTimelineProcessor->pushLatencyStatistics();
        mLastStatisticPushTime = currentTime;
    }
    return mLastStatisticPushTime + LATENCY_STATISTICS_PUSH_INTERVAL;
}

std::chrono::nanoseconds InputDispatcher::getDispatchingTimeoutLocked(
        const std::shared_ptr<Connection>& connection) {
    if (connection->monitor) {
@@ -6055,7 +6084,7 @@ void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
    dump += StringPrintf(INDENT2 "KeyRepeatTimeout: %" PRId64 "ms\n",
                         ns2ms(mConfig.keyRepeatTimeout));
    dump += mLatencyTracker.dump(INDENT2);
    dump += mLatencyAggregator.dump(INDENT2);
    dump += mInputEventTimelineProcessor->dump(INDENT2);
    dump += INDENT "InputTracer: ";
    dump += mTracer == nullptr ? "Disabled" : "Enabled";
}
+4 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#include "InputTarget.h"
#include "InputThread.h"
#include "LatencyAggregator.h"
#include "LatencyAggregatorWithHistograms.h"
#include "LatencyTracker.h"
#include "Monitor.h"
#include "TouchState.h"
@@ -326,6 +327,7 @@ private:
    std::chrono::nanoseconds mMonitorDispatchingTimeout GUARDED_BY(mLock);

    nsecs_t processAnrsLocked() REQUIRES(mLock);
    nsecs_t processLatencyStatisticsLocked() REQUIRES(mLock);
    std::chrono::nanoseconds getDispatchingTimeoutLocked(
            const std::shared_ptr<Connection>& connection) REQUIRES(mLock);

@@ -697,7 +699,8 @@ private:
                                         DeviceId deviceId) const REQUIRES(mLock);

    // Statistics gathering.
    LatencyAggregator mLatencyAggregator GUARDED_BY(mLock);
    nsecs_t mLastStatisticPushTime = 0;
    std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock);
    LatencyTracker mLatencyTracker GUARDED_BY(mLock);
    void traceInboundQueueLengthLocked() REQUIRES(mLock);
    void traceOutboundQueueLength(const Connection& connection);
+9 −1
Original line number Diff line number Diff line
@@ -121,13 +121,21 @@ struct InputEventTimeline {
class InputEventTimelineProcessor {
protected:
    InputEventTimelineProcessor() {}
    virtual ~InputEventTimelineProcessor() {}

public:
    virtual ~InputEventTimelineProcessor() {}

    /**
     * Process the provided timeline
     */
    virtual void processTimeline(const InputEventTimeline& timeline) = 0;

    /**
     * Trigger a push for the input event latency statistics
     */
    virtual void pushLatencyStatistics() = 0;

    virtual std::string dump(const char* prefix) const = 0;
};

} // namespace inputdispatcher
+7 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ using android::base::StringPrintf;
using dist_proc::aggregation::KllQuantile;
using std::chrono_literals::operator""ms;

namespace {

// Convert the provided nanoseconds into hundreds of microseconds.
// Use hundreds of microseconds (as opposed to microseconds) to preserve space.
static inline int64_t ns2hus(nsecs_t nanos) {
@@ -74,6 +76,8 @@ static std::chrono::milliseconds getSlowEventMinReportingInterval() {
    return std::chrono::milliseconds(std::stoi(millis));
}

} // namespace

namespace android::inputdispatcher {

/**
@@ -125,6 +129,9 @@ void LatencyAggregator::processTimeline(const InputEventTimeline& timeline) {
    processSlowEvent(timeline);
}

// This version of LatencyAggregator doesn't push any atoms
void LatencyAggregator::pushLatencyStatistics() {}

void LatencyAggregator::processStatistics(const InputEventTimeline& timeline) {
    std::scoped_lock lock(mLock);
    // Before we do any processing, check that we have not yet exceeded MAX_SIZE
Loading