From 8e8e8db549ffa6e692246abce6f5560c3a1e93c2 Mon Sep 17 00:00:00 2001 From: Dragoslav Mitrinovic Date: Thu, 15 Jan 2015 09:29:43 -0600 Subject: [PATCH] logd: fix persistent blocking reader performance logd suffers major performance degradation when persistent (blocking) client reader connects to it (e.g. logcat). The root cause of the degradation is that each time when reader is notified of the arrival of new log entries, the reader commences its search for the new entries from the beginning of the linked list (oldest entries first). This commit alters the search to start from the end of the linked list and work backwards. This dramatically decreases logd CPU consumption when blocking reader is connected, and increases the maximum logging throughput (before the logs start getting lost) by a factor ~ 20. Change-Id: Ib60955ce05544e52a8b24acc3dcf5863e1e39c5c --- logd/LogBuffer.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp index 6307bed63..3d0b38f8f 100644 --- a/logd/LogBuffer.cpp +++ b/logd/LogBuffer.cpp @@ -445,7 +445,24 @@ log_time LogBuffer::flushTo( uid_t uid = reader->getUid(); pthread_mutex_lock(&mLogElementsLock); - for (it = mLogElements.begin(); it != mLogElements.end(); ++it) { + + if (start == LogTimeEntry::EPOCH) { + // client wants to start from the beginning + it = mLogElements.begin(); + } else { + // Client wants to start from some specified time. Chances are + // we are better off starting from the end of the time sorted list. + for (it = mLogElements.end(); it != mLogElements.begin(); /* do nothing */) { + --it; + LogBufferElement *element = *it; + if (element->getMonotonicTime() <= start) { + it++; + break; + } + } + } + + for (; it != mLogElements.end(); ++it) { LogBufferElement *element = *it; if (!privileged && (element->getUid() != uid)) {