• Linux调度域与调度组


    引入调度域的讨论可以参考这篇文章。这篇笔记重点分析了内核调度域相关的数据结构以及内核用于构建调度域的代码实现,以此来加深对调度域的理解。调度域是调度器进行负载均衡的基础。

    调度域拓扑层级

    整个系统的调度域组成一个层级结构,内核设计了struct sched_domain_topology_level来描述一层调度域拓扑。

    1. typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
    2. typedef int (*sched_domain_flags_f)(void);
    3. struct sched_domain_topology_level {
    4. sched_domain_mask_f mask;
    5. sched_domain_flags_f sd_flags;
    6. int flags;
    7. int numa_level;
    8. struct sd_data data;
    9. #ifdef CONFIG_SCHED_DEBUG
    10. char *name;
    11. #endif
    12. };
    • mask:该回调用于指定该层级的调度域的CPU掩码。
    • sd_flags:该回调用于获取该层级的调度域标记。
    • data:该层级的调度域对象,见下面单独分析。
    • name:层级名字,如MC、DIE,这些名字会在用户态的/proc/sys/kernel/sched_domain目录中体现。

    系统的调度域拓扑用sched_domain_topology_level[]数组表示,保存在全局变量sched_domain_topology中,数组的每一个元素代表一个层级。default_topology是系统定义的默认调度域拓扑层级数组。各体系结构可以定义自己的调度域拓扑层级数组,然后通过set_sched_topology()函数替换该默认值。

    1. // 不考虑超线程,默认的由MC(多核)和DIE(socket)两个层级组成,手机产品基本上只有这两个层级
    2. static struct sched_domain_topology_level default_topology[] = {
    3. #ifdef CONFIG_SCHED_SMT
    4. { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, // 支持超线程时开启
    5. #endif
    6. #ifdef CONFIG_SCHED_MC
    7. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
    8. #endif
    9. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
    10. { NULL, },
    11. };
    12. struct sched_domain_topology_level *sched_domain_topology = default_topology;
    13. // MC层级包含了属于同一个cluster的所有CPU
    14. const struct cpumask *cpu_coregroup_mask(int cpu)
    15. {
    16. return &cpu_topology[cpu].core_sibling;
    17. }
    18. // DIE层级包含了系统所有的CPU(不考虑NUMA)
    19. static inline const struct cpumask *cpu_cpu_mask(int cpu)
    20. {
    21. return cpumask_of_node(cpu_to_node(cpu));
    22. }

    sd_data

    该结构用于辅助定义调度域拓扑层级,包含了一个拓扑层级所有的调度域对象,这些对象每个CPU一份。由于Per-CPU变量也是动态分配的,所以类型是二级指针。此外,由于整个调度域拓扑层级一旦建立就基本不会再发生变化,每个CPU一份可以提高访问效率。

    1. struct sd_data {
    2. struct sched_domain **__percpu sd;
    3. struct sched_group **__percpu sg;
    4. struct sched_group_capacity **__percpu sgc;
    5. };

    构建系统调度域拓扑结构时,会使用__sdt_alloc()为sd_data分配内存,对应的释放函数为__sdt_free()

    1. static int __sdt_alloc(const struct cpumask *cpu_map)
    2. {
    3. struct sched_domain_topology_level *tl;
    4. int j;
    5. for_each_sd_topology(tl) { // 遍历所有的拓扑层级,为每一个tl->sd_data分配空间
    6. struct sd_data *sdd = &tl->data;
    7. // 分配调度域指针对象,这些指针每个CPU一份
    8. sdd->sd = alloc_percpu(struct sched_domain *);
    9. sdd->sg = alloc_percpu(struct sched_group *);
    10. sdd->sgc = alloc_percpu(struct sched_group_capacity *);
    11. // 为每个CPU分配这三个调度域对象
    12. for_each_cpu(j, cpu_map) {
    13. struct sched_domain *sd;
    14. struct sched_group *sg;
    15. struct sched_group_capacity *sgc;
    16. sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
    17. GFP_KERNEL, cpu_to_node(j));
    18. *per_cpu_ptr(sdd->sd, j) = sd;
    19. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
    20. GFP_KERNEL, cpu_to_node(j));
    21. sg->next = sg;
    22. *per_cpu_ptr(sdd->sg, j) = sg;
    23. sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
    24. GFP_KERNEL, cpu_to_node(j));
    25. *per_cpu_ptr(sdd->sgc, j) = sgc;
    26. }
    27. }
    28. return 0;
    29. }

    调度域对象

    有3个调度域对象:

    1. struct sched_domain:调度域代表可以共享属性和调度参数的一组CPU。每个CPU在每一个调度域拓扑层级中都有一个shced_domain对象。
    2. struct sched_group:调度域可以由一个或多个调度组构成,每个调度组也代表可以共享属性和调度参数的一组CPU,属于同一个调度域的调度组的集合组成了调度域代表的CPU。调度域进行负载均衡的目的就是要保证其内部各个调度组之间的负载均衡。
    3. struct sched_capacity:包含了调度组的captacity内容,和调度组管理的CPU的能力强相关。

    调度域: sched_domain

    1. struct sched_domain {
    2. // 组成调度域拓扑层级
    3. struct sched_domain *parent; /* top domain must be null terminated */
    4. struct sched_domain *child; /* bottom domain must be null terminated */
    5. struct sched_group *groups; /* the balancing groups of the domain */
    6. // 暂时忽略诸多调度参数
    7. ...
    8. int level; // 代表的调度域拓扑等级,MC为0,其它依次加1
    9. #ifdef CONFIG_SCHED_DEBUG
    10. char *name;
    11. #endif
    12. union {
    13. void *private; /* used during construction */
    14. struct rcu_head rcu; /* used during destruction */
    15. };
    16. unsigned int span_weight; // 该调度域包含的CPU个数和CPU掩码
    17. unsigned long span[0];
    18. };
    1. parent、child、groups:组成了调度域拓扑等级。parent指针指向更高层的sched_domain对象,最低层的sched_domain对象(如MC层)的该指针为NULL。child则是和parent相反。groups指向了属于该调度域的所有调度组对象链表,见sched_group部分的介绍。
    2. private:指向所属的sched_domain_topology_level.sd_data。
    3. span_weight、span:包含了该调度域包含的CPU信息,span数组在构建系统调度域拓扑结构时根据sched_domain_topology中定义的系统调度域拓扑等级描述动态分配。

    构建调度域: build_sched_domain()

    sched_domain对象的分配是在__sdt_alloc()函数中完成的,但是其大多数字段的设置以及系统拓扑层级结构中sched_domain之间的父子关系是在build_sched_domain()函数中完成的。

    1. // 构建cpu上tl层级的sched_domain对象(tl->sd_data.sd[cpu]),其包含的CPU为cpu_map,child为其下一级
    2. struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
    3. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
    4. struct sched_domain *child, int cpu)
    5. {
    6. struct sched_domain *sd = sd_init(tl, cpu); // 初始化该sched_domain中的各字段
    7. if (!sd)
    8. return child;
    9. // 设置sched_domain的CPU掩码,即span数组
    10. cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
    11. if (child) {
    12. sd->level = child->level + 1;
    13. sched_domain_level_max = max(sched_domain_level_max, sd->level);
    14. child->parent = sd; // 相邻层级的sched_domain对象之间建立父子关系
    15. sd->child = child;
    16. // 检查确保低层级的sched_domain的CPU掩码必须是高层级的sched_domain的CPU掩码的子集
    17. if (!cpumask_subset(sched_domain_span(child),
    18. sched_domain_span(sd))) {
    19. #ifdef CONFIG_SCHED_DEBUG
    20. pr_err(" the %s domain not a subset of the %s domain\n",
    21. child->name, sd->name);
    22. #endif
    23. cpumask_or(sched_domain_span(sd),
    24. sched_domain_span(sd),
    25. sched_domain_span(child));
    26. }
    27. }
    28. set_domain_attribute(sd, attr);
    29. return sd;
    30. }

    如上可见,sched_domain对象的大部分字段都是在sd_init()函数中设置的,下面我们先忽略其中调度策略参数的设置,这部分内容在负载均衡相关的笔记中再来分析(结合负载均衡上下文更容易理解)。

    1. static struct sched_domain *
    2. sd_init(struct sched_domain_topology_level *tl, int cpu)
    3. {
    4. // 找到要初始化的sched_domain对象
    5. struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
    6. int sd_weight, sd_flags = 0;
    7. *sd = (struct sched_domain) {
    8. ...
    9. #ifdef CONFIG_SCHED_DEBUG
    10. .name = tl->name,
    11. #endif
    12. };
    13. ...
    14. sd->private = &tl->data;
    15. // private指向sched_domain_topology_level.sd_data
    16. return sd;
    17. }

    调度组: sched_group

    调度域进一步可以划分为若干个调度组,这些调度组的CPU的并集就是调度域的CPU集合。

    1. struct sched_group {
    2. struct sched_group *next; /* Must be a circular list */
    3. atomic_t ref;
    4. unsigned int group_weight; // cpumask中CPU的个数
    5. struct sched_group_capacity *sgc;
    6. // 调度组的CPU掩码
    7. unsigned long cpumask[0];
    8. };
    1. next:同一个调度域下的多个调度组用该指针组成一个单向循环链表,链表的表头结点为sched_domain.groups指针。
    2. ref:同一个sched_group对象可能被多个CPU上的sched_domain对象引用,所以需要一个引用计数。
    3. sgc:用来描述该调度组的capacity,见下面介绍。
    4. group_weight、cpumask:该调度组包含了哪些CPU和CPU的个数。

    构建调度组: build_sched_groups()

    __sdt_alloc()函数中完成sched_group对象的分配,每个CPU在每个调度域拓扑层级都分配了一个sched_group对象,但是这些对象并不会全部被使用。在构建系统调度域拓扑时,会调用build_sched_groups()函数为指定CPU的sched_domain对象构建其所有的sched_group对象。

    1. static int build_sched_groups(struct sched_domain *sd, int cpu)
    2. {
    3. struct sched_group *first = NULL, *last = NULL;
    4. struct sd_data *sdd = sd->private;
    5. const struct cpumask *span = sched_domain_span(sd); // span为该调度域的CPU集合
    6. struct cpumask *covered;
    7. int i;
    8. // 为调度域对象sdd->sd_data.sd[cpu]确定调度组,将确定的调度组对象及其sgc指针
    9. // 保存在sdd->sd_data.groups[cpu]中,该函数会修改sd->groups的指向
    10. get_group(cpu, sdd, &sd->groups);
    11. atomic_inc(&sd->groups->ref);
    12. // 后面的逻辑只有在处理调度域对象的第一个CPU时才会执行,
    13. // 对于MC层就是每个cluster的第一个CPU,对于DIE层就是整个系统的第一个CPU
    14. if (cpu != cpumask_first(span))
    15. return 0;
    16. lockdep_assert_held(&sched_domains_mutex);
    17. covered = sched_domains_tmpmask;
    18. cpumask_clear(covered);
    19. for_each_cpu(i, span) {
    20. // 检查调度域内的每个CPU,判断其是否可以作为一个独立的sched_group
    21. struct sched_group *sg;
    22. int group, j;
    23. if (cpumask_test_cpu(i, covered))
    24. continue;
    25. group = get_group(i, sdd, &sg);
    26. cpumask_setall(sched_group_mask(sg)); // 这个逻辑非常奇怪,为什么要设置所有的CPU到调度组?
    27. // 找到那些属于同一个sched_group的CPU,将其设置到调度组对象的CPU掩码中
    28. for_each_cpu(j, span) {
    29. if (get_group(j, sdd, NULL) != group)
    30. continue;
    31. cpumask_set_cpu(j, covered);
    32. cpumask_set_cpu(j, sched_group_cpus(sg));
    33. }
    34. // 将同一个调度域对象下面的若干个调度组对象通过sched_group.next指针组织成单向循环链表
    35. if (!first)
    36. first = sg;
    37. if (last)
    38. last->next = sg;
    39. last = sg;
    40. }
    41. last->next = first;
    42. return 0;
    43. }
    44. // 为调度域对象sdd->sd[cpu]确定调度组,将确定的调度组对象及其sgc指针保存在sg中
    45. static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
    46. {
    47. struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
    48. struct sched_domain *child = sd->child;
    49. if (child)
    50. cpu = cpumask_first(sched_domain_span(child));
    51. if (sg) {
    52. *sg = *per_cpu_ptr(sdd->sg, cpu);
    53. (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
    54. atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
    55. }
    56. return cpu;
    57. }

    get_group()函数非常关键,该函数对第一层拓扑和更高层拓扑的逻辑不同。对于第一层拓扑,其child为NULL,这时传入的cpu不会发生变化,这样调度域对象sdd->sd[cpu]关联的调度组对象就是该CPU的调度组对象。对于更高层拓扑,其child不是NULL,这时调度组对象来自其第一层拓扑的第一个CPU对应的对象。这样的get_group()函数实现的效果见下面"构建系统调度域拓扑"中第二组for循环后的示意图。

    调度组能力: sched_group_capacity

    每个sched_group对象都关联一个sched_group_capacity对象来描述调度组的能力。

    1. struct sched_group_capacity {
    2. atomic_t ref; // 同sched_group中的ref
    3. // 调度组的capacity
    4. unsigned int capacity, capacity_orig;
    5. unsigned long next_update;
    6. ...
    7. unsigned long cpumask[0]; /* iteration mask */
    8. };

    初始化调度组能力: init_sched_groups_capacity()

    在构建系统调度域拓扑过程中,会调用该函数将初始化指定调度域的所有调度组能力。调度组的能力和调度组中CPU的capacity强相关,这里不详细展开,在CPU能力相关笔记中再详细分析。

    1. static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
    2. {
    3. struct sched_group *sg = sd->groups;
    4. // 为该调度域的所有调度组计算其管理的CPU个数
    5. do {
    6. sg->group_weight = cpumask_weight(sched_group_cpus(sg));
    7. sg = sg->next;
    8. } while (sg != sd->groups);
    9. // 只有调度组中的第一个CPU才需要执行计算capacity的过程,因为其他CPU都会共享该sgc
    10. if (cpu != group_balance_cpu(sg))
    11. return;
    12. update_group_capacity(sd, cpu); // 计算capacity
    13. atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
    14. }

    根域: root_domain

    根域定义了一些全局信息,所有CPU共用一个根域对象。初始时系统会定义一个def_root_domain作为系统根域,后续整个调度域拓扑结构建立时会重新建立一个根域对象来替换默认的根域对象。

    1. /*
    2. * By default the system creates a single root-domain with all cpus as
    3. * members (mimicking the global state we have today).
    4. */
    5. struct root_domain def_root_domain;
    6. struct root_domain {
    7. atomic_t refcount;
    8. ...
    9. struct rcu_head rcu;
    10. cpumask_var_t span;
    11. cpumask_var_t online;
    12. };
    13. // 最终系统拓扑层级中的调度域和根域对象会关联到rq中
    14. struct rq {
    15. ...
    16. #ifdef CONFIG_SMP
    17. struct root_domain *rd;
    18. struct sched_domain *sd;
    19. #endif
    20. }

    cpu_attach_domain()

    系统调度域拓扑建立完毕后会调用该函数将每个CPU上的调度域对象(第一级)和根域对象保存到CPU运行队列rq中。

    1. static void
    2. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
    3. {
    4. struct rq *rq = cpu_rq(cpu);
    5. struct sched_domain *tmp;
    6. // 遍历系统调度域拓扑结构,去掉那些对调度没有意义的荣誉层级(如相邻两个层级的调度域管理的CPU相同)
    7. for (tmp = sd; tmp; ) {
    8. struct sched_domain *parent = tmp->parent;
    9. if (!parent)
    10. break;
    11. if (sd_parent_degenerate(tmp, parent)) {
    12. tmp->parent = parent->parent;
    13. if (parent->parent)
    14. parent->parent->child = tmp;
    15. if (parent->flags & SD_PREFER_SIBLING)
    16. tmp->flags |= SD_PREFER_SIBLING;
    17. destroy_sched_domain(parent, cpu);
    18. } else
    19. tmp = tmp->parent;
    20. }
    21. if (sd && sd_degenerate(sd)) {
    22. tmp = sd;
    23. sd = sd->parent;
    24. destroy_sched_domain(tmp, cpu);
    25. if (sd)
    26. sd->child = NULL;
    27. }
    28. // 将sd和rd指针保存到rq->sd和rq->rd中
    29. rq_attach_root(rq, rd);
    30. tmp = rq->sd;
    31. rcu_assign_pointer(rq->sd, sd);
    32. destroy_sched_domains(tmp, cpu);
    33. update_top_cache_domain(cpu);
    34. }

    构建系统调度域拓扑

    在开机过程中,会调用init_sched_domains()函数根据定义的系统调度域拓扑层级结构构建系统调度域拓扑。

    1. // 传入的cpu_map为cpu_active_mask
    2. static int init_sched_domains(const struct cpumask *cpu_map)
    3. {
    4. int err;
    5. arch_update_cpu_topology(); // 构建系统调度域拓扑之前让体系结构更新一次自己的cpu_map
    6. ndoms_cur = 1;
    7. doms_cur = alloc_sched_domains(ndoms_cur);
    8. if (!doms_cur)
    9. doms_cur = &fallback_doms;
    10. cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map); // 去掉隔离的CPU
    11. err = build_sched_domains(doms_cur[0], NULL); // 构建系统调度域拓扑结构
    12. // 系统调度域拓扑构建完毕,根据结果创建/proc/sys/kernel/domain目录
    13. register_sched_domain_sysctl();
    14. return err;
    15. }

    核心的系统调度域拓扑建立由build_sched_domains()函数完成。

    1. // 该临时数据结构用来保存分配的调度域对象
    2. struct s_data {
    3. struct sched_domain ** __percpu sd;
    4. struct root_domain *rd;
    5. };
    6. static int build_sched_domains(const struct cpumask *cpu_map,
    7. struct sched_domain_attr *attr)
    8. {
    9. enum s_alloc alloc_state;
    10. struct sched_domain *sd;
    11. struct s_data d;
    12. int i, ret = -ENOMEM;
    13. // 为调度域拓扑层级数组sched_domain_topology分配调度域对象
    14. alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
    15. if (alloc_state != sa_rootdomain)
    16. goto error;
    17. // 为每个CPU都建立一个sched_domain对象组成的层级结构(通过sched_domain的parent和child字段)
    18. for_each_cpu(i, cpu_map) {
    19. struct sched_domain_topology_level *tl;
    20. sd = NULL;
    21. for_each_sd_topology(tl) { // 拓扑层级从低到高遍历,sd为child
    22. // 每构建一层sched_domain对象都是下一层的child
    23. sd = build_sched_domain(tl, cpu_map, attr, sd, i);
    24. // 保存每个CPU的最低层次sched_domain对象到临时变量d.sd中,后面有用处
    25. if (tl == sched_domain_topology)
    26. *per_cpu_ptr(d.sd, i) = sd;
    27. if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
    28. sd->flags |= SD_OVERLAP;
    29. // 构建的拓扑层级最高层次只需要覆盖所有active_cpu_mask即可,这样可以防止数组sched_domain_topology
    30. // 定义多余的高层次拓扑层级,比如在DIE上面再定义一层,但是DIE层已经可以覆盖系统所有CPU了
    31. if (cpumask_equal(cpu_map, sched_domain_span(sd)))
    32. break;
    33. }
    34. }
    35. // 为每个CPU的各层sched_domain对象构建调度组对象
    36. for_each_cpu(i, cpu_map) {
    37. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
    38. // 由低到高为每一层调度域拓扑层级的sched_domain对象构建调度组对象
    39. sd->span_weight = cpumask_weight(sched_domain_span(sd));
    40. if (sd->flags & SD_OVERLAP) {
    41. // 调度域中各调度组的CPU存在重叠的情况,我们不考虑
    42. if (build_overlap_sched_groups(sd, i))
    43. goto error;
    44. } else {
    45. // 为CPU i上的调度域对象sd构建调度组对象
    46. if (build_sched_groups(sd, i))
    47. goto error;
    48. }
    49. }
    50. }
    51. // 1. 为每个CPU的各层sched_domain对象中的sched_group对象设置capacity。
    52. // 2. 标记系统调度域拓扑中不需要的调度域对象,后续根据标记结果释放。
    53. for (i = nr_cpumask_bits-1; i >= 0; i--) {
    54. if (!cpumask_test_cpu(i, cpu_map))
    55. continue;
    56. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
    57. claim_allocations(i, sd);
    58. init_sched_groups_capacity(i, sd);
    59. }
    60. }
    61. // 将调度域对象和根域对象保存到CPU运行队列中
    62. rcu_read_lock();
    63. for_each_cpu(i, cpu_map) {
    64. sd = *per_cpu_ptr(d.sd, i);
    65. cpu_attach_domain(sd, d.rd, i);
    66. }
    67. rcu_read_unlock();
    68. ret = 0;
    69. error:
    70. __free_domain_allocs(&d, alloc_state, cpu_map);
    71. return ret;
    72. }
    • __visit_domain_allocation_hell()函数只负责调度对象的分配。下图是6小核+2大核的系统在该函数调用之后,系统调度域拓扑层级结构中的调度域对象分配示意图。

    • 第一组for循环中,通过build_sched_domain()函数为每个CPU都建立一个sched_domain对象组成的层级结构(通过sched_domain的parent和child字段)。下图是6小核+2大核的系统在该组for循环之后,系统调度域拓扑层级结构中的调度域对象关系示意图。

    • 第二组for循环中,通过build_sched_groups()函数为每个CPU的各层sched_domain对象构建调度组对象(保存到sched_domain的groups字段中)。下图是6小核+2大核的系统在该组for循环之后,系统调度域拓扑层级结构中的调度域对象关系示意图。

    上图中置灰部分表示这些对象没有关联到拓扑结构中。在MC层,每个CPU为一个调度组,同一个cluster内的调度组组成该层的调度域。在DIE层,每个cluster内的CPU为一个调度组,多个cluster的调度组组成该层的调度域。

    • 第三组for循环中,通过claim_allocations()函数将系统调度域拓扑中不需要的调度域对象标记为NULL(上图中置灰部分调度域对象),这样最后的__free_domain_allocs()函数根据标记结果释放这些对象。通过init_sched_groups_capacity()函数为所有的sched_group对象初始化capacity。
    • 第四组for循环中,通过cpu_attach_domain()函数将调度域对象和根域对象保存到CPU运行队列中。
    • 最后,通过__free_domain_allocs()函数释放那些不需要的调度域对象。下图是6小核+2大核的系统在多余的调度域对象被释放之后的示意图。

    __visit_domain_allocation_hell()

    该函数为系统调度域拓扑层级数组sched_domain_topology分配调度域对象。

    1. static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
    2. const struct cpumask *cpu_map)
    3. {
    4. memset(d, 0, sizeof(*d));
    5. // 为所有的sched_domain_topology_level.sd_data分配对象
    6. if (__sdt_alloc(cpu_map))
    7. return sa_sd_storage;
    8. // 该临时的Per-CPU变量用于的后续流程,用于指向__sdt_alloc()中分配的调度对象
    9. d->sd = alloc_percpu(struct sched_domain *);
    10. if (!d->sd)
    11. return sa_sd_storage;
    12. // 分配并初始化根域对象
    13. d->rd = alloc_rootdomain();
    14. if (!d->rd)
    15. return sa_sd;
    16. return sa_rootdomain;
    17. }

    claim_allocations()

    将调度域sd中使用到的相关调度域对象指针标记为NULL,这样最后的__free_domain_allocs()函数就可以通过判断非NULL指针来将那些未使用到的调度域对象进行回收。需要注意的是这里将tl->sdd.xxx指针设置为NULL并不会导致内存泄漏,因为这些对象的指针在build_sched_domains()函数的临时变量d中还保存了一份,而且最后会通过cpu_attach_domain()函数将sd指针保存到CPU运行队列中。

    1. static void claim_allocations(int cpu, struct sched_domain *sd)
    2. {
    3. struct sd_data *sdd = sd->private;
    4. WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
    5. *per_cpu_ptr(sdd->sd, cpu) = NULL;
    6. if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
    7. *per_cpu_ptr(sdd->sg, cpu) = NULL;
    8. if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
    9. *per_cpu_ptr(sdd->sgc, cpu) = NULL;
    10. }

  • 相关阅读:
    免费IDEA插件分享:Apipost-Helper
    「Verilog学习笔记」数据选择器实现逻辑电路
    反射读取数组导出
    leetcode 11-盛最多水的容器
    宝安水环境管控平台(Ionic/Angular 移动端) 问题记录
    TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
    若依前后端分离项目部署
    小功能⭐️Unity静态方法拓展写法
    js回到顶部或定位到指定元素
    EMAS Serverless系列~4步教你快速搭建小程序
  • 原文地址:https://blog.csdn.net/fanxiaoyu321/article/details/134543434