Skip to content

Commit 14c1fdf

Browse files
authored
docs: update 内存池之PoolChunk设计与实现.md(doocs#71)
1 parent 046a6b0 commit 14c1fdf

File tree

1 file changed

+96
-85
lines changed

1 file changed

+96
-85
lines changed
Lines changed: 96 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,114 @@
1-
该文所涉及的netty源码版本为4.1.16。
1+
该文所涉及的 netty 源码版本为 4.1.16。
22

33
## 在一开始需要明确的几个概念
4-
在Netty的内存池的PoolChunk中,先要明确以下几个概念。
5-
- page: page是chunk中所能申请到的最小内存单位。
6-
- chunk: 一个chunk是一组page的集合
7-
- 在PoolChunk中,chunkSize的大小是2^maxOrder*pageSize,其中2^maxOrder是PoolChunk中的完全二叉树叶子结点的数量,pageSize则是单个page的大小。
8-
综合如上所述,举一个数字上的例子,默认情况下,单个Page的大小为8192,也就是8kb,maxOrder默认情况下是11,因此在这个情况下PoolChunk中的二叉树的叶子节点数量是2048,chunkSize的大小则是2048*8kb为16M。
9-
10-
## PoolChunk的内部完全二叉树结构
11-
PoolChunk中的page通过一颗完全二叉树来达到快速访达及操作,而不需要通过O(n)的时间复杂度来进行遍历,并耗费相当大的空间来记录各个page的使用情况。一颗完全二叉树的结构如下所示:
12-
- 高度=0 1 个节点 (单个节点表示的大小为chunkSize)
13-
- 高度=1 2个节点 (单个节点表示的大小为chunkSize/2)
14-
- ..
15-
- ..
16-
- 高度=d 2^d个节点 (单个节点表示的大小为chunkSize/2^d)
17-
- ..
18-
- 高度=maxOrder 2^maxOrder个节点 (单个节点的大小为chunkSize/2^{maxOrder},也就是pageSize)
19-
20-
在这棵树的帮助下,当我们要申请x大小的内存的时候 ,得到比x最接近的chunkSize/2^k的大小,也就是说只要从左开始找到k层第一个没有被使用的节点即可开始将其子树的叶子结点的page进行分配。
21-
22-
## PoolChunk的二叉树使用状态
23-
单依靠上述的完全二叉树是无法达到内存池设计的目的的,因为缺少了page的使用情况,仍旧需要一个数据结构来辅助记录各个节点的使用情况。
24-
PoolChunk中还给出了一个byte数组memoryMap,大小为完全二叉树所有节点的个数,在之前的例子中这个byte数组就为4096。在初始情况下,这个数组每个位置上的初始指为该位置的节点在完全二叉树中的高度。因此,这个数组memoryMap就有了以下几种状态。
25-
- 1) memoryMap[i] = i节点在完全二叉树中的深度,代表当前节点下的子树都还没有被分配。
26-
- 2) memoryMap[i] > i节点在完全二叉树中的深度, 这个节点下的子树也就有节点被使用,但是仍有节点处于空闲状态。
27-
- 3) memoryMap[i] = maxOrder + 1,这个节点下面的子树已经完全被使用。
28-
这个Byte数组,就相当于为这个完全二叉树准备了状态与索引存储,可以高效的在二叉树中选择定位所需要指定大小的子树进行分配。
4+
5+
在 Netty 的内存池的 PoolChunk 中,先要明确以下几个概念。
6+
7+
- page: page 是 chunk 中所能申请到的最小内存单位。
8+
- chunk: 一个 chunk 是一组 page 的集合
9+
- 在 PoolChunk 中,chunkSize 的大小是 `2^maxOrder * pageSize`,其中 2^maxOrder 是 PoolChunk 中的完全二叉树叶子结点的数量,pageSize 则是单个 page 的大小。
10+
11+
综合如上所述,举一个数字上的例子,默认情况下,单个 Page 的大小为 8192,也就是 8kb,maxOrder 默认情况下是 11,因此在这个情况下 PoolChunk 中的二叉树的叶子节点数量是 2048,chunkSize 的大小则是 2048*8kb 为 16M。
12+
13+
## PoolChunk 的内部完全二叉树结构
14+
15+
PoolChunk 中的 page 通过一颗完全二叉树来达到快速访达及操作,而不需要通过 O(n)的时间复杂度来进行遍历,并耗费相当大的空间来记录各个 page 的使用情况。一颗完全二叉树的结构如下所示:
16+
17+
- 高度=0 1 个节点 (单个节点表示的大小为 chunkSize)
18+
- 高度=1 2 个节点 (单个节点表示的大小为 chunkSize/2)
19+
- ..
20+
- ..
21+
- 高度=d 2^d 个节点 (单个节点表示的大小为 chunkSize/2^d)
22+
- ..
23+
- 高度=maxOrder 2^maxOrder 个节点 (单个节点的大小为 chunkSize/2^{maxOrder},也就是 pageSize)
24+
25+
在这棵树的帮助下,当我们要申请 x 大小的内存的时候 ,得到比 x 最接近的 chunkSize/2^k 的大小,也就是说只要从左开始找到 k 层第一个没有被使用的节点即可开始将其子树的叶子结点的 page 进行分配。
26+
27+
## PoolChunk 的二叉树使用状态
28+
29+
单依靠上述的完全二叉树是无法达到内存池设计的目的的,因为缺少了 page 的使用情况,仍旧需要一个数据结构来辅助记录各个节点的使用情况。
30+
PoolChunk 中还给出了一个 byte 数组 memoryMap,大小为完全二叉树所有节点的个数,在之前的例子中这个 byte 数组就为 4096。在初始情况下,这个数组每个位置上的初始指为该位置的节点在完全二叉树中的高度。因此,这个数组 memoryMap 就有了以下几种状态。
31+
32+
- 1. memoryMap[i] = i 节点在完全二叉树中的深度,代表当前节点下的子树都还没有被分配。
33+
- 2. memoryMap[i] > i 节点在完全二叉树中的深度, 这个节点下的子树也就有节点被使用,但是仍有节点处于空闲状态。
34+
- 3. memoryMap[i] = maxOrder + 1,这个节点下面的子树已经完全被使用。
35+
这个 Byte 数组,就相当于为这个完全二叉树准备了状态与索引存储,可以高效的在二叉树中选择定位所需要指定大小的子树进行分配。
2936

