Linux - 迁移和交换机之间的区别?

hor*_*rro 8 linux multithreading scheduler linux-kernel

通过查看调度统计信息/proc/<PID>/sched,您可以获得如下输出:

[horro@system ~]$ cat /proc/1/sched
systemd (1, #threads: 1)
-------------------------------------------------------------------
se.exec_start                                :    2499611106.982616
se.vruntime                                  :          7952.917943
se.sum_exec_runtime                          :         58651.279127
se.nr_migrations                             :                53355
nr_switches                                  :               169561
nr_voluntary_switches                        :               168185
nr_involuntary_switches                      :                 1376
se.load.weight                               :              1048576
se.avg.load_sum                              :               343837
se.avg.util_sum                              :               338827
se.avg.load_avg                              :                    7
se.avg.util_avg                              :                    7
se.avg.last_update_time                      :     2499611106982616
policy                                       :                    0
prio                                         :                  120
clock-delta                                  :                  180
mm->numa_scan_seq                            :                    1
numa_pages_migrated                          :                  296
numa_preferred_nid                           :                    0
total_numa_faults                            :                   34
current_node=0, numa_group_id=0
numa_faults node=0 task_private=0 task_shared=23 group_private=0 group_shared=0
numa_faults node=1 task_private=0 task_shared=0 group_private=0 group_shared=0
numa_faults node=2 task_private=0 task_shared=0 group_private=0 group_shared=0
numa_faults node=3 task_private=0 task_shared=11 group_private=0 group_shared=0
numa_faults node=4 task_private=0 task_shared=0 group_private=0 group_shared=0
numa_faults node=5 task_private=0 task_shared=0 group_private=0 group_shared=0
numa_faults node=6 task_private=0 task_shared=0 group_private=0 group_shared=0
numa_faults node=7 task_private=0 task_shared=0 group_private=0 group_shared=0
Run Code Online (Sandbox Code Playgroud)

我一直试图找出迁移和交换机之间的区别,这里这里有一些响应.总结这些回应:

  • nr_switches:上下文切换次数.
  • nr_voluntary_switches:自愿开关的数量,即线程被阻塞,因此拾取另一个线程.
  • nr_involuntary_switches:调度程序将线程踢出,因为有另一个饥饿的线程准备运行.

因此,有什么migrations?这些概念是否相关?迁移是核心内的核心和交换机之间的?

And*_*kyy 11

迁移是指线程(通常在上下文切换之后)在不同的CPU上进行调度的时间.

编辑1:

以下是Wikipedia关于迁移的更多信息:https: //en.wikipedia.org/wiki/Process_migration

以下是增加计数器的内核代码:https: //github.com/torvalds/linux/blob/master/kernel/sched/core.c#L1175

if (task_cpu(p) != new_cpu) {
    ...
    p->se.nr_migrations++;
Run Code Online (Sandbox Code Playgroud)

编辑2:

在以下情况下,线程可以迁移到另一个CPU:

  1. exec()
  2. fork()
  3. 在线程唤醒期间.
  4. 如果线程关联掩码已更改.
  5. 当前CPU脱机时.

欲了解更多信息,请看看功能set_task_cpu(),move_queued_task(),migrate_tasks()在同一个源文件:https://github.com/torvalds/linux/blob/master/kernel/sched/core.c

下面描述了策略调度程序select_task_rq(),它取决于您使用的调度程序类.政策的基本版本:

if (p->nr_cpus_allowed > 1)
    cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
else
    cpu = cpumask_any(&p->cpus_allowed);
Run Code Online (Sandbox Code Playgroud)

资料来源:https://github.com/torvalds/linux/blob/master/kernel/sched/core.c#L1534

因此,为了避免迁移,请使用sched_setaffinity(2)系统调用或相应的POSIX API 为线程设置CPU关联掩码pthread_setaffinity_np(3).

以下是完全公平调度程序的select_task_rq()的定义:https: //github.com/torvalds/linux/blob/master/kernel/sched/fair.c#L5860

逻辑非常复杂,但基本上,我们要么选择兄弟空闲CPU,要么找到最不忙的新CPU.

希望这能回答你的问题.

  • @horro,已经尝试过对线程核心亲和力的手动控制,我得出结论,没有必要,这些政策涉及太深入.必须考虑到这一点,并且每台计算机都有所不同.例如,在双CPU机器上,您可能会考虑哪个CPU具有大部分线程数据(QPI总线没有任何重载点).这本身就是一个复杂的问题; 连续的内存页面可以在CPU之间交错,也可以不交错(这是某些机器上的BIOS设置).操作系统知道所有这些以及更多.我害怕撬得太深...... (2认同)
  • @ St.Antario用几句话说:迁移导致更多缓存未命中(现代CPU至少有指令,数据和TLB缓存),更多分支预测器未命中等等.很确定还有别的东西,但这就是我想到的在这一刻 ;) (2认同)