Example #1
0
    def prepare_models_for_devices(self):
        logger.info(
            "==== {} ====".format(self.prepare_models_for_devices.__name__)
        )  # noqa
        device_work_dir = self.config["device_work_dir"]
        device_dict = self.config["device_dict"]
        model_dict = self.config["model_dict"]

        device_serials = list(device_dict.keys())
        model_names = list(model_dict.keys())

        cmds = list()
        for didx in range(len(device_serials)):
            device_serial = device_serials[didx]
            mkdir_cmd = "adb -s {} shell mkdir -p {}".format(
                device_serial, device_work_dir
            )
            cmds.append(mkdir_cmd)
            for midx in range(len(model_names)):
                model_name = model_names[midx]
                model_proto = model_dict[model_name]
                model_param = model_proto.replace("tnnproto", "tnnmodel")
                push_proto_cmd = "adb -s {} push {} {}".format(
                    device_serial, model_proto, device_work_dir
                )
                push_param_cmd = "adb -s {} push {} {}".format(
                    device_serial, model_param, device_work_dir
                )
                cmds.extend([push_proto_cmd, push_param_cmd])
                logger.debug([push_proto_cmd, push_param_cmd])

        run_cmds(cmds)
        return 0
Example #2
0
def get_cpu_max_freqs(serial_num):
    check_cpu_num_cmd = "adb -s {} shell cat /proc/cpuinfo | grep processor".format(  # noqa
        serial_num
    )
    cmd_res = run_cmd(check_cpu_num_cmd)
    cpu_num = len(cmd_res)
    check_cpu_max_freq_cmd_pattern = (
        "adb -s {} shell cat "
        "/sys/devices/system/cpu/cpu{}/cpufreq/cpuinfo_max_freq"  # noqa
    )
    cmds = map(
        lambda cpu_idx: check_cpu_max_freq_cmd_pattern.format(  # noqa
            serial_num, cpu_idx
        ),
        range(cpu_num),
    )
    cmds = list(cmds)

    try:
        cmd_res_list = run_cmds(cmds)
        logger.info(cmd_res_list[cmds[0]])
        cpu_max_freqs = map(lambda cmd_key: cmd_res_list[cmd_key], cmds)
    except IndexError:
        logger.warn(
            "cat: /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq:"  # noqa
            " Permission denied"  # noqa
        )
        logger.warn("replacing scaling_max_freq with cpuinfo_max_freq")
        cmds = map(lambda c: c.replace("cpuinfo", "scaling"), cmds)

        cmd_res_list = run_cmds(cmds)
        # logger.warn(cmd_res_list[cmds[0]].strip())
        cpu_max_freqs = map(
            lambda cmd_key: cmd_res_list[cmd_key].strip(), cmds  # noqa
        )  # noqa
    cpu_max_freqs = list(cpu_max_freqs)
    cpu_max_freqs = cpu_max_freqs[:cpu_num]
    # get str from list
    cpu_max_freqs = list(map(lambda l: l[0], cpu_max_freqs))
    logger.debug(
        "cpu_max_freqs:{}, cpu_num:{}".format(cpu_max_freqs, cpu_num)  # noqa
    )  # noqa
    is_valid_freq = (
        lambda freq_str: True
        if "No such file or director" not in freq_str
        else False  # noqa
    )
    cpu_max_freqs_ghz = map(
        lambda freq: float(freq) / 1e6 if is_valid_freq(freq) else None,  # noqa
        cpu_max_freqs,  # noqa
    )
    logger.debug(cpu_max_freqs_ghz)
    cpu_max_freqs_ghz = list(cpu_max_freqs_ghz)
    return cpu_max_freqs_ghz
