From 47aee33ad2d94baecb806993e2d9d0b8aea51f71 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 24 Oct 2024 14:00:13 -0700 Subject: [PATCH] sensord: fix timestamp race condition (#33867) Co-authored-by: Comma Device --- system/sensord/sensors_qcom2.cc | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/system/sensord/sensors_qcom2.cc b/system/sensord/sensors_qcom2.cc index 189da78099d888..55c8d2a4480a4c 100644 --- a/system/sensord/sensors_qcom2.cc +++ b/system/sensord/sensors_qcom2.cc @@ -39,6 +39,7 @@ void interrupt_loop(std::vector> sensors) { } } + uint64_t offset = 0; struct pollfd fd_list[1] = {0}; fd_list[0].fd = fd; fd_list[0].events = POLLIN | POLLPRI; @@ -68,9 +69,19 @@ void interrupt_loop(std::vector> sensors) { continue; } + uint64_t cur_offset = nanos_since_epoch() - nanos_since_boot(); + uint64_t diff = cur_offset > offset ? cur_offset - offset : offset - cur_offset; + if (diff > 1*1e6) { // 1ms + LOGW("time jumped: %lu %lu", cur_offset, offset); + offset = cur_offset; + + // we don't have a valid timestamp since the + // time jumped, so throw out this measurement. + continue; + } + int num_events = err / sizeof(*evdata); - uint64_t offset = nanos_since_epoch() - nanos_since_boot(); - uint64_t ts = evdata[num_events - 1].timestamp - offset; + uint64_t ts = evdata[num_events - 1].timestamp - cur_offset; for (auto &[sensor, msg_name] : sensors) { if (!sensor->has_interrupt_enabled()) {