trace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. page->private = pid;
  56. if (radix_tree_preload(GFP_NOFS))
  57. return;
  58. spin_lock(&pids_lock);
  59. p = radix_tree_lookup(&pids, pid);
  60. if (p == current)
  61. goto out;
  62. if (p)
  63. radix_tree_delete(&pids, pid);
  64. f2fs_radix_tree_insert(&pids, pid, current);
  65. trace_printk("%3x:%3x %4x %-16s\n",
  66. MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
  67. pid, current->comm);
  68. out:
  69. spin_unlock(&pids_lock);
  70. radix_tree_preload_end();
  71. }
  72. void f2fs_trace_ios(struct f2fs_io_info *fio, int flush)
  73. {
  74. struct inode *inode;
  75. pid_t pid;
  76. int major, minor;
  77. if (flush) {
  78. __print_last_io();
  79. return;
  80. }
  81. inode = fio->page->mapping->host;
  82. pid = page_private(fio->page);
  83. major = MAJOR(inode->i_sb->s_dev);
  84. minor = MINOR(inode->i_sb->s_dev);
  85. if (last_io.major == major && last_io.minor == minor &&
  86. last_io.pid == pid &&
  87. last_io.type == __file_type(inode, pid) &&
  88. last_io.fio.op == fio->op &&
  89. last_io.fio.op_flags == fio->op_flags &&
  90. last_io.fio.new_blkaddr + last_io.len ==
  91. fio->new_blkaddr) {
  92. last_io.len++;
  93. return;
  94. }
  95. __print_last_io();
  96. last_io.major = major;
  97. last_io.minor = minor;
  98. last_io.pid = pid;
  99. last_io.type = __file_type(inode, pid);
  100. last_io.fio = *fio;
  101. last_io.len = 1;
  102. return;
  103. }
  104. void f2fs_build_trace_ios(void)
  105. {
  106. spin_lock_init(&pids_lock);
  107. }
  108. #define PIDVEC_SIZE 128
  109. static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
  110. unsigned int max_items)
  111. {
  112. struct radix_tree_iter iter;
  113. void **slot;
  114. unsigned int ret = 0;
  115. if (unlikely(!max_items))
  116. return 0;
  117. radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
  118. results[ret] = iter.index;
  119. if (++ret == PIDVEC_SIZE)
  120. break;
  121. }
  122. return ret;
  123. }
  124. void f2fs_destroy_trace_ios(void)
  125. {
  126. pid_t pid[PIDVEC_SIZE];
  127. pid_t next_pid = 0;
  128. unsigned int found;
  129. spin_lock(&pids_lock);
  130. while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
  131. unsigned idx;
  132. next_pid = pid[found - 1] + 1;
  133. for (idx = 0; idx < found; idx++)
  134. radix_tree_delete(&pids, pid[idx]);
  135. }
  136. spin_unlock(&pids_lock);
  137. }