Example #3
0
    def prepare_models_for_devices(self):
        logger.info("==== {} ====".format(
            self.prepare_models_for_devices.__name__))  # noqa
        device_work_dir = self.config["device_work_dir"]
        device_dict = self.config["device_dict"]
        model_dict = self.config["model_dict"]

        device_serials = list(device_dict.keys())
        model_names = list(model_dict.keys())
        logger.debug(model_dict)

        cmds = list()
        for didx in range(len(device_serials)):
            device_serial = device_serials[didx]
            mkdir_cmd = "adb -s {} shell mkdir -p {}".format(
                device_serial, device_work_dir)
            cmds.append(mkdir_cmd)
            for midx in range(len(model_names)):
                model_name = model_names[midx]
                model_proto = model_dict[model_name]

                if (self.config["framework_name"] == "ncnn"
                        or self.config["framework_name"] == "tnn"):
                    model_param = model_proto.replace("tnnproto", "tnnmodel")
                elif (self.config["framework_name"] == "mnn"
                      or self.config["framework_name"] == "tflite"):
                    model_param = None
                else:
                    logger.fatal("Unsupported framework {}".format(  # noqa
                        self.config["framework_name"]  # noqa
                    )  # noqa
                                 )
                    exit(1)

                push_proto_cmd = "adb -s {} push {} {}".format(
                    device_serial,
                    model_proto,
                    "/".join([device_work_dir,
                              os.path.basename(model_proto)]),  # noqa
                )
                push_param_cmd = "adb -s {} push {} {}".format(
                    device_serial, model_param, device_work_dir)
                push_param_cmd = (
                    "echo" if model_param is None else push_param_cmd)  # noqa
                cmds.extend([push_proto_cmd, push_param_cmd])

        run_cmds(cmds)
        return 0
Example #4
0
def get_battery_level(serial_num):
    unset_battery_cmd = "adb -s {} shell dumpsys battery reset".format(  # noqa
        serial_num)
    lookup_battery_cmd = ("adb -s {} shell dumpsys battery"
                          " | grep level"
                          " | tr -cd 0-9".format(serial_num))
    cmds = [unset_battery_cmd, lookup_battery_cmd]
    cmd_res_dict = run_cmds(cmds)
    battery_level = cmd_res_dict[lookup_battery_cmd][0]
    return battery_level
Example #5
0
    def prepare_models(self):
        logger.info("==== {} ====".format(self.prepare_models.__name__))
        cmds = list()
        framework_name = self.config["framework_name"]
        model_repo = self.config["model_repo"]
        model_type_keyword = self.config["model_type_keyword"]
        repo_name = get_file_name(model_repo, False)
        logger.info("repo_name:{}".format(repo_name))
        logger.debug("os.path.exists:{}".format(os.path.exists(repo_name)))
        if os.path.exists(repo_name):
            clone_models_cmd = "ls {}".format(repo_name)
        else:
            clone_models_cmd = "git clone {}".format(model_repo)
        model_repo_version_cmd = (
            'cd ./{}-models/; git log --pretty=format:"SHA-1:%h date:%ad" '
            '--date=format:"%y-%m-%d" -n1 #--shortstat -n1'.format(
                framework_name)  # noqa
        )
        model_repo_version_extra_cmd = (
            "cd ./{}-models/; "
            'git log --pretty=format:"SHA-1:%h - author:%an date:%ad '
            'note:%s" --date=format:"%y-%m-%d %H:%M:%S" -n1'.format(
                framework_name)  # noqa
        )
        model_repo_branch_cmd = "cd ./{}-models/; git branch | sed 's/\* //g'".format(  # noqa
            framework_name)
        lookup_models_path_cmd = "realpath ./{}-models/*{}*".format(
            framework_name, model_type_keyword)

        cmds.extend([
            clone_models_cmd,
            model_repo_version_cmd,
            model_repo_version_extra_cmd,
            model_repo_branch_cmd,
            lookup_models_path_cmd,
        ])

        cmds_res = run_cmds(cmds)

        # TODO(ysh329): add model_repo_version to config
        # self.config["model_repo_branch"] =
        # cmds_res[model_repo_branch_cmd][0]
        # self.config["model_repo_version"] =
        # cmds_res[model_repo_version_cmd].readalines()[0]
        # self.config["model_repo_version_extra"] =
        # cmds_res[model_repo_version_extra_cmd].readalines()[0]

        models_dir = list(
            map(lambda path: path.strip(),
                cmds_res[lookup_models_path_cmd])  # noqa
        )

        model_dict = dict()
        for midx in range(len(models_dir)):
            model_dir = models_dir[midx]
            logger.debug("{} {}".format(midx, model_dir))
            file_type = model_dir.split(".")[-1]
            model_name = (model_dir.split("/")[-1].replace(
                "." + file_type, "").replace(file_type, ""))
            logger.debug("model_name:{}, file_type:{}".format(
                model_name, file_type))  # noqa
            model_dict[model_name] = model_dir
        logger.debug(models_dir)
        logger.debug(model_dict)
        return model_dict
