linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] devfreq: improve devfreq statistics counting
       [not found] <CGME20191205145542eucas1p2b39536c9c186d5656f39d214baa32670@eucas1p2.samsung.com>
@ 2019-12-05 14:55 ` Kamil Konieczny
       [not found]   ` <CGME20191205145543eucas1p11798d11e91f6a691d5989db7cba81b96@eucas1p1.samsung.com>
                     ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Kamil Konieczny @ 2019-12-05 14:55 UTC (permalink / raw)
  To: k.konieczny
  Cc: Bartlomiej Zolnierkiewicz, Chanwoo Choi, Krzysztof Kozlowski,
	Kyungmin Park, linux-kernel, linux-pm, Marek Szyprowski,
	MyungJoo Ham

Hi,

this patch series tries to improve devfreq statistics:

- do conversion to use 64-bit jiffies for storing elapsed time and prevent
  counters overflow,

- add ability to reset statistics using sysfs,

- move statistics data to separate structure for improved code
  readability and maintenance.

Changes in v3:
- changed types of cur_time and last_stats_updated to u64 as this is
  returned by get_jiffies_64() in 1/3
- add checks for zero in input and clear stats only when zero is written
  to trans_stats
- change documentation of trans_stat in sysfs
- removed freq_table and max_state from struct devfreq_stats as they are
  already present in struct devfreq_dev_profile
- renamed last_stat_updated to last_update, as 'stat' is already present
  in struct devfreq_stats
- define struct devfreq_stats stats; in devfreq as there is only one
  stats per devfreq
- improve descriptions of devfreq_stats and stats
- use profile instead of devfreq->profile in devfreq_add_device, as this
  var is already parameter
- added Reviewed-by: Matthias Kaehlcke <mka@chromium.org> to 3/3

Changes in v2:
- added Acked-by to first patch
- dropped spinlock patch, there is mutex used for protecting stats data
- rewrite clearing statistics, suggested by Chanwoo Choi: reuse
  trans_stats sysfs file, any write to it will clear devfreq stats
- dropped change var name last_stat_updated
- squashed three last patches into one, as it turned out that freq_table
  from devfreq_profile is used by other drivers
- rebased on linux-next

Kamil Konieczny (3):
  devfreq: change time stats to 64-bit
  devfreq: add clearing transitions stats
  devfreq: move statistics to separate struct

 Documentation/ABI/testing/sysfs-class-devfreq | 11 ++-
 drivers/devfreq/devfreq.c                     | 92 ++++++++++++-------
 include/linux/devfreq.h                       | 26 ++++--
 3 files changed, 83 insertions(+), 46 deletions(-)

-- 
2.24.0


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

* [PATCH v3 1/3] devfreq: change time stats to 64-bit
       [not found]   ` <CGME20191205145543eucas1p11798d11e91f6a691d5989db7cba81b96@eucas1p1.samsung.com>
@ 2019-12-05 14:55     ` Kamil Konieczny
  2019-12-06  1:37       ` Chanwoo Choi
  0 siblings, 1 reply; 11+ messages in thread
From: Kamil Konieczny @ 2019-12-05 14:55 UTC (permalink / raw)
  To: k.konieczny
  Cc: Bartlomiej Zolnierkiewicz, Chanwoo Choi, Krzysztof Kozlowski,
	Kyungmin Park, linux-kernel, linux-pm, Marek Szyprowski,
	MyungJoo Ham

Change time stats counting to bigger type by using 64-bit jiffies.
This will make devfreq stats code look similar to cpufreq stats and
prevents overflow (for HZ = 1000 after 49.7 days).

Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
Acked-by: Chanwoo Choi <cw00.choi@samsung.com>
---
Changes in v3:
- changed types of cur_time and last_stats_updated to u64 as this is
  returned by get_jiffies_64()

Changes in v2:
- added Acked-by, rebased on linux-next
---
 drivers/devfreq/devfreq.c | 14 +++++++-------
 include/linux/devfreq.h   |  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index bdeb4189c978..abecadeb3dc2 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -199,10 +199,10 @@ static int set_freq_table(struct devfreq *devfreq)
 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 {
 	int lev, prev_lev, ret = 0;
-	unsigned long cur_time;
+	u64 cur_time;
 
 	lockdep_assert_held(&devfreq->lock);
-	cur_time = jiffies;
+	cur_time = get_jiffies_64();
 
 	/* Immediately exit if previous_freq is not initialized yet. */
 	if (!devfreq->previous_freq)
@@ -525,7 +525,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
 			msecs_to_jiffies(devfreq->profile->polling_ms));
 
 out_update:
-	devfreq->last_stat_updated = jiffies;
+	devfreq->last_stat_updated = get_jiffies_64();
 	devfreq->stop_polling = false;
 
 	if (devfreq->profile->get_cur_freq &&
@@ -748,7 +748,7 @@ struct devfreq *devfreq_add_device(struct device *dev,
 
 	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
 			devfreq->profile->max_state,
-			sizeof(unsigned long),
+			sizeof(*devfreq->time_in_state),
 			GFP_KERNEL);
 	if (!devfreq->time_in_state) {
 		mutex_unlock(&devfreq->lock);
@@ -756,7 +756,7 @@ struct devfreq *devfreq_add_device(struct device *dev,
 		goto err_devfreq;
 	}
 
-	devfreq->last_stat_updated = jiffies;
+	devfreq->last_stat_updated = get_jiffies_64();
 
 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
 
@@ -1470,8 +1470,8 @@ static ssize_t trans_stat_show(struct device *dev,
 		for (j = 0; j < max_state; j++)
 			len += sprintf(buf + len, "%10u",
 				devfreq->trans_table[(i * max_state) + j]);
-		len += sprintf(buf + len, "%10u\n",
-			jiffies_to_msecs(devfreq->time_in_state[i]));
+		len += sprintf(buf + len, "%10llu\n", (u64)
+			jiffies64_to_msecs(devfreq->time_in_state[i]));
 	}
 
 	len += sprintf(buf + len, "Total transition : %u\n",
diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
index 2bae9ed3c783..41f15e7a22b8 100644
--- a/include/linux/devfreq.h
+++ b/include/linux/devfreq.h
@@ -174,8 +174,8 @@ struct devfreq {
 	/* information for device frequency transition */
 	unsigned int total_trans;
 	unsigned int *trans_table;
-	unsigned long *time_in_state;
-	unsigned long last_stat_updated;
+	u64 *time_in_state;
+	u64 last_stat_updated;
 
 	struct srcu_notifier_head transition_notifier_list;
 };
-- 
2.24.0


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

* [PATCH v3 2/3] devfreq: add clearing transitions stats
       [not found]   ` <CGME20191205145543eucas1p1a317647203c47be07bbcee7867fb3e1e@eucas1p1.samsung.com>
@ 2019-12-05 14:55     ` Kamil Konieczny
  2019-12-06  2:01       ` Chanwoo Choi
  0 siblings, 1 reply; 11+ messages in thread
From: Kamil Konieczny @ 2019-12-05 14:55 UTC (permalink / raw)
  To: k.konieczny
  Cc: Andrew Donnellan, Bartlomiej Zolnierkiewicz, Chanwoo Choi,
	Greg Kroah-Hartman, Krzysztof Kozlowski, Kyungmin Park,
	linux-kernel, linux-pm, Marek Szyprowski, Mauro Carvalho Chehab,
	MyungJoo Ham, Rafael J. Wysocki

Add clearing transition table and time in states devfreq statistics
by writing 0 (zero) to trans_stat file in devfreq sysfs. An example use
is like following:

echo 0 > /sys/class/devfreq/devfreqX/trans_stat

Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
---
Changes in v3:
- add checks for zero in input and clear stats only when zero is written
  to trans_stats
- change documentation of trans_stat in sysfs

Changes in v2:
- instead of creating new sysfs file, add new functionality to trans_stat
  and clear stats when anything is writen to it
---
 Documentation/ABI/testing/sysfs-class-devfreq | 11 +++++---
 drivers/devfreq/devfreq.c                     | 27 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-devfreq b/Documentation/ABI/testing/sysfs-class-devfreq
