Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 2 | /* |
| 3 | * f2fs IO tracer |
| 4 | * |
| 5 | * Copyright (c) 2014 Motorola Mobility |
| 6 | * Copyright (c) 2014 Jaegeuk Kim <jaegeuk@kernel.org> |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/fs.h> |
| 9 | #include <linux/f2fs_fs.h> |
| 10 | #include <linux/sched.h> |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 11 | #include <linux/radix-tree.h> |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 12 | |
| 13 | #include "f2fs.h" |
| 14 | #include "trace.h" |
| 15 | |
Jaegeuk Kim | 29e7043 | 2015-02-10 16:23:12 -0800 | [diff] [blame] | 16 | static RADIX_TREE(pids, GFP_ATOMIC); |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 17 | static spinlock_t pids_lock; |
Jaegeuk Kim | 29e7043 | 2015-02-10 16:23:12 -0800 | [diff] [blame] | 18 | static struct last_io_info last_io; |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 19 | |
| 20 | static inline void __print_last_io(void) |
| 21 | { |
| 22 | if (!last_io.len) |
| 23 | return; |
| 24 | |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 25 | trace_printk("%3x:%3x %4x %-16s %2x %5x %5x %12x %4x\n", |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 26 | last_io.major, last_io.minor, |
| 27 | last_io.pid, "----------------", |
| 28 | last_io.type, |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 29 | last_io.fio.op, last_io.fio.op_flags, |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 30 | last_io.fio.new_blkaddr, |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 31 | last_io.len); |
| 32 | memset(&last_io, 0, sizeof(last_io)); |
| 33 | } |
| 34 | |
| 35 | static int __file_type(struct inode *inode, pid_t pid) |
| 36 | { |
| 37 | if (f2fs_is_atomic_file(inode)) |
| 38 | return __ATOMIC_FILE; |
| 39 | else if (f2fs_is_volatile_file(inode)) |
| 40 | return __VOLATILE_FILE; |
| 41 | else if (S_ISDIR(inode->i_mode)) |
| 42 | return __DIR_FILE; |
| 43 | else if (inode->i_ino == F2FS_NODE_INO(F2FS_I_SB(inode))) |
| 44 | return __NODE_FILE; |
| 45 | else if (inode->i_ino == F2FS_META_INO(F2FS_I_SB(inode))) |
| 46 | return __META_FILE; |
| 47 | else if (pid) |
| 48 | return __NORMAL_FILE; |
| 49 | else |
| 50 | return __MISC_FILE; |
| 51 | } |
| 52 | |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 53 | void f2fs_trace_pid(struct page *page) |
| 54 | { |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 55 | struct inode *inode = page->mapping->host; |
| 56 | pid_t pid = task_pid_nr(current); |
| 57 | void *p; |
| 58 | |
Yunlei He | 5f4c3dec | 2017-03-22 11:59:30 +0800 | [diff] [blame] | 59 | set_page_private(page, (unsigned long)pid); |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 60 | |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 61 | retry: |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 62 | if (radix_tree_preload(GFP_NOFS)) |
| 63 | return; |
| 64 | |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 65 | spin_lock(&pids_lock); |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 66 | p = radix_tree_lookup(&pids, pid); |
| 67 | if (p == current) |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 68 | goto out; |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 69 | if (p) |
| 70 | radix_tree_delete(&pids, pid); |
| 71 | |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 72 | if (radix_tree_insert(&pids, pid, current)) { |
| 73 | spin_unlock(&pids_lock); |
| 74 | radix_tree_preload_end(); |
| 75 | cond_resched(); |
| 76 | goto retry; |
| 77 | } |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 78 | |
| 79 | trace_printk("%3x:%3x %4x %-16s\n", |
| 80 | MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev), |
| 81 | pid, current->comm); |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 82 | out: |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 83 | spin_unlock(&pids_lock); |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 84 | radix_tree_preload_end(); |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 87 | void f2fs_trace_ios(struct f2fs_io_info *fio, int flush) |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 88 | { |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 89 | struct inode *inode; |
| 90 | pid_t pid; |
| 91 | int major, minor; |
| 92 | |
| 93 | if (flush) { |
| 94 | __print_last_io(); |
| 95 | return; |
| 96 | } |
| 97 | |
Jaegeuk Kim | 05ca363 | 2015-04-23 14:38:15 -0700 | [diff] [blame] | 98 | inode = fio->page->mapping->host; |
| 99 | pid = page_private(fio->page); |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 100 | |
| 101 | major = MAJOR(inode->i_sb->s_dev); |
| 102 | minor = MINOR(inode->i_sb->s_dev); |
| 103 | |
| 104 | if (last_io.major == major && last_io.minor == minor && |
| 105 | last_io.pid == pid && |
| 106 | last_io.type == __file_type(inode, pid) && |
Mike Christie | 04d328d | 2016-06-05 14:31:55 -0500 | [diff] [blame] | 107 | last_io.fio.op == fio->op && |
| 108 | last_io.fio.op_flags == fio->op_flags && |
Chao Yu | 7a9d754 | 2016-02-22 18:36:38 +0800 | [diff] [blame] | 109 | last_io.fio.new_blkaddr + last_io.len == |
| 110 | fio->new_blkaddr) { |
Jaegeuk Kim | 0e689d03 | 2014-12-17 19:51:57 -0800 | [diff] [blame] | 111 | last_io.len++; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | __print_last_io(); |
| 116 | |
| 117 | last_io.major = major; |
| 118 | last_io.minor = minor; |
| 119 | last_io.pid = pid; |
| 120 | last_io.type = __file_type(inode, pid); |
| 121 | last_io.fio = *fio; |
| 122 | last_io.len = 1; |
| 123 | return; |
Jaegeuk Kim | 63f92dd | 2014-12-17 19:45:05 -0800 | [diff] [blame] | 124 | } |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 125 | |
| 126 | void f2fs_build_trace_ios(void) |
| 127 | { |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 128 | spin_lock_init(&pids_lock); |
Jaegeuk Kim | c050865 | 2015-01-07 14:07:36 -0800 | [diff] [blame] | 129 | } |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 130 | |
| 131 | #define PIDVEC_SIZE 128 |
| 132 | static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index, |
| 133 | unsigned int max_items) |
| 134 | { |
| 135 | struct radix_tree_iter iter; |
| 136 | void **slot; |
| 137 | unsigned int ret = 0; |
| 138 | |
| 139 | if (unlikely(!max_items)) |
| 140 | return 0; |
| 141 | |
| 142 | radix_tree_for_each_slot(slot, &pids, &iter, first_index) { |
| 143 | results[ret] = iter.index; |
Sheng Yong | b0beab5 | 2017-03-08 10:47:11 +0800 | [diff] [blame] | 144 | if (++ret == max_items) |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | void f2fs_destroy_trace_ios(void) |
| 151 | { |
| 152 | pid_t pid[PIDVEC_SIZE]; |
| 153 | pid_t next_pid = 0; |
| 154 | unsigned int found; |
| 155 | |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 156 | spin_lock(&pids_lock); |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 157 | while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) { |
| 158 | unsigned idx; |
| 159 | |
| 160 | next_pid = pid[found - 1] + 1; |
| 161 | for (idx = 0; idx < found; idx++) |
| 162 | radix_tree_delete(&pids, pid[idx]); |
| 163 | } |
Sahitya Tummala | 9083977 | 2019-02-04 13:36:53 +0530 | [diff] [blame] | 164 | spin_unlock(&pids_lock); |
Jaegeuk Kim | 351f4fb | 2015-01-07 14:09:48 -0800 | [diff] [blame] | 165 | } |