Source file
src/runtime/mpallocbits.go
Documentation: runtime
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, most, 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 most = max(most, cur)
155
156 cur = l
157 }
158 if start == notSetYet {
159
160 const n = uint(64 * len(b))
161 return packPallocSum(n, n, n)
162 }
163 most = max(most, cur)
164
165 if most >= 64-2 {
166
167 return packPallocSum(start, most, cur)
168 }
169
170
171 outer:
172 for i := 0; i < len(b); i++ {
173 x := b[i]
174
175
176
177
178
179
180
181 x >>= sys.TrailingZeros64(x) & 63
182 if x&(x+1) == 0 {
183 continue
184 }
185
186
187
188 p := most
189 k := uint(1)
190 for {
191
192 for p > 0 {
193 if p <= k {
194
195 x |= x >> (p & 63)
196 if x&(x+1) == 0 {
197 continue outer
198 }
199 break
200 }
201
202 x |= x >> (k & 63)
203 if x&(x+1) == 0 {
204 continue outer
205 }
206 p -= k
207
208
209 k *= 2
210 }
211
212
213 j := uint(sys.TrailingZeros64(^x))
214 x >>= j & 63
215 j = uint(sys.TrailingZeros64(x))
216 x >>= j & 63
217 most += j
218 if x&(x+1) == 0 {
219 continue outer
220 }
221 p = j
222 }
223 }
224 return packPallocSum(start, most, cur)
225 }
226
227
228
229
230
231
232
233
234
235
236 func (b *pallocBits) find(npages uintptr, searchIdx uint) (uint, uint) {
237 if npages == 1 {
238 addr := b.find1(searchIdx)
239 return addr, addr
240 } else if npages <= 64 {
241 return b.findSmallN(npages, searchIdx)
242 }
243 return b.findLargeN(npages, searchIdx)
244 }
245
246
247
248
249
250 func (b *pallocBits) find1(searchIdx uint) uint {
251 _ = b[0]
252 for i := searchIdx / 64; i < uint(len(b)); i++ {
253 x := b[i]
254 if ^x == 0 {
255 continue
256 }
257 return i*64 + uint(sys.TrailingZeros64(^x))
258 }
259 return ^uint(0)
260 }
261
262
263
264
265
266
267
268
269
270
271
272 func (b *pallocBits) findSmallN(npages uintptr, searchIdx uint) (uint, uint) {
273 end, newSearchIdx := uint(0), ^uint(0)
274 for i := searchIdx / 64; i < uint(len(b)); i++ {
275 bi := b[i]
276 if ^bi == 0 {
277 end = 0
278 continue
279 }
280
281
282 if newSearchIdx == ^uint(0) {
283
284
285 newSearchIdx = i*64 + uint(sys.TrailingZeros64(^bi))
286 }
287 start := uint(sys.TrailingZeros64(bi))
288 if end+start >= uint(npages) {
289 return i*64 - end, newSearchIdx
290 }
291
292 j := findBitRange64(^bi, uint(npages))
293 if j < 64 {
294 return i*64 + j, newSearchIdx
295 }
296 end = uint(sys.LeadingZeros64(bi))
297 }
298 return ^uint(0), newSearchIdx
299 }
300
301
302
303
304
305
306
307
308
309
310
311 func (b *pallocBits) findLargeN(npages uintptr, searchIdx uint) (uint, uint) {
312 start, size, newSearchIdx := ^uint(0), uint(0), ^uint(0)
313 for i := searchIdx / 64; i < uint(len(b)); i++ {
314 x := b[i]
315 if x == ^uint64(0) {
316 size = 0
317 continue
318 }
319 if newSearchIdx == ^uint(0) {
320
321
322 newSearchIdx = i*64 + uint(sys.TrailingZeros64(^x))
323 }
324 if size == 0 {
325 size = uint(sys.LeadingZeros64(x))
326 start = i*64 + 64 - size
327 continue
328 }
329 s := uint(sys.TrailingZeros64(x))
330 if s+size >= uint(npages) {
331 size += s
332 return start, newSearchIdx
333 }
334 if s < 64 {
335 size = uint(sys.LeadingZeros64(x))
336 start = i*64 + 64 - size
337 continue
338 }
339 size += 64
340 }
341 if size < uint(npages) {
342 return ^uint(0), newSearchIdx
343 }
344 return start, newSearchIdx
345 }
346
347
348 func (b *pallocBits) allocRange(i, n uint) {
349 (*pageBits)(b).setRange(i, n)
350 }
351
352
353 func (b *pallocBits) allocAll() {
354 (*pageBits)(b).setAll()
355 }
356
357
358 func (b *pallocBits) free1(i uint) {
359 (*pageBits)(b).clear(i)
360 }
361
362
363 func (b *pallocBits) free(i, n uint) {
364 (*pageBits)(b).clearRange(i, n)
365 }
366
367
368 func (b *pallocBits) freeAll() {
369 (*pageBits)(b).clearAll()
370 }
371
372
373
374
375 func (b *pallocBits) pages64(i uint) uint64 {
376 return (*pageBits)(b).block64(i)
377 }
378
379
380
381 func (b *pallocBits) allocPages64(i uint, alloc uint64) {
382 (*pageBits)(b).setBlock64(i, alloc)
383 }
384
385
386
387
388
389 func findBitRange64(c uint64, n uint) uint {
390
391
392
393 p := n - 1
394 k := uint(1)
395 for p > 0 {
396 if p <= k {
397
398 c &= c >> (p & 63)
399 break
400 }
401
402 c &= c >> (k & 63)
403 if c == 0 {
404 return 64
405 }
406 p -= k
407
408
409 k *= 2
410 }
411
412
413
414 return uint(sys.TrailingZeros64(c))
415 }
416
417
418
419
420
421
422
423
424 type pallocData struct {
425 pallocBits
426 scavenged pageBits
427 }
428
429
430
431 func (m *pallocData) allocRange(i, n uint) {
432
433 m.pallocBits.allocRange(i, n)
434 m.scavenged.clearRange(i, n)
435 }
436
437
438
439 func (m *pallocData) allocAll() {
440
441 m.pallocBits.allocAll()
442 m.scavenged.clearAll()
443 }
444
View as plain text