forked from couchbase/indexing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slab_manager.go
226 lines (180 loc) · 6.45 KB
/
slab_manager.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2014 Couchbase, Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
package indexer
import (
slab "github.com/couchbase/go-slab"
"sync"
)
//Slab Manager provides a wrapper around go-slab library to allow concurrent access and
//memory accounting(e.g. limit the max memory of Arena etc).
//It also has a release buffer to queue up release requests to reduce contention
//between concurrent threads waiting to return back memory.
type SlabManager interface {
//AllocBuf allocates a buffer of given size. If returned buffer is nil,
//Message will have error message
AllocBuf(bufSize int) ([]byte, Message)
//ReleaseBuf releases the buffer back to free pool
ReleaseBuf(buf []byte) bool
//SetMaxMemoryLimit sets the maximum memory that can be allocated
SetMaxMemoryLimit(maxMemAlloc uint64) bool
//GetMaxMemoryLimit returns the maximum memory that can be allocated
GetMaxMemoryLimit() uint64
}
type slabManager struct {
arena *slab.Arena
lock sync.Mutex //concurrent access to slab library needs to be protected
startChunkSize int //initial chunk size for the slab library
slabSize int //size of each slab
maxMemAlloc uint64 //max memory that can be allocated
currUserAllocatedMemory uint64 //curr memory user has allocated
currActualAllocatedMemory uint64 //curr slab memory allocated to fulfill the request
releaseChan chan []byte //buffered chan to queue release requests
internalErr bool //this flag is set if underlying library goes to a bad state
stopch StopChannel //panic handler closes this channel to signal stop
}
const DEFAULT_GROWTH_FACTOR float64 = 2.0
const DEFAULT_RELEASE_BUFFER int = 10000
//NewSlabManager returns a slabManager struct instance with an initialized Arena
func NewSlabManager(startChunkSize int, slabSize int, maxMemAlloc uint64) (SlabManager, Message) {
sm := &slabManager{
arena: slab.NewArena(startChunkSize,
slabSize,
DEFAULT_GROWTH_FACTOR,
nil), //default make([]byte) for slab memory
startChunkSize: startChunkSize,
slabSize: slabSize,
maxMemAlloc: maxMemAlloc,
releaseChan: make(chan []byte, DEFAULT_RELEASE_BUFFER),
}
//init error from slab library
if sm.arena == nil {
return nil, &MsgError{
err: Error{code: ERROR_SLAB_INIT,
severity: FATAL,
category: SLAB_MANAGER}}
}
//start the release handler
go sm.releaseHandler()
return sm, nil
}
//AllocBuf is a thread-safe wrapper over go-slab's Alloc.
//It also limits the memory allocation to the specified limit.
func (sm *slabManager) AllocBuf(bufSize int) ([]byte, Message) {
//bad alloc request
if bufSize <= 0 {
return nil, &MsgError{
err: Error{code: ERROR_SLAB_BAD_ALLOC_REQUEST,
severity: NORMAL,
category: SLAB_MANAGER}}
}
//panic handler
defer sm.panicHandler()
//if the underlying library is in bad shape, return internal error
//caller has to start a new slab manager
if sm.internalErr {
return nil, &MsgError{
err: Error{code: ERROR_SLAB_INTERNAL_ERROR,
severity: FATAL,
category: SLAB_MANAGER}}
}
//acquire lock and allocate memory
sm.lock.Lock()
defer sm.lock.Unlock()
//return error if caller has exhausted quota
if sm.currUserAllocatedMemory >= sm.maxMemAlloc {
return nil, &MsgError{
err: Error{code: ERROR_SLAB_MEM_LIMIT_EXCEED,
severity: NORMAL,
category: SLAB_MANAGER}}
}
//allocate memory
buf := sm.arena.Alloc(bufSize)
if buf == nil {
return nil, &MsgError{
err: Error{code: ERROR_SLAB_INTERNAL_ALLOC_ERROR,
severity: FATAL,
category: SLAB_MANAGER}}
}
return buf, nil
}
//incrementStats increments the stats for given buf size
func (sm *slabManager) incrementStats(bufSize int) {
sm.currUserAllocatedMemory += uint64(bufSize)
if bufSize <= sm.startChunkSize {
sm.currActualAllocatedMemory += uint64(sm.startChunkSize)
} else { //get the right chunk size
sm.currActualAllocatedMemory += uint64(((bufSize - 1) / sm.startChunkSize) *
int(DEFAULT_GROWTH_FACTOR) * sm.startChunkSize)
}
}
//ReleaseBuf queues the released buf on a buffered channel and returns.
//This helps to avoid lock contention between concurrent threads
//waiting to return back memory. A background goroutine will
//read from the channel and actually return the memory.
func (sm *slabManager) ReleaseBuf(buf []byte) bool {
//if internalErr has happened, underlying library is unusable
if sm.internalErr {
return false
}
sm.releaseChan <- buf
return true
}
//releaseHandler keeps polling the releaseChan and returns any buffer release back to the slab.
//It is a thread-safe wrapper over go-slab's DecRef.
func (sm *slabManager) releaseHandler() {
//panic handler
defer sm.panicHandler()
ok := true
var buf []byte
for ok {
select {
case buf, ok = <-sm.releaseChan:
func() {
//lock the arena and release back memory
sm.lock.Lock()
defer sm.lock.Unlock()
sm.arena.DecRef(buf)
sm.decrementStats(len(buf))
}()
case <-sm.stopch:
return
}
}
}
//decrementStats decrements the stats for given buf size
func (sm *slabManager) decrementStats(bufSize int) {
sm.currUserAllocatedMemory -= uint64(bufSize)
if bufSize <= sm.startChunkSize {
sm.currActualAllocatedMemory -= uint64(sm.startChunkSize)
} else { //get the right chunk size
sm.currActualAllocatedMemory -= uint64(((bufSize - 1) / sm.startChunkSize) *
int(DEFAULT_GROWTH_FACTOR) * sm.startChunkSize)
}
}
//panicHandler handles the panic from underlying slab allocation library
func (sm *slabManager) panicHandler() {
//panic recovery
if r := recover(); r != nil {
sm.internalErr = true //panic from underlying, mark the whole thing unusable
close(sm.stopch)
}
}
//SetMaxMemoryLimit sets the maximum memory that can be allocated
func (sm *slabManager) SetMaxMemoryLimit(maxMemAlloc uint64) bool {
sm.lock.Lock()
defer sm.lock.Unlock()
sm.maxMemAlloc = maxMemAlloc
return true
}
//GetMaxMemoryLimit returns the maximum memory that can be allocated
func (sm *slabManager) GetMaxMemoryLimit() uint64 {
sm.lock.Lock()
defer sm.lock.Unlock()
return sm.maxMemAlloc
}