index 75897e2fde43..9758eb85ade3 100644
--- a/Documentation/ABI/testing/sysfs-class-devfreq
+++ b/Documentation/ABI/testing/sysfs-class-devfreq
@@ -55,12 +55,15 @@ What:		/sys/class/devfreq/.../trans_stat
 Date:		October 2012
 Contact:	MyungJoo Ham <myungjoo.ham@samsung.com>
 Description:
-		This ABI shows the statistics of devfreq behavior on a
-		specific device. It shows the time spent in each state and
-		the number of transitions between states.
+		This ABI shows or clears the statistics of devfreq behavior
+		on a specific device. It shows the time spent in each state
+		and the number of transitions between states.
 		In order to activate this ABI, the devfreq target device
 		driver should provide the list of available frequencies
-		with its profile.
+		with its profile. If need to reset the statistics of devfreq
+		behavior on a specific device, enter 0(zero) to 'trans_stat'
+		as following:
+			echo 0 > /sys/class/devfreq/.../trans_stat
 
 What:		/sys/class/devfreq/.../userspace/set_freq
 Date:		September 2011
diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index abecadeb3dc2..218eb64d7f28 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -1478,7 +1478,32 @@ static ssize_t trans_stat_show(struct device *dev,
 					devfreq->total_trans);
 	return len;
 }
-static DEVICE_ATTR_RO(trans_stat);
+
+static ssize_t trans_stat_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct devfreq *df = to_devfreq(dev);
+	unsigned int cnt = df->profile->max_state;
+	int err, value;
+
+	if (cnt == 0)
+		return count;
+
+	err = kstrtoint(buf, 10, &value);
+	if (err || value != 0)
+		return count;
+
+	mutex_lock(&df->lock);
+	memset(df->time_in_state, 0, cnt * sizeof(u64));
+	memset(df->trans_table, 0, cnt * cnt * sizeof(int));
+	df->last_stat_updated = get_jiffies_64();
+	df->total_trans = 0;
+	mutex_unlock(&df->lock);
+
+	return count;
+}
+static DEVICE_ATTR_RW(trans_stat);
 
 static struct attribute *devfreq_attrs[] = {
 	&dev_attr_name.attr,
-- 
2.24.0


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

* [PATCH v3 3/3] devfreq: move statistics to separate struct
       [not found]   ` <CGME20191205145544eucas1p17e64389fdee24549a523c624c13194d7@eucas1p1.samsung.com>
@ 2019-12-05 14:55     ` Kamil Konieczny
  2019-12-06  5:30       ` Chanwoo Choi
       [not found]       ` <CGME20191206052729epcas1p110d11c728db1ebd7487c8c5fe936df21@epcas1p1.samsung.com>
  0 siblings, 2 replies; 11+ messages in thread
From: Kamil Konieczny @ 2019-12-05 14:55 UTC (permalink / raw)
  To: k.konieczny
  Cc: Bartlomiej Zolnierkiewicz, Chanwoo Choi, Krzysztof Kozlowski,
	Kyungmin Park, linux-kernel, linux-pm, Marek Szyprowski,
	MyungJoo Ham, Matthias Kaehlcke

Count time and transitions between devfreq frequencies in separate struct
for improved code readability and maintenance.

Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
---
Changes in v3:
- removed freq_table and max_state from struct devfreq_stats as they are
  already present in struct devfreq_dev_profile
- renamed last_stat_updated to last_update, as 'stat' is already present
  in struct devfreq_stats
- define struct devfreq_stats stats; in devfreq as there is only one
  stats per devfreq
- improve descriptions of devfreq_stats and stats
- use profile instead of devfreq->profile in devfreq_add_device, as this
  var is already parameter
- added Reviewed-by: Matthias Kaehlcke <mka@chromium.org>

Changes in v2:
- squash three patches into one, do not modify devfreq_profile and separate stats
  into devfreq_stats
---
 drivers/devfreq/devfreq.c | 67 ++++++++++++++++++++-------------------
 include/linux/devfreq.h   | 26 +++++++++------
 2 files changed, 51 insertions(+), 42 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 218eb64d7f28..bcd7e92d2cf3 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -198,6 +198,7 @@ static int set_freq_table(struct devfreq *devfreq)
  */
 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 {
+	struct devfreq_stats *stats = &devfreq->stats;
 	int lev, prev_lev, ret = 0;
 	u64 cur_time;
 
@@ -214,9 +215,7 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 		goto out;
 	}
 
-	devfreq->time_in_state[prev_lev] +=
-			 cur_time - devfreq->last_stat_updated;
-
+	stats->time_in_state[prev_lev] += cur_time - stats->last_update;
 	lev = devfreq_get_freq_level(devfreq, freq);
 	if (lev < 0) {
 		ret = lev;
@@ -224,13 +223,13 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 	}
 
 	if (lev != prev_lev) {
-		devfreq->trans_table[(prev_lev *
-				devfreq->profile->max_state) + lev]++;
-		devfreq->total_trans++;
+		stats->trans_table[(prev_lev * devfreq->profile->max_state) +
+				   lev]++;
+		stats->total_trans++;
 	}
 
 out:
-	devfreq->last_stat_updated = cur_time;
+	stats->last_update = cur_time;
 	return ret;
 }
 EXPORT_SYMBOL(devfreq_update_status);
@@ -525,7 +524,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
 			msecs_to_jiffies(devfreq->profile->polling_ms));
 
 out_update:
-	devfreq->last_stat_updated = get_jiffies_64();
+	devfreq->stats.last_update = get_jiffies_64();
 	devfreq->stop_polling = false;
 
 	if (devfreq->profile->get_cur_freq &&
@@ -735,28 +734,29 @@ struct devfreq *devfreq_add_device(struct device *dev,
 		goto err_out;
 	}
 
-	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
+	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
 			array3_size(sizeof(unsigned int),
-				    devfreq->profile->max_state,
-				    devfreq->profile->max_state),
+				    profile->max_state,
+				    profile->max_state),
 			GFP_KERNEL);
-	if (!devfreq->trans_table) {
+	if (!devfreq->stats.trans_table) {
 		mutex_unlock(&devfreq->lock);
 		err = -ENOMEM;
 		goto err_devfreq;
 	}
 
-	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
-			devfreq->profile->max_state,
-			sizeof(*devfreq->time_in_state),
+	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
+			profile->max_state,
+			sizeof(*devfreq->stats.time_in_state),
 			GFP_KERNEL);
-	if (!devfreq->time_in_state) {
+	if (!devfreq->stats.time_in_state) {
 		mutex_unlock(&devfreq->lock);
 		err = -ENOMEM;
 		goto err_devfreq;
 	}
 
-	devfreq->last_stat_updated = get_jiffies_64();
+	devfreq->stats.last_update = get_jiffies_64();
+	devfreq->stats.total_trans = 0;
 
 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
 
@@ -1435,9 +1435,11 @@ static ssize_t trans_stat_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
 	struct devfreq *devfreq = to_devfreq(dev);
+	struct devfreq_stats *stats = &devfreq->stats;
+	unsigned int max_state = devfreq->profile->max_state;
+	unsigned long *freq_table = devfreq->profile->freq_table;
 	ssize_t len;
 	int i, j;
-	unsigned int max_state = devfreq->profile->max_state;
 
 	if (max_state == 0)
 		return sprintf(buf, "Not Supported.\n");
@@ -1453,29 +1455,27 @@ static ssize_t trans_stat_show(struct device *dev,
 	len = sprintf(buf, "     From  :   To\n");
 	len += sprintf(buf + len, "           :");
 	for (i = 0; i < max_state; i++)
-		len += sprintf(buf + len, "%10lu",
-				devfreq->profile->freq_table[i]);
+		len += sprintf(buf + len, "%10lu", freq_table[i]);
 
 	len += sprintf(buf + len, "   time(ms)\n");
 
 	for (i = 0; i < max_state; i++) {
-		if (devfreq->profile->freq_table[i]
-					== devfreq->previous_freq) {
+		if (freq_table[i] == devfreq->previous_freq)
 			len += sprintf(buf + len, "*");
-		} else {
+		else
 			len += sprintf(buf + len, " ");
-		}
-		len += sprintf(buf + len, "%10lu:",
-				devfreq->profile->freq_table[i]);
+
+		len += sprintf(buf + len, "%10lu:", freq_table[i]);
 		for (j = 0; j < max_state; j++)
 			len += sprintf(buf + len, "%10u",
-				devfreq->trans_table[(i * max_state) + j]);
+				stats->trans_table[(i * max_state) + j]);
+
 		len += sprintf(buf + len, "%10llu\n", (u64)
-			jiffies64_to_msecs(devfreq->time_in_state[i]));
+			jiffies64_to_msecs(stats->time_in_state[i]));
 	}
 
 	len += sprintf(buf + len, "Total transition : %u\n",
-					devfreq->total_trans);
+					stats->total_trans);
 	return len;
 }
 
