3.3. 使用 tuna 工具调整 CPU

tuna 工具命令可以针对单个 CPU 为目标。

使用 tuna 工具,您可以:

隔离 CPU
在指定 CPU 上运行的所有任务都移至下一个可用 CPU。隔离 CPU 会将其从所有线程的关联性掩码中删除使其不可用。
包括 CPU
允许任务在指定的 CPU 上运行
恢复 CPU
将指定的 CPU 恢复到之前的配置。

这个步骤描述了如何使用 tuna CLI 调整 CPU。

先决条件

流程

  • 指定要受某一命令影响的 CPU 列表:

    # tuna --cpus=cpu_list [command]

    cpu_list 参数是一个用逗号分开的 CPU 号列表。例如,--cpus=0,2。CPU 列表也可以以范围的形式指定,例如 --cpus="1-3",这代表选择 CPU 1、2 和 3。

    要将特定的 CPU 添加到当前的 cpu_list 中,例如,使用 --cpus=+0

    将 [command] 替换为例如 --isolate

  • 隔离 CPU:

    # tuna --cpus=cpu_list --isolate
  • 包括一个 CPU:

    # tuna --cpus=cpu_list --include
  • 以下显示了,在带有四个或更多处理器的系统中,如何在 CPU 01 上运行所有 ssh 线程,在 CPU 23 中运行所有 http 线程:

    # tuna --cpus=0,1 --threads=ssh\* \
    --move --cpus=2,3 --threads=http\* --move

    这个命令会按顺序执行以下操作:

    1. 选择 CPU 01
    2. 选择以 ssh 开头的所有线程。
    3. 将所选线程移到所选 CPU。tuna 设置线程的关联掩码,从 ssh 开始到适当的 CPU。CPU 可以数字形式表示为 01,掩码为 0x3(十六进制),或 11(二进制)。
    4. 将 CPU 列表重置为 23
    5. 选择所有以 http 开头的线程。
    6. 将所选线程移到指定的 CPU。tuna 将以 http 开始的线程的关联掩码设置为指定的 CPU。CPU 可以数字化为 23,掩码为 0xC(十六进制),或 1100(二进制)。

验证步骤

  • 显示当前配置并验证更改是否已如预期执行:

    # tuna --threads=gnome-sc\* --show_threads \
    --cpus=0 --move --show_threads --cpus=1 \
    --move --show_threads --cpus=+0 --move --show_threads
    
                           thread       ctxt_switches
         pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
       3861   OTHER     0      0,1     33997           58 gnome-screensav
                           thread       ctxt_switches
         pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
       3861   OTHER     0        0     33997           58 gnome-screensav
                           thread       ctxt_switches
         pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
       3861   OTHER     0        1     33997           58 gnome-screensav
                           thread       ctxt_switches
         pid SCHED_ rtpri affinity voluntary nonvoluntary             cmd
       3861   OTHER     0      0,1     33997           58 gnome-screensav

    这个命令会按顺序执行以下操作:

    1. 选择以 gnome-sc 线程开头的所有线程。
    2. 显示所选线程,以便用户验证其关联性掩码和 RT 优先级。
    3. 选择 CPU 0
    4. gnome-sc 线程移到指定的 CPU CPU 中,CPU 0
    5. 显示移动的结果。
    6. 将 CPU 列表重置为 CPU 1
    7. gnome-sc 线程移到指定的 CPU 1 中。
    8. 显示移动的结果。
    9. 将 CPU 0 添加到 CPU 列表中。
    10. gnome-sc 线程移到指定的 CPU,CPU 01
    11. 显示移动的结果。

其他资源

  • /proc/cpuinfo 文件
  • tuna (8) 手册页