xref: /linux/fs/f2fs/debug.c (revision a2a4ee550704901aa148b57a55cb73cc1e8ae9b1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f2fs debugging statistics
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  * Copyright (c) 2012 Linux Foundation
8  * Copyright (c) 2012 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
9  */
10 
11 #include <linux/fs.h>
12 #include <linux/backing-dev.h>
13 #include <linux/f2fs_fs.h>
14 #include <linux/blkdev.h>
15 #include <linux/debugfs.h>
16 #include <linux/seq_file.h>
17 
18 #include "f2fs.h"
19 #include "node.h"
20 #include "segment.h"
21 #include "gc.h"
22 
23 static LIST_HEAD(f2fs_stat_list);
24 static DEFINE_RAW_SPINLOCK(f2fs_stat_lock);
25 #ifdef CONFIG_DEBUG_FS
26 static struct dentry *f2fs_debugfs_root;
27 #endif
28 
29 /*
30  * This function calculates BDF of every segments
31  */
32 void f2fs_update_sit_info(struct f2fs_sb_info *sbi)
33 {
34 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
35 	unsigned long long blks_per_sec, hblks_per_sec, total_vblocks;
36 	unsigned long long bimodal, dist;
37 	unsigned int segno, vblocks;
38 	int ndirty = 0;
39 
40 	bimodal = 0;
41 	total_vblocks = 0;
42 	blks_per_sec = BLKS_PER_SEC(sbi);
43 	hblks_per_sec = blks_per_sec / 2;
44 	for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
45 		vblocks = get_valid_blocks(sbi, segno, true);
46 		dist = abs(vblocks - hblks_per_sec);
47 		bimodal += dist * dist;
48 
49 		if (vblocks > 0 && vblocks < blks_per_sec) {
50 			total_vblocks += vblocks;
51 			ndirty++;
52 		}
53 	}
54 	dist = div_u64(MAIN_SECS(sbi) * hblks_per_sec * hblks_per_sec, 100);
55 	si->bimodal = div64_u64(bimodal, dist);
56 	if (si->dirty_count)
57 		si->avg_vblocks = div_u64(total_vblocks, ndirty);
58 	else
59 		si->avg_vblocks = 0;
60 }
61 
62 #ifdef CONFIG_DEBUG_FS
63 static void update_general_status(struct f2fs_sb_info *sbi)
64 {
65 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
66 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
67 	int i;
68 
69 	/* these will be changed if online resize is done */
70 	si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
71 	si->main_area_sections = le32_to_cpu(raw_super->section_count);
72 	si->main_area_zones = si->main_area_sections /
73 				le32_to_cpu(raw_super->secs_per_zone);
74 
75 	/* validation check of the segment numbers */
76 	si->hit_largest = atomic64_read(&sbi->read_hit_largest);
77 	si->hit_cached = atomic64_read(&sbi->read_hit_cached);
78 	si->hit_rbtree = atomic64_read(&sbi->read_hit_rbtree);
79 	si->hit_total = si->hit_largest + si->hit_cached + si->hit_rbtree;
80 	si->total_ext = atomic64_read(&sbi->total_hit_ext);
81 	si->ext_tree = atomic_read(&sbi->total_ext_tree);
82 	si->zombie_tree = atomic_read(&sbi->total_zombie_tree);
83 	si->ext_node = atomic_read(&sbi->total_ext_node);
84 	si->ndirty_node = get_pages(sbi, F2FS_DIRTY_NODES);
85 	si->ndirty_dent = get_pages(sbi, F2FS_DIRTY_DENTS);
86 	si->ndirty_meta = get_pages(sbi, F2FS_DIRTY_META);
87 	si->ndirty_data = get_pages(sbi, F2FS_DIRTY_DATA);
88 	si->ndirty_qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
89 	si->ndirty_imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
90 	si->ndirty_dirs = sbi->ndirty_inode[DIR_INODE];
91 	si->ndirty_files = sbi->ndirty_inode[FILE_INODE];
92 	si->nquota_files = sbi->nquota_files;
93 	si->ndirty_all = sbi->ndirty_inode[DIRTY_META];
94 	si->aw_cnt = sbi->atomic_files;
95 	si->max_aw_cnt = atomic_read(&sbi->max_aw_cnt);
96 	si->nr_dio_read = get_pages(sbi, F2FS_DIO_READ);
97 	si->nr_dio_write = get_pages(sbi, F2FS_DIO_WRITE);
98 	si->nr_wb_cp_data = get_pages(sbi, F2FS_WB_CP_DATA);
99 	si->nr_wb_data = get_pages(sbi, F2FS_WB_DATA);
100 	si->nr_rd_data = get_pages(sbi, F2FS_RD_DATA);
101 	si->nr_rd_node = get_pages(sbi, F2FS_RD_NODE);
102 	si->nr_rd_meta = get_pages(sbi, F2FS_RD_META);
103 	if (SM_I(sbi)->fcc_info) {
104 		si->nr_flushed =
105 			atomic_read(&SM_I(sbi)->fcc_info->issued_flush);
106 		si->nr_flushing =
107 			atomic_read(&SM_I(sbi)->fcc_info->queued_flush);
108 		si->flush_list_empty =
109 			llist_empty(&SM_I(sbi)->fcc_info->issue_list);
110 	}
111 	if (SM_I(sbi)->dcc_info) {
112 		si->nr_discarded =
113 			atomic_read(&SM_I(sbi)->dcc_info->issued_discard);
114 		si->nr_discarding =
115 			atomic_read(&SM_I(sbi)->dcc_info->queued_discard);
116 		si->nr_discard_cmd =
117 			atomic_read(&SM_I(sbi)->dcc_info->discard_cmd_cnt);
118 		si->undiscard_blks = SM_I(sbi)->dcc_info->undiscard_blks;
119 	}
120 	si->nr_issued_ckpt = atomic_read(&sbi->cprc_info.issued_ckpt);
121 	si->nr_total_ckpt = atomic_read(&sbi->cprc_info.total_ckpt);
122 	si->nr_queued_ckpt = atomic_read(&sbi->cprc_info.queued_ckpt);
123 	spin_lock(&sbi->cprc_info.stat_lock);
124 	si->cur_ckpt_time = sbi->cprc_info.cur_time;
125 	si->peak_ckpt_time = sbi->cprc_info.peak_time;
126 	spin_unlock(&sbi->cprc_info.stat_lock);
127 	si->total_count = (int)sbi->user_block_count / sbi->blocks_per_seg;
128 	si->rsvd_segs = reserved_segments(sbi);
129 	si->overp_segs = overprovision_segments(sbi);
130 	si->valid_count = valid_user_blocks(sbi);
131 	si->discard_blks = discard_blocks(sbi);
132 	si->valid_node_count = valid_node_count(sbi);
133 	si->valid_inode_count = valid_inode_count(sbi);
134 	si->inline_xattr = atomic_read(&sbi->inline_xattr);
135 	si->inline_inode = atomic_read(&sbi->inline_inode);
136 	si->inline_dir = atomic_read(&sbi->inline_dir);
137 	si->compr_inode = atomic_read(&sbi->compr_inode);
138 	si->compr_blocks = atomic64_read(&sbi->compr_blocks);
139 	si->append = sbi->im[APPEND_INO].ino_num;
140 	si->update = sbi->im[UPDATE_INO].ino_num;
141 	si->orphans = sbi->im[ORPHAN_INO].ino_num;
142 	si->utilization = utilization(sbi);
143 
144 	si->free_segs = free_segments(sbi);
145 	si->free_secs = free_sections(sbi);
146 	si->prefree_count = prefree_segments(sbi);
147 	si->dirty_count = dirty_segments(sbi);
148 	if (sbi->node_inode)
149 		si->node_pages = NODE_MAPPING(sbi)->nrpages;
150 	if (sbi->meta_inode)
151 		si->meta_pages = META_MAPPING(sbi)->nrpages;
152 #ifdef CONFIG_F2FS_FS_COMPRESSION
153 	if (sbi->compress_inode) {
154 		si->compress_pages = COMPRESS_MAPPING(sbi)->nrpages;
155 		si->compress_page_hit = atomic_read(&sbi->compress_page_hit);
156 	}
157 #endif
158 	si->nats = NM_I(sbi)->nat_cnt[TOTAL_NAT];
159 	si->dirty_nats = NM_I(sbi)->nat_cnt[DIRTY_NAT];
160 	si->sits = MAIN_SEGS(sbi);
161 	si->dirty_sits = SIT_I(sbi)->dirty_sentries;
162 	si->free_nids = NM_I(sbi)->nid_cnt[FREE_NID];
163 	si->avail_nids = NM_I(sbi)->available_nids;
164 	si->alloc_nids = NM_I(sbi)->nid_cnt[PREALLOC_NID];
165 	si->io_skip_bggc = sbi->io_skip_bggc;
166 	si->other_skip_bggc = sbi->other_skip_bggc;
167 	si->util_free = (int)(free_user_blocks(sbi) >> sbi->log_blocks_per_seg)
168 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
169 		/ 2;
170 	si->util_valid = (int)(written_block_count(sbi) >>
171 						sbi->log_blocks_per_seg)
172 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
173 		/ 2;
174 	si->util_invalid = 50 - si->util_free - si->util_valid;
175 	for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
176 		struct curseg_info *curseg = CURSEG_I(sbi, i);
177 
178 		si->curseg[i] = curseg->segno;
179 		si->cursec[i] = GET_SEC_FROM_SEG(sbi, curseg->segno);
180 		si->curzone[i] = GET_ZONE_FROM_SEC(sbi, si->cursec[i]);
181 	}
182 
183 	for (i = META_CP; i < META_MAX; i++)
184 		si->meta_count[i] = atomic_read(&sbi->meta_count[i]);
185 
186 	for (i = 0; i < NO_CHECK_TYPE; i++) {
187 		si->dirty_seg[i] = 0;
188 		si->full_seg[i] = 0;
189 		si->valid_blks[i] = 0;
190 	}
191 
192 	for (i = 0; i < MAIN_SEGS(sbi); i++) {
193 		int blks = get_seg_entry(sbi, i)->valid_blocks;
194 		int type = get_seg_entry(sbi, i)->type;
195 
196 		if (!blks)
197 			continue;
198 
199 		if (blks == sbi->blocks_per_seg)
200 			si->full_seg[type]++;
201 		else
202 			si->dirty_seg[type]++;
203 		si->valid_blks[type] += blks;
204 	}
205 
206 	for (i = 0; i < 2; i++) {
207 		si->segment_count[i] = sbi->segment_count[i];
208 		si->block_count[i] = sbi->block_count[i];
209 	}
210 
211 	si->inplace_count = atomic_read(&sbi->inplace_count);
212 }
213 
214 /*
215  * This function calculates memory footprint.
216  */
217 static void update_mem_info(struct f2fs_sb_info *sbi)
218 {
219 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
220 	int i;
221 
222 	if (si->base_mem)
223 		goto get_cache;
224 
225 	/* build stat */
226 	si->base_mem = sizeof(struct f2fs_stat_info);
227 
228 	/* build superblock */
229 	si->base_mem += sizeof(struct f2fs_sb_info) + sbi->sb->s_blocksize;
230 	si->base_mem += 2 * sizeof(struct f2fs_inode_info);
231 	si->base_mem += sizeof(*sbi->ckpt);
232 
233 	/* build sm */
234 	si->base_mem += sizeof(struct f2fs_sm_info);
235 
236 	/* build sit */
237 	si->base_mem += sizeof(struct sit_info);
238 	si->base_mem += MAIN_SEGS(sbi) * sizeof(struct seg_entry);
239 	si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
240 	si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * MAIN_SEGS(sbi);
241 	si->base_mem += SIT_VBLOCK_MAP_SIZE * MAIN_SEGS(sbi);
242 	si->base_mem += SIT_VBLOCK_MAP_SIZE;
243 	if (__is_large_section(sbi))
244 		si->base_mem += MAIN_SECS(sbi) * sizeof(struct sec_entry);
245 	si->base_mem += __bitmap_size(sbi, SIT_BITMAP);
246 
247 	/* build free segmap */
248 	si->base_mem += sizeof(struct free_segmap_info);
249 	si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
250 	si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
251 
252 	/* build curseg */
253 	si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE;
254 	si->base_mem += PAGE_SIZE * NR_CURSEG_TYPE;
255 
256 	/* build dirty segmap */
257 	si->base_mem += sizeof(struct dirty_seglist_info);
258 	si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(MAIN_SEGS(sbi));
259 	si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
260 
261 	/* build nm */
262 	si->base_mem += sizeof(struct f2fs_nm_info);
263 	si->base_mem += __bitmap_size(sbi, NAT_BITMAP);
264 	si->base_mem += (NM_I(sbi)->nat_bits_blocks << F2FS_BLKSIZE_BITS);
265 	si->base_mem += NM_I(sbi)->nat_blocks *
266 				f2fs_bitmap_size(NAT_ENTRY_PER_BLOCK);
267 	si->base_mem += NM_I(sbi)->nat_blocks / 8;
268 	si->base_mem += NM_I(sbi)->nat_blocks * sizeof(unsigned short);
269 
270 get_cache:
271 	si->cache_mem = 0;
272 
273 	/* build gc */
274 	if (sbi->gc_thread)
275 		si->cache_mem += sizeof(struct f2fs_gc_kthread);
276 
277 	/* build merge flush thread */
278 	if (SM_I(sbi)->fcc_info)
279 		si->cache_mem += sizeof(struct flush_cmd_control);
280 	if (SM_I(sbi)->dcc_info) {
281 		si->cache_mem += sizeof(struct discard_cmd_control);
282 		si->cache_mem += sizeof(struct discard_cmd) *
283 			atomic_read(&SM_I(sbi)->dcc_info->discard_cmd_cnt);
284 	}
285 
286 	/* free nids */
287 	si->cache_mem += (NM_I(sbi)->nid_cnt[FREE_NID] +
288 				NM_I(sbi)->nid_cnt[PREALLOC_NID]) *
289 				sizeof(struct free_nid);
290 	si->cache_mem += NM_I(sbi)->nat_cnt[TOTAL_NAT] *
291 				sizeof(struct nat_entry);
292 	si->cache_mem += NM_I(sbi)->nat_cnt[DIRTY_NAT] *
293 				sizeof(struct nat_entry_set);
294 	for (i = 0; i < MAX_INO_ENTRY; i++)
295 		si->cache_mem += sbi->im[i].ino_num * sizeof(struct ino_entry);
296 	si->cache_mem += atomic_read(&sbi->total_ext_tree) *
297 						sizeof(struct extent_tree);
298 	si->cache_mem += atomic_read(&sbi->total_ext_node) *
299 						sizeof(struct extent_node);
300 
301 	si->page_mem = 0;
302 	if (sbi->node_inode) {
303 		unsigned npages = NODE_MAPPING(sbi)->nrpages;
304 
305 		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
306 	}
307 	if (sbi->meta_inode) {
308 		unsigned npages = META_MAPPING(sbi)->nrpages;
309 
310 		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
311 	}
312 #ifdef CONFIG_F2FS_FS_COMPRESSION
313 	if (sbi->compress_inode) {
314 		unsigned npages = COMPRESS_MAPPING(sbi)->nrpages;
315 		si->page_mem += (unsigned long long)npages << PAGE_SHIFT;
316 	}
317 #endif
318 }
319 
320 static char *s_flag[] = {
321 	[SBI_IS_DIRTY]		= " fs_dirty",
322 	[SBI_IS_CLOSE]		= " closing",
323 	[SBI_NEED_FSCK]		= " need_fsck",
324 	[SBI_POR_DOING]		= " recovering",
325 	[SBI_NEED_SB_WRITE]	= " sb_dirty",
326 	[SBI_NEED_CP]		= " need_cp",
327 	[SBI_IS_SHUTDOWN]	= " shutdown",
328 	[SBI_IS_RECOVERED]	= " recovered",
329 	[SBI_CP_DISABLED]	= " cp_disabled",
330 	[SBI_CP_DISABLED_QUICK]	= " cp_disabled_quick",
331 	[SBI_QUOTA_NEED_FLUSH]	= " quota_need_flush",
332 	[SBI_QUOTA_SKIP_FLUSH]	= " quota_skip_flush",
333 	[SBI_QUOTA_NEED_REPAIR]	= " quota_need_repair",
334 	[SBI_IS_RESIZEFS]	= " resizefs",
335 	[SBI_IS_FREEZING]	= " freezefs",
336 };
337 
338 static int stat_show(struct seq_file *s, void *v)
339 {
340 	struct f2fs_stat_info *si;
341 	int i = 0, j = 0;
342 	unsigned long flags;
343 
344 	raw_spin_lock_irqsave(&f2fs_stat_lock, flags);
345 	list_for_each_entry(si, &f2fs_stat_list, stat_list) {
346 		update_general_status(si->sbi);
347 
348 		seq_printf(s, "\n=====[ partition info(%pg). #%d, %s, CP: %s]=====\n",
349 			si->sbi->sb->s_bdev, i++,
350 			f2fs_readonly(si->sbi->sb) ? "RO": "RW",
351 			is_set_ckpt_flags(si->sbi, CP_DISABLED_FLAG) ?
352 			"Disabled" : (f2fs_cp_error(si->sbi) ? "Error" : "Good"));
353 		if (si->sbi->s_flag) {
354 			seq_puts(s, "[SBI:");
355 			for_each_set_bit(j, &si->sbi->s_flag, 32)
356 				seq_puts(s, s_flag[j]);
357 			seq_puts(s, "]\n");
358 		}
359 		seq_printf(s, "[SB: 1] [CP: 2] [SIT: %d] [NAT: %d] ",
360 			   si->sit_area_segs, si->nat_area_segs);
361 		seq_printf(s, "[SSA: %d] [MAIN: %d",
362 			   si->ssa_area_segs, si->main_area_segs);
363 		seq_printf(s, "(OverProv:%d Resv:%d)]\n\n",
364 			   si->overp_segs, si->rsvd_segs);
365 		seq_printf(s, "Current Time Sec: %llu / Mounted Time Sec: %llu\n\n",
366 					ktime_get_boottime_seconds(),
367 					SIT_I(si->sbi)->mounted_time);
368 		if (test_opt(si->sbi, DISCARD))
369 			seq_printf(s, "Utilization: %u%% (%u valid blocks, %u discard blocks)\n",
370 				si->utilization, si->valid_count, si->discard_blks);
371 		else
372 			seq_printf(s, "Utilization: %u%% (%u valid blocks)\n",
373 				si->utilization, si->valid_count);
374 
375 		seq_printf(s, "  - Node: %u (Inode: %u, ",
376 			   si->valid_node_count, si->valid_inode_count);
377 		seq_printf(s, "Other: %u)\n  - Data: %u\n",
378 			   si->valid_node_count - si->valid_inode_count,
379 			   si->valid_count - si->valid_node_count);
380 		seq_printf(s, "  - Inline_xattr Inode: %u\n",
381 			   si->inline_xattr);
382 		seq_printf(s, "  - Inline_data Inode: %u\n",
383 			   si->inline_inode);
384 		seq_printf(s, "  - Inline_dentry Inode: %u\n",
385 			   si->inline_dir);
386 		seq_printf(s, "  - Compressed Inode: %u, Blocks: %llu\n",
387 			   si->compr_inode, si->compr_blocks);
388 		seq_printf(s, "  - Orphan/Append/Update Inode: %u, %u, %u\n",
389 			   si->orphans, si->append, si->update);
390 		seq_printf(s, "\nMain area: %d segs, %d secs %d zones\n",
391 			   si->main_area_segs, si->main_area_sections,
392 			   si->main_area_zones);
393 		seq_printf(s, "    TYPE         %8s %8s %8s %10s %10s %10s\n",
394 			   "segno", "secno", "zoneno", "dirty_seg", "full_seg", "valid_blk");
395 		seq_printf(s, "  - COLD   data: %8d %8d %8d %10u %10u %10u\n",
396 			   si->curseg[CURSEG_COLD_DATA],
397 			   si->cursec[CURSEG_COLD_DATA],
398 			   si->curzone[CURSEG_COLD_DATA],
399 			   si->dirty_seg[CURSEG_COLD_DATA],
400 			   si->full_seg[CURSEG_COLD_DATA],
401 			   si->valid_blks[CURSEG_COLD_DATA]);
402 		seq_printf(s, "  - WARM   data: %8d %8d %8d %10u %10u %10u\n",
403 			   si->curseg[CURSEG_WARM_DATA],
404 			   si->cursec[CURSEG_WARM_DATA],
405 			   si->curzone[CURSEG_WARM_DATA],
406 			   si->dirty_seg[CURSEG_WARM_DATA],
407 			   si->full_seg[CURSEG_WARM_DATA],
408 			   si->valid_blks[CURSEG_WARM_DATA]);
409 		seq_printf(s, "  - HOT    data: %8d %8d %8d %10u %10u %10u\n",
410 			   si->curseg[CURSEG_HOT_DATA],
411 			   si->cursec[CURSEG_HOT_DATA],
412 			   si->curzone[CURSEG_HOT_DATA],
413 			   si->dirty_seg[CURSEG_HOT_DATA],
414 			   si->full_seg[CURSEG_HOT_DATA],
415 			   si->valid_blks[CURSEG_HOT_DATA]);
416 		seq_printf(s, "  - Dir   dnode: %8d %8d %8d %10u %10u %10u\n",
417 			   si->curseg[CURSEG_HOT_NODE],
418 			   si->cursec[CURSEG_HOT_NODE],
419 			   si->curzone[CURSEG_HOT_NODE],
420 			   si->dirty_seg[CURSEG_HOT_NODE],
421 			   si->full_seg[CURSEG_HOT_NODE],
422 			   si->valid_blks[CURSEG_HOT_NODE]);
423 		seq_printf(s, "  - File  dnode: %8d %8d %8d %10u %10u %10u\n",
424 			   si->curseg[CURSEG_WARM_NODE],
425 			   si->cursec[CURSEG_WARM_NODE],
426 			   si->curzone[CURSEG_WARM_NODE],
427 			   si->dirty_seg[CURSEG_WARM_NODE],
428 			   si->full_seg[CURSEG_WARM_NODE],
429 			   si->valid_blks[CURSEG_WARM_NODE]);
430 		seq_printf(s, "  - Indir nodes: %8d %8d %8d %10u %10u %10u\n",
431 			   si->curseg[CURSEG_COLD_NODE],
432 			   si->cursec[CURSEG_COLD_NODE],
433 			   si->curzone[CURSEG_COLD_NODE],
434 			   si->dirty_seg[CURSEG_COLD_NODE],
435 			   si->full_seg[CURSEG_COLD_NODE],
436 			   si->valid_blks[CURSEG_COLD_NODE]);
437 		seq_printf(s, "  - Pinned file: %8d %8d %8d\n",
438 			   si->curseg[CURSEG_COLD_DATA_PINNED],
439 			   si->cursec[CURSEG_COLD_DATA_PINNED],
440 			   si->curzone[CURSEG_COLD_DATA_PINNED]);
441 		seq_printf(s, "  - ATGC   data: %8d %8d %8d\n",
442 			   si->curseg[CURSEG_ALL_DATA_ATGC],
443 			   si->cursec[CURSEG_ALL_DATA_ATGC],
444 			   si->curzone[CURSEG_ALL_DATA_ATGC]);
445 		seq_printf(s, "\n  - Valid: %d\n  - Dirty: %d\n",
446 			   si->main_area_segs - si->dirty_count -
447 			   si->prefree_count - si->free_segs,
448 			   si->dirty_count);
449 		seq_printf(s, "  - Prefree: %d\n  - Free: %d (%d)\n\n",
450 			   si->prefree_count, si->free_segs, si->free_secs);
451 		seq_printf(s, "CP calls: %d (BG: %d)\n",
452 				si->cp_count, si->bg_cp_count);
453 		seq_printf(s, "  - cp blocks : %u\n", si->meta_count[META_CP]);
454 		seq_printf(s, "  - sit blocks : %u\n",
455 				si->meta_count[META_SIT]);
456 		seq_printf(s, "  - nat blocks : %u\n",
457 				si->meta_count[META_NAT]);
458 		seq_printf(s, "  - ssa blocks : %u\n",
459 				si->meta_count[META_SSA]);
460 		seq_printf(s, "CP merge (Queued: %4d, Issued: %4d, Total: %4d, "
461 				"Cur time: %4d(ms), Peak time: %4d(ms))\n",
462 				si->nr_queued_ckpt, si->nr_issued_ckpt,
463 				si->nr_total_ckpt, si->cur_ckpt_time,
464 				si->peak_ckpt_time);
465 		seq_printf(s, "GC calls: %d (BG: %d)\n",
466 			   si->call_count, si->bg_gc);
467 		seq_printf(s, "  - data segments : %d (%d)\n",
468 				si->data_segs, si->bg_data_segs);
469 		seq_printf(s, "  - node segments : %d (%d)\n",
470 				si->node_segs, si->bg_node_segs);
471 		seq_printf(s, "  - Reclaimed segs : Normal (%d), Idle CB (%d), "
472 				"Idle Greedy (%d), Idle AT (%d), "
473 				"Urgent High (%d), Urgent Mid (%d), "
474 				"Urgent Low (%d)\n",
475 				si->sbi->gc_reclaimed_segs[GC_NORMAL],
476 				si->sbi->gc_reclaimed_segs[GC_IDLE_CB],
477 				si->sbi->gc_reclaimed_segs[GC_IDLE_GREEDY],
478 				si->sbi->gc_reclaimed_segs[GC_IDLE_AT],
479 				si->sbi->gc_reclaimed_segs[GC_URGENT_HIGH],
480 				si->sbi->gc_reclaimed_segs[GC_URGENT_MID],
481 				si->sbi->gc_reclaimed_segs[GC_URGENT_LOW]);
482 		seq_printf(s, "Try to move %d blocks (BG: %d)\n", si->tot_blks,
483 				si->bg_data_blks + si->bg_node_blks);
484 		seq_printf(s, "  - data blocks : %d (%d)\n", si->data_blks,
485 				si->bg_data_blks);
486 		seq_printf(s, "  - node blocks : %d (%d)\n", si->node_blks,
487 				si->bg_node_blks);
488 		seq_printf(s, "BG skip : IO: %u, Other: %u\n",
489 				si->io_skip_bggc, si->other_skip_bggc);
490 		seq_puts(s, "\nExtent Cache:\n");
491 		seq_printf(s, "  - Hit Count: L1-1:%llu L1-2:%llu L2:%llu\n",
492 				si->hit_largest, si->hit_cached,
493 				si->hit_rbtree);
494 		seq_printf(s, "  - Hit Ratio: %llu%% (%llu / %llu)\n",
495 				!si->total_ext ? 0 :
496 				div64_u64(si->hit_total * 100, si->total_ext),
497 				si->hit_total, si->total_ext);
498 		seq_printf(s, "  - Inner Struct Count: tree: %d(%d), node: %d\n",
499 				si->ext_tree, si->zombie_tree, si->ext_node);
500 		seq_puts(s, "\nBalancing F2FS Async:\n");
501 		seq_printf(s, "  - DIO (R: %4d, W: %4d)\n",
502 			   si->nr_dio_read, si->nr_dio_write);
503 		seq_printf(s, "  - IO_R (Data: %4d, Node: %4d, Meta: %4d\n",
504 			   si->nr_rd_data, si->nr_rd_node, si->nr_rd_meta);
505 		seq_printf(s, "  - IO_W (CP: %4d, Data: %4d, Flush: (%4d %4d %4d), "
506 			"Discard: (%4d %4d)) cmd: %4d undiscard:%4u\n",
507 			   si->nr_wb_cp_data, si->nr_wb_data,
508 			   si->nr_flushing, si->nr_flushed,
509 			   si->flush_list_empty,
510 			   si->nr_discarding, si->nr_discarded,
511 			   si->nr_discard_cmd, si->undiscard_blks);
512 		seq_printf(s, "  - atomic IO: %4d (Max. %4d)\n",
513 			   si->aw_cnt, si->max_aw_cnt);
514 		seq_printf(s, "  - compress: %4d, hit:%8d\n", si->compress_pages, si->compress_page_hit);
515 		seq_printf(s, "  - nodes: %4d in %4d\n",
516 			   si->ndirty_node, si->node_pages);
517 		seq_printf(s, "  - dents: %4d in dirs:%4d (%4d)\n",
518 			   si->ndirty_dent, si->ndirty_dirs, si->ndirty_all);
519 		seq_printf(s, "  - datas: %4d in files:%4d\n",
520 			   si->ndirty_data, si->ndirty_files);
521 		seq_printf(s, "  - quota datas: %4d in quota files:%4d\n",
522 			   si->ndirty_qdata, si->nquota_files);
523 		seq_printf(s, "  - meta: %4d in %4d\n",
524 			   si->ndirty_meta, si->meta_pages);
525 		seq_printf(s, "  - imeta: %4d\n",
526 			   si->ndirty_imeta);
527 		seq_printf(s, "  - fsync mark: %4lld\n",
528 			   percpu_counter_sum_positive(
529 					&si->sbi->rf_node_block_count));
530 		seq_printf(s, "  - NATs: %9d/%9d\n  - SITs: %9d/%9d\n",
531 			   si->dirty_nats, si->nats, si->dirty_sits, si->sits);
532 		seq_printf(s, "  - free_nids: %9d/%9d\n  - alloc_nids: %9d\n",
533 			   si->free_nids, si->avail_nids, si->alloc_nids);
534 		seq_puts(s, "\nDistribution of User Blocks:");
535 		seq_puts(s, " [ valid | invalid | free ]\n");
536 		seq_puts(s, "  [");
537 
538 		for (j = 0; j < si->util_valid; j++)
539 			seq_putc(s, '-');
540 		seq_putc(s, '|');
541 
542 		for (j = 0; j < si->util_invalid; j++)
543 			seq_putc(s, '-');
544 		seq_putc(s, '|');
545 
546 		for (j = 0; j < si->util_free; j++)
547 			seq_putc(s, '-');
548 		seq_puts(s, "]\n\n");
549 		seq_printf(s, "IPU: %u blocks\n", si->inplace_count);
550 		seq_printf(s, "SSR: %u blocks in %u segments\n",
551 			   si->block_count[SSR], si->segment_count[SSR]);
552 		seq_printf(s, "LFS: %u blocks in %u segments\n",
553 			   si->block_count[LFS], si->segment_count[LFS]);
554 
555 		/* segment usage info */
556 		f2fs_update_sit_info(si->sbi);
557 		seq_printf(s, "\nBDF: %u, avg. vblocks: %u\n",
558 			   si->bimodal, si->avg_vblocks);
559 
560 		/* memory footprint */
561 		update_mem_info(si->sbi);
562 		seq_printf(s, "\nMemory: %llu KB\n",
563 			(si->base_mem + si->cache_mem + si->page_mem) >> 10);
564 		seq_printf(s, "  - static: %llu KB\n",
565 				si->base_mem >> 10);
566 		seq_printf(s, "  - cached: %llu KB\n",
567 				si->cache_mem >> 10);
568 		seq_printf(s, "  - paged : %llu KB\n",
569 				si->page_mem >> 10);
570 	}
571 	raw_spin_unlock_irqrestore(&f2fs_stat_lock, flags);
572 	return 0;
573 }
574 
575 DEFINE_SHOW_ATTRIBUTE(stat);
576 #endif
577 
578 int f2fs_build_stats(struct f2fs_sb_info *sbi)
579 {
580 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
581 	struct f2fs_stat_info *si;
582 	unsigned long flags;
583 	int i;
584 
585 	si = f2fs_kzalloc(sbi, sizeof(struct f2fs_stat_info), GFP_KERNEL);
586 	if (!si)
587 		return -ENOMEM;
588 
589 	si->all_area_segs = le32_to_cpu(raw_super->segment_count);
590 	si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
591 	si->nat_area_segs = le32_to_cpu(raw_super->segment_count_nat);
592 	si->ssa_area_segs = le32_to_cpu(raw_super->segment_count_ssa);
593 	si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
594 	si->main_area_sections = le32_to_cpu(raw_super->section_count);
595 	si->main_area_zones = si->main_area_sections /
596 				le32_to_cpu(raw_super->secs_per_zone);
597 	si->sbi = sbi;
598 	sbi->stat_info = si;
599 
600 	atomic64_set(&sbi->total_hit_ext, 0);
601 	atomic64_set(&sbi->read_hit_rbtree, 0);
602 	atomic64_set(&sbi->read_hit_largest, 0);
603 	atomic64_set(&sbi->read_hit_cached, 0);
604 
605 	atomic_set(&sbi->inline_xattr, 0);
606 	atomic_set(&sbi->inline_inode, 0);
607 	atomic_set(&sbi->inline_dir, 0);
608 	atomic_set(&sbi->compr_inode, 0);
609 	atomic64_set(&sbi->compr_blocks, 0);
610 	atomic_set(&sbi->inplace_count, 0);
611 	for (i = META_CP; i < META_MAX; i++)
612 		atomic_set(&sbi->meta_count[i], 0);
613 
614 	atomic_set(&sbi->max_aw_cnt, 0);
615 
616 	raw_spin_lock_irqsave(&f2fs_stat_lock, flags);
617 	list_add_tail(&si->stat_list, &f2fs_stat_list);
618 	raw_spin_unlock_irqrestore(&f2fs_stat_lock, flags);
619 
620 	return 0;
621 }
622 
623 void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
624 {
625 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
626 	unsigned long flags;
627 
628 	raw_spin_lock_irqsave(&f2fs_stat_lock, flags);
629 	list_del(&si->stat_list);
630 	raw_spin_unlock_irqrestore(&f2fs_stat_lock, flags);
631 
632 	kfree(si);
633 }
634 
635 void __init f2fs_create_root_stats(void)
636 {
637 #ifdef CONFIG_DEBUG_FS
638 	f2fs_debugfs_root = debugfs_create_dir("f2fs", NULL);
639 
640 	debugfs_create_file("status", 0444, f2fs_debugfs_root, NULL,
641 			    &stat_fops);
642 #endif
643 }
644 
645 void f2fs_destroy_root_stats(void)
646 {
647 #ifdef CONFIG_DEBUG_FS
648 	debugfs_remove_recursive(f2fs_debugfs_root);
649 	f2fs_debugfs_root = NULL;
650 #endif
651 }
652