Skip to content

Commit 4d12d5b

Browse files
Jeff BrownAndroid (Google) Code Review
authored andcommitted
Merge "Recover from bad input event timestamps from the kernel." into jb-mr1-dev
2 parents b71ccfc + f33b2b2 commit 4d12d5b

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

services/input/EventHub.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,40 @@ size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSiz
787787
event->when = nsecs_t(iev.time.tv_sec) * 1000000000LL
788788
+ nsecs_t(iev.time.tv_usec) * 1000LL;
789789
ALOGV("event time %lld, now %lld", event->when, now);
790+
791+
// Bug 7291243: Add a guard in case the kernel generates timestamps
792+
// that appear to be far into the future because they were generated
793+
// using the wrong clock source.
794+
//
795+
// This can happen because when the input device is initially opened
796+
// it has a default clock source of CLOCK_REALTIME. Any input events
797+
// enqueued right after the device is opened will have timestamps
798+
// generated using CLOCK_REALTIME. We later set the clock source
799+
// to CLOCK_MONOTONIC but it is already too late.
800+
//
801+
// Invalid input event timestamps can result in ANRs, crashes and
802+
// and other issues that are hard to track down. We must not let them
803+
// propagate through the system.
804+
//
805+
// Log a warning so that we notice the problem and recover gracefully.
806+
if (event->when >= now + 10 * 1000000000LL) {
807+
// Double-check. Time may have moved on.
808+
nsecs_t time = systemTime(SYSTEM_TIME_MONOTONIC);
809+
if (event->when > time) {
810+
ALOGW("An input event from %s has a timestamp that appears to "
811+
"have been generated using the wrong clock source "
812+
"(expected CLOCK_MONOTONIC): "
813+
"event time %lld, current time %lld, call time %lld. "
814+
"Using current time instead.",
815+
device->path.string(), event->when, time, now);
816+
event->when = time;
817+
} else {
818+
ALOGV("Event time is ok but failed the fast path and required "
819+
"an extra call to systemTime: "
820+
"event time %lld, current time %lld, call time %lld.",
821+
event->when, time, now);
822+
}
823+
}
790824
#else
791825
event->when = now;
792826
#endif

services/input/InputReader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,8 +956,9 @@ void InputDevice::process(const RawEvent* rawEvents, size_t count) {
956956
size_t numMappers = mMappers.size();
957957
for (const RawEvent* rawEvent = rawEvents; count--; rawEvent++) {
958958
#if DEBUG_RAW_EVENTS
959-
ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x",
960-
rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value);
959+
ALOGD("Input event: device=%d type=0x%04x code=0x%04x value=0x%08x when=%lld",
960+
rawEvent->deviceId, rawEvent->type, rawEvent->code, rawEvent->value,
961+
rawEvent->when);
961962
#endif
962963

963964
if (mDropUntilNextSync) {

0 commit comments

Comments
 (0)