【原创】(五)Linux进程调度-CFS调度器

时间:2020-03-15 00:46:55   收藏:0   阅读:126

背景

说明:

  1. Kernel版本:4.14
  2. ARM64处理器,Contex-A53,双核
  3. 使用工具:Source Insight 3.5, Visio

1. 概述

老规矩,先上张图片来直观了解一下原理:

技术分享图片

在开始本文之前,建议先阅读下(一)Linux进程调度器-基础

开始探索之旅!

2. 数据结构

2.1 调度类

Linux内核抽象了一个调度类struct sched_class,这是一种典型的面向对象的设计思想,将共性的特征抽象出来封装成类,在实例化各个调度器的时候,可以根据具体的调度算法来实现。这种方式做到了高内聚低耦合,同时又很容易扩展新的调度器。

技术分享图片

2.2 rq/cfs_rq/task_struct/task_group/sched_entity

来一张图看看它们之间的组织关系:

技术分享图片

struct sched_entity {
    /* For load-balancing: */
    struct load_weight      load;                   //调度实体的负载权重值
    struct rb_node          run_node;             //用于连接到CFS运行队列的红黑树中的节点
    struct list_head        group_node;          //用于连接到CFS运行队列的cfs_tasks链表中的节点
    unsigned int            on_rq;              //用于表示是否在运行队列中

    u64             exec_start;             //当前调度实体的开始执行时间
    u64             sum_exec_runtime;   //调度实体执行的总时间
    u64             vruntime;           //虚拟运行时间,这个时间用于在CFS运行队列中排队
    u64             prev_sum_exec_runtime;  //上一个调度实体运行的总时间

    u64             nr_migrations;      //负载均衡

    struct sched_statistics     statistics;     //统计信息

#ifdef CONFIG_FAIR_GROUP_SCHED
    int             depth;      //任务组的深度,其中根任务组的深度为0,逐级往下增加
    struct sched_entity     *parent;        //指向调度实体的父对象
    /* rq on which this entity is (to be) queued: */
    struct cfs_rq           *cfs_rq;        //指向调度实体归属的CFS队列,也就是需要入列的CFS队列
    /* rq "owned" by this entity/group: */
    struct cfs_rq           *my_q;      //指向归属于当前调度实体的CFS队列,用于包含子任务或子的任务组
#endif

#ifdef CONFIG_SMP
    /*
     * Per entity load average tracking.
     *
     * Put into separate cache line so it does not
     * collide with read-mostly values above.
     */
    struct sched_avg        avg ____cacheline_aligned_in_smp;   //用于调度实体的负载计算(`PELT`)
#endif
};
/* CFS-related fields in a runqueue */
struct cfs_rq {
    struct load_weight load;        //CFS运行队列的负载权重值
    unsigned int nr_running, h_nr_running;  //nr_running:运行的调度实体数(参与时间片计算)

    u64 exec_clock;     //运行时间
    u64 min_vruntime;   //最少的虚拟运行时间,调度实体入队出队时需要进行增减处理
#ifndef CONFIG_64BIT
    u64 min_vruntime_copy;
#endif

    struct rb_root_cached tasks_timeline;   //红黑树,用于存放调度实体

    /*
     * 'curr' points to currently running entity on this cfs_rq.
     * It is set to NULL otherwise (i.e when none are currently running).
     */
    struct sched_entity *curr, *next, *last, *skip; //分别指向当前运行的调度实体、下一个调度的调度实体、CFS运行队列中排最后的调度实体、跳过运行的调度实体

#ifdef  CONFIG_SCHED_DEBUG
    unsigned int nr_spread_over;
#endif

#ifdef CONFIG_SMP
    /*
     * CFS load tracking
     */
    struct sched_avg avg;       //计算负载相关
    u64 runnable_load_sum;
    unsigned long runnable_load_avg;        //基于PELT的可运行平均负载
#ifdef CONFIG_FAIR_GROUP_SCHED
    unsigned long tg_load_avg_contrib;      //任务组的负载贡献
    unsigned long propagate_avg;
#endif
    atomic_long_t removed_load_avg, removed_util_avg;
#ifndef CONFIG_64BIT
    u64 load_last_update_time_copy;
#endif

#ifdef CONFIG_FAIR_GROUP_SCHED
    /*
     *   h_load = weight * f(tg)
     *
     * Where f(tg) is the recursive weight fraction assigned to
     * this group.
     */
    unsigned long h_load;
    u64 last_h_load_update;
    struct sched_entity *h_load_next;
#endif /* CONFIG_FAIR_GROUP_SCHED */
#endif /* CONFIG_SMP */

#ifdef CONFIG_FAIR_GROUP_SCHED
    struct rq *rq;  /* cpu runqueue to which this cfs_rq is attached */     //指向CFS运行队列所属的CPU RQ运行队列

