All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic
@ 2021-12-10 16:31 Chetankumar Mistry
  2021-12-10 16:31 ` [PATCH][RFC 2/2] Remove sysfs entry Chetankumar Mistry
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Chetankumar Mistry @ 2021-12-10 16:31 UTC (permalink / raw)
  To: linux-kernel
  Cc: lukasz.luba, rafael, daniel.lezcano, amitk, rui.zhang, linux-pm

Implement the Ziegler-Nichols Heuristic algorithm to better
estimate the PID Coefficients for a running platform.
The values are tuned to minimuse the amount of overshoot in
the temperature of the platform and subsequently minimise
the number of switches for cdev states.

Signed-off-by: Chetankumar Mistry <chetan.mistry@arm.com>
---
 drivers/thermal/gov_power_allocator.c | 384 ++++++++++++++++++++++++++
 drivers/thermal/thermal_sysfs.c       |   2 +
 include/linux/thermal.h               |   7 +
 3 files changed, 393 insertions(+)

diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
index 13e375751d22..7819c693ed1a 100644
--- a/drivers/thermal/gov_power_allocator.c
+++ b/drivers/thermal/gov_power_allocator.c
@@ -49,6 +49,58 @@ static inline s64 div_frac(s64 x, s64 y)
 	return div_s64(x << FRAC_BITS, y);
 }
 
+/**
+ * enum pivot_type - Values representing what type of pivot the current error
+ *                   value is
+ * @PEAK     - The current error is a peak
+ * @TROUGH   - The current error is a trough
+ * @MIDPOINT - The current error is neither a peak or trough and is some midpoint
+ *             in between
+ */
+enum pivot_type { PEAK = 1, TROUGH = -1, MIDPOINT = 0 };
+
+/**
+ * enum ZN_VALUES - Values which the Ziegler-Nichols variable can take. This
+ *                  determines which set of PID Coefficients to use
+ * @ZN_ORIGINAL - Use the Original PID Coefficients when the thermal zone was
+ *                initially bound
+ * @ZN_OFF      - Use the current set of PID Coefficients
+ * @ZN_ON       - Use Ziegler-Nichols to determine the best set of PID Coefficients
+ * @ZN_RESET    - Reset the Ziegler-Nichols set of PID Coefficients so they can be
+ *                found again
+ */
+enum ZN_VALUES { ZN_ORIGINAL = -1, ZN_OFF = 0, ZN_ON = 1, ZN_RESET = 2 };
+
+/**
+ * struct zn_coefficients - values used by the Ziegler-Nichols Heuristic to
+ *                          determine what the optimal PID coefficients are
+ * @zn_found    - Determine whether we have found or are still searching for
+ *                optimal PID coefficients
+ * @prev_err    - Previous err logged
+ * @curr_err    - Current err being processed
+ * @t_prev_peak - Timestamp for the previous "Peak"
+ * @period      - Period of osciallation
+ * @k_ultimate  - Value of k_P which produces stable oscillations
+ * @base_peak   - Err value of the current peak
+ * @base_trough - Err value fo the current trough
+ * @oscillation_count  - Number of stable oscillations we have observed
+ * @prev_pivot  - Whether the previous pivot was a peak or trough
+ *
+ */
+struct zn_coefficients {
+	bool zn_found;
+	s32 prev_err;
+	s32 curr_err;
+	u32 t_prev_peak;
+	u32 period;
+	u32 k_ultimate;
+
+	s32 base_peak;
+	s32 base_trough;
+	s32 oscillation_count;
+	enum pivot_type prev_pivot;
+};
+
 /**
  * struct power_allocator_params - parameters for the power allocator governor
  * @allocated_tzp:	whether we have allocated tzp for this thermal zone and
@@ -65,6 +117,8 @@ static inline s64 div_frac(s64 x, s64 y)
  *					controlling for.
  * @sustainable_power:	Sustainable power (heat) that this thermal zone can
  *			dissipate
+ * @zn_coeffs:  Structure to hold information used by the Ziegler-Nichols
+ *              heuristic
  */
 struct power_allocator_params {
 	bool allocated_tzp;
@@ -73,6 +127,7 @@ struct power_allocator_params {
 	int trip_switch_on;
 	int trip_max_desired_temperature;
 	u32 sustainable_power;
+	struct zn_coefficients *zn_coeffs;
 };
 
 /**
@@ -196,6 +251,302 @@ static u32 get_sustainable_power(struct thermal_zone_device *tz,
 	return sustainable_power;
 }
 
+/**
+ * set_original_pid_coefficients() - Reset PID Coefficients in the Thermal Zone
+ *                                   to original values
+ * @tzp - Thermal Zone Parameters we want to update
+ *
+ */
+static inline void
+set_original_pid_coefficients(struct thermal_zone_params *tzp)
+{
+	static bool init = true;
+	static s32 k_po, k_pu, k_i, k_d, integral_cutoff;
+
+	if (init) {
+		k_po = tzp->k_po;
+		k_pu = tzp->k_pu;
+		k_i = tzp->k_i;
+		k_d = tzp->k_d;
+		integral_cutoff = tzp->integral_cutoff;
+		init = false;
+	} else {
+		tzp->k_po = k_po;
+		tzp->k_pu = k_pu;
+		tzp->k_i = k_i;
+		tzp->k_d = k_d;
+		tzp->integral_cutoff = integral_cutoff;
+	}
+}
+
+/**
+ * set_zn_pid_coefficients() - Calculate and set PID Coefficients based
+ *                             on Ziegler-Nichols Heuristic
+ * @tzp: thermal zone params to set
+ * @period: time taken for error to cycle 1 period
+ * @k_ultimate: the Ultimate Proportional Gain value at which
+ *              the error oscillates around the set-point
+ *
+ * This function sets the PID Coefficients of the thermal device
+ */
+static inline void set_zn_pid_coefficients(struct thermal_zone_params *tzp,
+					   u32 period, s32 k_ultimate)
+{
+	/* Convert time in ms for 1 cycle to cycles/s */
+	s32 freq = 1000 / period;
+
+	/* Make k_pu and k_po identical so it represents k_p */
+	tzp->k_pu = k_ultimate * 1 / 10;
+	tzp->k_po = tzp->k_pu;
+
+	tzp->k_i = freq / 2;
+	/* We want an integral term so if the value is 0, set it to 1 */
+	tzp->k_i = tzp->k_i > 0 ? tzp->k_i : 1;
+
+	tzp->k_d = (33 * freq) / 100;
+	/* We want an integral term so if the value is 0, set it to 1 */
+	tzp->k_d = tzp->k_d > 0 ? tzp->k_d : 1;
+}
+
+/**
+ * is_error_acceptable() - Check whether the error determined to be a pivot
+ *                         point is within the acceptable range
+ * @err: error value we are checking
+ * @base: the base_line value we are comparing against
+ *
+ * This function is used to determine whether our current pivot point is within
+ * the acceptable limits. The value of base is the first pivot point within
+ * this series of oscillations
+ *
+ * Return: boolean representing whether or not the error was within the acceptable
+ *         range
+ */
+static inline bool is_error_acceptable(s32 err, s32 base)
+{
+	/* Margin for error in milli-celcius */
+	const s32 MARGIN = 500;
+	s32 lower = abs(base) - MARGIN;
+	s32 upper = abs(base) + MARGIN;
+
+	if (lower < abs(err) && abs(err) < upper)
+		return true;
+	return false;
+}
+
+/**
+ * is_error_pivot() - Determine whether an error value is a pivot based on the
+ *                    previous and next error values
+ * @next_err        - the next error in a series
+ * @curr_err        - the current error value we are checking
+ * @prev_err        - the previous error in a series
+ * @peak_trough     - integer value to output what kind of pivot (if any)
+ *                    the error value is
+ *
+ * Determine whether or not the current value of error is a pivot and if it is
+ * a pivot, which type of pivot it is (peak or trough).
+ *
+ * Return: Bool representing whether the current value is a pivot point and
+ *         integer set to PEAK, TROUGH or MIDPOINT
+ */
+static inline bool is_error_pivot(s32 next_err, s32 curr_err, s32 prev_err,
+				  enum pivot_type *peak_trough)
+{
+	/*
+	 * Check whether curr_err is at it's highest value compared to its neighbours and that error
+	 * value is positive
+	 */
+	if (prev_err < curr_err && curr_err > next_err && curr_err > 0) {
+		*peak_trough = PEAK;
+		return true;
+	}
+	/*
+	 * Check whether curr_err is at it's lowest value compared to its neighbours and that error
+	 * value is negative
+	 */
+	if (prev_err > curr_err && curr_err < next_err && curr_err < 0) {
+		*peak_trough = TROUGH;
+		return true;
+	}
+	/* If the error is not a pivot then it must be somewhere between pivots */
+	*peak_trough = MIDPOINT;
+	return false;
+}
+
+/** update_oscillation_count() - Update the Oscillation Count for this set of pivots @curr_err
+ * - the current error value we are checking @base_pivot      - the amplitude we are comparing
+ *   against @peak_trough     - the type of pivot we are currently processing @zn_coefficients - the
+ *   data structure holding information used by the Ziegler-Nichols Hueristic
+ *
+ * Update the number of times we have oscillated based on our current error value being within the
+ * accepted range from the amplitude of previous pivots in this oscillation series.
+ *
+ * Return: Integer count of the number of oscillations
+ */
+static inline s32 update_oscillation_count(s32 curr_err, s32 *base_pivot,
+					   enum pivot_type peak_trough,
+					   struct zn_coefficients *zn_coeffs)
+{
+	if (is_error_acceptable(curr_err, *base_pivot) &&
+	    zn_coeffs->prev_pivot == -peak_trough) {
+		zn_coeffs->oscillation_count++;
+	} else {
+		zn_coeffs->oscillation_count = 0;
+		*base_pivot = curr_err;
+	}
+	zn_coeffs->prev_pivot = peak_trough;
+	return zn_coeffs->oscillation_count;
+}
+
+/** get_oscillation_count() - Update and get the number of times we have oscillated
+ * @curr_err    - the current error value we are checking
+ * @peak_trough - the type of pivot we are currently processing
+ * @zn_coefficients - the data structure holding information used by the
+ *                    Ziegler-Nichols Hueristic
+ *
+ * Return: The number of times we have oscillated for this k_ultimate
+ */
+static inline s32 get_oscillation_count(s32 curr_err,
+					enum pivot_type peak_trough,
+					struct zn_coefficients *zn_coeffs)
+{
+	s32 *base_pivot = 0;
+
+	if (peak_trough == PEAK)
+		base_pivot = &zn_coeffs->base_peak;
+	else if (peak_trough == TROUGH)
+		base_pivot = &zn_coeffs->base_trough;
+
+	return update_oscillation_count(curr_err, base_pivot, peak_trough,
+					zn_coeffs);
+}
+
+/** is_temperature_safe() - Check if the current temperature is within 10% of the target
+ *
+ * @current_temperature: Current reported temperature
+ * @control_temp:        Control Temperature we are targeting
+ *
+ * Return: True if current temperature is within 10% of the target, False otherwise
+ */
+static inline bool is_temperature_safe(int current_temperature,
+				       int control_temp)
+{
+	return (current_temperature - control_temp) < (control_temp / 10) ?
+		       true :
+		       false;
+}
+
+/** reset_ziegler_nichols() - Reset the Values used to Track Ziegler-Nichols
+ *
+ * @zn_coefficients - the data structure holding information used by the Ziegler-Nichols Hueristic
+ *
+ */
+static inline void reset_ziegler_nichols(struct zn_coefficients *zn_coeffs)
+{
+	zn_coeffs->zn_found = false;
+	zn_coeffs->k_ultimate = 10;
+	zn_coeffs->prev_err = 0;
+	zn_coeffs->curr_err = 0;
+	zn_coeffs->t_prev_peak = 0;
+	zn_coeffs->period = 0;
+	/* Manually input INT_MAX as a previous value so the system cannot use it accidentally */
+	zn_coeffs->oscillation_count = update_oscillation_count(
+		INT_MAX, &zn_coeffs->curr_err, PEAK, zn_coeffs);
+}
+
+/** ziegler_nichols() - Calculate the k_ultimate and period for the thermal device
+ *                      and use these values to calculate and set the PID coefficients based on
+ *                      the Ziegler-Nichols Heuristic
+ * @tz               - The thermal device we are operating on
+ * @next_err         - The next error value to be used for calculations
+ * @control_temp     - The temperature we are trying to target
+ *
+ * The Ziegler-Nichols PID Coefficient Tuning Method works by determining a K_Ultimate value. This
+ * is the largest K_P which yields a stable set of oscillations in error. By using historic and
+ * current values of error, this function attempts to determine whether or not it is oscillating,
+ * and increment the value of K_Ultimate accordingly.  Once it has determined that the system is
+ * oscillating, it calculates the time between "peaks" to determine its period
+ *
+ */
+static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
+				   int control_temp)
+{
+	struct power_allocator_params *params = tz->governor_data;
+	struct zn_coefficients *zn_coeffs = params->zn_coeffs;
+	const int NUMBER_OF_OSCILLATIONS = 10;
+
+	u32 t_now = (u32)(ktime_get_real_ns() / 1000000);
+	enum pivot_type peak_trough = MIDPOINT;
+	s32 oscillation_count = 0;
+	bool is_pivot;
+	bool is_safe =
+		is_temperature_safe((control_temp - next_err), control_temp);
+
+	if (tz->tzp->ziegler_nichols == ZN_RESET) {
+		reset_ziegler_nichols(zn_coeffs);
+		tz->tzp->ziegler_nichols = ZN_ON;
+	}
+
+	/* Override default PID Coefficients. These will be updated later according to the
+	 * Heuristic
+	 */
+	tz->tzp->k_po = zn_coeffs->k_ultimate;
+	tz->tzp->k_pu = zn_coeffs->k_ultimate;
+	tz->tzp->k_i = 0;
+	tz->tzp->k_d = 0;
+
+	if (!zn_coeffs->zn_found) {
+		/* Make sure that the previous errors have been logged and this isn't executed on
+		 * first pass
+		 */
+		if (zn_coeffs->curr_err != zn_coeffs->prev_err &&
+		    zn_coeffs->prev_err != 0) {
+			if (!is_safe)
+				goto set_zn;
+			is_pivot = is_error_pivot(next_err, zn_coeffs->curr_err,
+						  zn_coeffs->prev_err,
+						  &peak_trough);
+			if (is_pivot) {
+				oscillation_count = get_oscillation_count(
+					zn_coeffs->curr_err, peak_trough,
+					zn_coeffs);
+				if (oscillation_count >=
+				    NUMBER_OF_OSCILLATIONS) {
+					goto set_zn;
+				}
+				if (peak_trough == PEAK)
+					zn_coeffs->t_prev_peak = t_now;
+			}
+			if (!is_pivot || !oscillation_count)
+				zn_coeffs->k_ultimate += 10;
+		}
+		goto update_errors;
+	} else {
+		set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
+					zn_coeffs->k_ultimate);
+		tz->tzp->ziegler_nichols = ZN_OFF;
+	}
+	return;
+
+update_errors:
+	zn_coeffs->prev_err = zn_coeffs->curr_err;
+	zn_coeffs->curr_err = next_err;
+	return;
+
+set_zn:
+	if (zn_coeffs->t_prev_peak) {
+		zn_coeffs->zn_found = true;
+		zn_coeffs->period = abs(t_now - zn_coeffs->t_prev_peak);
+		set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
+					zn_coeffs->k_ultimate);
+		((struct power_allocator_params *)tz->governor_data)
+			->err_integral = 0;
+		tz->tzp->ziegler_nichols = ZN_OFF;
+	} else {
+		if (peak_trough == PEAK)
+			zn_coeffs->t_prev_peak = t_now;
+	}
+}
+
 /**
  * pid_controller() - PID controller
  * @tz:	thermal zone we are operating in
@@ -228,6 +579,26 @@ static u32 pid_controller(struct thermal_zone_device *tz,
 	sustainable_power = get_sustainable_power(tz, params, control_temp);
 
 	err = control_temp - tz->temperature;
+
+	switch (tz->tzp->ziegler_nichols) {
+	case ZN_ORIGINAL: {
+		set_original_pid_coefficients(tz->tzp);
+		tz->tzp->ziegler_nichols = ZN_OFF;
+		break;
+	}
+	case ZN_RESET: {
+		ziegler_nichols(tz, err, control_temp);
+		tz->tzp->ziegler_nichols = ZN_ON;
+		break;
+	}
+	case ZN_ON: {
+		ziegler_nichols(tz, err, control_temp);
+		break;
+	}
+	default:
+		break;
+	}
+
 	err = int_to_frac(err);
 
 	/* Calculate the proportional term */
@@ -375,6 +746,7 @@ static void divvy_up_power(u32 *req_power, u32 *max_power, int num_actors,
 	if (capped_extra_power > 0)
 		for (i = 0; i < num_actors; i++) {
 			u64 extra_range = (u64)extra_actor_power[i] * extra_power;
+
 			granted_power[i] += DIV_ROUND_CLOSEST_ULL(extra_range,
 							 capped_extra_power);
 		}
@@ -644,6 +1016,7 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
 	int ret;
 	struct power_allocator_params *params;
 	int control_temp;
+	struct zn_coefficients *zn_coeffs;
 
 	ret = check_power_actors(tz);
 	if (ret)
@@ -653,6 +1026,12 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
 	if (!params)
 		return -ENOMEM;
 
+	zn_coeffs = kzalloc(sizeof(*zn_coeffs), GFP_KERNEL);
+	if (!zn_coeffs)
+		return -ENOMEM;
+
+	params->zn_coeffs = zn_coeffs;
+
 	if (!tz->tzp) {
 		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
 		if (!tz->tzp) {
@@ -676,6 +1055,8 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
 			estimate_pid_constants(tz, tz->tzp->sustainable_power,
 					       params->trip_switch_on,
 					       control_temp);
+		/* Store the original PID coefficient values */
+		set_original_pid_coefficients(tz->tzp);
 	}
 
 	reset_pid_controller(params);
@@ -696,6 +1077,9 @@ static void power_allocator_unbind(struct thermal_zone_device *tz)
 
 	dev_dbg(&tz->device, "Unbinding from thermal zone %d\n", tz->id);
 
+	kfree(params->zn_coeffs);
+	params->zn_coeffs = NULL;
+
 	if (params->allocated_tzp) {
 		kfree(tz->tzp);
 		tz->tzp = NULL;
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index f154bada2906..d2f410a33995 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -342,6 +342,7 @@ create_s32_tzp_attr(k_po);
 create_s32_tzp_attr(k_pu);
 create_s32_tzp_attr(k_i);
 create_s32_tzp_attr(k_d);
+create_s32_tzp_attr(ziegler_nichols);
 create_s32_tzp_attr(integral_cutoff);
 create_s32_tzp_attr(slope);
 create_s32_tzp_attr(offset);
@@ -375,6 +376,7 @@ static struct attribute *thermal_zone_dev_attrs[] = {
 	&dev_attr_k_pu.attr,
 	&dev_attr_k_i.attr,
 	&dev_attr_k_d.attr,
+	&dev_attr_ziegler_nichols.attr,
 	&dev_attr_integral_cutoff.attr,
 	&dev_attr_slope.attr,
 	&dev_attr_offset.attr,
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index c314893970b3..ed8cd6a826ed 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -282,6 +282,13 @@ struct thermal_zone_params {
 	 * 		Used by thermal zone drivers (default 0).
 	 */
 	int offset;
+
+	/*
+	 * Ziegler-Nichols estimation setting. Allows the user to decide
+	 * whether to use original PID coefficients or calculate using
+	 * the Ziegler-Nichols algorithm
+	 */
+	s32 ziegler_nichols;
 };
 
 /**
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH][RFC 2/2] Remove sysfs entry
  2021-12-10 16:31 [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Chetankumar Mistry
@ 2021-12-10 16:31 ` Chetankumar Mistry
  2021-12-11  0:45 ` [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Randy Dunlap
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Chetankumar Mistry @ 2021-12-10 16:31 UTC (permalink / raw)
  To: linux-kernel
  Cc: lukasz.luba, rafael, daniel.lezcano, amitk, rui.zhang, linux-pm

The previous patch introduced a sysfs entry to track the
Ziegler-Nichols state.
This patch will remove it, if it is unwanted.

Signed-off-by: Chetankumar Mistry <chetan.mistry@arm.com>
---
 drivers/thermal/gov_power_allocator.c | 68 ++++++++++++++++++---------
 drivers/thermal/thermal_sysfs.c       |  2 -
 include/linux/thermal.h               |  7 ---
 3 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
index 7819c693ed1a..5be0dfb5f7fc 100644
--- a/drivers/thermal/gov_power_allocator.c
+++ b/drivers/thermal/gov_power_allocator.c
@@ -99,6 +99,8 @@ struct zn_coefficients {
 	s32 base_trough;
 	s32 oscillation_count;
 	enum pivot_type prev_pivot;
+
+	int zn_state;
 };
 
 /**
@@ -420,7 +422,31 @@ static inline s32 get_oscillation_count(s32 curr_err,
 					zn_coeffs);
 }
 
-/** is_temperature_safe() - Check if the current temperature is within 10% of the target
+/**
+ * get_zn_state() - Update and get the current Ziegler-Nichols State
+ * @tzp - The thermal zone params to check to determine the current state
+ * @zn_state    - The current state which should be returned if no changes are
+ * made
+ *
+ * Return: The next zieger-nichols state for this pass of the PID controller
+ */
+static inline int get_zn_state(struct thermal_zone_params *tzp, int zn_state)
+{
+	if (tzp->k_po == ZN_RESET && tzp->k_pu == ZN_RESET)
+		return ZN_RESET;
+
+	if (tzp->k_po == ZN_ORIGINAL && tzp->k_pu == ZN_ORIGINAL)
+		return ZN_ORIGINAL;
+
+	if (tzp->k_po == ZN_ON && tzp->k_pu == ZN_ON)
+		return ZN_ON;
+
+	return zn_state;
+}
+
+/**
+ * is_temperature_safe() - Check if the current temperature is within 10% of
+ *                         the target
  *
  * @current_temperature: Current reported temperature
  * @control_temp:        Control Temperature we are targeting
@@ -481,9 +507,21 @@ static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
 	bool is_safe =
 		is_temperature_safe((control_temp - next_err), control_temp);
 
-	if (tz->tzp->ziegler_nichols == ZN_RESET) {
+	zn_coeffs->zn_state = get_zn_state(tz->tzp, zn_coeffs->zn_state);
+	switch (zn_coeffs->zn_state) {
+	case ZN_ORIGINAL: {
+		set_original_pid_coefficients(tz->tzp);
+		zn_coeffs->zn_state = ZN_OFF;
+		return;
+	}
+	case ZN_RESET: {
 		reset_ziegler_nichols(zn_coeffs);
-		tz->tzp->ziegler_nichols = ZN_ON;
+		zn_coeffs->zn_state = ZN_ON;
+		break;
+	}
+
+	case ZN_OFF:
+		return;
 	}
 
 	/* Override default PID Coefficients. These will be updated later according to the
@@ -523,7 +561,7 @@ static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
 	} else {
 		set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
 					zn_coeffs->k_ultimate);
-		tz->tzp->ziegler_nichols = ZN_OFF;
+		zn_coeffs->zn_state = ZN_OFF;
 	}
 	return;
 
@@ -540,7 +578,7 @@ static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
 					zn_coeffs->k_ultimate);
 		((struct power_allocator_params *)tz->governor_data)
 			->err_integral = 0;
-		tz->tzp->ziegler_nichols = ZN_OFF;
+		zn_coeffs->zn_state = ZN_OFF;
 	} else {
 		if (peak_trough == PEAK)
 			zn_coeffs->t_prev_peak = t_now;
@@ -580,24 +618,7 @@ static u32 pid_controller(struct thermal_zone_device *tz,
 
 	err = control_temp - tz->temperature;
 
-	switch (tz->tzp->ziegler_nichols) {
-	case ZN_ORIGINAL: {
-		set_original_pid_coefficients(tz->tzp);
-		tz->tzp->ziegler_nichols = ZN_OFF;
-		break;
-	}
-	case ZN_RESET: {
-		ziegler_nichols(tz, err, control_temp);
-		tz->tzp->ziegler_nichols = ZN_ON;
-		break;
-	}
-	case ZN_ON: {
-		ziegler_nichols(tz, err, control_temp);
-		break;
-	}
-	default:
-		break;
-	}
+	ziegler_nichols(tz, err, control_temp);
 
 	err = int_to_frac(err);
 
@@ -1031,6 +1052,7 @@ static int power_allocator_bind(struct thermal_zone_device *tz)
 		return -ENOMEM;
 
 	params->zn_coeffs = zn_coeffs;
+	zn_coeffs->zn_state = ZN_ON;
 
 	if (!tz->tzp) {
 		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index d2f410a33995..f154bada2906 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -342,7 +342,6 @@ create_s32_tzp_attr(k_po);
 create_s32_tzp_attr(k_pu);
 create_s32_tzp_attr(k_i);
 create_s32_tzp_attr(k_d);
-create_s32_tzp_attr(ziegler_nichols);
 create_s32_tzp_attr(integral_cutoff);
 create_s32_tzp_attr(slope);
 create_s32_tzp_attr(offset);
@@ -376,7 +375,6 @@ static struct attribute *thermal_zone_dev_attrs[] = {
 	&dev_attr_k_pu.attr,
 	&dev_attr_k_i.attr,
 	&dev_attr_k_d.attr,
-	&dev_attr_ziegler_nichols.attr,
 	&dev_attr_integral_cutoff.attr,
 	&dev_attr_slope.attr,
 	&dev_attr_offset.attr,
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index ed8cd6a826ed..c314893970b3 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -282,13 +282,6 @@ struct thermal_zone_params {
 	 * 		Used by thermal zone drivers (default 0).
 	 */
 	int offset;
-
-	/*
-	 * Ziegler-Nichols estimation setting. Allows the user to decide
-	 * whether to use original PID coefficients or calculate using
-	 * the Ziegler-Nichols algorithm
-	 */
-	s32 ziegler_nichols;
 };
 
 /**
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic
  2021-12-10 16:31 [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Chetankumar Mistry
  2021-12-10 16:31 ` [PATCH][RFC 2/2] Remove sysfs entry Chetankumar Mistry
