blob: 501c283761d29ba51b6d882b44efe9ef0f5cddc1 [file] [log] [blame]
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -08001/*
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 Kim351f4fb2015-01-07 14:09:48 -080014#include <linux/radix-tree.h>
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080015
16#include "f2fs.h"
17#include "trace.h"
18
Jaegeuk Kim29e70432015-02-10 16:23:12 -080019static RADIX_TREE(pids, GFP_ATOMIC);
kbuild test robot08e41262015-01-09 03:38:38 +080020static spinlock_t pids_lock;
Jaegeuk Kim29e70432015-02-10 16:23:12 -080021static struct last_io_info last_io;
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080022
23static inline void __print_last_io(void)
24{
25 if (!last_io.len)
26 return;
27
Mike Christie04d328d2016-06-05 14:31:55 -050028 trace_printk("%3x:%3x %4x %-16s %2x %5x %5x %12x %4x\n",
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080029 last_io.major, last_io.minor,
30 last_io.pid, "----------------",
31 last_io.type,
Mike Christie04d328d2016-06-05 14:31:55 -050032 last_io.fio.op, last_io.fio.op_flags,
Chao Yu7a9d7542016-02-22 18:36:38 +080033 last_io.fio.new_blkaddr,
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080034 last_io.len);
35 memset(&last_io, 0, sizeof(last_io));
36}
37
38static 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 Kim63f92dd2014-12-17 19:45:05 -080056void f2fs_trace_pid(struct page *page)
57{
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080058 struct inode *inode = page->mapping->host;
59 pid_t pid = task_pid_nr(current);
60 void *p;
61
62 page->private = pid;
63
Sahitya Tummalaaf17a242019-02-04 13:36:53 +053064retry:
Jaegeuk Kimc0508652015-01-07 14:07:36 -080065 if (radix_tree_preload(GFP_NOFS))
66 return;
67
68 spin_lock(&pids_lock);
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080069 p = radix_tree_lookup(&pids, pid);
70 if (p == current)
Jaegeuk Kimc0508652015-01-07 14:07:36 -080071 goto out;
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080072 if (p)
73 radix_tree_delete(&pids, pid);
74
Sahitya Tummalaaf17a242019-02-04 13:36:53 +053075 if (radix_tree_insert(&pids, pid, current)) {
76 spin_unlock(&pids_lock);
77 radix_tree_preload_end();
78 cond_resched();
79 goto retry;
80 }
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080081
82 trace_printk("%3x:%3x %4x %-16s\n",
83 MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
84 pid, current->comm);
Jaegeuk Kimc0508652015-01-07 14:07:36 -080085out:
86 spin_unlock(&pids_lock);
87 radix_tree_preload_end();
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080088}
89
Jaegeuk Kim05ca3632015-04-23 14:38:15 -070090void f2fs_trace_ios(struct f2fs_io_info *fio, int flush)
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -080091{
Jaegeuk Kim0e689d02014-12-17 19:51:57 -080092 struct inode *inode;
93 pid_t pid;
94 int major, minor;
95
96 if (flush) {
97 __print_last_io();
98 return;
99 }
100
Jaegeuk Kim05ca3632015-04-23 14:38:15 -0700101 inode = fio->page->mapping->host;
102 pid = page_private(fio->page);
Jaegeuk Kim0e689d02014-12-17 19:51:57 -0800103
104 major = MAJOR(inode->i_sb->s_dev);
105 minor = MINOR(inode->i_sb->s_dev);
106
107 if (last_io.major == major && last_io.minor == minor &&
108 last_io.pid == pid &&
109 last_io.type == __file_type(inode, pid) &&
Mike Christie04d328d2016-06-05 14:31:55 -0500110 last_io.fio.op == fio->op &&
111 last_io.fio.op_flags == fio->op_flags &&
Chao Yu7a9d7542016-02-22 18:36:38 +0800112 last_io.fio.new_blkaddr + last_io.len ==
113 fio->new_blkaddr) {
Jaegeuk Kim0e689d02014-12-17 19:51:57 -0800114 last_io.len++;
115 return;
116 }
117
118 __print_last_io();
119
120 last_io.major = major;
121 last_io.minor = minor;
122 last_io.pid = pid;
123 last_io.type = __file_type(inode, pid);
124 last_io.fio = *fio;
125 last_io.len = 1;
126 return;
Jaegeuk Kim63f92dd2014-12-17 19:45:05 -0800127}
Jaegeuk Kimc0508652015-01-07 14:07:36 -0800128
129void f2fs_build_trace_ios(void)
130{
131 spin_lock_init(&pids_lock);
132}
Jaegeuk Kim351f4fb2015-01-07 14:09:48 -0800133
134#define PIDVEC_SIZE 128
135static unsigned int gang_lookup_pids(pid_t *results, unsigned long first_index,
136 unsigned int max_items)
137{
138 struct radix_tree_iter iter;
139 void **slot;
140 unsigned int ret = 0;
141
142 if (unlikely(!max_items))
143 return 0;
144
145 radix_tree_for_each_slot(slot, &pids, &iter, first_index) {
146 results[ret] = iter.index;
147 if (++ret == PIDVEC_SIZE)
148 break;
149 }
150 return ret;
151}
152
153void f2fs_destroy_trace_ios(void)
154{
155 pid_t pid[PIDVEC_SIZE];
156 pid_t next_pid = 0;
157 unsigned int found;
158
159 spin_lock(&pids_lock);
160 while ((found = gang_lookup_pids(pid, next_pid, PIDVEC_SIZE))) {
161 unsigned idx;
162
163 next_pid = pid[found - 1] + 1;
164 for (idx = 0; idx < found; idx++)
165 radix_tree_delete(&pids, pid[idx]);
166 }
167 spin_unlock(&pids_lock);
168}