    /*
     * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
     * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
     * (like users, containers etc.)
     *
     * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
     * list is used during load balance.
     */
    int on_list;
    struct list_head leaf_cfs_rq_list;
    struct task_group *tg;  /* group that "owns" this runqueue */       //CFS运行队列所属的任务组

#ifdef CONFIG_CFS_BANDWIDTH
    int runtime_enabled;    //CFS运行队列中使用CFS带宽控制
    u64 runtime_expires;    //到期的运行时间
    s64 runtime_remaining;      //剩余的运行时间

    u64 throttled_clock, throttled_clock_task;  //限流时间相关
    u64 throttled_clock_task_time;
    int throttled, throttle_count;      //throttled:限流,throttle_count:CFS运行队列限流次数
    struct list_head throttled_list;    //运行队列限流链表节点,用于添加到cfs_bandwidth结构中的cfttle_cfs_rq链表中
#endif /* CONFIG_CFS_BANDWIDTH */
#endif /* CONFIG_FAIR_GROUP_SCHED */
};

3. 流程分析

整个流程分析,围绕着CFS调度类实体:fair_sched_class中的关键函数来展开。

先来看看fair_sched_class都包含了哪些函数:

/*
 * All the scheduling class methods:
 */
const struct sched_class fair_sched_class = {
    .next           = &idle_sched_class,
    .enqueue_task       = enqueue_task_fair,
    .dequeue_task       = dequeue_task_fair,
    .yield_task     = yield_task_fair,
    .yield_to_task      = yield_to_task_fair,

    .check_preempt_curr = check_preempt_wakeup,

    .pick_next_task     = pick_next_task_fair,
    .put_prev_task      = put_prev_task_fair,

#ifdef CONFIG_SMP
    .select_task_rq     = select_task_rq_fair,
    .migrate_task_rq    = migrate_task_rq_fair,

    .rq_online      = rq_online_fair,
    .rq_offline     = rq_offline_fair,

    .task_dead      = task_dead_fair,
    .set_cpus_allowed   = set_cpus_allowed_common,
#endif

    .set_curr_task          = set_curr_task_fair,
    .task_tick      = task_tick_fair,
    .task_fork      = task_fork_fair,

    .prio_changed       = prio_changed_fair,
    .switched_from      = switched_from_fair,
    .switched_to        = switched_to_fair,

    .get_rr_interval    = get_rr_interval_fair,

    .update_curr        = update_curr_fair,

#ifdef CONFIG_FAIR_GROUP_SCHED
    .task_change_group  = task_change_group_fair,
#endif
};

3.1 runtime与vruntime

CFS调度器没有时间片的概念了,而是根据实际的运行时间和虚拟运行时间来对任务进行排序,从而选择调度。
那么,运行时间和虚拟运行时间是怎么计算的呢?看一下流程调用:

技术分享图片

还是来看一个实例吧,以5个Task为例,其中每个Task的nice值不一样(优先级不同),对应到的权重值在内核中提供了一个转换数组:

const int sched_prio_to_weight[40] = {
 /* -20 */     88761,     71755,     56483,     46273,     36291,
 /* -15 */     29154,     23254,     18705,     14949,     11916,
 /* -10 */      9548,      7620,      6100,      4904,      3906,
 /*  -5 */      3121,      2501,      1991,      1586,      1277,
 /*   0 */      1024,       820,       655,       526,       423,
 /*   5 */       335,       272,       215,       172,       137,
 /*  10 */       110,        87,        70,        56,        45,
 /*  15 */        36,        29,        23,        18,        15,
};

图来了:

技术分享图片

3.2 CFS调度tick

CFS调度器中的tick函数为task_tick_fair,系统中每个调度tick都会调用到,此外如果使用了hrtimer,也会调用到这个函数。
流程如下:

技术分享图片

主要的工作包括:

来一张图,感受一下update_curr函数的相关信息更新吧:

技术分享图片

3.3 任务出队入队

技术分享图片

3.3 任务创建

在父进程通过fork创建子进程的时候,task_fork_fair函数会被调用,这个函数的传入参数是子进程的task_struct。该函数的主要作用,就是确定子任务的vruntime,因此也能确定子任务的调度实体在红黑树RB中的位置。

task_fork_fair本身比较简单,流程如下图:

技术分享图片

3.4 任务选择

每当进程任务切换的时候,也就是schedule函数执行时,调度器都需要选择下一个将要执行的任务。
在CFS调度器中,是通过pick_next_task_fair函数完成的,流程如下:

技术分享图片

暂且分析到这吧,CFS调度器涵盖的内容还是挺多的,fair.c一个文件就有将近一万行代码,相关内容的分析也分散在前边的文章中了,感兴趣的可以去看看。

打完收工,洗洗睡了。

技术分享图片

原文:https://www.cnblogs.com/LoyenWang/p/12495319.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!