Do not report latency for injected events
When reporting the latency of touch events, ensure that injected events
are excluded. These events can have arbitrary timestamps, and would not
result in meaningful data. Also do not report events for statistics if
inputfilter is enabled.
Move the statistics reporting from InputTransport to InputDispatcher.
This ensures that there's only 1 instance of the mStatistics object.
This also provides easy access to the inputfilterenabled state.
Bug: 13894199
Test: Change the reporting period to 0 (to report every event immediately)
Inject events in various ways and ensure they don't go to statsd
$ m statsd_testdrive && ./out/host/linux-x86/bin/statsd_testdrive 34
$ adb shell input tap 100 100
$ adb shell monkey 1000
Next, relaunch the statsd_testdrive script and touch the screen
Observe that events are reported.
Change-Id: Ief8040599a347e084e75584ed3164c60a6dbc4ad
diff --git a/services/inputflinger/Android.bp b/services/inputflinger/Android.bp
index 11578c3..f6b5935 100644
--- a/services/inputflinger/Android.bp
+++ b/services/inputflinger/Android.bp
@@ -44,6 +44,7 @@
"libhidlbase",
"libinput",
"liblog",
+ "libstatslog",
"libutils",
"libui",
"server_configurable_flags",
diff --git a/services/inputflinger/dispatcher/Android.bp b/services/inputflinger/dispatcher/Android.bp
index b8c3a80..9185e00 100644
--- a/services/inputflinger/dispatcher/Android.bp
+++ b/services/inputflinger/dispatcher/Android.bp
@@ -34,6 +34,7 @@
"libinputreporter",
"libinputflinger_base",
"liblog",
+ "libstatslog",
"libui",
"libutils",
],
diff --git a/services/inputflinger/dispatcher/Entry.h b/services/inputflinger/dispatcher/Entry.h
index a9e22f1..28c2799 100644
--- a/services/inputflinger/dispatcher/Entry.h
+++ b/services/inputflinger/dispatcher/Entry.h
@@ -29,6 +29,9 @@
namespace android::inputdispatcher {
+// Sequence number for synthesized or injected events.
+constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0;
+
struct EventEntry {
enum { TYPE_CONFIGURATION_CHANGED, TYPE_DEVICE_RESET, TYPE_KEY, TYPE_MOTION };
@@ -41,8 +44,23 @@
bool dispatchInProgress; // initially false, set to true while dispatching
+ /**
+ * Injected keys are events from an external (probably untrusted) application
+ * and are not related to real hardware state. They come in via
+ * InputDispatcher::injectInputEvent, which sets policy flag POLICY_FLAG_INJECTED.
+ */
inline bool isInjected() const { return injectionState != nullptr; }
+ /**
+ * Synthesized events are either injected events, or events that come
+ * from real hardware, but aren't directly attributable to a specific hardware event.
+ * Key repeat is a synthesized event, because it is related to an actual hardware state
+ * (a key is currently pressed), but the repeat itself is generated by the framework.
+ */
+ inline bool isSynthesized() const {
+ return isInjected() || sequenceNum == SYNTHESIZED_EVENT_SEQUENCE_NUM;
+ }
+
void release();
virtual void appendDescription(std::string& msg) const = 0;
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 039462e..2361867 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -50,6 +50,7 @@
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
+#include <statslog.h>
#include <stddef.h>
#include <time.h>
#include <unistd.h>
@@ -2274,6 +2275,7 @@
motionEntry->downTime, motionEntry->eventTime,
motionEntry->pointerCount,
motionEntry->pointerProperties, usingCoords);
+ reportTouchEventForStatistics(*motionEntry);
break;
}
@@ -4478,6 +4480,34 @@
// TODO Write some statistics about how long we spend waiting.
}
+/**
+ * Report the touch event latency to the statsd server.
+ * Input events are reported for statistics if:
+ * - This is a touchscreen event
+ * - InputFilter is not enabled
+ * - Event is not injected or synthesized
+ *
+ * Statistics should be reported before calling addValue, to prevent a fresh new sample
+ * from getting aggregated with the "old" data.
+ */
+void InputDispatcher::reportTouchEventForStatistics(const MotionEntry& motionEntry)
+ REQUIRES(mLock) {
+ const bool reportForStatistics = (motionEntry.source == AINPUT_SOURCE_TOUCHSCREEN) &&
+ !(motionEntry.isSynthesized()) && !mInputFilterEnabled;
+ if (!reportForStatistics) {
+ return;
+ }
+
+ if (mTouchStatistics.shouldReport()) {
+ android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mTouchStatistics.getMin(),
+ mTouchStatistics.getMax(), mTouchStatistics.getMean(),
+ mTouchStatistics.getStDev(), mTouchStatistics.getCount());
+ mTouchStatistics.reset();
+ }
+ const float latencyMicros = nanoseconds_to_microseconds(now() - motionEntry.eventTime);
+ mTouchStatistics.addValue(latencyMicros);
+}
+
void InputDispatcher::traceInboundQueueLengthLocked() {
if (ATRACE_ENABLED()) {
ATRACE_INT("iq", mInboundQueue.size());
diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h
index f2c0402..0d9d6b0 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.h
+++ b/services/inputflinger/dispatcher/InputDispatcher.h
@@ -33,6 +33,7 @@
#include <input/InputApplication.h>
#include <input/InputTransport.h>
#include <input/InputWindow.h>
+#include <input/LatencyStatistics.h>
#include <limits.h>
#include <stddef.h>
#include <ui/Region.h>
@@ -455,6 +456,10 @@
void doOnPointerDownOutsideFocusLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
// Statistics gathering.
+ static constexpr std::chrono::duration TOUCH_STATS_REPORT_PERIOD = 5min;
+ LatencyStatistics mTouchStatistics{TOUCH_STATS_REPORT_PERIOD};
+
+ void reportTouchEventForStatistics(const MotionEntry& entry);
void updateDispatchStatistics(nsecs_t currentTime, const EventEntry* entry,
int32_t injectionResult, nsecs_t timeSpentWaitingForApplication);
void traceInboundQueueLengthLocked() REQUIRES(mLock);
diff --git a/services/inputflinger/dispatcher/InputState.h b/services/inputflinger/dispatcher/InputState.h
index bccef0f..47e9b36 100644
--- a/services/inputflinger/dispatcher/InputState.h
+++ b/services/inputflinger/dispatcher/InputState.h
@@ -24,9 +24,6 @@
namespace android::inputdispatcher {
-// Sequence number for synthesized or injected events.
-constexpr uint32_t SYNTHESIZED_EVENT_SEQUENCE_NUM = 0;
-
/* Tracks dispatched key and motion event state so that cancellation events can be
* synthesized when events are dropped. */
class InputState {