Пример #1
0
def run_loop_test(top_cfg, run=False):
    # use loop to get all intervals and print to file
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)

    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    os.chdir(top_cfg["dir_out"])
    for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
        if top_cfg["partial_test"] == True and dirname not in top_cfg[
                "user_test_list"]:
            continue
        os.chdir(dirname)
        for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
            os.chdir(inputs)

            if run == False:
                print(
                    os.path.join(top_cfg["simget_home"], "bin/perf_ov_loop") +
                    " " +
                    os.path.join(os.getcwd(), simpoint_warm_cfg_prefix +
                                 "loop_cfg.json"))
            else:
                subprocess.run(
                    os.path.join(top_cfg["simget_home"], "bin/perf_ov_loop") +
                    " " +
                    os.path.join(os.getcwd(),
                                 simpoint_warm_cfg_prefix + "loop_cfg.json"),
                    shell=True)

            os.chdir("..")
        os.chdir("..")

    return
Пример #2
0
def calc_criu_all(top_cfg, run=False):
    '''
    calculater criu restored(according to simpoint) ipc result for all checkpoint
    if one simpoint has more than one checkpoint, it will restore the nearest one
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        run: bool, @optional
            run perf_restore_cnt if true, print commond otherwise, pass to calc_criu_simpoint
    '''
    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)

    os.chdir(top_cfg["dir_out"])
    criu_res_dict = {}
    criu_res_dict["time"] = time.asctime(time.localtime(time.time()))
    criu_res_dict["warmup_ratio"] = top_cfg["warmup_ratio"]
    criu_res_dict["interval_size"] = top_cfg["interval_size"]
    criu_res_dict["maxK"] = top_cfg["simpoint"]["maxK"]
    with open(get_criu_json_filename(simpoint_warm_cfg_prefix), 'w') as json_file:
        for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
            if top_cfg["partial_test"] == True and dirname not in top_cfg["user_test_list"]:
                continue
            os.chdir(dirname)
            criu_res_dict[dirname] = {}
            for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
                os.chdir(inputs)
                criu_res_dict[dirname][inputs] = {}
                try:
                    local_cfg_file = open(
                        simpoint_warm_cfg_prefix+"loop_cfg.json", "r")
                    local_cfg = json.load(local_cfg_file)
                    criu_res_dict[dirname][inputs]["points"] = local_cfg["simpoint"]["k"]
                    criu_res_dict[dirname][inputs]["size"] = subprocess.getoutput(
                        "du -sh "+local_cfg["image_dir"]).split()[0]
                    print(dirname, inputs)
                    criu_res_dict[dirname][inputs]["insts"], \
                        criu_res_dict[dirname][inputs]["cycles"], \
                        criu_res_dict[dirname][inputs]["ipc"] = \
                        calc_criu_simpoint(top_cfg, local_cfg, run)
                    print("ipc:", criu_res_dict[dirname][inputs]["ipc"])

                except Exception as exc:
                    print("run error!", exc)

                os.chdir("..")
            os.chdir("..")

        json.dump(criu_res_dict, json_file, indent=4)

    print_criu_result(top_cfg, get_criu_json_filename(
        simpoint_warm_cfg_prefix, False))
    return
Пример #3
0
def run_simpoint(top_cfg, run=False):
    '''
    use simpoint to generate points
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        run: bool, @optional
            run if true, print commond if false
    '''
    simpoint_cfg_prefix = get_simpoint_cfg_prefix(top_cfg)
    pool = Pool(int(cpu_count() / 2))

    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    os.chdir(top_cfg["dir_out"])
    for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
        if top_cfg["partial_test"] == True and dirname not in top_cfg[
                "user_test_list"]:
            continue
        os.chdir(dirname)
        for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
            os.chdir(inputs)
            simpoint_full_cmd = simpoint_cmd(
                top_cfg, "simpoint.bb", simpoint_cfg_prefix + "sim.points",
                simpoint_cfg_prefix + "sim.weights")
            if run == False:
                pool.apply_async(func=sys.stdout.write, args=simpoint_full_cmd)
            else:
                pool.apply_async(func=subprocess.run,
                                 kwds={
                                     "args": simpoint_full_cmd,
                                     "shell": True,
                                     "cwd": os.getcwd()
                                 })
            os.chdir("..")
        os.chdir("..")

    pool.close()
    print("waiting for calc...")
    pool.join()
    return
