[PATCH v1 2/2] thermal/debugfs: Add helper function for trip stats updates

From: Rafael J. Wysocki
Date: Mon Apr 15 2024 - 15:03:33 EST


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

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@xxxxxxxxx>
---
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);
}