common.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #!/usr/bin/env python3
  2. import argparse
  3. import base64
  4. import copy
  5. import glob
  6. import imp
  7. import os
  8. import re
  9. import shlex
  10. import shutil
  11. import signal
  12. import stat
  13. import subprocess
  14. import sys
  15. this = sys.modules[__name__]
  16. # Default paths.
  17. root_dir = os.path.dirname(os.path.abspath(__file__))
  18. data_dir = os.path.join(root_dir, 'data')
  19. p9_dir = os.path.join(data_dir, '9p')
  20. gem5_non_default_src_root_dir = os.path.join(data_dir, 'gem5')
  21. out_dir = os.path.join(root_dir, 'out')
  22. bench_boot = os.path.join(out_dir, 'bench-boot.txt')
  23. dl_dir = os.path.join(out_dir, 'dl')
  24. submodules_dir = os.path.join(root_dir, 'submodules')
  25. buildroot_src_dir = os.path.join(submodules_dir, 'buildroot')
  26. gem5_default_src_dir = os.path.join(submodules_dir, 'gem5')
  27. linux_src_dir = os.path.join(submodules_dir, 'linux')
  28. qemu_src_dir = os.path.join(submodules_dir, 'qemu')
  29. parsec_benchmark_src_dir = os.path.join(submodules_dir, 'parsec-benchmark')
  30. # Other default variables.
  31. arch_map = {
  32. 'a': 'arm',
  33. 'A': 'aarch64',
  34. 'x': 'x86_64',
  35. }
  36. arches = [arch_map[k] for k in arch_map]
  37. gem5_cpt_prefix = '^cpt\.'
  38. sha = subprocess.check_output(['git', '-C', root_dir, 'log', '-1', '--format=%H']).decode().rstrip()
  39. config_file = os.path.join(data_dir, 'config')
  40. if os.path.exists(config_file):
  41. config = imp.load_source('config', config_file)
  42. configs = {x:getattr(config, x) for x in dir(config) if not x.startswith('__')}
  43. def base64_encode(string):
  44. return base64.b64encode(string.encode()).decode()
  45. def gem_list_checkpoint_dirs():
  46. '''
  47. List checkpoint directory, oldest first.
  48. '''
  49. global this
  50. prefix_re = re.compile(this.gem5_cpt_prefix)
  51. files = list(filter(lambda x: os.path.isdir(os.path.join(this.m5out_dir, x)) and prefix_re.search(x), os.listdir(this.m5out_dir)))
  52. files.sort(key=lambda x: os.path.getmtime(os.path.join(this.m5out_dir, x)))
  53. return files
  54. def get_argparse(default_args=None, argparse_args=None):
  55. '''
  56. Return an argument parser with common arguments set.
  57. :type default_args: Dict[str,str]
  58. :type argparse_args: Dict
  59. '''
  60. global this
  61. if default_args is None:
  62. default_args = {}
  63. if argparse_args is None:
  64. argparse_args = {}
  65. arch_choices = []
  66. for key in this.arch_map:
  67. arch_choices.append(key)
  68. arch_choices.append(this.arch_map[key])
  69. default_build_id = 'default'
  70. parser = argparse.ArgumentParser(
  71. formatter_class=argparse.RawTextHelpFormatter,
  72. **argparse_args
  73. )
  74. parser.add_argument(
  75. '-a', '--arch', choices=arch_choices, default='x86_64',
  76. help='CPU architecture. Default: %(default)s'
  77. )
  78. parser.add_argument(
  79. '-g', '--gem5', default=False, action='store_true',
  80. help='Use gem5 instead of QEMU'
  81. )
  82. parser.add_argument(
  83. '-L', '--linux-build-id', default=default_build_id,
  84. help='Linux build ID. Allows you to keep multiple separate Linux builds. Default: %(default)s'
  85. )
  86. parser.add_argument(
  87. '-M', '--gem5-build-id', default=default_build_id,
  88. help='gem5 build ID. Allows you to keep multiple separate gem5 builds. Default: %(default)s'
  89. )
  90. parser.add_argument(
  91. '-N', '--gem5-worktree',
  92. help='''\
  93. gem5 git worktree to use for build and Python scripts at runtime. Automatically
  94. create a new git worktree with the given id if one does not exist. If not
  95. given, just use the submodule source.
  96. '''
  97. )
  98. parser.add_argument(
  99. '-n', '--run-id', default='0',
  100. help='''\
  101. ID for run outputs such as gem5's m5out. Allows you to do multiple runs,
  102. and then inspect separate outputs later in different output directories.
  103. Default: %(default)s
  104. '''
  105. )
  106. parser.add_argument(
  107. '--port-offset', type=int,
  108. help='''\
  109. Increase the ports to be used such as for GDB by an offset to run multiple
  110. instances in parallel.
  111. Default: the run ID (-n) if that is an integer, otherwise 0.
  112. '''
  113. )
  114. parser.add_argument(
  115. '-Q', '--qemu-build-id', default=default_build_id,
  116. help='QEMU build ID. Allows you to keep multiple separate QEMU builds. Default: %(default)s'
  117. )
  118. parser.add_argument(
  119. '--buildroot-build-id',
  120. default=default_build_id,
  121. help='Buildroot build ID. Allows you to keep multiple separate gem5 builds. Default: %(default)s'
  122. )
  123. parser.add_argument(
  124. '-t', '--gem5-build-type', default='opt',
  125. help='gem5 build type, most often used for "debug" builds. Default: %(default)s'
  126. )
  127. if hasattr(this, 'configs'):
  128. defaults = this.configs.copy()
  129. else:
  130. defaults = {}
  131. defaults.update(default_args)
  132. # A bit ugly as it actually changes the defaults shown on --help, but we can't do any better
  133. # because it is impossible to check if arguments were given or not...
  134. # - https://stackoverflow.com/questions/30487767/check-if-argparse-optional-argument-is-set-or-not
  135. # - https://stackoverflow.com/questions/3609852/which-is-the-best-way-to-allow-configuration-options-be-overridden-at-the-comman
  136. parser.set_defaults(**defaults)
  137. return parser
  138. def add_build_arguments(parser):
  139. parser.add_argument(
  140. '--clean',
  141. help='Clean the build instead of building.',
  142. action='store_true',
  143. )
  144. def get_elf_entry(elf_file_path):
  145. global this
  146. readelf_header = subprocess.check_output([
  147. this.get_toolchain_tool('readelf'),
  148. '-h',
  149. elf_file_path
  150. ])
  151. for line in readelf_header.decode().split('\n'):
  152. split = line.split()
  153. if line.startswith(' Entry point address:'):
  154. addr = line.split()[-1]
  155. break
  156. return int(addr, 0)
  157. def get_stats(stat_re=None, stats_file=None):
  158. global this
  159. if stat_re is None:
  160. stat_re = '^system.cpu[0-9]*.numCycles$'
  161. if stats_file is None:
  162. stats_file = this.stats_file
  163. stat_re = re.compile(stat_re)
  164. ret = []
  165. with open(stats_file, 'r') as statfile:
  166. for line in statfile:
  167. if line[0] != '-':
  168. cols = line.split()
  169. if len(cols) > 1 and stat_re.search(cols[0]):
  170. ret.append(cols[1])
  171. return ret
  172. def get_toolchain_tool(tool):
  173. global this
  174. return glob.glob(os.path.join(this.host_bin_dir, '*-buildroot-*-{}'.format(tool)))[0]
  175. def log_error(msg):
  176. print('error: {}'.format(msg), file=sys.stderr)
  177. def print_cmd(cmd, cmd_file=None, extra_env=None):
  178. '''
  179. Format a command given as a list of strings so that it can
  180. be viewed nicely and executed by bash directly and print it to stdout.
  181. Optionally save the command to cmd_file file, and add extra_env
  182. environment variables to the command generated.
  183. '''
  184. newline_separator = ' \\\n'
  185. out = []
  186. for key in extra_env:
  187. out.extend(['{}={}'.format(shlex.quote(key), shlex.quote(extra_env[key])), newline_separator])
  188. for arg in cmd:
  189. out.extend([shlex.quote(arg), newline_separator])
  190. out = ''.join(out)
  191. print(out)
  192. if cmd_file is not None:
  193. with open(cmd_file, 'w') as f:
  194. f.write('#!/usr/bin/env bash\n')
  195. f.write(out)
  196. st = os.stat(cmd_file)
  197. os.chmod(cmd_file, st.st_mode | stat.S_IXUSR)
  198. def resolve_args(defaults, args, extra_args):
  199. if extra_args is None:
  200. extra_args = {}
  201. argcopy = copy.copy(args)
  202. argcopy.__dict__ = dict(list(defaults.items()) + list(argcopy.__dict__.items()) + list(extra_args.items()))
  203. return argcopy
  204. def rmrf(path):
  205. if os.path.exists(path):
  206. shutil.rmtree(path)
  207. def run_cmd(
  208. cmd,
  209. cmd_file=None,
  210. out_file=None,
  211. show_stdout=True,
  212. show_cmd=True,
  213. extra_env=None,
  214. delete_env=None,
  215. **kwargs
  216. ):
  217. '''
  218. Run a command. Write the command to stdout before running it.
  219. Wait until the command finishes execution.
  220. :param cmd: command to run
  221. :type cmd: List[str]
  222. :param cmd_file: if not None, write the command to be run to that file
  223. :type cmd_file: str
  224. :param out_file: if not None, write the stdout and stderr of the command the file
  225. :type out_file: str
  226. :param show_stdout: wether to show stdout and stderr on the terminal or not
  227. :type show_stdout: bool
  228. :param extra_env: extra environment variables to add when running the command
  229. :type extra_env: Dict[str,str]
  230. '''
  231. if out_file is not None:
  232. stdout = subprocess.PIPE
  233. stderr = subprocess.STDOUT
  234. else:
  235. if show_stdout:
  236. stdout = None
  237. stderr = None
  238. else:
  239. stdout = subprocess.DEVNULL
  240. stderr = subprocess.DEVNULL
  241. if extra_env is None:
  242. extra_env = {}
  243. if delete_env is None:
  244. delete_env = []
  245. env = os.environ.copy()
  246. env.update(extra_env)
  247. for key in delete_env:
  248. del env[key]
  249. if show_cmd:
  250. print_cmd(cmd, cmd_file, extra_env=extra_env)
  251. # Otherwise Ctrl + C gives:
  252. # - ugly Python stack trace for gem5 (QEMU takes over terminal and is fine).
  253. # - kills Python, and that then kills GDB: https://stackoverflow.com/questions/19807134/does-python-always-raise-an-exception-if-you-do-ctrlc-when-a-subprocess-is-exec
  254. signal.signal(signal.SIGINT, signal.SIG_IGN)
  255. # https://stackoverflow.com/questions/15535240/python-popen-write-to-stdout-and-log-file-simultaneously/52090802#52090802
  256. with subprocess.Popen(cmd, stdout=stdout, stderr=stderr, env=env, **kwargs) as proc:
  257. if out_file is not None:
  258. with open(out_file, 'bw') as logfile:
  259. while True:
  260. byte = proc.stdout.read(1)
  261. if byte:
  262. if show_stdout:
  263. sys.stdout.buffer.write(byte)
  264. sys.stdout.flush()
  265. logfile.write(byte)
  266. else:
  267. break
  268. signal.signal(signal.SIGINT, signal.SIG_DFL)
  269. return proc.returncode
  270. def setup(parser, **extra_args):
  271. '''
  272. Parse the command line arguments, and setup several variables based on them.
  273. Typically done after getting inputs from the command line arguments.
  274. '''
  275. global this
  276. args = parser.parse_args()
  277. if args.arch in this.arch_map:
  278. args.arch = this.arch_map[args.arch]
  279. if args.arch == 'arm':
  280. this.armv = 7
  281. this.gem5_arch = 'ARM'
  282. elif args.arch == 'aarch64':
  283. this.armv = 8
  284. this.gem5_arch = 'ARM'
  285. elif args.arch == 'x86_64':
  286. this.gem5_arch = 'X86'
  287. this.buildroot_build_dir = os.path.join(this.out_dir, 'buildroot', args.arch, args.buildroot_build_id)
  288. this.buildroot_config_file = os.path.join(this.buildroot_build_dir, '.config')
  289. this.build_dir = os.path.join(this.buildroot_build_dir, 'build')
  290. this.linux_build_dir = os.path.join(this.build_dir, 'linux-custom')
  291. this.linux_variant_dir = '{}.{}'.format(this.linux_build_dir, args.linux_build_id)
  292. this.vmlinux = os.path.join(this.linux_variant_dir, "vmlinux")
  293. this.qemu_build_dir = os.path.join(this.out_dir, 'qemu', args.qemu_build_id)
  294. this.qemu_executable = os.path.join(this.qemu_build_dir, '{}-softmmu'.format(args.arch), 'qemu-system-{}'.format(args.arch))
  295. this.qemu_img_executable = os.path.join(this.qemu_build_dir, 'qemu-img')
  296. this.qemu_guest_build_dir = os.path.join(this.build_dir, 'qemu-custom')
  297. this.host_dir = os.path.join(this.buildroot_build_dir, 'host')
  298. this.host_bin_dir = os.path.join(this.host_dir, 'usr', 'bin')
  299. this.images_dir = os.path.join(this.buildroot_build_dir, 'images')
  300. this.ext2_file = os.path.join(this.images_dir, 'rootfs.ext2')
  301. this.qcow2_file = os.path.join(this.images_dir, 'rootfs.ext2.qcow2')
  302. this.staging_dir = os.path.join(this.buildroot_build_dir, 'staging')
  303. this.target_dir = os.path.join(this.buildroot_build_dir, 'target')
  304. this.run_dir_base = os.path.join(this.out_dir, 'run')
  305. this.gem5_run_dir = os.path.join(this.run_dir_base, 'gem5', args.arch, str(args.run_id))
  306. this.m5out_dir = os.path.join(this.gem5_run_dir, 'm5out')
  307. this.stats_file = os.path.join(this.m5out_dir, 'stats.txt')
  308. this.trace_txt_file = os.path.join(this.m5out_dir, 'trace.txt')
  309. this.gem5_readfile = os.path.join(this.gem5_run_dir, 'readfile')
  310. this.gem5_termout_file = os.path.join(this.gem5_run_dir, 'termout.txt')
  311. this.qemu_run_dir = os.path.join(this.run_dir_base, 'qemu', args.arch, str(args.run_id))
  312. this.qemu_trace_basename = 'trace.bin'
  313. this.qemu_trace_file = os.path.join(this.qemu_run_dir, 'trace.bin')
  314. this.qemu_trace_txt_file = os.path.join(this.qemu_run_dir, 'trace.txt')
  315. this.qemu_termout_file = os.path.join(this.qemu_run_dir, 'termout.txt')
  316. this.qemu_rrfile = os.path.join(this.qemu_run_dir, 'rrfile')
  317. this.gem5_build_dir = os.path.join(this.out_dir, 'gem5', args.gem5_build_id)
  318. this.gem5_m5term = os.path.join(this.gem5_build_dir, 'm5term')
  319. this.gem5_build_build_dir = os.path.join(this.gem5_build_dir, 'build')
  320. this.gem5_executable = os.path.join(this.gem5_build_build_dir, gem5_arch, 'gem5.{}'.format(args.gem5_build_type))
  321. this.gem5_system_dir = os.path.join(this.gem5_build_dir, 'system')
  322. if args.gem5_worktree is not None:
  323. this.gem5_src_dir = os.path.join(this.gem5_non_default_src_root_dir, args.gem5_worktree)
  324. else:
  325. this.gem5_src_dir = this.gem5_default_src_dir
  326. if args.gem5:
  327. this.executable = this.gem5_executable
  328. this.run_dir = this.gem5_run_dir
  329. this.termout_file = this.gem5_termout_file
  330. else:
  331. this.executable = this.qemu_executable
  332. this.run_dir = this.qemu_run_dir
  333. this.termout_file = this.qemu_termout_file
  334. this.gem5_config_dir = os.path.join(this.gem5_src_dir, 'configs')
  335. this.gem5_se_file = os.path.join(this.gem5_config_dir, 'example', 'se.py')
  336. this.gem5_fs_file = os.path.join(this.gem5_config_dir, 'example', 'fs.py')
  337. this.run_cmd_file = os.path.join(this.run_dir, 'run.sh')
  338. if args.arch == 'arm':
  339. this.linux_image = os.path.join('arch', 'arm', 'boot', 'zImage')
  340. elif args.arch == 'aarch64':
  341. this.linux_image = os.path.join('arch', 'arm64', 'boot', 'Image')
  342. elif args.arch == 'x86_64':
  343. this.linux_image = os.path.join('arch', 'x86', 'boot', 'bzImage')
  344. this.linux_image = os.path.join(this.linux_variant_dir, linux_image)
  345. # Ports.
  346. if args.port_offset is None:
  347. try:
  348. args.port_offset = int(args.run_id)
  349. except ValueError:
  350. args.port_offset = 0
  351. if args.gem5:
  352. this.gem5_telnet_port = 3456 + args.port_offset
  353. this.gdb_port = 7000 + args.port_offset
  354. else:
  355. this.qemu_base_port = 45454 + 10 * args.port_offset
  356. this.qemu_monitor_port = this.qemu_base_port + 0
  357. this.qemu_hostfwd_generic_port = this.qemu_base_port + 1
  358. this.qemu_hostfwd_ssh_port = this.qemu_base_port + 2
  359. this.qemu_gdb_port = this.qemu_base_port + 3
  360. this.gdb_port = this.qemu_gdb_port
  361. return args
  362. def mkdir():
  363. global this
  364. os.makedirs(this.build_dir, exist_ok=True)
  365. os.makedirs(this.gem5_build_dir, exist_ok=True)
  366. os.makedirs(this.gem5_run_dir, exist_ok=True)
  367. os.makedirs(this.qemu_run_dir, exist_ok=True)
  368. os.makedirs(this.p9_dir, exist_ok=True)