Skip to content

Commit c98e247

Browse files
committed
thermal: gov_fair_share: Eliminate unnecessary integer divisions
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 <[email protected]> Reviewed-by: Lukasz Luba <[email protected]>
1 parent 0292991 commit c98e247

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

drivers/thermal/gov_fair_share.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ static int get_trip_level(struct thermal_zone_device *tz)
4141
return trip_level;
4242
}
4343

44-
static long get_target_state(struct thermal_zone_device *tz,
45-
struct thermal_cooling_device *cdev, int percentage, int level)
46-
{
47-
return (long)(percentage * level * cdev->max_state) / (100 * tz->num_trips);
48-
}
49-
5044
/**
5145
* fair_share_throttle - throttles devices associated with the given zone
5246
* @tz: thermal_zone_device
@@ -58,7 +52,7 @@ static long get_target_state(struct thermal_zone_device *tz,
5852
*
5953
* Parameters used for Throttling:
6054
* P1. max_state: Maximum throttle state exposed by the cooling device.
61-
* P2. percentage[i]/100:
55+
* P2. weight[i]/total_weight:
6256
* How 'effective' the 'i'th device is, in cooling the given zone.
6357
* P3. trip_level/max_no_of_trips:
6458
* This describes the extent to which the devices should be throttled.
@@ -72,30 +66,34 @@ static void fair_share_throttle(struct thermal_zone_device *tz,
7266
{
7367
struct thermal_instance *instance;
7468
int total_weight = 0;
75-
int total_instance = 0;
69+
int nr_instances = 0;
7670

7771
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
7872
if (instance->trip != trip)
7973
continue;
8074

8175
total_weight += instance->weight;
82-
total_instance++;
76+
nr_instances++;
8377
}
8478

8579
list_for_each_entry(instance, &tz->thermal_instances, tz_node) {
86-
int percentage;
8780
struct thermal_cooling_device *cdev = instance->cdev;
81+
u64 dividend;
82+
u32 divisor;
8883

8984
if (instance->trip != trip)
9085
continue;
9186

92-
if (!total_weight)
93-
percentage = 100 / total_instance;
94-
else
95-
percentage = (instance->weight * 100) / total_weight;
96-
97-
instance->target = get_target_state(tz, cdev, percentage,
98-
trip_level);
87+
dividend = trip_level;
88+
dividend *= cdev->max_state;
89+
divisor = tz->num_trips;
90+
if (total_weight) {
91+
dividend *= instance->weight;
92+
divisor *= total_weight;
93+
} else {
94+
divisor *= nr_instances;
95+
}
96+
instance->target = div_u64(dividend, divisor);
9997

10098
mutex_lock(&cdev->lock);
10199
__thermal_cdev_update(cdev);

0 commit comments

Comments
 (0)