forked from huajsj/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RPC] android process isolation/watchdog (apache#1387)
- Loading branch information
Showing
8 changed files
with
242 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apps/android_rpc/app/src/main/java/ml/dmlc/tvm/tvmrpc/RPCService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ml.dmlc.tvm.tvmrpc; | ||
|
||
import android.app.Service; | ||
import android.os.IBinder; | ||
import android.content.Intent; | ||
|
||
public class RPCService extends Service { | ||
private String host; | ||
private int port; | ||
private String key; | ||
private int intentNum; | ||
private RPCProcessor tvmServerWorker; | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
synchronized(this) { | ||
System.err.println("start command intent"); | ||
// use an alternate kill to prevent android from recycling the | ||
// process | ||
if (intent.getBooleanExtra("kill", false)) { | ||
System.err.println("rpc service received kill..."); | ||
System.exit(0); | ||
} | ||
|
||
this.host = intent.getStringExtra("host"); | ||
this.port = intent.getIntExtra("port", 9090); | ||
this.key = intent.getStringExtra("key"); | ||
System.err.println("got the following: " + this.host + ", " + this.port + ", " + this.key); | ||
System.err.println("intent num: " + this.intentNum); | ||
|
||
if (tvmServerWorker == null) { | ||
System.err.println("service created worker..."); | ||
tvmServerWorker = new RPCProcessor(); | ||
tvmServerWorker.setDaemon(true); | ||
tvmServerWorker.start(); | ||
tvmServerWorker.connect(this.host, this.port, this.key); | ||
} | ||
else if (tvmServerWorker.timedOut(System.currentTimeMillis())) { | ||
System.err.println("rpc service timed out, killing self..."); | ||
System.exit(0); | ||
} | ||
this.intentNum++; | ||
} | ||
// do not restart unless watchdog/app expliciltly does so | ||
return START_NOT_STICKY; | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
System.err.println("rpc service got onBind, doing nothing..."); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
System.err.println("rpc service onCreate..."); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
tvmServerWorker.disconnect(); | ||
System.err.println("rpc service onDestroy..."); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
apps/android_rpc/app/src/main/java/ml/dmlc/tvm/tvmrpc/RPCWatchdog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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 ml.dmlc.tvm.tvmrpc; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
|
||
/** | ||
* Watchdog for RPCService | ||
*/ | ||
class RPCWatchdog extends Thread { | ||
public static final int WATCHDOG_POLL_INTERVAL = 5000; | ||
private String host; | ||
private int port; | ||
private String key; | ||
private Context context; | ||
private boolean done = false; | ||
|
||
public RPCWatchdog(String host, int port, String key, Context context) { | ||
super(); | ||
this.host = host; | ||
this.port = port; | ||
this.key = key; | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* Polling loop to check on RPCService status | ||
*/ | ||
@Override public void run() { | ||
try { | ||
while (true) { | ||
synchronized (this) { | ||
if (done) { | ||
System.err.println("watchdog done, returning..."); | ||
return; | ||
} | ||
else { | ||
System.err.println("polling rpc service..."); | ||
System.err.println("sending rpc service intent..."); | ||
Intent intent = new Intent(context, RPCService.class); | ||
intent.putExtra("host", host); | ||
intent.putExtra("port", port); | ||
intent.putExtra("key", key); | ||
// will implicilty restart the service if it died | ||
context.startService(intent); | ||
} | ||
} | ||
Thread.sleep(WATCHDOG_POLL_INTERVAL); | ||
} | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
|
||
/** | ||
* Disconnect from the proxy server. | ||
*/ | ||
synchronized void disconnect() { | ||
// kill service | ||
System.err.println("watchdog disconnect call..."); | ||
System.err.println("stopping rpc service..."); | ||
done = true; | ||
Intent intent = new Intent(context, RPCService.class); | ||
intent.putExtra("kill", true); | ||
context.startService(intent); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.