From nobody Fri Apr 19 12:26:51 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40F43C4332F for ; Sat, 14 May 2022 14:20:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233183AbiENOUn (ORCPT ); Sat, 14 May 2022 10:20:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233136AbiENOUh (ORCPT ); Sat, 14 May 2022 10:20:37 -0400 Received: from m12-13.163.com (m12-13.163.com [220.181.12.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 173329FD2; Sat, 14 May 2022 07:20:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=fs7xO mXbGEknwJrqsFpipHU+pL7BLI3UV/BiTzqh4uk=; b=mPfx/jPWdKRyT+J4Y+tMp eYKLGBWgQXgAQZ9qr0oLjpWI6FlZy88RHqzbHR9tIWsfQYoUbrOJ/Jj6sYPjfQLm j5QPX3NxZosKFvmnkk+Irw1hzN+dm4+QPEiX7x3g24mSyvx8VbB3/aqCMIdL+ytx af3TrdMBx4RQbB0I4T6XaI= Received: from localhost (unknown [223.74.106.63]) by smtp9 (Coremail) with SMTP id DcCowAA3Py98un9iKCZdCw--.42048S2; Sat, 14 May 2022 22:19:41 +0800 (CST) From: Junwen Wu To: rafael@kernel.org, daniel.lezcano@linaro.org, amitk@kernel.org, rui.zhang@intel.com Cc: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, Junwen Wu Subject: [PATCH v2] thermal/core: Make trans_table tunnable to avoid some needless zero output Date: Sat, 14 May 2022 14:19:32 +0000 Message-Id: <20220514141932.53938-1-wudaemon@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: DcCowAA3Py98un9iKCZdCw--.42048S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxtr1fZF4fWF1kKFykWr1xZrb_yoW7uF4rpF nxJa4Y9r4UX3WkGay3Jr4DX3sY9F1Sqa47JFZ7K3s5KF4UJ3sxXFyDJry2yrW5GrWkuFy3 K3Wqvr98C3yUtFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pioUDdUUUUU= X-Originating-IP: [223.74.106.63] X-CM-SenderInfo: 5zxgtvxprqqiywtou0bp/1tbisQEBbVXlqDM-GAABsR Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Very high cooling device max state value make trans_table node prompt File too large. We introduce show_state node, tunnable by user,thus trans_table only show show_state'th trans count to the max trans count, in this way trans_table_show's buffer is always less than PAGE_SIZE and shows the important changes. Signed-off-by: Junwen Wu --- drivers/thermal/thermal_sysfs.c | 111 +++++++++++++++++++++++++------- 1 file changed, 86 insertions(+), 25 deletions(-) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysf= s.c index f154bada2906..bb8627aa49b7 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -656,6 +656,7 @@ struct cooling_dev_stats { spinlock_t lock; unsigned int total_trans; unsigned long state; + unsigned long show_state; unsigned long max_states; ktime_t last_time; ktime_t *time_in_state; @@ -752,38 +753,93 @@ reset_store(struct device *dev, struct device_attribu= te *attr, const char *buf, return count; } =20 -static ssize_t trans_table_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t +show_state_store(struct device *dev, struct device_attribute *attr, const = char *buf, + size_t count) { struct thermal_cooling_device *cdev =3D to_cooling_device(dev); struct cooling_dev_stats *stats =3D cdev->stats; - ssize_t len =3D 0; - int i, j; + unsigned long state; + ssize_t ret; =20 - len +=3D snprintf(buf + len, PAGE_SIZE - len, " From : To\n"); - len +=3D snprintf(buf + len, PAGE_SIZE - len, " : "); - for (i =3D 0; i < stats->max_states; i++) { - if (len >=3D PAGE_SIZE) - break; - len +=3D snprintf(buf + len, PAGE_SIZE - len, "state%2u ", i); - } - if (len >=3D PAGE_SIZE) - return PAGE_SIZE; + spin_lock(&stats->lock); + + ret =3D kstrtoul(buf, 10, &state); + if (ret || (state > stats->max_states)) + goto unlock; =20 - len +=3D snprintf(buf + len, PAGE_SIZE - len, "\n"); + stats->show_state =3D state; +unlock: + spin_unlock(&stats->lock); =20 - for (i =3D 0; i < stats->max_states; i++) { - if (len >=3D PAGE_SIZE) - break; + return count; +} + +static ssize_t +show_state_show(struct device *dev, struct device_attribute *attr, char *b= uf) +{ + struct thermal_cooling_device *cdev =3D to_cooling_device(dev); + struct cooling_dev_stats *stats =3D cdev->stats; + + return sprintf(buf, "%lu\n", stats->show_state); +} =20 - len +=3D snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i); +/* find the kth largest in nums array*/ +static int find_show_state(int *nums, int numsSize, int k, unsigned int *m= ax_value) +{ + int i, l, r, mid, cnt =3D 0, min =3D INT_MAX, max =3D 0; =20 - for (j =3D 0; j < stats->max_states; j++) { - if (len >=3D PAGE_SIZE) - break; - len +=3D snprintf(buf + len, PAGE_SIZE - len, "%8u ", - stats->trans_table[i * stats->max_states + j]); + for (i =3D 0; i < numsSize; i++) { + min =3D nums[i] < min ? nums[i] : min; + max =3D nums[i] > max ? nums[i] : max; + } + l =3D min; + r =3D max; + while (l < r) { + mid =3D r - (r - l) / 2; + for (i =3D 0; i < numsSize; i++) { + if (nums[i] >=3D mid) + cnt++; + } + if (cnt < k) { + r =3D mid - 1; + cnt =3D 0; + } else { + l =3D mid; + cnt =3D 0; } + } + + *max_value =3D max; + + return l; +} + + + +static ssize_t trans_table_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct thermal_cooling_device *cdev =3D to_cooling_device(dev); + struct cooling_dev_stats *stats =3D cdev->stats; + unsigned int show_state_value =3D 0; + unsigned int max_state_value =3D 0; + ssize_t len =3D 0; + int i, j; + + len +=3D snprintf(buf + len, PAGE_SIZE - len, " From : Index_change\n= "); + for (i =3D 0; i < stats->max_states; i++) { + show_state_value =3D find_show_state(&stats->trans_table[i * stats->max_= states], + stats->max_states, stats->show_state, &max_state_value); + if (max_state_value) + len +=3D snprintf(buf + len, PAGE_SIZE - len, "state%2u:", i); + else + continue; + for (j =3D 0; j < stats->max_states; j++) + if (stats->trans_table[i * stats->max_states + j] && (show_state_value = <=3D + stats->trans_table[i * stats->max_states + j])) + len +=3D snprintf(buf + len, PAGE_SIZE - len, " ->%u(%u)", j, + stats->trans_table[i * stats->max_states + j]); if (len >=3D PAGE_SIZE) break; len +=3D snprintf(buf + len, PAGE_SIZE - len, "\n"); @@ -793,6 +849,7 @@ static ssize_t trans_table_show(struct device *dev, pr_warn_once("Thermal transition table exceeds PAGE_SIZE. Disabling\n"); return -EFBIG; } + return len; } =20 @@ -800,12 +857,14 @@ static DEVICE_ATTR_RO(total_trans); static DEVICE_ATTR_RO(time_in_state_ms); static DEVICE_ATTR_WO(reset); static DEVICE_ATTR_RO(trans_table); +static DEVICE_ATTR_RW(show_state); =20 static struct attribute *cooling_device_stats_attrs[] =3D { &dev_attr_total_trans.attr, &dev_attr_time_in_state_ms.attr, &dev_attr_reset.attr, &dev_attr_trans_table.attr, + &dev_attr_show_state.attr, NULL }; =20 @@ -829,7 +888,7 @@ static void cooling_device_stats_setup(struct thermal_c= ooling_device *cdev) var +=3D sizeof(*stats->time_in_state) * states; var +=3D sizeof(*stats->trans_table) * states * states; =20 - stats =3D kzalloc(var, GFP_KERNEL); + stats =3D kvzalloc(var, GFP_KERNEL); if (!stats) return; =20 @@ -838,6 +897,8 @@ static void cooling_device_stats_setup(struct thermal_c= ooling_device *cdev) cdev->stats =3D stats; stats->last_time =3D ktime_get(); stats->max_states =3D states; + /* default set show_state =3D max_states/2 */ + stats->show_state =3D states / 2; =20 spin_lock_init(&stats->lock); =20 @@ -848,7 +909,7 @@ static void cooling_device_stats_setup(struct thermal_c= ooling_device *cdev) =20 static void cooling_device_stats_destroy(struct thermal_cooling_device *cd= ev) { - kfree(cdev->stats); + kvfree(cdev->stats); cdev->stats =3D NULL; } =20 --=20 2.25.1