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

Prepare leaking GC for multithreading #4560

Open
wants to merge 2 commits into
base: dev
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
37 changes: 37 additions & 0 deletions src/internal/task/atomic-cooperative.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package task

// Atomics implementation for cooperative systems. The atomic types here aren't
// actually atomic, they assume that accesses cannot be interrupted by a
// different goroutine or interrupt happening at the same time.

type atomicIntegerType interface {
uintptr | uint64
}

type pseudoAtomic[T atomicIntegerType] struct {
v T
}

func (x *pseudoAtomic[T]) Add(delta T) T { x.v += delta; return x.v }
func (x *pseudoAtomic[T]) Load() T { return x.v }
func (x *pseudoAtomic[T]) Store(val T) { x.v = val }
func (x *pseudoAtomic[T]) CompareAndSwap(old, new T) (swapped bool) {
if x.v != old {
return false
}
x.v = new
return true
}
func (x *pseudoAtomic[T]) Swap(new T) (old T) {
old = x.v
x.v = new
return
}

// Uintptr is an atomic uintptr when multithreading is enabled, and a plain old
// uintptr otherwise.
type Uintptr = pseudoAtomic[uintptr]

// Uint64 is an atomic uint64 when multithreading is enabled, and a plain old
// uint64 otherwise.
type Uint64 = pseudoAtomic[uint64]
33 changes: 20 additions & 13 deletions src/runtime/gc_leaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ package runtime
// may be the only memory allocator possible.

import (
"internal/task"
"unsafe"
)

// Ever-incrementing pointer: no memory is freed.
var heapptr = heapStart
var heapptr task.Uintptr

// Total amount allocated for runtime.MemStats
var gcTotalAlloc uint64
var gcTotalAlloc task.Uint64

// Total number of calls to alloc()
var gcMallocs uint64
var gcMallocs task.Uint64

// Total number of objected freed; for leaking collector this stays 0
const gcFrees = 0
Expand All @@ -31,18 +32,23 @@ func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
// much. And by using platform-native data types (e.g. *uint8 for 8-bit
// systems).
size = align(size)
addr := heapptr
gcTotalAlloc += uint64(size)
gcMallocs++
heapptr += size
for heapptr >= heapEnd {

// Track statistics. These are stored separately so are not strictly atomic,
// which means that ReadMemStats might read a _slightly_ inconsistent state.
gcTotalAlloc.Add(uint64(size))
gcMallocs.Add(1)

nextAddr := heapptr.Add(size)
for nextAddr >= heapEnd {
// Try to increase the heap and check again.
if growHeap() {
continue
}
// Failed to make the heap bigger, so we must really be out of memory.
runtimePanic("out of memory")
}
addr := nextAddr - size

pointer := unsafe.Pointer(addr)
zero_new_alloc(pointer, size)
return pointer
Expand All @@ -69,18 +75,19 @@ func free(ptr unsafe.Pointer) {
// The returned memory statistics are up to date as of the
// call to ReadMemStats. This would not do GC implicitly for you.
func ReadMemStats(m *MemStats) {
totalAlloc := gcTotalAlloc.Load()
m.HeapIdle = 0
m.HeapInuse = gcTotalAlloc
m.HeapInuse = totalAlloc
m.HeapReleased = 0 // always 0, we don't currently release memory back to the OS.

m.HeapSys = m.HeapInuse + m.HeapIdle
m.GCSys = 0
m.TotalAlloc = gcTotalAlloc
m.Mallocs = gcMallocs
m.TotalAlloc = totalAlloc
m.Mallocs = gcMallocs.Load()
m.Frees = gcFrees
m.Sys = uint64(heapEnd - heapStart)
// no free -- current in use heap is the total allocated
m.HeapAlloc = gcTotalAlloc
m.HeapAlloc = totalAlloc
m.Alloc = m.HeapAlloc
}

Expand All @@ -94,7 +101,7 @@ func SetFinalizer(obj interface{}, finalizer interface{}) {

func initHeap() {
// preinit() may have moved heapStart; reset heapptr
heapptr = heapStart
heapptr.Store(heapStart)
}

// setHeapEnd sets a new (larger) heapEnd pointer.
Expand Down
Loading