示例#1
0
def init():
    # TODO: loader can not load module using WASI
    #   https://github.com/bytecodealliance/wasmtime-py/pull/44
    # import wasmtime.loader
    # import benchmark_wasm_wasi

    wasm_cfg = wasmtime.Config()
    wasm_cfg.cache = True

    wasi_cfg = wasmtime.WasiConfig()
    store = wasmtime.Store(wasmtime.Engine(wasm_cfg))
    linker = wasmtime.Linker(store)
    linker.define_wasi(wasmtime.WasiInstance(store,
                                             "wasi_snapshot_preview1", wasi_cfg))
    wasm = pkg_resources.read_binary(__package__, "benchmark_wasm.wasm")
    instance = linker.instantiate(wasmtime.Module(store.engine, wasm))
    return instance
示例#2
0
def _run_wasm_app(wasm_filename, argv):
    module_binary = importlib_resources.read_binary(__package__, wasm_filename)
    module_digest = hashlib.sha1(module_binary).digest()

    wasi_cfg = wasmtime.WasiConfig()
    wasi_cfg.argv = argv
    wasi_cfg.preopen_dir(str(importlib_resources.files(__package__) / "share"),
                         "/share")
    wasi_cfg.preopen_dir("/", "/")
    wasi_cfg.preopen_dir(".", ".")
    wasi_cfg.inherit_stdin()
    wasi_cfg.inherit_stdout()
    wasi_cfg.inherit_stderr()

    engine = wasmtime.Engine()
    cache_path = pathlib.Path(
        os.getenv("YOWASP_CACHE_DIR", appdirs.user_cache_dir("yowasp")))
    cache_path.mkdir(parents=True, exist_ok=True)
    cache_filename = (cache_path / "{}-cache".format(wasm_filename))
    digest_filename = (cache_path / "{}-digest".format(wasm_filename))
    try:
        with digest_filename.open("rb") as digest_file:
            if digest_file.read() != module_digest:
                raise Exception("cache miss")
        module = wasmtime.Module.deserialize_file(engine, str(cache_filename))
    except:
        print("Preparing to run {}. This might take a while...".format(
            argv[0]),
              file=sys.stderr)
        module = wasmtime.Module(engine, module_binary)
        with cache_filename.open("wb") as cache_file:
            cache_file.write(module.serialize())
        with digest_filename.open("wb") as digest_file:
            digest_file.write(module_digest)

    linker = wasmtime.Linker(engine)
    linker.define_wasi()
    store = wasmtime.Store(engine)
    store.set_wasi(wasi_cfg)
    app = linker.instantiate(store, module)
    linker.define_instance(store, "app", app)
    try:
        app.exports(store)["_start"](store)
        return 0
    except wasmtime.ExitTrap as trap:
        return trap.code
示例#3
0
relative_dir = 'lib/python'

wasm_cfg = wasmtime.Config()
wasm_cfg.cache = True

wasi_cfg = wasmtime.WasiConfig()
wasi_cfg.argv = ()
wasi_cfg.preopen_dir(".", "/")
wasi_cfg.inherit_stdin()
wasi_cfg.inherit_stdout()
wasi_cfg.inherit_stderr()

wasm_bytes = open(f'{relative_dir}/smartcore_wasi_lib.wasm', 'rb').read()
store = wasmtime.Store(wasmtime.Engine(wasm_cfg))
linker = wasmtime.Linker(store)
module = wasmtime.Module(store.engine, wasm_bytes)
wasi = linker.define_wasi(
    wasmtime.WasiInstance(store, "wasi_snapshot_preview1", wasi_cfg))
smartcore_wasi = linker.instantiate(module)

(file_ptr, file_len) = get_string_ptr(f'{relative_dir}/iris_knn.model',
                                      smartcore_wasi)
smartcore_wasi.exports["init"](file_ptr)
perfomances = []
if os.environ.get("noe") is not None:
    num_executions = int(os.environ.get("noe"))
print(f"Executing {num_executions} times")
start_time = datetime.datetime.now()
for i in range(num_executions):
    t1 = time.monotonic_ns()
示例#4
0
import wasmtime
try:
    from importlib import resources as importlib_resources
    importlib_resources.files  # py3.9+ stdlib
except (ImportError, AttributeError):
    import importlib_resources  # py3.8- shim

wasm_cfg = wasmtime.Config()
wasm_cfg.cache = True

wasi_cfg = wasmtime.WasiConfig()
wasi_cfg.argv = ("amaranth-yosys", *sys.argv[1:])
wasi_cfg.preopen_dir(".", ".")
wasi_cfg.inherit_stdin()
wasi_cfg.inherit_stdout()
wasi_cfg.inherit_stderr()

engine = wasmtime.Engine(wasm_cfg)
linker = wasmtime.Linker(engine)
linker.define_wasi()
store = wasmtime.Store(engine)
store.set_wasi(wasi_cfg)
yosys = linker.instantiate(
    store,
    wasmtime.Module(engine, (importlib_resources.files(__package__) /
                             "yosys.wasm").read_bytes()))
try:
    yosys.exports(store)["_start"](store)
except wasmtime.ExitTrap as trap:
    sys.exit(trap.code)