|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +repo_root = Path(__file__).parent.parent.parent |
| 5 | + |
| 6 | +import sys |
| 7 | +sys.path.append(str(repo_root)) |
| 8 | +sys.path.append(f'{repo_root}/mpcstats') |
| 9 | + |
| 10 | +from common_lib import benchmark_dir, mpcstats_dir |
| 11 | + |
| 12 | +import argparse |
| 13 | +import subprocess |
| 14 | +import os |
| 15 | +import time |
| 16 | +from typing import List, Literal, Optional |
| 17 | +import json |
| 18 | +from common_lib import read_script |
| 19 | +from constants import MAX_MEM_USAGE_KB, EXEC_TIME_SEC |
| 20 | + |
| 21 | +# 1.rss (Resident Set Size): |
| 22 | +# - Measures the physical memory the process is currently using (in RAM). |
| 23 | +# - This is typically the most relevant for benchmarking how much memory a process is actively using. |
| 24 | +# -Best for benchmarking real memory usage because it shows the actual RAM being consumed by the process. |
| 25 | +# 2. vsz (Virtual Memory Size): |
| 26 | +# - Measures the total virtual memory the process is using, including memory that has been swapped out, memory that has been mapped but not used, and shared memory. |
| 27 | +# - Less relevant for benchmarking real memory usage because it includes parts of memory that aren’t physically loaded into RAM, such as mapped files and swapped-out memory. |
| 28 | + |
| 29 | +# TODO generate list from type definition |
| 30 | +MemoryFieldsType = Literal['rss', 'vsz'] |
| 31 | +MemoryFields = ['rss', 'vsz'] |
| 32 | + |
| 33 | +os.environ['PATH'] += os.pathsep + str(benchmark_dir) |
| 34 | + |
| 35 | +def parse_args(): |
| 36 | + parser = argparse.ArgumentParser(description="Benchmarking Script") |
| 37 | + parser.add_argument( |
| 38 | + 'protocol', |
| 39 | + type=str, |
| 40 | + help='MPC protocol', |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + 'num_parties', |
| 44 | + type=int, |
| 45 | + help='Number of participating parties', |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + '--name', |
| 49 | + type=str, |
| 50 | + default=f'computation', |
| 51 | + help='Name of the computation', |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + '--mem-field', |
| 55 | + type=str, |
| 56 | + choices=MemoryFields, |
| 57 | + default=MemoryFields[0], |
| 58 | + help='ps command field to retrieve memory usage', |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + '--edabit', |
| 62 | + action='store_true', |
| 63 | + help='Use edaBit', |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + '--mem-get-sleep', |
| 67 | + type=float, |
| 68 | + default=1, |
| 69 | + help='Time interval (in seconds) to sleep between memory retrievals for execution' |
| 70 | + ) |
| 71 | + parser.add_argument( |
| 72 | + '--file', |
| 73 | + type=str, |
| 74 | + help='Computation definition file. If not specified, the definition will be read from stdin', |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + '--comp-args', |
| 78 | + type=str, |
| 79 | + help='Arguments for `compile.py`', |
| 80 | + ) |
| 81 | + parser.add_argument( |
| 82 | + '--remote', |
| 83 | + type=int, |
| 84 | + help='Party number in remote execution', |
| 85 | + ) |
| 86 | + parser.add_argument( |
| 87 | + '--verbose-compiler', |
| 88 | + action='store_true', |
| 89 | + help='Show output from internally called scripts', |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + '--verbose-vm', |
| 93 | + action='store_true', |
| 94 | + help='Show output from vm', |
| 95 | + ) |
| 96 | + return parser.parse_args() |
| 97 | + |
| 98 | +def exec_ps(pid: int, field: MemoryFieldsType) -> int: |
| 99 | + if os.name == 'posix': |
| 100 | + res = subprocess.run( |
| 101 | + ['ps', '-o', f'{field}=', '-p', str(pid)], |
| 102 | + stdout=subprocess.PIPE, |
| 103 | + ) |
| 104 | + return int(res.stdout.decode().strip()) |
| 105 | + |
| 106 | +def gen_compile_cmd(args: argparse.Namespace) -> list[str]: |
| 107 | + compile_script = benchmark_dir / 'compile.py' |
| 108 | + opts = [] |
| 109 | + if args.comp_args is not None: |
| 110 | + opts.extend(args.comp_args.split()) |
| 111 | + if args.name: |
| 112 | + opts.extend(['--name', args.name]) |
| 113 | + if args.file: |
| 114 | + opts.extend(['--file', args.file]) |
| 115 | + if args.edabit: |
| 116 | + opts.append('--edabit') |
| 117 | + if args.verbose_compiler: |
| 118 | + opts.append('--verbose') |
| 119 | + |
| 120 | + return [compile_script] + opts |
| 121 | + |
| 122 | +def gen_executor_cmd(args: argparse.Namespace) -> list[str]: |
| 123 | + executor_script = benchmark_dir / 'executor.py' |
| 124 | + opts = [] |
| 125 | + if args.name: |
| 126 | + opts.extend(['--name', args.name]) |
| 127 | + if args.file: |
| 128 | + opts.extend(['--file', args.file]) |
| 129 | + if args.remote is not None: |
| 130 | + opts.extend(['--remote', str(args.remote)]) |
| 131 | + if args.verbose_vm: |
| 132 | + opts.append('--verbose') |
| 133 | + |
| 134 | + return [executor_script, args.protocol, str(args.num_parties)] + opts |
| 135 | + |
| 136 | +def monitor_mem_usage(proc: subprocess.Popen, mem_field: str, mem_get_sleep: float) -> int: |
| 137 | + max_mem_usage = 0 |
| 138 | + while proc.poll() is None: # While the process is running |
| 139 | + ps_output = subprocess.run(['ps', '-p', str(proc.pid), '-o', f'{mem_field}='], capture_output=True, text=True) |
| 140 | + mem_usage = int(ps_output.stdout.strip()) |
| 141 | + if mem_usage > max_mem_usage: |
| 142 | + max_mem_usage = mem_usage |
| 143 | + time.sleep(mem_get_sleep) |
| 144 | + |
| 145 | + return max_mem_usage |
| 146 | + |
| 147 | +def read_proc_stdout(proc: subprocess.Popen) -> list[str]: |
| 148 | + lines = [] |
| 149 | + while True: |
| 150 | + line = proc.stdout.readline() |
| 151 | + if not line: |
| 152 | + break |
| 153 | + lines.append(line.strip()) |
| 154 | + proc.wait() |
| 155 | + return lines |
| 156 | + |
| 157 | +def exec_cmd(cmd: list[str], computation_script: str, mem_field: str, mem_get_sleep: float, verbose: bool) -> object: |
| 158 | + proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 159 | + proc.stdin.write(computation_script.encode()) |
| 160 | + proc.stdin.close() |
| 161 | + |
| 162 | + lines = [] |
| 163 | + beg_time = time.time() |
| 164 | + try: |
| 165 | + max_mem_usage = monitor_mem_usage(proc, mem_field, mem_get_sleep) |
| 166 | + lines = read_proc_stdout(proc) |
| 167 | + if len(lines) == 0: |
| 168 | + return {} |
| 169 | + |
| 170 | + exec_time = time.time() - beg_time |
| 171 | + |
| 172 | + script = os.path.splitext(cmd[0].name)[0] |
| 173 | + |
| 174 | + # print out the script output excluding the last line |
| 175 | + if verbose: |
| 176 | + other_lines = [line.decode('utf-8') for line in lines[:-1]] |
| 177 | + print('\n'.join(other_lines)) |
| 178 | + |
| 179 | + # parse the last line to a json object |
| 180 | + res = json.loads(lines[-1].decode('utf-8')) |
| 181 | + |
| 182 | + res[f'{script}_{MAX_MEM_USAGE_KB}'] = max_mem_usage |
| 183 | + res[f'{script}_{EXEC_TIME_SEC}'] = exec_time |
| 184 | + return res |
| 185 | + |
| 186 | + except Exception as e: |
| 187 | + print(f'Error occurred while monitoring subprocess: {e}\n{lines}') |
| 188 | + proc.terminate() |
| 189 | + raise |
| 190 | + |
| 191 | +args = parse_args() |
| 192 | + |
| 193 | +# read computaiton script from file or stdin |
| 194 | +computation_script = read_script(open(args.file) if args.file else None) |
| 195 | + |
| 196 | +# execute compile script |
| 197 | +compile_result = exec_cmd(gen_compile_cmd(args), computation_script, args.mem_field, 0.1, args.verbose_compiler) |
| 198 | + |
| 199 | +# execute executor script |
| 200 | +executor_result = exec_cmd(gen_executor_cmd(args), computation_script, args.mem_field, args.mem_get_sleep, args.verbose_vm) |
| 201 | + |
| 202 | +print(json.dumps([compile_result, executor_result]), end='') |
0 commit comments