second-state / ssvm-napi-extensions

Apache License 2.0
4 stars 0 forks source link

Second State WebAssembly VM for Node.js Addon with all extensions

The Second State VM (SSVM) is a high-performance WebAssembly runtime optimized for server-side applications. This project provides support for accessing SSVM as a Node.js addon. It allows Node.js applications to call WebAssembly functions written in Rust or other high-performance languages. Why do you want to run WebAssembly on the server-side? The SSVM addon could interact with the wasm files generated by the ssvmup compiler tool.

NOTICE

SSVM Node.js Addon is in active development.

In the current stage, our prebuilt version only supports x86_64 Linux. Or you could use --build-from-source flag to build from source during addon installation.

Requirements

After SSVM Napi 0.4.0 release, we upgrade the base image from Ubuntu 18.04 to Ubuntu 20.04.

Users should install the dependencies by the following requirments:

For the tensorflow extensions:

Prepare environment

Use our docker image or dockerfile

$ docker pull secondstate/ssvm-extensions
# Or you can build it on your local environment.
$ docker build . -f utils/docker/Dockerfile -t secondstate/ssvm-extensions

Verify the version of llvm

$ sudo apt list | grep llvm
...omitted...
llvm-dev/focal,now 1:10.0-50~exp1 amd64 [installed]
llvm-runtime/focal,now 1:10.0-50~exp1 amd64 [installed,automatic]
llvm/focal,now 1:10.0-50~exp1 amd64 [installed,automatic]
...omitted...

# If the version is 1:10.x, then your llvm version is correct.

Verify the version of libstdc++6

$ strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep GLIBCXX
...omitted...
GLIBCXX_3.4.24
GLIBCXX_3.4.25
GLIBCXX_3.4.26
GLIBCXX_3.4.27
GLIBCXX_3.4.28
GLIBCXX_DEBUG_MESSAGE_LENGTH

# If you can find GLIBCXX_3.4.28 in the output, then your libstdc++6 version is correct.

Works with Rust library using Wasm-Bindgen

Please refer to Tutorial: A Wasm-Bindgen application.

Works with Rust application using standalone wasm32-wasi backend

Please refer to Tutorial: A standalone wasm32-wasi application.

APIs

Constructor: ssvm.VM(wasm, ssvm_options) -> vm_instance

Methods

Start() -> Integer

Run(function_name, args...) -> void

RunInt(function_name, args...) -> Integer

RunUInt(function_name, args...) -> Integer

RunInt64(function_name, args...) -> BigInt

RunUInt64(function_name, args...) -> BigInt

RunString(function_name, args...) -> String

RunUint8Array(function_name, args...) -> Uint8Array

Compile(output_filename) -> boolean

// When you want to run the compiled file let vm = ssvm.VM("/path/to/aot/file", options); vm.RunXXX("Func", args);


#### `GetStatistics() -> Object`
* If you want to enable measurement, set the option `EnableMeasurement` to `true`. But please notice that enabling measurement will significantly affect performance.
* Get the statistics of execution runtime.
* Return Value `Statistics` <Object>
    * `Measure` -> <Boolean>: To show if the measurement is enabled or not.
    * `TotalExecutionTime` -> <Integer>: Total execution time (Wasm exeuction time + Host function execution time) in `ns` unit.
    * `WasmExecutionTime` -> <Integer>: Wasm instructions execution time in `ns` unit.
    * `HostFunctionExecutionTime` -> <Integer>: Host functions (e.g. eei or wasi functions) execution time in `ns` unit.
    * `InstructionCount` -> <Integer>: The number of executed instructions in this execution.
    * `TotalGasCost` -> <Integer>: The cost of this execution.
    * `InstructionPerSecond` -> <Float>: The instructions per second of this execution.

```javascript
let result = RunInt("Add", 1, 2);
// result should be 3
let stat = GetStatistics();
/*
If the `EnableMeasurement: true`:

stat = Statistics:  {
  Measure: true,
  TotalExecutionTime: 1512,
  WasmExecutionTime: 1481,
  HostFunctionExecutionTime: 31,
  InstructionCount: 27972,
  TotalGasCost: 27972,
  InstructionPerSecond: 18887238.35246455
}

Else:

stat = Statistics:  {
  Measure: false
}
*/