|
| 1 | +// Copyright 2019 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Page allocator. |
| 6 | +// |
| 7 | +// The page allocator manages mapped pages (defined by pageSize, NOT |
| 8 | +// physPageSize) for allocation and re-use. It is embedded into mheap. |
| 9 | +// |
| 10 | +// Pages are managed using a bitmap that is sharded into chunks. |
| 11 | +// In the bitmap, 1 means in-use, and 0 means free. The bitmap spans the |
| 12 | +// process's address space. Chunks are allocated using a SLAB allocator |
| 13 | +// and pointers to chunks are managed in one large array, which is mapped |
| 14 | +// in as needed. |
| 15 | +// |
| 16 | +// The bitmap is efficiently searched by using a radix tree in combination |
| 17 | +// with fast bit-wise intrinsics. Allocation is performed using an address-ordered |
| 18 | +// first-fit approach. |
| 19 | +// |
| 20 | +// Each entry in the radix tree is a summary that describes three properties of |
| 21 | +// a particular region of the address space: the number of contiguous free pages |
| 22 | +// at the start and end of the region it represents, and the maximum number of |
| 23 | +// contiguous free pages found anywhere in that region. |
| 24 | +// |
| 25 | +// Each level of the radix tree is stored as one contiguous array, which represents |
| 26 | +// a different granularity of subdivision of the processes' address space. Thus, this |
| 27 | +// radix tree is actually implicit in these large arrays, as opposed to having explicit |
| 28 | +// dynamically-allocated pointer-based node structures. Naturally, these arrays may be |
| 29 | +// quite large for system with large address spaces, so in these cases they are mapped |
| 30 | +// into memory as needed. The leaf summaries of the tree correspond to a bitmap chunk. |
| 31 | +// |
| 32 | +// The root level (referred to as L0 and index 0 in pageAlloc.summary) has each |
| 33 | +// summary represent the largest section of address space (16 GiB on 64-bit systems), |
| 34 | +// with each subsequent level representing successively smaller subsections until we |
| 35 | +// reach the finest granularity at the leaves, a chunk. |
| 36 | +// |
| 37 | +// More specifically, each summary in each level (except for leaf summaries) |
| 38 | +// represents some number of entries in the following level. For example, each |
| 39 | +// summary in the root level may represent a 16 GiB region of address space, |
| 40 | +// and in the next level there could be 8 corresponding entries which represent 2 |
| 41 | +// GiB subsections of that 16 GiB region, each of which could correspond to 8 |
| 42 | +// entries in the next level which each represent 256 MiB regions, and so on. |
| 43 | +// |
| 44 | +// Thus, this design only scales to heaps so large, but can always be extended to |
| 45 | +// larger heaps by simply adding levels to the radix tree, which mostly costs |
| 46 | +// additional virtual address space. The choice of managing large arrays also means |
| 47 | +// that a large amount of virtual address space may be reserved by the runtime. |
| 48 | + |
| 49 | +package runtime |
| 50 | + |
| 51 | +const ( |
| 52 | + // The size of a bitmap chunk, i.e. the amount of bits (that is, pages) to consider |
| 53 | + // in the bitmap at once. |
| 54 | + pallocChunkPages = 1 << logPallocChunkPages |
| 55 | + pallocChunkBytes = pallocChunkPages * pageSize |
| 56 | + logPallocChunkPages = 9 |
| 57 | + logPallocChunkBytes = logPallocChunkPages + pageShift |
| 58 | + |
| 59 | + // The number of radix bits for each level. |
| 60 | + // |
| 61 | + // The value of 3 is chosen such that the block of summaries we need to scan at |
| 62 | + // each level fits in 64 bytes (2^3 summaries * 8 bytes per summary), which is |
| 63 | + // close to the L1 cache line width on many systems. Also, a value of 3 fits 4 tree |
| 64 | + // levels perfectly into the 21-bit mallocBits summary field at the root level. |
| 65 | + // |
| 66 | + // The following equation explains how each of the constants relate: |
| 67 | + // summaryL0Bits + (summaryLevels-1)*summaryLevelBits + logPallocChunkBytes = heapAddrBits |
| 68 | + // |
| 69 | + // summaryLevels is an architecture-dependent value defined in mpagealloc_*.go. |
| 70 | + summaryLevelBits = 3 |
| 71 | + summaryL0Bits = heapAddrBits - logPallocChunkBytes - (summaryLevels-1)*summaryLevelBits |
| 72 | +) |
0 commit comments