Пример #4
0
def rm_criu_file_size_check_all(top_cfg, ignore_list):
    '''
    loop rm files.img size and mode check
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        ignore_list: list, @request
            file names list in which the function will remove file size and mode check
    '''
    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    pool = Pool(int(cpu_count() / 2))
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)
    os.chdir(top_cfg["dir_out"])
    for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
        if top_cfg["partial_test"] == True and dirname not in top_cfg[
                "user_test_list"]:
            continue
        os.chdir(dirname)
        for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
            os.chdir(inputs)
            dump_path = simpoint_warm_cfg_prefix + "dump"
            if os.path.exists(dump_path) == False:
                os.chdir("..")
                continue

            os.chdir(dump_path)
            for checkpoint in filter(os.path.isdir, os.listdir(os.getcwd())):
                pool.apply_async(
                    func=rm_criu_file_size_check,
                    args=[ignore_list,
                          os.path.join(os.getcwd(), checkpoint)])

            os.chdir("..")
            os.chdir("..")
        os.chdir("..")
    pool.close()
    print("waiting for processing...")
    pool.join()

    return
Пример #5
0
def dump_criu_all(top_cfg, run=False, bias_check=True, bias_clean=True):
    '''
    dump all criu checkpoint into a folder named by checkpoint insts
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        run: bool, @optional
            run perf_loop_dump if true, print commond otherwise
        bias_check: bool, @optional
            check if dump point is correct(the instructions error between a dumped point and simpoint results is less than an interval)
        bias_clean: bool, @optional
            remove the image if the bias is too large
    '''
    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)
    os.chdir(top_cfg["dir_out"])
    for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
        if top_cfg["partial_test"] == True and dirname not in top_cfg["user_test_list"]:
            continue
        os.chdir(dirname)
        for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
            os.chdir(inputs)

            cmd = os.path.join(
                top_cfg["simget_home"], "bin/perf_loop_dump") + " " + os.path.join(os.getcwd(), simpoint_warm_cfg_prefix + "loop_cfg.json")
            if run == True:
                subprocess.run(cmd, shell=True)
            else:
                print(cmd)

            loop_cfg_filename = simpoint_warm_cfg_prefix+"loop_cfg.json"
            with open(loop_cfg_filename, 'r') as f:
                loop_cfg = json.load(f)
            os.chdir(simpoint_warm_cfg_prefix+"dump")
            dir_list = filter(os.path.isdir, os.listdir(os.getcwd()))
            dir_list = [int(item) for item in dir_list]
            least_offset_list = []
            idx = 0
            for point in loop_cfg["simpoint"]["points"]:
                o_cfg = loop_cfg.copy()
                o_cfg["simpoint"]["current"] = idx
                if point < int(loop_cfg["process"]["warmup_ratio"]):
                    target = 0
                else:
                    target = (point-int(loop_cfg["process"]["warmup_ratio"])) * \
                        int(loop_cfg["process"]["ov_insts"])
                min_abs = sys.maxsize
                for d in dir_list:
                    if min_abs > abs(target-d):
                        min_abs = abs(target-d)
                        target_dir = d
                if bias_check == True and min_abs > int(loop_cfg["process"]["ov_insts"]):
                    print("no suitable checkpoint for", dirname, inputs, idx)
                    idx += 1
                    continue

                least_offset_list.append(str(target_dir))
                o_cfg["image_dir"] = os.path.join(
                    loop_cfg["image_dir"], str(target_dir))

                files = os.listdir(str(target_dir))
                for img_file in files:
                    pids = re.findall(r"mm-(\d+)\.img", img_file)
                    if pids:
                        o_cfg["process"]["pid"] = int(pids[0])
                        break
                with open(str(target_dir) + "_restore_cfg.json", 'w') as f:
                    json.dump(o_cfg, f, indent=4)
                idx += 1

            for f in os.listdir(os.getcwd()):
                if len(re.findall(r"\d+", f)) > 0 and re.findall(r"\d+", f)[0] not in least_offset_list:
                    print("remove checkpoint ", f)
                    if bias_clean == True:
                        if os.path.isdir(f) == True:
                            shutil.rmtree(f)
                        else:
                            os.remove(f)
                # elif clean == True and os.path.exists(directory+"_restore_cfg.json") == False:
                #     shutil.rmtree(directory)

            os.chdir("../..")
        os.chdir("..")
    return
