Metrics for touch latency
Add some basic metrics for touch latency here.
Currently just measure the time from evdev to eventhub.
This will later become "hard interrupt" to eventhub, after we fix
b/119840121.
Bug: 111431676
Test: set reporting interval to 5 seconds instead of 5 minutes, then run
'statsd_testdrive 34' and touch the screen for a minute. Review the
results to ensure that times are reported as expected.
Change-Id: I6144aa0feab897a161f1ee058686dd0a790d17f1
diff --git a/services/inputflinger/InputReader.h b/services/inputflinger/InputReader.h
index 35f3c23..aaffce2 100644
--- a/services/inputflinger/InputReader.h
+++ b/services/inputflinger/InputReader.h
@@ -567,6 +567,69 @@
}
};
+/**
+ * Basic statistics information.
+ * Keep track of min, max, average, and standard deviation of the received samples.
+ * Used to report latency information about input events.
+ */
+struct LatencyStatistics {
+ float min;
+ float max;
+ // Sum of all samples
+ float sum;
+ // Sum of squares of all samples
+ float sum2;
+ // The number of samples
+ size_t count;
+ // The last time statistics were reported.
+ nsecs_t lastReportTime;
+
+ LatencyStatistics() {
+ reset(systemTime(SYSTEM_TIME_MONOTONIC));
+ }
+
+ inline void addValue(float x) {
+ if (x < min) {
+ min = x;
+ }
+ if (x > max) {
+ max = x;
+ }
+ sum += x;
+ sum2 += x * x;
+ count++;
+ }
+
+ // Get the average value. Should not be called if no samples have been added.
+ inline float mean() {
+ if (count == 0) {
+ return 0;
+ }
+ return sum / count;
+ }
+
+ // Get the standard deviation. Should not be called if no samples have been added.
+ inline float stdev() {
+ if (count == 0) {
+ return 0;
+ }
+ float average = mean();
+ return sqrt(sum2 / count - average * average);
+ }
+
+ /**
+ * Reset internal state. The variable 'when' is the time when the data collection started.
+ * Call this to start a new data collection window.
+ */
+ inline void reset(nsecs_t when) {
+ max = 0;
+ min = std::numeric_limits<float>::max();
+ sum = 0;
+ sum2 = 0;
+ count = 0;
+ lastReportTime = when;
+ }
+};
/* Keeps track of the state of single-touch protocol. */
class SingleTouchMotionAccumulator {
@@ -1511,6 +1574,9 @@
VelocityControl mWheelXVelocityControl;
VelocityControl mWheelYVelocityControl;
+ // Latency statistics for touch events
+ struct LatencyStatistics mStatistics;
+
std::optional<DisplayViewport> findViewport();
void resetExternalStylus();
@@ -1580,6 +1646,8 @@
static void assignPointerIds(const RawState* last, RawState* current);
+ void reportEventForStatistics(nsecs_t evdevTime);
+
const char* modeToString(DeviceMode deviceMode);
};