@ 2021-12-11  0:45 ` Randy Dunlap
  2021-12-11 15:42 ` kernel test robot
  2021-12-11 17:38 ` kernel test robot
  3 siblings, 0 replies; 6+ messages in thread
From: Randy Dunlap @ 2021-12-11  0:45 UTC (permalink / raw)
  To: Chetankumar Mistry, linux-kernel
  Cc: lukasz.luba, rafael, daniel.lezcano, amitk, rui.zhang, linux-pm

Hi,


On 12/10/21 08:31, Chetankumar Mistry wrote:
> Implement the Ziegler-Nichols Heuristic algorithm to better
> estimate the PID Coefficients for a running platform.
> The values are tuned to minimuse the amount of overshoot in

                          minimise
(as on next line)

> the temperature of the platform and subsequently minimise
> the number of switches for cdev states.
> 
> Signed-off-by: Chetankumar Mistry <chetan.mistry@arm.com>

There are lots of kernel-doc warnings caused by this patch:

gov_power_allocator.c:145: warning: No description found for return value of 'estimate_sustainable_power'
gov_power_allocator.c:233: warning: No description found for return value of 'get_sustainable_power'
gov_power_allocator.c:348: warning: contents before sections
gov_power_allocator.c:60: warning: Enum value 'PEAK' not described in enum 'pivot_type'
gov_power_allocator.c:60: warning: Enum value 'TROUGH' not described in enum 'pivot_type'
gov_power_allocator.c:60: warning: Enum value 'MIDPOINT' not described in enum 'pivot_type'
gov_power_allocator.c:72: warning: Enum value 'ZN_ORIGINAL' not described in enum 'ZN_VALUES'
gov_power_allocator.c:72: warning: Enum value 'ZN_OFF' not described in enum 'ZN_VALUES'
gov_power_allocator.c:72: warning: Enum value 'ZN_ON' not described in enum 'ZN_VALUES'
gov_power_allocator.c:72: warning: Enum value 'ZN_RESET' not described in enum 'ZN_VALUES'
gov_power_allocator.c:102: warning: Function parameter or member 'zn_found' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'prev_err' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'curr_err' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 't_prev_peak' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'period' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'k_ultimate' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'base_peak' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'base_trough' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'oscillation_count' not described in 'zn_coefficients'
gov_power_allocator.c:102: warning: Function parameter or member 'prev_pivot' not described in 'zn_coefficients'
gov_power_allocator.c:145: warning: No description found for return value of 'estimate_sustainable_power'
gov_power_allocator.c:233: warning: No description found for return value of 'get_sustainable_power'
gov_power_allocator.c:262: warning: Function parameter or member 'tzp' not described in 'set_original_pid_coefficients'
gov_power_allocator.c:348: warning: contents before sections
gov_power_allocator.c:353: warning: Function parameter or member 'next_err' not described in 'is_error_pivot'
gov_power_allocator.c:353: warning: Function parameter or member 'curr_err' not described in 'is_error_pivot'
gov_power_allocator.c:353: warning: Function parameter or member 'prev_err' not described in 'is_error_pivot'
gov_power_allocator.c:353: warning: Function parameter or member 'peak_trough' not described in 'is_error_pivot'

Many of these are due to the use of '-' as a kernel-doc separator character
when it should be ':'.  See examples below.

> ---
>  drivers/thermal/gov_power_allocator.c | 384 ++++++++++++++++++++++++++
>  drivers/thermal/thermal_sysfs.c       |   2 +
>  include/linux/thermal.h               |   7 +
>  3 files changed, 393 insertions(+)
> 
> diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
> index 13e375751d22..7819c693ed1a 100644
> --- a/drivers/thermal/gov_power_allocator.c
> +++ b/drivers/thermal/gov_power_allocator.c
> @@ -49,6 +49,58 @@ static inline s64 div_frac(s64 x, s64 y)
>  	return div_s64(x << FRAC_BITS, y);
>  }
>  
> +/**
> + * enum pivot_type - Values representing what type of pivot the current error
> + *                   value is
> + * @PEAK     - The current error is a peak
> + * @TROUGH   - The current error is a trough
> + * @MIDPOINT - The current error is neither a peak or trough and is some midpoint
> + *             in between

 * @PEAK:     The current error is a peak
 * @TROUGH:   The current error is a trough
 * @MIDPOINT: The current error is neither a peak or trough and is some midpoint
 *            in between


[snip]

>  /**
>   * struct power_allocator_params - parameters for the power allocator governor
>   * @allocated_tzp:	whether we have allocated tzp for this thermal zone and
> @@ -65,6 +117,8 @@ static inline s64 div_frac(s64 x, s64 y)
>   *					controlling for.
>   * @sustainable_power:	Sustainable power (heat) that this thermal zone can
>   *			dissipate
> + * @zn_coeffs:  Structure to hold information used by the Ziegler-Nichols
> + *              heuristic
>   */
>  struct power_allocator_params {
>  	bool allocated_tzp;
> @@ -73,6 +127,7 @@ struct power_allocator_params {
>  	int trip_switch_on;
>  	int trip_max_desired_temperature;
>  	u32 sustainable_power;
> +	struct zn_coefficients *zn_coeffs;
>  };
>  
>  /**
> @@ -196,6 +251,302 @@ static u32 get_sustainable_power(struct thermal_zone_device *tz,
>  	return sustainable_power;
>  }
>  
> +/**
> + * set_original_pid_coefficients() - Reset PID Coefficients in the Thermal Zone
> + *                                   to original values
> + * @tzp - Thermal Zone Parameters we want to update

 * @tzp: Thermal Zone Parameters we want to update

> + *
> + */
> +static inline void
> +set_original_pid_coefficients(struct thermal_zone_params *tzp)

Bad style for Linux; instead use

static inline void set_original_pid_coefficients(struct thermal_zone_params *tzp)
{


> +{
> +	static bool init = true;
> +	static s32 k_po, k_pu, k_i, k_d, integral_cutoff;
> +
> +	if (init) {
> +		k_po = tzp->k_po;
> +		k_pu = tzp->k_pu;
> +		k_i = tzp->k_i;
> +		k_d = tzp->k_d;
> +		integral_cutoff = tzp->integral_cutoff;
> +		init = false;
> +	} else {
> +		tzp->k_po = k_po;
> +		tzp->k_pu = k_pu;
> +		tzp->k_i = k_i;
> +		tzp->k_d = k_d;
> +		tzp->integral_cutoff = integral_cutoff;
> +	}
> +}
> +

[snip]


> +/** update_oscillation_count() - Update the Oscillation Count for this set of pivots @curr_err

Please put "/**" on a separate line:

/**
 * update_oscillation_count() - Update the Oscillation Count for this set of pivots @curr_err


> + * - the current error value we are checking @base_pivot      - the amplitude we are comparing
> + *   against @peak_trough     - the type of pivot we are currently processing @zn_coefficients - the
> + *   data structure holding information used by the Ziegler-Nichols Hueristic
> + *
> + * Update the number of times we have oscillated based on our current error value being within the
> + * accepted range from the amplitude of previous pivots in this oscillation series.
> + *
> + * Return: Integer count of the number of oscillations
> + */
> +static inline s32 update_oscillation_count(s32 curr_err, s32 *base_pivot,
> +					   enum pivot_type peak_trough,
> +					   struct zn_coefficients *zn_coeffs)
> +{
> +	if (is_error_acceptable(curr_err, *base_pivot) &&
> +	    zn_coeffs->prev_pivot == -peak_trough) {
> +		zn_coeffs->oscillation_count++;
> +	} else {
> +		zn_coeffs->oscillation_count = 0;
> +		*base_pivot = curr_err;
> +	}
> +	zn_coeffs->prev_pivot = peak_trough;
> +	return zn_coeffs->oscillation_count;
> +}
> +
> +/** get_oscillation_count() - Update and get the number of times we have oscillated

Ditto.

> + * @curr_err    - the current error value we are checking
> + * @peak_trough - the type of pivot we are currently processing
> + * @zn_coefficients - the data structure holding information used by the
> + *                    Ziegler-Nichols Hueristic
> + *
> + * Return: The number of times we have oscillated for this k_ultimate
> + */
> +static inline s32 get_oscillation_count(s32 curr_err,
> +					enum pivot_type peak_trough,
> +					struct zn_coefficients *zn_coeffs)
> +{
> +	s32 *base_pivot = 0;
> +
> +	if (peak_trough == PEAK)
> +		base_pivot = &zn_coeffs->base_peak;
> +	else if (peak_trough == TROUGH)
> +		base_pivot = &zn_coeffs->base_trough;
> +
> +	return update_oscillation_count(curr_err, base_pivot, peak_trough,
> +					zn_coeffs);
> +}
> +
> +/** is_temperature_safe() - Check if the current temperature is within 10% of the target

Ditto.

> + *
> + * @current_temperature: Current reported temperature
> + * @control_temp:        Control Temperature we are targeting
> + *
> + * Return: True if current temperature is within 10% of the target, False otherwise
> + */
> +static inline bool is_temperature_safe(int current_temperature,
> +				       int control_temp)
> +{
> +	return (current_temperature - control_temp) < (control_temp / 10) ?
> +		       true :
> +		       false;
> +}
> +
> +/** reset_ziegler_nichols() - Reset the Values used to Track Ziegler-Nichols

Ditto.

> + *
> + * @zn_coefficients - the data structure holding information used by the Ziegler-Nichols Hueristic
> + *
> + */
> +static inline void reset_ziegler_nichols(struct zn_coefficients *zn_coeffs)
> +{
> +	zn_coeffs->zn_found = false;
> +	zn_coeffs->k_ultimate = 10;
> +	zn_coeffs->prev_err = 0;
> +	zn_coeffs->curr_err = 0;
> +	zn_coeffs->t_prev_peak = 0;
> +	zn_coeffs->period = 0;
> +	/* Manually input INT_MAX as a previous value so the system cannot use it accidentally */
> +	zn_coeffs->oscillation_count = update_oscillation_count(
> +		INT_MAX, &zn_coeffs->curr_err, PEAK, zn_coeffs);
> +}
> +
> +/** ziegler_nichols() - Calculate the k_ultimate and period for the thermal device

Ditto.

> + *                      and use these values to calculate and set the PID coefficients based on
> + *                      the Ziegler-Nichols Heuristic
> + * @tz               - The thermal device we are operating on
> + * @next_err         - The next error value to be used for calculations
> + * @control_temp     - The temperature we are trying to target
> + *
> + * The Ziegler-Nichols PID Coefficient Tuning Method works by determining a K_Ultimate value. This
> + * is the largest K_P which yields a stable set of oscillations in error. By using historic and
> + * current values of error, this function attempts to determine whether or not it is oscillating,
> + * and increment the value of K_Ultimate accordingly.  Once it has determined that the system is
> + * oscillating, it calculates the time between "peaks" to determine its period
> + *
> + */
> +static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
> +				   int control_temp)
> +{
> +	struct power_allocator_params *params = tz->governor_data;
> +	struct zn_coefficients *zn_coeffs = params->zn_coeffs;
> +	const int NUMBER_OF_OSCILLATIONS = 10;
> +
> +	u32 t_now = (u32)(ktime_get_real_ns() / 1000000);
> +	enum pivot_type peak_trough = MIDPOINT;
> +	s32 oscillation_count = 0;
> +	bool is_pivot;
> +	bool is_safe =
> +		is_temperature_safe((control_temp - next_err), control_temp);
> +
> +	if (tz->tzp->ziegler_nichols == ZN_RESET) {
> +		reset_ziegler_nichols(zn_coeffs);
> +		tz->tzp->ziegler_nichols = ZN_ON;
> +	}
> +
> +	/* Override default PID Coefficients. These will be updated later according to the
> +	 * Heuristic
> +	 */
> +	tz->tzp->k_po = zn_coeffs->k_ultimate;
> +	tz->tzp->k_pu = zn_coeffs->k_ultimate;
> +	tz->tzp->k_i = 0;
> +	tz->tzp->k_d = 0;
> +
> +	if (!zn_coeffs->zn_found) {
> +		/* Make sure that the previous errors have been logged and this isn't executed on
> +		 * first pass
> +		 */
> +		if (zn_coeffs->curr_err != zn_coeffs->prev_err &&
> +		    zn_coeffs->prev_err != 0) {
> +			if (!is_safe)
> +				goto set_zn;
> +			is_pivot = is_error_pivot(next_err, zn_coeffs->curr_err,
> +						  zn_coeffs->prev_err,
> +						  &peak_trough);
> +			if (is_pivot) {
> +				oscillation_count = get_oscillation_count(
> +					zn_coeffs->curr_err, peak_trough,
> +					zn_coeffs);
> +				if (oscillation_count >=
> +				    NUMBER_OF_OSCILLATIONS) {
> +					goto set_zn;
> +				}
> +				if (peak_trough == PEAK)
> +					zn_coeffs->t_prev_peak = t_now;
> +			}
> +			if (!is_pivot || !oscillation_count)
> +				zn_coeffs->k_ultimate += 10;
> +		}
> +		goto update_errors;
> +	} else {
> +		set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
> +					zn_coeffs->k_ultimate);
> +		tz->tzp->ziegler_nichols = ZN_OFF;
> +	}
> +	return;
> +
> +update_errors:
> +	zn_coeffs->prev_err = zn_coeffs->curr_err;
> +	zn_coeffs->curr_err = next_err;
> +	return;
> +
> +set_zn:
> +	if (zn_coeffs->t_prev_peak) {
> +		zn_coeffs->zn_found = true;
> +		zn_coeffs->period = abs(t_now - zn_coeffs->t_prev_peak);
> +		set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
> +					zn_coeffs->k_ultimate);
> +		((struct power_allocator_params *)tz->governor_data)
> +			->err_integral = 0;
> +		tz->tzp->ziegler_nichols = ZN_OFF;
> +	} else {
> +		if (peak_trough == PEAK)
> +			zn_coeffs->t_prev_peak = t_now;
> +	}
> +}

[snip]


thanks.
-- 
~Randy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic
@ 2021-12-11  4:28 kernel test robot
  0 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2021-12-11  4:28 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 9530 bytes --]

CC: kbuild-all(a)lists.01.org
In-Reply-To: <20211210163141.213106-1-chetan.mistry@arm.com>
References: <20211210163141.213106-1-chetan.mistry@arm.com>
TO: Chetankumar Mistry <chetan.mistry@arm.com>

Hi Chetankumar,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on rafael-pm/thermal]
[also build test WARNING on linux/master linus/master v5.16-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chetankumar-Mistry/Implement-Ziegler-Nichols-Heuristic/20211211-003430
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
:::::: branch date: 12 hours ago
:::::: commit date: 12 hours ago
config: x86_64-randconfig-m001-20211210 (https://download.01.org/0day-ci/archive/20211211/202112111243.7n3OVH4u-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
drivers/thermal/gov_power_allocator.c:1031 power_allocator_bind() warn: possible memory leak of 'params'

vim +/params +1031 drivers/thermal/gov_power_allocator.c

7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1003  
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1004  /**
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1005   * power_allocator_bind() - bind the power_allocator governor to a thermal zone
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1006   * @tz:	thermal zone to bind it to
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1007   *
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1008   * Initialize the PID controller parameters and bind it to the thermal
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1009   * zone.
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1010   *
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1011   * Return: 0 on success, or -ENOMEM if we ran out of memory, or -EINVAL
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1012   * when there are unsupported cooling devices in the @tz.
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1013   */
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1014  static int power_allocator_bind(struct thermal_zone_device *tz)
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1015  {
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1016  	int ret;
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1017  	struct power_allocator_params *params;
e055bb0f9a6e5c drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1018  	int control_temp;
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1019  	struct zn_coefficients *zn_coeffs;
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1020  
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1021  	ret = check_power_actors(tz);
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1022  	if (ret)
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1023  		return ret;
7a583405f24bff drivers/thermal/gov_power_allocator.c Lukasz Luba        2021-01-19  1024  
cf736ea6f902c2 drivers/thermal/power_allocator.c     Dmitry Torokhov    2015-08-04  1025  	params = kzalloc(sizeof(*params), GFP_KERNEL);
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1026  	if (!params)
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1027  		return -ENOMEM;
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1028  
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1029  	zn_coeffs = kzalloc(sizeof(*zn_coeffs), GFP_KERNEL);
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1030  	if (!zn_coeffs)
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10 @1031  		return -ENOMEM;
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1032  
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1033  	params->zn_coeffs = zn_coeffs;
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1034  
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1035  	if (!tz->tzp) {
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1036  		tz->tzp = kzalloc(sizeof(*tz->tzp), GFP_KERNEL);
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1037  		if (!tz->tzp) {
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1038  			ret = -ENOMEM;
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1039  			goto free_params;
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1040  		}
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1041  
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1042  		params->allocated_tzp = true;
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1043  	}
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1044  
e055bb0f9a6e5c drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1045  	if (!tz->tzp->sustainable_power)
e055bb0f9a6e5c drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1046  		dev_warn(&tz->device, "power_allocator: sustainable_power will be estimated\n");
e055bb0f9a6e5c drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1047  
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1048  	get_governor_trips(tz, params);
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1049  
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1050  	if (tz->trips > 0) {
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1051  		ret = tz->ops->get_trip_temp(tz,
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1052  					params->trip_max_desired_temperature,
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1053  					&control_temp);
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1054  		if (!ret)
e055bb0f9a6e5c drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1055  			estimate_pid_constants(tz, tz->tzp->sustainable_power,
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1056  					       params->trip_switch_on,
90a996544946d1 drivers/thermal/gov_power_allocator.c Lukasz Luba        2020-11-24  1057  					       control_temp);
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1058  		/* Store the original PID coefficient values */
91f16d6a958fcd drivers/thermal/gov_power_allocator.c Chetankumar Mistry 2021-12-10  1059  		set_original_pid_coefficients(tz->tzp);
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1060  	}
8b7b390f805f09 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1061  
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1062  	reset_pid_controller(params);
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1063  
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1064  	tz->governor_data = params;
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1065  
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1066  	return 0;
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1067  
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1068  free_params:
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1069  	kfree(params);
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1070  
f5cbb182586ea3 drivers/thermal/power_allocator.c     Javi Merino        2015-09-14  1071  	return ret;
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1072  }
6b775e870c56c5 drivers/thermal/power_allocator.c     Javi Merino        2015-03-02  1073  

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic
  2021-12-10 16:31 [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Chetankumar Mistry
  2021-12-10 16:31 ` [PATCH][RFC 2/2] Remove sysfs entry Chetankumar Mistry
  2021-12-11  0:45 ` [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Randy Dunlap
@ 2021-12-11 15:42 ` kernel test robot
  2021-12-11 17:38 ` kernel test robot
  3 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2021-12-11 15:42 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 5640 bytes --]

Hi Chetankumar,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on rafael-pm/thermal]
[also build test ERROR on linux/master linus/master v5.16-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chetankumar-Mistry/Implement-Ziegler-Nichols-Heuristic/20211211-003430
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
config: i386-randconfig-r034-20211210 (https://download.01.org/0day-ci/archive/20211211/202112112254.HX7rf2NF-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/91f16d6a958fcd818cd8649e7d8d92958ada8c2f
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Chetankumar-Mistry/Implement-Ziegler-Nichols-Heuristic/20211211-003430
        git checkout 91f16d6a958fcd818cd8649e7d8d92958ada8c2f
        # save the config file to linux build tree
        mkdir build_dir
        make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   ld: drivers/thermal/gov_power_allocator.o: in function `ziegler_nichols':