Пример #6
0
def gen_perf_loop_cfg_file(top_cfg, cmd_list):
    '''
    for each folder, generate a .json file which can be used by perf* binary files(also used by criu dump, but not used by criu restore)
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        cmd_list: list, @request
            raw commond list, can be generated by gen_run_cmd_list or read from file
    '''
    simpoint_cfg_prefix = get_simpoint_cfg_prefix(top_cfg)
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)

    if os.path.exists(top_cfg["dir_out"]) == False:
        os.mkdir(top_cfg["dir_out"])
    os.chdir(top_cfg["dir_out"])
    calc_file = open("calc_file.txt", 'w')

    for cmd_set in cmd_list:
        test_name = cmd_set[0]["path"].split('/')[-3]
        if top_cfg["partial_test"] == True and test_name not in top_cfg[
                "user_test_list"]:
            continue
        if os.path.exists(test_name) == False:
            os.mkdir(test_name)
        os.chdir(test_name)

        i = 0
        for cmd in cmd_set:
            i = i + 1
            save_dir = "run" + str(i)
            if os.path.exists(save_dir) == False:
                os.mkdir(save_dir)
            os.chdir(save_dir)
            cur_dir = os.getcwd()

            process_cfg = {}
            process_cfg["path"] = cmd["path"]
            process_cfg["filename"] = cmd["run"].split(" ")[0]
            process_cfg["args"] = cmd["run"].strip().split(" ")[1:]
            if cmd["input_file"] != None:
                process_cfg["input_from_file"] = 1
                process_cfg["file_in"] = os.path.join(
                    cmd["path"], cmd["input_file"].strip())
            process_cfg["affinity"] = top_cfg["perf_config"]["affinity"]
            process_cfg["ov_insts"] = str(top_cfg["interval_size"])
            process_cfg["irq_offset"] = top_cfg["perf_config"]["irq_offset"]
            process_cfg["warmup_ratio"] = top_cfg["warmup_ratio"]

            loop_cfg = {}
            loop_cfg["out_file"] = os.path.join(
                cur_dir, simpoint_cfg_prefix + "loop_intervals.log")

            simpoint_cfg = {}
            simpoint_cfg["point_file"] = os.path.join(
                cur_dir, simpoint_cfg_prefix + "sim.points")
            simpoint_cfg["weight_file"] = os.path.join(
                cur_dir, simpoint_cfg_prefix + "sim.weights")

            k = 0
            point_weight_pair = []
            points = []
            weights = []
            with open(os.path.join(cur_dir, simpoint_cfg_prefix +
                                   "sim.points")) as points_file:
                for line in points_file.readlines():
                    point_weight_pair.append([
                        int(line.split(' ')[0]),
                    ])

            with open(
                    os.path.join(cur_dir, simpoint_cfg_prefix +
                                 "sim.weights")) as weights_file:
                for line in weights_file.readlines():
                    point_weight_pair[k].append(float(line.split(' ')[0]))
                    k += 1

            point_weight_pair.sort(key=lambda x: x[0])
            for pair in point_weight_pair:
                points.append(pair[0])
                weights.append(pair[1])

            simpoint_cfg["k"] = k
            simpoint_cfg["points"] = points
            simpoint_cfg["weights"] = weights

            perf_cfg = {}
            if os.path.exists(simpoint_warm_cfg_prefix + "dump") == False:
                os.mkdir(simpoint_warm_cfg_prefix + "dump")
            perf_cfg["image_dir"] = os.path.join(
                cur_dir, simpoint_warm_cfg_prefix + "dump")
            perf_cfg["process"] = process_cfg
            perf_cfg["loop"] = loop_cfg
            perf_cfg["simpoint"] = simpoint_cfg

            with open(simpoint_warm_cfg_prefix + "loop_cfg.json", 'w') as f:
                json.dump(perf_cfg, f, indent=4)

            print(test_name + "/" + save_dir + '\t' + str(k), file=calc_file)
            for p, w in zip(points, weights):
                print(str(p) + '\t' + str(w), file=calc_file)
            print('\n', file=calc_file)

            os.chdir("..")
        os.chdir("..")
    calc_file.close()
    return
