Linux 内核的内存管理 - Zones

Describing Physical Memory (kernel.org)
Memory zones and a Linux driver issue (redhat.com)
virtual.dvi (uc.edu)

Zones 介绍

图片.png
32位 x86架构
x86 zones Size Page
ZONE_DMA First 16MiB of memory
ZONE_NORMAL 16MiB - 896MiB
ZONE_HIGHMEM 896 MiB - End
64位 x86_64架构
x86_64 zones Size Page
ZONE_DMA first 16MB/24bit for I/O
内存开始的16M
ZONE_DMA32 4GB I/O
16MB~4GB
ZONE_NORMAL all further memory
4GB到结束

这样划分内存有助于内存管理。
DMA zone 用于传输,比如网卡,24位地址,16MB。一些设备/驱动只能使用DMA zone的内存。
DMA32 zone 用于与卡交换数据,32位地址,4GB。
Normal zone 用于进程。
启动时,dmesg可以展示这3个zone的物理地址分配。

Zones优先选择 Normal > DMA32 > DMA。如果Normal有free memory,kernel则从Normal zone取内存,如果没有则从DMA32取内存。

dmesg查看Zones

范例1G内存

[sysadmin@VM_200_2_centos ~]$ free
             total        used        free      shared  buff/cache   available
Mem:        1016288      197452       97624         924      721212      642568
Swap:             0           0           0
  1. dmesg -kT 查看
[Mon Jun  8 20:57:35 2020] Zone ranges:
[Mon Jun  8 20:57:35 2020]   DMA      [mem 0x00001000-0x00ffffff]
[Mon Jun  8 20:57:35 2020]   DMA32    [mem 0x01000000-0xffffffff]
[Mon Jun  8 20:57:35 2020]   Normal   empty
[Mon Jun  8 20:57:35 2020] Movable zone start for each node
[Mon Jun  8 20:57:35 2020] Early memory node ranges
[Mon Jun  8 20:57:35 2020]   node   0: [mem 0x00001000-0x0009efff]
[Mon Jun  8 20:57:35 2020]   node   0: [mem 0x00100000-0x3fffdfff]
[Mon Jun  8 20:57:35 2020] Initmem setup node 0 [mem 0x00001000-0x3fffdfff]
[Mon Jun  8 20:57:35 2020] On node 0 totalpages: 262044
[Mon Jun  8 20:57:35 2020]   DMA zone: 64 pages used for memmap
[Mon Jun  8 20:57:35 2020]   DMA zone: 21 pages reserved
[Mon Jun  8 20:57:35 2020]   DMA zone: 3998 pages, LIFO batch:0
[Mon Jun  8 20:57:35 2020]   DMA32 zone: 4032 pages used for memmap
[Mon Jun  8 20:57:35 2020]   DMA32 zone: 258046 pages, LIFO batch:31

DMA [mem 0x00001000-0x00ffffff]
0x00000000 - 0x00001000 之间存在1页(4096字节)。去哪了?
加上该页才能凑齐4096页=16MB

1G内存 = 262,144页 = 0x3fffffff
0x3fffdfff = 262,142
0x3fffdfff - 0x3fffffff 之间的2页去哪了?
0xffffffff = 4g

(0x3fffdfff - 0x00001000) = (1073733631 - 4096) = 1,073,729,535
(1073729535+1)/4096 = 262141 页 , 与totalpages: 262044相比多100页,这100页去哪了?

