trace.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 %12x %4x\n",
  25. last_io.major, last_io.minor,
  26. last_io.pid, "----------------",
  27. last_io.type,
  28. last_io.fio.rw, last_io.fio.blk_addr,
  29. last_io.len);
  30. memset(&last_io, 0, sizeof(last_io));
  31. }
  32. static int __file_type(struct inode *inode, pid_t pid)
  33. {
  34. if (f2fs_is_atomic_file(inode))
  35. return __ATOMIC_FILE;
  36. else if (f2fs_is_volatile_file(inode))
  37. return __VOLATILE_FILE;
  38. else if (S_ISDIR(inode->i_mode))
  39. return __DIR_FILE;
  40. else if (inode->i_ino == F2FS_NODE_INO(F2FS_I_SB(inode)))
  41. return __NODE_FILE;
  42. else if (inode->i_ino == F2FS_META_INO(F2FS_I_SB(inode)))
  43. return __META_FILE;
  44. else if (pid)
  45. return __NORMAL_FILE;
  46. else
  47. return __MISC_FILE;
  48. }
  49. void f2fs_trace_pid(struct page *page)
  50. {
  51. struct inode *inode = page->mapping->host;
  52. pid_t pid = task_pid_nr(current);
  53. void *p;
  54. page->private = pid;
  55. retry:
  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. if (radix_tree_insert(&pids, pid, current)) {
  65. spin_unlock(&pids_lock);
  66. radix_tree_preload_end();
  67. cond_resched();
  68. goto retry;
  69. }
  70. trace_printk("%3x:%3x %4x %-16s\n",
  71. MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
  72. pid, current->comm);
  73. out:
  74. spin_unlock(&pids_lock);
  75. radix_tree_preload_end();
  76. }
  77. void f2fs_trace_ios(struct f2fs_io_info *fio, int flush)
  78. {
  79. struct inode *inode;
  80. pid_t pid;
  81. int major, minor;
  82. if (flush) {
  83. __print_last_io();
  84. return;
  85. }
  86. inode = fio->page->mapping->host;
  87. pid = page_private(fio->page);
  88. major = MAJOR(inode->i_sb->s_dev);
  89. minor = MINOR(inode->i_sb->s_dev);
  90. if (last_io.major == major && last_io.minor == minor &&
  91. last_io.pid == pid &&
  92. last_io.type == __file_type(inode, pid) &&
  93. last_io.fio.rw == fio->rw &&
  94. last_io.fio.blk_addr + last_io.len == fio->blk_addr) {
  95. last_io.len++;
  96. return;
  97. }
  98. __print_last_io();
  99. last_io.major = major;
  100. last_io.minor = minor;
  101. last_io.pid = pid;
  102. last_io.type = __file_type(inode, pid);
  103. last_io.fio = *fio;
  104. last_io.len = 1;
  105. return;
  106. }
  107. void f2fs_build_trace_ios(void)
  108. {
  109. spin_lock_init(&pids_lock);
  110. }
  111. #define PIDVEC_SIZE 128
  112. static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
  113. unsigned int max_items)
  114. {
  115. struct radix_tree_iter iter;
  116. void **slot;
  117. unsigned int ret = 0;
  118. if (unlikely(!max_items))
  119. return 0;
  120. radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
  121. results[ret] = iter.index;
  122. if (++ret == PIDVEC_SIZE)
  123. break;
  124. }
  125. return ret;
  126. }
  127. void f2fs_destroy_trace_ios(void)
  128. {
  129. pid_t pid[PIDVEC_SIZE];
  130. pid_t next_pid = 0;
  131. unsigned int found;
  132. spin_lock(&pids_lock);
  133. while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
  134. unsigned idx;
  135. next_pid = pid[found - 1] + 1;
  136. for (idx = 0; idx < found; idx++)
  137. radix_tree_delete(&pids, pid[idx]);
  138. }
  139. spin_unlock(&pids_lock);
  140. }