aboutsummaryrefslogtreecommitdiff
path: root/salis.py
diff options
context:
space:
mode:
Diffstat (limited to 'salis.py')
-rwxr-xr-xsalis.py560
1 files changed, 161 insertions, 399 deletions
diff --git a/salis.py b/salis.py
index 4f1553c..896bc71 100755
--- a/salis.py
+++ b/salis.py
@@ -1,19 +1,15 @@
#!/usr/bin/env -S PYTHONDONTWRITEBYTECODE=1 python
-import contextlib
import ctypes
import json
import os
import random
import shutil
-import sqlite3
import subprocess
import sys
-import urllib.parse
-import urllib.request
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, ArgumentTypeError, RawTextHelpFormatter
-from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
+from importlib.machinery import SourceFileLoader
from tempfile import TemporaryDirectory
# ------------------------------------------------------------------------------
@@ -21,22 +17,16 @@ from tempfile import TemporaryDirectory
# ------------------------------------------------------------------------------
description = "Salis: Simple A-Life Simulator"
prog = sys.argv[0]
-epilog = f"Use '-h' to list arguments for each command.\nExample: '{prog} bench -h'"
+epilog = f"Use '-h' to list arguments for each command.\nExample: '{prog} new -h'"
-main_parser = ArgumentParser(
- description=description,
- epilog=epilog,
- formatter_class=RawTextHelpFormatter,
- prog=prog,
-)
-
-parsers = main_parser.add_subparsers(dest="command", required=True)
+main_parser = ArgumentParser(description=description, epilog=epilog, formatter_class=RawTextHelpFormatter, prog=prog)
+sub_parsers = main_parser.add_subparsers(dest="command", required=True)
formatter_class = lambda prog: ArgumentDefaultsHelpFormatter(prog, max_help_position=32)
-bench = parsers.add_parser("bench", formatter_class=formatter_class, help="run benchmark")
-load = parsers.add_parser("load", formatter_class=formatter_class, help="load saved simulation")
-new = parsers.add_parser("new", formatter_class=formatter_class, help="create new simulation")
-serve = parsers.add_parser("serve", formatter_class=formatter_class, help="run data server")
+new = sub_parsers.add_parser("new", formatter_class=formatter_class, help="create new simulation")
+load = sub_parsers.add_parser("load", formatter_class=formatter_class, help="load saved simulation")
+server = sub_parsers.add_parser("server", formatter_class=formatter_class, help="run data server")
+client = sub_parsers.add_parser("client", formatter_class=formatter_class, help="run data client")
architectures = os.listdir("./arch")
uis = os.listdir("./ui")
@@ -63,29 +53,29 @@ def iport(i):
option_keys = ["short", "long", "metavar", "help", "default", "required", "type", "parsers"]
option_list = [
- ["A", "anc", "ANC", "ancestor file name without extension, to be compiled on all cores (ANC points to 'anc/{arch}/{ANC}.asm')", None, True, str, [bench, new]],
- ["a", "arch", architectures, "VM architecture", "dummy", False, str, [bench, new]],
- ["b", "steps", "N", "number of steps to run in benchmark", 0x1000000, False, ipos, [bench]],
- ["C", "clones", "N", "number of ancestor clones on each core", 1, False, inat, [bench, new]],
- ["c", "cores", "N", "number of simulator cores", 2, False, inat, [bench, new]],
+ ["A", "anc", "ANC", "ancestor file name without extension, to be compiled on all cores (ANC points to 'anc/{arch}/{ANC}.asm')", None, True, str, [new]],
+ ["a", "arch", architectures, "VM architecture", "dummy", False, str, [new]],
+ ["C", "clones", "N", "number of ancestor clones on each core", 1, False, inat, [new]],
+ ["c", "cores", "N", "number of simulator cores", 2, False, inat, [new]],
["d", "data-push-pow", "POW", "data aggregation interval exponent (interval == 2^{POW} >= {sync-pow}); a value of 0 disables data aggregation (requires 'sqlite')", 28, False, ipos, [new]],
["f", "force", None, "overwrite existing simulation of given name", False, False, bool, [new]],
- ["F", "muta-flip", None, "cosmic rays flip bits instead of randomizing whole bytes", False, False, bool, [bench, new]],
- ["g", "compiler", "CC", "C compiler to use", "gcc", False, str, [bench, load, new, serve]],
- ["G", "compiler-flags", "FLAGS", "base set of flags to pass to C compiler", "-Wall -Wextra -Werror -pedantic", False, str, [bench, load, new, serve]],
- ["M", "muta-pow", "POW", "mutator range exponent (range == 2^{POW})", 32, False, ipos, [bench, new]],
- ["m", "mvec-pow", "POW", "memory vector size exponent (size == 2^{POW})", 20, False, ipos, [bench, new]],
- ["n", "name", "NAME", "name of new or loaded simulation", "def.sim", False, str, [load, new, serve]],
- ["o", "optimized", None, "build with optimizations", False, False, bool, [bench, load, new, serve]],
- ["P", "port", "PORT", "port number for data server", 8080, False, iport, [serve]],
- ["p", "pre-cmd", "CMD", "shell command to wrap call to executable (e.g. gdb, time, valgrind, etc.)", None, False, str, [bench, load, new]],
- ["s", "seed", "SEED", "seed value for new simulation; a value of 0 disables cosmic rays; a value of -1 creates a random seed", 0, False, seed, [bench, new]],
- ["T", "keep-temp-dir", None, "delete temporary directory on exit", False, False, bool, [bench, load, new, serve]],
- ["t", "thread-gap", "N", "memory gap between cores in bytes (may help reduce cache misses)", 0x100, False, inat, [bench, load, new, serve]],
- ["u", "ui", uis, "user interface", "curses", False, str, [load, new]],
- ["U", "update", None, "update vendors (call 'git commit' afterwards to track updated files, if any)", False, False, bool, [serve]],
+ ["F", "muta-flip", None, "cosmic rays flip bits instead of randomizing whole bytes", False, False, bool, [new]],
+ ["g", "compiler", "CC", "C compiler to use", "gcc", False, str, [new, load, server, client]],
+ ["G", "compiler-flags", "FLAGS", "base set of flags to pass to C compiler", "-Wall -Wextra -Werror -pedantic", False, str, [new, load, server, client]],
+ ["H", "home", "PATH", "salis home directory", os.path.join(os.environ["HOME"], ".salis"), False, str, [new, load, server]],
+ ["i", "ip", "IP", "ip address of data server", "127.0.0.1", False, str, [client]],
+ ["M", "muta-pow", "POW", "mutator range exponent (range == 2^{POW})", 32, False, ipos, [new]],
+ ["m", "mvec-pow", "POW", "memory vector size exponent (size == 2^{POW})", 20, False, ipos, [new]],
+ ["n", "name", "NAME", "name of new or loaded simulation", "def.sim", False, str, [new, load, server]],
+ ["o", "optimized", None, "build with optimizations", False, False, bool, [new, load, server, client]],
+ ["P", "port", "PORT", "port number for data server", 8080, False, iport, [server, client]],
+ ["p", "pre-cmd", "CMD", "shell command to wrap call to executable (e.g. gdb, time, valgrind, etc.)", None, False, str, [new, load, server, client]],
+ ["s", "seed", "SEED", "seed value for new simulation; a value of 0 disables cosmic rays; a value of -1 creates a random seed", 0, False, seed, [new]],
+ ["T", "keep-temp-dir", None, "keep temporary directory on exit", False, False, bool, [new, load, server, client]],
+ ["t", "thread-gap", "N", "memory gap between cores in bytes (may help reduce cache misses)", 0x100, False, inat, [new, load]],
+ ["u", "ui", uis, "user interface", "curses", False, str, [new, load]],
["x", "no-compress", None, "do not compress save files (useful if 'zlib' is unavailable)", True, False, bool, [new]],
- ["y", "sync-pow", "POW", "core sync interval exponent (interval == 2^{POW})", 20, False, ipos, [bench, new]],
+ ["y", "sync-pow", "POW", "core sync interval exponent (interval == 2^{POW})", 20, False, ipos, [new]],
["z", "auto-save-pow", "POW", "auto-save interval exponent (interval == 2^{POW})", 36, False, ipos, [new]],
]
@@ -116,72 +106,73 @@ for parser, option in parser_map:
args = main_parser.parse_args()
# ------------------------------------------------------------------------------
-# Bootstrap logging system
+# Define build class
# ------------------------------------------------------------------------------
tempdir = TemporaryDirectory(prefix="salis_", delete=not args.keep_temp_dir)
-logger_flags = set()
-logger_includes = set()
-logger_defines = set()
-logger_flags.update({*args.compiler_flags.split(), "-shared", "-fPIC"})
-logger_includes.update({"assert.h", "stdarg.h", "stdbool.h", "stdio.h", "stdlib.h", "time.h", "unistd.h"})
+class Build:
+ def __init__(self, name, library=False):
+ self.srcfile = f"core/{name}.c"
+ self.argfile = os.path.join(tempdir.name, f"{name}.arg")
+ self.binfile = os.path.join(tempdir.name, f"{name}.{"so" if library else "bin"}")
+
+ self.flags = {
+ *args.compiler_flags.split(),
+ *({"-shared", "-fPIC"} if library else set()),
+ *({"-O3"} if args.optimized else {"-ggdb"}),
+ }
+ self.defines = {"-DNDEBUG"} if args.optimized else set()
+ self.includes = set()
+ self.links = set()
+
+ self.build_cmd = [args.compiler, f"@{self.argfile}", self.srcfile, "-o", self.binfile]
-if args.optimized:
- logger_flags.add("-O3")
- logger_defines.add("-DNDEBUG")
-else:
- logger_flags.add("-ggdb")
+ def build(self):
+ fmt_nl = lambda l: f"{l}\n"
+ fmt_include = lambda l: f"-include {l}\n"
+ fmt_define = lambda l: f"{l.replace(" ", "\\ ").replace("\"", "\\\"").replace("'", "\\'")}\n"
-logger_so = os.path.join(tempdir.name, "log.so")
-logger_build_cmd = [args.compiler, "core/logger.c", "-o", logger_so]
-logger_build_cmd.extend(logger_flags)
-logger_build_cmd.extend(sum(map(lambda include: ["-include", include], logger_includes), []))
-logger_build_cmd.extend(logger_defines)
-subprocess.run(logger_build_cmd, check=True)
+ with open(self.argfile, "w") as f:
+ f.writelines(map(fmt_nl, sorted(self.flags)))
+ f.writelines(map(fmt_include, sorted(self.includes)))
+ f.writelines(map(fmt_define, sorted(self.defines)))
+ f.writelines(map(fmt_nl, sorted(self.links)))
+
+ subprocess.run(self.build_cmd, check=True)
+
+# ------------------------------------------------------------------------------
+# Bootstrap logging system
+# ------------------------------------------------------------------------------
+logger_build = Build("logger", library=True)
+logger_build.build()
-logger_dll = ctypes.CDLL(logger_so)
+logger_dll = ctypes.CDLL(logger_build.binfile)
def info(msg): logger_dll.log_info(msg.encode())
def warn(msg): logger_dll.log_warn(msg.encode())
-def erro(msg): logger_dll.log_erro(msg.encode()) and sys.exit(1)
# ------------------------------------------------------------------------------
-# Load configuration
+# Application start - source main configuration
# ------------------------------------------------------------------------------
info(description)
-info(f"Called '{prog} {args.command}' with the following options: {vars(args)}")
-info(f"Using temporary directory: {tempdir.name}")
-info(f"Using logging dl: {logger_so}")
-info(f"Built with command: {logger_build_cmd}")
+info(f"Called '{prog} {args.command}' with the following options: {json.dumps(vars(args), indent=2)}")
+info(f"With temporary directory: {tempdir.name}")
+info(f"With logging library: {logger_build.binfile}")
+info(f"Logging library built with command: '{" ".join(logger_build.build_cmd)}'")
-if args.command in ["load", "new", "serve"]:
- sim_dir = os.path.join(os.environ["HOME"], ".salis", args.name)
- sim_opts = os.path.join(sim_dir, "opts.py")
+if args.command in ["new", "load", "server"]:
+ info(f"With home directory: {args.home}")
+ sim_dir = os.path.join(args.home, args.name)
+ sim_opts = os.path.join(sim_dir, "opts.json")
sim_path = os.path.join(sim_dir, args.name)
-if args.command in ["load", "serve"]:
- if not os.path.isdir(sim_dir):
- erro(f"No simulation found named: {args.name}")
-
- sys.path.append(sim_dir)
- import opts
-
- opt_vars = {opt: getattr(opts, opt) for opt in dir(opts) if not opt.startswith("__")}
-
- for key, val in opt_vars.items():
- setattr(args, key, val)
-
- info(f"Sourced configuration from: '{sim_opts}': {opt_vars}")
-
if args.command in ["new"]:
- if args.data_push_pow and args.data_push_pow < args.sync_pow:
- erro("Data push power must be equal or greater than thread sync power")
+ assert not args.data_push_pow or args.data_push_pow >= args.sync_pow, "Data push interval must be equal or greater than thread sync interval"
if os.path.isdir(sim_dir) and args.force:
warn(f"Force flag used! Wiping old simulation at: {sim_dir}")
shutil.rmtree(sim_dir)
- if os.path.isdir(sim_dir):
- erro(f"Simulation directory found at: {sim_dir}")
+ assert not os.path.isdir(sim_dir), f"Simulation directory found at: {sim_dir} (use --force flag to remove)"
if args.seed == -1:
args.seed = random.getrandbits(64)
@@ -189,243 +180,40 @@ if args.command in ["new"]:
info(f"Creating new simulation directory at: {sim_dir}")
info(f"Creating configuration file at: {sim_opts}")
-
os.mkdir(sim_dir)
- opts = (
- option["long"].replace("-", "_")
+ opts = {
+ option["long"]: getattr(args, option["long"].replace("-", "_"))
for option in options
if new in option["parsers"] and load not in option["parsers"]
- )
+ }
- with open(sim_opts, "w") as file:
- for opt in opts:
- file.write(f"{opt} = {repr(eval(f"args.{opt}"))}\n")
+ with open(sim_opts, "w") as f: f.write(f"{json.dumps(opts, indent=2)}\n")
-# ------------------------------------------------------------------------------
-# Load architecture variables
-# ------------------------------------------------------------------------------
-arch_path = os.path.join("arch", args.arch)
-info(f"Loading architecture variables from: {os.path.join(arch_path, "arch_vars.py")}")
-sys.path.append(arch_path)
-from arch_vars import ArchVars
-arch_vars = ArchVars(args)
+if args.command in ["load", "server"]:
+ assert os.path.isdir(sim_dir), f"No simulation found named {args.name}"
+ with open(sim_opts, "r") as f: opts = json.loads(f.read())
+ for key, val in opts.items(): setattr(args, key.replace("-", "_"), val)
+ info(f"Sourced configuration from: {sim_opts}: {json.dumps(opts, indent=2)}")
# ------------------------------------------------------------------------------
-# Launch data server
+# Load architecture variables
# ------------------------------------------------------------------------------
-if args.command in ["serve"]:
- if (args.update):
- vendors = {
- "plotly.min.js": "https://cdnjs.cloudflare.com/ajax/libs/plotly.js/3.1.1/plotly.min.js",
- "vue3-sfc-loader": "https://cdn.jsdelivr.net/npm/vue3-sfc-loader",
- "vue@latest": "https://unpkg.com/vue@latest",
- }
-
- info(f"Updating vendors: {vendors}")
-
- for file, url in vendors.items():
- with urllib.request.urlopen(url) as response:
- with open(os.path.join("data/vendor", file), "wb") as f:
- f.write(response.read())
-
- # Build SQLite event-array render extension
- sqlx_flags = set()
- sqlx_defines = set()
- sqlx_links = set()
-
- sqlx_flags.update({*args.compiler_flags.split(), "-shared", "-fPIC", "-Icore"})
- sqlx_defines.add(f"-DMVEC_SIZE={2 ** args.mvec_pow}ul")
- sqlx_defines.add(f"-DTHREAD_GAP={args.thread_gap}")
-
- if arch_vars.mvec_loop: sqlx_defines.add("-DMVEC_LOOP")
-
- if args.optimized:
- sqlx_flags.add("-O3")
- sqlx_defines.add("-DNDEBUG")
- else:
- sqlx_flags.add("-ggdb")
-
- sqlx_links.add("-lz")
-
- sqlx_so = os.path.join(tempdir.name, "render.so")
- info(f"Building salis SQLite extension at: {sqlx_so}")
-
- sqlx_build_cmd = [args.compiler, "core/render.c", "-o", sqlx_so]
- sqlx_build_cmd.extend(sqlx_flags)
- sqlx_build_cmd.extend(sqlx_defines)
- sqlx_build_cmd.extend(sqlx_links)
-
- info(f"Using build command: {sqlx_build_cmd}")
- subprocess.run(sqlx_build_cmd, check=True)
-
- # Generate configuration so front-end knows how to render the plots.
- # Each architecture may also provide its own set of plots, which will be merged with the
- # default dictionary below.
- plots = {
- "General": {
- "cycl": {
- "table": "general",
- "type": "lines",
- "cols": [f"cycl_{i}" for i in range(args.cores)],
- },
- "mall": {
- "table": "general",
- "type": "lines",
- "cols": [f"mall_{i}" for i in range(args.cores)],
- },
- "pnum": {
- "table": "general",
- "type": "lines",
- "cols": [f"pnum_{i}" for i in range(args.cores)],
- },
- "ppop": {
- "table": "general",
- "type": "lines",
- "cols": [f"{pref}_{i}" for i in range(args.cores) for pref in ["pfst", "plst"]],
- },
- "ambs": {
- "table": "general",
- "type": "lines",
- "cols": [f"{pref}_{i}" for pref in ["amb0", "amb1"] for i in range(args.cores)],
- },
- "eevs": {
- "table": "general",
- "type": "lines",
- "cols": [f"{pref}_{i}" for pref in ["emb0", "emb1", "eliv", "edea"] for i in range(args.cores)],
- },
- },
- }
-
- heatmaps = {
- "Events": {
- f"aev_{i}": {
- "table": f"aev_{i}",
- } for i in range(args.cores)
- } | {
- f"eev_{i}": {
- "table": f"eev_{i}",
- } for i in range(args.cores)
- } | {
- f"bev_{i}": {
- "table": f"bev_{i}",
- } for i in range(args.cores)
- },
- }
-
- for key in arch_vars.plots:
- plots[key] = (plots[key] if key in plots else {}) | arch_vars.plots[key]
-
- for key in arch_vars.heatmaps:
- heatmaps[key] = (heatmaps[key] if key in heatmaps else {}) | arch_vars.heatmaps[key]
-
- info(f"Generated plot configuration: {plots}")
- info(f"Generated heatmap configuration: {heatmaps}")
-
- # NOTE: this server implementation is very minimal and has no built-in security.
- # Please do not put this on the internet! Only run the data server within secure
- # networks that you own.
- sim_db = os.path.join(sim_dir, f"{args.name}.sqlite3")
-
- class Handler(SimpleHTTPRequestHandler):
- def __init__(self, *args, **kwargs):
- super().__init__(*args, **kwargs, directory="data")
-
- def log_message(_, format, *args):
- info(format % args)
-
- def log_error(_, format, *args):
- warn(format % args)
-
- def send_200_and_mime_type(self, mime_type):
- self.send_response(200)
- self.send_header("Content-type", mime_type)
- self.end_headers()
-
- def send_as_json(self, obj):
- self.send_200_and_mime_type("application/json")
- self.wfile.write(json.dumps(obj).encode("utf-8"))
-
- def send_file_as(self, path, mime_type):
- self.send_200_and_mime_type(mime_type)
- with open(path, "r") as f: self.wfile.write(f.read().encode("utf-8"))
-
- @staticmethod
- @contextlib.contextmanager
- def sqlite_connect():
- db_con = sqlite3.connect(sim_db, timeout=600)
- db_con.row_factory = sqlite3.Row
- db_con.enable_load_extension(True)
- db_con.execute("PRAGMA journal_mode=wal;")
- db_con.execute(f"SELECT load_extension('{sqlx_so}');")
- try: yield db_con
- finally: db_con.close()
-
- def do_GET(self):
- bits = urllib.parse.urlparse(self.path)
-
- if bits.path == "/": return self.send_file_as("data/index.html", "text/html")
- if bits.path.split("/")[1] in ["js", "vendor", "vue"]: return self.send_file_as("data" + bits.path, "text/javascript")
- if bits.path == "/opts": return self.send_as_json(opt_vars | {"mvec_loop": arch_vars.mvec_loop, "name": args.name})
- if bits.path == "/plots": return self.send_as_json(plots)
- if bits.path == "/heatmaps": return self.send_as_json(heatmaps)
-
- if bits.path == "/data":
- http_query = urllib.parse.parse_qs(bits.query)
- table = http_query["table"][0]
- rows = http_query["rows"][0]
- nth = http_query["nth"][0]
- axis = http_query["axis"][0]
- low = http_query["low"][0]
- high = http_query["high"][0]
- is_eva = http_query["is_eva"][0]
-
- if is_eva == "true":
- left = http_query["left"][0]
- pixels = http_query["pixels"][0]
- pixel_pow = http_query["pixel_pow"][0]
- selects = ", ".join([f"cycl_{i}" for i in range(args.cores)]) + f", eva_render({left}, {pixels}, {pixel_pow}, evts) as eva_render, step"
- else:
- selects = "*"
-
- sql_query = f"SELECT * FROM (SELECT rowid, {selects} FROM {table} WHERE {axis} >= {low} AND {axis} <= {high} AND rowid % {nth} == 0 ORDER BY {axis} DESC LIMIT {rows}) ORDER BY {axis} ASC;"
- with Handler.sqlite_connect() as db_con: sql_list = [dict(row) for row in db_con.execute(sql_query).fetchall()]
- return self.send_as_json(sql_list)
-
- if bits.path == "/high":
- http_query = urllib.parse.parse_qs(bits.query)
- axis = http_query["axis"][0]
- sql_query = f"SELECT {axis} as high FROM general ORDER BY {axis} DESC LIMIT 1;"
- with Handler.sqlite_connect() as db_con: sql_dict = dict(db_con.execute(sql_query).fetchone())
- return self.send_as_json(sql_dict)
-
- self.log_error(f"Unsupported endpoint: {bits.path}")
- self.send_response(400)
- self.end_headers()
-
- info("Launching data server")
- server = ThreadingHTTPServer(("", args.port), Handler)
-
- try:
- server.serve_forever()
- except KeyboardInterrupt:
- pass
-
- info("Shutting down data server")
- sys.exit(0)
+if args.command in ["new", "load", "server"]:
+ arch_path = os.path.join("arch", args.arch, "arch_vars.py")
+ info(f"Loading architecture variables from: {arch_path}")
+ arch_mod = SourceFileLoader("arch_vars", arch_path).load_module()
+ arch_vars = arch_mod.ArchVars(args)
# ------------------------------------------------------------------------------
# Compile ancestor organism
# ------------------------------------------------------------------------------
-if args.command in ["bench", "new"] and args.anc is not None:
+if args.command in ["new", "load", "server"]:
anc_path = os.path.join("anc", args.arch, f"{args.anc}.asm")
- if not os.path.isfile(anc_path):
- erro(f"Could not find ancestor file: {anc_path}")
-
- with open(anc_path, "r") as file:
- lines = file.read().splitlines()
+ assert os.path.isfile(anc_path), f"Could not find ancestor file: {anc_path}"
+ with open(anc_path, "r") as file: lines = file.read().splitlines()
lines = filter(lambda line: not line.startswith(";"), lines)
lines = filter(lambda line: not line.isspace(), lines)
lines = filter(lambda line: line, lines)
@@ -442,8 +230,7 @@ if args.command in ["bench", "new"] and args.anc is not None:
found = True
break
- if not found:
- erro(f"Unrecognized instruction in ancestor file: {line}")
+ assert found, f"Unrecognized instruction in ancestor file: {line}"
anc_bytes_repr = ",".join(map(str, anc_bytes))
info(f"Compiled ancestor file '{anc_path}' into byte array: {{{anc_bytes_repr}}}")
@@ -451,121 +238,96 @@ if args.command in ["bench", "new"] and args.anc is not None:
# ------------------------------------------------------------------------------
# Populate compiler flags
# ------------------------------------------------------------------------------
-flags = set()
-includes = set()
-defines = set()
-links = set()
-
-flags.update({*args.compiler_flags.split(), f"-Iarch/{args.arch}", "-Icore"})
-
-defines.add(f"-DARCH=\"{args.arch}\"")
-defines.add(f"-DCOMMAND_{args.command.upper()}")
-defines.add(f"-DCORES={args.cores}")
-defines.add(f"-DMUTA_RANGE={2 ** args.muta_pow}ul")
-defines.add(f"-DMVEC_SIZE={2 ** args.mvec_pow}ul")
-defines.add(f"-DSEED={args.seed}ul")
-defines.add(f"-DSYNC_INTERVAL={2 ** args.sync_pow}ul")
-defines.add(f"-DTHREAD_GAP={args.thread_gap}")
-
-defines.add(f"-DCORE_FIELDS={" ".join(f"CORE_FIELD({", ".join(field)})" for field in arch_vars.core_fields)}")
-defines.add(f"-DCORE_DATA_FIELDS={" ".join(f"CORE_DATA_FIELD({", ".join(field)})" for field in arch_vars.core_data_fields)}")
-defines.add(f"-DPROC_FIELDS={" ".join(f"PROC_FIELD({", ".join(field)})" for field in arch_vars.proc_fields)}")
-defines.add(f"-DINST_SET={" ".join(f"INST({index}, {"_".join(inst[0])}, \"{" ".join(inst[0])}\", L'{inst[1]}')" for index, inst in enumerate(arch_vars.inst_set))}")
-defines.add(f"-DCORE_FIELD_COUNT={len(arch_vars.core_fields)}")
-defines.add(f"-DPROC_FIELD_COUNT={len(arch_vars.proc_fields)}")
-defines.add(f"-DINST_COUNT={len(arch_vars.inst_set)}")
-defines.add(f"-DFOR_CORES={" ".join(f"FOR_CORE({i})" for i in range(args.cores))}")
+if args.command in ["new", "load"]:
+ ui_path = os.path.join("ui", args.ui, "ui_vars.py")
+ info(f"Loading UI variables from: {ui_path}")
+ ui_mod = SourceFileLoader("ui_vars", ui_path).load_module()
+ ui_vars = ui_mod.UIVars(args)
-if args.muta_flip: defines.add("-DMUTA_FLIP")
-if arch_vars.mvec_loop: defines.add("-DMVEC_LOOP")
-
-if args.optimized:
- flags.add("-O3")
- defines.add("-DNDEBUG")
-else:
- flags.add("-ggdb")
-
-if args.command in ["bench"]:
- includes.add("stdio.h")
- defines.add(f"-DSTEPS={args.steps}ul")
-
-if args.command in ["bench", "new"]:
- defines.add(f"-DCLONES={args.clones}")
-
- if args.anc is not None:
- defines.add(f"-DANC_BYTES={{{anc_bytes_repr}}}")
- defines.add(f"-DANC_SIZE={len(anc_bytes)}")
-
-if args.command in ["load", "new"]:
- ui_path = os.path.join("ui", args.ui)
- info(f"Loading UI variables from: {os.path.join(ui_path, "ui_vars.py")}")
- sys.path.append(ui_path)
- from ui_vars import UIVars
- ui_vars = UIVars(args)
-
- flags.add(f"-Iui/{args.ui}")
- flags.update(ui_vars.flags)
- includes.update(ui_vars.includes)
- defines.update(ui_vars.defines)
- defines.add(f"-DAUTOSAVE_INTERVAL={2 ** args.auto_save_pow}ul")
- defines.add(f"-DAUTOSAVE_NAME_LEN={len(sim_path) + 20}")
- defines.add(f"-DNAME=\"{args.name}\"")
- defines.add(f"-DSIM_PATH=\"{sim_path}\"")
- links.update(ui_vars.links)
+ build = Build("salis")
+ build.flags.update({*ui_vars.flags, f"-Iarch/{args.arch}", "-Icore", f"-Iui/{args.ui}"})
+ build.includes.update({*ui_vars.includes})
+ build.defines.update({*ui_vars.defines, f"-DTHREAD_GAP={args.thread_gap}"})
+ build.links.update({*ui_vars.links})
if args.data_push_pow:
- includes.update({"sqlite3.h", "zlib.h"})
- data_push_path = os.path.join(sim_dir, f"{args.name}.sqlite3")
- defines.add(f"-DDATA_PUSH_INTERVAL={2 ** args.data_push_pow}ul")
- defines.add(f"-DDATA_PUSH_PATH=\"{data_push_path}\"")
- links.update({"-lsqlite3", "-lz"})
- info(f"Data will be aggregated at: {data_push_path}")
+ sim_db = os.path.join(sim_dir, f"{args.name}.sqlite3")
+ build.defines.update({f"-DDATA_PUSH_PATH=\"{sim_db}\""})
+ build.includes.update({"sqlite3.h", "zlib.h"})
+ build.links.update({"-lsqlite3", "-lz"})
+ info(f"Data will be aggregated at: {sim_db}")
else:
warn("Data aggregation disabled")
if not args.no_compress:
- includes.add("zlib.h")
- defines.add("-DCOMPRESS")
- links.add("-lz")
+ build.includes.update({"zlib.h"})
+ build.links.update({"-lz"})
info("Save file compression enabled")
else:
warn("Save file compression disabled")
-# ------------------------------------------------------------------------------
-# Build executable
-# ------------------------------------------------------------------------------
-salis_bin = os.path.join(tempdir.name, "salis_bin")
-info(f"Building salis binary at: {salis_bin}")
+if args.command in ["server"]:
+ build = Build("server")
+ build.defines.update({f"-DPORT={args.port}"})
+ build.links.update({"-ljson-c"})
-build_cmd = [args.compiler, "core/salis.c", "-o", salis_bin]
-build_cmd.extend(flags)
-build_cmd.extend(sum(map(lambda include: ["-include", include], includes), []))
-build_cmd.extend(defines)
-build_cmd.extend(links)
+if args.command in ["new", "load", "server"]:
+ build.defines.update({
+ f"-DANC=\"{args.anc}\"",
+ f"-DANC_BYTES={{{anc_bytes_repr}}}",
+ f"-DANC_SIZE={len(anc_bytes)}",
+ f"-DARCH=\"{args.arch}\"",
+ f"-DAUTOSAVE_INTERVAL={2 ** args.auto_save_pow}ul",
+ f"-DAUTOSAVE_NAME_LEN={len(sim_path) + 20}",
+ f"-DCLONES={args.clones}",
+ f"-DCOMMAND_{args.command.upper()}",
+ f"-DCOMPRESS={1 if not args.no_compress else 0}",
+ f"-DCORE_DATA_FIELDS={" ".join(f"CORE_DATA_FIELD({", ".join(field)})" for field in arch_vars.core_data_fields)}",
+ f"-DCORE_FIELD_COUNT={len(arch_vars.core_fields)}",
+ f"-DCORE_FIELDS={" ".join(f"CORE_FIELD({", ".join(field)})" for field in arch_vars.core_fields)}",
+ f"-DCORES={args.cores}",
+ f"-DDATA_PUSH={1 if args.data_push_pow else 0}",
+ f"-DDATA_PUSH_INTERVAL={2 ** args.data_push_pow}ul",
+ f"-DFOR_CORES={" ".join(f"FOR_CORE({i})" for i in range(args.cores))}",
+ f"-DINST_COUNT={len(arch_vars.inst_set)}",
+ f"-DINST_SET={" ".join(f"INST({index}, {"_".join(inst[0])}, \"{" ".join(inst[0])}\", L'{inst[1]}')" for index, inst in enumerate(arch_vars.inst_set))}",
+ f"-DMUTA_FLIP={1 if args.muta_flip else 0}",
+ f"-DMUTA_RANGE={2 ** args.muta_pow}ul",
+ f"-DMVEC_LOOP={1 if arch_vars.mvec_loop else 0}",
+ f"-DMVEC_SIZE={2 ** args.mvec_pow}ul",
+ f"-DNAME=\"{args.name}\"",
+ f"-DPROC_FIELD_COUNT={len(arch_vars.proc_fields)}",
+ f"-DPROC_FIELDS={" ".join(f"PROC_FIELD({", ".join(field)})" for field in arch_vars.proc_fields)}",
+ f"-DSEED={args.seed}ul",
+ f"-DSIM_PATH=\"{sim_path}\"",
+ f"-DSYNC_INTERVAL={2 ** args.sync_pow}ul",
+ })
-info(f"Using build command: {build_cmd}")
-subprocess.run(build_cmd, check=True)
+if args.command in ["client"]:
+ build = Build("client")
+ build.defines.update({f"-DIP=\"{args.ip}\"", f"-DPORT={args.port}"})
+ build.links.update({"-ljson-c"})
# ------------------------------------------------------------------------------
-# Run salis binary
+# Build and run executable
# ------------------------------------------------------------------------------
-info("Running salis binary...")
+info(f"Building binary with command: '{" ".join(build.build_cmd)}'")
+build.build()
-run_cmd = [args.pre_cmd] if args.pre_cmd else []
-run_cmd.append(salis_bin)
+run_cmd = args.pre_cmd.split() if args.pre_cmd else []
+run_cmd.append(build.binfile)
-info(f"Using run command: {" ".join(run_cmd)}")
-salis_sp = subprocess.Popen(run_cmd, stdout=sys.stdout, stderr=sys.stderr)
+info(f"Running binary with command: '{" ".join(run_cmd)}'")
+proc = subprocess.Popen(run_cmd, stdout=sys.stdout, stderr=sys.stderr)
# When using signals (e.g. SIGTERM), they must be sent to the entire process group
# to make sure both the simulator and the interpreter get shut down.
try:
- salis_sp.wait()
+ proc.wait()
except KeyboardInterrupt:
- salis_sp.terminate()
- salis_sp.wait()
+ proc.terminate()
+ proc.wait()
-code = salis_sp.returncode
+code = proc.returncode
-if code != 0:
- erro(f"Salis binary returned code: {code}")
+assert code == 0, f"Binary returned code: {code}"