1143 [Fri May  7 18:52:02 2021] Mem-Info:
1144 [Fri May  7 18:52:02 2021] active_anon:207723 inactive_anon:72 isolated_anon:0
1145  active_file:2645 inactive_file:6379 isolated_file:0
1146  unevictable:0 dirty:0 writeback:0 unstable:0
1147  slab_reclaimable:6455 slab_unreclaimable:3695
1148  mapped:1656 shmem:210 pagetables:3268 bounce:0
1149  free:12216 free_pcp:30 free_cma:0
1150 [Fri May  7 18:52:02 2021] Node 0 DMA free:4600kB min:704kB low:880kB high:1056kB active_anon:9560kB inactive_anon:4kB active_file:16kB inactive_file:520kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:15992kB managed:15908kB mlock     ed:0kB dirty:0kB writeback:0kB mapped:32kB shmem:4kB slab_reclaimable:384kB slab_unreclaimable:144kB kernel_stack:80kB pagetables:160kB unstable:0kB bounce:0kB free_pcp:0kB local_pcp:0kB free_cma:0kB writeback_tmp:0kB pages_scanned:1152 all_unrecl     aimable? yes
1151 [Fri May  7 18:52:02 2021] lowmem_reserve[]: 0 975 975 975
1152 [Fri May  7 18:52:02 2021] Node 0 DMA32 free:44264kB min:44348kB low:55432kB high:66520kB active_anon:821332kB inactive_anon:284kB active_file:10564kB inactive_file:24996kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:1032184kB ma     naged:1000380kB mlocked:0kB dirty:0kB writeback:0kB mapped:6592kB shmem:836kB slab_reclaimable:25436kB slab_unreclaimable:14636kB kernel_stack:2880kB pagetables:12912kB unstable:0kB bounce:0kB free_pcp:120kB local_pcp:120kB free_cma:0kB writeback_     tmp:0kB pages_scanned:0 all_unreclaimable? no
1153 [Fri May  7 18:52:02 2021] lowmem_reserve[]: 0 0 0 0
1154 [Fri May  7 18:52:02 2021] Node 0 DMA: 12*4kB (UE) 21*8kB (UE) 14*16kB (UEM) 10*32kB (UE) 6*64kB (UEM) 3*128kB (U) 2*256kB (UE) 1*512kB (M) 2*1024kB (EM) 0*2048kB 0*4096kB = 4600kB
1155 [Fri May  7 18:52:02 2021] Node 0 DMA32: 332*4kB (UEM) 465*8kB (UE) 503*16kB (UEM) 303*32kB (UEM) 194*64kB (UEM) 37*128kB (UE) 11*256kB (UEM) 3*512kB (M) 0*1024kB 0*2048kB 0*4096kB = 44296kB
1156 [Fri May  7 18:52:02 2021] Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
1157 [Fri May  7 18:52:02 2021] 9248 total pagecache pages
1158 [Fri May  7 18:52:02 2021] 0 pages in swap cache
1159 [Fri May  7 18:52:02 2021] Swap cache stats: add 0, delete 0, find 0/0
1160 [Fri May  7 18:52:02 2021] Free swap  = 0kB
1161 [Fri May  7 18:52:02 2021] Total swap = 0kB
1162 [Fri May  7 18:52:02 2021] 262044 pages RAM
1163 [Fri May  7 18:52:02 2021] 0 pages HighMem/MovableOnly
1164 [Fri May  7 18:52:02 2021] 7972 pages reserved

  1. /proc/buddyinfo 查看明细
[sysadmin@VM_200_2_centos ~]$ cat /proc/buddyinfo
Node 0, zone      DMA      7     13     20      9      9      3      4      2      1      0      0 
Node 0, zone    DMA32   1387   2527    946    434    239     49     23      0      0      0      0 

最左侧列显示,DMA有 7 个4k free page,DMA32有 1387 个4k free page。
没有NORMAL(1G内存嘛~)。

图片.png

Buddy memory allocation - Wikipedia
/proc/buddyinfo (archive.org)

  1. /proc/pagetypeinfo 查看更详细信息
    2G内存
[root@localhost ~]# cat /proc/pagetypeinfo 
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order       0      1      2      3      4      5      6      7      8      9     10 
Node    0, zone      DMA, type    Unmovable      1      3      2      3      1      0      1      1      0      0      0 
Node    0, zone      DMA, type  Reclaimable      0      5      5      2      2      0      0      1      1      1      0 
Node    0, zone      DMA, type      Movable      7      2      2      0      1      0      1      0      1      1      0 
Node    0, zone      DMA, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type    Unmovable      0     18    114    102     75     35      8      0      0      0      0 
Node    0, zone    DMA32, type  Reclaimable      1      1      0     34    168      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Movable      1      0     21    217     39     12      4      1      3      4      1 
Node    0, zone    DMA32, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 

