Commit 227c6c83 authored by Andrzej Hajda's avatar Andrzej Hajda Committed by Jakub Kicinski

lib/ref_tracker: add printing to memory buffer

Similar to stack_(depot|trace)_snprint the patch
adds helper to printing stats to memory buffer.
It will be helpful in case of debugfs.
Signed-off-by: default avatarAndrzej Hajda <andrzej.hajda@intel.com>
Reviewed-by: default avatarAndi Shyti <andi.shyti@linux.intel.com>
Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parent b6d7c0eb
...@@ -46,6 +46,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, ...@@ -46,6 +46,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir,
void ref_tracker_dir_print(struct ref_tracker_dir *dir, void ref_tracker_dir_print(struct ref_tracker_dir *dir,
unsigned int display_limit); 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, int ref_tracker_alloc(struct ref_tracker_dir *dir,
struct ref_tracker **trackerp, gfp_t gfp); struct ref_tracker **trackerp, gfp_t gfp);
...@@ -74,6 +76,12 @@ static inline void ref_tracker_dir_print(struct ref_tracker_dir *dir, ...@@ -74,6 +76,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, static inline int ref_tracker_alloc(struct ref_tracker_dir *dir,
struct ref_tracker **trackerp, struct ref_tracker **trackerp,
gfp_t gfp) gfp_t gfp)
......
...@@ -62,8 +62,27 @@ ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit) ...@@ -62,8 +62,27 @@ ref_tracker_get_stats(struct ref_tracker_dir *dir, unsigned int limit)
return stats; return stats;
} }
void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, struct ostream {
unsigned int display_limit) 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; struct ref_tracker_dir_stats *stats;
unsigned int i = 0, skipped; unsigned int i = 0, skipped;
...@@ -77,8 +96,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, ...@@ -77,8 +96,8 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir,
stats = ref_tracker_get_stats(dir, display_limit); stats = ref_tracker_get_stats(dir, display_limit);
if (IS_ERR(stats)) { if (IS_ERR(stats)) {
pr_err("%s@%pK: couldn't get stats, error %pe\n", pr_ostream(s, "%s@%pK: couldn't get stats, error %pe\n",
dir->name, dir, stats); dir->name, dir, stats);
return; return;
} }
...@@ -88,19 +107,27 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir, ...@@ -88,19 +107,27 @@ void ref_tracker_dir_print_locked(struct ref_tracker_dir *dir,
stack = stats->stacks[i].stack_handle; stack = stats->stacks[i].stack_handle;
if (sbuf && !stack_depot_snprint(stack, sbuf, STACK_BUF_SIZE, 4)) if (sbuf && !stack_depot_snprint(stack, sbuf, STACK_BUF_SIZE, 4))
sbuf[0] = 0; sbuf[0] = 0;
pr_err("%s@%pK has %d/%d users at\n%s\n", dir->name, dir, pr_ostream(s, "%s@%pK has %d/%d users at\n%s\n", dir->name, dir,
stats->stacks[i].count, stats->total, sbuf); stats->stacks[i].count, stats->total, sbuf);
skipped -= stats->stacks[i].count; skipped -= stats->stacks[i].count;
} }
if (skipped) if (skipped)
pr_err("%s@%pK skipped reports about %d/%d users.\n", pr_ostream(s, "%s@%pK skipped reports about %d/%d users.\n",
dir->name, dir, skipped, stats->total); dir->name, dir, skipped, stats->total);
kfree(sbuf); kfree(sbuf);
kfree(stats); kfree(stats);
} }
void ref_tracker_dir_print_locked(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_locked); EXPORT_SYMBOL(ref_tracker_dir_print_locked);
void ref_tracker_dir_print(struct ref_tracker_dir *dir, void ref_tracker_dir_print(struct ref_tracker_dir *dir,
...@@ -114,6 +141,19 @@ 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); 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) void ref_tracker_dir_exit(struct ref_tracker_dir *dir)
{ {
struct ref_tracker *tracker, *n; struct ref_tracker *tracker, *n;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment