Fix issue #10948509: Crash in procstats when there is no data

Not dealing with the case where there is a null list.

Also fixed some bugs I found while looking at this:

- When resetting the stats, we would use a newly computed time stamp
  for the total durations rather than the one we used to reset the
  proc/service entries.  This would result in them being able to be
  slightly > 100%.
- There was a bug in how we split a single process state into its
  per-package representation, where we would but the cloned process
  state into the new package's entry (instead of properly for its
  own package entry), to be immediately overwritten by the new
  process state we make for that package.  This could result in
  bad data for processes that have multiple packages.
- There was a bug in resetting service stats, where we wouldn't
  update the overall run timestamp, allowing that time to sometimes
  be > 100%.
- There was a bug in computing pss data for processes with multiple
  packages, where the pss data was not distributed across all of the
  activity per-package process states.
- There was a bug in computing the zram information that would cause
  it to compute the wrong value, and then never be displayed.

Finally a little code refactoring so that ProcessState and ServiceState
can now share a common implementation for the table of duration values.

Change-Id: I5e0f4e9107829b81f395dad9419c33257b4f8902
diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp
index 15792e8..62f057f 100644
--- a/core/jni/android_os_Debug.cpp
+++ b/core/jni/android_os_Debug.cpp
@@ -156,31 +156,32 @@
  * Uses libmemtrack to retrieve graphics memory that the process is using.
  * Any graphics memory reported in /proc/pid/smaps is not included here.
  */
-static int read_memtrack_memory(struct memtrack_proc* p, int pid, struct graphics_memory_pss* graphics_mem)
+static int read_memtrack_memory(struct memtrack_proc* p, int pid,
+        struct graphics_memory_pss* graphics_mem)
 {
     int err = memtrack_proc_get(p, pid);
     if (err != 0) {
-        ALOGE("failed to get memory consumption info: %d", err);
+        ALOGW("failed to get memory consumption info: %d", err);
         return err;
     }
 
     ssize_t pss = memtrack_proc_graphics_pss(p);
     if (pss < 0) {
-        ALOGE("failed to get graphics pss: %d", pss);
+        ALOGW("failed to get graphics pss: %d", pss);
         return pss;
     }
     graphics_mem->graphics = pss / 1024;
 
     pss = memtrack_proc_gl_pss(p);
     if (pss < 0) {
-        ALOGE("failed to get gl pss: %d", pss);
+        ALOGW("failed to get gl pss: %d", pss);
         return pss;
     }
     graphics_mem->gl = pss / 1024;
 
     pss = memtrack_proc_other_pss(p);
     if (pss < 0) {
-        ALOGE("failed to get other pss: %d", pss);
+        ALOGW("failed to get other pss: %d", pss);
         return pss;
     }
     graphics_mem->other = pss / 1024;
@@ -199,7 +200,7 @@
 
     struct memtrack_proc* p = memtrack_proc_new();
     if (p == NULL) {
-        ALOGE("failed to create memtrack_proc");
+        ALOGW("failed to create memtrack_proc");
         return -1;
     }
 
@@ -418,8 +419,6 @@
         stats[HEAP_GL].privateDirty = graphics_mem.gl;
         stats[HEAP_OTHER_MEMTRACK].pss = graphics_mem.other;
         stats[HEAP_OTHER_MEMTRACK].privateDirty = graphics_mem.other;
-    } else {
-        ALOGE("Failed to read gpu memory");
     }
 
     for (int i=_NUM_CORE_HEAP; i<_NUM_EXCLUSIVE_HEAP; i++) {
@@ -623,7 +622,7 @@
         close(fd);
         if (len > 0) {
             buffer[len] = 0;
-            mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer);
+            mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
         }
     }
 
@@ -633,7 +632,7 @@
     }
     jlong* outArray = env->GetLongArrayElements(out, 0);
     if (outArray != NULL) {
-        for (int i=0; i<maxNum && tags[i]; i++) {
+        for (int i=0; i<maxNum; i++) {
             outArray[i] = mem[i];
         }
     }