summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2015-12-28 17:33:01 -0600
committerMark Salyzyn2015-12-29 13:34:29 -0600
commit10b82b683405e9ccd0cc162fa44a2ec6a9f26448 (patch)
treeb0e78abff45b56ca1c0611973d6aab7ac4b4c8a9 /logd/LogKlog.cpp
parentb5e821316da31d2f4d06c3dea74ed57098b69033 (diff)
downloadplatform-system-core-10b82b683405e9ccd0cc162fa44a2ec6a9f26448.tar.gz
platform-system-core-10b82b683405e9ccd0cc162fa44a2ec6a9f26448.tar.xz
platform-system-core-10b82b683405e9ccd0cc162fa44a2ec6a9f26448.zip
logd: isMonotonic improvements
Use 1972 as a right delineation. Otherwise use half way point between the monotonic and realtime. Treat correction factor as unsigned, ensure that any wrapping to a negative value is dropped or set to EPOCH. Acknowledge that we can get a more accurate time track by acquiring the time rather than relying on healthd timestamp. Bug: 26331432 Change-Id: I09075fca58676a30cf7d87baf2d4b0f53795abaa
Diffstat (limited to 'logd/LogKlog.cpp')
-rw-r--r--logd/LogKlog.cpp42
1 files changed, 26 insertions, 16 deletions
diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp
index db7e68218..ba0b6e5db 100644
--- a/logd/LogKlog.cpp
+++ b/logd/LogKlog.cpp
@@ -206,7 +206,10 @@ char *log_strntok_r(char *s, size_t *len, char **last, size_t *sublen) {
206 // NOTREACHED 206 // NOTREACHED
207} 207}
208 208
209log_time LogKlog::correction = log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC); 209log_time LogKlog::correction =
210 (log_time(CLOCK_REALTIME) < log_time(CLOCK_MONOTONIC))
211 ? log_time::EPOCH
212 : (log_time(CLOCK_REALTIME) - log_time(CLOCK_MONOTONIC));
210 213
211LogKlog::LogKlog(LogBuffer *buf, LogReader *reader, int fdWrite, int fdRead, bool auditd) : 214LogKlog::LogKlog(LogBuffer *buf, LogReader *reader, int fdWrite, int fdRead, bool auditd) :
212 SocketListener(fdRead, false), 215 SocketListener(fdRead, false),
@@ -272,7 +275,7 @@ void LogKlog::calculateCorrection(const log_time &monotonic,
272 size_t len) { 275 size_t len) {
273 log_time real; 276 log_time real;
274 const char *ep = real.strptime(real_string, "%Y-%m-%d %H:%M:%S.%09q UTC"); 277 const char *ep = real.strptime(real_string, "%Y-%m-%d %H:%M:%S.%09q UTC");
275 if (!ep || (ep > &real_string[len])) { 278 if (!ep || (ep > &real_string[len]) || (real > log_time(CLOCK_REALTIME))) {
276 return; 279 return;
277 } 280 }
278 // kernel report UTC, log_time::strptime is localtime from calendar. 281 // kernel report UTC, log_time::strptime is localtime from calendar.
@@ -283,8 +286,16 @@ void LogKlog::calculateCorrection(const log_time &monotonic,
283 memset(&tm, 0, sizeof(tm)); 286 memset(&tm, 0, sizeof(tm));
284 tm.tm_isdst = -1; 287 tm.tm_isdst = -1;
285 localtime_r(&now, &tm); 288 localtime_r(&now, &tm);
286 real.tv_sec += tm.tm_gmtoff; 289 if ((tm.tm_gmtoff < 0) && ((-tm.tm_gmtoff) > real.tv_sec)) {
287 correction = real - monotonic; 290 real = log_time::EPOCH;
291 } else {
292 real.tv_sec += tm.tm_gmtoff;
293 }
294 if (monotonic > real) {
295 correction = log_time::EPOCH;
296 } else {
297 correction = real - monotonic;
298 }
288} 299}
289 300
290static const char suspendStr[] = "PM: suspend entry "; 301static const char suspendStr[] = "PM: suspend entry ";
@@ -319,11 +330,11 @@ void LogKlog::sniffTime(log_time &now,
319 if (cp && (cp >= &(*buf)[len])) { 330 if (cp && (cp >= &(*buf)[len])) {
320 cp = NULL; 331 cp = NULL;
321 } 332 }
322 len -= cp - *buf;
323 if (cp) { 333 if (cp) {
324 static const char healthd[] = "healthd"; 334 static const char healthd[] = "healthd";
325 static const char battery[] = ": battery "; 335 static const char battery[] = ": battery ";
326 336
337 len -= cp - *buf;
327 if (len && isspace(*cp)) { 338 if (len && isspace(*cp)) {
328 ++cp; 339 ++cp;
329 --len; 340 --len;
@@ -347,16 +358,11 @@ void LogKlog::sniffTime(log_time &now,
347 && ((size_t)((b += sizeof(healthd) - 1) - cp) < len) 358 && ((size_t)((b += sizeof(healthd) - 1) - cp) < len)
348 && ((b = strnstr(b, len -= b - cp, battery))) 359 && ((b = strnstr(b, len -= b - cp, battery)))
349 && ((size_t)((b += sizeof(battery) - 1) - cp) < len)) { 360 && ((size_t)((b += sizeof(battery) - 1) - cp) < len)) {
350 len -= b - cp; 361 // NB: healthd is roughly 150us late, so we use it instead to
351 // NB: healthd is roughly 150us late, worth the price to deal with 362 // trigger a check for ntp-induced or hardware clock drift.
352 // ntp-induced or hardware clock drift. 363 log_time real(CLOCK_REALTIME);
353 // look for " 2???-??-?? ??:??:??.????????? ???" 364 log_time mono(CLOCK_MONOTONIC);
354 for (; len && *b && (*b != '\n'); ++b, --len) { 365 correction = (real < mono) ? log_time::EPOCH : (real - mono);
355 if ((b[0] == ' ') && (b[1] == '2') && (b[5] == '-')) {
356 calculateCorrection(now, b + 1, len - 1);
357 break;
358 }
359 }
360 } else if (((b = strnstr(cp, len, suspendedStr))) 366 } else if (((b = strnstr(cp, len, suspendedStr)))
361 && ((size_t)((b += sizeof(suspendStr) - 1) - cp) < len)) { 367 && ((size_t)((b += sizeof(suspendStr) - 1) - cp) < len)) {
362 len -= b - cp; 368 len -= b - cp;
@@ -371,7 +377,11 @@ void LogKlog::sniffTime(log_time &now,
371 real.tv_nsec += (*endp - '0') * multiplier; 377 real.tv_nsec += (*endp - '0') * multiplier;
372 } 378 }
373 if (reverse) { 379 if (reverse) {
374 correction -= real; 380 if (real > correction) {
381 correction = log_time::EPOCH;
382 } else {
383 correction -= real;
384 }
375 } else { 385 } else {
376 correction += real; 386 correction += real;
377 } 387 }