Do not spam activity manager if remote dies.
authorJarkko Pöyry <jpoyry@google.com>
Sat, 25 Apr 2015 00:56:13 +0000 (17:56 -0700)
committerJarkko Pöyry <jpoyry@google.com>
Thu, 30 Apr 2015 00:42:23 +0000 (17:42 -0700)
- Cache isRunning the first time the process is observed dead.
- Avoid busy-polling the log file in DeqpInstrumentation.

Change-Id: Ie4d042cfef261c42ddc24dfaf9d58c13de867b9d

android/package/src/com/drawelements/deqp/testercore/DeqpInstrumentation.java
android/package/src/com/drawelements/deqp/testercore/RemoteAPI.java

index 466c6fd..f104310 100644 (file)
@@ -35,6 +35,7 @@ public class DeqpInstrumentation extends Instrumentation
        private static final long       LAUNCH_TIMEOUT_MS               = 10000;
        private static final long       NO_DATA_TIMEOUT_MS              = 1000;
        private static final long       NO_ACTIVITY_SLEEP_MS    = 100;
+       private static final long       REMOTE_DEAD_SLEEP_MS    = 100;
 
        private String                          m_cmdLine;
        private String                          m_logFileName;
@@ -105,6 +106,20 @@ public class DeqpInstrumentation extends Instrumentation
 
                        parser.init(this, m_logFileName, m_logData);
 
+                       // parse until tester dies
+                       {
+                               while (true)
+                               {
+                                       if (!parser.parse())
+                                       {
+                                               Thread.sleep(NO_ACTIVITY_SLEEP_MS);
+                                               if (!remoteApi.isRunning())
+                                                       break;
+                                       }
+                               }
+                       }
+
+                       // parse remaining messages
                        {
                                long lastDataMs = System.currentTimeMillis();
 
@@ -112,15 +127,16 @@ public class DeqpInstrumentation extends Instrumentation
                                {
                                        if (parser.parse())
                                                lastDataMs = System.currentTimeMillis();
-                                       else if (!remoteApi.isRunning())
+                                       else
                                        {
                                                final long timeSinceLastDataMs = System.currentTimeMillis()-lastDataMs;
 
                                                if (timeSinceLastDataMs > NO_DATA_TIMEOUT_MS)
                                                        break; // Assume no data is available for reading any more
+
+                                               // Remote is dead, wait a bit until trying to read again
+                                               Thread.sleep(REMOTE_DEAD_SLEEP_MS);
                                        }
-                                       else
-                                               Thread.sleep(NO_ACTIVITY_SLEEP_MS);
                                }
                        }
 
index 3c7fd7a..b1c515d 100644 (file)
@@ -38,11 +38,13 @@ public class RemoteAPI {
        private Context                                         m_context;
        private String                                          m_processName;
        private String                                          m_logFileName;
+       private boolean                                         m_canBeRunning;
 
        public RemoteAPI (Context context, String logFileName) {
                m_context                       = context;
                m_processName           = m_context.getPackageName() + ":testercore";
                m_logFileName           = logFileName;
+               m_canBeRunning          = false;
        }
 
        private ComponentName getDefaultTesterComponent () {
@@ -101,12 +103,15 @@ public class RemoteAPI {
                        return false;
                }
 
+               m_canBeRunning = true;
                return true;
        }
 
        public boolean kill () {
                ActivityManager.RunningAppProcessInfo processInfo = findProcess(m_processName);
 
+               // \note not mutating m_canBeRunning yet since process does not die immediately
+
                if (processInfo != null) {
                        Log.d(LOG_TAG, "Killing " + m_processName);
                        Process.killProcess(processInfo.pid);
@@ -119,7 +124,15 @@ public class RemoteAPI {
        }
 
        public boolean isRunning () {
-               return isProcessRunning(m_processName);
+               if (!m_canBeRunning) {
+                       return false;
+               } else if (isProcessRunning(m_processName)) {
+                       return true;
+               } else {
+                       // Cache result. Safe, because only start() can spawn the process
+                       m_canBeRunning = false;
+                       return false;
+               }
        }
 
        public String getLogFileName () {