[JVM] Support overriding RPCWatchdog termination behavior on Android and other platfo...
authorChris Sullivan <csullivan@octoml.ai>
Tue, 11 Aug 2020 20:04:26 +0000 (13:04 -0700)
committerGitHub <noreply@github.com>
Tue, 11 Aug 2020 20:04:26 +0000 (13:04 -0700)
* Instead of performing a system exit and leaving unhandled items on
the activity stack, finish the RPCActivity and return cleanly to the
MainActivity where the RPCActivity can be restarted automatically.

* Update doc. string for checkstyle.

apps/android_rpc/app/src/main/java/org/apache/tvm/tvmrpc/RPCActivity.java
apps/android_rpc/app/src/main/java/org/apache/tvm/tvmrpc/RPCAndroidWatchdog.java [new file with mode: 0644]
apps/android_rpc/app/src/main/java/org/apache/tvm/tvmrpc/RPCProcessor.java
jvm/core/src/main/java/org/apache/tvm/rpc/RPCWatchdog.java

index 54182d7..0d7f047 100644 (file)
@@ -51,7 +51,7 @@ public class RPCActivity extends AppCompatActivity {
     int port = intent.getIntExtra("port", 9090);
     String key = intent.getStringExtra("key");
 
-    tvmServerWorker = new RPCProcessor();
+    tvmServerWorker = new RPCProcessor(this);
     tvmServerWorker.setDaemon(true);
     tvmServerWorker.start();
     tvmServerWorker.connect(host, port, key);
@@ -62,5 +62,6 @@ public class RPCActivity extends AppCompatActivity {
     System.err.println("rpc activity onDestroy");
     tvmServerWorker.disconnect();
     super.onDestroy();
+    android.os.Process.killProcess(android.os.Process.myPid());
   }
 }
diff --git a/apps/android_rpc/app/src/main/java/org/apache/tvm/tvmrpc/RPCAndroidWatchdog.java b/apps/android_rpc/app/src/main/java/org/apache/tvm/tvmrpc/RPCAndroidWatchdog.java
new file mode 100644 (file)
index 0000000..69b7d27
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tvm.tvmrpc;
+
+import android.app.Activity;
+import org.apache.tvm.rpc.RPCWatchdog;
+
+/**
+ * Watchdog for Android RPC.
+ */
+public class RPCAndroidWatchdog extends RPCWatchdog {
+  public Activity rpc_activity = null;
+  public RPCAndroidWatchdog(Activity activity) {
+    super();
+    rpc_activity = activity;
+  }
+
+  /**
+   * Method to non-destructively terminate the running thread on Android
+   */
+  @Override protected void terminate() {
+    rpc_activity.finish();
+  }
+}
index ca0c9c8..8bb44f1 100644 (file)
 
 package org.apache.tvm.tvmrpc;
 
+import android.app.Activity;
 import android.os.ParcelFileDescriptor;
 import java.net.Socket;
 import org.apache.tvm.rpc.ConnectTrackerServerProcessor;
-import org.apache.tvm.rpc.RPCWatchdog;
 
 /**
  * Connect to RPC proxy and deal with requests.
@@ -33,9 +33,15 @@ class RPCProcessor extends Thread {
   private long startTime;
   private ConnectTrackerServerProcessor currProcessor;
   private boolean first = true;
+  private Activity rpc_activity = null;
+
+  public RPCProcessor(Activity activity) {
+    super();
+    rpc_activity = activity;
+  }
 
   @Override public void run() {
-    RPCWatchdog watchdog = new RPCWatchdog();
+    RPCAndroidWatchdog watchdog = new RPCAndroidWatchdog(rpc_activity);
     watchdog.start();
     while (true) {
       synchronized (this) {
index 33dbdde..ef5bb60 100644 (file)
@@ -71,7 +71,7 @@ public class RPCWatchdog extends Thread {
             } else {
               System.err.println("watchdog woke up!");
               System.err.println("terminating...");
-              System.exit(0);
+              terminate();
             }
           } catch (InterruptedException e) {
             System.err.println("watchdog interrupted...");
@@ -80,4 +80,11 @@ public class RPCWatchdog extends Thread {
       }
     }
   }
+
+  /**
+   * Default method to terminate the running RPCActivity process.
+   */
+  protected void terminate() {
+    System.exit(0);
+  }
 }