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