Seperate LatencyStatistics from InputReader
Test: atest LatencyStatisticsTest
Change-Id: I22a39cd5bef7fa9180bc1ee1fd9478a2cf872e83
diff --git a/services/inputflinger/InputReader.cpp b/services/inputflinger/InputReader.cpp
index eee49d5..df5dcaf 100644
--- a/services/inputflinger/InputReader.cpp
+++ b/services/inputflinger/InputReader.cpp
@@ -85,9 +85,6 @@
// data.
static constexpr nsecs_t STYLUS_DATA_LATENCY = ms2ns(10);
-// How often to report input event statistics
-static constexpr nsecs_t STATISTICS_REPORT_FREQUENCY = seconds_to_nanoseconds(5 * 60);
-
// --- Static Functions ---
template<typename T>
@@ -4314,16 +4311,14 @@
}
void TouchInputMapper::reportEventForStatistics(nsecs_t evdevTime) {
- nsecs_t now = systemTime(CLOCK_MONOTONIC);
- nsecs_t latency = now - evdevTime;
- mStatistics.addValue(nanoseconds_to_microseconds(latency));
- nsecs_t timeSinceLastReport = now - mStatistics.lastReportTime;
- if (timeSinceLastReport > STATISTICS_REPORT_FREQUENCY) {
- android::util::stats_write(android::util::TOUCH_EVENT_REPORTED,
- mStatistics.min, mStatistics.max,
- mStatistics.mean(), mStatistics.stdev(), mStatistics.count);
- mStatistics.reset(now);
+ if (mStatistics.shouldReport()) {
+ android::util::stats_write(android::util::TOUCH_EVENT_REPORTED, mStatistics.getMin(),
+ mStatistics.getMax(), mStatistics.getMean(),
+ mStatistics.getStDev(), mStatistics.getCount());
+ mStatistics.reset();
}
+ nsecs_t latency = nanoseconds_to_microseconds(systemTime(CLOCK_MONOTONIC) - evdevTime);
+ mStatistics.addValue(latency);
}
void TouchInputMapper::process(const RawEvent* rawEvent) {
diff --git a/services/inputflinger/InputReader.h b/services/inputflinger/InputReader.h
index 0c08e7d..33763b6 100644
--- a/services/inputflinger/InputReader.h
+++ b/services/inputflinger/InputReader.h
@@ -24,14 +24,15 @@
#include <input/DisplayViewport.h>
#include <input/Input.h>
+#include <input/LatencyStatistics.h>
#include <input/VelocityControl.h>
#include <input/VelocityTracker.h>
#include <ui/DisplayInfo.h>
-#include <utils/KeyedVector.h>
+#include <utils/BitSet.h>
#include <utils/Condition.h>
+#include <utils/KeyedVector.h>
#include <utils/Mutex.h>
#include <utils/Timers.h>
-#include <utils/BitSet.h>
#include <optional>
#include <stddef.h>
@@ -569,69 +570,6 @@
}
};
-/**
- * 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 {
@@ -1571,8 +1509,10 @@
VelocityControl mWheelXVelocityControl;
VelocityControl mWheelYVelocityControl;
+ static constexpr std::chrono::duration STATS_REPORT_PERIOD = 5min;
+
// Latency statistics for touch events
- struct LatencyStatistics mStatistics;
+ LatencyStatistics mStatistics{STATS_REPORT_PERIOD};
std::optional<DisplayViewport> findViewport();