From: Waiman Long <longman@redhat.com>
To: Xavier <xavier_qy@163.com>, tj@kernel.org
Cc: mkoutny@suse.com, lizefan.x@bytedance.com, hannes@cmpxchg.org,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
torvalds@linux-foundation.org, akpm@linux-foundation.org
Subject: Re: [PATCH-cpuset v9 2/2] cpuset: use Union-Find to optimize the merging of cpumasks
Date: Tue, 2 Jul 2024 23:05:38 -0400 [thread overview]
Message-ID: <22061c0a-d60d-4a04-9192-3e58f892deab@redhat.com> (raw)
In-Reply-To: <20240702105010.253933-3-xavier_qy@163.com>
On 7/2/24 06:50, Xavier wrote:
> The process of constructing scheduling domains
> involves multiple loops and repeated evaluations, leading to numerous
> redundant and ineffective assessments that impact code efficiency.
>
> Here, we use Union-Find to optimize the merging of cpumasks. By employing
> path compression and union by rank, we effectively reduce the number of
> lookups and merge comparisons.
>
> Signed-off-by: Xavier <xavier_qy@163.com>
> ---
> kernel/cgroup/cpuset.c | 95 ++++++++++++++++--------------------------
> 1 file changed, 36 insertions(+), 59 deletions(-)
>
> diff --git a/kernel/cgroup/cpuset.c b/kernel/cgroup/cpuset.c
> index fe76045aa5..4d32cd1407 100644
> --- a/kernel/cgroup/cpuset.c
> +++ b/kernel/cgroup/cpuset.c
> @@ -45,6 +45,7 @@
> #include <linux/cgroup.h>
> #include <linux/wait.h>
> #include <linux/workqueue.h>
> +#include <linux/union_find.h>
>
> DEFINE_STATIC_KEY_FALSE(cpusets_pre_enable_key);
> DEFINE_STATIC_KEY_FALSE(cpusets_enabled_key);
> @@ -172,9 +173,6 @@ struct cpuset {
> */
> int attach_in_progress;
>
> - /* partition number for rebuild_sched_domains() */
> - int pn;
> -
> /* for custom sched domain */
> int relax_domain_level;
>
> @@ -208,6 +206,9 @@ struct cpuset {
>
> /* Remote partition silbling list anchored at remote_children */
> struct list_head remote_sibling;
> +
> + /* Used to merge intersecting subsets for generate_sched_domains*/
> + struct uf_node node;
> };
>
> /*
> @@ -1007,7 +1008,7 @@ static int generate_sched_domains(cpumask_var_t **domains,
> struct cpuset *cp; /* top-down scan of cpusets */
> struct cpuset **csa; /* array of all cpuset ptrs */
> int csn; /* how many cpuset ptrs in csa so far */
> - int i, j, k; /* indices for partition finding loops */
> + int i, j; /* indices for partition finding loops */
> cpumask_var_t *doms; /* resulting partition; i.e. sched domains */
> struct sched_domain_attr *dattr; /* attributes for custom domains */
> int ndoms = 0; /* number of sched domains in result */
> @@ -1015,6 +1016,7 @@ static int generate_sched_domains(cpumask_var_t **domains,
> struct cgroup_subsys_state *pos_css;
> bool root_load_balance = is_sched_load_balance(&top_cpuset);
> bool cgrpv2 = cgroup_subsys_on_dfl(cpuset_cgrp_subsys);
> + int nslot_update;
>
> doms = NULL;
> dattr = NULL;
> @@ -1102,31 +1104,25 @@ static int generate_sched_domains(cpumask_var_t **domains,
> if (root_load_balance && (csn == 1))
> goto single_root_domain;
>
> - for (i = 0; i < csn; i++)
> - csa[i]->pn = i;
> - ndoms = csn;
> -
> -restart:
> - /* Find the best partition (set of sched domains) */
> - for (i = 0; i < csn; i++) {
> - struct cpuset *a = csa[i];
> - int apn = a->pn;
> + if (!cgrpv2) {
> + for (i = 0; i < csn; i++)
> + uf_node_init(&csa[i]->node);
>
> - for (j = 0; j < csn; j++) {
> - struct cpuset *b = csa[j];
> - int bpn = b->pn;
> -
> - if (apn != bpn && cpusets_overlap(a, b)) {
> - for (k = 0; k < csn; k++) {
> - struct cpuset *c = csa[k];
> -
> - if (c->pn == bpn)
> - c->pn = apn;
> - }
> - ndoms--; /* one less element */
> - goto restart;
> + /* Merge overlapping cpusets */
> + for (i = 0; i < csn; i++) {
> + for (j = i + 1; j < csn; j++) {
> + if (cpusets_overlap(csa[i], csa[j]))
> + uf_union(&csa[i]->node, &csa[j]->node);
> }
> }
> +
> + /* Count the total number of domains */
> + for (i = 0; i < csn; i++) {
> + if (csa[i]->node.parent == &csa[i]->node)
> + ndoms++;
> + }
> + } else {
> + ndoms = csn;
> }
>
> /*
> @@ -1159,44 +1155,25 @@ static int generate_sched_domains(cpumask_var_t **domains,
> }
>
> for (nslot = 0, i = 0; i < csn; i++) {
> - struct cpuset *a = csa[i];
> - struct cpumask *dp;
> - int apn = a->pn;
> -
> - if (apn < 0) {
> - /* Skip completed partitions */
> - continue;
> - }
> -
> - dp = doms[nslot];
> -
> - if (nslot == ndoms) {
> - static int warnings = 10;
> - if (warnings) {
> - pr_warn("rebuild_sched_domains confused: nslot %d, ndoms %d, csn %d, i %d, apn %d\n",
> - nslot, ndoms, csn, i, apn);
> - warnings--;
> - }
> - continue;
> - }
> -
> - cpumask_clear(dp);
> - if (dattr)
> - *(dattr + nslot) = SD_ATTR_INIT;
> + nslot_update = 0;
> for (j = i; j < csn; j++) {
> - struct cpuset *b = csa[j];
> -
> - if (apn == b->pn) {
> - cpumask_or(dp, dp, b->effective_cpus);
> + if (uf_find(&csa[j]->node) == &csa[i]->node) {
> + struct cpumask *dp = doms[nslot];
> +
> + if (i == j) {
> + nslot_update = 1;
> + cpumask_clear(dp);
> + if (dattr)
> + *(dattr + nslot) = SD_ATTR_INIT;
> + }
> + cpumask_or(dp, dp, csa[j]->effective_cpus);
> cpumask_and(dp, dp, housekeeping_cpumask(HK_TYPE_DOMAIN));
> if (dattr)
> - update_domain_attr_tree(dattr + nslot, b);
> -
> - /* Done with this partition */
> - b->pn = -1;
> + update_domain_attr_tree(dattr + nslot, csa[j]);
> }
> }
> - nslot++;
> + if (nslot_update)
> + nslot++;
> }
> BUG_ON(nslot != ndoms);
>
The code change looks OK to me. However, the following comment above
generate_sched_domains() describes the generation process.
* Finding the best partition (set of domains):
* The triple nested loops below over i, j, k scan over the
* load balanced cpusets (using the array of cpuset pointers in
* csa[]) looking for pairs of cpusets that have overlapping
* cpus_allowed, but which don't have the same 'pn' partition
* number and gives them in the same partition number. It keeps
* looping on the 'restart' label until it can no longer find
* any such pairs.
*
* The union of the cpus_allowed masks from the set of
* all cpusets having the same 'pn' value then form the one
* element of the partition (one sched domain) to be passed to
* partition_sched_domains().
This part is no longer correct with your patch. Would you mind updating
it to match what your new patch is doing?
BTW, please also incorporate the Andrew's suggestion about the kernel
convention of writing comments.
Thanks,
Longman
next prev parent reply other threads:[~2024-07-03 3:05 UTC|newest]
Thread overview: 52+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-31 2:48 [PATCH v2] cpuset: Optimize the number of iterations in the scheduling domain construction process Xavier
2024-05-31 21:13 ` Waiman Long
2024-06-03 12:31 ` [PATCH v3] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-04 15:02 ` Waiman Long
2024-06-10 17:18 ` Michal Koutný
2024-06-20 8:52 ` [PATCH v4 v4 0/2] cpuset: use Union-Find to optimize Xavier
2024-06-20 8:52 ` [PATCH v4 v4 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-20 14:54 ` Waiman Long
2024-06-21 8:49 ` [PATCH-cpuset v5 0/2] cpuset: use Union-Find to optimize Xavier
2024-06-21 8:49 ` [PATCH-cpuset v5 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-21 21:10 ` Tejun Heo
2024-06-22 7:14 ` [PATCH-cpuset v6 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-06-22 7:14 ` [PATCH-cpuset v6 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-22 7:14 ` [PATCH-cpuset v6 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-22 16:13 ` [PATCH-cpuset v6 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-06-23 2:38 ` [PATCH-cpuset v7 " Xavier
2024-06-23 2:39 ` [PATCH-cpuset v7 1/2] Union-Find: add a new module in kernel library Xavier
2024-06-23 2:39 ` [PATCH-cpuset v7 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-06-27 21:06 ` [PATCH-cpuset v7 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-06-28 16:13 ` [PATCH-cpuset v8 " Xavier
2024-06-28 16:13 ` [PATCH-cpuset v8 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-01 20:53 ` Tejun Heo
2024-07-02 10:50 ` [PATCH-cpuset v9 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-07-02 10:50 ` [PATCH-cpuset v9 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-02 10:50 ` [PATCH-cpuset v9 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-03 3:05 ` Waiman Long [this message]
2024-07-02 19:22 ` [PATCH-cpuset v9 0/2] Add Union-Find and use it to optimize cpuset Tejun Heo
2024-07-03 0:31 ` Andrew Morton
2024-07-03 17:34 ` Tejun Heo
2024-07-03 6:37 ` [PATCH-cpuset v10 " Xavier
2024-07-03 6:37 ` [PATCH-cpuset v10 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-03 9:40 ` Michal Koutný
2024-07-03 11:20 ` Xavier
2024-07-04 12:12 ` Michal Koutný
2024-07-03 6:37 ` [PATCH-cpuset v10 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-03 9:40 ` Michal Koutný
2024-07-03 10:49 ` Xavier
2024-07-03 16:43 ` Waiman Long
2024-07-04 6:24 ` [PATCH-cpuset v11 0/2] Add Union-Find and use it to optimize cpuset Xavier
2024-07-04 6:24 ` [PATCH-cpuset v11 1/2] Union-Find: add a new module in kernel library Xavier
2024-07-30 23:05 ` Tejun Heo
2024-07-04 6:24 ` [PATCH-cpuset v11 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-30 23:05 ` Tejun Heo
2024-07-08 1:59 ` [PATCH-cpuset v11 0/2] Add Union-Find and use it to optimize cpuset Waiman Long
2024-07-08 18:38 ` Tejun Heo
2024-07-09 2:45 ` Xavier
2024-07-29 2:44 ` Xavier
2024-07-30 23:06 ` Tejun Heo
2024-06-28 16:13 ` [PATCH-cpuset v8 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Xavier
2024-07-01 20:53 ` Tejun Heo
2024-06-21 8:49 ` [PATCH-cpuset v5 " Xavier
2024-06-20 8:52 ` [PATCH v4 v4 " Xavier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=22061c0a-d60d-4a04-9192-3e58f892deab@redhat.com \
--to=longman@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan.x@bytedance.com \
--cc=mkoutny@suse.com \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=xavier_qy@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®