#!/usr/bin/env python3
"""Compare SQLite prepared lookups under native execution and the Graal runtime."""

import argparse
from datetime import datetime, timezone
from hashlib import sha256
import json
import os
from pathlib import Path
import platform
from random import Random
import re
import subprocess
import sys
from time import perf_counter_ns


ROOT = Path(__file__).resolve().parents[1]
GRAPH_CAP = "-Dpolyglot.compiler.MaximumGraalGraphSize=150000"
LIFTED_GRAPH_CAP = "-Dpolyglot.compiler.MaximumGraalGraphSize=2147483647"
NATIVE_PROFILES = ("native-O0", "native-O2")
GRAAL_PROFILES = ("graal-interpreter", "graal-150000", "graal-lifted", "graal-region")
DEFAULT_PROFILES = (*NATIVE_PROFILES, *GRAAL_PROFILES[:-1])
PROFILE_NAME = re.compile(r"(?:graal|native)-[a-zA-Z0-9_-]+")
JIT_TARGETS = {
    "sqlite3VdbeExec": "vdbe_entry",
    "sqlite3VdbeExecLookup": "lookup_region",
    "run_lookups": "lookup_driver",
    "run_mutex_pairs": "mutex_pairs",
}
TARGET_NAMES = "|".join(map(re.escape, JIT_TARGETS))
TARGET_EVENT = re.compile(
    r"^\[engine\] (opt done|opt inval\.)\s+engine=(\S+)\s+id=(\d+)\s+"
    rf"({TARGET_NAMES})\s+\|([^\n]*?)UTC ([^|\n]+)", re.MULTILINE
)
INSTALL_FIELDS = re.compile(r"Tier (\d+)\|Time\s+(\d+).*?\|CodeSize\s+(\d+)")
COMPILER_EVENT = re.compile(
    r"^\[engine\] (opt done|opt inval\.|opt deopt|opt failed)[^\n]*?\|UTC ([^|\n]+)",
    re.MULTILINE,
)
COMPILE_TIME = re.compile(r"^\[engine\] opt done[^\n]*?\|Time\s+(\d+)", re.MULTILINE)


def digest(path):
    checksum = sha256()
    with path.open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            checksum.update(block)
    return checksum.hexdigest()


def compile_workload(args, directory):
    clang = os.environ.get("GRAAL_CLANG", "clang")
    target = os.environ.get("GRAAL_C_TARGET", "x86_64-unknown-linux-gnu")
    build_native = [profile for profile in args.profile
                    if profile in NATIVE_PROFILES and profile not in args.native_binary]
    build_guest = [profile for profile in args.profile
                   if profile in GRAAL_PROFILES and profile not in args.graal_bitcode]
    sources = []
    flags = []
    if build_native or build_guest:
        amalgamation = args.amalgamation.resolve(strict=True)
        sources = [args.source.resolve(strict=True), amalgamation / "sqlite3.c"]
        flags = ["-std=gnu11", "-pthread", "-DNDEBUG", "-fPIC", f"-I{amalgamation}",
                 "-DSQLITE_ENABLE_FTS5", "-DSQLITE_ENABLE_RTREE", "-DSQLITE_ENABLE_MATH_FUNCTIONS"]
    commands = []
    outputs = {profile: [str(path)] for profile, path in args.native_binary.items()}
    artifacts = {path.name: path for path in sources}
    for profile in build_native:
        opt = profile.removeprefix("native-")
        binary = directory / f"lookup-{opt}"
        commands.append([clang, f"--target={target}", *flags, f"-{opt}",
                         *map(str, sources), "-lm", "-ldl", "-o", str(binary)])
        outputs[profile] = [str(binary)]
        artifacts[binary.name] = binary
    module = directory / "lookup.bc"
    if build_guest:
        lowered = directory / "unoptimized.bc" if args.llvm_passes else module
        commands.append([sys.executable, str(ROOT / "cgraal"), "build", "--target", target,
                         "-o", str(lowered), *[f"--cflag={flag}" for flag in [*flags, *args.guest_cflag]],
                         *map(str, sources)])
        if args.llvm_passes:
            commands.append(["opt", f"-passes={args.llvm_passes}", str(lowered), "-o", str(module)])
        artifacts[module.name] = module
    for command in commands:
        subprocess.run(command, check=True)
    for profile, path in (args.native_binary | args.graal_bitcode).items():
        artifacts[f"{profile}:{path.name}"] = path
    modules = {profile: module for profile in build_guest} | args.graal_bitcode
    if any(profile not in args.graal_runner for profile in modules):
        artifacts[args.exe.name] = args.exe
    for profile, path in modules.items():
        runner = args.graal_runner.get(profile, args.exe)
        if profile in args.graal_runner:
            artifacts[f"{profile}:runner"] = runner
        image = runner.parent / "libexec" / "sqlite-api-shell-image"
        if image.is_file():
            key = f"{profile}:runner-image" if profile in args.graal_runner else image.name
            artifacts[key] = image
        runtime_flags = [*args.runtime_arg, *args.profile_runtime_flags.get(profile, [])]
        if profile == "graal-interpreter":
            outputs[profile] = [str(runner), *runtime_flags, "--run-module", "--interpret", str(path)]
            continue
        cap = GRAPH_CAP if profile == "graal-150000" else LIFTED_GRAPH_CAP
        options = [f"-Dpolyglot.{option}" for option in args.graal_option]
        if args.synchronous:
            options.append("-Dpolyglot.engine.BackgroundCompilation=false")
        outputs[profile] = [str(runner), cap, *options, *runtime_flags, "--run-module", str(path)]
    artifact_hashes = {key: digest(path) for key, path in artifacts.items()}
    compiler = subprocess.check_output([clang, "--version"], text=True) if commands else None
    return outputs, commands, artifact_hashes, compiler


