Commit c0265342 authored by Minchan Kim's avatar Minchan Kim Committed by Linus Torvalds

zram: introduce zram memory tracking

zRam as swap is useful for small memory device.  However, swap means
those pages on zram are mostly cold pages due to VM's LRU algorithm.
Especially, once init data for application are touched for launching,
they tend to be not accessed any more and finally swapped out.  zRAM can
store such cold pages as compressed form but it's pointless to keep in
memory.  Better idea is app developers free them directly rather than
remaining them on heap.

This patch tell us last access time of each block of zram via "cat
/sys/kernel/debug/zram/zram0/block_state".

The output is as follows,
      300    75.033841 .wh
      301    63.806904 s..
      302    63.806919 ..h

First column is zram's block index and 3rh one represents symbol (s:
same page w: written page to backing store h: huge page) of the block
state.  Second column represents usec time unit of the block was last
accessed.  So above example means the 300th block is accessed at
75.033851 second and it was huge so it was written to the backing store.

Admin can leverage this information to catch cold|incompressible pages
of process with *pagemap* once part of heaps are swapped out.

I used the feature a few years ago to find memory hoggers in userspace
to notify them what memory they have wasted without touch for a long
time.  With it, they could reduce unnecessary memory space.  However, at
that time, I hacked up zram for the feature but now I need the feature
again so I decided it would be better to upstream rather than keeping it
alone.  I hope I submit the userspace tool to use the feature soon.

[akpm@linux-foundation.org: fix i386 printk warning]
[minchan@kernel.org: use ktime_get_boottime() instead of sched_clock()]
  Link: http://lkml.kernel.org/r/20180420063525.GA253739@rodete-desktop-imager.corp.google.com
[akpm@linux-foundation.org: documentation tweak]
[akpm@linux-foundation.org: fix i386 printk warning]
[minchan@kernel.org: fix compile warning]
  Link: http://lkml.kernel.org/r/20180508104849.GA8209@rodete-desktop-imager.corp.google.com
[rdunlap@infradead.org: fix printk formats]
  Link: http://lkml.kernel.org/r/3652ccb1-96ef-0b0b-05d1-f661d7733dcc@infradead.org
