Kernel Exploring
  • 前言
  • 支持
  • 老司机带你探索内核编译系统
    • 编译出你的第一个内核
    • 内核编译中的小目标
    • 可能是kbuild中最直接的小目标 – help
    • 使用了一个kbuild函数的目标 – cscope
    • 内核中单个.o文件的编译过程
    • 根目录vmlinux的编译过程
    • 启动镜像bzImage的前世今生
    • setup.bin的诞生记
    • 真假vmlinux–由vmlinux.bin揭开的秘密
    • bzImage的全貌
    • kbuild系统浅析
  • 启动时的小秘密
    • INIT_CALLS的秘密
    • 内核参数
  • 内核加载全流程
    • bootloader如何加载bzImage
    • 内核压缩与解压
    • 内核加载的几个阶段
    • 保护模式内核代码赏析
  • 内存管理
    • 内核页表成长记
      • 未解压时的内核页表
      • 内核早期的页表
      • cleanup_highmap之后的页表
      • 映射完整物理地址
      • 启用init_level4_pgt
    • 自底而上话内存
      • e820从硬件获取内存分布
      • 原始内存分配器--memblock
      • 页分配器
        • 寻找页结构体的位置
        • 眼花的页结构体
        • Node-Zone-Page
        • 传说的伙伴系统
        • Compound Page
        • GFP的功效
        • 页分配器的用户们
      • slub分配器
        • slub的理念
        • 图解slub
      • 内存管理的不同粒度
      • 挑战和进化
        • 扩展性的设计和实现
        • 减少竞争 per_cpu_pageset
        • 海量内存
        • 延迟初始化
        • 内存热插拔
        • 连续内存分配器
    • 虚拟内存空间
      • 页表和缺页中断
      • 虚拟地址空间的管家--vma
      • 匿名反向映射的前世今生
      • 图解匿名反向映射
      • THP和mapcount之间的恩恩怨怨
      • 透明大页的玄机
      • NUMA策略
      • numa balance
      • 老版vma
    • 内存的回收再利用
      • 水线
      • Big Picture
      • 手动触发回收
      • Page Fram Reclaim Algorithm
      • swapfile原理使用和演进
    • 内存隔离
      • memcg初始化
      • 限制memcg大小
      • 对memcg记账
    • 通用
      • 常用全局变量
      • 常用转换
    • 测试
      • 功能测试
      • 性能测试
  • 中断和异常
    • 从IDT开始
    • 中断?异常?有什么区别
    • 系统调用的实现
    • 异常向量表的设置
    • 中断向量和中断函数
    • APIC
    • 时钟中断
    • 软中断
    • 中断、软中断、抢占和多处理器
  • 设备模型
    • 总线
    • 驱动
    • 设备
    • 绑定
  • nvdimm初探
    • 使用手册
    • 上帝视角
    • nvdimm_bus
    • nvdimm
    • nd_region
    • nd_namespace_X
    • nd_dax
      • dev_dax
  • KVM
    • 内存虚拟化
      • Qemu内存模型
      • KVM内存管理
  • cgroup
    • 使用cgroup控制进程cpu和内存
    • cgroup文件系统
    • cgroup层次结构
    • cgroup和进程的关联
    • cgroup数据统计
  • 同步机制
    • 内存屏障
    • RCU
  • Trace/Profie/Debug
    • ftrace的使用
    • 探秘ftrace
    • 内核热补丁的黑科技
    • eBPF初探
    • TraceEvent
    • Drgn
  • 内核中的数据结构
    • 双链表
    • 优先级队列
    • 哈希表
    • xarray
    • B树
    • Maple Tree
    • Interval Tree
  • Tools
  • Good To Read
    • 内核自带文档
    • 内存相关
    • 下载社区邮件
Powered by GitBook
On this page

Was this helpful?

  1. 内存管理
  2. 自底而上话内存
  3. 挑战和进化

减少竞争 per_cpu_pageset

在伙伴系统的研究中,我们看到page是挂在对应的zone下面的,这个数量是固定的。如果我们每次直接从伙伴系统中获取页,那会导致一个问题。

随着cpu个数的增加,对伙伴系统的竞争也会越来越大

linux内核中为了解决这个问题,引入了per_cpu_pageset。

其实理念很简单,就是先从zone这个大仓库里拿一些页出来,放到每个cpu自己的小仓库。用好了,也先放回到这里,等满了再一起还给zone。