def epoch_ns(stamp):
    timestamp = datetime.fromisoformat(stamp.strip().replace("Z", "+00:00"))
    if timestamp.tzinfo is None:
        timestamp = timestamp.replace(tzinfo=timezone.utc)
    return int(timestamp.timestamp() * 1e9)


def jit_trace(trace):
    targets = {target: [] for target in JIT_TARGETS.values()}
    for event, engine, target_id, name, properties, stamp in TARGET_EVENT.findall(trace):
        record = dict(event=event, engine=engine, target_id=int(target_id), unix_ns=epoch_ns(stamp))
        fields = INSTALL_FIELDS.search(properties)
        if fields is not None:
            tier, compilation_ms, code_size = map(int, fields.groups())
            record.update(tier=tier, compilation_ms=compilation_ms, code_size_bytes=code_size)
        targets[JIT_TARGETS[name]].append(record)
    for records in targets.values():
        records.sort(key=lambda record: record["unix_ns"])
    events = [(event, epoch_ns(stamp)) for event, stamp in COMPILER_EVENT.findall(trace)]
    return targets, events


def jit_status(targets, events, start, duration):
    end = start + duration
    window_events = [event for event, instant in events if start <= instant < end]
    status = {
        "all_compilations_during_measurement": window_events.count("opt done"),
        "all_invalidations_during_measurement": window_events.count("opt inval."),
        "all_deoptimizations_during_measurement": window_events.count("opt deopt"),
        "all_failed_compilations_during_measurement": window_events.count("opt failed"),
    }
    for target, records in targets.items():
        active = {}
        installed_before = installed_during = invalidations_during = 0
        for record in records:
            instant = record["unix_ns"]
            if instant >= end:
                break
            installed = record["event"] == "opt done"
            if instant >= start:
                installed_during += installed
                invalidations_during += not installed
            else:
                compiler_target = (record["engine"], record["target_id"])
                if installed:
                    active[compiler_target] = record.get("tier", 0)
                    installed_before += 1
                else:
                    active.pop(compiler_target, None)
        status.update({
            f"{target}_active_at_measurement_start": bool(active),
            f"{target}_active_tier_at_measurement_start": max(active.values(), default=None),
            f"{target}_compilations_before_measurement": installed_before,
            f"{target}_compilations_during_measurement": installed_during,
            f"{target}_invalidations_during_measurement": invalidations_during,
        })
    return status