Link: http://lkml.kernel.org/r/20180416090946.63057-5-minchan@kernel.orgSigned-off-by: default avatarMinchan Kim <minchan@kernel.org>
Signed-off-by: default avatarRandy Dunlap <rdunlap@infradead.org>
Reviewed-by: default avatarSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Acked-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent d7eac6b6
...@@ -243,5 +243,29 @@ to backing storage rather than keeping it in memory. ...@@ -243,5 +243,29 @@ to backing storage rather than keeping it in memory.
User should set up backing device via /sys/block/zramX/backing_dev User should set up backing device via /sys/block/zramX/backing_dev
before disksize setting. before disksize setting.
= memory tracking
With CONFIG_ZRAM_MEMORY_TRACKING, user can know information of the
zram block. It could be useful to catch cold or incompressible
pages of the process with*pagemap.
If you enable the feature, you could see block state via
/sys/kernel/debug/zram/zram0/block_state". The output is as follows,
300 75.033841 .wh
301 63.806904 s..
302 63.806919 ..h
First column is zram's block index.
Second column is access time since the system was booted
Third column is state of the block.
(s: same page
w: written page to backing store
h: huge page)
First line of above example says 300th block is accessed at 75.033841sec
and the block's state is huge so it is written back to the backing
storage. It's a debugging feature so anyone shouldn't rely on it to work
properly.
Nitin Gupta Nitin Gupta
ngupta@vflare.org ngupta@vflare.org
...@@ -13,7 +13,7 @@ config ZRAM ...@@ -13,7 +13,7 @@ config ZRAM
It has several use cases, for example: /tmp storage, use as swap It has several use cases, for example: /tmp storage, use as swap
disks and maybe many more. disks and maybe many more.
See zram.txt for more information. See Documentation/blockdev/zram.txt for more information.
config ZRAM_WRITEBACK config ZRAM_WRITEBACK
bool "Write back incompressible page to backing device" bool "Write back incompressible page to backing device"
...@@ -25,4 +25,14 @@ config ZRAM_WRITEBACK ...@@ -25,4 +25,14 @@ config ZRAM_WRITEBACK
For this feature, admin should set up backing device via For this feature, admin should set up backing device via
/sys/block/zramX/backing_dev. /sys/block/zramX/backing_dev.
See zram.txt for more infomration. See Documentation/blockdev/zram.txt for more information.
config ZRAM_MEMORY_TRACKING
bool "Track zRam block status"
depends on ZRAM && DEBUG_FS
help
With this feature, admin can track the state of allocated blocks
of zRAM. Admin could see the information via
/sys/kernel/debug/zram/zramX/block_state.
See Documentation/blockdev/zram.txt for more information.
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <linux/err.h> #include <linux/err.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
#include <linux/debugfs.h>
#include <linux/cpuhotplug.h> #include <linux/cpuhotplug.h>
#include "zram_drv.h" #include "zram_drv.h"
...@@ -67,6 +68,13 @@ static inline bool init_done(struct zram *zram) ...@@ -67,6 +68,13 @@ static inline bool init_done(struct zram *zram)
return zram->disksize; return zram->disksize;
} }
static inline bool zram_allocated(struct zram *zram, u32 index)
{
return (zram->table[index].value >> (ZRAM_FLAG_SHIFT + 1)) ||
zram->table[index].handle;
}
static inline struct zram *dev_to_zram(struct device *dev) static inline struct zram *dev_to_zram(struct device *dev)
{ {
return (struct zram *)dev_to_disk(dev)->private_data; return (struct zram *)dev_to_disk(dev)->private_data;
...@@ -83,7 +91,7 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle) ...@@ -83,7 +91,7 @@ static void zram_set_handle(struct zram *zram, u32 index, unsigned long handle)
} }
/* flag operations require table entry bit_spin_lock() being held */ /* flag operations require table entry bit_spin_lock() being held */
static int zram_test_flag(struct zram *zram, u32 index, static bool zram_test_flag(struct zram *zram, u32 index,
enum zram_pageflags flag) enum zram_pageflags flag)
{ {
return zram->table[index].value & BIT(flag); return zram->table[index].value & BIT(flag);
...@@ -107,16 +115,6 @@ static inline void zram_set_element(struct zram *zram, u32 index, ...@@ -107,16 +115,6 @@ static inline void zram_set_element(struct zram *zram, u32 index,
zram->table[index].element = element; zram->table[index].element = element;
} }
static void zram_accessed(struct zram *zram, u32 index)
{
zram->table[index].ac_time = sched_clock();
}
static void zram_reset_access(struct zram *zram, u32 index)
{
zram->table[index].ac_time = 0;
}
static unsigned long zram_get_element(struct zram *zram, u32 index) static unsigned long zram_get_element(struct zram *zram, u32 index)
{ {
return zram->table[index].element; return zram->table[index].element;
...@@ -620,6 +618,114 @@ static int read_from_bdev(struct zram *zram, struct bio_vec *bvec, ...@@ -620,6 +618,114 @@ static int read_from_bdev(struct zram *zram, struct bio_vec *bvec,
static void zram_wb_clear(struct zram *zram, u32 index) {} static void zram_wb_clear(struct zram *zram, u32 index) {}
#endif #endif
#ifdef CONFIG_ZRAM_MEMORY_TRACKING
static struct dentry *zram_debugfs_root;
static void zram_debugfs_create(void)
{
zram_debugfs_root = debugfs_create_dir("zram", NULL);
}
static void zram_debugfs_destroy(void)
{
debugfs_remove_recursive(zram_debugfs_root);
}
static void zram_accessed(struct zram *zram, u32 index)
{
zram->table[index].ac_time = ktime_get_boottime();
}
static void zram_reset_access(struct zram *zram, u32 index)
{
zram->table[index].ac_time = 0;
}
static ssize_t read_block_state(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
char *kbuf;
ssize_t index, written = 0;
struct zram *zram = file->private_data;
unsigned long nr_pages = zram->disksize >> PAGE_SHIFT;
struct timespec64 ts;
kbuf = kvmalloc(count, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;
down_read(&zram->init_lock);
if (!init_done(zram)) {
up_read(&zram->init_lock);
kvfree(kbuf);
return -EINVAL;
}
for (index = *ppos; index < nr_pages; index++) {
int copied;
zram_slot_lock(zram, index);
if (!zram_allocated(zram, index))
goto next;
ts = ktime_to_timespec64(zram->table[index].ac_time);
copied = snprintf(kbuf + written, count,
"%12zd %12lld.%06lu %c%c%c\n",
index, (s64)ts.tv_sec,
ts.tv_nsec / NSEC_PER_USEC,
zram_test_flag(zram, index, ZRAM_SAME) ? 's' : '.',
zram_test_flag(zram, index, ZRAM_WB) ? 'w' : '.',
zram_test_flag(zram, index, ZRAM_HUGE) ? 'h' : '.');
if (count < copied) {
zram_slot_unlock(zram, index);
break;
}
written += copied;
count -= copied;
next:
zram_slot_unlock(zram, index);
*ppos += 1;
}
up_read(&zram->init_lock);
if (copy_to_user(buf, kbuf, written))
written = -EFAULT;
kvfree(kbuf);
return written;
}
static const struct file_operations proc_zram_block_state_op = {
.open = simple_open,
.read = read_block_state,
.llseek = default_llseek,
};
static void zram_debugfs_register(struct zram *zram)
{
if (!zram_debugfs_root)
return;
zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
zram_debugfs_root);
debugfs_create_file("block_state", 0400, zram->debugfs_dir,
zram, &proc_zram_block_state_op);
}
static void zram_debugfs_unregister(struct zram *zram)
{
debugfs_remove_recursive(zram->debugfs_dir);
}
#else
static void zram_debugfs_create(void) {};
static void zram_debugfs_destroy(void) {};
static void zram_accessed(struct zram *zram, u32 index) {};
static void zram_reset_access(struct zram *zram, u32 index) {};
static void zram_debugfs_register(struct zram *zram) {};
static void zram_debugfs_unregister(struct zram *zram) {};
#endif
/* /*
* We switched to per-cpu streams and this attr is not needed anymore. * We switched to per-cpu streams and this attr is not needed anymore.
...@@ -1604,6 +1710,7 @@ static int zram_add(void) ...@@ -1604,6 +1710,7 @@ static int zram_add(void)
} }
strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor)); strlcpy(zram->compressor, default_compressor, sizeof(zram->compressor));
zram_debugfs_register(zram);
pr_info("Added device: %s\n", zram->disk->disk_name); pr_info("Added device: %s\n", zram->disk->disk_name);
return device_id; return device_id;
...@@ -1637,6 +1744,7 @@ static int zram_remove(struct zram *zram) ...@@ -1637,6 +1744,7 @@ static int zram_remove(struct zram *zram)
zram->claim = true; zram->claim = true;
mutex_unlock(&bdev->bd_mutex); mutex_unlock(&bdev->bd_mutex);
zram_debugfs_unregister(zram);
/* /*
* Remove sysfs first, so no one will perform a disksize * Remove sysfs first, so no one will perform a disksize
* store while we destroy the devices. This also helps during * store while we destroy the devices. This also helps during
...@@ -1739,6 +1847,7 @@ static void destroy_devices(void) ...@@ -1739,6 +1847,7 @@ static void destroy_devices(void)
{ {
class_unregister(&zram_control_class); class_unregister(&zram_control_class);
idr_for_each(&zram_index_idr, &zram_remove_cb, NULL); idr_for_each(&zram_index_idr, &zram_remove_cb, NULL);
zram_debugfs_destroy();
idr_destroy(&zram_index_idr); idr_destroy(&zram_index_idr);
unregister_blkdev(zram_major, "zram"); unregister_blkdev(zram_major, "zram");
cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE); cpuhp_remove_multi_state(CPUHP_ZCOMP_PREPARE);
...@@ -1760,6 +1869,7 @@ static int __init zram_init(void) ...@@ -1760,6 +1869,7 @@ static int __init zram_init(void)
return ret; return ret;
} }
zram_debugfs_create();
zram_major = register_blkdev(0, "zram"); zram_major = register_blkdev(0, "zram");
if (zram_major <= 0) { if (zram_major <= 0) {
pr_err("Unable to get major number\n"); pr_err("Unable to get major number\n");
......
...@@ -61,7 +61,9 @@ struct zram_table_entry { ...@@ -61,7 +61,9 @@ struct zram_table_entry {
unsigned long element; unsigned long element;
}; };
unsigned long value; unsigned long value;
u64 ac_time; #ifdef CONFIG_ZRAM_MEMORY_TRACKING
ktime_t ac_time;
#endif
}; };
struct zram_stats { struct zram_stats {
...@@ -110,5 +112,8 @@ struct zram { ...@@ -110,5 +112,8 @@ struct zram {
unsigned long nr_pages; unsigned long nr_pages;
spinlock_t bitmap_lock; spinlock_t bitmap_lock;
#endif #endif
#ifdef CONFIG_ZRAM_MEMORY_TRACKING
struct dentry *debugfs_dir;
#endif
}; };
#endif #endif
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