Skip to content

Commit

Permalink
log: better handling of Log()/Debug(), add new DebugMemory() function
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Mar 8, 2024
1 parent b215ed0 commit 5cac6a6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
6 changes: 3 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ func (e *Engine) Init() error {
return ErrNoInterpreter
}

mechanoid.Log("Initializing interpreter...")
mechanoid.Debug("Initializing interpreter...")
if err := e.Interpreter.Init(); err != nil {
return fmt.Errorf("init interpreter: %v", err)
}

if e.FileStore != nil {
mechanoid.Log("Initializing file store...")
mechanoid.Debug("Initializing file store...")
if err := e.FileStore.Init(); err != nil {
return fmt.Errorf("init file store: %v", err)
}
}

mechanoid.Log("Initializing devices...")
mechanoid.Debug("Initializing devices...")
for _, d := range e.Devices {
err := d.Init()
if err != nil {
Expand Down
27 changes: 5 additions & 22 deletions interp/wasman/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ func (i *Interpreter) Init() error {
}

func (i *Interpreter) Load(code engine.Reader) error {
ms := runtime.MemStats{}

if mechanoid.Debugging {
runtime.ReadMemStats(&ms)
mechanoid.Debug("Interpreter Load - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}
mechanoid.DebugMemory("Interpreter Load")

conf := config.ModuleConfig{
Recover: true,
Expand All @@ -59,12 +54,7 @@ func (i *Interpreter) Load(code engine.Reader) error {
}

func (i *Interpreter) Run() (engine.Instance, error) {
ms := runtime.MemStats{}

if mechanoid.Debugging {
runtime.ReadMemStats(&ms)
mechanoid.Debug("Interpreter Run - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}
mechanoid.DebugMemory("Interpreter Run")

var err error
i.instance, err = i.linker.Instantiate(i.module)
Expand All @@ -84,21 +74,14 @@ func (i *Interpreter) Run() (engine.Instance, error) {
}

func (i *Interpreter) Halt() error {
ms := runtime.MemStats{}
mechanoid.DebugMemory("Interpreter Halt")

if mechanoid.Debugging {
runtime.ReadMemStats(&ms)
mechanoid.Debug("Interpreter Halt - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}
i.instance = nil
i.module = nil

// force a garbage collection to free memory
runtime.GC()

if mechanoid.Debugging {
runtime.ReadMemStats(&ms)
mechanoid.Debug("Interpreter Halt after GC - Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}
mechanoid.DebugMemory("Interpreter Halt after GC")

return nil
}
Expand Down
8 changes: 4 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ func Log(msg string) {
}

// Log a message into terminal if debug mode is enabled
func Debug(args ...any) {
func Debug(msg string) {
if Debugging {
println(args)
println(msg)
}
}

// DebugMemory prints memory usage if debug mode is enabled.
func DebugMemory(args ...any) {
func DebugMemory(msg string) {
if Debugging {
ms := runtime.MemStats{}
runtime.ReadMemStats(&ms)
println(args, "Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
println(msg, "Heap Used: ", ms.HeapInuse, " Free: ", ms.HeapIdle, " Meta: ", ms.GCSys)
}
}

0 comments on commit 5cac6a6

Please sign in to comment.