|
| 1 | +from libcachesim import open_trace, TraceType, get_trace_file_path, get_trace_file_lists |
| 2 | +from libcachesim.eviction import S3FIFO |
| 3 | +import pandas as pd |
| 4 | +import multiprocessing as mp |
| 5 | +from functools import partial |
| 6 | + |
| 7 | + |
| 8 | +def simulate(eviction_algo, trace_file_name: str, cache_size_ratio: float, ignore_obj_size: bool, eviction_algo_params: dict) -> None: |
| 9 | + # get the trace file path |
| 10 | + trace_file_path = get_trace_file_path(trace_file_name) |
| 11 | + # open the trace file |
| 12 | + reader = open_trace(trace_file_path, type=TraceType.ORACLE_GENERAL_TRACE, |
| 13 | + ignore_obj_size=ignore_obj_size) |
| 14 | + # create a cache with the eviction policy |
| 15 | + cache = eviction_algo(cache_size=int(reader.get_wss(ignore_obj_size=ignore_obj_size)*cache_size_ratio), **eviction_algo_params) |
| 16 | + # process the trace |
| 17 | + miss_ratio = cache.process_trace(reader) |
| 18 | + # return the miss ratio |
| 19 | + return miss_ratio |
| 20 | + |
| 21 | + |
| 22 | +def simulate_single_trace(args): |
| 23 | + """单个trace文件的模拟函数,用于多进程""" |
| 24 | + trace_file_name, eviction_algo, cache_size_ratio, ignore_obj_size, eviction_algo_params = args |
| 25 | + try: |
| 26 | + miss_ratio = simulate(eviction_algo, trace_file_name, cache_size_ratio, ignore_obj_size, eviction_algo_params) |
| 27 | + return {"trace_file_name": trace_file_name, "miss_ratio": miss_ratio, "status": "success"} |
| 28 | + except Exception as e: |
| 29 | + return {"trace_file_name": trace_file_name, "miss_ratio": None, "status": f"error: {str(e)}"} |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + ignore_obj_size = True |
| 34 | + cache_size_ratio = 0.01 |
| 35 | + eviction_algo = S3FIFO |
| 36 | + eviction_algo_params = { |
| 37 | + "fifo_size_ratio": 0.1, |
| 38 | + "move_to_main_threshold": 2, |
| 39 | + } |
| 40 | + |
| 41 | + # get the trace file path |
| 42 | + files = get_trace_file_lists("msr") |
| 43 | + miss_ratios = [] |
| 44 | + for file in files: |
| 45 | + trace_file_path = get_trace_file_path(file) |
| 46 | + # open the trace file |
| 47 | + reader = open_trace(trace_file_path, type=TraceType.ORACLE_GENERAL_TRACE, ignore_obj_size=ignore_obj_size) |
| 48 | + # create a cache with the eviction policy |
| 49 | + cache = eviction_algo(cache_size=int(reader.get_wss(ignore_obj_size=ignore_obj_size)*cache_size_ratio), **eviction_algo_params) |
| 50 | + # process the trace |
| 51 | + miss_ratio = cache.process_trace(reader) |
| 52 | + miss_ratios.append(miss_ratio) |
| 53 | + |
| 54 | + print(f"Miss ratio: {miss_ratios}") |
| 55 | + |
| 56 | + |
| 57 | +def simulate_all_traces_parallel(num_processes=None): |
| 58 | + """使用多进程并行处理所有trace文件""" |
| 59 | + if num_processes is None: |
| 60 | + num_processes = mp.cpu_count() |
| 61 | + |
| 62 | + print(f"使用 {num_processes} 个进程进行并行处理...") |
| 63 | + |
| 64 | + # 读取trace文件列表 |
| 65 | + df = pd.read_csv("trace_set_files.csv") |
| 66 | + |
| 67 | + # 准备参数 |
| 68 | + eviction_algo = S3FIFO |
| 69 | + cache_size_ratio = 0.01 |
| 70 | + ignore_obj_size = True |
| 71 | + eviction_algo_params = {"fifo_size_ratio": 0.1, "move_to_main_threshold": 2} |
| 72 | + |
| 73 | + # 准备任务参数 |
| 74 | + tasks = [] |
| 75 | + for index, row in df.iterrows(): |
| 76 | + trace_file_name = str(row["trace_file_name"]) |
| 77 | + task_args = (trace_file_name, eviction_algo, cache_size_ratio, ignore_obj_size, eviction_algo_params) |
| 78 | + tasks.append(task_args) |
| 79 | + |
| 80 | + print(f"总共需要处理 {len(tasks)} 个trace文件") |
| 81 | + |
| 82 | + # 使用多进程池处理 |
| 83 | + results = [] |
| 84 | + with mp.Pool(processes=num_processes) as pool: |
| 85 | + # 使用imap来显示进度 |
| 86 | + for i, result in enumerate(pool.imap(simulate_single_trace, tasks)): |
| 87 | + results.append(result) |
| 88 | + if (i + 1) % 10 == 0: |
| 89 | + print(f"已处理 {i + 1}/{len(tasks)} 个文件") |
| 90 | + |
| 91 | + # 处理结果 |
| 92 | + successful_results = [r for r in results if r["status"] == "success"] |
| 93 | + failed_results = [r for r in results if r["status"] != "success"] |
| 94 | + |
| 95 | + print(f"\n处理完成!") |
| 96 | + print(f"成功处理: {len(successful_results)} 个文件") |
| 97 | + print(f"失败处理: {len(failed_results)} 个文件") |
| 98 | + |
| 99 | + if failed_results: |
| 100 | + print("\n失败的文件:") |
| 101 | + for result in failed_results: |
| 102 | + print(f" {result['trace_file_name']}: {result['status']}") |
| 103 | + |
| 104 | + # 提取miss ratios |
| 105 | + miss_ratios = [r["miss_ratio"] for r in successful_results] |
| 106 | + print(f"\nMiss ratios: {miss_ratios}") |
| 107 | + |
| 108 | + # 保存结果到CSV |
| 109 | + results_df = pd.DataFrame(results) |
| 110 | + results_df.to_csv("simulation_results.csv", index=False) |
| 111 | + print("结果已保存到 simulation_results.csv") |
| 112 | + |
| 113 | + return results |
| 114 | + |
| 115 | + |
| 116 | +def simulate_all_traces(): |
| 117 | + """原始的单进程版本""" |
| 118 | + df = pd.read_csv("trace_set_files.csv") |
| 119 | + miss_ratios = [] |
| 120 | + for index, row in df.iterrows(): |
| 121 | + trace_file_name = str(row["trace_file_name"]) |
| 122 | + miss_ratio = simulate(S3FIFO, trace_file_name, 0.01, True, {"fifo_size_ratio": 0.1, "move_to_main_threshold": 2}) |
| 123 | + miss_ratios.append(miss_ratio) |
| 124 | + print(f"Miss ratio: {miss_ratios}") |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + # 使用多进程版本 |
| 129 | + simulate_all_traces_parallel() |
0 commit comments