logd: Add Pid statistics

- Optional class of statistics for PID
- Enhance pidToName
- Enhanced uidToName
- Enhance pidToUid
- template sort and iteration

Bug: 19608965
Change-Id: I04a1f02e9851b62987f9b176908134e455f22d1d
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index accd660..6e5454a 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -17,7 +17,6 @@
 #include <algorithm> // std::max
 #include <fcntl.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -27,7 +26,8 @@
 
 #include "LogStatistics.h"
 
-LogStatistics::LogStatistics() {
+LogStatistics::LogStatistics()
+        : enable(false) {
     log_id_for_each(id) {
         mSizes[id] = 0;
         mElements[id] = 0;
@@ -36,8 +36,10 @@
     }
 }
 
+namespace android {
+
 // caller must own and free character string
-char *LogStatistics::pidToName(pid_t pid) {
+static char *pidToName(pid_t pid) {
     char *retval = NULL;
     if (pid == 0) { // special case from auditd for kernel
         retval = strdup("logd.auditd");
@@ -60,6 +62,8 @@
     return retval;
 }
 
+}
+
 void LogStatistics::add(LogBufferElement *e) {
     log_id_t log_id = e->getLogId();
     unsigned short size = e->getMsgLen();
@@ -81,6 +85,31 @@
 
     mSizesTotal[log_id] += size;
     ++mElementsTotal[log_id];
+
+    if (!enable) {
+        return;
+    }
+
+    pid_t pid = e->getPid();
+    hash = android::hash_type(pid);
+    index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        PidEntry initEntry(pid, uid, android::pidToName(pid));
+        initEntry.add(size);
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (entry.getUid() != uid) {
+            entry.setUid(uid);
+            entry.setName(android::pidToName(pid));
+        } else if (!entry.getName()) {
+            char *name = android::pidToName(pid);
+            if (name) {
+                entry.setName(name);
+            }
+        }
+        entry.add(size);
+    }
 }
 
 void LogStatistics::subtract(LogBufferElement *e) {
@@ -99,33 +128,20 @@
             table.removeAt(index);
         }
     }
-}
 