>> drivers/thermal/gov_power_allocator.c:477: undefined reference to `__udivdi3'


vim +477 drivers/thermal/gov_power_allocator.c

   455	
   456	/** ziegler_nichols() - Calculate the k_ultimate and period for the thermal device
   457	 *                      and use these values to calculate and set the PID coefficients based on
   458	 *                      the Ziegler-Nichols Heuristic
   459	 * @tz               - The thermal device we are operating on
   460	 * @next_err         - The next error value to be used for calculations
   461	 * @control_temp     - The temperature we are trying to target
   462	 *
   463	 * The Ziegler-Nichols PID Coefficient Tuning Method works by determining a K_Ultimate value. This
   464	 * is the largest K_P which yields a stable set of oscillations in error. By using historic and
   465	 * current values of error, this function attempts to determine whether or not it is oscillating,
   466	 * and increment the value of K_Ultimate accordingly.  Once it has determined that the system is
   467	 * oscillating, it calculates the time between "peaks" to determine its period
   468	 *
   469	 */
   470	static inline void ziegler_nichols(struct thermal_zone_device *tz, s32 next_err,
   471					   int control_temp)
   472	{
   473		struct power_allocator_params *params = tz->governor_data;
   474		struct zn_coefficients *zn_coeffs = params->zn_coeffs;
   475		const int NUMBER_OF_OSCILLATIONS = 10;
   476	
 > 477		u32 t_now = (u32)(ktime_get_real_ns() / 1000000);
   478		enum pivot_type peak_trough = MIDPOINT;
   479		s32 oscillation_count = 0;
   480		bool is_pivot;
   481		bool is_safe =
   482			is_temperature_safe((control_temp - next_err), control_temp);
   483	
   484		if (tz->tzp->ziegler_nichols == ZN_RESET) {
   485			reset_ziegler_nichols(zn_coeffs);
   486			tz->tzp->ziegler_nichols = ZN_ON;
   487		}
   488	
   489		/* Override default PID Coefficients. These will be updated later according to the
   490		 * Heuristic
   491		 */
   492		tz->tzp->k_po = zn_coeffs->k_ultimate;
   493		tz->tzp->k_pu = zn_coeffs->k_ultimate;
   494		tz->tzp->k_i = 0;
   495		tz->tzp->k_d = 0;
   496	
   497		if (!zn_coeffs->zn_found) {
   498			/* Make sure that the previous errors have been logged and this isn't executed on
   499			 * first pass
   500			 */
   501			if (zn_coeffs->curr_err != zn_coeffs->prev_err &&
   502			    zn_coeffs->prev_err != 0) {
   503				if (!is_safe)
   504					goto set_zn;
   505				is_pivot = is_error_pivot(next_err, zn_coeffs->curr_err,
   506							  zn_coeffs->prev_err,
   507							  &peak_trough);
   508				if (is_pivot) {
   509					oscillation_count = get_oscillation_count(
   510						zn_coeffs->curr_err, peak_trough,
   511						zn_coeffs);
   512					if (oscillation_count >=
   513					    NUMBER_OF_OSCILLATIONS) {
   514						goto set_zn;
   515					}
   516					if (peak_trough == PEAK)
   517						zn_coeffs->t_prev_peak = t_now;
   518				}
   519				if (!is_pivot || !oscillation_count)
   520					zn_coeffs->k_ultimate += 10;
   521			}
   522			goto update_errors;
   523		} else {
   524			set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
   525						zn_coeffs->k_ultimate);
   526			tz->tzp->ziegler_nichols = ZN_OFF;
   527		}
   528		return;
   529	
   530	update_errors:
   531		zn_coeffs->prev_err = zn_coeffs->curr_err;
   532		zn_coeffs->curr_err = next_err;
   533		return;
   534	
   535	set_zn:
   536		if (zn_coeffs->t_prev_peak) {
   537			zn_coeffs->zn_found = true;
   538			zn_coeffs->period = abs(t_now - zn_coeffs->t_prev_peak);
   539			set_zn_pid_coefficients(tz->tzp, zn_coeffs->period,
   540						zn_coeffs->k_ultimate);
   541			((struct power_allocator_params *)tz->governor_data)
   542				->err_integral = 0;
   543			tz->tzp->ziegler_nichols = ZN_OFF;
   544		} else {
   545			if (peak_trough == PEAK)
   546				zn_coeffs->t_prev_peak = t_now;
   547		}
   548	}
   549	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic
  2021-12-10 16:31 [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Chetankumar Mistry
                   ` (2 preceding siblings ...)
  2021-12-11 15:42 ` kernel test robot
@ 2021-12-11 17:38 ` kernel test robot
  3 siblings, 0 replies; 6+ messages in thread
