DO NOT MERGE:Add missing SOCK_CLOEXEC flag when opening socket am: bef09aaf9e  -s ours am: b1fada1564 am: 466a521607  -s ours am: ea11f5547c am: f93261010a am: 6a40db71f7 am: fc6c0a3480  -s ours am: 9df391f156 am: ab177b77f7 am: acdde34428
am: c706c7ffcc

Change-Id: Ia71b3d6a6b0a35d8806524853507a63c96e4a535
diff --git a/toolbox/top.c b/toolbox/top.c
index 6fda132..e70ea31 100644
--- a/toolbox/top.c
+++ b/toolbox/top.c
@@ -31,6 +31,7 @@
 
 #include <ctype.h>
 #include <dirent.h>
+#include <errno.h>
 #include <grp.h>
 #include <inttypes.h>
 #include <pwd.h>
@@ -38,6 +39,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <cutils/sched_policy.h>
@@ -180,6 +182,11 @@
     new_procs = old_procs = NULL;
 
     read_procs();
+
+    // Pause 250ms to get better data and avoid divide by zero later (http://b/32478213).
+    struct timespec ts = { .tv_sec = 0, .tv_nsec = 250000000 };
+    TEMP_FAILURE_RETRY(nanosleep(&ts, &ts));
+
     while ((iterations == -1) || (iterations-- > 0)) {
         old_procs = new_procs;
         num_old_procs = num_new_procs;