那接下来先看看这个结构在zone中是什么样子的。

      struct zone
      +------------------------------------------------------------------------------------------------+
      |pageset                                                                                         |
      |   (struct per_cpu_pageset *)                                                                   |
      |   cpu0                          cpu1                                cpuN                       |
      |   +--------------------------+  +--------------------------+  ...   +--------------------------+
      |   |pcp                       |  |pcp                       |        |pcp                       |
      |   |  (struct per_cpu_pages)  |  |  (struct per_cpu_pages)  |        |  (struct per_cpu_pages)  |
      |   |  +-----------------------+  |  +-----------------------+        |  +-----------------------+
      |   |  |count                  |  |  |count                  |        |  |count                  |
      |   |  |high                   |  |  |high                   |        |  |high                   |
      |   |  |batch                  |  |  |batch                  |        |  |batch                  |
      |   |  |                       |  |  |                       |        |  |                       |
      |   |  |lists[MIGRATE_PCPTYPES]|  |  |lists[MIGRATE_PCPTYPES]|        |  |lists[MIGRATE_PCPTYPES]|
      +---+--+-----------------------+--+--+-----------------------+--------+--+-----------------------+

可以看到,对于任意一个zone都记录了每个cpu上那个小仓库的信息。具体到每个成员的含义为:

  • count: 当前小仓库中有多少页

  • high: 如果小仓库里有超过high个数量的页,则还页面到zone

  • batch: 如果小仓库没有页面了,则一次从zone中找batch个页面来

这些值还能通过/proc/percpu_pagelist_fraction来动态调节。

那系统中是否能够观察到这些数值呢?也是有办法的。通过/proc/zoneinfo就可以。不过这个文件有点大,大家要仔细去看pageset的字段。

我在这里给大家整理出来一个4个node,8个cpu系统上的zoneinfo中pageset的信息。这样或许有个直观的理解。

Node 0, zone      DMA                Node 0, zone    DMA32                  Node 0, zone   Normal                 Node 0, zone  Movable
  pagesets                             pagesets
    cpu: 0                               cpu: 0
              count:  0                            count:  299
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 1                               cpu: 1
              count:  0                            count:  86
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 2                               cpu: 2
              count:  0                            count:  298
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 3                               cpu: 3
              count:  0                            count:  0
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 4                               cpu: 4
              count:  0                            count:  0
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 5                               cpu: 5
              count:  0                            count:  33
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 6                               cpu: 6
              count:  0                            count:  7
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40
    cpu: 7                               cpu: 7
              count:  0                            count:  0
              high:   0                            high:   378
              batch:  1                            batch:  63
              vm stats threshold: 8                vm stats threshold: 40


Node 1, zone      DMA                Node 1, zone    DMA32                  Node 1, zone   Normal                 Node 1, zone  Movable
                                       pagesets                               pagesets
                                         cpu: 0                                 cpu: 0
                                                   count:  0                              count:  16
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 1                                 cpu: 1
                                                   count:  0                              count:  6
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 2                                 cpu: 2
                                                   count:  59                             count:  259
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 3                                 cpu: 3
                                                   count:  61                             count:  329
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 4                                 cpu: 4
                                                   count:  0                              count:  5
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 5                                 cpu: 5
                                                   count:  0                              count:  125
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 6                                 cpu: 6
                                                   count:  0                              count:  110
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32
                                         cpu: 7                                 cpu: 7
                                                   count:  0                              count:  272
                                                   high:   378                            high:   378
                                                   batch:  63                             batch:  63
                                                   vm stats threshold: 32                 vm stats threshold: 32


Node 2, zone      DMA                Node 2, zone    DMA32                  Node 2, zone   Normal                 Node 2, zone  Movable
                                                                              pagesets
                                                                                cpu: 0
                                                                                          count:  165
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 1
                                                                                          count:  175
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 2
                                                                                          count:  291
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 3
                                                                                          count:  137
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 4
                                                                                          count:  122
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 5
                                                                                          count:  362
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 6
                                                                                          count:  322
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 7
                                                                                          count:  198
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40





Node 3, zone      DMA                Node 3, zone    DMA32                  Node 3, zone   Normal                 Node 3, zone  Movable
                                                                              pagesets
                                                                                cpu: 0
                                                                                          count:  206
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 1
                                                                                          count:  354
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 2
                                                                                          count:  271
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 3
                                                                                          count:  0
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 4
                                                                                          count:  139
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 5
                                                                                          count:  325
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 6
                                                                                          count:  135
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
                                                                                cpu: 7
                                                                                          count:  332
                                                                                          high:   378
                                                                                          batch:  63
                                                                                          vm stats threshold: 40
Previous扩展性的设计和实现Next海量内存

Last updated 11 months ago

Was this helpful?