logd: prune more aggressively when over the top

(cherry picked from commit 63c15d50615a4c9e9ad25f601ef9dcb1161d8990)

Change-Id: I929dddc7da048c032fb791c7af23f215f8856bf3
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 8b273e2..64d5cc2 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -99,12 +99,20 @@
 }
 
 // If we're using more than 256K of memory for log entries, prune
-// 10% of the log entries.
+// at least 10% of the log entries.
 //
 // mLogElementsLock must be held when this function is called.
 void LogBuffer::maybePrune(log_id_t id) {
-    if (mSizes[id] > LOG_BUFFER_SIZE) {
-        prune(id, mElements[id] / 10);
+    unsigned long sizes = mSizes[id];
+    if (sizes > LOG_BUFFER_SIZE) {
+        unsigned long sizeOver90Percent = sizes - ((LOG_BUFFER_SIZE * 9) / 10);
+        unsigned long elements = mElements[id];
+        unsigned long pruneRows = elements * sizeOver90Percent / sizes;
+        elements /= 10;
+        if (pruneRows <= elements) {
+            pruneRows = elements;
+        }
+        prune(id, pruneRows);
     }
 }