Number of blocks type     Unmovable  Reclaimable      Movable      Reserve          CMA      Isolate 
Node 0, zone      DMA            1            2            5            0            0            0 
Node 0, zone    DMA32           57           23          936            0            0            0 

4G内存

[sysadmin@prod-proxy ~]$ cat /proc/pagetypeinfo 
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order       0      1      2      3      4      5      6      7      8      9     10 
Node    0, zone      DMA, type    Unmovable      2      0      0      1      1      0      1      0      1      0      0 
Node    0, zone      DMA, type  Reclaimable      2      2      1      0      1      1      1      1      1      1      0 
Node    0, zone      DMA, type      Movable      0      3      0      1      1      1      0      1      1      0      2 
Node    0, zone      DMA, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type    Unmovable      0    235    389    197     75     25      3      1      1      0      0 
Node    0, zone    DMA32, type  Reclaimable     32    320    325    227     88     32     11      0      1      1      0 
Node    0, zone    DMA32, type      Movable      0   1489   2651   1228    656    162     11      9      1      4      2 
Node    0, zone    DMA32, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type    Unmovable     57    129    126     73     36     10      1      1      0      0      0 
Node    0, zone   Normal, type  Reclaimable     45    123     87     53     13      7      3      0      0      0      0 
Node    0, zone   Normal, type      Movable      0    551   1176    524    127     28      3      5      1      1      0 
Node    0, zone   Normal, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 

Number of blocks type     Unmovable  Reclaimable      Movable      Reserve          CMA      Isolate 
Node 0, zone      DMA            1            2            5            0            0            0 
Node 0, zone    DMA32          973           22          533            0            0            0 
Node 0, zone   Normal          329            8          175            0            0            0 

16G内存

[sysadmin@VM_1_13_centos ~]$ cat /proc/pagetypeinfo 
Page block order: 9
Pages per block:  512

Free pages count per migrate type at order       0      1      2      3      4      5      6      7      8      9     10 
Node    0, zone      DMA, type    Unmovable      1      0      0      0      2      1      1      0      1      0      0 
Node    0, zone      DMA, type  Reclaimable      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type      Movable      0      0      0      0      0      0      0      0      0      1      3 
Node    0, zone      DMA, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone      DMA, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type    Unmovable     22      5      1      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type  Reclaimable  18301  16290  18959     18      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Movable      2      3      4      7      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone    DMA32, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type    Unmovable   9190   6730      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type  Reclaimable 126786 124911      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type      Movable      1      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type      Reserve      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type          CMA      0      0      0      0      0      0      0      0      0      0      0 
Node    0, zone   Normal, type      Isolate      0      0      0      0      0      0      0      0      0      0      0 

Number of blocks type     Unmovable  Reclaimable      Movable      Reserve          CMA      Isolate 
Node 0, zone      DMA            1            0            7            0            0            0 
Node 0, zone    DMA32            4         1652          128            0            0            0 
Node 0, zone   Normal          359         5625          416            0            0            0 

根据buddyinfo计算free内存 (不确定哦~)

[sysadmin@VM_200_2_centos ~]$ free
              total        used        free      shared  buff/cache   available
Mem:        1016288      149756       84916         828      781616      685284
Swap:             0           0           0


[sysadmin@VM_200_2_centos ~]$ cat /proc/buddyinfo 
Node 0, zone      DMA     37     16     33      7      7      5      3      2      1      0      0 
Node 0, zone    DMA32   1021   2132    830    454    269     77     15      0      0      0      0 

u
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,254评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,875评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,682评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,896评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,015评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,152评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,208评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,962评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,388评论 1 304
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,700评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,867评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,551评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,186评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,901评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,142评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,689评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,757评论 2 351

推荐阅读更多精彩内容