美文网首页
Linux 内核的内存管理 - Zones

Linux 内核的内存管理 - Zones

作者: 偷油考拉 | 来源:发表于2021-08-25 23:46 被阅读0次

    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

    相关文章

      网友评论

          本文标题:Linux 内核的内存管理 - Zones

          本文链接:https://www.haomeiwen.com/subject/fxpfiltx.html