Example #6
0
    def prepare_benchmark_assets_for_devices(self):
        logger.info("==== {} ====".format(
            self.prepare_benchmark_assets_for_devices.__name__)  # noqa
                    )
        benchmark_platform = self.config["benchmark_platform"]
        device_work_dir = self.config["device_work_dir"]
        device_dict = self.config["device_dict"]
        device_serials = list(device_dict.keys())

        cmds = list()
        for didx in range(len(device_serials)):
            device_serial = device_serials[didx]
            for pidx in range(len(benchmark_platform)):
                platform = benchmark_platform[pidx]
                device_work_dir_platform = device_work_dir + "/" + platform
                # benchmark assets
                benchmark_bin = self.config[platform]["benchmark_bin"]
                benchmark_lib = self.config[platform]["shared_lib"]
                benchmark_libs = (benchmark_lib if isinstance(
                    benchmark_lib, list) else [benchmark_lib])

                # create cmds
                rmdir_cmd = "adb -s {} shell rm -rf {}".format(
                    device_serial, device_work_dir_platform)
                mkdir_cmd = "adb -s {} shell mkdir -p {}".format(
                    device_serial, device_work_dir_platform)

                # lib
                benchmark_lib_device_paths = map(
                    lambda lib: device_work_dir_platform + "/" + os.path.
                    basename(lib),  # noqa
                    benchmark_libs,
                )
                benchmark_lib_device_paths = list(benchmark_lib_device_paths)
                logger.debug("benchmark_lib_device_paths:{}".format(  # noqa
                    benchmark_lib_device_paths  # noqa
                )  # noqa
                             )
                push_shared_lib_cmds = map(
                    lambda lib, lib_device: "adb -s {} push {} {}".
                    format(  # noqa
                        device_serial, lib, lib_device),  # noqa
                    benchmark_libs,
                    benchmark_lib_device_paths,
                )
                push_shared_lib_cmds = list(push_shared_lib_cmds)
                push_shared_lib_cmds = (["echo"] if benchmark_lib is None else
                                        push_shared_lib_cmds)
                logger.debug("push_shared_lib_cmds:{}".format(  # noqa
                    push_shared_lib_cmds)  # noqa
                             )
                logger.debug("len(push_shared_lib_cmds):{}".format(  # noqa
                    len(push_shared_lib_cmds)  # noqa
                )  # noqa
                             )

                # bin
                benchmark_bin_device_path = (
                    device_work_dir_platform + "/" +
                    os.path.basename(benchmark_bin)  # noqa
                )
                push_benchmark_bin_cmd = "adb -s {} push {} {}".format(
                    device_serial, benchmark_bin, benchmark_bin_device_path)
                chmod_x_bin_cmd = "adb -s {} shell chmod +x {}".format(
                    device_serial, benchmark_bin_device_path)

                cmds.extend([rmdir_cmd, mkdir_cmd])
                cmds.extend(push_shared_lib_cmds)
                cmds.extend([push_benchmark_bin_cmd, chmod_x_bin_cmd])
        logger.info(cmds)
        run_cmds(cmds)
        return 0
Example #7
0
    def prepare_benchmark_assets_for_devices(self):
        logger.info(
            "==== {} ====".format(
                self.prepare_benchmark_assets_for_devices.__name__
            )  # noqa
        )
        benchmark_platform = self.config["benchmark_platform"]
        device_work_dir = self.config["device_work_dir"]
        device_dict = self.config["device_dict"]
        device_serials = list(device_dict.keys())

        cmds = list()
        for didx in range(len(device_serials)):
            device_serial = device_serials[didx]
            for pidx in range(len(benchmark_platform)):
                platform = benchmark_platform[pidx]
                device_work_dir_platform = device_work_dir + "/" + platform
                # benchmark assets
                benchmark_bin = self.config[platform]["benchmark_bin"]
                benchmark_lib = self.config[platform]["shared_lib"]

                rmdir_cmd = "adb -s {} shell rm -rf {}".format(
                    device_serial, device_work_dir_platform
                )
                mkdir_cmd = "adb -s {} shell mkdir -p {}".format(
                    device_serial, device_work_dir_platform
                )
                benchmark_lib_device_path = (
                    device_work_dir_platform
                    + "/"
                    + os.path.basename(benchmark_lib)  # noqa
                )
                benchmark_bin_device_path = (
                    device_work_dir_platform
                    + "/"
                    + os.path.basename(benchmark_bin)  # noqa
                )

                push_shared_lib_cmd = "adb -s {} push {} {}".format(
                    device_serial, benchmark_lib, benchmark_lib_device_path
                )
                push_shared_lib_cmd = (
                    "echo" if benchmark_lib is None else push_shared_lib_cmd
                )

                push_benchmark_bin_cmd = "adb -s {} push {} {}".format(
                    device_serial, benchmark_bin, benchmark_bin_device_path
                )
                chmod_x_bin_cmd = "adb -s {} shell chmod +x {}".format(
                    device_serial, benchmark_bin_device_path
                )

                cmds.extend(
                    [
                        rmdir_cmd,
                        mkdir_cmd,
                        push_shared_lib_cmd,
                        push_benchmark_bin_cmd,
                        chmod_x_bin_cmd,
                    ]
                )

        run_cmds(cmds)
        return 0