def benchmark(args):
    output = args.output.resolve()
    output.mkdir(parents=True)
    if any(profile.startswith("graal-") and profile not in args.graal_runner
           for profile in args.profile):
        args.exe = args.exe.resolve(strict=True)
    workloads, compile_commands, hashes, compiler = compile_workload(args, output)

    profiles = args.profile
    rng = Random(args.order_seed)
    schedule = []
    for trial in range(args.trials):
        offset = trial % len(profiles)
        order = profiles[offset:] + profiles[:offset]
        if args.order_seed is not None:
            rng.shuffle(order)
        schedule.append(order)

    report = {
        "platform": platform.platform(),
        "cpu_affinity": sorted(os.sched_getaffinity(0)) if hasattr(os, "sched_getaffinity") else None,
        "compiler": compiler,
        "synchronous_compilation": args.synchronous,
        "sha256": hashes,
        "compile_commands": compile_commands,
        "order_seed": args.order_seed,
        "scheduled_profiles": schedule,
        "trials": [],
    }
    checkpoint = output / "lookup-progress.json"
    temporary = checkpoint.with_suffix(".tmp")

    def save_report():
        temporary.write_text(json.dumps(report, indent=2) + "\n")
        temporary.replace(checkpoint)

    save_report()
    reference = None
    for trial, order in enumerate(schedule):
        for profile in order:
            command = workloads[profile] + [str(args.rows), str(args.warmup), str(args.lookups)]
            connection_mode = args.profile_connections.get(profile)
            if args.windows != 1 or connection_mode:
                command.append(str(args.windows))
            if connection_mode:
                command.append(connection_mode)
            prefix = output / f"{trial + 1}-{profile}"
            start = perf_counter_ns()
            try:
                result = subprocess.run(command, capture_output=True, text=True, timeout=args.timeout,
                                        env=dict(os.environ, CGRAAL_TRACE="1", CGRAAL_INTERPRET="0"))
            except subprocess.TimeoutExpired as failure:
                prefix.with_suffix(".stdout").write_bytes(failure.stdout or b"")
                prefix.with_suffix(".stderr").write_bytes(failure.stderr or b"")
                prefix.with_suffix(".status.json").write_text(json.dumps({
                    "command": command, "process_wall_ns": perf_counter_ns() - start,
                    "timeout_seconds": args.timeout,
                }, indent=2) + "\n")
                raise
            wall = perf_counter_ns() - start
            prefix.with_suffix(".stdout").write_text(result.stdout)
            prefix.with_suffix(".stderr").write_text(result.stderr)
            if result.returncode:
                prefix.with_suffix(".status.json").write_text(json.dumps({
                    "command": command, "process_wall_ns": wall, "exit_code": result.returncode,
                }, indent=2) + "\n")
            result.check_returncode()
            measurement = json.loads(result.stdout)
            if connection_mode and measurement.get("connection_mode") != connection_mode:
                raise ValueError(f"{profile} did not report the requested connection mode")
            windows = measurement.get("lookup_windows")
            if windows is None:
                windows = [{key: measurement[key]
                            for key in ("measurement_start_unix_ns", "lookup_ns", "checksum")}]
            targets, events = jit_trace(result.stderr)
            for window in windows:
                window.update(jit_status(targets, events, window["measurement_start_unix_ns"],
                                         window["lookup_ns"]))
            checksums = (measurement["warmup_checksum"], *[window["checksum"] for window in windows])
            if reference is None:
                reference = checksums
            if checksums != reference:
                raise ValueError(f"lookup checksum mismatch in {profile}")
            measurement["lookup_windows"] = windows
            measurement["jit_targets"] = targets
            report["trials"].append(dict(
                measurement, profile=profile, trial=trial + 1, process_wall_ns=wall, command=command,
                logged_guest_compilation_ms=sum(map(int, COMPILE_TIME.findall(result.stderr))),
            ))
            save_report()
            print(f"{profile} trial {trial + 1}  wall {wall / 1e9:.3f}s  "
                  f"lookup windows {[round(window['lookup_ns'] / 1e9, 6) for window in windows]}  "
                  f"compilations {[window['all_compilations_during_measurement'] for window in windows]}",
                  flush=True)
    checkpoint.replace(output / "lookup-results.json")


