-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypervisor.go
152 lines (127 loc) · 3.5 KB
/
hypervisor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package alloc
import (
"context"
"github.com/Inspirate789/alloc/internal/generation"
"math"
"math/rand/v2"
"runtime"
"runtime/debug"
"sync"
"time"
)
const generationMovementThreshold = 0.9
type gcState struct {
lastCollectedGenerations int
curHeapSize int
heapTarget int
}
type hypervisor struct {
gcLock sync.Mutex
gcCtx context.Context
gcCancel context.CancelFunc
mem memory
arenaSignals <-chan struct{}
gcState
}
func (h *hypervisor) run() {
for range h.arenaSignals {
if h.gcLock.TryLock() {
h.curHeapSize++
curHeapSize := h.curHeapSize
heapTarget := h.heapTarget
h.gcLock.Unlock()
if curHeapSize >= heapTarget {
h.gc()
}
}
Debugger.arenasAllocated.Add(1)
}
}
func mergeSearchFunctions[F ~func(K) (V, bool), K comparable, V any](functions []F) F {
return func(key K) (value V, exist bool) {
for _, search := range functions {
value, exist = search(key)
if exist {
return
}
}
return
}
}
func (h *hypervisor) mergeGenerations(sizesBefore, sizesAfter []int) {
for i := len(sizesBefore) - 3; i >= 0; i-- { // suppose that len(sizesBefore) == len(sizesAfter)
if float64(sizesAfter[i])/float64(sizesBefore[i]) <= generationMovementThreshold {
h.mem.movingGenerations[i].MoveTo(h.mem.movingGenerations[i+1])
}
if sizesBefore[i] != sizesAfter[i] {
Debugger.arenasFreed.Add(int64(sizesBefore[i] - sizesAfter[i]))
}
}
}
func gogc() int {
percent := debug.SetGCPercent(100)
debug.SetGCPercent(percent)
return percent
}
func (h *hypervisor) calculateNewHeapTarget() {
sizes := make([]int, len(h.mem.movingGenerations)+1)
for i := range h.mem.movingGenerations {
sizes[i] = h.mem.movingGenerations[i].Size()
}
sizes[len(sizes)-1] = h.mem.largeObjectGeneration.Size()
sizeTotal := 0
for _, size := range sizes {
sizeTotal += size
}
h.curHeapSize = sizeTotal
h.heapTarget = int(math.Round(float64(h.curHeapSize) * (1 + float64(gogc())/100)))
}
func (h *hypervisor) gc() {
runtime.GC()
generationsCount := (h.lastCollectedGenerations+1)%(len(h.mem.movingGenerations)+1) + 1
h.lastCollectedGenerations = generationsCount
generations := append(h.mem.movingGenerations, h.mem.largeObjectGeneration)
searchFunctions := make([]generation.SearchFunc, 0, generationsCount)
for i := 0; i < generationsCount; i++ {
searchFunctions = append(searchFunctions, generations[i].SearchObject)
}
searchFunc := mergeSearchFunctions(searchFunctions)
gcID := rand.Uint64()
wg := sync.WaitGroup{}
sizesBefore := make([]int, len(generations))
sizesAfter := make([]int, len(generations))
for i := 0; i < generationsCount; i++ {
i := i // remove?
wg.Add(1)
go func() {
generations[i].Mark(gcID, searchFunc)
sizesBefore[i], sizesAfter[i] = generations[i].Sweep()
wg.Done()
}()
}
for i := generationsCount; i < len(generations); i++ {
size := generations[i].Size()
sizesBefore[i] = size
sizesAfter[i] = size
}
wg.Wait()
h.mergeGenerations(sizesBefore, sizesAfter)
h.calculateNewHeapTarget()
}
// GC runs a garbage collection and blocks the caller until the
// garbage collection is complete. It may also block the entire
// program.
func GC() {
if mainHypervisor.gcLock.TryLock() {
mainHypervisor.gc()
ctx, cancel := context.WithCancel(context.Background())
mainHypervisor.gcCancel()
mainHypervisor.gcCancel = cancel
Debugger.numCC.Add(1)
Debugger.lastCC.Store(time.Now().Unix())
mainHypervisor.gcLock.Unlock()
mainHypervisor.gcCtx = ctx
} else {
<-mainHypervisor.gcCtx.Done()
}
}