Re: [RFC PATCH v4 2/2] zram: keep the hot statistics per cpu
From: Sergey Senozhatsky
Date: Sat Nov 21 2026 - 03:36:07 EST
Once again, recovered from the SPAM folder. Sorry for the delay.
On (26/10/17 11:15), Hongfeng Chen wrote:
[..]
> +#define zram_pcpu_add(zram_dev, field, val) \
> +do { \
> + struct zram *__zram = (zram_dev); \
> + struct zram_pcpu_stats *__stats; \
> + \
> + __stats = get_cpu_ptr(__zram->pcpu_stats); \
> + u64_stats_update_begin(&__stats->syncp); \
> + WRITE_ONCE(__stats->field, __stats->field + (val)); \
> + u64_stats_update_end(&__stats->syncp); \
> + put_cpu_ptr(__zram->pcpu_stats); \
> +} while (0)
> +
> +static void zram_pcpu_sum(struct zram *zram, struct zram_stats_sum *sum)
> +{
> + int cpu;
> +
> + memset(sum, 0, sizeof(*sum));
> +
> + for_each_possible_cpu(cpu) {
> + struct zram_pcpu_stats *stats;
> + unsigned int start;
> + s64 pages, bytes, same, huge;
> + u64 notify;
> +
> + stats = per_cpu_ptr(zram->pcpu_stats, cpu);
> + do {
> + start = u64_stats_fetch_begin(&stats->syncp);
> + pages = READ_ONCE(stats->pages_stored);
> + bytes = READ_ONCE(stats->compr_data_size);
> + same = READ_ONCE(stats->same_pages);
> + huge = READ_ONCE(stats->huge_pages);
> + notify = READ_ONCE(stats->notify_free);
> + } while (u64_stats_fetch_retry(&stats->syncp, start));
> +
> + sum->pages_stored += pages;
> + sum->compr_data_size += bytes;
> + sum->same_pages += same;
> + sum->huge_pages += huge;
> + sum->notify_free += notify;
> + }
> +}
> +
> #ifdef CONFIG_ZRAM_WRITEBACK
> #define INVALID_BDEV_BLOCK (~0UL)
>
> @@ -919,6 +969,8 @@ static void zram_account_writeback_submit(struct zram *zram)
> static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req)
> {
> unsigned long index = req->pps->index;
> + struct zram_pcpu_stats *stats;
> + s64 huge, size;
> int err;
>
> err = blk_status_to_errno(req->bio.bi_status);
> @@ -947,9 +999,14 @@ static int zram_writeback_complete(struct zram *zram, struct zram_wb_req *req)
> }
>
> clear_slot_flag(zram, index, ZRAM_IDLE);
> - if (test_slot_flag(zram, index, ZRAM_HUGE))
> - atomic64_dec(&zram->stats.huge_pages);
> - atomic64_sub(get_slot_size(zram, index), &zram->stats.compr_data_size);
> + huge = test_slot_flag(zram, index, ZRAM_HUGE) ? -1 : 0;
> + size = get_slot_size(zram, index);
> + stats = get_cpu_ptr(zram->pcpu_stats);
> + u64_stats_update_begin(&stats->syncp);
> + WRITE_ONCE(stats->huge_pages, stats->huge_pages + huge);
> + WRITE_ONCE(stats->compr_data_size, stats->compr_data_size - size);
> + u64_stats_update_end(&stats->syncp);
> + put_cpu_ptr(zram->pcpu_stats);
Thanks for reminding me that I actually looked into per-CPU stats a while
ago. And I had much less code, IIRC, let me find and rebase that patch,
these WRITE_ONCE()-s, etc. are hard to maintain. Let me find my patch.