From: kernel test robot @ 2021-12-11 17:38 UTC (permalink / raw)
  To: kbuild-all

[-- Attachment #1: Type: text/plain, Size: 1900 bytes --]

Hi Chetankumar,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on rafael-pm/thermal]
[also build test ERROR on linux/master linus/master v5.16-rc4]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Chetankumar-Mistry/Implement-Ziegler-Nichols-Heuristic/20211211-003430
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git thermal
config: mips-allmodconfig (https://download.01.org/0day-ci/archive/20211212/202112120159.DJxXDBRN-lkp(a)intel.com/config)
compiler: mips-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/0day-ci/linux/commit/91f16d6a958fcd818cd8649e7d8d92958ada8c2f
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Chetankumar-Mistry/Implement-Ziegler-Nichols-Heuristic/20211211-003430
        git checkout 91f16d6a958fcd818cd8649e7d8d92958ada8c2f
        # save the config file to linux build tree
        mkdir build_dir
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=mips SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   mips-linux-ld: drivers/thermal/gov_power_allocator.o: in function `ziegler_nichols':
>> gov_power_allocator.c:(.text.ziegler_nichols+0x18c): undefined reference to `__udivdi3'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-12-11 17:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-10 16:31 [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Chetankumar Mistry
2021-12-10 16:31 ` [PATCH][RFC 2/2] Remove sysfs entry Chetankumar Mistry
2021-12-11  0:45 ` [PATCH][RFC 1/2] Implement Ziegler-Nichols Heuristic Randy Dunlap
2021-12-11 15:42 ` kernel test robot
2021-12-11 17:38 ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2021-12-11  4:28 kernel test robot

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.