Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp835549lqt; Fri, 19 Apr 2024 11:47:52 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUIKnX3XyDXw71fi+p/77SriB1LHu61vU2bKMshlb2kld9HJkCKR8PF4JdBZS8ebHVGAoRGEmtHPcykfSJULA0XJYb17Y6WdIaX/CHcVQ== X-Google-Smtp-Source: AGHT+IFdyXoaYL1Km3Qdjb0n+fErXP9dIkw016G4DOU75DYHkZ7YN2Pifs0ULpdcoRB8Hmjw4bjQ X-Received: by 2002:a17:902:e74b:b0:1e5:375c:429a with SMTP id p11-20020a170902e74b00b001e5375c429amr4122042plf.17.1713552472479; Fri, 19 Apr 2024 11:47:52 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713552472; cv=pass; d=google.com; s=arc-20160816; b=DENkjej6xp953kWI4FLOGHfDzgndSXrqZ1OoMSi/Ajkh2TJdJRZI+Dl9HlMLZ6ApCh 6Qb+ttlpbX4uMJzXAASBvrGaEp9795LbfdwFK5NJ2bxQPyvpZE/QR0xEbp/PAg1WIdV+ i/0N0RgMfqxVuDvk8J6bukNsQ1YCieAmudW7GS6VFYn2ESdI6LPR0+bxJR2puzLsGHtp unKOJCmHQX1WE/H80jUSoSYK20rEoH5ftU/v4HvxfM72e8AVIUIEhDH1TBsYfPMlNz1y FDDov6BA9CPXA+SGY6XiV2OTBbJAjh2I8QeWsTX4wXldrLh1nsUepRnZOiLnhwl7o5SP FxFg== 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:content-language :references:cc:to:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=M0CPLklEmsNuyb04riRneItHd8B5GUiVny5FCSpZfYg=; fh=NWbihT0n2LVGI0eIabKMMzs0D4KhesRC0I1HJGdvI0I=; b=vJbiK0N4Hzt2NQJ28qijpOycm3GmGN2Cx+X2hhb0I83E1NgVF1PkdzJgznHyv0lZL9 EYqRY0dxnArx3oopX2shXjpjw5Obl7VUMdr9wpcsK9LS0oG55qM/OZAJkeCs0gjo4+H4 ybfmPoHa7H9R8hflNq2Cc7QpUDs8Q2OQrhj7bUAOPJPxnRZ4O7g1fwDO4C5tadDliIWB Q5X37CFSGFnu51e26wAnSndAycOThfTg6IHjZaSzmX4NnuwGuvXOjiHElw1i2f1esZr+ Wk3x0Zmhr172gP9RLcDGe6etB5gJAVVU1lrt/Jw/C3icVa7Tj848M0Bea/hf0fKXHtAU uQcA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-151881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id e18-20020a170903241200b001e259d00f15si3729984plo.20.2024.04.19.11.47.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 11:47:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-151881-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151881-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com 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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 797B8B222E0 for ; Fri, 19 Apr 2024 18:46:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D2AE612E1ED; Fri, 19 Apr 2024 18:45:58 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9453177F13; Fri, 19 Apr 2024 18:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713552358; cv=none; b=o8aj5/1+YP+pOR1aM6GZI5b+0m3R6f7WaF78WmfIhWCVz4pRo+Bhdn4iv2g2KFh8n2TxjsHAGdFTJhuMCwc/PPqDnCXE1h7EHGFjblQORu+6qNL1pUqRyRBqkbdWWPOtxhcbUDGnn5E21Q9lIaZZ2BhrXoWNlh48tsbAWHM0/34= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713552358; c=relaxed/simple; bh=R+Hm02VfwLUiXkuHnQjHgGTxMk8Q1vvEbG83tnYl8Eg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Gq1GSi2AINUwYdMOt7CPtfVw7uwxBZL+oJ+UUgzO30eIG1BmpFC0MY5r6XyvhUqAF3e2ffqv6uV7lgvVxCzlNtj52Y6MYtkvVVP0XsXz3cVRhP2OTWLX9HkbC4Kit9+zmikQ4UnpNXSQBOWURPVJ7Q74BV21ajQWlQeFfM0qrQA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 566F32F; Fri, 19 Apr 2024 11:46:24 -0700 (PDT) Received: from [10.57.77.69] (unknown [10.57.77.69]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1A6153F792; Fri, 19 Apr 2024 11:45:54 -0700 (PDT) Message-ID: Date: Fri, 19 Apr 2024 19:46:01 +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 v1 13/16] thermal: gov_fair_share: Eliminate unnecessary integer divisions To: "Rafael J. Wysocki" Cc: LKML , Linux PM , Daniel Lezcano , Srinivas Pandruvada References: <13515747.uLZWGnKmhe@kreacher> <2170379.OBFZWjSADL@kreacher> Content-Language: en-US From: Lukasz Luba In-Reply-To: <2170379.OBFZWjSADL@kreacher> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/10/24 18:00, Rafael J. Wysocki wrote: > 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); > > > Reviewed-by: Lukasz Luba