3037
## 业务逻辑展开
31-
```Java
32-
private int allocateNode(int d) {
33-
int id = 1;
34-
int initial = - (1 << d); // has last d bits = 0 and rest all = 1
35-
byte val = value(id);
36-
if (val > d) { // unusable
37-
return -1;
38-
}
39-
while (val < d || (id & initial) == 0) { // id & initial == 1 << d for all ids at depth d, for < d it is 0
40-
id <<= 1;
38+
39+
```java
40+
private int allocateNode(int d) {
41+
int id = 1;
42+
int initial = - (1 << d); // has last d bits = 0 and rest all = 1
43+
byte val = value(id);
44+
if (val > d) { // unusable
45+
return -1;
46+
}
47+
while (val < d || (id & initial) == 0) { // id & initial == 1 << d for all ids at depth d, for < d it is 0
48+
id <<= 1;
49+
val = value(id);
50+
if (val > d) {
51+
id ^= 1;
4152
val = value(id);
42-
if (val > d) {
43-
id ^= 1;
44-
val = value(id);
45-
}
4653
}
47-
byte value = value(id);
48-
assert value == d && (id & initial) == 1 << d : String.format("val = %d, id & initial = %d, d = %d",
49-
value, id & initial, d);
50-
setValue(id, unusable); // mark as unusable
51-
updateParentsAlloc(id);
52-
return id;
5354
}
55+
byte value = value(id);
56+
assert value == d && (id & initial) == 1 << d : String.format("val = %d, id & initial = %d, d = %d",
57+
value, id & initial, d);
58+
setValue(id, unusable); // mark as unusable
59+
updateParentsAlloc(id);
60+
return id;
61+
}
5462
```
55-
allocateNode(int d)方法用来在完全二叉树中以从左开始的顺序获取一颗高度为d的没有被使用过的子树。具体顺序如下:
56-
- 首先从根节点1开始,判断memoryMap[1]的值,如果大于d,则说明当前的二叉树已经不存在能够分配的节点了。如果小于d,则可以继续往下分配。
57-
- 如果其左节点在memoryMap的值小于d,则继续从左节点往下寻找。如果大于,则从其右节点开始往下寻找。
63+
64+
allocateNode(int d)方法用来在完全二叉树中以从左开始的顺序获取一颗高度为 d 的没有被使用过的子树。具体顺序如下:
65+
66+
- 首先从根节点 1 开始,判断 memoryMap[1]的值,如果大于 d,则说明当前的二叉树已经不存在能够分配的节点了。如果小于 d,则可以继续往下分配。
67+
- 如果其左节点在 memoryMap 的值小于 d,则继续从左节点往下寻找。如果大于,则从其右节点开始往下寻找。
5868
- 在下一层的节点中持续进行上述的判断,直到在书中找到符合高度条件的子树。
5969

60-
```Java
61-
private long allocateRun(int normCapacity) {
62-
int d = maxOrder - (log2(normCapacity) - pageShifts);
70+
```java
71+
private long allocateRun(int normCapacity) {
72+
int d = maxOrder - (log2(normCapacity) - pageShifts);
73+
int id = allocateNode(d);
74+
if (id < 0) {
75+
return id;
76+
}
77+
freeBytes -= runLength(id);
78+
return id;
79+
}
80+
```
81+
82+
allocateRun()方法就是在上文的 allocateNode()的前提下,根据指定的大小的内存在二叉树上分配指定大小的子树。比如说在上述 16M 大小每个 page8kb 的 chunk 中寻求 64k 的内存的时候,需要 8 个 page 叶子结点,那么就是需要一个高度为 4 的完全二叉树,那么也就是只要在 PoolChunk 中通过 allocateNode()方法从完全二叉树的第 7 层开始从左往右找到一颗可以使用的子树即可。
83+
84+
```java
85+
private long allocateSubpage(int normCapacity) {
86+
// Obtain the head of the PoolSubPage pool that is owned by the PoolArena and synchronize on it.
87+
// This is need as we may add it back and so alter the linked-list structure.
88+
PoolSubpage<T> head = arena.findSubpagePoolHead(normCapacity);
89+
synchronized (head) {
90+
int d = maxOrder; // subpages are only be allocated from pages i.e., leaves
6391
int id = allocateNode(d);
6492
if (id < 0) {
6593
return id;
6694
}
67-
freeBytes -= runLength(id);
68-
return id;
69-
}
70-
```
71-
allocateRun()方法就是在上文的allocateNode()的前提下,根据指定的大小的内存在二叉树上分配指定大小的子树。比如说在上述16M大小每个page8kb的chunk中寻求64k的内存的时候,需要8个page叶子结点,那么就是需要一个高度为4的完全二叉树,那么也就是只要在PoolChunk中通过allocateNode()方法从完全二叉树的第7层开始从左往右找到一颗可以使用的子树即可。
72-
73-
```Java
74-
private long allocateSubpage(int normCapacity) {
75-
// Obtain the head of the PoolSubPage pool that is owned by the PoolArena and synchronize on it.
76-
// This is need as we may add it back and so alter the linked-list structure.
77-
PoolSubpage<T> head = arena.findSubpagePoolHead(normCapacity);
78-
synchronized (head) {
79-
int d = maxOrder; // subpages are only be allocated from pages i.e., leaves
80-
int id = allocateNode(d);
81-
if (id < 0) {
82-
return id;
83-
}
84-
85-
final PoolSubpage<T>[] subpages = this.subpages;
86-
final int pageSize = this.pageSize;
87-
88-
freeBytes -= pageSize;
89-
90-
int subpageIdx = subpageIdx(id);
91-
PoolSubpage<T> subpage = subpages[subpageIdx];
92-
if (subpage == null) {
93-
subpage = new PoolSubpage<T>(head, this, id, runOffset(id), pageSize, normCapacity);
94-
subpages[subpageIdx] = subpage;
95-
} else {
96-
subpage.init(head, normCapacity);
97-
}
98-
return subpage.allocate();
95+
96+
final PoolSubpage<T>[] subpages = this.subpages;
97+
final int pageSize = this.pageSize;
98+
99+
freeBytes -= pageSize;
100+
101+
int subpageIdx = subpageIdx(id);
102+
PoolSubpage<T> subpage = subpages[subpageIdx];
103+
if (subpage == null) {
104+
subpage = new PoolSubpage<T>(head, this, id, runOffset(id), pageSize, normCapacity);
105+
subpages[subpageIdx] = subpage;
106+
} else {
107+
subpage.init(head, normCapacity);
99108
}
109+
return subpage.allocate();
100110
}
111+
}
101112
```
102-
当向PoolChunk申请的内存大小小于pageSize的时候,将直接通过allocateSubpage()方法尝试直接在叶子结点,也就是二叉树的最后一层选择一个空的还未使用的叶子结点,在选择的叶子结点中构造一个PoolSubPage来返回,而不需要耗费整整一个叶子结点导致内存占用浪费。
103113

114+
当向 PoolChunk 申请的内存大小小于 pageSize 的时候,将直接通过 allocateSubpage()方法尝试直接在叶子结点,也就是二叉树的最后一层选择一个空的还未使用的叶子结点,在选择的叶子结点中构造一个 PoolSubPage 来返回,而不需要耗费整整一个叶子结点导致内存占用浪费。

0 commit comments

Comments
 (0)