summaryrefslogtreecommitdiffstats
path: root/services/input/EventHub.cpp
diff options
context:
space:
mode:
authorJeff Brown <jeffbrown@google.com>2012-10-05 17:59:56 -0700
committerJeff Brown <jeffbrown@google.com>2012-10-05 19:33:28 -0700
commitf33b2b2b2483fa824c650b281159ca62e1d0123a (patch)
treefbad3fecf3697d311bbbe16746d046a7696ca343 /services/input/EventHub.cpp
parentf948500239803b2b7247ff5058e39e4d645c5b70 (diff)
downloadframeworks_base-f33b2b2b2483fa824c650b281159ca62e1d0123a.zip
frameworks_base-f33b2b2b2483fa824c650b281159ca62e1d0123a.tar.gz
frameworks_base-f33b2b2b2483fa824c650b281159ca62e1d0123a.tar.bz2
Recover from bad input event timestamps from the kernel.
This can happen due to a race when the input device is opened or if the kernel happens to be missing the required Android patches to set the timestamp correctly. Bug: 7291243 Change-Id: If4319440eaff2889147c86296abd39efc5664346
Diffstat (limited to 'services/input/EventHub.cpp')
-rw-r--r--services/input/EventHub.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/services/input/EventHub.cpp b/services/input/EventHub.cpp
index f80ac18..1c9520d 100644
--- a/services/input/EventHub.cpp
+++ b/services/input/EventHub.cpp
@@ -787,6 +787,40 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
+ nsecs_t(iev.time.tv_usec) * 1000LL;
ALOGV("event time %lld, now %lld", event->when, now);
+
+ // Bug 7291243: Add a guard in case the kernel generates timestamps
+ // that appear to be far into the future because they were generated
+ // using the wrong clock source.
+ //
+ // This can happen because when the input device is initially opened
+ // it has a default clock source of CLOCK_REALTIME. Any input events
+ // enqueued right after the device is opened will have timestamps
+ // generated using CLOCK_REALTIME. We later set the clock source
+ // to CLOCK_MONOTONIC but it is already too late.
+ //
+ // Invalid input event timestamps can result in ANRs, crashes and
+ // and other issues that are hard to track down. We must not let them
+ // propagate through the system.
+ //
+ // Log a warning so that we notice the problem and recover gracefully.
+ if (event->when >= now + 10 * 1000000000LL) {
+ // Double-check. Time may have moved on.
+ nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC);
+ if (event->when > time) {
+ ALOGW("An input event from %s has a timestamp that appears to "
+ "have been generated using the wrong clock source "
+ "(expected CLOCK_MONOTONIC): "
+ "event time %lld, current time %lld, call time %lld. "
+ "Using current time instead.",
+ device->path.string(), event->when, time, now);
+ event->when = time;
+ } else {
+ ALOGV("Event time is ok but failed the fast path and required "
+ "an extra call to systemTime: "
+ "event time %lld, current time %lld, call time %lld.",
+ event->when, time, now);
+ }
+ }
#else
event->when = now;
#endif