Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scan memory, reset allocator #3148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ func gnoTestPkg(
// XXX: make maxAllocTx configurable.
maxAllocTx := int64(math.MaxInt64)

m.Alloc = gno.NewAllocator(maxAllocTx)
alloc := gno.NewAllocator(maxAllocTx)
alloc.M = m
m.Alloc = alloc
}
m.RunMemPackage(memPkg, true)
err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, printEvents, runFlag, io)
Expand Down
60 changes: 55 additions & 5 deletions gnovm/pkg/gnolang/alloc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package gnolang

import "reflect"
import (
"reflect"
)

// Keeps track of in-memory allocations.
// In the future, allocations within realm boundaries will be
// (optionally?) condensed (objects to be GC'd will be discarded),
// but for now, allocations strictly increment across the whole tx.
type Allocator struct {
maxBytes int64
bytes int64
maxBytes int64
bytes int64
M *Machine
collected bool
}

// for gonative, which doesn't consider the allocator.
Expand Down Expand Up @@ -103,8 +107,54 @@
}

alloc.bytes += size
if alloc.bytes > alloc.maxBytes {
panic("allocation limit exceeded")
if alloc.bytes > alloc.maxBytes && alloc.M != nil {
if alloc.collected {
panic("allocation limit exceeded")

Check warning on line 112 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L111-L112

Added lines #L111 - L112 were not covered by tests
}

newAlloc := NewAllocator(alloc.maxBytes)
for _, block := range alloc.M.Blocks {
newAlloc.NewBlock(block.Source, nil)
for _, value := range block.Values {
newAlloc.AllocateObj(value)
}

Check warning on line 120 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L115-L120

Added lines #L115 - L120 were not covered by tests
}

alloc.bytes = newAlloc.bytes
alloc.collected = true
alloc.Allocate(size)
return

Check warning on line 126 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L123-L126

Added lines #L123 - L126 were not covered by tests
}

alloc.collected = false
}

func (alloc *Allocator) AllocateObj(tv TypedValue) {
switch v := tv.V.(type) {
case PointerValue:
alloc.AllocateType()

if v.TV != nil {
alloc.AllocateObj(*v.TV)
}
case *StructValue:
alloc.AllocateStruct()
alloc.AllocateStructFields(int64(len(v.Fields)))
alloc.AllocateType()
alloc.AllocateHeapItem()

for _, field := range v.Fields {
alloc.AllocateObj(field)
}
case *SliceValue:
alloc.AllocateSlice()
case *ArrayValue:
alloc.AllocateDataArray(int64(len(v.Data)))
case *FuncValue:
alloc.AllocateFunc()
case TypeValue:
alloc.NewType(v.Type)
default:

Check warning on line 157 in gnovm/pkg/gnolang/alloc.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/alloc.go#L132-L157

Added lines #L132 - L157 were not covered by tests
}
}

Expand Down
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
mm := machinePool.Get().(*Machine)
mm.Package = pv
mm.Alloc = alloc

if mm.Alloc != nil {
mm.Alloc.M = mm
}

Check warning on line 175 in gnovm/pkg/gnolang/machine.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/machine.go#L174-L175

Added lines #L174 - L175 were not covered by tests

mm.CheckTypes = checkTypes
mm.ReadOnly = readOnly
mm.MaxCycles = maxCycles
Expand Down
Loading