From nobody Fri Dec 19 17:39:43 2025 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8317E1FF61A for ; Wed, 8 Jan 2025 17:29:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736357396; cv=none; b=O0jzEDg1SF6F4Wiym9510XCPXPl8BvMIF1/66H5Byd0UWI7320LmxYRf6CxJRgQNwieA6B4cpIti5I1aDxlJICWak7RIadYCbRiNr2E1ssacxbCfpR1Vt920Nyp5ImAKWEAnJL0eUFAGjVmKJroN3Z/lHWakBke9HYWrXydBpWk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736357396; c=relaxed/simple; bh=fH7naInrft9G0Hg0XSlYCWlERwjlWNpDtrSKpMnJD+s=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=kIfHA8rBAOKAiA8Y7LQw+0wtSUoOE0qkMyzqPZQL6wA1GWie8YSfeijCN6TwT0C/klTb4jUimMq5FO7kDN4SlmbuVVQdWWrcBSVH3PkM8kbp/n3Q5j5aOShkK3Cx+3qdJNudwMNBJ1+mof4oXjSRmeaST1iadpVsOEM5fR+KF4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Nl3va3I0; arc=none smtp.client-ip=209.85.214.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Nl3va3I0" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-21a1e6fd923so4227195ad.1 for ; Wed, 08 Jan 2025 09:29:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736357394; x=1736962194; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=wHlefvCJLmuQrcHaWrSemjkx/IcR32ebp4M02XE01wQ=; b=Nl3va3I0N4/QdjA6IImnizutc0PWSvGO+yfsDtR22tqRiFK3BCDLa6Hq662d5ZE7cx /Iw+Q+oi20c848zVHX7J/zXne3EceKAPMUEgbJBFsGiDbfawCcJ6qATT8/OKDvzWr7QP QxYuxurjaZjBb4tNISaORV1PPUA30ZqoL3oeA9IwO9Knz2KGhOvA5h3/DsDrsinqPzJ2 wIOAy9JTGEIa57igwtRVXx+yncHzWu7w050CJ20KpTfiavGjYWYFQlUcSYiYZlTjodyo hmE2gMbclOGqfipEEIOTqgdG8SmmDLg3PTinuxxG+hdKSI82M3o55Zn50NNY0Dh4czaZ 4h/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736357394; x=1736962194; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wHlefvCJLmuQrcHaWrSemjkx/IcR32ebp4M02XE01wQ=; b=hHx4ogTlB22lN7OA9Yp7tqcY5EkEQebUfS4G5iNr0wbSlsCUZGe9m0NJ+A201odyDv WosTY2F63daIblZG/ANhUFITue5GBIQgmYIacprcQQI81cWXj6eB7yrVJKLJDcwd0xXt C6HS6qnoNqzb/QzJLHkruUEyEQvtSWCxeoBufW+cfa/FZKJuZwPr17cS+SA5hZxDns2Y xGGP/s+s7s3pdtgQy46OOYLKvlBfaG3vuUMfT9b9QyDZTIDp9xS17/XKn4UySVlYFEuY ok9PSJa5RBXnG6N8YDwSveaFJljqoG6mKh4XVUlsOG9Qmx+kvr+FoN9hYZcD/VBZFRWn JujA== X-Forwarded-Encrypted: i=1; AJvYcCVYvTMlqbt4mfLjr6S1MxWThzuI93YSyg6INSzb642nul7KCOIO1wa7+YAC4b2xY/swKdSboNEhEO12000=@vger.kernel.org X-Gm-Message-State: AOJu0YwtLzoPqNFI7Gw4p6Enxo+L3WXs9vjJG17DVoSBDMQTuIoB6KqS 0jVWs7gPYjLi1gH1wfUxnLLq63iLgtL+tlStLSisjnQE3SR1CpH8 X-Gm-Gg: ASbGncv3hm4yY+uytOPNBNQBLKNTKCeVQOIIG1NxKZGYTaE0Z0d16NnUE3qaWu9MSc0 U64UmiLW7o5gNMmfTCsrD1m+kw6yUjmr8RBG1Ld7k9fkACUcQG/qjlWJxsPAll4g4hk/v0tcDoO KMyLZwTHBSI+MutFqa2UgrAwY3fzWNKXVUBfmMd2Bf0r0uiFKLa6X/ViEjq3TuUXCl5YvgxfjU6 gNGalbzt4xeD7XxXZfH69GaSGYDrhO2WFOl1855tEiSlLycNAb7OJ1jNLl7/Y/ykGKkpzt6BUP2 X-Google-Smtp-Source: AGHT+IGPokF2DqGQMkhEcbhzHIqzB3pKgFwsbAdMlV3EH7NPAAgwQsHEgnqqy4PbOP8ecKD3KjrLDA== X-Received: by 2002:a17:903:41c3:b0:216:3732:ade3 with SMTP id d9443c01a7336-21a83fc3c07mr50928195ad.35.1736357393735; Wed, 08 Jan 2025 09:29:53 -0800 (PST) Received: from vaxr-BM6660-BM6360.. ([2001:288:7001:2703:3148:f4e2:2393:e5c8]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dca02b3fsm329860825ad.244.2025.01.08.09.29.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2025 09:29:53 -0800 (PST) From: I Hsin Cheng To: mingo@redhat.com Cc: peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, linux-kernel@vger.kernel.org, I Hsin Cheng Subject: [RFC PATCH] sched/fair: Refactor can_migrate_task() to elimate looping Date: Thu, 9 Jan 2025 01:29:47 +0800 Message-ID: <20250108172948.452158-1-richard120310@gmail.com> X-Mailer: git-send-email 2.43.0 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 Content-Type: text/plain; charset="utf-8" The function "can_migrate_task()" utilize "for_each_cpu_and" with a "if" statement inside to find the destination cpu. It's the same logic to find the first set bit of the result of the bitwise-AND of "env->dst_grpmask", "env->cpus" and "p->cpus_ptr". Refactor it by directly perform bitwise-AND for "env->dst_grpmask", "env->cpus" and "p->cpus_ptr" and use "cpumask_first()" to select the destination cpu, so we can elimate the need of looping and multiple times of branch. After the refactoring this part of the code can speed up from ~130ns to ~93ns, according to the test below. Ran the test for 5 times and the result is showned in the following table, and the test script is paste in next section. ------------------------------------------------------- |Old method| 123| 135| 126| 129| 135| avg ~130ns| ------------------------------------------------------- |New method| 87| 97| 90| 92| 98| avg ~93ns| ------------------------------------------------------- Signed-off-by: I Hsin Cheng --- Test is done on Linux 6.8.0-48-generic x86_64 with Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz Test is executed in the form of kernel module. Test script: int init_module(void) { struct cpumask cur_mask, custom_mask, result_mask; struct task_struct *p =3D current; int cpu, cpu1 =3D nr_cpu_ids, cpu2 =3D nr_cpu_ids; unsigned tmp =3D 0; cpumask_copy(&cur_mask, cpu_online_mask); /* Self-implemented function, didn't paste here because the length */ generate_random_cpumask(&custom_mask); ktime_t start_1 =3D ktime_get(); for_each_cpu_and(cpu, &cur_mask, &custom_mask) { if (cpumask_test_cpu(cpu, p->cpus_ptr)) { /* imitate load balance operation */ tmp |=3D 0x01010101; cpu1 =3D cpu; break; } } ktime_t end_1 =3D ktime_get(); ktime_t start_2 =3D ktime_get(); cpumask_and(&result_mask, &cur_mask, &custom_mask); cpumask_and(&result_mask, &result_mask, p->cpus_ptr); cpu =3D cpumask_first(&result_mask); if (cpu < nr_cpu_ids) { /* imitate load balance operation */ tmp |=3D 0x01010101; cpu2 =3D cpu; } ktime_t end_2 =3D ktime_get(); if (cpu1 !=3D cpu2) { pr_err("Failed Assertion, cpu1 =3D %d, cpu2 =3D %d\n", cpu1, cpu2); return 0; } pr_info("Old method spend time : %lld\n", ktime_to_ns(end_1 - start_1)); pr_info("New method spend time : %lld\n", ktime_to_ns(end_2 - start_2)); return 0; } --- kernel/sched/fair.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 2d16c8545..ce46f61da 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -9404,12 +9404,16 @@ int can_migrate_task(struct task_struct *p, struct = lb_env *env) return 0; =20 /* Prevent to re-select dst_cpu via env's CPUs: */ - for_each_cpu_and(cpu, env->dst_grpmask, env->cpus) { - if (cpumask_test_cpu(cpu, p->cpus_ptr)) { - env->flags |=3D LBF_DST_PINNED; - env->new_dst_cpu =3D cpu; - break; - } + struct cpumask dst_mask; + + cpumask_and(&dst_mask, env->dst_grpmask, env->cpus); + cpumask_and(&dst_mask, &dst_mask, p->cpus_ptr); + + cpu =3D cpumask_first(&dst_mask); + + if (cpu < nr_cpu_ids) { + env->flags |=3D LBF_DST_PINNED; + env->new_dst_cpu =3D cpu; } =20 return 0; --=20 2.43.0