debug.c 15 KB

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