Example #8
0
    def prepare_devices(self):
        logger.info("==== {} ====".format(self.prepare_devices.__name__))

        device_status_dict = get_adb_devices(True)
        serial_num_list = list(device_status_dict.keys())
        logger.debug(serial_num_list)

        device_dict = dict()
        for sidx in range(len(serial_num_list)):
            device_serial_num = serial_num_list[sidx]
            device_status = device_status_dict[device_serial_num]
            if device_status != "device":
                logger.info(
                    "device {} status is {}, skipped".format(
                        device_serial_num, device_status
                    )
                )
                continue
            device_dict[device_serial_num] = dict()
            device_dict[device_serial_num]["status"] = device_status
            device_dict[device_serial_num]["cpu_max_freqs"] = get_cpu_max_freqs(  # noqa
                device_serial_num
            )
            cpu_max_freqs = get_cpu_max_freqs(device_serial_num)
            cpu_valid_freqs = list(
                filter(lambda freq: freq is not None, cpu_max_freqs)
            )  # noqa
            big_cores_idx = get_target_freq_idx(
                max(cpu_valid_freqs), device_serial_num, cpu_max_freqs
            )
            big_cores_idx_str = ",".join(big_cores_idx)
            little_cores_idx = get_target_freq_idx(
                min(cpu_valid_freqs), device_serial_num, cpu_max_freqs
            )
            little_cores_idx_str = ",".join(little_cores_idx)
            device_dict[device_serial_num]["big_cores_idx"] = big_cores_idx_str
            device_dict[device_serial_num][
                "little_cores_idx"
            ] = little_cores_idx_str  # noqa
            if self.config["power_mode"] == "big_cores":
                device_dict[device_serial_num][
                    "bind_cpu_idx"
                ] = big_cores_idx_str  # noqa
            elif self.config["power_mode"] == "little_cores":
                device_dict[device_serial_num][
                    "bind_cpu_idx"
                ] = little_cores_idx_str  # noqa
            elif self.config["power_mode"] == "no_bind":
                device_dict[device_serial_num]["bind_cpu_idx"] = ",".join(
                    map(str, range(len(cpu_max_freqs)))
                )
            else:
                logger.info(
                    "Unsupported power_mode:{}".format(
                        self.config["power_mode"]
                    )  # noqa
                )
                exit(1)

            # battery level
            device_dict[device_serial_num]["battery_level"] = get_battery_level(  # noqa
                device_serial_num
            )

            # system version
            device_dict[device_serial_num][
                "system_version"
            ] = get_system_version(  # noqa
                device_serial_num
            )

            # imie
            device_dict[device_serial_num]["imei"] = get_imei(device_serial_num)  # noqa

            # ro.board.platform, ro.board.chiptype, ro.board.hardware
            device_soc_cmd = (
                "adb -s {} shell getprop |"
                " grep 'ro.board.platform'".format(device_serial_num)
            )
            cmd_handls = run_cmds([device_soc_cmd])
            soc = cmd_handls[device_soc_cmd][0]
            soc = soc.split(": ")[1].strip().replace("[", "").replace("]", "")  # noqa
            device_dict[device_serial_num]["soc"] = soc
            logger.debug(soc)

            # product
            device_product_cmd = (
                "adb -s {} shell getprop | "
                "grep 'ro.product.model'".format(device_serial_num)
            )
            cmd_handle = run_cmd(device_product_cmd)
            product = cmd_handle[0]
            product = (
                product.split(": ")[1].strip().replace("[", "").replace("]", "")  # noqa
            )  # noqa
            device_dict[device_serial_num]["product"] = product
            logger.debug(product)

        logger.debug(device_dict)
        logger.info("len(device_dict):{}".format(len(device_dict)))
        return device_dict