@@ -1484,6 +1484,7 @@ static ssize_t trans_stat_store(struct device *dev,
 				const char *buf, size_t count)
 {
 	struct devfreq *df = to_devfreq(dev);
+	struct devfreq_stats *stats = &df->stats;
 	unsigned int cnt = df->profile->max_state;
 	int err, value;
 
@@ -1495,10 +1496,10 @@ static ssize_t trans_stat_store(struct device *dev,
 		return count;
 
 	mutex_lock(&df->lock);
-	memset(df->time_in_state, 0, cnt * sizeof(u64));
-	memset(df->trans_table, 0, cnt * cnt * sizeof(int));
-	df->last_stat_updated = get_jiffies_64();
-	df->total_trans = 0;
+	memset(stats->time_in_state, 0, cnt * sizeof(u64));
+	memset(stats->trans_table, 0, cnt * cnt * sizeof(int));
+	stats->last_update = get_jiffies_64();
+	stats->total_trans = 0;
 	mutex_unlock(&df->lock);
 
 	return count;
diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
index 41f15e7a22b8..de2fdc56aa5b 100644
--- a/include/linux/devfreq.h
+++ b/include/linux/devfreq.h
@@ -106,6 +106,20 @@ struct devfreq_dev_profile {
 	unsigned int max_state;
 };
 
+/**
+ * struct devfreq_stats - Statistics of devfreq device behavior
+ * @total_trans:	Number of devfreq transitions.
+ * @trans_table:	Statistics of devfreq transitions.
+ * @time_in_state:	Statistics of devfreq states.
+ * @last_update:	The last time stats were updated.
+ */
+struct devfreq_stats {
+	unsigned int total_trans;
+	unsigned int *trans_table;
+	u64 *time_in_state;
+	u64 last_update;
+};
+
 /**
  * struct devfreq - Device devfreq structure
  * @node:	list node - contains the devices with devfreq that have been
@@ -131,10 +145,7 @@ struct devfreq_dev_profile {
  * @suspend_freq:	 frequency of a device set during suspend phase.
  * @resume_freq:	 frequency of a device set in resume phase.
  * @suspend_count:	 suspend requests counter for a device.
- * @total_trans:	Number of devfreq transitions
- * @trans_table:	Statistics of devfreq transitions
- * @time_in_state:	Statistics of devfreq states
- * @last_stat_updated:	The last time stat updated
+ * @stats:	Statistics of devfreq device behavior
  * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
  *
  * This structure stores the devfreq information for a give device.
@@ -171,11 +182,8 @@ struct devfreq {
 	unsigned long resume_freq;
 	atomic_t suspend_count;
 
-	/* information for device frequency transition */
-	unsigned int total_trans;
-	unsigned int *trans_table;
-	u64 *time_in_state;
-	u64 last_stat_updated;
+	/* information for device frequency transitions */
+	struct devfreq_stats stats;
 
 	struct srcu_notifier_head transition_notifier_list;
 };
-- 
2.24.0


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

* Re: [PATCH v3 1/3] devfreq: change time stats to 64-bit
  2019-12-05 14:55     ` [PATCH v3 1/3] devfreq: change time stats to 64-bit Kamil Konieczny
@ 2019-12-06  1:37       ` Chanwoo Choi
  0 siblings, 0 replies; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  1:37 UTC (permalink / raw)
  To: Kamil Konieczny
  Cc: Bartlomiej Zolnierkiewicz, Krzysztof Kozlowski, Kyungmin Park,
	linux-kernel, linux-pm, Marek Szyprowski, MyungJoo Ham

On 12/5/19 11:55 PM, Kamil Konieczny wrote:
> Change time stats counting to bigger type by using 64-bit jiffies.
> This will make devfreq stats code look similar to cpufreq stats and
> prevents overflow (for HZ = 1000 after 49.7 days).
> 
> Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
> Acked-by: Chanwoo Choi <cw00.choi@samsung.com>
> ---
> Changes in v3:
> - changed types of cur_time and last_stats_updated to u64 as this is
>   returned by get_jiffies_64()
> 
> Changes in v2:
> - added Acked-by, rebased on linux-next
> ---
>  drivers/devfreq/devfreq.c | 14 +++++++-------
>  include/linux/devfreq.h   |  4 ++--
>  2 files changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index bdeb4189c978..abecadeb3dc2 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -199,10 +199,10 @@ static int set_freq_table(struct devfreq *devfreq)
>  int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  {
>  	int lev, prev_lev, ret = 0;
> -	unsigned long cur_time;
> +	u64 cur_time;
>  
>  	lockdep_assert_held(&devfreq->lock);
> -	cur_time = jiffies;
> +	cur_time = get_jiffies_64();
>  
>  	/* Immediately exit if previous_freq is not initialized yet. */
>  	if (!devfreq->previous_freq)
> @@ -525,7 +525,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
>  			msecs_to_jiffies(devfreq->profile->polling_ms));
>  
>  out_update:
> -	devfreq->last_stat_updated = jiffies;
> +	devfreq->last_stat_updated = get_jiffies_64();
>  	devfreq->stop_polling = false;
>  
>  	if (devfreq->profile->get_cur_freq &&
> @@ -748,7 +748,7 @@ struct devfreq *devfreq_add_device(struct device *dev,
>  
>  	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
>  			devfreq->profile->max_state,
> -			sizeof(unsigned long),
> +			sizeof(*devfreq->time_in_state),
>  			GFP_KERNEL);
>  	if (!devfreq->time_in_state) {
>  		mutex_unlock(&devfreq->lock);
> @@ -756,7 +756,7 @@ struct devfreq *devfreq_add_device(struct device *dev,
>  		goto err_devfreq;
>  	}
>  
> -	devfreq->last_stat_updated = jiffies;
> +	devfreq->last_stat_updated = get_jiffies_64();
>  
>  	srcu_init_notifier_head(&devfreq->transition_notifier_list);
>  
> @@ -1470,8 +1470,8 @@ static ssize_t trans_stat_show(struct device *dev,
>  		for (j = 0; j < max_state; j++)
>  			len += sprintf(buf + len, "%10u",
>  				devfreq->trans_table[(i * max_state) + j]);
> -		len += sprintf(buf + len, "%10u\n",
> -			jiffies_to_msecs(devfreq->time_in_state[i]));
> +		len += sprintf(buf + len, "%10llu\n", (u64)
> +			jiffies64_to_msecs(devfreq->time_in_state[i]));
>  	}
>  
>  	len += sprintf(buf + len, "Total transition : %u\n",
> diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
> index 2bae9ed3c783..41f15e7a22b8 100644
> --- a/include/linux/devfreq.h
> +++ b/include/linux/devfreq.h
> @@ -174,8 +174,8 @@ struct devfreq {
>  	/* information for device frequency transition */
>  	unsigned int total_trans;
>  	unsigned int *trans_table;
> -	unsigned long *time_in_state;
> -	unsigned long last_stat_updated;
> +	u64 *time_in_state;
> +	u64 last_stat_updated;
>  
>  	struct srcu_notifier_head transition_notifier_list;
>  };
> 

Applied it. Thanks.

-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

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

* Re: [PATCH v3 2/3] devfreq: add clearing transitions stats
  2019-12-05 14:55     ` [PATCH v3 2/3] devfreq: add clearing transitions stats Kamil Konieczny
@ 2019-12-06  2:01       ` Chanwoo Choi
  0 siblings, 0 replies; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  2:01 UTC (permalink / raw)
  To: Kamil Konieczny
  Cc: Andrew Donnellan, Bartlomiej Zolnierkiewicz, Greg Kroah-Hartman,
	Krzysztof Kozlowski, Kyungmin Park, linux-kernel, linux-pm,
	Marek Szyprowski, Mauro Carvalho Chehab, MyungJoo Ham,
	Rafael J. Wysocki, cpgs (cpgs@samsung.com)

On 12/5/19 11:55 PM, Kamil Konieczny wrote:
> Add clearing transition table and time in states devfreq statistics
> by writing 0 (zero) to trans_stat file in devfreq sysfs. An example use
> is like following:
> 
> echo 0 > /sys/class/devfreq/devfreqX/trans_stat
> 
> Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
> ---
> Changes in v3:
> - add checks for zero in input and clear stats only when zero is written
>   to trans_stats
> - change documentation of trans_stat in sysfs
> 
> Changes in v2:
> - instead of creating new sysfs file, add new functionality to trans_stat
>   and clear stats when anything is writen to it
> ---
>  Documentation/ABI/testing/sysfs-class-devfreq | 11 +++++---
>  drivers/devfreq/devfreq.c                     | 27 ++++++++++++++++++-
>  2 files changed, 33 insertions(+), 5 deletions(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-class-devfreq b/Documentation/ABI/testing/sysfs-class-devfreq
> index 75897e2fde43..9758eb85ade3 100644
> --- a/Documentation/ABI/testing/sysfs-class-devfreq
> +++ b/Documentation/ABI/testing/sysfs-class-devfreq
> @@ -55,12 +55,15 @@ What:		/sys/class/devfreq/.../trans_stat
>  Date:		October 2012
>  Contact:	MyungJoo Ham <myungjoo.ham@samsung.com>
>  Description:
> -		This ABI shows the statistics of devfreq behavior on a
> -		specific device. It shows the time spent in each state and
> -		the number of transitions between states.
> +		This ABI shows or clears the statistics of devfreq behavior
> +		on a specific device. It shows the time spent in each state
> +		and the number of transitions between states.
>  		In order to activate this ABI, the devfreq target device
>  		driver should provide the list of available frequencies
> -		with its profile.
> +		with its profile. If need to reset the statistics of devfreq
> +		behavior on a specific device, enter 0(zero) to 'trans_stat'
> +		as following:
> +			echo 0 > /sys/class/devfreq/.../trans_stat
>  
>  What:		/sys/class/devfreq/.../userspace/set_freq
>  Date:		September 2011
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index abecadeb3dc2..218eb64d7f28 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -1478,7 +1478,32 @@ static ssize_t trans_stat_show(struct device *dev,
>  					devfreq->total_trans);
>  	return len;
>  }
> -static DEVICE_ATTR_RO(trans_stat);
> +
> +static ssize_t trans_stat_store(struct device *dev,
> +				struct device_attribute *attr,
> +				const char *buf, size_t count)
> +{
> +	struct devfreq *df = to_devfreq(dev);
> +	unsigned int cnt = df->profile->max_state;
> +	int err, value;
> +
> +	if (cnt == 0)
> +		return count;
> +
> +	err = kstrtoint(buf, 10, &value);
> +	if (err || value != 0)
> +		return count;

Better returning -EINVAL to inform the error of user.
I changed it by myself.

> +
> +	mutex_lock(&df->lock);
> +	memset(df->time_in_state, 0, cnt * sizeof(u64));

Changed the size calculation way as following by myself:

    memset(df->time_in_state, 0, (df->profile->max_state *                  
					sizeof(*devfreq->time_in_state)));  

> +	memset(df->trans_table, 0, cnt * cnt * sizeof(int));

Changed the size calculation way with array3_size() as following by myself:
	memset(df->trans_table, 0, array3_size(sizeof(unsigned int),            
					df->profile->max_state,                 
					df->profile->max_state));     


> +	df->last_stat_updated = get_jiffies_64();
> +	df->total_trans = 0;

Changed the init sequence as following by myself.
	df->total_trans = 0;
	df->last_stat_updated = get_jiffies_64();

-- 
Best Regards,
Chanwoo Choi
Samsung Electronics


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

* [PATCH v4] PM / devfreq: add clearing transitions stats
       [not found]   ` <CGME20191206021208epcas1p1d7b3bb434108f38b0ef83b1c7d7a5d79@epcas1p1.samsung.com>
@ 2019-12-06  2:18     ` Chanwoo Choi
  2019-12-06  2:19       ` Chanwoo Choi
  0 siblings, 1 reply; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  2:18 UTC (permalink / raw)
  To: linux-kernel, linux-pm
  Cc: rafael.j.wysocki, myungjoo.ham, b.zolnierkie, cw00.choi, gregkh,
	krzk, kyungmin.park, m.szyprowski, mchehab, cpgs,
	Kamil Konieczny

From: Kamil Konieczny <k.konieczny@samsung.com>

Add clearing transition table and time in states devfreq statistics
by writing 0 (zero) to trans_stat file in devfreq sysfs. An example use
is like following:

echo 0 > /sys/class/devfreq/devfreqX/trans_stat

Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
[cw00.choi: Edit return value if entering the wrong value for reset
 and use arrary3_size() to get the size of 3-dimensional array]
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
---
 Documentation/ABI/testing/sysfs-class-devfreq | 11 ++++---
 drivers/devfreq/devfreq.c                     | 29 ++++++++++++++++++-
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/Documentation/ABI/testing/sysfs-class-devfreq b/Documentation/ABI/testing/sysfs-class-devfreq
index 75897e2fde43..9758eb85ade3 100644
--- a/Documentation/ABI/testing/sysfs-class-devfreq
+++ b/Documentation/ABI/testing/sysfs-class-devfreq
@@ -55,12 +55,15 @@ What:		/sys/class/devfreq/.../trans_stat
 Date:		October 2012
 Contact:	MyungJoo Ham <myungjoo.ham@samsung.com>
 Description:
-		This ABI shows the statistics of devfreq behavior on a
-		specific device. It shows the time spent in each state and
-		the number of transitions between states.
+		This ABI shows or clears the statistics of devfreq behavior
+		on a specific device. It shows the time spent in each state
+		and the number of transitions between states.
 		In order to activate this ABI, the devfreq target device
 		driver should provide the list of available frequencies
-		with its profile.
+		with its profile. If need to reset the statistics of devfreq
+		behavior on a specific device, enter 0(zero) to 'trans_stat'
+		as following:
+			echo 0 > /sys/class/devfreq/.../trans_stat
 
 What:		/sys/class/devfreq/.../userspace/set_freq
 Date:		September 2011
diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index abecadeb3dc2..df31f430051d 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -1478,7 +1478,34 @@ static ssize_t trans_stat_show(struct device *dev,
 					devfreq->total_trans);
 	return len;
 }
-static DEVICE_ATTR_RO(trans_stat);
+
+static ssize_t trans_stat_store(struct device *dev,
+				struct device_attribute *attr,
+				const char *buf, size_t count)
+{
+	struct devfreq *df = to_devfreq(dev);
+	int err, value;
+
+	if (df->profile->max_state == 0)
+		return count;
+
+	err = kstrtoint(buf, 10, &value);
+	if (err || value != 0)
+		return -EINVAL;
+
+	mutex_lock(&df->lock);
+	memset(df->time_in_state, 0, (df->profile->max_state *
+					sizeof(*df->time_in_state)));
+	memset(df->trans_table, 0, array3_size(sizeof(unsigned int),
+					df->profile->max_state,
+					df->profile->max_state));
+	df->total_trans = 0;
+	df->last_stat_updated = get_jiffies_64();
+	mutex_unlock(&df->lock);
+
+	return count;
+}
+static DEVICE_ATTR_RW(trans_stat);
 
 static struct attribute *devfreq_attrs[] = {
 	&dev_attr_name.attr,
-- 
2.17.1


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

* Re: [PATCH v4] PM / devfreq: add clearing transitions stats
  2019-12-06  2:18     ` [PATCH v4] PM / devfreq: add clearing transitions stats Chanwoo Choi
@ 2019-12-06  2:19       ` Chanwoo Choi
  0 siblings, 0 replies; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  2:19 UTC (permalink / raw)
  To: linux-kernel, linux-pm
  Cc: rafael.j.wysocki, myungjoo.ham, b.zolnierkie, gregkh, krzk,
	kyungmin.park, m.szyprowski, mchehab, cpgs, Kamil Konieczny

On 12/6/19 11:18 AM, Chanwoo Choi wrote:
> From: Kamil Konieczny <k.konieczny@samsung.com>
> 
> Add clearing transition table and time in states devfreq statistics
> by writing 0 (zero) to trans_stat file in devfreq sysfs. An example use
> is like following:
> 
> echo 0 > /sys/class/devfreq/devfreqX/trans_stat
> 
> Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
> [cw00.choi: Edit return value if entering the wrong value for reset
>  and use arrary3_size() to get the size of 3-dimensional array]
> Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
> ---
>  Documentation/ABI/testing/sysfs-class-devfreq | 11 ++++---
>  drivers/devfreq/devfreq.c                     | 29 ++++++++++++++++++-
>  2 files changed, 35 insertions(+), 5 deletions(-)
> 
> diff --git a/Documentation/ABI/testing/sysfs-class-devfreq b/Documentation/ABI/testing/sysfs-class-devfreq
> index 75897e2fde43..9758eb85ade3 100644
> --- a/Documentation/ABI/testing/sysfs-class-devfreq
> +++ b/Documentation/ABI/testing/sysfs-class-devfreq
> @@ -55,12 +55,15 @@ What:		/sys/class/devfreq/.../trans_stat
>  Date:		October 2012
>  Contact:	MyungJoo Ham <myungjoo.ham@samsung.com>
>  Description:
> -		This ABI shows the statistics of devfreq behavior on a
> -		specific device. It shows the time spent in each state and
> -		the number of transitions between states.
> +		This ABI shows or clears the statistics of devfreq behavior
> +		on a specific device. It shows the time spent in each state
> +		and the number of transitions between states.
>  		In order to activate this ABI, the devfreq target device
>  		driver should provide the list of available frequencies
> -		with its profile.
> +		with its profile. If need to reset the statistics of devfreq
> +		behavior on a specific device, enter 0(zero) to 'trans_stat'
> +		as following:
> +			echo 0 > /sys/class/devfreq/.../trans_stat
>  
>  What:		/sys/class/devfreq/.../userspace/set_freq
>  Date:		September 2011
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index abecadeb3dc2..df31f430051d 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -1478,7 +1478,34 @@ static ssize_t trans_stat_show(struct device *dev,
>  					devfreq->total_trans);
>  	return len;
>  }
> -static DEVICE_ATTR_RO(trans_stat);
> +
> +static ssize_t trans_stat_store(struct device *dev,
> +				struct device_attribute *attr,
> +				const char *buf, size_t count)
> +{
> +	struct devfreq *df = to_devfreq(dev);
> +	int err, value;
> +
> +	if (df->profile->max_state == 0)
> +		return count;
> +
> +	err = kstrtoint(buf, 10, &value);
> +	if (err || value != 0)
> +		return -EINVAL;
> +
> +	mutex_lock(&df->lock);
> +	memset(df->time_in_state, 0, (df->profile->max_state *
> +					sizeof(*df->time_in_state)));
> +	memset(df->trans_table, 0, array3_size(sizeof(unsigned int),
> +					df->profile->max_state,
> +					df->profile->max_state));
> +	df->total_trans = 0;
> +	df->last_stat_updated = get_jiffies_64();
> +	mutex_unlock(&df->lock);
> +
> +	return count;
> +}
> +static DEVICE_ATTR_RW(trans_stat);
>  
>  static struct attribute *devfreq_attrs[] = {
>  	&dev_attr_name.attr,
> 

Applied it. Thanks.

-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

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

* Re: [PATCH v3 3/3] devfreq: move statistics to separate struct
  2019-12-05 14:55     ` [PATCH v3 3/3] devfreq: move statistics to separate struct Kamil Konieczny
@ 2019-12-06  5:30       ` Chanwoo Choi
       [not found]       ` <CGME20191206052729epcas1p110d11c728db1ebd7487c8c5fe936df21@epcas1p1.samsung.com>
  1 sibling, 0 replies; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  5:30 UTC (permalink / raw)
  To: Kamil Konieczny
  Cc: Bartlomiej Zolnierkiewicz, Krzysztof Kozlowski, Kyungmin Park,
	linux-kernel, linux-pm, Marek Szyprowski, MyungJoo Ham,
	Matthias Kaehlcke

Hi,

Looks good. But, it include the some clean-up code
regardless of 'struct stats'. So, I modified them as following by myself:

- Remove the code touched the 'devfreq->profile' in this patch
- Use 'devfreq->stats.*' style instead of 'stats->*' in order to
 keep the consistent coding style.

On 12/5/19 11:55 PM, Kamil Konieczny wrote:
> Count time and transitions between devfreq frequencies in separate struct
> for improved code readability and maintenance.
> 
> Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
> Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
> ---
> Changes in v3:
> - removed freq_table and max_state from struct devfreq_stats as they are
>   already present in struct devfreq_dev_profile
> - renamed last_stat_updated to last_update, as 'stat' is already present
>   in struct devfreq_stats
> - define struct devfreq_stats stats; in devfreq as there is only one
>   stats per devfreq
> - improve descriptions of devfreq_stats and stats
> - use profile instead of devfreq->profile in devfreq_add_device, as this
>   var is already parameter
> - added Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
> 
> Changes in v2:
> - squash three patches into one, do not modify devfreq_profile and separate stats
>   into devfreq_stats
> ---
>  drivers/devfreq/devfreq.c | 67 ++++++++++++++++++++-------------------
>  include/linux/devfreq.h   | 26 +++++++++------
>  2 files changed, 51 insertions(+), 42 deletions(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index 218eb64d7f28..bcd7e92d2cf3 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -198,6 +198,7 @@ static int set_freq_table(struct devfreq *devfreq)
>   */
>  int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  {
> +	struct devfreq_stats *stats = &devfreq->stats;
>  	int lev, prev_lev, ret = 0;
>  	u64 cur_time;
>  
> @@ -214,9 +215,7 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  		goto out;
>  	}
>  
> -	devfreq->time_in_state[prev_lev] +=
> -			 cur_time - devfreq->last_stat_updated;
> -
> +	stats->time_in_state[prev_lev] += cur_time - stats->last_update;
>  	lev = devfreq_get_freq_level(devfreq, freq);
>  	if (lev < 0) {
>  		ret = lev;
> @@ -224,13 +223,13 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  	}
>  
>  	if (lev != prev_lev) {
> -		devfreq->trans_table[(prev_lev *
> -				devfreq->profile->max_state) + lev]++;
> -		devfreq->total_trans++;
> +		stats->trans_table[(prev_lev * devfreq->profile->max_state) +
> +				   lev]++;
> +		stats->total_trans++;
>  	}
>  
>  out:
> -	devfreq->last_stat_updated = cur_time;
> +	stats->last_update = cur_time;
>  	return ret;
>  }
>  EXPORT_SYMBOL(devfreq_update_status);
> @@ -525,7 +524,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
>  			msecs_to_jiffies(devfreq->profile->polling_ms));
>  
>  out_update:
> -	devfreq->last_stat_updated = get_jiffies_64();
> +	devfreq->stats.last_update = get_jiffies_64();
>  	devfreq->stop_polling = false;
>  
>  	if (devfreq->profile->get_cur_freq &&
> @@ -735,28 +734,29 @@ struct devfreq *devfreq_add_device(struct device *dev,
>  		goto err_out;
>  	}
>  
> -	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
> +	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
>  			array3_size(sizeof(unsigned int),
> -				    devfreq->profile->max_state,
> -				    devfreq->profile->max_state),

It is not good touching the 'devfreq->profile' variable
that is not related to 'struct devfreq_stats'. Just keep the previous code.


> +				    profile->max_state,
> +				    profile->max_state),
>  			GFP_KERNEL);
> -	if (!devfreq->trans_table) {
> +	if (!devfreq->stats.trans_table) {
>  		mutex_unlock(&devfreq->lock);
>  		err = -ENOMEM;
>  		goto err_devfreq;
>  	}
>  
> -	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
> -			devfreq->profile->max_state,
> -			sizeof(*devfreq->time_in_state),
> +	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
> +			profile->max_state,

ditto. Just keep the previous code.

> +			sizeof(*devfreq->stats.time_in_state),
>  			GFP_KERNEL);
> -	if (!devfreq->time_in_state) {
> +	if (!devfreq->stats.time_in_state) {
>  		mutex_unlock(&devfreq->lock);
>  		err = -ENOMEM;
>  		goto err_devfreq;
>  	}
>  
> -	devfreq->last_stat_updated = get_jiffies_64();
> +	devfreq->stats.last_update = get_jiffies_64();
> +	devfreq->stats.total_trans = 0;
>  
>  	srcu_init_notifier_head(&devfreq->transition_notifier_list);
>  
> @@ -1435,9 +1435,11 @@ static ssize_t trans_stat_show(struct device *dev,
>  			       struct device_attribute *attr, char *buf)
>  {
>  	struct devfreq *devfreq = to_devfreq(dev);
> +	struct devfreq_stats *stats = &devfreq->stats;
> +	unsigned int max_state = devfreq->profile->max_state;
> +	unsigned long *freq_table = devfreq->profile->freq_table;
>  	ssize_t len;
>  	int i, j;
> -	unsigned int max_state = devfreq->profile->max_state;

It is not good touching the 'devfreq->profile' variable
that is not related to 'struct devfreq_stats'.

>  
>  	if (max_state == 0)
>  		return sprintf(buf, "Not Supported.\n");
> @@ -1453,29 +1455,27 @@ static ssize_t trans_stat_show(struct device *dev,
>  	len = sprintf(buf, "     From  :   To\n");
>  	len += sprintf(buf + len, "           :");
>  	for (i = 0; i < max_state; i++)
> -		len += sprintf(buf + len, "%10lu",
> -				devfreq->profile->freq_table[i]);
> +		len += sprintf(buf + len, "%10lu", freq_table[i]);

It is not good touching the 'devfreq->profile' variable
that is not related to 'struct devfreq_stats'.

>  
>  	len += sprintf(buf + len, "   time(ms)\n");
>  
>  	for (i = 0; i < max_state; i++) {
> -		if (devfreq->profile->freq_table[i]
> -					== devfreq->previous_freq) {
> +		if (freq_table[i] == devfreq->previous_freq)

ditto.

>  			len += sprintf(buf + len, "*");
> -		} else {

Don't touch the something if it is not related to 'struct devfreq_stats'

> +		else
>  			len += sprintf(buf + len, " ");
> -		}

ditto.

> -		len += sprintf(buf + len, "%10lu:",
> -				devfreq->profile->freq_table[i]);
> +
> +		len += sprintf(buf + len, "%10lu:", freq_table[i]);

ditto.

>  		for (j = 0; j < max_state; j++)
>  			len += sprintf(buf + len, "%10u",
> -				devfreq->trans_table[(i * max_state) + j]);
> +				stats->trans_table[(i * max_state) + j]);
> +
>  		len += sprintf(buf + len, "%10llu\n", (u64)
> -			jiffies64_to_msecs(devfreq->time_in_state[i]));
> +			jiffies64_to_msecs(stats->time_in_state[i]));
>  	}
>  
>  	len += sprintf(buf + len, "Total transition : %u\n",
> -					devfreq->total_trans);
> +					stats->total_trans);
>  	return len;
>  }
>  
> @@ -1484,6 +1484,7 @@ static ssize_t trans_stat_store(struct device *dev,
>  				const char *buf, size_t count)
>  {
>  	struct devfreq *df = to_devfreq(dev);
> +	struct devfreq_stats *stats = &df->stats;
>  	unsigned int cnt = df->profile->max_state;
>  	int err, value;
>  
> @@ -1495,10 +1496,10 @@ static ssize_t trans_stat_store(struct device *dev,
>  		return count;
>  
>  	mutex_lock(&df->lock);
> -	memset(df->time_in_state, 0, cnt * sizeof(u64));
> -	memset(df->trans_table, 0, cnt * cnt * sizeof(int));
> -	df->last_stat_updated = get_jiffies_64();
> -	df->total_trans = 0;
> +	memset(stats->time_in_state, 0, cnt * sizeof(u64));
> +	memset(stats->trans_table, 0, cnt * cnt * sizeof(int));
> +	stats->last_update = get_jiffies_64();
> +	stats->total_trans = 0;
>  	mutex_unlock(&df->lock);
>  
>  	return count;
> diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
> index 41f15e7a22b8..de2fdc56aa5b 100644
> --- a/include/linux/devfreq.h
> +++ b/include/linux/devfreq.h
> @@ -106,6 +106,20 @@ struct devfreq_dev_profile {
>  	unsigned int max_state;
>  };
>  
> +/**
> + * struct devfreq_stats - Statistics of devfreq device behavior
> + * @total_trans:	Number of devfreq transitions.
> + * @trans_table:	Statistics of devfreq transitions.
> + * @time_in_state:	Statistics of devfreq states.
> + * @last_update:	The last time stats were updated.
> + */
> +struct devfreq_stats {
> +	unsigned int total_trans;
> +	unsigned int *trans_table;
> +	u64 *time_in_state;
> +	u64 last_update;
> +};
> +
>  /**
>   * struct devfreq - Device devfreq structure
>   * @node:	list node - contains the devices with devfreq that have been
> @@ -131,10 +145,7 @@ struct devfreq_dev_profile {
>   * @suspend_freq:	 frequency of a device set during suspend phase.
>   * @resume_freq:	 frequency of a device set in resume phase.
>   * @suspend_count:	 suspend requests counter for a device.
> - * @total_trans:	Number of devfreq transitions
> - * @trans_table:	Statistics of devfreq transitions
> - * @time_in_state:	Statistics of devfreq states
> - * @last_stat_updated:	The last time stat updated
> + * @stats:	Statistics of devfreq device behavior
>   * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
>   *
>   * This structure stores the devfreq information for a give device.
> @@ -171,11 +182,8 @@ struct devfreq {
>  	unsigned long resume_freq;
>  	atomic_t suspend_count;
>  
> -	/* information for device frequency transition */
> -	unsigned int total_trans;
> -	unsigned int *trans_table;
> -	u64 *time_in_state;
> -	u64 last_stat_updated;
> +	/* information for device frequency transitions */
> +	struct devfreq_stats stats;
>  
>  	struct srcu_notifier_head transition_notifier_list;
>  };
> 


-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

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

* [PATCH v4] PM / devfreq: Move statistics to separate struct devfreq_stats
       [not found]       ` <CGME20191206052729epcas1p110d11c728db1ebd7487c8c5fe936df21@epcas1p1.samsung.com>
@ 2019-12-06  5:33         ` Chanwoo Choi
  2019-12-06  5:36           ` Chanwoo Choi
  0 siblings, 1 reply; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  5:33 UTC (permalink / raw)
  To: linux-kernel, linux-pm
  Cc: rafael.j.wysocki, myungjoo.ham, b.zolnierkie, cw00.choi, gregkh,
	krzk, kyungmin.park, m.szyprowski, mchehab, cpgs,
	Kamil Konieczny

From: Kamil Konieczny <k.konieczny@samsung.com>

Count time and transitions between devfreq frequencies in separate
struct devfreq_stats for improved code readability and maintenance.

Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
[cw00.choi: Fix the merge conflict in trasn_stat_store]
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
---
 drivers/devfreq/devfreq.c | 44 ++++++++++++++++++++-------------------
 include/linux/devfreq.h   | 26 +++++++++++++++--------
 2 files changed, 40 insertions(+), 30 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index df31f430051d..1786a86b1779 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -214,8 +214,8 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 		goto out;
 	}
 
-	devfreq->time_in_state[prev_lev] +=
-			 cur_time - devfreq->last_stat_updated;
+	devfreq->stats.time_in_state[prev_lev] +=
+			cur_time - devfreq->stats.last_update;
 
 	lev = devfreq_get_freq_level(devfreq, freq);
 	if (lev < 0) {
@@ -224,13 +224,13 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
 	}
 
 	if (lev != prev_lev) {
-		devfreq->trans_table[(prev_lev *
-				devfreq->profile->max_state) + lev]++;
-		devfreq->total_trans++;
+		devfreq->stats.trans_table[
+			(prev_lev * devfreq->profile->max_state) + lev]++;
+		devfreq->stats.total_trans++;
 	}
 
 out:
-	devfreq->last_stat_updated = cur_time;
+	devfreq->stats.last_update = cur_time;
 	return ret;
 }
 EXPORT_SYMBOL(devfreq_update_status);
@@ -525,7 +525,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
 			msecs_to_jiffies(devfreq->profile->polling_ms));
 
 out_update:
-	devfreq->last_stat_updated = get_jiffies_64();
+	devfreq->stats.last_update = get_jiffies_64();
 	devfreq->stop_polling = false;
 
 	if (devfreq->profile->get_cur_freq &&
@@ -735,28 +735,29 @@ struct devfreq *devfreq_add_device(struct device *dev,
 		goto err_out;
 	}
 
-	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
+	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
 			array3_size(sizeof(unsigned int),
 				    devfreq->profile->max_state,
 				    devfreq->profile->max_state),
 			GFP_KERNEL);
-	if (!devfreq->trans_table) {
+	if (!devfreq->stats.trans_table) {
 		mutex_unlock(&devfreq->lock);
 		err = -ENOMEM;
 		goto err_devfreq;
 	}
 
-	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
+	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
 			devfreq->profile->max_state,
-			sizeof(*devfreq->time_in_state),
+			sizeof(*devfreq->stats.time_in_state),
 			GFP_KERNEL);
-	if (!devfreq->time_in_state) {
+	if (!devfreq->stats.time_in_state) {
 		mutex_unlock(&devfreq->lock);
 		err = -ENOMEM;
 		goto err_devfreq;
 	}
 
-	devfreq->last_stat_updated = get_jiffies_64();
+	devfreq->stats.total_trans = 0;
+	devfreq->stats.last_update = get_jiffies_64();
 
 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
 
@@ -1469,13 +1470,14 @@ static ssize_t trans_stat_show(struct device *dev,
 				devfreq->profile->freq_table[i]);
 		for (j = 0; j < max_state; j++)
 			len += sprintf(buf + len, "%10u",
-				devfreq->trans_table[(i * max_state) + j]);
+				devfreq->stats.trans_table[(i * max_state) + j]);
+
 		len += sprintf(buf + len, "%10llu\n", (u64)
-			jiffies64_to_msecs(devfreq->time_in_state[i]));
+			jiffies64_to_msecs(devfreq->stats.time_in_state[i]));
 	}
 
 	len += sprintf(buf + len, "Total transition : %u\n",
-					devfreq->total_trans);
+					devfreq->stats.total_trans);
 	return len;
 }
 
@@ -1494,13 +1496,13 @@ static ssize_t trans_stat_store(struct device *dev,
 		return -EINVAL;
 
 	mutex_lock(&df->lock);
-	memset(df->time_in_state, 0, (df->profile->max_state *
-					sizeof(*df->time_in_state)));
-	memset(df->trans_table, 0, array3_size(sizeof(unsigned int),
+	memset(df->stats.time_in_state, 0, (df->profile->max_state *
+					sizeof(*df->stats.time_in_state)));
+	memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
 					df->profile->max_state,
 					df->profile->max_state));
-	df->total_trans = 0;
-	df->last_stat_updated = get_jiffies_64();
+	df->stats.total_trans = 0;
+	df->stats.last_update = get_jiffies_64();
 	mutex_unlock(&df->lock);
 
 	return count;
diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
index 41f15e7a22b8..de2fdc56aa5b 100644
--- a/include/linux/devfreq.h
+++ b/include/linux/devfreq.h
@@ -106,6 +106,20 @@ struct devfreq_dev_profile {
 	unsigned int max_state;
 };
 
+/**
+ * struct devfreq_stats - Statistics of devfreq device behavior
+ * @total_trans:	Number of devfreq transitions.
+ * @trans_table:	Statistics of devfreq transitions.
+ * @time_in_state:	Statistics of devfreq states.
+ * @last_update:	The last time stats were updated.
+ */
+struct devfreq_stats {
+	unsigned int total_trans;
+	unsigned int *trans_table;
+	u64 *time_in_state;
+	u64 last_update;
+};
+
 /**
  * struct devfreq - Device devfreq structure
  * @node:	list node - contains the devices with devfreq that have been
@@ -131,10 +145,7 @@ struct devfreq_dev_profile {
  * @suspend_freq:	 frequency of a device set during suspend phase.
  * @resume_freq:	 frequency of a device set in resume phase.
  * @suspend_count:	 suspend requests counter for a device.
- * @total_trans:	Number of devfreq transitions
- * @trans_table:	Statistics of devfreq transitions
- * @time_in_state:	Statistics of devfreq states
- * @last_stat_updated:	The last time stat updated
+ * @stats:	Statistics of devfreq device behavior
  * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
  *
  * This structure stores the devfreq information for a give device.
@@ -171,11 +182,8 @@ struct devfreq {
 	unsigned long resume_freq;
 	atomic_t suspend_count;
 
-	/* information for device frequency transition */
-	unsigned int total_trans;
-	unsigned int *trans_table;
-	u64 *time_in_state;
-	u64 last_stat_updated;
+	/* information for device frequency transitions */
+	struct devfreq_stats stats;
 
 	struct srcu_notifier_head transition_notifier_list;
 };
-- 
2.17.1


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

* Re: [PATCH v4] PM / devfreq: Move statistics to separate struct devfreq_stats
  2019-12-06  5:33         ` [PATCH v4] PM / devfreq: Move statistics to separate struct devfreq_stats Chanwoo Choi
@ 2019-12-06  5:36           ` Chanwoo Choi
  0 siblings, 0 replies; 11+ messages in thread
From: Chanwoo Choi @ 2019-12-06  5:36 UTC (permalink / raw)
  To: linux-kernel, linux-pm
  Cc: rafael.j.wysocki, myungjoo.ham, b.zolnierkie, gregkh, krzk,
	kyungmin.park, m.szyprowski, mchehab, cpgs, Kamil Konieczny

On 12/6/19 2:33 PM, Chanwoo Choi wrote:
> From: Kamil Konieczny <k.konieczny@samsung.com>
> 
> Count time and transitions between devfreq frequencies in separate
> struct devfreq_stats for improved code readability and maintenance.
> 
> Signed-off-by: Kamil Konieczny <k.konieczny@samsung.com>
> Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
> [cw00.choi: Fix the merge conflict in trasn_stat_store]
> Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
> ---
>  drivers/devfreq/devfreq.c | 44 ++++++++++++++++++++-------------------
>  include/linux/devfreq.h   | 26 +++++++++++++++--------
>  2 files changed, 40 insertions(+), 30 deletions(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index df31f430051d..1786a86b1779 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -214,8 +214,8 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  		goto out;
>  	}
>  
> -	devfreq->time_in_state[prev_lev] +=
> -			 cur_time - devfreq->last_stat_updated;
> +	devfreq->stats.time_in_state[prev_lev] +=
> +			cur_time - devfreq->stats.last_update;
>  
>  	lev = devfreq_get_freq_level(devfreq, freq);
>  	if (lev < 0) {
> @@ -224,13 +224,13 @@ int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
>  	}
>  
>  	if (lev != prev_lev) {
> -		devfreq->trans_table[(prev_lev *
> -				devfreq->profile->max_state) + lev]++;
> -		devfreq->total_trans++;
> +		devfreq->stats.trans_table[
> +			(prev_lev * devfreq->profile->max_state) + lev]++;
> +		devfreq->stats.total_trans++;
>  	}
>  
>  out:
> -	devfreq->last_stat_updated = cur_time;
> +	devfreq->stats.last_update = cur_time;
>  	return ret;
>  }
>  EXPORT_SYMBOL(devfreq_update_status);
> @@ -525,7 +525,7 @@ void devfreq_monitor_resume(struct devfreq *devfreq)
>  			msecs_to_jiffies(devfreq->profile->polling_ms));
>  
>  out_update:
> -	devfreq->last_stat_updated = get_jiffies_64();
> +	devfreq->stats.last_update = get_jiffies_64();
>  	devfreq->stop_polling = false;
>  
>  	if (devfreq->profile->get_cur_freq &&
> @@ -735,28 +735,29 @@ struct devfreq *devfreq_add_device(struct device *dev,
>  		goto err_out;
>  	}
>  
> -	devfreq->trans_table = devm_kzalloc(&devfreq->dev,
> +	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
>  			array3_size(sizeof(unsigned int),
>  				    devfreq->profile->max_state,
>  				    devfreq->profile->max_state),
>  			GFP_KERNEL);
> -	if (!devfreq->trans_table) {
> +	if (!devfreq->stats.trans_table) {
>  		mutex_unlock(&devfreq->lock);
>  		err = -ENOMEM;
>  		goto err_devfreq;
>  	}
>  
> -	devfreq->time_in_state = devm_kcalloc(&devfreq->dev,
> +	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
>  			devfreq->profile->max_state,
> -			sizeof(*devfreq->time_in_state),
> +			sizeof(*devfreq->stats.time_in_state),
>  			GFP_KERNEL);
> -	if (!devfreq->time_in_state) {
> +	if (!devfreq->stats.time_in_state) {
>  		mutex_unlock(&devfreq->lock);
>  		err = -ENOMEM;
>  		goto err_devfreq;
>  	}
>  
> -	devfreq->last_stat_updated = get_jiffies_64();
> +	devfreq->stats.total_trans = 0;
> +	devfreq->stats.last_update = get_jiffies_64();
>  
>  	srcu_init_notifier_head(&devfreq->transition_notifier_list);
>  
> @@ -1469,13 +1470,14 @@ static ssize_t trans_stat_show(struct device *dev,
>  				devfreq->profile->freq_table[i]);
>  		for (j = 0; j < max_state; j++)
>  			len += sprintf(buf + len, "%10u",
> -				devfreq->trans_table[(i * max_state) + j]);
> +				devfreq->stats.trans_table[(i * max_state) + j]);
> +
>  		len += sprintf(buf + len, "%10llu\n", (u64)
> -			jiffies64_to_msecs(devfreq->time_in_state[i]));
> +			jiffies64_to_msecs(devfreq->stats.time_in_state[i]));
>  	}
>  
>  	len += sprintf(buf + len, "Total transition : %u\n",
> -					devfreq->total_trans);
> +					devfreq->stats.total_trans);
>  	return len;
>  }
>  
> @@ -1494,13 +1496,13 @@ static ssize_t trans_stat_store(struct device *dev,
>  		return -EINVAL;
>  
>  	mutex_lock(&df->lock);
> -	memset(df->time_in_state, 0, (df->profile->max_state *
> -					sizeof(*df->time_in_state)));
> -	memset(df->trans_table, 0, array3_size(sizeof(unsigned int),
> +	memset(df->stats.time_in_state, 0, (df->profile->max_state *
> +					sizeof(*df->stats.time_in_state)));
> +	memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
>  					df->profile->max_state,
>  					df->profile->max_state));
> -	df->total_trans = 0;
> -	df->last_stat_updated = get_jiffies_64();
> +	df->stats.total_trans = 0;
> +	df->stats.last_update = get_jiffies_64();
>  	mutex_unlock(&df->lock);
>  
>  	return count;
> diff --git a/include/linux/devfreq.h b/include/linux/devfreq.h
> index 41f15e7a22b8..de2fdc56aa5b 100644
> --- a/include/linux/devfreq.h
> +++ b/include/linux/devfreq.h
> @@ -106,6 +106,20 @@ struct devfreq_dev_profile {
>  	unsigned int max_state;
>  };
>  
> +/**
> + * struct devfreq_stats - Statistics of devfreq device behavior
> + * @total_trans:	Number of devfreq transitions.
> + * @trans_table:	Statistics of devfreq transitions.
> + * @time_in_state:	Statistics of devfreq states.
> + * @last_update:	The last time stats were updated.
> + */
> +struct devfreq_stats {
> +	unsigned int total_trans;
> +	unsigned int *trans_table;
> +	u64 *time_in_state;
> +	u64 last_update;
> +};
> +
>  /**
>   * struct devfreq - Device devfreq structure
>   * @node:	list node - contains the devices with devfreq that have been
> @@ -131,10 +145,7 @@ struct devfreq_dev_profile {
>   * @suspend_freq:	 frequency of a device set during suspend phase.
>   * @resume_freq:	 frequency of a device set in resume phase.
>   * @suspend_count:	 suspend requests counter for a device.
> - * @total_trans:	Number of devfreq transitions
> - * @trans_table:	Statistics of devfreq transitions
> - * @time_in_state:	Statistics of devfreq states
> - * @last_stat_updated:	The last time stat updated
> + * @stats:	Statistics of devfreq device behavior
>   * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
>   *
>   * This structure stores the devfreq information for a give device.
> @@ -171,11 +182,8 @@ struct devfreq {
>  	unsigned long resume_freq;
>  	atomic_t suspend_count;
>  
> -	/* information for device frequency transition */
> -	unsigned int total_trans;
> -	unsigned int *trans_table;
> -	u64 *time_in_state;
> -	u64 last_stat_updated;
> +	/* information for device frequency transitions */
> +	struct devfreq_stats stats;
>  
>  	struct srcu_notifier_head transition_notifier_list;
>  };
> 

Applied it. Thanks.

-- 
Best Regards,
Chanwoo Choi
Samsung Electronics

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

end of thread, other threads:[~2019-12-06  5:30 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20191205145542eucas1p2b39536c9c186d5656f39d214baa32670@eucas1p2.samsung.com>
2019-12-05 14:55 ` [PATCH v3 0/3] devfreq: improve devfreq statistics counting Kamil Konieczny
     [not found]   ` <CGME20191205145543eucas1p11798d11e91f6a691d5989db7cba81b96@eucas1p1.samsung.com>
2019-12-05 14:55     ` [PATCH v3 1/3] devfreq: change time stats to 64-bit Kamil Konieczny
2019-12-06  1:37       ` Chanwoo Choi
     [not found]   ` <CGME20191205145543eucas1p1a317647203c47be07bbcee7867fb3e1e@eucas1p1.samsung.com>
2019-12-05 14:55     ` [PATCH v3 2/3] devfreq: add clearing transitions stats Kamil Konieczny
2019-12-06  2:01       ` Chanwoo Choi
     [not found]   ` <CGME20191205145544eucas1p17e64389fdee24549a523c624c13194d7@eucas1p1.samsung.com>
2019-12-05 14:55     ` [PATCH v3 3/3] devfreq: move statistics to separate struct Kamil Konieczny
2019-12-06  5:30       ` Chanwoo Choi
     [not found]       ` <CGME20191206052729epcas1p110d11c728db1ebd7487c8c5fe936df21@epcas1p1.samsung.com>
2019-12-06  5:33         ` [PATCH v4] PM / devfreq: Move statistics to separate struct devfreq_stats Chanwoo Choi
2019-12-06  5:36           ` Chanwoo Choi
     [not found]   ` <CGME20191206021208epcas1p1d7b3bb434108f38b0ef83b1c7d7a5d79@epcas1p1.samsung.com>
2019-12-06  2:18     ` [PATCH v4] PM / devfreq: add clearing transitions stats Chanwoo Choi
2019-12-06  2:19       ` Chanwoo Choi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).