xref: /linux/fs/f2fs/debug.c (revision e0bf6c5ca2d3281f231c5f0c9bf145e9513644de)
1 /*
2  * f2fs debugging statistics
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  * Copyright (c) 2012 Linux Foundation
7  * Copyright (c) 2012 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/fs.h>
15 #include <linux/backing-dev.h>
16 #include <linux/f2fs_fs.h>
17 #include <linux/blkdev.h>
18 #include <linux/debugfs.h>
19 #include <linux/seq_file.h>
20 
21 #include "f2fs.h"
22 #include "node.h"
23 #include "segment.h"
24 #include "gc.h"
25 
26 static LIST_HEAD(f2fs_stat_list);
27 static struct dentry *f2fs_debugfs_root;
28 static DEFINE_MUTEX(f2fs_stat_mutex);
29 
30 static void update_general_status(struct f2fs_sb_info *sbi)
31 {
32 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
33 	int i;
34 
35 	/* validation check of the segment numbers */
36 	si->hit_ext = sbi->read_hit_ext;
37 	si->total_ext = sbi->total_hit_ext;
38 	si->ndirty_node = get_pages(sbi, F2FS_DIRTY_NODES);
39 	si->ndirty_dent = get_pages(sbi, F2FS_DIRTY_DENTS);
40 	si->ndirty_dirs = sbi->n_dirty_dirs;
41 	si->ndirty_meta = get_pages(sbi, F2FS_DIRTY_META);
42 	si->inmem_pages = get_pages(sbi, F2FS_INMEM_PAGES);
43 	si->wb_pages = get_pages(sbi, F2FS_WRITEBACK);
44 	si->total_count = (int)sbi->user_block_count / sbi->blocks_per_seg;
45 	si->rsvd_segs = reserved_segments(sbi);
46 	si->overp_segs = overprovision_segments(sbi);
47 	si->valid_count = valid_user_blocks(sbi);
48 	si->valid_node_count = valid_node_count(sbi);
49 	si->valid_inode_count = valid_inode_count(sbi);
50 	si->inline_inode = atomic_read(&sbi->inline_inode);
51 	si->inline_dir = atomic_read(&sbi->inline_dir);
52 	si->utilization = utilization(sbi);
53 
54 	si->free_segs = free_segments(sbi);
55 	si->free_secs = free_sections(sbi);
56 	si->prefree_count = prefree_segments(sbi);
57 	si->dirty_count = dirty_segments(sbi);
58 	si->node_pages = NODE_MAPPING(sbi)->nrpages;
59 	si->meta_pages = META_MAPPING(sbi)->nrpages;
60 	si->nats = NM_I(sbi)->nat_cnt;
61 	si->dirty_nats = NM_I(sbi)->dirty_nat_cnt;
62 	si->sits = MAIN_SEGS(sbi);
63 	si->dirty_sits = SIT_I(sbi)->dirty_sentries;
64 	si->fnids = NM_I(sbi)->fcnt;
65 	si->bg_gc = sbi->bg_gc;
66 	si->util_free = (int)(free_user_blocks(sbi) >> sbi->log_blocks_per_seg)
67 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
68 		/ 2;
69 	si->util_valid = (int)(written_block_count(sbi) >>
70 						sbi->log_blocks_per_seg)
71 		* 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
72 		/ 2;
73 	si->util_invalid = 50 - si->util_free - si->util_valid;
74 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_NODE; i++) {
75 		struct curseg_info *curseg = CURSEG_I(sbi, i);
76 		si->curseg[i] = curseg->segno;
77 		si->cursec[i] = curseg->segno / sbi->segs_per_sec;
78 		si->curzone[i] = si->cursec[i] / sbi->secs_per_zone;
79 	}
80 
81 	for (i = 0; i < 2; i++) {
82 		si->segment_count[i] = sbi->segment_count[i];
83 		si->block_count[i] = sbi->block_count[i];
84 	}
85 
86 	si->inplace_count = atomic_read(&sbi->inplace_count);
87 }
88 
89 /*
90  * This function calculates BDF of every segments
91  */
92 static void update_sit_info(struct f2fs_sb_info *sbi)
93 {
94 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
95 	unsigned int blks_per_sec, hblks_per_sec, total_vblocks, bimodal, dist;
96 	unsigned int segno, vblocks;
97 	int ndirty = 0;
98 
99 	bimodal = 0;
100 	total_vblocks = 0;
101 	blks_per_sec = sbi->segs_per_sec * (1 << sbi->log_blocks_per_seg);
102 	hblks_per_sec = blks_per_sec / 2;
103 	for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
104 		vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
105 		dist = abs(vblocks - hblks_per_sec);
106 		bimodal += dist * dist;
107 
108 		if (vblocks > 0 && vblocks < blks_per_sec) {
109 			total_vblocks += vblocks;
110 			ndirty++;
111 		}
112 	}
113 	dist = MAIN_SECS(sbi) * hblks_per_sec * hblks_per_sec / 100;
114 	si->bimodal = bimodal / dist;
115 	if (si->dirty_count)
116 		si->avg_vblocks = total_vblocks / ndirty;
117 	else
118 		si->avg_vblocks = 0;
119 }
120 
121 /*
122  * This function calculates memory footprint.
123  */
124 static void update_mem_info(struct f2fs_sb_info *sbi)
125 {
126 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
127 	unsigned npages;
128 	int i;
129 
130 	if (si->base_mem)
131 		goto get_cache;
132 
133 	si->base_mem = sizeof(struct f2fs_sb_info) + sbi->sb->s_blocksize;
134 	si->base_mem += 2 * sizeof(struct f2fs_inode_info);
135 	si->base_mem += sizeof(*sbi->ckpt);
136 
137 	/* build sm */
138 	si->base_mem += sizeof(struct f2fs_sm_info);
139 
140 	/* build sit */
141 	si->base_mem += sizeof(struct sit_info);
142 	si->base_mem += MAIN_SEGS(sbi) * sizeof(struct seg_entry);
143 	si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
144 	si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * MAIN_SEGS(sbi);
145 	si->base_mem += SIT_VBLOCK_MAP_SIZE;
146 	if (sbi->segs_per_sec > 1)
147 		si->base_mem += MAIN_SECS(sbi) * sizeof(struct sec_entry);
148 	si->base_mem += __bitmap_size(sbi, SIT_BITMAP);
149 
150 	/* build free segmap */
151 	si->base_mem += sizeof(struct free_segmap_info);
152 	si->base_mem += f2fs_bitmap_size(MAIN_SEGS(sbi));
153 	si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
154 
155 	/* build curseg */
156 	si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE;
157 	si->base_mem += PAGE_CACHE_SIZE * NR_CURSEG_TYPE;
158 
159 	/* build dirty segmap */
160 	si->base_mem += sizeof(struct dirty_seglist_info);
161 	si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(MAIN_SEGS(sbi));
162 	si->base_mem += f2fs_bitmap_size(MAIN_SECS(sbi));
163 
164 	/* build nm */
165 	si->base_mem += sizeof(struct f2fs_nm_info);
166 	si->base_mem += __bitmap_size(sbi, NAT_BITMAP);
167 
168 get_cache:
169 	si->cache_mem = 0;
170 
171 	/* build gc */
172 	if (sbi->gc_thread)
173 		si->cache_mem += sizeof(struct f2fs_gc_kthread);
174 
175 	/* build merge flush thread */
176 	if (SM_I(sbi)->cmd_control_info)
177 		si->cache_mem += sizeof(struct flush_cmd_control);
178 
179 	/* free nids */
180 	si->cache_mem += NM_I(sbi)->fcnt * sizeof(struct free_nid);
181 	si->cache_mem += NM_I(sbi)->nat_cnt * sizeof(struct nat_entry);
182 	si->cache_mem += NM_I(sbi)->dirty_nat_cnt *
183 					sizeof(struct nat_entry_set);
184 	si->cache_mem += si->inmem_pages * sizeof(struct inmem_pages);
185 	si->cache_mem += sbi->n_dirty_dirs * sizeof(struct inode_entry);
186 	for (i = 0; i <= UPDATE_INO; i++)
187 		si->cache_mem += sbi->im[i].ino_num * sizeof(struct ino_entry);
188 
189 	si->page_mem = 0;
190 	npages = NODE_MAPPING(sbi)->nrpages;
191 	si->page_mem += npages << PAGE_CACHE_SHIFT;
192 	npages = META_MAPPING(sbi)->nrpages;
193 	si->page_mem += npages << PAGE_CACHE_SHIFT;
194 }
195 
196 static int stat_show(struct seq_file *s, void *v)
197 {
198 	struct f2fs_stat_info *si;
199 	int i = 0;
200 	int j;
201 
202 	mutex_lock(&f2fs_stat_mutex);
203 	list_for_each_entry(si, &f2fs_stat_list, stat_list) {
204 		char devname[BDEVNAME_SIZE];
205 
206 		update_general_status(si->sbi);
207 
208 		seq_printf(s, "\n=====[ partition info(%s). #%d ]=====\n",
209 			bdevname(si->sbi->sb->s_bdev, devname), i++);
210 		seq_printf(s, "[SB: 1] [CP: 2] [SIT: %d] [NAT: %d] ",
211 			   si->sit_area_segs, si->nat_area_segs);
212 		seq_printf(s, "[SSA: %d] [MAIN: %d",
213 			   si->ssa_area_segs, si->main_area_segs);
214 		seq_printf(s, "(OverProv:%d Resv:%d)]\n\n",
215 			   si->overp_segs, si->rsvd_segs);
216 		seq_printf(s, "Utilization: %d%% (%d valid blocks)\n",
217 			   si->utilization, si->valid_count);
218 		seq_printf(s, "  - Node: %u (Inode: %u, ",
219 			   si->valid_node_count, si->valid_inode_count);
220 		seq_printf(s, "Other: %u)\n  - Data: %u\n",
221 			   si->valid_node_count - si->valid_inode_count,
222 			   si->valid_count - si->valid_node_count);
223 		seq_printf(s, "  - Inline_data Inode: %u\n",
224 			   si->inline_inode);
225 		seq_printf(s, "  - Inline_dentry Inode: %u\n",
226 			   si->inline_dir);
227 		seq_printf(s, "\nMain area: %d segs, %d secs %d zones\n",
228 			   si->main_area_segs, si->main_area_sections,
229 			   si->main_area_zones);
230 		seq_printf(s, "  - COLD  data: %d, %d, %d\n",
231 			   si->curseg[CURSEG_COLD_DATA],
232 			   si->cursec[CURSEG_COLD_DATA],
233 			   si->curzone[CURSEG_COLD_DATA]);
234 		seq_printf(s, "  - WARM  data: %d, %d, %d\n",
235 			   si->curseg[CURSEG_WARM_DATA],
236 			   si->cursec[CURSEG_WARM_DATA],
237 			   si->curzone[CURSEG_WARM_DATA]);
238 		seq_printf(s, "  - HOT   data: %d, %d, %d\n",
239 			   si->curseg[CURSEG_HOT_DATA],
240 			   si->cursec[CURSEG_HOT_DATA],
241 			   si->curzone[CURSEG_HOT_DATA]);
242 		seq_printf(s, "  - Dir   dnode: %d, %d, %d\n",
243 			   si->curseg[CURSEG_HOT_NODE],
244 			   si->cursec[CURSEG_HOT_NODE],
245 			   si->curzone[CURSEG_HOT_NODE]);
246 		seq_printf(s, "  - File   dnode: %d, %d, %d\n",
247 			   si->curseg[CURSEG_WARM_NODE],
248 			   si->cursec[CURSEG_WARM_NODE],
249 			   si->curzone[CURSEG_WARM_NODE]);
250 		seq_printf(s, "  - Indir nodes: %d, %d, %d\n",
251 			   si->curseg[CURSEG_COLD_NODE],
252 			   si->cursec[CURSEG_COLD_NODE],
253 			   si->curzone[CURSEG_COLD_NODE]);
254 		seq_printf(s, "\n  - Valid: %d\n  - Dirty: %d\n",
255 			   si->main_area_segs - si->dirty_count -
256 			   si->prefree_count - si->free_segs,
257 			   si->dirty_count);
258 		seq_printf(s, "  - Prefree: %d\n  - Free: %d (%d)\n\n",
259 			   si->prefree_count, si->free_segs, si->free_secs);
260 		seq_printf(s, "CP calls: %d\n", si->cp_count);
261 		seq_printf(s, "GC calls: %d (BG: %d)\n",
262 			   si->call_count, si->bg_gc);
263 		seq_printf(s, "  - data segments : %d\n", si->data_segs);
264 		seq_printf(s, "  - node segments : %d\n", si->node_segs);
265 		seq_printf(s, "Try to move %d blocks\n", si->tot_blks);
266 		seq_printf(s, "  - data blocks : %d\n", si->data_blks);
267 		seq_printf(s, "  - node blocks : %d\n", si->node_blks);
268 		seq_printf(s, "\nExtent Hit Ratio: %d / %d\n",
269 			   si->hit_ext, si->total_ext);
270 		seq_puts(s, "\nBalancing F2FS Async:\n");
271 		seq_printf(s, "  - inmem: %4d, wb: %4d\n",
272 			   si->inmem_pages, si->wb_pages);
273 		seq_printf(s, "  - nodes: %4d in %4d\n",
274 			   si->ndirty_node, si->node_pages);
275 		seq_printf(s, "  - dents: %4d in dirs:%4d\n",
276 			   si->ndirty_dent, si->ndirty_dirs);
277 		seq_printf(s, "  - meta: %4d in %4d\n",
278 			   si->ndirty_meta, si->meta_pages);
279 		seq_printf(s, "  - NATs: %9d/%9d\n  - SITs: %9d/%9d\n",
280 			   si->dirty_nats, si->nats, si->dirty_sits, si->sits);
281 		seq_printf(s, "  - free_nids: %9d\n",
282 			   si->fnids);
283 		seq_puts(s, "\nDistribution of User Blocks:");
284 		seq_puts(s, " [ valid | invalid | free ]\n");
285 		seq_puts(s, "  [");
286 
287 		for (j = 0; j < si->util_valid; j++)
288 			seq_putc(s, '-');
289 		seq_putc(s, '|');
290 
291 		for (j = 0; j < si->util_invalid; j++)
292 			seq_putc(s, '-');
293 		seq_putc(s, '|');
294 
295 		for (j = 0; j < si->util_free; j++)
296 			seq_putc(s, '-');
297 		seq_puts(s, "]\n\n");
298 		seq_printf(s, "IPU: %u blocks\n", si->inplace_count);
299 		seq_printf(s, "SSR: %u blocks in %u segments\n",
300 			   si->block_count[SSR], si->segment_count[SSR]);
301 		seq_printf(s, "LFS: %u blocks in %u segments\n",
302 			   si->block_count[LFS], si->segment_count[LFS]);
303 
304 		/* segment usage info */
305 		update_sit_info(si->sbi);
306 		seq_printf(s, "\nBDF: %u, avg. vblocks: %u\n",
307 			   si->bimodal, si->avg_vblocks);
308 
309 		/* memory footprint */
310 		update_mem_info(si->sbi);
311 		seq_printf(s, "\nMemory: %u KB\n",
312 			(si->base_mem + si->cache_mem + si->page_mem) >> 10);
313 		seq_printf(s, "  - static: %u KB\n",
314 				si->base_mem >> 10);
315 		seq_printf(s, "  - cached: %u KB\n",
316 				si->cache_mem >> 10);
317 		seq_printf(s, "  - paged : %u KB\n",
318 				si->page_mem >> 10);
319 	}
320 	mutex_unlock(&f2fs_stat_mutex);
321 	return 0;
322 }
323 
324 static int stat_open(struct inode *inode, struct file *file)
325 {
326 	return single_open(file, stat_show, inode->i_private);
327 }
328 
329 static const struct file_operations stat_fops = {
330 	.open = stat_open,
331 	.read = seq_read,
332 	.llseek = seq_lseek,
333 	.release = single_release,
334 };
335 
336 int f2fs_build_stats(struct f2fs_sb_info *sbi)
337 {
338 	struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
339 	struct f2fs_stat_info *si;
340 
341 	si = kzalloc(sizeof(struct f2fs_stat_info), GFP_KERNEL);
342 	if (!si)
343 		return -ENOMEM;
344 
345 	si->all_area_segs = le32_to_cpu(raw_super->segment_count);
346 	si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
347 	si->nat_area_segs = le32_to_cpu(raw_super->segment_count_nat);
348 	si->ssa_area_segs = le32_to_cpu(raw_super->segment_count_ssa);
349 	si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
350 	si->main_area_sections = le32_to_cpu(raw_super->section_count);
351 	si->main_area_zones = si->main_area_sections /
352 				le32_to_cpu(raw_super->secs_per_zone);
353 	si->sbi = sbi;
354 	sbi->stat_info = si;
355 
356 	atomic_set(&sbi->inline_inode, 0);
357 	atomic_set(&sbi->inline_dir, 0);
358 	atomic_set(&sbi->inplace_count, 0);
359 
360 	mutex_lock(&f2fs_stat_mutex);
361 	list_add_tail(&si->stat_list, &f2fs_stat_list);
362 	mutex_unlock(&f2fs_stat_mutex);
363 
364 	return 0;
365 }
366 
367 void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
368 {
369 	struct f2fs_stat_info *si = F2FS_STAT(sbi);
370 
371 	mutex_lock(&f2fs_stat_mutex);
372 	list_del(&si->stat_list);
373 	mutex_unlock(&f2fs_stat_mutex);
374 
375 	kfree(si);
376 }
377 
378 void __init f2fs_create_root_stats(void)
379 {
380 	struct dentry *file;
381 
382 	f2fs_debugfs_root = debugfs_create_dir("f2fs", NULL);
383 	if (!f2fs_debugfs_root)
384 		return;
385 
386 	file = debugfs_create_file("status", S_IRUGO, f2fs_debugfs_root,
387 			NULL, &stat_fops);
388 	if (!file) {
389 		debugfs_remove(f2fs_debugfs_root);
390 		f2fs_debugfs_root = NULL;
391 	}
392 }
393 
394 void f2fs_destroy_root_stats(void)
395 {
396 	if (!f2fs_debugfs_root)
397 		return;
398 
399 	debugfs_remove_recursive(f2fs_debugfs_root);
400 	f2fs_debugfs_root = NULL;
401 }
402