blob: ce2a5eb210b6681f5b5f317314ca9335eb7c9d9b [file] [log] [blame]
Chao Yu6d1a8322018-09-12 09:16:07 +08001// SPDX-License-Identifier: GPL-2.0
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -08002/*
3 * f2fs IO tracer
4 *
5 * Copyright (c) 2014 Motorola Mobility
6 * Copyright (c) 2014 Jaegeuk Kim <jaegeuk@kernel.org>
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -08007 */
8#include <linux/fs.h>
9#include <linux/f2fs_fs.h>
10#include <linux/sched.h>
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -080011#include <linux/radix-tree.h>
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080012
13#include "f2fs.h"
14#include "trace.h"
15
Jaegeuk Kim29e70432015-02-10 16:23:12 -080016static RADIX_TREE(pids, GFP_ATOMIC);
Chao Yubf1750b2017-12-12 14:11:40 +080017static struct mutex pids_lock;
Jaegeuk Kim29e70432015-02-10 16:23:12 -080018static struct last_io_info last_io;
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080019
20static inline void __print_last_io(void)
21{
22 if (!last_io.len)
23 return;
24
Mike Christie04d328d2016-06-05 14:31:55 -050025 trace_printk("%3x:%3x %4x %-16s %2x %5x %5x %12x %4x\n",
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080026 last_io.major, last_io.minor,
27 last_io.pid, "----------------",
28 last_io.type,
Mike Christie04d328d2016-06-05 14:31:55 -050029 last_io.fio.op, last_io.fio.op_flags,
Chao Yu7a9d7542016-02-22 18:36:38 +080030 last_io.fio.new_blkaddr,
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080031 last_io.len);
32 memset(&last_io, 0, sizeof(last_io));
33}
34
35static 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 Kim63f92dd2014-12-17 19:45:05 -080053void f2fs_trace_pid(struct page *page)
54{
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080055 struct inode *inode = page->mapping->host;
56 pid_t pid = task_pid_nr(current);
57 void *p;
58
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070059 set_page_private(page, (unsigned long)pid);
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080060
Jaegeuk Kimc0508652015-01-07 14:07:36 -080061 if (radix_tree_preload(GFP_NOFS))
62 return;
63
Chao Yubf1750b2017-12-12 14:11:40 +080064 mutex_lock(&pids_lock);
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080065 p = radix_tree_lookup(&pids, pid);
66 if (p == current)
Jaegeuk Kimc0508652015-01-07 14:07:36 -080067 goto out;
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080068 if (p)
69 radix_tree_delete(&pids, pid);
70
71 f2fs_radix_tree_insert(&pids, pid, current);
72
73 trace_printk("%3x:%3x %4x %-16s\n",
74 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
75 pid, current->comm);
Jaegeuk Kimc0508652015-01-07 14:07:36 -080076out:
Chao Yubf1750b2017-12-12 14:11:40 +080077 mutex_unlock(&pids_lock);
Jaegeuk Kimc0508652015-01-07 14:07:36 -080078 radix_tree_preload_end();
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080079}
80
Jaegeuk Kim05ca3632015-04-23 14:38:15 -070081void f2fs_trace_ios(struct f2fs_io_info *fio, int flush)
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080082{
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080083 struct inode *inode;
84 pid_t pid;
85 int major, minor;
86
87 if (flush) {
88 __print_last_io();
89 return;
90 }
91
Jaegeuk Kim05ca3632015-04-23 14:38:15 -070092 inode = fio->page->mapping->host;
93 pid = page_private(fio->page);
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080094
95 major = MAJOR(inode->i_sb->s_dev);
96 minor = MINOR(inode->i_sb->s_dev);
97
98 if (last_io.major == major && last_io.minor == minor &&
99 last_io.pid == pid &&
100 last_io.type == __file_type(inode, pid) &&
Mike Christie04d328d2016-06-05 14:31:55 -0500101 last_io.fio.op == fio->op &&
102 last_io.fio.op_flags == fio->op_flags &&
Chao Yu7a9d7542016-02-22 18:36:38 +0800103 last_io.fio.new_blkaddr + last_io.len ==
104 fio->new_blkaddr) {
Jaegeuk Kim0e689d02014-12-17 19:51:57 -0800105 last_io.len++;
106 return;
107 }
108
109 __print_last_io();
110
111 last_io.major = major;
112 last_io.minor = minor;
113 last_io.pid = pid;
114 last_io.type = __file_type(inode, pid);
115 last_io.fio = *fio;
116 last_io.len = 1;
117 return;
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -0800118}
Jaegeuk Kimc0508652015-01-07 14:07:36 -0800119
120void f2fs_build_trace_ios(void)
121{
Chao Yubf1750b2017-12-12 14:11:40 +0800122 mutex_init(&pids_lock);
Jaegeuk Kimc0508652015-01-07 14:07:36 -0800123}
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -0800124
125#define PIDVEC_SIZE 128
126static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
127 unsigned int max_items)
128{
129 struct radix_tree_iter iter;
130 void **slot;
131 unsigned int ret = 0;
132
133 if (unlikely(!max_items))
134 return 0;
135
136 radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
137 results[ret] = iter.index;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700138 if (++ret == max_items)
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -0800139 break;
140 }
141 return ret;
142}
143
144void f2fs_destroy_trace_ios(void)
145{
146 pid_t pid[PIDVEC_SIZE];
147 pid_t next_pid = 0;
148 unsigned int found;
149
Chao Yubf1750b2017-12-12 14:11:40 +0800150 mutex_lock(&pids_lock);
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -0800151 while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
152 unsigned idx;
153
154 next_pid = pid[found - 1] + 1;
155 for (idx = 0; idx < found; idx++)
156 radix_tree_delete(&pids, pid[idx]);
157 }
Chao Yubf1750b2017-12-12 14:11:40 +0800158 mutex_unlock(&pids_lock);
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -0800159}