def prebuilt_paths(parser, option, entries, allowed, selected):
    paths = {}
    for entry in entries:
        profile, separator, path = entry.partition("=")
        if not separator or not path or profile not in allowed:
            parser.error(f"{option} expects PROFILE=PATH for one of {', '.join(allowed)}")
        if profile not in selected or profile in paths:
            parser.error(f"{option} requires one path per selected profile: {profile}")
        try:
            paths[profile] = Path(path).resolve(strict=True)
        except OSError as error:
            parser.error(f"{option}: {error}")
        if not paths[profile].is_file():
            parser.error(f"{option} path is not a file: {path}")
    return paths


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("output", type=Path, help="fresh directory for builds and trial logs")
    parser.add_argument("--amalgamation", type=Path, default=ROOT / "vendor/sqlite-amalgamation-3540100")
    parser.add_argument("--source", type=Path, default=ROOT / "bench" / "lookup.c")
    parser.add_argument("--exe", type=Path, default=ROOT / "dist/sqlite-api-shell")
    parser.add_argument("--rows", type=int, default=4096)
    parser.add_argument("--warmup", type=int, default=30000)
    parser.add_argument("--lookups", type=int, default=50000)
    parser.add_argument("--windows", type=int, choices=range(1, 33), default=1, metavar="[1-32]")
    parser.add_argument("--trials", type=int, default=3)
    parser.add_argument("--order-seed", type=int, help="shuffle each fresh-process block with a fixed seed")
    parser.add_argument("--timeout", type=int, default=180)
    parser.add_argument("--synchronous", action="store_true")
    parser.add_argument("--profile", action="append",
                        help="built-in profile or a custom native-NAME or graal-NAME with a prebuilt input")
    parser.add_argument("--native-binary", action="append", default=[], metavar="PROFILE=PATH",
                        help="use a prebuilt native lookup program for a selected native profile")
    parser.add_argument("--graal-bitcode", action="append", default=[], metavar="PROFILE=PATH",
                        help="use prebuilt lookup bitcode for a selected Graal profile")
    parser.add_argument("--graal-runner", action="append", default=[], metavar="PROFILE=PATH",
                        help="use a distinct launcher for a selected Graal profile")
    parser.add_argument("--profile-runtime-arg", action="append", default=[], metavar="PROFILE=FLAG",
                        help="forward a runtime flag to one selected Graal profile")
    parser.add_argument("--profile-connection", action="append", default=[], metavar="PROFILE=MODE",
                        help="select serialized or owned connection mode for one profile")
    parser.add_argument("--guest-cflag", action="append", default=[], metavar="FLAG")
    parser.add_argument("--llvm-passes", metavar="PIPELINE")
    parser.add_argument("--graal-option", action="append", default=[], metavar="NAME=VALUE")
    parser.add_argument("--runtime-arg", action="append", default=[], metavar="FLAG")
    args = parser.parse_args()
    if min(args.rows, args.lookups, args.trials, args.timeout) < 1 or args.warmup < 0:
        parser.error("rows, lookups, trials and timeout must be positive, and warmup nonnegative")
    args.profile = list(dict.fromkeys(args.profile or DEFAULT_PROFILES))
    if any(not PROFILE_NAME.fullmatch(profile) for profile in args.profile):
        parser.error("profile names must start with graal- or native- and use letters, digits, _ or -")
    guest_profiles = tuple(profile for profile in args.profile if profile.startswith("graal-"))
    native_profiles = tuple(profile for profile in args.profile if profile.startswith("native-"))
    args.native_binary = prebuilt_paths(parser, "--native-binary", args.native_binary,
                                        native_profiles, args.profile)
    args.graal_bitcode = prebuilt_paths(parser, "--graal-bitcode", args.graal_bitcode,
                                         guest_profiles, args.profile)
    args.graal_runner = prebuilt_paths(parser, "--graal-runner", args.graal_runner,
                                      guest_profiles, args.profile)
    args.profile_runtime_flags = {}
    for entry in args.profile_runtime_arg:
        profile, separator, flag = entry.partition("=")
        if not separator or profile not in guest_profiles or not flag.startswith("-"):
            parser.error("--profile-runtime-arg expects a selected Graal profile and a flag")
        args.profile_runtime_flags.setdefault(profile, []).append(flag)
    args.profile_connections = {}
    for entry in args.profile_connection:
        profile, separator, mode = entry.partition("=")
        if (not separator or profile not in args.profile or profile in args.profile_connections
                or mode not in ("serialized", "owned")):
            parser.error("--profile-connection expects one PROFILE=serialized or PROFILE=owned per profile")
        args.profile_connections[profile] = mode
    for profile in native_profiles:
        if profile not in NATIVE_PROFILES and profile not in args.native_binary:
            parser.error(f"{profile} requires --native-binary {profile}=PATH")
    for profile in guest_profiles:
        if profile not in DEFAULT_PROFILES and profile not in args.graal_bitcode:
            parser.error(f"{profile} requires --graal-bitcode {profile}=PATH")
    if (args.guest_cflag or args.llvm_passes) and not any(
            profile in GRAAL_PROFILES and profile not in args.graal_bitcode for profile in args.profile):
        parser.error("--guest-cflag and --llvm-passes require a Graal profile built by the runner")
    benchmark(args)


if __name__ == "__main__":
    main()
