From nobody Fri Dec 19 11:04:19 2025 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.4]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BAD0B51004; Tue, 2 Jul 2024 10:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719917640; cv=none; b=V09pI3tfIdyZ+gdjklpF0/+JyndXrw40vvQ6ufIaizUJAND0lKB+iEIm+rBt/Ir3EYzpUiCggRmWmEh5pwk4LqlGMIyUlucVWgCFfQq/t409ULif1cv03tatsU3KqnxerUWF5YkE+LEi+8Kt6GSZduPhodqaM4YWzuy6BxcBPAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719917640; c=relaxed/simple; bh=CpTcOvl0QkABqKFQB6/gYUAOE1TtybWUTE1vLk4wGmo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RGOF2sr8Phjbv7JkuviPECf7iYJ/QsNKWePRu1hrrobdkx09Fo58CUCiwzP/ilDmHR3PRC/VK6rTbCbn8JpeflhfjsJRQqiCqcHSBX4TiaEZ7FxjaITr81JBHB2us0I8B83e51XQslwmiBAV0Tgd08ipeogW5Tr1murK9RHJeLw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=gwGaxGLq; arc=none smtp.client-ip=117.135.210.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="gwGaxGLq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=PQ9RM 3z3im+VjcU0v0S4k3STJQrv4lvRtxUnRKvb9y8=; b=gwGaxGLqTXIHZJ6NtaNn4 d1I0KA5rRuSylBta9YIHGn+N86gdMZ8J5z0lcmMXirnOtWolqBvKUH78kAViu1sO YODAVuR6FRVKFJonz69iKnNj4sP0utoOESDPPwexyXJ/an6beq2iA/IoFLT1C4I9 wZfES0vrEMG3VllHqTvLjY= Received: from localhost (unknown [101.132.132.191]) by gzga-smtp-mta-g3-0 (Coremail) with SMTP id _____wD3P7Kp24NmfgFZAw--.52978S2; Tue, 02 Jul 2024 18:51:22 +0800 (CST) From: Xavier To: tj@kernel.org Cc: longman@redhat.com, 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, Xavier Subject: [PATCH-cpuset v9 2/2] cpuset: use Union-Find to optimize the merging of cpumasks Date: Tue, 2 Jul 2024 18:50:10 +0800 Message-Id: <20240702105010.253933-3-xavier_qy@163.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240702105010.253933-1-xavier_qy@163.com> References: <20240702105010.253933-1-xavier_qy@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wD3P7Kp24NmfgFZAw--.52978S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxAFWfur4kKFyUZFWUJrW3Awb_yoWruryUpF 4Sk3y2qrWrJryUGwsakay8Zw1Yk3ykJayUtw15Gw1rtrnrA3Z29a40qFs3KayUZrWq9F1U uF9Igr47Wr1UKFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UbBMNUUUUU= X-CM-SenderInfo: 50dyxvpubt5qqrwthudrp/1tbiYwoQEGV4JGHcNAAAsZ Content-Type: text/plain; charset="utf-8" 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 --- 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 #include #include +#include =20 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; =20 - /* partition number for rebuild_sched_domains() */ - int pn; - /* for custom sched domain */ int relax_domain_level; =20 @@ -208,6 +206,9 @@ struct cpuset { =20 /* 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; }; =20 /* @@ -1007,7 +1008,7 @@ static int generate_sched_domains(cpumask_var_t **dom= ains, 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 =3D 0; /* number of sched domains in result */ @@ -1015,6 +1016,7 @@ static int generate_sched_domains(cpumask_var_t **dom= ains, struct cgroup_subsys_state *pos_css; bool root_load_balance =3D is_sched_load_balance(&top_cpuset); bool cgrpv2 =3D cgroup_subsys_on_dfl(cpuset_cgrp_subsys); + int nslot_update; =20 doms =3D NULL; dattr =3D NULL; @@ -1102,31 +1104,25 @@ static int generate_sched_domains(cpumask_var_t **d= omains, if (root_load_balance && (csn =3D=3D 1)) goto single_root_domain; =20 - for (i =3D 0; i < csn; i++) - csa[i]->pn =3D i; - ndoms =3D csn; - -restart: - /* Find the best partition (set of sched domains) */ - for (i =3D 0; i < csn; i++) { - struct cpuset *a =3D csa[i]; - int apn =3D a->pn; + if (!cgrpv2) { + for (i =3D 0; i < csn; i++) + uf_node_init(&csa[i]->node); =20 - for (j =3D 0; j < csn; j++) { - struct cpuset *b =3D csa[j]; - int bpn =3D b->pn; - - if (apn !=3D bpn && cpusets_overlap(a, b)) { - for (k =3D 0; k < csn; k++) { - struct cpuset *c =3D csa[k]; - - if (c->pn =3D=3D bpn) - c->pn =3D apn; - } - ndoms--; /* one less element */ - goto restart; + /* Merge overlapping cpusets */ + for (i =3D 0; i < csn; i++) { + for (j =3D 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 =3D 0; i < csn; i++) { + if (csa[i]->node.parent =3D=3D &csa[i]->node) + ndoms++; + } + } else { + ndoms =3D csn; } =20 /* @@ -1159,44 +1155,25 @@ static int generate_sched_domains(cpumask_var_t **d= omains, } =20 for (nslot =3D 0, i =3D 0; i < csn; i++) { - struct cpuset *a =3D csa[i]; - struct cpumask *dp; - int apn =3D a->pn; - - if (apn < 0) { - /* Skip completed partitions */ - continue; - } - - dp =3D doms[nslot]; - - if (nslot =3D=3D ndoms) { - static int warnings =3D 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) =3D SD_ATTR_INIT; + nslot_update =3D 0; for (j =3D i; j < csn; j++) { - struct cpuset *b =3D csa[j]; - - if (apn =3D=3D b->pn) { - cpumask_or(dp, dp, b->effective_cpus); + if (uf_find(&csa[j]->node) =3D=3D &csa[i]->node) { + struct cpumask *dp =3D doms[nslot]; + + if (i =3D=3D j) { + nslot_update =3D 1; + cpumask_clear(dp); + if (dattr) + *(dattr + nslot) =3D 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 =3D -1; + update_domain_attr_tree(dattr + nslot, csa[j]); } } - nslot++; + if (nslot_update) + nslot++; } BUG_ON(nslot !=3D ndoms); =20 --=20 2.45.0