blktrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/blktrace_api.h>
  21. #include <linux/percpu.h>
  22. #include <linux/init.h>
  23. #include <linux/mutex.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/time.h>
  26. #include <asm/uaccess.h>
  27. static DEFINE_PER_CPU(unsigned long long, blk_trace_cpu_offset) = { 0, };
  28. static unsigned int blktrace_seq __read_mostly = 1;
  29. /*
  30. * Send out a notify message.
  31. */
  32. static inline unsigned int trace_note(struct blk_trace *bt,
  33. pid_t pid, int action,
  34. const void *data, size_t len)
  35. {
  36. struct blk_io_trace *t;
  37. int cpu = smp_processor_id();
  38. t = relay_reserve(bt->rchan, sizeof(*t) + len);
  39. if (t == NULL)
  40. return 0;
  41. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  42. t->time = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
  43. t->device = bt->dev;
  44. t->action = action;
  45. t->pid = pid;
  46. t->cpu = cpu;
  47. t->pdu_len = len;
  48. memcpy((void *) t + sizeof(*t), data, len);
  49. return blktrace_seq;
  50. }
  51. /*
  52. * Send out a notify for this process, if we haven't done so since a trace
  53. * started
  54. */
  55. static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
  56. {
  57. tsk->btrace_seq = trace_note(bt, tsk->pid,
  58. BLK_TN_PROCESS,
  59. tsk->comm, sizeof(tsk->comm));
  60. }
  61. static void trace_note_time(struct blk_trace *bt)
  62. {
  63. struct timespec now;
  64. unsigned long flags;
  65. u32 words[2];
  66. getnstimeofday(&now);
  67. words[0] = now.tv_sec;
  68. words[1] = now.tv_nsec;
  69. local_irq_save(flags);
  70. trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
  71. local_irq_restore(flags);
  72. }
  73. static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
  74. pid_t pid)
  75. {
  76. if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
  77. return 1;
  78. if (sector < bt->start_lba || sector > bt->end_lba)
  79. return 1;
  80. if (bt->pid && pid != bt->pid)
  81. return 1;
  82. return 0;
  83. }
  84. /*
  85. * Data direction bit lookup
  86. */
  87. static u32 ddir_act[2] __read_mostly = { BLK_TC_ACT(BLK_TC_READ), BLK_TC_ACT(BLK_TC_WRITE) };
  88. /*
  89. * Bio action bits of interest
  90. */
  91. static u32 bio_act[9] __read_mostly = { 0, BLK_TC_ACT(BLK_TC_BARRIER), BLK_TC_ACT(BLK_TC_SYNC), 0, BLK_TC_ACT(BLK_TC_AHEAD), 0, 0, 0, BLK_TC_ACT(BLK_TC_META) };
  92. /*
  93. * More could be added as needed, taking care to increment the decrementer
  94. * to get correct indexing
  95. */
  96. #define trace_barrier_bit(rw) \
  97. (((rw) & (1 << BIO_RW_BARRIER)) >> (BIO_RW_BARRIER - 0))
  98. #define trace_sync_bit(rw) \
  99. (((rw) & (1 << BIO_RW_SYNC)) >> (BIO_RW_SYNC - 1))
  100. #define trace_ahead_bit(rw) \
  101. (((rw) & (1 << BIO_RW_AHEAD)) << (2 - BIO_RW_AHEAD))
  102. #define trace_meta_bit(rw) \
  103. (((rw) & (1 << BIO_RW_META)) >> (BIO_RW_META - 3))
  104. /*
  105. * The worker for the various blk_add_trace*() types. Fills out a
  106. * blk_io_trace structure and places it in a per-cpu subbuffer.
  107. */
  108. void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
  109. int rw, u32 what, int error, int pdu_len, void *pdu_data)
  110. {
  111. struct task_struct *tsk = current;
  112. struct blk_io_trace *t;
  113. unsigned long flags;
  114. unsigned long *sequence;
  115. pid_t pid;
  116. int cpu;
  117. if (unlikely(bt->trace_state != Blktrace_running))
  118. return;
  119. what |= ddir_act[rw & WRITE];
  120. what |= bio_act[trace_barrier_bit(rw)];
  121. what |= bio_act[trace_sync_bit(rw)];
  122. what |= bio_act[trace_ahead_bit(rw)];
  123. what |= bio_act[trace_meta_bit(rw)];
  124. pid = tsk->pid;
  125. if (unlikely(act_log_check(bt, what, sector, pid)))
  126. return;
  127. /*
  128. * A word about the locking here - we disable interrupts to reserve
  129. * some space in the relay per-cpu buffer, to prevent an irq
  130. * from coming in and stepping on our toes. Once reserved, it's
  131. * enough to get preemption disabled to prevent read of this data
  132. * before we are through filling it. get_cpu()/put_cpu() does this
  133. * for us
  134. */
  135. local_irq_save(flags);
  136. if (unlikely(tsk->btrace_seq != blktrace_seq))
  137. trace_note_tsk(bt, tsk);
  138. t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
  139. if (t) {
  140. cpu = smp_processor_id();
  141. sequence = per_cpu_ptr(bt->sequence, cpu);
  142. t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
  143. t->sequence = ++(*sequence);
  144. t->time = sched_clock() - per_cpu(blk_trace_cpu_offset, cpu);
  145. t->sector = sector;
  146. t->bytes = bytes;
  147. t->action = what;
  148. t->pid = pid;
  149. t->device = bt->dev;
  150. t->cpu = cpu;
  151. t->error = error;
  152. t->pdu_len = pdu_len;
  153. if (pdu_len)
  154. memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
  155. }
  156. local_irq_restore(flags);
  157. }
  158. EXPORT_SYMBOL_GPL(__blk_add_trace);
  159. static struct dentry *blk_tree_root;
  160. static struct mutex blk_tree_mutex;
  161. static unsigned int root_users;
  162. static inline void blk_remove_root(void)
  163. {
  164. if (blk_tree_root) {
  165. debugfs_remove(blk_tree_root);
  166. blk_tree_root = NULL;
  167. }
  168. }
  169. static void blk_remove_tree(struct dentry *dir)
  170. {
  171. mutex_lock(&blk_tree_mutex);
  172. debugfs_remove(dir);
  173. if (--root_users == 0)
  174. blk_remove_root();
  175. mutex_unlock(&blk_tree_mutex);
  176. }
  177. static struct dentry *blk_create_tree(const char *blk_name)
  178. {
  179. struct dentry *dir = NULL;
  180. mutex_lock(&blk_tree_mutex);
  181. if (!blk_tree_root) {
  182. blk_tree_root = debugfs_create_dir("block", NULL);
  183. if (!blk_tree_root)
  184. goto err;
  185. }
  186. dir = debugfs_create_dir(blk_name, blk_tree_root);
  187. if (dir)
  188. root_users++;
  189. else
  190. blk_remove_root();
  191. err:
  192. mutex_unlock(&blk_tree_mutex);
  193. return dir;
  194. }
  195. static void blk_trace_cleanup(struct blk_trace *bt)
  196. {
  197. relay_close(bt->rchan);
  198. debugfs_remove(bt->dropped_file);
  199. blk_remove_tree(bt->dir);
  200. free_percpu(bt->sequence);
  201. kfree(bt);
  202. }
  203. static int blk_trace_remove(request_queue_t *q)
  204. {
  205. struct blk_trace *bt;
  206. bt = xchg(&q->blk_trace, NULL);
  207. if (!bt)
  208. return -EINVAL;
  209. if (bt->trace_state == Blktrace_setup ||
  210. bt->trace_state == Blktrace_stopped)
  211. blk_trace_cleanup(bt);
  212. return 0;
  213. }
  214. static int blk_dropped_open(struct inode *inode, struct file *filp)
  215. {
  216. filp->private_data = inode->i_private;
  217. return 0;
  218. }
  219. static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
  220. size_t count, loff_t *ppos)
  221. {
  222. struct blk_trace *bt = filp->private_data;
  223. char buf[16];
  224. snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
  225. return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
  226. }
  227. static struct file_operations blk_dropped_fops = {
  228. .owner = THIS_MODULE,
  229. .open = blk_dropped_open,
  230. .read = blk_dropped_read,
  231. };
  232. /*
  233. * Keep track of how many times we encountered a full subbuffer, to aid
  234. * the user space app in telling how many lost events there were.
  235. */
  236. static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
  237. void *prev_subbuf, size_t prev_padding)
  238. {
  239. struct blk_trace *bt;
  240. if (!relay_buf_full(buf))
  241. return 1;
  242. bt = buf->chan->private_data;
  243. atomic_inc(&bt->dropped);
  244. return 0;
  245. }
  246. static int blk_remove_buf_file_callback(struct dentry *dentry)
  247. {
  248. debugfs_remove(dentry);
  249. return 0;
  250. }
  251. static struct dentry *blk_create_buf_file_callback(const char *filename,
  252. struct dentry *parent,
  253. int mode,
  254. struct rchan_buf *buf,
  255. int *is_global)
  256. {
  257. return debugfs_create_file(filename, mode, parent, buf,
  258. &relay_file_operations);
  259. }
  260. static struct rchan_callbacks blk_relay_callbacks = {
  261. .subbuf_start = blk_subbuf_start_callback,
  262. .create_buf_file = blk_create_buf_file_callback,
  263. .remove_buf_file = blk_remove_buf_file_callback,
  264. };
  265. /*
  266. * Setup everything required to start tracing
  267. */
  268. static int blk_trace_setup(request_queue_t *q, struct block_device *bdev,
  269. char __user *arg)
  270. {
  271. struct blk_user_trace_setup buts;
  272. struct blk_trace *old_bt, *bt = NULL;
  273. struct dentry *dir = NULL;
  274. char b[BDEVNAME_SIZE];
  275. int ret, i;
  276. if (copy_from_user(&buts, arg, sizeof(buts)))
  277. return -EFAULT;
  278. if (!buts.buf_size || !buts.buf_nr)
  279. return -EINVAL;
  280. strcpy(buts.name, bdevname(bdev, b));
  281. /*
  282. * some device names have larger paths - convert the slashes
  283. * to underscores for this to work as expected
  284. */
  285. for (i = 0; i < strlen(buts.name); i++)
  286. if (buts.name[i] == '/')
  287. buts.name[i] = '_';
  288. if (copy_to_user(arg, &buts, sizeof(buts)))
  289. return -EFAULT;
  290. ret = -ENOMEM;
  291. bt = kzalloc(sizeof(*bt), GFP_KERNEL);
  292. if (!bt)
  293. goto err;
  294. bt->sequence = alloc_percpu(unsigned long);
  295. if (!bt->sequence)
  296. goto err;
  297. ret = -ENOENT;
  298. dir = blk_create_tree(buts.name);
  299. if (!dir)
  300. goto err;
  301. bt->dir = dir;
  302. bt->dev = bdev->bd_dev;
  303. atomic_set(&bt->dropped, 0);
  304. ret = -EIO;
  305. bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt, &blk_dropped_fops);
  306. if (!bt->dropped_file)
  307. goto err;
  308. bt->rchan = relay_open("trace", dir, buts.buf_size, buts.buf_nr, &blk_relay_callbacks);
  309. if (!bt->rchan)
  310. goto err;
  311. bt->rchan->private_data = bt;
  312. bt->act_mask = buts.act_mask;
  313. if (!bt->act_mask)
  314. bt->act_mask = (u16) -1;
  315. bt->start_lba = buts.start_lba;
  316. bt->end_lba = buts.end_lba;
  317. if (!bt->end_lba)
  318. bt->end_lba = -1ULL;
  319. bt->pid = buts.pid;
  320. bt->trace_state = Blktrace_setup;
  321. ret = -EBUSY;
  322. old_bt = xchg(&q->blk_trace, bt);
  323. if (old_bt) {
  324. (void) xchg(&q->blk_trace, old_bt);
  325. goto err;
  326. }
  327. return 0;
  328. err:
  329. if (dir)
  330. blk_remove_tree(dir);
  331. if (bt) {
  332. if (bt->dropped_file)
  333. debugfs_remove(bt->dropped_file);
  334. if (bt->sequence)
  335. free_percpu(bt->sequence);
  336. if (bt->rchan)
  337. relay_close(bt->rchan);
  338. kfree(bt);
  339. }
  340. return ret;
  341. }
  342. static int blk_trace_startstop(request_queue_t *q, int start)
  343. {
  344. struct blk_trace *bt;
  345. int ret;
  346. if ((bt = q->blk_trace) == NULL)
  347. return -EINVAL;
  348. /*
  349. * For starting a trace, we can transition from a setup or stopped
  350. * trace. For stopping a trace, the state must be running
  351. */
  352. ret = -EINVAL;
  353. if (start) {
  354. if (bt->trace_state == Blktrace_setup ||
  355. bt->trace_state == Blktrace_stopped) {
  356. blktrace_seq++;
  357. smp_mb();
  358. bt->trace_state = Blktrace_running;
  359. trace_note_time(bt);
  360. ret = 0;
  361. }
  362. } else {
  363. if (bt->trace_state == Blktrace_running) {
  364. bt->trace_state = Blktrace_stopped;
  365. relay_flush(bt->rchan);
  366. ret = 0;
  367. }
  368. }
  369. return ret;
  370. }
  371. /**
  372. * blk_trace_ioctl: - handle the ioctls associated with tracing
  373. * @bdev: the block device
  374. * @cmd: the ioctl cmd
  375. * @arg: the argument data, if any
  376. *
  377. **/
  378. int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
  379. {
  380. request_queue_t *q;
  381. int ret, start = 0;
  382. q = bdev_get_queue(bdev);
  383. if (!q)
  384. return -ENXIO;
  385. mutex_lock(&bdev->bd_mutex);
  386. switch (cmd) {
  387. case BLKTRACESETUP:
  388. ret = blk_trace_setup(q, bdev, arg);
  389. break;
  390. case BLKTRACESTART:
  391. start = 1;
  392. case BLKTRACESTOP:
  393. ret = blk_trace_startstop(q, start);
  394. break;
  395. case BLKTRACETEARDOWN:
  396. ret = blk_trace_remove(q);
  397. break;
  398. default:
  399. ret = -ENOTTY;
  400. break;
  401. }
  402. mutex_unlock(&bdev->bd_mutex);
  403. return ret;
  404. }
  405. /**
  406. * blk_trace_shutdown: - stop and cleanup trace structures
  407. * @q: the request queue associated with the device
  408. *
  409. **/
  410. void blk_trace_shutdown(request_queue_t *q)
  411. {
  412. if (q->blk_trace) {
  413. blk_trace_startstop(q, 0);
  414. blk_trace_remove(q);
  415. }
  416. }
  417. /*
  418. * Average offset over two calls to sched_clock() with a gettimeofday()
  419. * in the middle
  420. */
  421. static void blk_check_time(unsigned long long *t)
  422. {
  423. unsigned long long a, b;
  424. struct timeval tv;
  425. a = sched_clock();
  426. do_gettimeofday(&tv);
  427. b = sched_clock();
  428. *t = tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
  429. *t -= (a + b) / 2;
  430. }
  431. /*
  432. * calibrate our inter-CPU timings
  433. */
  434. static void blk_trace_check_cpu_time(void *data)
  435. {
  436. unsigned long long *t;
  437. int cpu = get_cpu();
  438. t = &per_cpu(blk_trace_cpu_offset, cpu);
  439. /*
  440. * Just call it twice, hopefully the second call will be cache hot
  441. * and a little more precise
  442. */
  443. blk_check_time(t);
  444. blk_check_time(t);
  445. put_cpu();
  446. }
  447. static void blk_trace_set_ht_offsets(void)
  448. {
  449. #if defined(CONFIG_SCHED_SMT)
  450. int cpu, i;
  451. /*
  452. * now make sure HT siblings have the same time offset
  453. */
  454. preempt_disable();
  455. for_each_online_cpu(cpu) {
  456. unsigned long long *cpu_off, *sibling_off;
  457. for_each_cpu_mask(i, cpu_sibling_map[cpu]) {
  458. if (i == cpu)
  459. continue;
  460. cpu_off = &per_cpu(blk_trace_cpu_offset, cpu);
  461. sibling_off = &per_cpu(blk_trace_cpu_offset, i);
  462. *sibling_off = *cpu_off;
  463. }
  464. }
  465. preempt_enable();
  466. #endif
  467. }
  468. static __init int blk_trace_init(void)
  469. {
  470. mutex_init(&blk_tree_mutex);
  471. on_each_cpu(blk_trace_check_cpu_time, NULL, 1, 1);
  472. blk_trace_set_ht_offsets();
  473. return 0;
  474. }
  475. module_init(blk_trace_init);