-// caller must own and delete UidEntry array
-const UidEntry **LogStatistics::sort(size_t n, log_id id) {
-    if (!n) {
-        return NULL;
+    if (!enable) {
+        return;
     }
 
-    const UidEntry **retval = new const UidEntry* [n];
-    memset(retval, 0, sizeof(*retval) * n);
-
-    uidTable_t &table = uidTable[id];
-    ssize_t index = -1;
-    while ((index = table.next(index)) >= 0) {
-        const UidEntry &entry = table.entryAt(index);
-        size_t s = entry.getSizes();
-        ssize_t i = n - 1;
-        while ((!retval[i] || (s > retval[i]->getSizes())) && (--i >= 0));
-        if (++i < (ssize_t)n) {
-            size_t b = n - i - 1;
-            if (b) {
-                memmove(&retval[i+1], &retval[i], b * sizeof(retval[0]));
-            }
-            retval[i] = &entry;
+    pid_t pid = e->getPid();
+    hash = android::hash_type(pid);
+    index = pidTable.find(-1, hash, pid);
+    if (index != -1) {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (entry.subtract(size)) {
+            pidTable.removeAt(index);
         }
     }
-    return retval;
 }
 
 // caller must own and free character string
@@ -145,8 +161,29 @@
         ++info;
     }
 
+    char *name = NULL;
+
+    // report uid -> pid(s) -> pidToName if unique
+    ssize_t index = -1;
+    while ((index = pidTable.next(index)) != -1) {
+        const PidEntry &entry = pidTable.entryAt(index);
+
+        if (entry.getUid() == uid) {
+            const char *n = entry.getName();
+
+            if (n) {
+                if (!name) {
+                    name = strdup(n);
+                } else if (strcmp(name, n)) {
+                    free(name);
+                    return NULL;
+                }
+            }
+        }
+    }
+
     // No one
-    return NULL;
+    return name;
 }
 
 static void format_line(android::String8 &output,
@@ -223,37 +260,23 @@
     // Report on Chattiest
 
     // Chattiest by application (UID)
+    static const size_t maximum_sorted_entries = 32;
     log_id_for_each(id) {
         if (!(logMask & (1 << id))) {
             continue;
         }
 
-        static const size_t maximum_sorted_entries = 32;
-        const UidEntry **sorted = sort(maximum_sorted_entries, id);
-
-        if (!sorted) {
-            continue;
-        }
-
-        bool print = false;
-        for(size_t index = 0; index < maximum_sorted_entries; ++index) {
+        bool headerPrinted = false;
+        std::unique_ptr<const UidEntry *[]> sorted = sort(maximum_sorted_entries, id);
+        ssize_t index = -1;
+        while ((index = uidTable_t::next(index, sorted, maximum_sorted_entries)) >= 0) {
             const UidEntry *entry = sorted[index];
-
-            if (!entry) {
-                break;
-            }
-
-            size_t sizes = entry->getSizes();
-            if (sizes < (65536/100)) {
-                break;
-            }
-
             uid_t u = entry->getKey();
             if ((uid != AID_ROOT) && (u != uid)) {
                 continue;
             }
 
-            if (!print) {
+            if (!headerPrinted) {
                 if (uid == AID_ROOT) {
                     output.appendFormat(
                         "\n\nChattiest UIDs in %s:\n",
@@ -266,7 +289,7 @@
                         "\n\nLogging for your UID in %s:\n",
                         android_log_id_to_name(id));
                 }
-                print = true;
+                headerPrinted = true;
             }
 
             android::String8 name("");
@@ -278,18 +301,62 @@
             }
 
             android::String8 size("");
-            size.appendFormat("%zu", sizes);
+            size.appendFormat("%zu", entry->getSizes());
 
             format_line(output, name, size);
         }
+    }
 
-        delete [] sorted;
+    if (enable) {
+        bool headerPrinted = false;
+        std::unique_ptr<const PidEntry *[]> sorted = pidTable.sort(maximum_sorted_entries);
+        ssize_t index = -1;
+        while ((index = pidTable.next(index, sorted, maximum_sorted_entries)) >= 0) {
+            const PidEntry *entry = sorted[index];
+            uid_t u = entry->getUid();
+            if ((uid != AID_ROOT) && (u != uid)) {
+                continue;
+            }
+
+            if (!headerPrinted) {
+                if (uid == AID_ROOT) {
+                    output.appendFormat("\n\nChattiest PIDs:\n");
+                } else {
+                    output.appendFormat("\n\nLogging for this PID:\n");
+                }
+                android::String8 name("  PID/UID");
+                android::String8 size("Size");
+                android::String8 pruned("Pruned");
+                format_line(output, name, size, pruned);
+                headerPrinted = true;
+            }
+
+            android::String8 name("");
+            name.appendFormat("%5u/%u", entry->getKey(), u);
+            const char *n = entry->getName();
+            if (n) {
+                name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", n);
+            } else {
+                char *un = uidToName(u);
+                if (un) {
+                    name.appendFormat("%*s%s", (int)std::max(12 - name.length(), (size_t)1), "", un);
+                    free(un);
+                }
+            }
+
+            android::String8 size("");
+            size.appendFormat("%zu", entry->getSizes());
+
+            format_line(output, name, size);
+        }
     }
 
     *buf = strdup(output.string());
 }
 
-uid_t LogStatistics::pidToUid(pid_t pid) {
+namespace android {
+
+uid_t pidToUid(pid_t pid) {
     char buffer[512];
     snprintf(buffer, sizeof(buffer), "/proc/%u/status", pid);
     FILE *fp = fopen(buffer, "r");
@@ -305,3 +372,52 @@
     }
     return getuid(); // associate this with the logger
 }
+
+}
+
+uid_t LogStatistics::pidToUid(pid_t pid) {
+    uid_t uid;
+    android::hash_t hash = android::hash_type(pid);
+    ssize_t index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        uid = android::pidToUid(pid);
+        PidEntry initEntry(pid, uid, android::pidToName(pid));
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        if (!entry.getName()) {
+            char *name = android::pidToName(pid);
+            if (name) {
+                entry.setName(name);
+            }
+        }
+        uid = entry.getUid();
+    }
+    return uid;
+}
+
+// caller must free character string
+char *LogStatistics::pidToName(pid_t pid) {
+    char *name;
+
+    android::hash_t hash = android::hash_type(pid);
+    ssize_t index = pidTable.find(-1, hash, pid);
+    if (index == -1) {
+        name = android::pidToName(pid);
+        PidEntry initEntry(pid, android::pidToUid(pid), name ? strdup(name) : NULL);
+        pidTable.add(hash, initEntry);
+    } else {
+        PidEntry &entry = pidTable.editEntryAt(index);
+        const char *n = entry.getName();
+        if (n) {
+            name = strdup(n);
+        } else {
+            name = android::pidToName(pid);
+            if (name) {
+                entry.setName(strdup(name));
+            }
+        }
+    }
+
+    return name;
+}