diff --git a/.gitignore b/.gitignore index c78b060c4a06e8597917b39f67d90fb53f466a29..27ab66f9ec8071c47b818c68404a9b73c924108d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ mace/codegen/opencl_bin/ mace/codegen/tuning/ mace/codegen/version/ build/ +docs/_build/ diff --git a/tools/bazel_adb_run.py b/tools/bazel_adb_run.py index 88d8c1814b84aaa36e178f2c844ce3675a279d58..2698a9eadf3e28e24b6b5fc851f52ca2eb8516d4 100644 --- a/tools/bazel_adb_run.py +++ b/tools/bazel_adb_run.py @@ -86,7 +86,12 @@ def main(unused_args): target_socs = set(FLAGS.target_socs.split(',')) target_devices = sh_commands.adb_devices(target_socs=target_socs) if FLAGS.target_socs == "random": - target_devices = [random.choice(target_devices)] + unlocked_devices = \ + [d for d in target_devices if not sh_commands.is_device_locked(d)] + if len(unlocked_devices) > 0: + target_devices = [random.choice(unlocked_devices)] + else: + target_devices = [random.choice(target_devices)] target = FLAGS.target host_bin_path, bin_name = sh_commands.bazel_target_to_bin(target) diff --git a/tools/sh_commands.py b/tools/sh_commands.py index 4c3d16a2b09fdd62f60d00e81f5eab020fcd825b..40cc932a33977fa34c0c3b3b75156001966e871c 100644 --- a/tools/sh_commands.py +++ b/tools/sh_commands.py @@ -20,6 +20,22 @@ def make_output_processor(buff): return process_output +def device_lock_path(serialno): + return "/tmp/device-lock-%s" % serialno + + +def device_lock(serialno, timeout=3600): + return filelock.FileLock(device_lock_path(serialno), timeout=timeout) + + +def is_device_locked(serialno): + try: + with device_lock(serialno, timeout=0.000001): + return False + except filelock.Timeout: + return True + + ################################ # adb commands ################################ @@ -90,12 +106,11 @@ def adb_run(serialno, print( "=====================================================================" ) - lock_path = "/tmp/device-lock-%s" % serialno - print("Try to lock ", lock_path) - with filelock.FileLock(lock_path, timeout=3600): - print("Run on device: %s, %s, %s" % (serialno, - props["ro.board.platform"], - props["ro.product.model"])) + print("Trying to lock device", serialno) + with device_lock(serialno): + print("Run on device: %s, %s, %s" % + (serialno, props["ro.board.platform"], + props["ro.product.model"])) sh.adb("-s", serialno, "shell", "rm -rf %s" % device_bin_path) sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path) print("Push %s to %s" % (host_bin_full_path, device_bin_full_path)) @@ -111,7 +126,7 @@ def adb_run(serialno, "MACE_OUT_OF_RANGE_CHECK=%d MACE_OPENCL_PROFILING=%d " "MACE_CPP_MIN_VLOG_LEVEL=%d %s %s" % (out_of_range_check, opencl_profiling, vlog_level, - device_bin_full_path, args), + device_bin_full_path, args), _out=process_output, _bg=True, _err_to_out=True)