diff mbox series

[v4,03/10] lib/ref_tracker: add printing to memory buffer

Message ID 20230224-track_gt-v4-3-464e8ab4c9ab@intel.com (mailing list archive)
State New, archived
Headers show
Series drm/i915: use ref_tracker library for tracking wakerefs | expand

Commit Message

Andrzej Hajda March 6, 2023, 4:31 p.m. UTC
In case one wants to show stats via debugfs.

Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>
---
 include/linux/ref_tracker.h |  8 +++++++
 lib/ref_tracker.c           | 56 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 56 insertions(+), 8 deletions(-)

Comments

Andi Shyti March 19, 2023, 11:18 p.m. UTC | #1
Hi Andrzej,

This looks also good, just few questions.

On Mon, Mar 06, 2023 at 05:31:59PM +0100, Andrzej Hajda wrote:
> In case one wants to show stats via debugfs.

shall I say it? I'll say it... you can do better with the log
here. It's not a typo fix :)

> Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>

[...]

> +void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
> +			   unsigned int display_limit)
> +{
> +	struct ostream os = {};
> +
> +	__ref_tracker_dir_pr_ostream(dir, display_limit, &os);
> +}
>  EXPORT_SYMBOL(__ref_tracker_dir_print);
>  
>  void ref_tracker_dir_print(struct ref_tracker_dir *dir,
> @@ -114,6 +141,19 @@ void ref_tracker_dir_print(struct ref_tracker_dir *dir,
>  }
>  EXPORT_SYMBOL(ref_tracker_dir_print);
>  
> +int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size)

nit: snprintf is normally referred to its variable parameter
counterpart... I would choose a different name... how about
ref_tracker_dir_fetch_print()?

> +{
> +	struct ostream os = { .buf = buf, .size = size };
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&dir->lock, flags);
> +	__ref_tracker_dir_pr_ostream(dir, 16, &os);
> +	spin_unlock_irqrestore(&dir->lock, flags);

What are you trying to protect with this spinlock? what if
the caller has already locked here? do we need a _locked()
version?

Thanks,
Andi

> +	return os.used;
> +}
> +EXPORT_SYMBOL(ref_tracker_dir_snprint);
> +
>  void ref_tracker_dir_exit(struct ref_tracker_dir *dir)
>  {
>  	struct ref_tracker *tracker, *n;
> 
> -- 
> 2.34.1
Andrzej Hajda March 20, 2023, 6:24 p.m. UTC | #2
On 20.03.2023 00:18, Andi Shyti wrote:
> Hi Andrzej,
> 
> This looks also good, just few questions.
> 
> On Mon, Mar 06, 2023 at 05:31:59PM +0100, Andrzej Hajda wrote:
>> In case one wants to show stats via debugfs.
> 
> shall I say it? I'll say it... you can do better with the log
> here. It's not a typo fix :)
> 
>> Signed-off-by: Andrzej Hajda <andrzej.hajda@intel.com>
> 
> [...]
> 
>> +void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
>> +			   unsigned int display_limit)
>> +{
>> +	struct ostream os = {};
>> +
>> +	__ref_tracker_dir_pr_ostream(dir, display_limit, &os);
>> +}
>>   EXPORT_SYMBOL(__ref_tracker_dir_print);
>>   
>>   void ref_tracker_dir_print(struct ref_tracker_dir *dir,
>> @@ -114,6 +141,19 @@ void ref_tracker_dir_print(struct ref_tracker_dir *dir,
>>   }
>>   EXPORT_SYMBOL(ref_tracker_dir_print);
>>   
>> +int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size)
> 
> nit: snprintf is normally referred to its variable parameter
> counterpart... I would choose a different name... how about
> ref_tracker_dir_fetch_print()?


Hmm, original ref_tracker_dir_print prints the stats to dmesg,
ref_tracker_dir_snprint prints to memory buffer, like:
- stack_depot_print and stack_depot_snprint,
- stack_trace_print and stack_trace_snprint.


> 
>> +{
>> +	struct ostream os = { .buf = buf, .size = size };
>> +	unsigned long flags;
>> +
>> +	spin_lock_irqsave(&dir->lock, flags);
>> +	__ref_tracker_dir_pr_ostream(dir, 16, &os);
>> +	spin_unlock_irqrestore(&dir->lock, flags);
> 
> What are you trying to protect with this spinlock? what if
> the caller has already locked here? do we need a _locked()
> version?

spinlock is to serialize access to dir,
at the moment _locked version is not necessary, but it can be easily 
added if needed.

Regards
Andrzej


> 
> Thanks,
> Andi
> 
>> +	return os.used;
>> +}
>> +EXPORT_SYMBOL(ref_tracker_dir_snprint);
>> +
>>   void ref_tracker_dir_exit(struct ref_tracker_dir *dir)
>>   {
>>   	struct ref_tracker *tracker, *n;
>>
>> -- 
>> 2.34.1
diff mbox series

Patch

diff --git a/include/linux/ref_tracker.h b/include/linux/ref_tracker.h
index a2cf1f6309adb2..2fdbfd2e14797a 100644
--- a/include/linux/ref_tracker.h
+++ b/include/linux/ref_tracker.h
@@ -50,6 +50,8 @@  void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
 void ref_tracker_dir_print(struct ref_tracker_dir *dir,
 			   unsigned int display_limit);
 
+int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size);
+
 int ref_tracker_alloc(struct ref_tracker_dir *dir,
 		      struct ref_tracker **trackerp, gfp_t gfp);
 
