-
Notifications
You must be signed in to change notification settings - Fork 270
/
pool.go
187 lines (164 loc) · 5.32 KB
/
pool.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
/*
* Copyright (c) 2024 Yunshan Networks
*
* 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 pool
import (
"math"
"reflect"
"sync"
"sync/atomic"
logging "github.com/op/go-logging"
)
var log = logging.MustGetLogger("pool")
type Option = interface{}
type OptionPoolSizePerCPU int
type OptionInitFullPoolSize int // 太大会导致Get操作卡顿,太小会导致创建过多的slice
type OptionCounterNameSuffix string
const OPTIMAL_BLOCK_SIZE = 1 << 16
const POOL_SIZE_PER_CPU = OptionPoolSizePerCPU(256)
const INIT_FULL_POOL_SIZE = OptionInitFullPoolSize(256)
type Counter struct {
Name string
ObjectSize uint64
PoolSizePerCPU uint32
InitFullPoolSize uint32
closed bool
InUseObjects uint64 `statsd:"in_use_objects,gauge"`
InUseBytes uint64 `statsd:"in_use_bytes,gauge"`
}
func (c *Counter) GetCounter() interface{} {
return c
}
func (c *Counter) Closed() bool {
return c.closed
}
// 此Callback可用于为Counter添加statsd监控
type CounterRegisterCallback func(*Counter)
var (
counterListLock sync.Mutex
counterRegisterCallback CounterRegisterCallback
allCounters []*Counter
)
func SetCounterRegisterCallback(callback CounterRegisterCallback) {
counterListLock.Lock()
counterRegisterCallback = callback
for _, counter := range allCounters {
counterRegisterCallback(counter)
}
counterListLock.Unlock()
}
// sync.Pool对于每一个系统线程,能够无锁提取存放一个元素,
// 其余元素会通过锁追加到数组中。为了能够尽可能避免锁的使用,
// 我们需要利用好这一个元素的位置,所以在这个元素上放置slice指针
// 作为实际的pool使用,每次Get/Put时,先拿到slice,弹出/推入元素后再
// 将slice放回,以尽可能无锁分配释放资源
type LockFreePool struct {
emptyPool *sync.Pool
fullPool *sync.Pool
counter *Counter
}
func (p *LockFreePool) Get() interface{} {
atomic.AddUint64(&p.counter.InUseObjects, 1)
atomic.AddUint64(&p.counter.InUseBytes, p.counter.ObjectSize)
elemPool := p.fullPool.Get().(*[]interface{}) // avoid convT2Eslice
pool := *elemPool
e := pool[len(pool)-1]
*elemPool = pool[:len(pool)-1]
if len(pool) > 1 {
p.fullPool.Put(elemPool)
} else {
p.emptyPool.Put(elemPool) // Empty, 还给别的CPU
}
return e
}
func (p *LockFreePool) Put(x interface{}) {
atomic.AddUint64(&p.counter.InUseObjects, math.MaxUint64)
atomic.AddUint64(&p.counter.InUseBytes, math.MaxUint64-p.counter.ObjectSize+1)
pool := p.emptyPool.Get().(*[]interface{}) // avoid convT2Eslice
*pool = append(*pool, x)
if len(*pool) < cap(*pool) {
p.emptyPool.Put(pool)
} else {
p.fullPool.Put(pool) // Full, 还给别的CPU
}
}
// 注意OptionInitFullPoolSize不能大于OptionPoolSizePerCPU,且不能小于等于0
func NewLockFreePool(alloc func() interface{}, options ...Option) *LockFreePool {
// options
poolSizePerCPU := POOL_SIZE_PER_CPU
initFullPoolSize := INIT_FULL_POOL_SIZE
counterNameSuffix := ""
for _, opt := range options {
if size, ok := opt.(OptionPoolSizePerCPU); ok {
poolSizePerCPU = size
} else if size, ok := opt.(OptionInitFullPoolSize); ok {
initFullPoolSize = size
} else if suffixName, ok := opt.(OptionCounterNameSuffix); ok {
counterNameSuffix = string(suffixName)
}
}
if poolSizePerCPU < OptionPoolSizePerCPU(initFullPoolSize) || initFullPoolSize <= 0 {
poolSizePerCPU = POOL_SIZE_PER_CPU
initFullPoolSize = INIT_FULL_POOL_SIZE
}
objectType := reflect.Indirect(reflect.ValueOf(alloc())).Type()
objectSize := uint64(objectType.Size())
if len(options) == 0 { // automatically adjust pool size if no option is assigned
optimalSize := uint64(OPTIMAL_BLOCK_SIZE) / objectSize
if optimalSize > 4 && OptionPoolSizePerCPU(optimalSize) < POOL_SIZE_PER_CPU {
poolSizePerCPU = OptionPoolSizePerCPU(optimalSize)
initFullPoolSize = OptionInitFullPoolSize(optimalSize)
}
}
// functions
newEmptySlice := func() interface{} {
p := make([]interface{}, 0, poolSizePerCPU)
return &p
}
newFullSlice := func() interface{} {
p := make([]interface{}, initFullPoolSize, poolSizePerCPU)
for i := OptionInitFullPoolSize(0); i < initFullPoolSize; i++ {
p[i] = alloc()
}
return &p
}
// counter
counter := &Counter{
Name: objectType.String() + counterNameSuffix,
ObjectSize: objectSize,
PoolSizePerCPU: uint32(poolSizePerCPU),
InitFullPoolSize: uint32(initFullPoolSize),
}
counterListLock.Lock()
for _, c := range allCounters {
if c.Name == counter.Name {
c.closed = true // close old counter with the same objectType
}
}
if counterRegisterCallback != nil {
counterRegisterCallback(counter)
}
allCounters = append(allCounters, counter)
counterListLock.Unlock()
return &LockFreePool{
emptyPool: &sync.Pool{
New: newEmptySlice,
},
fullPool: &sync.Pool{
New: newFullSlice,
},
counter: counter,
}
}