zram: report maximum used memory
authorMinchan Kim <minchan@kernel.org>
Thu, 9 Oct 2014 22:29:55 +0000 (15:29 -0700)
committerAlex Shi <alex.shi@linaro.org>
Mon, 11 May 2015 13:23:25 +0000 (21:23 +0800)
Normally, zram user could get maximum memory usage zram consumed via
polling mem_used_total with sysfs in userspace.

But it has a critical problem because user can miss peak memory usage
during update inverval of polling.  For avoiding that, user should poll it
with shorter interval(ie, 0.0000000001s) with mlocking to avoid page fault
delay when memory pressure is heavy.  It would be troublesome.

This patch adds new knob "mem_used_max" so user could see the maximum
memory usage easily via reading the knob and reset it via "echo 0 >
/sys/block/zram0/mem_used_max".

Signed-off-by: Minchan Kim <minchan@kernel.org>
Reviewed-by: Dan Streetman <ddstreet@ieee.org>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Jerome Marchand <jmarchan@redhat.com>
Cc: <juno.choi@lge.com>
Cc: <seungho1.park@lge.com>
Cc: Luigi Semenzato <semenzato@google.com>
Cc: Nitin Gupta <ngupta@vflare.org>
Cc: Seth Jennings <sjennings@variantweb.net>
Reviewed-by: David Horner <ds2horner@gmail.com>
Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 461a8eee6af3b55745be64bea403ed0b743563cf)
Signed-off-by: Alex Shi <alex.shi@linaro.org>
Documentation/ABI/testing/sysfs-block-zram
Documentation/blockdev/zram.txt
drivers/block/zram/zram_drv.c
drivers/block/zram/zram_drv.h

index f861cbfab6b1a9590963d594c2f28f3de1998ed8..31db44f01936fbb1a5ac5a56a087cbc60929aed8 100644 (file)
@@ -128,6 +128,16 @@ Description:
                statistic.
                Unit: bytes
 
+What:          /sys/block/zram<id>/mem_used_max
+Date:          August 2014
+Contact:       Minchan Kim <minchan@kernel.org>
+Description:
+               The mem_used_max file is read/write and specifies the amount
+               of maximum memory zram have consumed to store compressed data.
+               For resetting the value, you should write "0". Otherwise,
+               you could see -EINVAL.
+               Unit: bytes
+
 What:          /sys/block/zram<id>/mem_limit
 Date:          August 2014
 Contact:       Minchan Kim <minchan@kernel.org>
index 4331ebf94bf0e764742c3436b2a883a13e261fe5..5cd0bd903f547b2cd05b8d923fb3af38c032e8dd 100644 (file)
@@ -110,6 +110,7 @@ size of the disk when not in use so a huge zram is wasteful.
                orig_data_size
                compr_data_size
                mem_used_total
+               mem_used_max
 
 8) Deactivate:
        swapoff /dev/zram0
index 3f4da06c89c08b9bd5fbc48efc9f838c6c66a0db..204b7fa9d78eb35f155a7a7a7c2e1cd7b4f667e6 100644 (file)
@@ -153,6 +153,41 @@ static ssize_t mem_limit_store(struct device *dev,
        return len;
 }
 
+static ssize_t mem_used_max_show(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       u64 val = 0;
+       struct zram *zram = dev_to_zram(dev);
+
+       down_read(&zram->init_lock);
+       if (init_done(zram))
+               val = atomic_long_read(&zram->stats.max_used_pages);
+       up_read(&zram->init_lock);
+
+       return scnprintf(buf, PAGE_SIZE, "%llu\n", val << PAGE_SHIFT);
+}
+
+static ssize_t mem_used_max_store(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t len)
+{
+       int err;
+       unsigned long val;
+       struct zram *zram = dev_to_zram(dev);
+       struct zram_meta *meta = zram->meta;
+
+       err = kstrtoul(buf, 10, &val);
+       if (err || val != 0)
+               return -EINVAL;
+
+       down_read(&zram->init_lock);
+       if (init_done(zram))
+               atomic_long_set(&zram->stats.max_used_pages,
+                               zs_get_total_pages(meta->mem_pool));
+       up_read(&zram->init_lock);
+
+       return len;
+}
+
 static ssize_t max_comp_streams_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t len)
 {
@@ -464,6 +499,21 @@ out_cleanup:
        return ret;
 }
 
+static inline void update_used_max(struct zram *zram,
+                                       const unsigned long pages)
+{
+       int old_max, cur_max;
+
+       old_max = atomic_long_read(&zram->stats.max_used_pages);
+
+       do {
+               cur_max = old_max;
+               if (pages > cur_max)
+                       old_max = atomic_long_cmpxchg(
+                               &zram->stats.max_used_pages, cur_max, pages);
+       } while (old_max != cur_max);
+}
+
 static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
                           int offset)
 {
@@ -475,6 +525,7 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
        struct zram_meta *meta = zram->meta;
        struct zcomp_strm *zstrm;
        bool locked = false;
+       unsigned long alloced_pages;
 
        page = bvec->bv_page;
        if (is_partial_io(bvec)) {
@@ -544,13 +595,15 @@ static int zram_bvec_write(struct zram *zram, struct bio_vec *bvec, u32 index,
                goto out;
        }
 
-       if (zram->limit_pages &&
-               zs_get_total_pages(meta->mem_pool) > zram->limit_pages) {
+       alloced_pages = zs_get_total_pages(meta->mem_pool);
+       if (zram->limit_pages && alloced_pages > zram->limit_pages) {
                zs_free(meta->mem_pool, handle);
                ret = -ENOMEM;
                goto out;
        }
 
+       update_used_max(zram, alloced_pages);
+
        cmem = zs_map_object(meta->mem_pool, handle, ZS_MM_WO);
 
        if ((clen == PAGE_SIZE) && !is_partial_io(bvec)) {
@@ -899,6 +952,8 @@ static DEVICE_ATTR(orig_data_size, S_IRUGO, orig_data_size_show, NULL);
 static DEVICE_ATTR(mem_used_total, S_IRUGO, mem_used_total_show, NULL);
 static DEVICE_ATTR(mem_limit, S_IRUGO | S_IWUSR, mem_limit_show,
                mem_limit_store);
+static DEVICE_ATTR(mem_used_max, S_IRUGO | S_IWUSR, mem_used_max_show,
+               mem_used_max_store);
 static DEVICE_ATTR(max_comp_streams, S_IRUGO | S_IWUSR,
                max_comp_streams_show, max_comp_streams_store);
 static DEVICE_ATTR(comp_algorithm, S_IRUGO | S_IWUSR,
@@ -928,6 +983,7 @@ static struct attribute *zram_disk_attrs[] = {
        &dev_attr_compr_data_size.attr,
        &dev_attr_mem_used_total.attr,
        &dev_attr_mem_limit.attr,
+       &dev_attr_mem_used_max.attr,
        &dev_attr_max_comp_streams.attr,
        &dev_attr_comp_algorithm.attr,
        NULL,
index b7aa9c21553f8edf83045fe7b468f989c3798464..c6ee271317f5b344619bdc7c01bc16a858f18a9f 100644 (file)
@@ -90,6 +90,7 @@ struct zram_stats {
        atomic64_t notify_free; /* no. of swap slot free notifications */
        atomic64_t zero_pages;          /* no. of zero filled pages */
        atomic64_t pages_stored;        /* no. of pages currently stored */
+       atomic_long_t max_used_pages;   /* no. of maximum pages stored */
 };
 
 struct zram_meta {