@@ -78,6 +80,12 @@  static inline void ref_tracker_dir_print(struct ref_tracker_dir *dir,
 {
 }
 
+static inline int ref_tracker_dir_snprint(struct ref_tracker_dir *dir,
+					  char *buf, size_t size)
+{
+	return 0;
+}
+
 static inline int ref_tracker_alloc(struct ref_tracker_dir *dir,
 				    struct ref_tracker **trackerp,
 				    gfp_t gfp)
diff --git a/lib/ref_tracker.c b/lib/ref_tracker.c
index ab1253fde244ea..2ef4596b6b36f5 100644
--- a/lib/ref_tracker.c
+++ b/lib/ref_tracker.c
@@ -62,8 +62,27 @@  ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit)
 	return stats;
 }
 
-void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
-			   unsigned int display_limit)
+struct ostream {
+	char *buf;
+	int size, used;
+};
+
+#define pr_ostream(stream, fmt, args...) \
+({ \
+	struct ostream *_s = (stream); \
+\
+	if (!_s->buf) { \
+		pr_err(fmt, ##args); \
+	} else { \
+		int ret, len = _s->size - _s->used; \
+		ret = snprintf(_s->buf + _s->used, len, pr_fmt(fmt), ##args); \
+		_s->used += min(ret, len); \
+	} \
+})
+
+static void
+__ref_tracker_dir_pr_ostream(struct ref_tracker_dir *dir,
+			     unsigned int display_limit, struct ostream *s)
 {
 	struct ref_tracker_dir_stats *stats;
 	unsigned int i = 0, skipped;
@@ -77,8 +96,8 @@  void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
 
 	stats = ref_tracker_get_stats(dir, display_limit);
 	if (IS_ERR(stats)) {
-		pr_err("%s@%pK: couldn't get stats, error %pe\n",
-		       dir->name, dir, stats);
+		pr_ostream(s, "%s@%pK: couldn't get stats, error %pe\n",
+			   dir->name, dir, stats);
 		return;
 	}
 
@@ -88,19 +107,27 @@  void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
 		stack = stats->stacks[i].stack_handle;
 		if (sbuf && !stack_depot_snprint(stack, sbuf, STACK_BUF_SIZE, 4))
 			sbuf[0] = 0;
-		pr_err("%s@%pK has %d/%d users at\n%s\n", dir->name, dir,
-		       stats->stacks[i].count, stats->total, sbuf);
+		pr_ostream(s, "%s@%pK has %d/%d users at\n%s\n", dir->name, dir,
+			   stats->stacks[i].count, stats->total, sbuf);
 		skipped -= stats->stacks[i].count;
 	}
 
 	if (skipped)
-		pr_err("%s@%pK skipped reports about %d/%d users.\n",
-		       dir->name, dir, skipped, stats->total);
+		pr_ostream(s, "%s@%pK skipped reports about %d/%d users.\n",
+			   dir->name, dir, skipped, stats->total);
 
 	kfree(sbuf);
 
 	kfree(stats);
 }
+
+void __ref_tracker_dir_print(struct ref_tracker_dir *dir,
+			   unsigned int display_limit)
+{
+	struct ostream os = {};
+
+	__ref_tracker_dir_pr_ostream(dir, display_limit, &os);
+}
 EXPORT_SYMBOL(__ref_tracker_dir_print);
 
 void ref_tracker_dir_print(struct ref_tracker_dir *dir,
@@ -114,6 +141,19 @@  void ref_tracker_dir_print(struct ref_tracker_dir *dir,
 }
 EXPORT_SYMBOL(ref_tracker_dir_print);
 
+int ref_tracker_dir_snprint(struct ref_tracker_dir *dir, char *buf, size_t size)
+{
+	struct ostream os = { .buf = buf, .size = size };
+	unsigned long flags;
+
+	spin_lock_irqsave(&dir->lock, flags);
+	__ref_tracker_dir_pr_ostream(dir, 16, &os);
+	spin_unlock_irqrestore(&dir->lock, flags);
+
+	return os.used;
+}
+EXPORT_SYMBOL(ref_tracker_dir_snprint);
+
 void ref_tracker_dir_exit(struct ref_tracker_dir *dir)
 {
 	struct ref_tracker *tracker, *n;