Skip to content

Commit 23eddf3

Browse files
minchanktorvalds
authored andcommitted
zram: add bd_stat statistics
bd_stat represents things that happened in the backing device. Currently it supports bd_counts, bd_reads and bd_writes which are helpful to understand wearout of flash and memory saving. [minchan@kernel.org: v4] Link: http://lkml.kernel.org/r/20181203024045.153534-7-minchan@kernel.org Link: http://lkml.kernel.org/r/20181127055429.251614-7-minchan@kernel.org Signed-off-by: Minchan Kim <minchan@kernel.org> Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Joey Pabalinas <joeypabalinas@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a939888 commit 23eddf3

4 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎Documentation/ABI/testing/sysfs-block-zram‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ Contact: Minchan Kim <minchan@kernel.org>
113113
Description:
114114
The writeback file is write-only and trigger idle and/or
115115
huge page writeback to backing device.
116+
117+
What: /sys/block/zram<id>/bd_stat
118+
Date: November 2018
119+
Contact: Minchan Kim <minchan@kernel.org>
120+
Description:
121+
The bd_stat file is read-only and represents backing device's
122+
statistics (bd_count, bd_reads, bd_writes) in a format
123+
similar to block layer statistics file format.

‎Documentation/blockdev/zram.txt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,17 @@ line of text and contains the following stats separated by whitespace:
221221
pages_compacted the number of pages freed during compaction
222222
huge_pages the number of incompressible pages
223223

224+
File /sys/block/zram<id>/bd_stat
225+
226+
The stat file represents device's backing device statistics. It consists of
227+
a single line of text and contains the following stats separated by whitespace:
228+
bd_count size of data written in backing device.
229+
Unit: 4K bytes
230+
bd_reads the number of reads from backing device
231+
Unit: 4K bytes
232+
bd_writes the number of writes to backing device
233+
Unit: 4K bytes
234+
224235
9) Deactivate:
225236
swapoff /dev/zram0
226237
umount /dev/zram1

‎drivers/block/zram/zram_drv.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ static unsigned long alloc_block_bdev(struct zram *zram)
502502
if (test_and_set_bit(blk_idx, zram->bitmap))
503503
goto retry;
504504

505+
atomic64_inc(&zram->stats.bd_count);
505506
return blk_idx;
506507
}
507508

@@ -511,6 +512,7 @@ static void free_block_bdev(struct zram *zram, unsigned long blk_idx)
511512

512513
was_set = test_and_clear_bit(blk_idx, zram->bitmap);
513514
WARN_ON_ONCE(!was_set);
515+
atomic64_dec(&zram->stats.bd_count);
514516
}
515517

516518
static void zram_page_end_io(struct bio *bio)
@@ -668,6 +670,7 @@ static ssize_t writeback_store(struct device *dev,
668670
continue;
669671
}
670672

673+
atomic64_inc(&zram->stats.bd_writes);
671674
/*
672675
* We released zram_slot_lock so need to check if the slot was
673676
* changed. If there is freeing for the slot, we can catch it
@@ -757,6 +760,7 @@ static int read_from_bdev_sync(struct zram *zram, struct bio_vec *bvec,
757760
static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
758761
unsigned long entry, struct bio *parent, bool sync)
759762
{
763+
atomic64_inc(&zram->stats.bd_reads);
760764
if (sync)
761765
return read_from_bdev_sync(zram, bvec, entry, parent);
762766
else
@@ -1013,6 +1017,25 @@ static ssize_t mm_stat_show(struct device *dev,
10131017
return ret;
10141018
}
10151019

1020+
#ifdef CONFIG_ZRAM_WRITEBACK
1021+
static ssize_t bd_stat_show(struct device *dev,
1022+
struct device_attribute *attr, char *buf)
1023+
{
1024+
struct zram *zram = dev_to_zram(dev);
1025+
ssize_t ret;
1026+
1027+
down_read(&zram->init_lock);
1028+
ret = scnprintf(buf, PAGE_SIZE,
1029+
"%8llu %8llu %8llu\n",
1030+
(u64)atomic64_read(&zram->stats.bd_count) * (PAGE_SHIFT - 12),
1031+
(u64)atomic64_read(&zram->stats.bd_reads) * (PAGE_SHIFT - 12),
1032+
(u64)atomic64_read(&zram->stats.bd_writes) * (PAGE_SHIFT - 12));
1033+
up_read(&zram->init_lock);
1034+
1035+
return ret;
1036+
}
1037+
#endif
1038+
10161039
static ssize_t debug_stat_show(struct device *dev,
10171040
struct device_attribute *attr, char *buf)
10181041
{
@@ -1033,6 +1056,9 @@ static ssize_t debug_stat_show(struct device *dev,
10331056

10341057
static DEVICE_ATTR_RO(io_stat);
10351058
static DEVICE_ATTR_RO(mm_stat);
1059+
#ifdef CONFIG_ZRAM_WRITEBACK
1060+
static DEVICE_ATTR_RO(bd_stat);
1061+
#endif
10361062
static DEVICE_ATTR_RO(debug_stat);
10371063

10381064
static void zram_meta_free(struct zram *zram, u64 disksize)
@@ -1759,6 +1785,9 @@ static struct attribute *zram_disk_attrs[] = {
17591785
#endif
17601786
&dev_attr_io_stat.attr,
17611787
&dev_attr_mm_stat.attr,
1788+
#ifdef CONFIG_ZRAM_WRITEBACK
1789+
&dev_attr_bd_stat.attr,
1790+
#endif
17621791
&dev_attr_debug_stat.attr,
17631792
NULL,
17641793
};

‎drivers/block/zram/zram_drv.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ struct zram_stats {
8282
atomic_long_t max_used_pages; /* no. of maximum pages stored */
8383
atomic64_t writestall; /* no. of write slow paths */
8484
atomic64_t miss_free; /* no. of missed free */
85+
#ifdef CONFIG_ZRAM_WRITEBACK
86+
atomic64_t bd_count; /* no. of pages in backing device */
87+
atomic64_t bd_reads; /* no. of reads from backing device */
88+
atomic64_t bd_writes; /* no. of writes from backing device */
89+
#endif
8590
};
8691

8792
struct zram {

0 commit comments

Comments
 (0)