Received: by 2002:ab2:4a89:0:b0:1f4:a8b6:6e69 with SMTP id w9csp301420lqj; Wed, 10 Apr 2024 10:47:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX5iMiatmFCn5SFQoZRfgQnrViDCq5Tch+8t9APkKfie35v5h33ozvSLdvGD6I0hEhznEIVylE3to+HLQ/E0lsNuBE9RsUqKHsz95Turw== X-Google-Smtp-Source: AGHT+IHLW+IVbDRQVtyIyeKeT47iZbROchuTU/DhJu6tlfh+HqBynDwKwRCki5LhdYOg2uqYdx3S X-Received: by 2002:a50:9b15:0:b0:56e:2a38:1fb3 with SMTP id o21-20020a509b15000000b0056e2a381fb3mr2967593edi.4.1712771248899; Wed, 10 Apr 2024 10:47:28 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712771248; cv=pass; d=google.com; s=arc-20160816; b=tFWN4gryl0p8/UNhABBvyi2H0cLNlfUHy18U6tZLIK5x0vyiGcWHcvXIbA6ZjMDXmI zEf5hVGY1/psQMimv5NnaJEgyk3rF/RCLJ1o9TlTpHmv9b9v0zGanqodXX1r+WoQJ6qe aZ0oV0n8TqskN7cDs1VAuxTa/nwLvdRHrktY1QM5gGXLLsKUmyX+7ERqFgadiuhHe/gZ jwSBtMzz32W2Xk1Lz9aS4d9Of8ZqzOMQavD7Kxsp2++5BfFSsfUvSRYl2oJxTMb9V4v8 or722V4b8lwl9eFeIPJmJHrgJPp5YXnwPDTRtWXIDM/DRkURv+Y9LjT/5U6x0akOpnw1 /XqQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=h16+jLZjQk2c7sGXidfm43588oTy1RjQwpqT/478FGM=; fh=0I/rqIlL4373pOps8oaBYF1ECx5xbCzL9wLU5PgpMhU=; b=llmnv66d99hN6eSigMLkxNn7hzcLyN6Xvgvbo4ISbPTXBbZDFcXkNwG62DHZe2l5fC OIQbqIarj5omba4XIAniSywQSgPWGkivayHJYRAKVsCGfEQpq9fq4B2KUt6pEpBlpMpd yyJWIJ6PrY+79zFD+R0v7ZZTnSZUIVZiESfCtKwLSqI/OX42fzXGHzY77CF2mmozGn97 QdhBHde2YOXSNlDecaKOclAQKS/2oKhYbLpEsxVnwQv6BA8eiPQJojaWMntlQYcMpca4 aLb+J3BanIU5571FQ4HgwVAT7H/pe2J6xlf+J/L2uSkXuFWPRIxzF3czaQj2VREpQPPP +zgA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@rjwysocki.net header.s=dkim header.b=BICP+tk1; arc=pass (i=1 spf=pass spfdomain=rjwysocki.net); spf=pass (google.com: domain of linux-kernel+bounces-139131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139131-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id kk17-20020a170907767100b00a4a348cde82si5770558ejc.503.2024.04.10.10.47.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 10:47:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-139131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=fail header.i=@rjwysocki.net header.s=dkim header.b=BICP+tk1; arc=pass (i=1 spf=pass spfdomain=rjwysocki.net); spf=pass (google.com: domain of linux-kernel+bounces-139131-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-139131-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 98CFD1F243AC for ; Wed, 10 Apr 2024 17:47:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF8A3184116; Wed, 10 Apr 2024 17:45:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b="BICP+tk1" Received: from cloudserver094114.home.pl (cloudserver094114.home.pl [79.96.170.134]) (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 2FDC017F39C; Wed, 10 Apr 2024 17:44:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.96.170.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712771102; cv=none; b=b5KB+iA9t0hzUewgUh1QuSgtoSOJ61cLTzcSqonivDDZhi1MSpEYtrHLKrpy1NnqJmKtFvD33kni1hVMmX7euN8SP3hlRtCDn6UKacdtOiU/171sQGHtOpCk/+92h2yeh+tRC9KT69aP0g6340wOKsO6v6v40Vv/mtlNKcGICpE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712771102; c=relaxed/simple; bh=x/rTmZAzWqW+xMwWax6HbPC4NnWFSCmVjfuyEhUoQpQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YzMeB1k97l7095JkChtg6+f0PHF4tD0EHy+OA5Sqe3o4L595JUzRzRcbLhpjN1YmnSCpZKykyIYwWpHU8nbXEj1q1fEqY5RQJx6cJwgIYvIoK09uvexIr4huGI5YJ7nlVWS+P7hPn1AGe221nb7IhSzWCUTVtL5o55qX0jKmcoI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net; spf=pass smtp.mailfrom=rjwysocki.net; dkim=fail (2048-bit key) header.d=rjwysocki.net header.i=@rjwysocki.net header.b=BICP+tk1 reason="signature verification failed"; arc=none smtp.client-ip=79.96.170.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rjwysocki.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rjwysocki.net Received: from localhost (127.0.0.1) (HELO v370.home.net.pl) by /usr/run/smtp (/usr/run/postfix/private/idea_relay_lmtp) via UNIX with SMTP (IdeaSmtpServer 6.0.0) id 8c375ef2b1d9d543; Wed, 10 Apr 2024 19:44:50 +0200 Received: from kreacher.localnet (unknown [195.136.19.94]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by cloudserver094114.home.pl (Postfix) with ESMTPSA id 2C7A566C66F; Wed, 10 Apr 2024 19:44:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rjwysocki.net; s=dkim; t=1712771090; bh=x/rTmZAzWqW+xMwWax6HbPC4NnWFSCmVjfuyEhUoQpQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BICP+tk1ODnlcoY2xZjSZvNG/vvHmv5Hvvb5kCuzE5Bdy1XdZoCzLNPix8FBpqTd/ bJS3mq9U3iyGf4wvBrKM2+ZoqH00liiJ221Mt8v6TBmuWaUqEwtyhIlfvvNszuHaVv eZKsxyQi/12gDu3+yrG/5+umwDMij7xq9lfOleSpX3OQKq65YSIc1VFHm4cyJmI91D HH2aH/ppt++BNAV8EizuM1NS5X20dOWbsFSsLmN60b3Ebl9dUcgMhkWrJaxxP8BErr kjFHgkUqGOktKDdhjOhU5kWmwOBy4iM2pNoAGJctPdCRyGVzId8Y6BsI8JBpqgekdi JkcYT1usIaC0w== From: "Rafael J. Wysocki" To: Linux PM Cc: LKML , Daniel Lezcano , Lukasz Luba , Srinivas Pandruvada Subject: [PATCH v1 13/16] thermal: gov_fair_share: Eliminate unnecessary integer divisions Date: Wed, 10 Apr 2024 19:00:10 +0200 Message-ID: <2170379.OBFZWjSADL@kreacher> In-Reply-To: <13515747.uLZWGnKmhe@kreacher> References: <13515747.uLZWGnKmhe@kreacher> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="UTF-8" X-CLIENT-IP: 195.136.19.94 X-CLIENT-HOSTNAME: 195.136.19.94 X-VADE-SPAMSTATE: clean X-VADE-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgedvledrudehiedguddugecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfjqffogffrnfdpggftiffpkfenuceurghilhhouhhtmecuudehtdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhephffvvefufffkjghfggfgtgesthfuredttddtjeenucfhrhhomhepfdftrghfrggvlhculfdrucghhihsohgtkhhifdcuoehrjhifsehrjhifhihsohgtkhhirdhnvghtqeenucggtffrrghtthgvrhhnpedvffeuiedtgfdvtddugeeujedtffetteegfeekffdvfedttddtuefhgeefvdejhfenucfkphepudelhedrudefiedrudelrdelgeenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmhepihhnvghtpeduleehrddufeeirdduledrleegpdhhvghlohepkhhrvggrtghhvghrrdhlohgtrghlnhgvthdpmhgrihhlfhhrohhmpedftfgrfhgrvghlucflrdcuhgihshhotghkihdfuceorhhjfiesrhhjfiihshhotghkihdrnhgvtheqpdhnsggprhgtphhtthhopeehpdhrtghpthhtoheplhhinhhugidqphhmsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrnhhivghlrdhlvgiitggrnhhosehlihhnrghrohdrohhrghdprhgtphhtthhopehluhhkrghsiidrlhhusggrsegrrhhmrdgtohhmpdhrtghpthhtohepshhr ihhnihhvrghsrdhprghnughruhhvrggurgeslhhinhhugidrihhnthgvlhdrtghomh X-DCC--Metrics: v370.home.net.pl 1024; Body=5 Fuz1=5 Fuz2=5 From: Rafael J. Wysocki The computations carried out by fair_share_throttle() for each trip point include at least one redundant integer division which introduces superfluous rounding errors. Also the multiplications by 100 in it are not really necessary and can be eliminated. Rearrange fair_share_throttle() to carry out only one integer division per trip and only as many integer multiplications as necessary and rename one variable in it (while at it). Signed-off-by: Rafael J. Wysocki --- drivers/thermal/gov_fair_share.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) Index: linux-pm/drivers/thermal/gov_fair_share.c =================================================================== --- linux-pm.orig/drivers/thermal/gov_fair_share.c +++ linux-pm/drivers/thermal/gov_fair_share.c @@ -41,12 +41,6 @@ static int get_trip_level(struct thermal return trip_level; } -static long get_target_state(struct thermal_zone_device *tz, - struct thermal_cooling_device *cdev, int percentage, int level) -{ - return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips); -} - /** * fair_share_throttle - throttles devices associated with the given zone * @tz: thermal_zone_device @@ -58,7 +52,7 @@ static long get_target_state(struct ther * * Parameters used for Throttling: * P1. max_state: Maximum throttle state exposed by the cooling device. - * P2. percentage[i]/100: + * P2. weight[i]/total_weight: * How 'effective' the 'i'th device is, in cooling the given zone. * P3. trip_level/max_no_of_trips: * This describes the extent to which the devices should be throttled. @@ -72,30 +66,34 @@ static void fair_share_throttle(struct t { struct thermal_instance *instance; int total_weight = 0; - int total_instance = 0; + int nr_instances = 0; list_for_each_entry(instance, &tz->thermal_instances, tz_node) { if (instance->trip != trip) continue; total_weight += instance->weight; - total_instance++; + nr_instances++; } list_for_each_entry(instance, &tz->thermal_instances, tz_node) { - int percentage; struct thermal_cooling_device *cdev = instance->cdev; + u64 dividend; + u32 divisor; if (instance->trip != trip) continue; - if (!total_weight) - percentage = 100 / total_instance; - else - percentage = (instance->weight * 100) / total_weight; - - instance->target = get_target_state(tz, cdev, percentage, - trip_level); + dividend = trip_level; + dividend *= cdev->max_state; + divisor = tz->num_trips; + if (total_weight) { + dividend *= instance->weight; + divisor *= total_weight; + } else { + divisor *= nr_instances; + } + instance->target = div_u64(dividend, divisor); mutex_lock(&cdev->lock); __thermal_cdev_update(cdev);