sh_commands.py 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
import sh
import re
import time
import falcon_cli

################################
# common
################################
def strip_invalid_utf8(str):
  return sh.iconv(str, "-c", "-t", "UTF-8")

def make_output_processor(buff):
  def process_output(line):
    print(line.strip())
    buff.append(line)
  return process_output

################################
# adb commands
################################
def adb_split_stdout(stdout_str):
  stdout_str = strip_invalid_utf8(stdout_str)
  # Filter out last empty line
  return [l.strip() for l in stdout_str.split('\n') if len(l.strip()) > 0]

def adb_devices(target_socs=None):
  outputs = sh.grep(sh.adb("devices"), "^[A-Za-z0-9]\+[[:space:]]\+device$")
  raw_lists = sh.cut(outputs, "-f1")
  device_ids = adb_split_stdout(raw_lists)
  if target_socs != None:
    target_socs_set = set(target_socs)
    target_devices = []
    for serialno in device_ids:
      props = adb_getprop_by_serialno(serialno)
      if props["ro.board.platform"] in target_socs_set:
        target_devices.append(serialno)
    return target_devices
  else:
    return device_ids

def adb_getprop_by_serialno(serialno):
  outputs = sh.adb("-s", serialno, "shell", "getprop")
  raw_props = adb_split_stdout(outputs)
  props = {}
  p = re.compile("\[(.+)\]: \[(.+)\]")
  for raw_prop in raw_props:
    m = p.match(raw_prop)
    if m:
      props[m.group(1)] = m.group(2)
  return props

52 53 54 55 56 57
def adb_supported_abis(serialno):
  props = adb_getprop_by_serialno(serialno)
  abilist_str = props["ro.product.cpu.abilist"]
  abis = [abi.strip() for abi in abilist_str.split(',')]
  return abis

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
def adb_get_all_socs():
  socs = []
  for d in adb_devices():
    props = adb_getprop_by_serialno(d)
    socs.append(props["ro.board.platform"])
  return set(socs)

def adb_run(serialno, host_bin_path, bin_name,
            args="",
            opencl_profiling=1,
            vlog_level=0,
            device_bin_path="/data/local/tmp/mace"):
  host_bin_full_path = "%s/%s" % (host_bin_path, bin_name)
  device_bin_full_path = "%s/%s" % (device_bin_path, bin_name)
  props = adb_getprop_by_serialno(serialno)
L
Liangliang He 已提交
73 74 75
  print("=====================================================================")
  print("Run on device: %s, %s, %s" % (serialno, props["ro.board.platform"],
                                       props["ro.product.model"]))
76 77
  sh.adb("-s", serialno, "shell", "rm -rf %s" % device_bin_path)
  sh.adb("-s", serialno, "shell", "mkdir -p %s" % device_bin_path)
L
Liangliang He 已提交
78
  print("Push %s to %s" % (host_bin_full_path, device_bin_full_path))
L
Liangliang He 已提交
79
  sh.adb("-s", serialno, "push", host_bin_full_path, device_bin_full_path)
L
Liangliang He 已提交
80
  print("Run %s" % device_bin_full_path)
81 82 83
  stdout_buff=[]
  process_output = make_output_processor(stdout_buff)
  p = sh.adb("-s", serialno, "shell",
L
liuqi 已提交
84 85
             "MACE_OPENCL_PROFILING=%d MACE_CPP_MIN_VLOG_LEVEL=%d %s %s" %
             (opencl_profiling, vlog_level, device_bin_full_path, args),
L
Liangliang He 已提交
86
             _out=process_output, _bg=True, _err_to_out=True)
87 88 89 90 91 92 93 94
  p.wait()
  return "".join(stdout_buff)


################################
# bazel commands
################################
def bazel_build(target, strip="always", abi="armeabi-v7a"):
95
  print("Build %s with ABI %s" % (target, abi))
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  stdout_buff=[]
  process_output = make_output_processor(stdout_buff)
  p= sh.bazel("build",
              "-c", "opt",
              "--strip", strip,
              "--verbose_failures",
              target,
              "--crosstool_top=//external:android/crosstool",
              "--host_crosstool_top=@bazel_tools//tools/cpp:toolchain",
              "--cpu=%s" % abi,
              "--copt=-std=c++11",
              "--copt=-D_GLIBCXX_USE_C99_MATH_TR1",
              "--copt=-DMACE_DISABLE_NO_TUNING_WARNING",
              "--copt=-Werror=return-type",
              "--copt=-O3",
              "--define", "neon=true",
              "--define", "openmp=true",
L
Liangliang He 已提交
113
              _out=process_output, _bg=True, _err_to_out=True)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  p.wait()
  return "".join(stdout_buff)

def bazel_target_to_bin(target):
  # change //mace/a/b:c to bazel-bin/mace/a/b/c
  prefix, bin_name = target.split(':')
  prefix = prefix.replace('//', '/')
  if prefix.startswith('/'):
    prefix = prefix[1:]
  host_bin_path = "bazel-bin/%s" % prefix
  return host_bin_path, bin_name

################################
# mace commands
################################
# TODO this should be refactored
def gen_encrypted_opencl_source(codegen_path="mace/codegen"):
L
liuqi 已提交
131
  sh.mkdir("-p", "%s/opencl" % codegen_path)
132 133 134 135 136 137 138 139 140
  sh.python("mace/python/tools/encrypt_opencl_codegen.py",
            "--cl_kernel_dir=./mace/kernels/opencl/cl/",
            "--output_path=%s/opencl/opencl_encrypt_program.cc" % codegen_path)

def gen_mace_version(codegen_path="mace/codegen"):
  sh.mkdir("-p", "%s/version" % codegen_path)
  sh.bash("mace/tools/git/gen_version_source.sh",
          "%s/version/version.cc" % codegen_path)

L
liuqi 已提交
141
def gen_compiled_opencl_source(codegen_path="mace/codegen"):
L
liuqi 已提交
142
  sh.mkdir("-p", "%s/opencl" % codegen_path)
L
liuqi 已提交
143 144 145
  sh.python("mace/python/tools/opencl_codegen.py",
            "--output_path=%s/opencl/opencl_compiled_program.cc" % codegen_path)

146 147 148
################################
# falcon
################################
L
Liangliang He 已提交
149 150 151 152 153 154 155 156 157 158
def falcon_tags(tags_dict):
  tags = ""
  for k, v in tags_dict.iteritems():
    if tags == "":
      tags = "%s=%s" % (k, v)
    else:
      tags = tags + ",%s=%s" % (k, v)
  return tags

def falcon_push_metrics(metrics, endpoint="mace_dev", tags={}):
159 160
  cli = falcon_cli.FalconCli.connect(server="transfer.falcon.miliao.srv",
                                     port=8433,
L
Liangliang He 已提交
161
                                     debug=False)
162 163
  ts = int(time.time())
  falcon_metrics = [{
L
Liangliang He 已提交
164
      "endpoint": endpoint,
165
      "metric": key,
L
Liangliang He 已提交
166
      "tags": falcon_tags(tags),
167 168
      "timestamp": ts,
      "value": value,
169
      "step": 86400,
170 171 172 173
      "counterType": "GAUGE"
      } for key, value in metrics.iteritems()]
  cli.update(falcon_metrics)