trace.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * f2fs IO tracer
  3. *
  4. * Copyright (c) 2014 Motorola Mobility
  5. * Copyright (c) 2014 Jaegeuk Kim <jaegeuk@kernel.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/sched.h>
  14. #include <linux/radix-tree.h>
  15. #include "f2fs.h"
  16. #include "trace.h"
  17. static RADIX_TREE(pids, GFP_ATOMIC);
  18. static spinlock_t pids_lock;
  19. static struct last_io_info last_io;
  20. static inline void __print_last_io(void)
  21. {
  22. if (!last_io.len)
  23. return;
  24. trace_printk("%3x:%3x %4x %-16s %2x %5x %5x %12x %4x\n",
  25. last_io.major, last_io.minor,
  26. last_io.pid, "----------------",
  27. last_io.type,
  28. last_io.fio.op, last_io.fio.op_flags,
  29. last_io.fio.new_blkaddr,
  30. last_io.len);
  31. memset(&last_io, 0, sizeof(last_io));
  32. }
  33. static int __file_type(struct inode *inode, pid_t pid)
  34. {
  35. if (f2fs_is_atomic_file(inode))
  36. return __ATOMIC_FILE;
  37. else if (f2fs_is_volatile_file(inode))
  38. return __VOLATILE_FILE;
  39. else if (S_ISDIR(inode->i_mode))
  40. return __DIR_FILE;
  41. else if (inode->i_ino == F2FS_NODE_INO(F2FS_I_SB(inode)))
  42. return __NODE_FILE;
  43. else if (inode->i_ino == F2FS_META_INO(F2FS_I_SB(inode)))
  44. return __META_FILE;
  45. else if (pid)
  46. return __NORMAL_FILE;
  47. else
  48. return __MISC_FILE;
  49. }
  50. void f2fs_trace_pid(struct page *page)
  51. {
  52. struct inode *inode = page->mapping->host;
  53. pid_t pid = task_pid_nr(current);
  54. void *p;
  55. set_page_private(page, (unsigned long)pid);
  56. retry:
  57. if (radix_tree_preload(GFP_NOFS))
  58. return;
  59. spin_lock(&pids_lock);
  60. p = radix_tree_lookup(&pids, pid);
  61. if (p == current)
  62. goto out;
  63. if (p)
  64. radix_tree_delete(&pids, pid);
  65. if (radix_tree_insert(&pids, pid, current)) {
  66. spin_unlock(&pids_lock);
  67. radix_tree_preload_end();
  68. cond_resched();
  69. goto retry;
  70. }
  71. trace_printk("%3x:%3x %4x %-16s\n",
  72. MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
  73. pid, current->comm);
  74. out:
  75. spin_unlock(&pids_lock);
  76. radix_tree_preload_end();
  77. }
  78. void f2fs_trace_ios(struct f2fs_io_info *fio, int flush)
  79. {
  80. struct inode *inode;
  81. pid_t pid;
  82. int major, minor;
  83. if (flush) {
  84. __print_last_io();
  85. return;
  86. }
  87. inode = fio->page->mapping->host;
  88. pid = page_private(fio->page);
  89. major = MAJOR(inode->i_sb->s_dev);
  90. minor = MINOR(inode->i_sb->s_dev);
  91. if (last_io.major == major && last_io.minor == minor &&
  92. last_io.pid == pid &&
  93. last_io.type == __file_type(inode, pid) &&
  94. last_io.fio.op == fio->op &&
  95. last_io.fio.op_flags == fio->op_flags &&
  96. last_io.fio.new_blkaddr + last_io.len ==
  97. fio->new_blkaddr) {
  98. last_io.len++;
  99. return;
  100. }
  101. __print_last_io();
  102. last_io.major = major;
  103. last_io.minor = minor;
  104. last_io.pid = pid;
  105. last_io.type = __file_type(inode, pid);
  106. last_io.fio = *fio;
  107. last_io.len = 1;
  108. return;
  109. }
  110. void f2fs_build_trace_ios(void)
  111. {
  112. spin_lock_init(&pids_lock);
  113. }
  114. #define PIDVEC_SIZE 128
  115. static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
  116. unsigned int max_items)
  117. {
  118. struct radix_tree_iter iter;
  119. void **slot;
  120. unsigned int ret = 0;
  121. if (unlikely(!max_items))
  122. return 0;
  123. radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
  124. results[ret] = iter.index;
  125. if (++ret == max_items)
  126. break;
  127. }
  128. return ret;
  129. }
  130. void f2fs_destroy_trace_ios(void)
  131. {
  132. pid_t pid[PIDVEC_SIZE];
  133. pid_t next_pid = 0;
  134. unsigned int found;
  135. spin_lock(&pids_lock);
  136. while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
  137. unsigned idx;
  138. next_pid = pid[found - 1] + 1;
  139. for (idx = 0; idx < found; idx++)
  140. radix_tree_delete(&pids, pid[idx]);
  141. }
  142. spin_unlock(&pids_lock);
  143. }