logd: cleanup

- simplify access and control exposure to class list
- indent
- compile warning
- Follow standard naming convention for variables and methods
- merge common fragments
- Side Effects: none

Change-Id: I74796043ac34753c6dd10018719ebc0bcd94e010
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index cd9ea20..8c1c344 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -93,9 +93,9 @@
 }
 
 LogBuffer::LogBuffer(LastLogTimes *times)
-        : mTimes(*times) {
+        : dgramQlenStatistics(false)
+        , mTimes(*times) {
     pthread_mutex_init(&mLogElementsLock, NULL);
-    dgram_qlen_statistics = false;
 
     static const char global_tuneable[] = "persist.logd.size"; // Settings App
     static const char global_default[] = "ro.logd.size";       // BoardConfig.mk
@@ -150,10 +150,10 @@
     while (--it != mLogElements.begin()) {
         if ((*it)->getRealTime() <= realtime) {
             // halves the peak performance, use with caution
-            if (dgram_qlen_statistics) {
+            if (dgramQlenStatistics) {
                 LogBufferElementCollection::iterator ib = it;
                 unsigned short buckets, num = 1;
-                for (unsigned short i = 0; (buckets = stats.dgram_qlen(i)); ++i) {
+                for (unsigned short i = 0; (buckets = stats.dgramQlen(i)); ++i) {
                     buckets -= num;
                     num += buckets;
                     while (buckets && (--ib != mLogElements.begin())) {
@@ -267,8 +267,7 @@
 
             if (uid == caller_uid) {
                 it = mLogElements.erase(it);
-                unsigned short len = e->getMsgLen();
-                stats.subtract(len, id, uid, e->getPid());
+                stats.subtract(e->getMsgLen(), id, uid, e->getPid());
                 delete e;
                 pruneRows--;
                 if (pruneRows == 0) {
@@ -318,23 +317,19 @@
 
             uid_t uid = e->getUid();
 
-            if (uid == worst) {
+            if ((uid == worst) || mPrune.naughty(e)) { // Worst or BlackListed
                 it = mLogElements.erase(it);
                 unsigned short len = e->getMsgLen();
-                stats.subtract(len, id, worst, e->getPid());
-                delete e;
-                kick = true;
-                pruneRows--;
-                if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
-                    break;
-                }
-                worst_sizes -= len;
-            } else if (mPrune.naughty(e)) { // BlackListed
-                it = mLogElements.erase(it);
-                stats.subtract(e->getMsgLen(), id, uid, e->getPid());
+                stats.subtract(len, id, uid, e->getPid());
                 delete e;
                 pruneRows--;
-                if (pruneRows == 0) {
+                if (uid == worst) {
+                    kick = true;
+                    if ((pruneRows == 0) || (worst_sizes < second_worst_sizes)) {
+                        break;
+                    }
+                    worst_sizes -= len;
+                } else if (pruneRows == 0) {
                     break;
                 }
             } else {
diff --git a/logd/LogBuffer.h b/logd/LogBuffer.h
index 4b982a8..91175e0 100644
--- a/logd/LogBuffer.h
+++ b/logd/LogBuffer.h
@@ -37,8 +37,7 @@
     pthread_mutex_t mLogElementsLock;
 
     LogStatistics stats;
-
-    bool dgram_qlen_statistics;
+    bool dgramQlenStatistics;
 
     PruneList mPrune;
 
@@ -66,7 +65,7 @@
 
     void enableDgramQlenStatistics() {
         stats.enableDgramQlenStatistics();
-        dgram_qlen_statistics = true;
+        dgramQlenStatistics = true;
     }
 
     int initPrune(char *cp) { return mPrune.init(cp); }
diff --git a/logd/LogStatistics.cpp b/logd/LogStatistics.cpp
index 82f9165..b887ce9 100644
--- a/logd/LogStatistics.cpp
+++ b/logd/LogStatistics.cpp
@@ -51,10 +51,7 @@
 }
 
 bool PidStatistics::pidGone() {
-    if (mGone) {
-        return true;
-    }
-    if (pid == gone) {
+    if (mGone || (pid == gone)) {
         return true;
     }
     if (kill(pid, 0) && (errno != EPERM)) {
@@ -90,6 +87,9 @@
 }
 
 // must call free to release return value
+//  If only we could sniff our own logs for:
+//   <time> <pid> <pid> E AndroidRuntime: Process: <name>, PID: <pid>
+//  which debuggerd prints as a process is crashing.
 char *PidStatistics::pidToName(pid_t pid) {
     char *retval = NULL;
     if (pid != gone) {
@@ -122,7 +122,7 @@
     PidStatisticsCollection::iterator it;
     for (it = begin(); it != end();) {
         delete (*it);
-        it = Pids.erase(it);
+        it = erase(it);
     }
 }
 
@@ -130,7 +130,7 @@
     mSizes += size;
     ++mElements;
 
-    PidStatistics *p;
+    PidStatistics *p = NULL;
     PidStatisticsCollection::iterator last;
     PidStatisticsCollection::iterator it;
     for (last = it = begin(); it != end(); last = it, ++it) {
@@ -141,12 +141,12 @@
         }
     }
     // insert if the gone entry.
-    bool insert = (last != it) && (p->getPid() == p->gone);
+    bool insert_before_last = (last != it) && p && (p->getPid() == p->gone);
     p = new PidStatistics(pid, pidToName(pid));
-    if (insert) {
-        Pids.insert(last, p);
+    if (insert_before_last) {
+        insert(last, p);
     } else {
-        Pids.push_back(p);
+        push_back(p);
     }
     p->add(size);
 }
@@ -163,17 +163,17 @@
                 size_t szsTotal = p->sizesTotal();
                 size_t elsTotal = p->elementsTotal();
                 delete p;
-                Pids.erase(it);
+                erase(it);
                 it = end();
                 --it;
                 if (it == end()) {
                     p = new PidStatistics(p->gone);
-                    Pids.push_back(p);
+                    push_back(p);
                 } else {
                     p = *it;
                     if (p->getPid() != p->gone) {
                         p = new PidStatistics(p->gone);
-                        Pids.push_back(p);
+                        push_back(p);
                     }
                 }
                 p->addTotal(szsTotal, elsTotal);
@@ -194,8 +194,8 @@
                 PidStatistics *n = (*it);
                 if ((n->getPid() != n->gone) && (n->sizes() > l->sizes())) {
                     pass = true;
-                    Pids.erase(it);
-                    Pids.insert(lt, n);
+                    erase(it);
+                    insert(lt, n);
                     it = lt;
                     n = l;
                 }
@@ -384,24 +384,24 @@
 }
 
 LogStatistics::LogStatistics()
-        : start(CLOCK_MONOTONIC) {
+        : dgramQlenStatistics(false)
+        , start(CLOCK_MONOTONIC) {
     log_id_for_each(i) {
         mSizes[i] = 0;
         mElements[i] = 0;
     }
 
-    dgram_qlen_statistics = false;
-    for(unsigned short bucket = 0; dgram_qlen(bucket); ++bucket) {
+    for(unsigned short bucket = 0; dgramQlen(bucket); ++bucket) {
         mMinimum[bucket].tv_sec = mMinimum[bucket].tv_sec_max;
         mMinimum[bucket].tv_nsec = mMinimum[bucket].tv_nsec_max;
     }
 }
 
-//   Each bucket below represents a dgram_qlen of log messages. By
+//   Each bucket below represents a dgramQlen of log messages. By
 //   finding the minimum period of time from start to finish
-//   of each dgram_qlen, we can get a performance expectation for
+//   of each dgramQlen, we can get a performance expectation for
 //   the user space logger. The net result is that the period
-//   of time divided by the dgram_qlen will give us the average time
+//   of time divided by the dgramQlen will give us the average time
 //   between log messages; at the point where the average time
 //   is greater than the throughput capability of the logger
 //   we will not longer require the benefits of the FIFO formed
@@ -412,7 +412,7 @@
 //
 //   for example (reformatted):
 //
-//       Minimum time between log events per dgram_qlen:
+//       Minimum time between log events per dgramQlen:
 //       1   2   3   5   10  20  30  50  100  200 300 400 500 600
 //       5u2 12u 13u 15u 16u 27u 30u 36u 407u 3m1 3m3 3m9 3m9 5m5
 //
@@ -426,12 +426,12 @@
 //   a large engineering margin so the rule of thumb that lead us to 100 is
 //   fine.
 //
-// bucket dgram_qlen are tuned for /proc/sys/net/unix/max_dgram_qlen = 300
+// bucket dgramQlen are tuned for /proc/sys/net/unix/max_dgram_qlen = 300
 const unsigned short LogStatistics::mBuckets[] = {
     1, 2, 3, 5, 10, 20, 30, 50, 100, 200, 300, 400, 500, 600
 };
 
-unsigned short LogStatistics::dgram_qlen(unsigned short bucket) {
+unsigned short LogStatistics::dgramQlen(unsigned short bucket) {
     if (bucket >= sizeof(mBuckets) / sizeof(mBuckets[0])) {
         return 0;
     }
@@ -689,23 +689,23 @@
         pids.clear();
     }
 
-    if (dgram_qlen_statistics) {
+    if (dgramQlenStatistics) {
         const unsigned short spaces_time = 6;
         const unsigned long long max_seconds = 100000;
         spaces = 0;
-        string.append("\n\nMinimum time between log events per dgram_qlen:\n");
-        for(unsigned short i = 0; dgram_qlen(i); ++i) {
+        string.append("\n\nMinimum time between log events per max_dgram_qlen:\n");
+        for(unsigned short i = 0; dgramQlen(i); ++i) {
             oldLength = string.length();
             if (spaces < 0) {
                 spaces = 0;
             }
-            string.appendFormat("%*s%u", spaces, "", dgram_qlen(i));
+            string.appendFormat("%*s%u", spaces, "", dgramQlen(i));
             spaces += spaces_time + oldLength - string.length();
         }
         string.append("\n");
         spaces = 0;
         unsigned short n;
-        for(unsigned short i = 0; (n = dgram_qlen(i)); ++i) {
+        for(unsigned short i = 0; (n = dgramQlen(i)); ++i) {
             unsigned long long duration = minimum(i);
             if (duration) {
                 duration /= n;
@@ -727,8 +727,8 @@
                             / (NS_PER_SEC / 1000));
                 } else if (duration >= (NS_PER_SEC / (1000000 / 10))) {
                     string.appendFormat("%lluu",
-                         (duration + (NS_PER_SEC / 2 / 1000000))
-                             / (NS_PER_SEC / 1000000));
+                        (duration + (NS_PER_SEC / 2 / 1000000))
+                            / (NS_PER_SEC / 1000000));
                 } else {
                     string.appendFormat("%llun", duration);
                 }
diff --git a/logd/LogStatistics.h b/logd/LogStatistics.h
index 3733137..6b20e8b 100644
--- a/logd/LogStatistics.h
+++ b/logd/LogStatistics.h
@@ -72,6 +72,10 @@
 
     PidStatisticsCollection Pids;
 
+    void insert(PidStatisticsCollection::iterator i, PidStatistics *p)
+        { Pids.insert(i, p); }
+    void push_back(PidStatistics *p) { Pids.push_back(p); }
+
     size_t mSizes;
     size_t mElements;
 
@@ -81,6 +85,8 @@
 
     PidStatisticsCollection::iterator begin() { return Pids.begin(); }
     PidStatisticsCollection::iterator end() { return Pids.end(); }
+    PidStatisticsCollection::iterator erase(PidStatisticsCollection::iterator i)
+        { return Pids.erase(i); }
 
     uid_t getUid() { return uid; }
 
@@ -138,7 +144,7 @@
     size_t mSizes[LOG_ID_MAX];
     size_t mElements[LOG_ID_MAX];
 
-    bool dgram_qlen_statistics;
+    bool dgramQlenStatistics;
 
     static const unsigned short mBuckets[14];
     log_time mMinimum[sizeof(mBuckets) / sizeof(mBuckets[0])];
@@ -150,8 +156,8 @@
 
     LidStatistics &id(log_id_t log_id) { return LogIds[log_id]; }
 
-    void enableDgramQlenStatistics() { dgram_qlen_statistics = true; }
-    static unsigned short dgram_qlen(unsigned short bucket);
+    void enableDgramQlenStatistics() { dgramQlenStatistics = true; }
+    static unsigned short dgramQlen(unsigned short bucket);
     unsigned long long minimum(unsigned short bucket);
     void recordDiff(log_time diff, unsigned short bucket);