All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rjw@rjwysocki.net>
To: Linux PM <linux-pm@vger.kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Lukasz Luba <lukasz.luba@arm.com>,
	Daniel Lezcano <daniel.lezcano@linaro.org>
Subject: [PATCH v1 2/2] thermal/debugfs: Add helper function for trip stats updates
Date: Mon, 15 Apr 2024 21:03:06 +0200	[thread overview]
Message-ID: <2321994.ElGaqSPkdT@kreacher> (raw)
In-Reply-To: <12418263.O9o76ZdvQC@kreacher>

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The code updating a trip_stats entry in thermal_debug_tz_trip_up()
and thermal_debug_update_temp() is almost entirely duplicate, so move
it to a new helper function that will be called from both these places.

While at it, drop a redundant tz_dbg->nr_trips check and a label related
to it from thermal_debug_update_temp().

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/thermal/thermal_debugfs.c |   42 +++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 23 deletions(-)

Index: linux-pm/drivers/thermal/thermal_debugfs.c
===================================================================
--- linux-pm.orig/drivers/thermal/thermal_debugfs.c
+++ linux-pm/drivers/thermal/thermal_debugfs.c
@@ -539,6 +539,19 @@ static struct tz_episode *thermal_debugf
 	return tze;
 }
 
+static struct trip_stats *update_tz_episode(struct tz_debugfs *tz_dbg,
+					    int trip_id, int temperature)
+{
+	struct tz_episode *tze = list_first_entry(&tz_dbg->tz_episodes,
+						  struct tz_episode, node);
+	struct trip_stats *trip_stats = &tze->trip_stats[trip_id];
+
+	trip_stats->max = max(trip_stats->max, temperature);
+	trip_stats->min = min(trip_stats->min, temperature);
+	trip_stats->avg += (temperature - trip_stats->avg) / ++trip_stats->count;
+	return trip_stats;
+}
+
 void thermal_debug_tz_trip_up(struct thermal_zone_device *tz,
 			      const struct thermal_trip *trip)
 {
@@ -547,6 +560,7 @@ void thermal_debug_tz_trip_up(struct the
 	struct thermal_debugfs *thermal_dbg = tz->debugfs;
 	int temperature = tz->temperature;
 	int trip_id = thermal_zone_trip_id(tz, trip);
+	struct trip_stats *trip_stats;
 	ktime_t now = ktime_get();
 
 	if (!thermal_dbg)
@@ -612,14 +626,8 @@ void thermal_debug_tz_trip_up(struct the
 	 */
 	tz_dbg->trips_crossed[tz_dbg->nr_trips++] = trip_id;
 
-	tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
-	tze->trip_stats[trip_id].timestamp = now;
-	tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, temperature);
-	tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, temperature);
-	tze->trip_stats[trip_id].count++;
-	tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
-		(temperature - tze->trip_stats[trip_id].avg) /
-		tze->trip_stats[trip_id].count;
+	trip_stats = update_tz_episode(tz_dbg, trip_id, temperature);
+	trip_stats->timestamp = now;
 
 unlock:
 	mutex_unlock(&thermal_dbg->lock);
@@ -686,9 +694,8 @@ out:
 void thermal_debug_update_temp(struct thermal_zone_device *tz)
 {
 	struct thermal_debugfs *thermal_dbg = tz->debugfs;
-	struct tz_episode *tze;
 	struct tz_debugfs *tz_dbg;
-	int trip_id, i;
+	int i;
 
 	if (!thermal_dbg)
 		return;
@@ -697,20 +704,9 @@ void thermal_debug_update_temp(struct th
 
 	tz_dbg = &thermal_dbg->tz_dbg;
 
-	if (!tz_dbg->nr_trips)
-		goto out;
+	for (i = 0; i < tz_dbg->nr_trips; i++)
+		update_tz_episode(tz_dbg, tz_dbg->trips_crossed[i], tz->temperature);
 
-	for (i = 0; i < tz_dbg->nr_trips; i++) {
-		trip_id = tz_dbg->trips_crossed[i];
-		tze = list_first_entry(&tz_dbg->tz_episodes, struct tz_episode, node);
-		tze->trip_stats[trip_id].count++;
-		tze->trip_stats[trip_id].max = max(tze->trip_stats[trip_id].max, tz->temperature);
-		tze->trip_stats[trip_id].min = min(tze->trip_stats[trip_id].min, tz->temperature);
-		tze->trip_stats[trip_id].avg = tze->trip_stats[trip_id].avg +
-			(tz->temperature - tze->trip_stats[trip_id].avg) /
-			tze->trip_stats[trip_id].count;
-	}
-out:
 	mutex_unlock(&thermal_dbg->lock);
 }
 




  parent reply	other threads:[~2024-04-15 19:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-15 18:59 [PATCH v1 0/2] thermal/debufs: Fix and clean up trip statistics collection Rafael J. Wysocki
2024-04-15 19:02 ` [PATCH v1 1/2] thermal/debugfs: Add missing count increment to thermal_debug_tz_trip_up() Rafael J. Wysocki
2024-04-15 19:03 ` Rafael J. Wysocki [this message]
2024-04-17  9:35   ` [PATCH v1 2/2] thermal/debugfs: Add helper function for trip stats updates Rafael J. Wysocki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2321994.ElGaqSPkdT@kreacher \
    --to=rjw@rjwysocki.net \
    --cc=daniel.lezcano@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.