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

通过查看/ 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

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

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

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

解决方法

迁移是指线程(通常在上下文切换之后)在不同的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++;

编辑2:

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

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

有关更多信息,请查看同一源文件中的函数set_task_cpu(),move_queued_task(),migrate_tasks():https://github.com/torvalds/linux/blob/master/kernel/sched/core.c

select_task_rq()中描述了策略调度程序,它取决于您正在使用的调度程序类. policier的基本版本:

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);

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

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

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

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

希望这能回答你的问题.

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...