Skip to content

Instantly share code, notes, and snippets.

@tsandall
Created November 12, 2020 23:14
Show Gist options
  • Save tsandall/f27a11016b0c528c8740599cbcd8b0f1 to your computer and use it in GitHub Desktop.
Save tsandall/f27a11016b0c528c8740599cbcd8b0f1 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
"time"
"github.com/bytecodealliance/wasmtime-go"
)
func getRSS() int64 {
buf, err := ioutil.ReadFile("/proc/self/statm")
if err != nil {
panic(err)
}
fields := strings.Split(string(buf), " ")
if len(fields) < 2 {
panic(err)
}
rss, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
panic(err)
}
return (rss * int64(os.Getpagesize())) / (1024 * 1024)
}
func main() {
store := wasmtime.NewStore(wasmtime.NewEngine())
wasm, err := ioutil.ReadFile("policy.wasm")
if err != nil {
panic(err)
}
module, err := wasmtime.NewModule(store.Engine, wasm)
if err != nil {
panic(err)
}
memorytype := wasmtime.NewMemoryType(wasmtime.Limits{Min: 2})
memory := wasmtime.NewMemory(store, memorytype)
opaAbort := wasmtime.WrapFunc(store, func(int32) {
panic("opa_abort")
})
opaBuiltin0 := wasmtime.WrapFunc(store, func(int32, int32) int32 {
panic("opaBuiltin0")
})
opaBuiltin1 := wasmtime.WrapFunc(store, func(int32, int32, int32) int32 {
panic("opaBuiltin1")
})
opaBuiltin2 := wasmtime.WrapFunc(store, func(int32, int32, int32, int32) int32 {
panic("opaBuiltin2")
})
opaBuiltin3 := wasmtime.WrapFunc(store, func(int32, int32, int32, int32, int32) int32 {
panic("opaBuiltin3")
})
opaBuiltin4 := wasmtime.WrapFunc(store, func(int32, int32, int32, int32, int32, int32) int32 {
panic("opaBuiltin4")
})
for {
instance, err := wasmtime.NewInstance(store, module, []*wasmtime.Extern{
memory.AsExtern(),
opaAbort.AsExtern(),
opaBuiltin0.AsExtern(),
opaBuiltin1.AsExtern(),
opaBuiltin2.AsExtern(),
opaBuiltin3.AsExtern(),
opaBuiltin4.AsExtern(),
})
if err != nil {
panic(err)
}
fmt.Println(getRSS(), "MB")
_ = instance
time.Sleep(time.Millisecond * 100)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment