debug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. /* valid check of the segment numbers */
  31. si->hit_ext = sbi->read_hit_ext;
  32. si->total_ext = sbi->total_hit_ext;
  33. si->ndirty_node = get_pages(sbi, F2FS_DIRTY_NODES);
  34. si->ndirty_dent = get_pages(sbi, F2FS_DIRTY_DENTS);
  35. si->ndirty_dirs = sbi->n_dirty_dirs;
  36. si->ndirty_meta = get_pages(sbi, F2FS_DIRTY_META);
  37. si->total_count = (int)sbi->user_block_count / sbi->blocks_per_seg;
  38. si->rsvd_segs = reserved_segments(sbi);
  39. si->overp_segs = overprovision_segments(sbi);
  40. si->valid_count = valid_user_blocks(sbi);
  41. si->valid_node_count = valid_node_count(sbi);
  42. si->valid_inode_count = valid_inode_count(sbi);
  43. si->inline_inode = sbi->inline_inode;
  44. si->utilization = utilization(sbi);
  45. si->free_segs = free_segments(sbi);
  46. si->free_secs = free_sections(sbi);
  47. si->prefree_count = prefree_segments(sbi);
  48. si->dirty_count = dirty_segments(sbi);
  49. si->node_pages = NODE_MAPPING(sbi)->nrpages;
  50. si->meta_pages = META_MAPPING(sbi)->nrpages;
  51. si->nats = NM_I(sbi)->nat_cnt;
  52. si->sits = SIT_I(sbi)->dirty_sentries;
  53. si->fnids = NM_I(sbi)->fcnt;
  54. si->bg_gc = sbi->bg_gc;
  55. si->util_free = (int)(free_user_blocks(sbi) >> sbi->log_blocks_per_seg)
  56. * 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
  57. / 2;
  58. si->util_valid = (int)(written_block_count(sbi) >>
  59. sbi->log_blocks_per_seg)
  60. * 100 / (int)(sbi->user_block_count >> sbi->log_blocks_per_seg)
  61. / 2;
  62. si->util_invalid = 50 - si->util_free - si->util_valid;
  63. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_NODE; i++) {
  64. struct curseg_info *curseg = CURSEG_I(sbi, i);
  65. si->curseg[i] = curseg->segno;
  66. si->cursec[i] = curseg->segno / sbi->segs_per_sec;
  67. si->curzone[i] = si->cursec[i] / sbi->secs_per_zone;
  68. }
  69. for (i = 0; i < 2; i++) {
  70. si->segment_count[i] = sbi->segment_count[i];
  71. si->block_count[i] = sbi->block_count[i];
  72. }
  73. }
  74. /*
  75. * This function calculates BDF of every segments
  76. */
  77. static void update_sit_info(struct f2fs_sb_info *sbi)
  78. {
  79. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  80. unsigned int blks_per_sec, hblks_per_sec, total_vblocks, bimodal, dist;
  81. unsigned int segno, vblocks;
  82. int ndirty = 0;
  83. bimodal = 0;
  84. total_vblocks = 0;
  85. blks_per_sec = sbi->segs_per_sec * (1 << sbi->log_blocks_per_seg);
  86. hblks_per_sec = blks_per_sec / 2;
  87. for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
  88. vblocks = get_valid_blocks(sbi, segno, sbi->segs_per_sec);
  89. dist = abs(vblocks - hblks_per_sec);
  90. bimodal += dist * dist;
  91. if (vblocks > 0 && vblocks < blks_per_sec) {
  92. total_vblocks += vblocks;
  93. ndirty++;
  94. }
  95. }
  96. dist = TOTAL_SECS(sbi) * hblks_per_sec * hblks_per_sec / 100;
  97. si->bimodal = bimodal / dist;
  98. if (si->dirty_count)
  99. si->avg_vblocks = total_vblocks / ndirty;
  100. else
  101. si->avg_vblocks = 0;
  102. }
  103. /*
  104. * This function calculates memory footprint.
  105. */
  106. static void update_mem_info(struct f2fs_sb_info *sbi)
  107. {
  108. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  109. unsigned npages;
  110. if (si->base_mem)
  111. goto get_cache;
  112. si->base_mem = sizeof(struct f2fs_sb_info) + sbi->sb->s_blocksize;
  113. si->base_mem += 2 * sizeof(struct f2fs_inode_info);
  114. si->base_mem += sizeof(*sbi->ckpt);
  115. /* build sm */
  116. si->base_mem += sizeof(struct f2fs_sm_info);
  117. /* build sit */
  118. si->base_mem += sizeof(struct sit_info);
  119. si->base_mem += TOTAL_SEGS(sbi) * sizeof(struct seg_entry);
  120. si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi));
  121. si->base_mem += 2 * SIT_VBLOCK_MAP_SIZE * TOTAL_SEGS(sbi);
  122. if (sbi->segs_per_sec > 1)
  123. si->base_mem += TOTAL_SECS(sbi) * sizeof(struct sec_entry);
  124. si->base_mem += __bitmap_size(sbi, SIT_BITMAP);
  125. /* build free segmap */
  126. si->base_mem += sizeof(struct free_segmap_info);
  127. si->base_mem += f2fs_bitmap_size(TOTAL_SEGS(sbi));
  128. si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi));
  129. /* build curseg */
  130. si->base_mem += sizeof(struct curseg_info) * NR_CURSEG_TYPE;
  131. si->base_mem += PAGE_CACHE_SIZE * NR_CURSEG_TYPE;
  132. /* build dirty segmap */
  133. si->base_mem += sizeof(struct dirty_seglist_info);
  134. si->base_mem += NR_DIRTY_TYPE * f2fs_bitmap_size(TOTAL_SEGS(sbi));
  135. si->base_mem += f2fs_bitmap_size(TOTAL_SECS(sbi));
  136. /* buld nm */
  137. si->base_mem += sizeof(struct f2fs_nm_info);
  138. si->base_mem += __bitmap_size(sbi, NAT_BITMAP);
  139. /* build gc */
  140. si->base_mem += sizeof(struct f2fs_gc_kthread);
  141. get_cache:
  142. /* free nids */
  143. si->cache_mem = NM_I(sbi)->fcnt;
  144. si->cache_mem += NM_I(sbi)->nat_cnt;
  145. npages = NODE_MAPPING(sbi)->nrpages;
  146. si->cache_mem += npages << PAGE_CACHE_SHIFT;
  147. npages = META_MAPPING(sbi)->nrpages;
  148. si->cache_mem += npages << PAGE_CACHE_SHIFT;
  149. si->cache_mem += sbi->n_orphans * sizeof(struct orphan_inode_entry);
  150. si->cache_mem += sbi->n_dirty_dirs * sizeof(struct dir_inode_entry);
  151. }
  152. static int stat_show(struct seq_file *s, void *v)
  153. {
  154. struct f2fs_stat_info *si;
  155. int i = 0;
  156. int j;
  157. mutex_lock(&f2fs_stat_mutex);
  158. list_for_each_entry(si, &f2fs_stat_list, stat_list) {
  159. char devname[BDEVNAME_SIZE];
  160. update_general_status(si->sbi);
  161. seq_printf(s, "\n=====[ partition info(%s). #%d ]=====\n",
  162. bdevname(si->sbi->sb->s_bdev, devname), i++);
  163. seq_printf(s, "[SB: 1] [CP: 2] [SIT: %d] [NAT: %d] ",
  164. si->sit_area_segs, si->nat_area_segs);
  165. seq_printf(s, "[SSA: %d] [MAIN: %d",
  166. si->ssa_area_segs, si->main_area_segs);
  167. seq_printf(s, "(OverProv:%d Resv:%d)]\n\n",
  168. si->overp_segs, si->rsvd_segs);
  169. seq_printf(s, "Utilization: %d%% (%d valid blocks)\n",
  170. si->utilization, si->valid_count);
  171. seq_printf(s, " - Node: %u (Inode: %u, ",
  172. si->valid_node_count, si->valid_inode_count);
  173. seq_printf(s, "Other: %u)\n - Data: %u\n",
  174. si->valid_node_count - si->valid_inode_count,
  175. si->valid_count - si->valid_node_count);
  176. seq_printf(s, " - Inline_data Inode: %u\n",
  177. si->inline_inode);
  178. seq_printf(s, "\nMain area: %d segs, %d secs %d zones\n",
  179. si->main_area_segs, si->main_area_sections,
  180. si->main_area_zones);
  181. seq_printf(s, " - COLD data: %d, %d, %d\n",
  182. si->curseg[CURSEG_COLD_DATA],
  183. si->cursec[CURSEG_COLD_DATA],
  184. si->curzone[CURSEG_COLD_DATA]);
  185. seq_printf(s, " - WARM data: %d, %d, %d\n",
  186. si->curseg[CURSEG_WARM_DATA],
  187. si->cursec[CURSEG_WARM_DATA],
  188. si->curzone[CURSEG_WARM_DATA]);
  189. seq_printf(s, " - HOT data: %d, %d, %d\n",
  190. si->curseg[CURSEG_HOT_DATA],
  191. si->cursec[CURSEG_HOT_DATA],
  192. si->curzone[CURSEG_HOT_DATA]);
  193. seq_printf(s, " - Dir dnode: %d, %d, %d\n",
  194. si->curseg[CURSEG_HOT_NODE],
  195. si->cursec[CURSEG_HOT_NODE],
  196. si->curzone[CURSEG_HOT_NODE]);
  197. seq_printf(s, " - File dnode: %d, %d, %d\n",
  198. si->curseg[CURSEG_WARM_NODE],
  199. si->cursec[CURSEG_WARM_NODE],
  200. si->curzone[CURSEG_WARM_NODE]);
  201. seq_printf(s, " - Indir nodes: %d, %d, %d\n",
  202. si->curseg[CURSEG_COLD_NODE],
  203. si->cursec[CURSEG_COLD_NODE],
  204. si->curzone[CURSEG_COLD_NODE]);
  205. seq_printf(s, "\n - Valid: %d\n - Dirty: %d\n",
  206. si->main_area_segs - si->dirty_count -
  207. si->prefree_count - si->free_segs,
  208. si->dirty_count);
  209. seq_printf(s, " - Prefree: %d\n - Free: %d (%d)\n\n",
  210. si->prefree_count, si->free_segs, si->free_secs);
  211. seq_printf(s, "CP calls: %d\n", si->cp_count);
  212. seq_printf(s, "GC calls: %d (BG: %d)\n",
  213. si->call_count, si->bg_gc);
  214. seq_printf(s, " - data segments : %d\n", si->data_segs);
  215. seq_printf(s, " - node segments : %d\n", si->node_segs);
  216. seq_printf(s, "Try to move %d blocks\n", si->tot_blks);
  217. seq_printf(s, " - data blocks : %d\n", si->data_blks);
  218. seq_printf(s, " - node blocks : %d\n", si->node_blks);
  219. seq_printf(s, "\nExtent Hit Ratio: %d / %d\n",
  220. si->hit_ext, si->total_ext);
  221. seq_puts(s, "\nBalancing F2FS Async:\n");
  222. seq_printf(s, " - nodes: %4d in %4d\n",
  223. si->ndirty_node, si->node_pages);
  224. seq_printf(s, " - dents: %4d in dirs:%4d\n",
  225. si->ndirty_dent, si->ndirty_dirs);
  226. seq_printf(s, " - meta: %4d in %4d\n",
  227. si->ndirty_meta, si->meta_pages);
  228. seq_printf(s, " - NATs: %9d\n - SITs: %9d\n",
  229. si->nats, si->sits);
  230. seq_printf(s, " - free_nids: %9d\n",
  231. si->fnids);
  232. seq_puts(s, "\nDistribution of User Blocks:");
  233. seq_puts(s, " [ valid | invalid | free ]\n");
  234. seq_puts(s, " [");
  235. for (j = 0; j < si->util_valid; j++)
  236. seq_putc(s, '-');
  237. seq_putc(s, '|');
  238. for (j = 0; j < si->util_invalid; j++)
  239. seq_putc(s, '-');
  240. seq_putc(s, '|');
  241. for (j = 0; j < si->util_free; j++)
  242. seq_putc(s, '-');
  243. seq_puts(s, "]\n\n");
  244. seq_printf(s, "SSR: %u blocks in %u segments\n",
  245. si->block_count[SSR], si->segment_count[SSR]);
  246. seq_printf(s, "LFS: %u blocks in %u segments\n",
  247. si->block_count[LFS], si->segment_count[LFS]);
  248. /* segment usage info */
  249. update_sit_info(si->sbi);
  250. seq_printf(s, "\nBDF: %u, avg. vblocks: %u\n",
  251. si->bimodal, si->avg_vblocks);
  252. /* memory footprint */
  253. update_mem_info(si->sbi);
  254. seq_printf(s, "\nMemory: %u KB = static: %u + cached: %u\n",
  255. (si->base_mem + si->cache_mem) >> 10,
  256. si->base_mem >> 10, si->cache_mem >> 10);
  257. }
  258. mutex_unlock(&f2fs_stat_mutex);
  259. return 0;
  260. }
  261. static int stat_open(struct inode *inode, struct file *file)
  262. {
  263. return single_open(file, stat_show, inode->i_private);
  264. }
  265. static const struct file_operations stat_fops = {
  266. .open = stat_open,
  267. .read = seq_read,
  268. .llseek = seq_lseek,
  269. .release = single_release,
  270. };
  271. int f2fs_build_stats(struct f2fs_sb_info *sbi)
  272. {
  273. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  274. struct f2fs_stat_info *si;
  275. si = kzalloc(sizeof(struct f2fs_stat_info), GFP_KERNEL);
  276. if (!si)
  277. return -ENOMEM;
  278. si->all_area_segs = le32_to_cpu(raw_super->segment_count);
  279. si->sit_area_segs = le32_to_cpu(raw_super->segment_count_sit);
  280. si->nat_area_segs = le32_to_cpu(raw_super->segment_count_nat);
  281. si->ssa_area_segs = le32_to_cpu(raw_super->segment_count_ssa);
  282. si->main_area_segs = le32_to_cpu(raw_super->segment_count_main);
  283. si->main_area_sections = le32_to_cpu(raw_super->section_count);
  284. si->main_area_zones = si->main_area_sections /
  285. le32_to_cpu(raw_super->secs_per_zone);
  286. si->sbi = sbi;
  287. sbi->stat_info = si;
  288. mutex_lock(&f2fs_stat_mutex);
  289. list_add_tail(&si->stat_list, &f2fs_stat_list);
  290. mutex_unlock(&f2fs_stat_mutex);
  291. return 0;
  292. }
  293. void f2fs_destroy_stats(struct f2fs_sb_info *sbi)
  294. {
  295. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  296. mutex_lock(&f2fs_stat_mutex);
  297. list_del(&si->stat_list);
  298. mutex_unlock(&f2fs_stat_mutex);
  299. kfree(si);
  300. }
  301. void __init f2fs_create_root_stats(void)
  302. {
  303. struct dentry *file;
  304. f2fs_debugfs_root = debugfs_create_dir("f2fs", NULL);
  305. if (!f2fs_debugfs_root)
  306. goto bail;
  307. file = debugfs_create_file("status", S_IRUGO, f2fs_debugfs_root,
  308. NULL, &stat_fops);
  309. if (!file)
  310. goto free_debugfs_dir;
  311. return;
  312. free_debugfs_dir:
  313. debugfs_remove(f2fs_debugfs_root);
  314. bail:
  315. f2fs_debugfs_root = NULL;
  316. return;
  317. }
  318. void f2fs_destroy_root_stats(void)
  319. {
  320. if (!f2fs_debugfs_root)
  321. return;
  322. debugfs_remove_recursive(f2fs_debugfs_root);
  323. f2fs_debugfs_root = NULL;
  324. }