Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp202213lqp; Wed, 22 May 2024 01:43:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWvwDzoYZ9UqRn+ZtnWed7bXCj3W7Hb4pUXUgMa4u93OwySfJbDE4qjuc9k7fUhYedsEOQj1JPM5lpqZVKh99y1DEsHnADWx2ztjm9ynA== X-Google-Smtp-Source: AGHT+IEBEg1KtlYZBqUWV6b7qo4PAatnTiK5YpzziJeUTR+sBmCyhNR/UEAaJBwgqDxKU1aoWWAI X-Received: by 2002:a25:d08e:0:b0:de5:5702:614f with SMTP id 3f1490d57ef6-df4e0bc8018mr2223935276.11.1716367404657; Wed, 22 May 2024 01:43:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716367404; cv=pass; d=google.com; s=arc-20160816; b=H50BauIEKrX2LHgMv7lr8vGryHex73HXxhxTmqjqLqSlzW1g6k65Xx5hCYn+dFqPCM z0TEscW8bZLpMihw9ExXsNAG8hLpfuIg9DuIDnXVmkZxXIZ5doQzApUikJYj8iRAwAU1 FUEDXp19qvTexKQfuwwqE4Gl6YV1yeBGciYOF2zyy7asILXc9r3qtuoqIxi0y5Y8JfXr y6F00yE836ulHiIpwiAtLsCTtUJjIqN44PbCK+QJ/BBkEnjkOHWVBHVkgHfA4itB5YO5 WeT4VE7WZdtghy2mx7ylJCmJ91DnDak7WK3T1KWyJLmaS1zzNwetA4ANr7G2a+LeWyys LWag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=WpmsIJrbdQjP0ANo+jHMzdspWIJkHRqfkFoQSba9FGg=; fh=kkia7P/8R5JqVLM5d8VZefAWtcluXwylpqtHZEKmRz4=; b=UsWN2GO4ljfVmsyCSRAXmDqTyPQINUHMPaaVfmw9jWtRJoEVEKkGDPkD/1zDoA0/9l qf24/a7ejMTpeFHLgkr1IXHxC6dLKoSui7Zz451MGlJZ6Xf3sthYXELAKYMR2P8r9w8V Ce9de8VMhk8itkoHvR8stcCAng8DfrRSsubDwNvKAeKuYZw/EPU2YQ0YBp4cvhyvl4ME gpr7UGc33ae5z81aDVp+rgr6KVF4M3w/+JKHJlHFO3Nfz6qwjwrahD4Ck94fPgN19+NU Ji9Mdy+gq2V0OCyPTOjGQ4FoePR8j075UrZmgV9VybCXMEbueUmfrDfEq6AuCXegnHtJ BxSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=P+djzFDZ; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-185947-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185947-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id d75a77b69052e-43e114217c5si76268021cf.313.2024.05.22.01.43.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 May 2024 01:43:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185947-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=P+djzFDZ; arc=pass (i=1 spf=pass spfdomain=igalia.com dkim=pass dkdomain=igalia.com); spf=pass (google.com: domain of linux-kernel+bounces-185947-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185947-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 16E7A1C21316 for ; Wed, 22 May 2024 08:43:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2A42F7FBAE; Wed, 22 May 2024 08:43:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="P+djzFDZ" Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73C49770FB for ; Wed, 22 May 2024 08:43:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716367397; cv=none; b=GkXsy8aFjs1/8pBSWCgOMmVfm91uJOvSP16MD3IO/Yrr/rlfefI4tYbO08Qg3ym0fFxi3We0wBYilcqzJWyfIl1HEQ1jBesiXn7R8Xy3Ykow8zqeRgqb1V53hw7h38g09lEHbOXdeL9rznBq5RS0LvKY/MUqrlRkLI1oWQuOfD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716367397; c=relaxed/simple; bh=08lKOkLNL3XdBTW43tCGVeymxKCeJCPxZgN9ppxVj4A=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=ccvDGzqT/vPYJ/Pf3dLTiJjEH/LKV3Vvk4QL3aweez9OIk/RfC8gIBKeaBYgmKMiwHQayUSp2V6NmyTYe2pXUYvNGT85CscDwjOOUzPIVDzPoHpqGBKpwgXZvhzRqT0gureWP4XNiyQKIWcDK2O+AS7GLyQ/hRuflaEVSC+hVsg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=P+djzFDZ; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From: References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=WpmsIJrbdQjP0ANo+jHMzdspWIJkHRqfkFoQSba9FGg=; b=P+djzFDZOZ/Syt5F8K7tOLrh/u Uoh7PWH9dkw0ujql2O0okGKdogAoD3ILGJK6jaTTrc31pGMs7IYoA/GsN4Ne1pRpTL2CWSLeGDLiN 9Q8kvoJFpYaaM0MseVSSdBVAU9xkudLxi3EqvSdFoyfAh7OV/R8eVMLkMiGQzD40dqIvvsyIDor1m yiFVDRJdoDT5WtaU/1DfrOgdbAJs5CIHY2j9knspyuFRHTWCFFjYq5RYYyMtpmJQjmZX+D9dA1hwu MPHDaZyLgwvHNbQ0VRxN+csu+pl2rnilLlyNNDSGuF+oy5/ziBHYYsEEh7WeDDgwE4NMVnJW/sivf kxTy1Ysw==; Received: from [84.69.19.168] (helo=[192.168.0.101]) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_128_GCM:128) (Exim) id 1s9hYo-00B19C-7R; Wed, 22 May 2024 10:42:58 +0200 Message-ID: <86542894-d085-41da-840f-26045ef590e0@igalia.com> Date: Wed, 22 May 2024 09:42:57 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] sched/psi: Optimise psi_group_change a bit Content-Language: en-GB To: Tvrtko Ursulin Cc: linux-kernel@vger.kernel.org, Tvrtko Ursulin , Suren Baghdasaryan , Peter Ziljstra , kernel-dev@igalia.com, Johannes Weiner , Juri Lelli , Vincent Guittot , Ingo Molnar References: <20240329160648.86999-1-tursulin@igalia.com> <20240329185147.GA877460@cmpxchg.org> <20240409223847.GE1057805@cmpxchg.org> <99b39d9e-1fae-41a5-96f9-d1298c7cc29c@igalia.com> <91c82905-4319-470c-8797-3c6effa7e32c@igalia.com> From: Tvrtko Ursulin In-Reply-To: <91c82905-4319-470c-8797-3c6effa7e32c@igalia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, Any chance of getting this in somewhere? I am gradually implementing an exponential back-off in terms of pinging this thread but it is a simple and clear improvement so I don't know why it is stuck really. Kind regards, Tvrtko On 01/05/2024 16:09, Tvrtko Ursulin wrote: > > Hi, > > Adding more scheduler maintainers to Cc - hopefully someone can merge to > the relevant tree? > > Thanks, > > Tvrtko > > On 18/04/2024 09:54, Tvrtko Ursulin wrote: >> >> Hi Ingo, >> >> On 09/04/2024 23:38, Johannes Weiner wrote: >>> [ Oops, I still had an old mutt alias for Ingo's address. ] >>> >>> Ingo, would you mind taking this through the scheduler tree? >> >> Gentle reminder so this one does not fall through the cracks. >> >> Regards, >> >> Tvrtko >> >>> On Fri, Mar 29, 2024 at 02:51:53PM -0400, Johannes Weiner wrote: >>>> On Fri, Mar 29, 2024 at 04:06:48PM +0000, Tvrtko Ursulin wrote: >>>>> From: Tvrtko Ursulin >>>>> >>>>> The current code loops over the psi_states only to call a helper which >>>>> then resolves back to the action needed for each state using a switch >>>>> statement. That is effectively creating a double indirection of a kind >>>>> which, given how all the states need to be explicitly listed and >>>>> handled >>>>> anyway, we can simply remove. Both the for loop and the switch >>>>> statement >>>>> that is. >>>>> >>>>> The benefit is both in the code size and CPU time spent in this >>>>> function. >>>>> YMMV but on my Steam Deck, while in a game, the patch makes the CPU >>>>> usage >>>>> go from ~2.4% down to ~1.2%. Text size at the same time went from >>>>> 0x323 to >>>>> 0x2c1. >>>>> >>>>> Signed-off-by: Tvrtko Ursulin >>>>> Cc: Johannes Weiner >>>>> Cc: Suren Baghdasaryan >>>>> Cc: Peter Ziljstra >>>>> Cc: linux-kernel@vger.kernel.org >>>>> Cc: kernel-dev@igalia.com >>>> >>>> This is great. >>>> >>>> Acked-by: Johannes Weiner >>>> >>>> Ingo, would you mind please taking this through the scheduler tree? I >>>> think Peter is still out. >>>> >>>> Remaining quote below. >>>> >>>> Thanks >>>> >>>>> --- >>>>>   kernel/sched/psi.c | 54 >>>>> +++++++++++++++++++++++----------------------- >>>>>   1 file changed, 27 insertions(+), 27 deletions(-) >>>>> >>>>> diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c >>>>> index 7b4aa5809c0f..55720ecf420e 100644 >>>>> --- a/kernel/sched/psi.c >>>>> +++ b/kernel/sched/psi.c >>>>> @@ -218,28 +218,32 @@ void __init psi_init(void) >>>>>       group_init(&psi_system); >>>>>   } >>>>> -static bool test_state(unsigned int *tasks, enum psi_states state, >>>>> bool oncpu) >>>>> +static u32 test_states(unsigned int *tasks, u32 state_mask) >>>>>   { >>>>> -    switch (state) { >>>>> -    case PSI_IO_SOME: >>>>> -        return unlikely(tasks[NR_IOWAIT]); >>>>> -    case PSI_IO_FULL: >>>>> -        return unlikely(tasks[NR_IOWAIT] && !tasks[NR_RUNNING]); >>>>> -    case PSI_MEM_SOME: >>>>> -        return unlikely(tasks[NR_MEMSTALL]); >>>>> -    case PSI_MEM_FULL: >>>>> -        return unlikely(tasks[NR_MEMSTALL] && >>>>> -            tasks[NR_RUNNING] == tasks[NR_MEMSTALL_RUNNING]); >>>>> -    case PSI_CPU_SOME: >>>>> -        return unlikely(tasks[NR_RUNNING] > oncpu); >>>>> -    case PSI_CPU_FULL: >>>>> -        return unlikely(tasks[NR_RUNNING] && !oncpu); >>>>> -    case PSI_NONIDLE: >>>>> -        return tasks[NR_IOWAIT] || tasks[NR_MEMSTALL] || >>>>> -            tasks[NR_RUNNING]; >>>>> -    default: >>>>> -        return false; >>>>> +    const bool oncpu = state_mask & PSI_ONCPU; >>>>> + >>>>> +    if (tasks[NR_IOWAIT]) { >>>>> +        state_mask |= BIT(PSI_IO_SOME); >>>>> +        if (!tasks[NR_RUNNING]) >>>>> +            state_mask |= BIT(PSI_IO_FULL); >>>>>       } >>>>> + >>>>> +    if (tasks[NR_MEMSTALL]) { >>>>> +        state_mask |= BIT(PSI_MEM_SOME); >>>>> +        if (tasks[NR_RUNNING] == tasks[NR_MEMSTALL_RUNNING]) >>>>> +            state_mask |= BIT(PSI_MEM_FULL); >>>>> +    } >>>>> + >>>>> +    if (tasks[NR_RUNNING] > oncpu) >>>>> +        state_mask |= BIT(PSI_CPU_SOME); >>>>> + >>>>> +    if (tasks[NR_RUNNING] && !oncpu) >>>>> +        state_mask |= BIT(PSI_CPU_FULL); >>>>> + >>>>> +    if (tasks[NR_IOWAIT] || tasks[NR_MEMSTALL] || tasks[NR_RUNNING]) >>>>> +        state_mask |= BIT(PSI_NONIDLE); >>>>> + >>>>> +    return state_mask; >>>>>   } >>>>>   static void get_recent_times(struct psi_group *group, int cpu, >>>>> @@ -770,7 +774,6 @@ static void psi_group_change(struct psi_group >>>>> *group, int cpu, >>>>>   { >>>>>       struct psi_group_cpu *groupc; >>>>>       unsigned int t, m; >>>>> -    enum psi_states s; >>>>>       u32 state_mask; >>>>>       groupc = per_cpu_ptr(group->pcpu, cpu); >>>>> @@ -841,10 +844,7 @@ static void psi_group_change(struct psi_group >>>>> *group, int cpu, >>>>>           return; >>>>>       } >>>>> -    for (s = 0; s < NR_PSI_STATES; s++) { >>>>> -        if (test_state(groupc->tasks, s, state_mask & PSI_ONCPU)) >>>>> -            state_mask |= (1 << s); >>>>> -    } >>>>> +    state_mask = test_states(groupc->tasks, state_mask); >>>>>       /* >>>>>        * Since we care about lost potential, a memstall is FULL >>>>> @@ -1194,7 +1194,7 @@ void psi_cgroup_restart(struct psi_group *group) >>>>>       /* >>>>>        * After we disable psi_group->enabled, we don't actually >>>>>        * stop percpu tasks accounting in each psi_group_cpu, >>>>> -     * instead only stop test_state() loop, record_times() >>>>> +     * instead only stop test_states() loop, record_times() >>>>>        * and averaging worker, see psi_group_change() for details. >>>>>        * >>>>>        * When disable cgroup PSI, this function has nothing to sync >>>>> @@ -1202,7 +1202,7 @@ void psi_cgroup_restart(struct psi_group *group) >>>>>        * would see !psi_group->enabled and only do task accounting. >>>>>        * >>>>>        * When re-enable cgroup PSI, this function use >>>>> psi_group_change() >>>>> -     * to get correct state mask from test_state() loop on tasks[], >>>>> +     * to get correct state mask from test_states() loop on tasks[], >>>>>        * and restart groupc->state_start from now, use .clear = >>>>> .set = 0 >>>>>        * here since no task status really changed. >>>>>        */ >>>>> -- >>>>> 2.44.0 >>>>>