Пример #7
0
def traverse_raw_cmd(top_cfg, cmd_list, method="valgrind", run=False):
    '''
    traverse spec raw cmds with different methods
    Args:
        top_cfg: dict, @request
            base config file, see top_cfg_example.json
        cmd_list: list, @request
            raw commond list, can be generated by gen_run_cmd_list or read from file
        method: string, @request
            use this method to traverse raw commond, now support:valgrind, qemu-user, perf and inst-cnt
        run: bool, @optional
            run if true, print commond if false
    '''
    if os.path.exists(top_cfg["dir_out"]) == False:
        os.mkdir(top_cfg["dir_out"])
    os.chdir(top_cfg["dir_out"])

    pool = Pool(int(cpu_count() / 2 if cpu_count() > 1 else 1))

    full_cmd_list = []
    bb_file = None
    perf_file = None
    for cmd_set in cmd_list:
        test_name = cmd_set[0]["path"].split('/')[-3]
        if top_cfg["partial_test"] == True and test_name not in top_cfg[
                "user_test_list"]:
            continue

        if os.path.exists(test_name) == False:
            os.mkdir(test_name)
        os.chdir(test_name)

        i = 0
        for cmd in cmd_set:
            i = i + 1
            save_dir = "run" + str(i)
            if os.path.exists(save_dir) == False:
                os.mkdir(save_dir)
            cur_dir = os.getcwd()

            if method == "valgrind":
                bb_file = os.path.join(cur_dir, save_dir, "simpoint.bb")
                full_cmd = valgrind_cmd(top_cfg, bb_file) + " ./" + cmd["run"]
            elif method == "qemu-user":
                bb_file = os.path.join(cur_dir, save_dir, "simpoint.bb")
                full_cmd = qemu_user_cmd(top_cfg, bb_file) + " ./" + cmd["run"]
            elif method == "perf":
                perf_file = os.path.join(cur_dir, save_dir, "perf.result")
                full_cmd = perf_cmd(perf_file) + \
                    " taskset -c 3 ./" + cmd["run"]
            elif method == "pin":
                pin_file = os.path.join(cur_dir, save_dir, "pin.result")
                full_cmd = pin_cmd(top_cfg, pin_file) + " ./" + cmd["run"]
            elif method == "mycnt":
                cfg_file = os.path.join(cur_dir, save_dir,
                                        get_simpoint_cfg_prefix(
                                            top_cfg, True)) + "loop_cfg.json"
                target_file = os.path.join(cur_dir, save_dir, "mycnt.result")
                full_cmd = mycnt_cmd(top_cfg, cfg_file, target_file)
            else:
                raise ValueError

            if method != "mycnt":
                if cmd["input_file"] != None:
                    full_cmd += " < " + cmd["input_file"]
                full_cmd += " > std.out 2>> std.err"

            if run == True:
                if method == "perf" or method == "mycnt":
                    subprocess.run(full_cmd, shell=True, cwd=cmd["path"])
                else:
                    pool.apply_async(func=subprocess.run,
                                     kwds={
                                         "args": full_cmd,
                                         "shell": True,
                                         "cwd": cmd["path"]
                                     })
            else:
                full_cmd_list.append(full_cmd)

        os.chdir("..")

    if run == True:
        pool.close()
        print("waiting for calc...")
        pool.join()
    else:
        print("============== full cmds ==============")
        for each in full_cmd_list:
            print(each)

    return
