1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2021 Google LLC
6 * Author: Daeho Jeong <daehojeong@google.com>
10 #include <linux/f2fs_fs.h>
11 #include <linux/seq_file.h>
15 #include <trace/events/f2fs.h>
17 static struct kmem_cache *bio_iostat_ctx_cache;
18 static mempool_t *bio_iostat_ctx_pool;
20 static inline unsigned long long iostat_get_avg_bytes(struct f2fs_sb_info *sbi,
21 enum iostat_type type)
23 return sbi->iostat_count[type] ? div64_u64(sbi->iostat_bytes[type],
24 sbi->iostat_count[type]) : 0;
27 #define IOSTAT_INFO_SHOW(name, type) \
28 seq_printf(seq, "%-23s %-16llu %-16llu %-16llu\n", \
29 name":", sbi->iostat_bytes[type], \
30 sbi->iostat_count[type], \
31 iostat_get_avg_bytes(sbi, type))
33 int __maybe_unused iostat_info_seq_show(struct seq_file *seq, void *offset)
35 struct super_block *sb = seq->private;
36 struct f2fs_sb_info *sbi = F2FS_SB(sb);
38 if (!sbi->iostat_enable)
41 seq_printf(seq, "time: %-16llu\n", ktime_get_real_seconds());
42 seq_printf(seq, "\t\t\t%-16s %-16s %-16s\n",
43 "io_bytes", "count", "avg_bytes");
45 /* print app write IOs */
46 seq_puts(seq, "[WRITE]\n");
47 IOSTAT_INFO_SHOW("app buffered data", APP_BUFFERED_IO);
48 IOSTAT_INFO_SHOW("app direct data", APP_DIRECT_IO);
49 IOSTAT_INFO_SHOW("app mapped data", APP_MAPPED_IO);
50 IOSTAT_INFO_SHOW("app buffered cdata", APP_BUFFERED_CDATA_IO);
51 IOSTAT_INFO_SHOW("app mapped cdata", APP_MAPPED_CDATA_IO);
53 /* print fs write IOs */
54 IOSTAT_INFO_SHOW("fs data", FS_DATA_IO);
55 IOSTAT_INFO_SHOW("fs cdata", FS_CDATA_IO);
56 IOSTAT_INFO_SHOW("fs node", FS_NODE_IO);
57 IOSTAT_INFO_SHOW("fs meta", FS_META_IO);
58 IOSTAT_INFO_SHOW("fs gc data", FS_GC_DATA_IO);
59 IOSTAT_INFO_SHOW("fs gc node", FS_GC_NODE_IO);
60 IOSTAT_INFO_SHOW("fs cp data", FS_CP_DATA_IO);
61 IOSTAT_INFO_SHOW("fs cp node", FS_CP_NODE_IO);
62 IOSTAT_INFO_SHOW("fs cp meta", FS_CP_META_IO);
64 /* print app read IOs */
65 seq_puts(seq, "[READ]\n");
66 IOSTAT_INFO_SHOW("app buffered data", APP_BUFFERED_READ_IO);
67 IOSTAT_INFO_SHOW("app direct data", APP_DIRECT_READ_IO);
68 IOSTAT_INFO_SHOW("app mapped data", APP_MAPPED_READ_IO);
69 IOSTAT_INFO_SHOW("app buffered cdata", APP_BUFFERED_CDATA_READ_IO);
70 IOSTAT_INFO_SHOW("app mapped cdata", APP_MAPPED_CDATA_READ_IO);
72 /* print fs read IOs */
73 IOSTAT_INFO_SHOW("fs data", FS_DATA_READ_IO);
74 IOSTAT_INFO_SHOW("fs gc data", FS_GDATA_READ_IO);
75 IOSTAT_INFO_SHOW("fs cdata", FS_CDATA_READ_IO);
76 IOSTAT_INFO_SHOW("fs node", FS_NODE_READ_IO);
77 IOSTAT_INFO_SHOW("fs meta", FS_META_READ_IO);
80 seq_puts(seq, "[OTHER]\n");
81 IOSTAT_INFO_SHOW("fs discard", FS_DISCARD_IO);
82 IOSTAT_INFO_SHOW("fs flush", FS_FLUSH_IO);
83 IOSTAT_INFO_SHOW("fs zone reset", FS_ZONE_RESET_IO);
88 static inline void __record_iostat_latency(struct f2fs_sb_info *sbi)
91 struct f2fs_iostat_latency iostat_lat[MAX_IO_TYPE][NR_PAGE_TYPE];
92 struct iostat_lat_info *io_lat = sbi->iostat_io_lat;
95 spin_lock_irqsave(&sbi->iostat_lat_lock, flags);
96 for (idx = 0; idx < MAX_IO_TYPE; idx++) {
97 for (io = 0; io < NR_PAGE_TYPE; io++) {
98 iostat_lat[idx][io].peak_lat =
99 jiffies_to_msecs(io_lat->peak_lat[idx][io]);
100 iostat_lat[idx][io].cnt = io_lat->bio_cnt[idx][io];
101 iostat_lat[idx][io].avg_lat = iostat_lat[idx][io].cnt ?
102 jiffies_to_msecs(io_lat->sum_lat[idx][io]) / iostat_lat[idx][io].cnt : 0;
103 io_lat->sum_lat[idx][io] = 0;
104 io_lat->peak_lat[idx][io] = 0;
105 io_lat->bio_cnt[idx][io] = 0;
108 spin_unlock_irqrestore(&sbi->iostat_lat_lock, flags);
110 trace_f2fs_iostat_latency(sbi, iostat_lat);
113 static inline void f2fs_record_iostat(struct f2fs_sb_info *sbi)
115 unsigned long long iostat_diff[NR_IO_TYPE];
119 if (time_is_after_jiffies(sbi->iostat_next_period))
122 /* Need double check under the lock */
123 spin_lock_irqsave(&sbi->iostat_lock, flags);
124 if (time_is_after_jiffies(sbi->iostat_next_period)) {
125 spin_unlock_irqrestore(&sbi->iostat_lock, flags);
128 sbi->iostat_next_period = jiffies +
129 msecs_to_jiffies(sbi->iostat_period_ms);
131 for (i = 0; i < NR_IO_TYPE; i++) {
132 iostat_diff[i] = sbi->iostat_bytes[i] -
133 sbi->prev_iostat_bytes[i];
134 sbi->prev_iostat_bytes[i] = sbi->iostat_bytes[i];
136 spin_unlock_irqrestore(&sbi->iostat_lock, flags);
138 trace_f2fs_iostat(sbi, iostat_diff);
140 __record_iostat_latency(sbi);
143 void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
145 struct iostat_lat_info *io_lat = sbi->iostat_io_lat;
148 spin_lock_irq(&sbi->iostat_lock);
149 for (i = 0; i < NR_IO_TYPE; i++) {
150 sbi->iostat_count[i] = 0;
151 sbi->iostat_bytes[i] = 0;
152 sbi->prev_iostat_bytes[i] = 0;
154 spin_unlock_irq(&sbi->iostat_lock);
156 spin_lock_irq(&sbi->iostat_lat_lock);
157 memset(io_lat, 0, sizeof(struct iostat_lat_info));
158 spin_unlock_irq(&sbi->iostat_lat_lock);
161 static inline void __f2fs_update_iostat(struct f2fs_sb_info *sbi,
162 enum iostat_type type, unsigned long long io_bytes)
164 sbi->iostat_bytes[type] += io_bytes;
165 sbi->iostat_count[type]++;
168 void f2fs_update_iostat(struct f2fs_sb_info *sbi, struct inode *inode,
169 enum iostat_type type, unsigned long long io_bytes)
173 if (!sbi->iostat_enable)
176 spin_lock_irqsave(&sbi->iostat_lock, flags);
177 __f2fs_update_iostat(sbi, type, io_bytes);
179 if (type == APP_BUFFERED_IO || type == APP_DIRECT_IO)
180 __f2fs_update_iostat(sbi, APP_WRITE_IO, io_bytes);
182 if (type == APP_BUFFERED_READ_IO || type == APP_DIRECT_READ_IO)
183 __f2fs_update_iostat(sbi, APP_READ_IO, io_bytes);
185 #ifdef CONFIG_F2FS_FS_COMPRESSION
186 if (inode && f2fs_compressed_file(inode)) {
187 if (type == APP_BUFFERED_IO)
188 __f2fs_update_iostat(sbi, APP_BUFFERED_CDATA_IO, io_bytes);
190 if (type == APP_BUFFERED_READ_IO)
191 __f2fs_update_iostat(sbi, APP_BUFFERED_CDATA_READ_IO, io_bytes);
193 if (type == APP_MAPPED_READ_IO)
194 __f2fs_update_iostat(sbi, APP_MAPPED_CDATA_READ_IO, io_bytes);
196 if (type == APP_MAPPED_IO)
197 __f2fs_update_iostat(sbi, APP_MAPPED_CDATA_IO, io_bytes);
199 if (type == FS_DATA_READ_IO)
200 __f2fs_update_iostat(sbi, FS_CDATA_READ_IO, io_bytes);
202 if (type == FS_DATA_IO)
203 __f2fs_update_iostat(sbi, FS_CDATA_IO, io_bytes);
207 spin_unlock_irqrestore(&sbi->iostat_lock, flags);
209 f2fs_record_iostat(sbi);
212 static inline void __update_iostat_latency(struct bio_iostat_ctx *iostat_ctx,
213 enum iostat_lat_type lat_type)
215 unsigned long ts_diff;
216 unsigned int page_type = iostat_ctx->type;
217 struct f2fs_sb_info *sbi = iostat_ctx->sbi;
218 struct iostat_lat_info *io_lat = sbi->iostat_io_lat;
221 if (!sbi->iostat_enable)
224 ts_diff = jiffies - iostat_ctx->submit_ts;
225 if (page_type == META_FLUSH) {
227 } else if (page_type >= NR_PAGE_TYPE) {
228 f2fs_warn(sbi, "%s: %d over NR_PAGE_TYPE", __func__, page_type);
232 spin_lock_irqsave(&sbi->iostat_lat_lock, flags);
233 io_lat->sum_lat[lat_type][page_type] += ts_diff;
234 io_lat->bio_cnt[lat_type][page_type]++;
235 if (ts_diff > io_lat->peak_lat[lat_type][page_type])
236 io_lat->peak_lat[lat_type][page_type] = ts_diff;
237 spin_unlock_irqrestore(&sbi->iostat_lat_lock, flags);
240 void iostat_update_and_unbind_ctx(struct bio *bio)
242 struct bio_iostat_ctx *iostat_ctx = bio->bi_private;
243 enum iostat_lat_type lat_type;
245 if (op_is_write(bio_op(bio))) {
246 lat_type = bio->bi_opf & REQ_SYNC ?
247 WRITE_SYNC_IO : WRITE_ASYNC_IO;
248 bio->bi_private = iostat_ctx->sbi;
251 bio->bi_private = iostat_ctx->post_read_ctx;
254 __update_iostat_latency(iostat_ctx, lat_type);
255 mempool_free(iostat_ctx, bio_iostat_ctx_pool);
258 void iostat_alloc_and_bind_ctx(struct f2fs_sb_info *sbi,
259 struct bio *bio, struct bio_post_read_ctx *ctx)
261 struct bio_iostat_ctx *iostat_ctx;
262 /* Due to the mempool, this never fails. */
263 iostat_ctx = mempool_alloc(bio_iostat_ctx_pool, GFP_NOFS);
264 iostat_ctx->sbi = sbi;
265 iostat_ctx->submit_ts = 0;
266 iostat_ctx->type = 0;
267 iostat_ctx->post_read_ctx = ctx;
268 bio->bi_private = iostat_ctx;
271 int __init f2fs_init_iostat_processing(void)
273 bio_iostat_ctx_cache =
274 kmem_cache_create("f2fs_bio_iostat_ctx",
275 sizeof(struct bio_iostat_ctx), 0, 0, NULL);
276 if (!bio_iostat_ctx_cache)
278 bio_iostat_ctx_pool =
279 mempool_create_slab_pool(NUM_PREALLOC_IOSTAT_CTXS,
280 bio_iostat_ctx_cache);
281 if (!bio_iostat_ctx_pool)
282 goto fail_free_cache;
286 kmem_cache_destroy(bio_iostat_ctx_cache);
291 void f2fs_destroy_iostat_processing(void)
293 mempool_destroy(bio_iostat_ctx_pool);
294 kmem_cache_destroy(bio_iostat_ctx_cache);
297 int f2fs_init_iostat(struct f2fs_sb_info *sbi)
299 /* init iostat info */
300 spin_lock_init(&sbi->iostat_lock);
301 spin_lock_init(&sbi->iostat_lat_lock);
302 sbi->iostat_enable = false;
303 sbi->iostat_period_ms = DEFAULT_IOSTAT_PERIOD_MS;
304 sbi->iostat_io_lat = f2fs_kzalloc(sbi, sizeof(struct iostat_lat_info),
306 if (!sbi->iostat_io_lat)
312 void f2fs_destroy_iostat(struct f2fs_sb_info *sbi)
314 kfree(sbi->iostat_io_lat);