Skip to content

Commit 14849f0

Browse files
committed
runtime: add new page allocator constants and description
This change is the first of a series of changes which replace the current page allocator (which is based on the contents of mgclarge.go and some of mheap.go) with one based on free/used bitmaps. It adds in the key constants for the page allocator as well as a comment describing the implementation. Updates #35112. Change-Id: I839d3a07f46842ad379701d27aa691885afdba63 Reviewed-on: https://go-review.googlesource.com/c/go/+/190619 Run-TryBot: Michael Knyszek <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent 0bf2eb5 commit 14849f0

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/runtime/mpagealloc.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
)

src/runtime/mpagealloc_32bit.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
// +build 386 arm mips mipsle wasm darwin,arm64
6+
7+
// wasm is a treated as a 32-bit architecture for the purposes of the page
8+
// allocator, even though it has 64-bit pointers. This is because any wasm
9+
// pointer always has its top 32 bits as zero, so the effective heap address
10+
// space is only 2^32 bytes in size (see heapAddrBits).
11+
12+
// darwin/arm64 is treated as a 32-bit architecture for the purposes of the
13+
// page allocator, even though it has 64-bit pointers and a 33-bit address
14+
// space (see heapAddrBits). The 33 bit address space cannot be rounded up
15+
// to 64 bits because there are too many summary levels to fit in just 33
16+
// bits.
17+
18+
package runtime
19+
20+
const (
21+
// The number of levels in the radix tree.
22+
summaryLevels = 4
23+
)

src/runtime/mpagealloc_64bit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
// +build amd64 !darwin,arm64 mips64 mips64le ppc64 ppc64le s390x
6+
7+
// See mpagealloc_32bit.go for why darwin/arm64 is excluded here.
8+
9+
package runtime
10+
11+
const (
12+
// The number of levels in the radix tree.
13+
summaryLevels = 5
14+
)

0 commit comments

Comments
 (0)