zram: record accessed second
authorMinchan Kim <minchan@kernel.org>
Fri, 8 Jun 2018 00:05:45 +0000 (17:05 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Fri, 8 Jun 2018 00:34:34 +0000 (17:34 -0700)
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 records last access time of each block of zram so that With
upcoming zram memory tracking, it could help userspace developers to
reduce memory footprint.

Link: http://lkml.kernel.org/r/20180416090946.63057-4-minchan@kernel.org
Signed-off-by: Minchan Kim <minchan@kernel.org>
Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
drivers/block/zram/zram_drv.c
drivers/block/zram/zram_drv.h

index 777fb3339f598e698cdee580735770ec4a2dd611..7fc10e2ad734a8c6a483254345bc21d2e0fed606 100644 (file)
@@ -107,6 +107,16 @@ static inline void zram_set_element(struct zram *zram, u32 index,
        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)
 {
        return zram->table[index].element;
@@ -806,6 +816,8 @@ static void zram_free_page(struct zram *zram, size_t index)
 {
        unsigned long handle;
 
+       zram_reset_access(zram, index);
+
        if (zram_test_flag(zram, index, ZRAM_HUGE)) {
                zram_clear_flag(zram, index, ZRAM_HUGE);
                atomic64_dec(&zram->stats.huge_pages);
@@ -1177,6 +1189,10 @@ static int zram_bvec_rw(struct zram *zram, struct bio_vec *bvec, u32 index,
 
        generic_end_io_acct(q, rw_acct, &zram->disk->part0, start_time);
 
+       zram_slot_lock(zram, index);
+       zram_accessed(zram, index);
+       zram_slot_unlock(zram, index);
+
        if (unlikely(ret < 0)) {
                if (!is_write)
                        atomic64_inc(&zram->stats.failed_reads);
index ff0547bdb58678e9ee7dd4fc71407b8610d5bb32..1075218e88b2a7f4e74bb20de7dfccac7ad83e64 100644 (file)
@@ -61,6 +61,7 @@ struct zram_table_entry {
                unsigned long element;
        };
        unsigned long value;
+       u64 ac_time;
 };
 
 struct zram_stats {