Source file
src/runtime/mpallocbits.go
1
2
3
4
5 package runtime
6
7 import (
8 "runtime/internal/sys"
9 )
10
11
12 type pageBits [pallocChunkPages / 64]uint64
13
14
15 func (b *pageBits) get(i uint) uint {
16 return uint((b[i/64] >> (i % 64)) & 1)
17 }
18
19
20 func (b *pageBits) block64(i uint) uint64 {
21 return b[i/64]
22 }
23
24
25 func (b *pageBits) set(i uint) {
26 b[i/64] |= 1 << (i % 64)
27 }
28
29
30 func (b *pageBits) setRange(i, n uint) {
31 _ = b[i/64]
32 if n == 1 {
33
34 b.set(i)
35 return
36 }
37
38 j := i + n - 1
39 if i/64 == j/64 {
40 b[i/64] |= ((uint64(1) << n) - 1) << (i % 64)
41 return
42 }
43 _ = b[j/64]
44
45 b[i/64] |= ^uint64(0) << (i % 64)
46 for k := i/64 + 1; k < j/64; k++ {
47 b[k] = ^uint64(0)
48 }
49
50 b[j/64] |= (uint64(1) << (j%64 + 1)) - 1
51 }
52
53
54 func (b *pageBits) setAll() {
55 for i := range b {
56 b[i] = ^uint64(0)
57 }
58 }
59
60
61
62 func (b *pageBits) setBlock64(i uint, v uint64) {
63 b[i/64] |= v
64 }
65
66
67 func (b *pageBits) clear(i uint) {
68 b[i/64] &^= 1 << (i % 64)
69 }
70
71
72 func (b *pageBits) clearRange(i, n uint) {
73 _ = b[i/64]
74 if n == 1 {
75
76 b.clear(i)
77 return
78 }
79
80 j := i + n - 1
81 if i/64 == j/64 {
82 b[i/64] &^= ((uint64(1) << n) - 1) << (i % 64)
83 return
84 }
85 _ = b[j/64]
86
87 b[i/64] &^= ^uint64(0) << (i % 64)
88 for k := i/64 + 1; k < j/64; k++ {
89 b[k] = 0
90 }
91
92 b[j/64] &^= (uint64(1) << (j%64 + 1)) - 1
93 }
94
95
96 func (b *pageBits) clearAll() {
97 for i := range b {
98 b[i] = 0
99 }
100 }
101
102
103
104 func (b *pageBits) clearBlock64(i uint, v uint64) {
105 b[i/64] &^= v
106 }
107
108
109
110 func (b *pageBits) popcntRange(i, n uint) (s uint) {
111 if n == 1 {
112 return uint((b[i/64] >> (i % 64)) & 1)
113 }
114 _ = b[i/64]
115 j := i + n - 1
116 if i/64 == j/64 {
117 return uint(sys.OnesCount64((b[i/64] >> (i % 64)) & ((1 << n) - 1)))
118 }
119 _ = b[j/64]
120 s += uint(sys.OnesCount64(b[i/64] >> (i % 64)))
121 for k := i/64 + 1; k < j/64; k++ {
122 s += uint(sys.OnesCount64(b[k]))
123 }
124 s += uint(sys.OnesCount64(b[j/64] & ((1 << (j%64 + 1)) - 1)))
125 return
126 }
127
128
129
130
131
132
133 type pallocBits pageBits
134
135
136 func (b *pallocBits) summarize() pallocSum {
137 var start, max, cur uint
138 const notSetYet = ^uint(0)
139 start = notSetYet
140 for i := 0; i < len(b); i++ {
141 x := b[i]
142 if x == 0 {
143 cur += 64
144 continue
145 }
146 t := uint(sys.TrailingZeros64(x))
147 l := uint(sys.LeadingZeros64(x))
148
149
150 cur += t
151 if start == notSetYet {
152 start = cur
153 }
154 if cur > max {
155 max = cur
156 }
157
158 cur = l
159 }
160 if start == notSetYet {
161
162 const n = uint(64 * len(b))
163 return packPallocSum(n, n, n)
164 }
165 if cur > max {
166 max = cur
167 }
168 if max >= 64-2 {
169
170 return packPallocSum(start, max, cur)
171 }
172
173
174 outer:
175 for i := 0; i < len(b); i++ {
176 x := b[i]
177
178
179
180
181
182
183
184 x >>= sys.TrailingZeros64(x) & 63
185 if x&(x+1) == 0 {
186 continue
187 }
188
189
190
191 p := max
192 k := uint(1)
193 for {
194
195 for p > 0 {
196 if p <= k {
197
198 x |= x >> (p & 63)
199 if x&(x+1) == 0 {
200 continue outer
201 }
202 break
203 }
204
205 x |= x >> (k & 63)
206 if x&(x+1) == 0 {
207 continue outer
208 }
209 p -= k
210
211
212 k *= 2
213 }
214
215
216 j := uint(sys.TrailingZeros64(^x))
217 x >>= j & 63
218 j = uint(sys.TrailingZeros64(x))
219 x >>= j & 63
220 max += j
221 if x&(x+1) == 0 {
222 continue outer
223 }
224 p = j
225 }
226 }
227 return packPallocSum(start, max, cur)
228 }
229
230
231
232
233
234
235
236
237
238
239 func (b *pallocBits) find(npages uintptr, searchIdx uint) (uint, uint) {
240 if npages == 1 {
241 addr := b.find1(searchIdx)
242 return addr, addr
243 } else if npages <= 64 {
244 return b.findSmallN(npages, searchIdx)
245 }
246 return b.findLargeN(npages, searchIdx)
247 }
248
249
250
251
252
253 func (b *pallocBits) find1(searchIdx uint) uint {
254 _ = b[0]
255 for i := searchIdx / 64; i < uint(len(b)); i++ {
256 x := b[i]
257 if ^x == 0 {
258 continue
259 }
260 return i*64 + uint(sys.TrailingZeros64(^x))
261 }
262 return ^uint(0)
263 }
264
265
266
267
268
269
270
271
272
273
274
275 func (b *pallocBits) findSmallN(npages uintptr, searchIdx uint) (uint, uint) {
276 end, newSearchIdx := uint(0), ^uint(0)
277 for i := searchIdx / 64; i < uint(len(b)); i++ {
278 bi := b[i]
279 if ^bi == 0 {
280 end = 0
281 continue
282 }
283
284
285 if newSearchIdx == ^uint(0) {
286
287
288 newSearchIdx = i*64 + uint(sys.TrailingZeros64(^bi))
289 }
290 start := uint(sys.TrailingZeros64(bi))
291 if end+start >= uint(npages) {
292 return i*64 - end, newSearchIdx
293 }
294
295 j := findBitRange64(^bi, uint(npages))
296 if j < 64 {
297 return i*64 + j, newSearchIdx
298 }
299 end = uint(sys.LeadingZeros64(bi))
300 }
301 return ^uint(0), newSearchIdx
302 }
303
304
305
306
307
308
309
310
311
312
313
314 func (b *pallocBits) findLargeN(npages uintptr, searchIdx uint) (uint, uint) {
315 start, size, newSearchIdx := ^uint(0), uint(0), ^uint(0)
316 for i := searchIdx / 64; i < uint(len(b)); i++ {
317 x := b[i]
318 if x == ^uint64(0) {
319 size = 0
320 continue
321 }
322 if newSearchIdx == ^uint(0) {
323
324
325 newSearchIdx = i*64 + uint(sys.TrailingZeros64(^x))
326 }
327 if size == 0 {
328 size = uint(sys.LeadingZeros64(x))
329 start = i*64 + 64 - size
330 continue
331 }
332 s := uint(sys.TrailingZeros64(x))
333 if s+size >= uint(npages) {
334 size += s
335 return start, newSearchIdx
336 }
337 if s < 64 {
338 size = uint(sys.LeadingZeros64(x))
339 start = i*64 + 64 - size
340 continue
341 }
342 size += 64
343 }
344 if size < uint(npages) {
345 return ^uint(0), newSearchIdx
346 }
347 return start, newSearchIdx
348 }
349
350
351 func (b *pallocBits) allocRange(i, n uint) {
352 (*pageBits)(b).setRange(i, n)
353 }
354
355
356 func (b *pallocBits) allocAll() {
357 (*pageBits)(b).setAll()
358 }
359
360
361 func (b *pallocBits) free1(i uint) {
362 (*pageBits)(b).clear(i)
363 }
364
365
366 func (b *pallocBits) free(i, n uint) {
367 (*pageBits)(b).clearRange(i, n)
368 }
369
370
371 func (b *pallocBits) freeAll() {
372 (*pageBits)(b).clearAll()
373 }
374
375
376
377
378 func (b *pallocBits) pages64(i uint) uint64 {
379 return (*pageBits)(b).block64(i)
380 }
381
382
383
384 func (b *pallocBits) allocPages64(i uint, alloc uint64) {
385 (*pageBits)(b).setBlock64(i, alloc)
386 }
387
388
389
390
391
392 func findBitRange64(c uint64, n uint) uint {
393
394
395
396 p := n - 1
397 k := uint(1)
398 for p > 0 {
399 if p <= k {
400
401 c &= c >> (p & 63)
402 break
403 }
404
405 c &= c >> (k & 63)
406 if c == 0 {
407 return 64
408 }
409 p -= k
410
411
412 k *= 2
413 }
414
415
416
417 return uint(sys.TrailingZeros64(c))
418 }
419
420
421
422
423
424
425
426
427 type pallocData struct {
428 pallocBits
429 scavenged pageBits
430 }
431
432
433
434 func (m *pallocData) allocRange(i, n uint) {
435
436 m.pallocBits.allocRange(i, n)
437 m.scavenged.clearRange(i, n)
438 }
439
440
441
442 func (m *pallocData) allocAll() {
443
444 m.pallocBits.allocAll()
445 m.scavenged.clearAll()
446 }
447
View as plain text