Пример #8
0
def save_fixed_result(top_cfg):
    # save fixed results to json
    simpoint_warm_cfg_prefix = get_simpoint_cfg_prefix(top_cfg, True)

    if os.path.exists(top_cfg["dir_out"]) == False:
        print("no folder exists!")
        return
    os.chdir(top_cfg["dir_out"])
    collect_dict = {}
    collect_dict["time"] = time.asctime(time.localtime(time.time()))
    collect_dict["platform"] = platform.processor()
    for dirname in filter(os.path.isdir, os.listdir(os.getcwd())):
        os.chdir(dirname)
        dir_result = {}
        for inputs in filter(os.path.isdir, os.listdir(os.getcwd())):
            os.chdir(inputs)
            input_result = {}
            bb_result = {}
            loop_result = {}
            avg_result = {}
            simpoint_result = {}
            perf_result = {}
            pin_result = {}
            mycnt_result = {}

            try:
                bb_file = open("simpoint.bb", 'r')
                bb_insts = re.compile(r"#\s*Total instructions:\s*(\d+)")
                for line in bb_file.readlines():
                    match_insts = bb_insts.match(line)
                    if match_insts:
                        bb_result["insts"] = int(
                            match_insts.group(1).replace(',', ''))
            except FileNotFoundError:
                print(dirname, inputs, "simpoint.bb not found")

            try:
                perf_file = open("perf.result", 'r')
                perf_insts = re.compile(r"\s*([\d,]*)\s*instructions:u.*")
                perf_cycles = re.compile(r"\s*([\d,]*)\s*cycles:u.*")
                for line in perf_file.readlines():
                    match_insts = perf_insts.match(line)
                    match_cycles = perf_cycles.match(line)
                    if match_insts:
                        perf_result["insts"] = int(
                            match_insts.group(1).replace(',', ''))
                    elif match_cycles:
                        perf_result["cycles"] = int(
                            match_cycles.group(1).replace(',', ''))
            except FileNotFoundError:
                print(dirname, inputs, "perf.result not found")

            try:
                pin_file = open("pin.result", 'r')
                pin_insts = re.compile(r"Count ([\d]*)")
                for line in pin_file.readlines():
                    match_insts = pin_insts.match(line)
                    if match_insts:
                        pin_result["insts"] = int(match_insts.group(1))
            except FileNotFoundError:
                print(dirname, inputs, "pin.result not found")

            try:
                perf_file = open("mycnt.result", 'r')
                mycnt_insts = re.compile(r"total inst counts:([\d,]*)")
                mycnt_cycles = re.compile(r"total cycle counts:([\d,]*)")
                for line in perf_file.readlines():
                    match_insts = mycnt_insts.match(line)
                    match_cycles = mycnt_cycles.match(line)
                    if match_insts:
                        mycnt_result["insts"] = int(
                            match_insts.group(1).replace(',', ''))
                    if match_cycles:
                        mycnt_result["cycles"] = int(
                            match_cycles.group(1).replace(',', ''))
            except FileNotFoundError:
                print(dirname, inputs, "mycnt.result not found")

            try:
                loop_cfg_file = open(
                    simpoint_warm_cfg_prefix + "loop_cfg.json", 'r')
                loop_cfg = json.load(loop_cfg_file)
                loop_result["insts"], loop_result["cycles"], loop_result["ipc"], \
                    avg_result["insts"], avg_result["cycles"], avg_result["ipc"], \
                    simpoint_result["insts"], simpoint_result["cycles"], simpoint_result["ipc"] = \
                    resolve_loop_result_file(loop_cfg)
            except Exception as exc:
                print(dirname, inputs, "Exception", exc)

            if len(bb_result) > 0:
                input_result["bb_result"] = bb_result
            if len(perf_result) > 0:
                input_result["perf_result"] = perf_result
            if len(pin_result) > 0:
                input_result["pin_result"] = pin_result
            if len(mycnt_result) > 0:
                input_result["mycnt_result"] = mycnt_result

            if len(loop_result) > 0:
                input_result["loop_result"] = loop_result
            if len(avg_result) > 0:
                input_result["avg_result"] = avg_result
            if len(simpoint_result) > 0:
                input_result["simpoint_result"] = simpoint_result

            if len(input_result) > 0:
                dir_result[inputs] = input_result
            os.chdir("..")
            collect_dict[dirname] = dir_result
        os.chdir("..")

    with open("fix_test_res.json", 'w') as f:
        json.dump(collect_dict, f, indent=4)