2 * Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
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.
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.
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
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/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/uaccess.h>
29 #include <trace/events/block.h>
31 #include "trace_output.h"
33 #ifdef CONFIG_BLK_DEV_IO_TRACE
35 static unsigned int blktrace_seq __read_mostly = 1;
37 static struct trace_array *blk_tr;
38 static bool blk_tracer_enabled __read_mostly;
40 /* Select an alternative, minimalistic output than the original one */
41 #define TRACE_BLK_OPT_CLASSIC 0x1
43 static struct tracer_opt blk_tracer_opts[] = {
44 /* Default disable the minimalistic output */
45 { TRACER_OPT(blk_classic, TRACE_BLK_OPT_CLASSIC) },
49 static struct tracer_flags blk_tracer_flags = {
51 .opts = blk_tracer_opts,
54 /* Global reference count of probes */
55 static atomic_t blk_probes_ref = ATOMIC_INIT(0);
57 static void blk_register_tracepoints(void);
58 static void blk_unregister_tracepoints(void);
61 * Send out a notify message.
63 static void trace_note(struct blk_trace *bt, pid_t pid, int action,
64 const void *data, size_t len)
66 struct blk_io_trace *t;
67 struct ring_buffer_event *event = NULL;
69 int cpu = smp_processor_id();
70 bool blk_tracer = blk_tracer_enabled;
74 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
79 t = ring_buffer_event_data(event);
86 t = relay_reserve(bt->rchan, sizeof(*t) + len);
88 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
89 t->time = ktime_to_ns(ktime_get());
96 memcpy((void *) t + sizeof(*t), data, len);
99 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
104 * Send out a notify for this process, if we haven't done so since a trace
107 static void trace_note_tsk(struct blk_trace *bt, struct task_struct *tsk)
109 tsk->btrace_seq = blktrace_seq;
110 trace_note(bt, tsk->pid, BLK_TN_PROCESS, tsk->comm, sizeof(tsk->comm));
113 static void trace_note_time(struct blk_trace *bt)
119 getnstimeofday(&now);
120 words[0] = now.tv_sec;
121 words[1] = now.tv_nsec;
123 local_irq_save(flags);
124 trace_note(bt, 0, BLK_TN_TIMESTAMP, words, sizeof(words));
125 local_irq_restore(flags);
128 void __trace_note_message(struct blk_trace *bt, const char *fmt, ...)
135 if (unlikely(bt->trace_state != Blktrace_running &&
136 !blk_tracer_enabled))
139 local_irq_save(flags);
140 buf = per_cpu_ptr(bt->msg_data, smp_processor_id());
142 n = vscnprintf(buf, BLK_TN_MAX_MSG, fmt, args);
145 trace_note(bt, 0, BLK_TN_MESSAGE, buf, n);
146 local_irq_restore(flags);
148 EXPORT_SYMBOL_GPL(__trace_note_message);
150 static int act_log_check(struct blk_trace *bt, u32 what, sector_t sector,
153 if (((bt->act_mask << BLK_TC_SHIFT) & what) == 0)
155 if (sector && (sector < bt->start_lba || sector > bt->end_lba))
157 if (bt->pid && pid != bt->pid)
164 * Data direction bit lookup
166 static const u32 ddir_act[2] = { BLK_TC_ACT(BLK_TC_READ),
167 BLK_TC_ACT(BLK_TC_WRITE) };
169 /* The ilog2() calls fall out because they're constant */
170 #define MASK_TC_BIT(rw, __name) ((rw & (1 << BIO_RW_ ## __name)) << \
171 (ilog2(BLK_TC_ ## __name) + BLK_TC_SHIFT - BIO_RW_ ## __name))
174 * The worker for the various blk_add_trace*() types. Fills out a
175 * blk_io_trace structure and places it in a per-cpu subbuffer.
177 static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
178 int rw, u32 what, int error, int pdu_len, void *pdu_data)
180 struct task_struct *tsk = current;
181 struct ring_buffer_event *event = NULL;
182 struct blk_io_trace *t;
183 unsigned long flags = 0;
184 unsigned long *sequence;
187 bool blk_tracer = blk_tracer_enabled;
189 if (unlikely(bt->trace_state != Blktrace_running && !blk_tracer))
192 what |= ddir_act[rw & WRITE];
193 what |= MASK_TC_BIT(rw, BARRIER);
194 what |= MASK_TC_BIT(rw, SYNCIO);
195 what |= MASK_TC_BIT(rw, AHEAD);
196 what |= MASK_TC_BIT(rw, META);
197 what |= MASK_TC_BIT(rw, DISCARD);
200 if (act_log_check(bt, what, sector, pid))
202 cpu = raw_smp_processor_id();
205 tracing_record_cmdline(current);
207 pc = preempt_count();
208 event = trace_buffer_lock_reserve(blk_tr, TRACE_BLK,
209 sizeof(*t) + pdu_len,
213 t = ring_buffer_event_data(event);
218 * A word about the locking here - we disable interrupts to reserve
219 * some space in the relay per-cpu buffer, to prevent an irq
220 * from coming in and stepping on our toes.
222 local_irq_save(flags);
224 if (unlikely(tsk->btrace_seq != blktrace_seq))
225 trace_note_tsk(bt, tsk);
227 t = relay_reserve(bt->rchan, sizeof(*t) + pdu_len);
229 sequence = per_cpu_ptr(bt->sequence, cpu);
231 t->magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION;
232 t->sequence = ++(*sequence);
233 t->time = ktime_to_ns(ktime_get());
236 * These two are not needed in ftrace as they are in the
237 * generic trace_entry, filled by tracing_generic_entry_update,
238 * but for the trace_event->bin() synthesizer benefit we do it
249 t->pdu_len = pdu_len;
252 memcpy((void *) t + sizeof(*t), pdu_data, pdu_len);
255 trace_buffer_unlock_commit(blk_tr, event, 0, pc);
260 local_irq_restore(flags);
263 static struct dentry *blk_tree_root;
264 static DEFINE_MUTEX(blk_tree_mutex);
266 static void blk_trace_free(struct blk_trace *bt)
268 debugfs_remove(bt->msg_file);
269 debugfs_remove(bt->dropped_file);
270 debugfs_remove(bt->dir);
271 relay_close(bt->rchan);
272 free_percpu(bt->sequence);
273 free_percpu(bt->msg_data);
277 static void blk_trace_cleanup(struct blk_trace *bt)
280 if (atomic_dec_and_test(&blk_probes_ref))
281 blk_unregister_tracepoints();
284 int blk_trace_remove(struct request_queue *q)
286 struct blk_trace *bt;
288 bt = xchg(&q->blk_trace, NULL);
292 if (bt->trace_state != Blktrace_running)
293 blk_trace_cleanup(bt);
297 EXPORT_SYMBOL_GPL(blk_trace_remove);
299 static int blk_dropped_open(struct inode *inode, struct file *filp)
301 filp->private_data = inode->i_private;
306 static ssize_t blk_dropped_read(struct file *filp, char __user *buffer,
307 size_t count, loff_t *ppos)
309 struct blk_trace *bt = filp->private_data;
312 snprintf(buf, sizeof(buf), "%u\n", atomic_read(&bt->dropped));
314 return simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
317 static const struct file_operations blk_dropped_fops = {
318 .owner = THIS_MODULE,
319 .open = blk_dropped_open,
320 .read = blk_dropped_read,
323 static int blk_msg_open(struct inode *inode, struct file *filp)
325 filp->private_data = inode->i_private;
330 static ssize_t blk_msg_write(struct file *filp, const char __user *buffer,
331 size_t count, loff_t *ppos)
334 struct blk_trace *bt;
336 if (count >= BLK_TN_MAX_MSG)
339 msg = kmalloc(count + 1, GFP_KERNEL);
343 if (copy_from_user(msg, buffer, count)) {
349 bt = filp->private_data;
350 __trace_note_message(bt, "%s", msg);
356 static const struct file_operations blk_msg_fops = {
357 .owner = THIS_MODULE,
358 .open = blk_msg_open,
359 .write = blk_msg_write,
363 * Keep track of how many times we encountered a full subbuffer, to aid
364 * the user space app in telling how many lost events there were.
366 static int blk_subbuf_start_callback(struct rchan_buf *buf, void *subbuf,
367 void *prev_subbuf, size_t prev_padding)
369 struct blk_trace *bt;
371 if (!relay_buf_full(buf))
374 bt = buf->chan->private_data;
375 atomic_inc(&bt->dropped);
379 static int blk_remove_buf_file_callback(struct dentry *dentry)
381 struct dentry *parent = dentry->d_parent;
382 debugfs_remove(dentry);
385 * this will fail for all but the last file, but that is ok. what we
386 * care about is the top level buts->name directory going away, when
387 * the last trace file is gone. Then we don't have to rmdir() that
388 * manually on trace stop, so it nicely solves the issue with
389 * force killing of running traces.
392 debugfs_remove(parent);
396 static struct dentry *blk_create_buf_file_callback(const char *filename,
397 struct dentry *parent,
399 struct rchan_buf *buf,
402 return debugfs_create_file(filename, mode, parent, buf,
403 &relay_file_operations);
406 static struct rchan_callbacks blk_relay_callbacks = {
407 .subbuf_start = blk_subbuf_start_callback,
408 .create_buf_file = blk_create_buf_file_callback,
409 .remove_buf_file = blk_remove_buf_file_callback,
412 static void blk_trace_setup_lba(struct blk_trace *bt,
413 struct block_device *bdev)
415 struct hd_struct *part = NULL;
418 part = bdev->bd_part;
421 bt->start_lba = part->start_sect;
422 bt->end_lba = part->start_sect + part->nr_sects;
430 * Setup everything required to start tracing
432 int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
433 struct block_device *bdev,
434 struct blk_user_trace_setup *buts)
436 struct blk_trace *old_bt, *bt = NULL;
437 struct dentry *dir = NULL;
440 if (!buts->buf_size || !buts->buf_nr)
443 strncpy(buts->name, name, BLKTRACE_BDEV_SIZE);
444 buts->name[BLKTRACE_BDEV_SIZE - 1] = '\0';
447 * some device names have larger paths - convert the slashes
448 * to underscores for this to work as expected
450 for (i = 0; i < strlen(buts->name); i++)
451 if (buts->name[i] == '/')
454 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
459 bt->sequence = alloc_percpu(unsigned long);
463 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
469 mutex_lock(&blk_tree_mutex);
470 if (!blk_tree_root) {
471 blk_tree_root = debugfs_create_dir("block", NULL);
472 if (!blk_tree_root) {
473 mutex_unlock(&blk_tree_mutex);
477 mutex_unlock(&blk_tree_mutex);
479 dir = debugfs_create_dir(buts->name, blk_tree_root);
486 atomic_set(&bt->dropped, 0);
489 bt->dropped_file = debugfs_create_file("dropped", 0444, dir, bt,
491 if (!bt->dropped_file)
494 bt->msg_file = debugfs_create_file("msg", 0222, dir, bt, &blk_msg_fops);
498 bt->rchan = relay_open("trace", dir, buts->buf_size,
499 buts->buf_nr, &blk_relay_callbacks, bt);
503 bt->act_mask = buts->act_mask;
505 bt->act_mask = (u16) -1;
507 blk_trace_setup_lba(bt, bdev);
509 /* overwrite with user settings */
511 bt->start_lba = buts->start_lba;
513 bt->end_lba = buts->end_lba;
516 bt->trace_state = Blktrace_setup;
519 old_bt = xchg(&q->blk_trace, bt);
521 (void) xchg(&q->blk_trace, old_bt);
525 if (atomic_inc_return(&blk_probes_ref) == 1)
526 blk_register_tracepoints();
534 int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
535 struct block_device *bdev,
538 struct blk_user_trace_setup buts;
541 ret = copy_from_user(&buts, arg, sizeof(buts));
545 ret = do_blk_trace_setup(q, name, dev, bdev, &buts);
549 if (copy_to_user(arg, &buts, sizeof(buts)))
554 EXPORT_SYMBOL_GPL(blk_trace_setup);
556 int blk_trace_startstop(struct request_queue *q, int start)
559 struct blk_trace *bt = q->blk_trace;
565 * For starting a trace, we can transition from a setup or stopped
566 * trace. For stopping a trace, the state must be running
570 if (bt->trace_state == Blktrace_setup ||
571 bt->trace_state == Blktrace_stopped) {
574 bt->trace_state = Blktrace_running;
580 if (bt->trace_state == Blktrace_running) {
581 bt->trace_state = Blktrace_stopped;
582 relay_flush(bt->rchan);
589 EXPORT_SYMBOL_GPL(blk_trace_startstop);
592 * blk_trace_ioctl: - handle the ioctls associated with tracing
593 * @bdev: the block device
594 * @cmd: the ioctl cmd
595 * @arg: the argument data, if any
598 int blk_trace_ioctl(struct block_device *bdev, unsigned cmd, char __user *arg)
600 struct request_queue *q;
602 char b[BDEVNAME_SIZE];
604 q = bdev_get_queue(bdev);
608 mutex_lock(&bdev->bd_mutex);
613 ret = blk_trace_setup(q, b, bdev->bd_dev, bdev, arg);
618 ret = blk_trace_startstop(q, start);
620 case BLKTRACETEARDOWN:
621 ret = blk_trace_remove(q);
628 mutex_unlock(&bdev->bd_mutex);
633 * blk_trace_shutdown: - stop and cleanup trace structures
634 * @q: the request queue associated with the device
637 void blk_trace_shutdown(struct request_queue *q)
640 blk_trace_startstop(q, 0);
650 * blk_add_trace_rq - Add a trace for a request oriented action
651 * @q: queue the io is for
652 * @rq: the source request
656 * Records an action against a request. Will log the bio offset + size.
659 static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
662 struct blk_trace *bt = q->blk_trace;
663 int rw = rq->cmd_flags & 0x03;
668 if (blk_discard_rq(rq))
669 rw |= (1 << BIO_RW_DISCARD);
671 if (blk_pc_request(rq)) {
672 what |= BLK_TC_ACT(BLK_TC_PC);
673 __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
674 what, rq->errors, rq->cmd_len, rq->cmd);
676 what |= BLK_TC_ACT(BLK_TC_FS);
677 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), rw,
678 what, rq->errors, 0, NULL);
682 static void blk_add_trace_rq_abort(struct request_queue *q, struct request *rq)
684 blk_add_trace_rq(q, rq, BLK_TA_ABORT);
687 static void blk_add_trace_rq_insert(struct request_queue *q, struct request *rq)
689 blk_add_trace_rq(q, rq, BLK_TA_INSERT);
692 static void blk_add_trace_rq_issue(struct request_queue *q, struct request *rq)
694 blk_add_trace_rq(q, rq, BLK_TA_ISSUE);
697 static void blk_add_trace_rq_requeue(struct request_queue *q,
700 blk_add_trace_rq(q, rq, BLK_TA_REQUEUE);
703 static void blk_add_trace_rq_complete(struct request_queue *q,
706 blk_add_trace_rq(q, rq, BLK_TA_COMPLETE);
710 * blk_add_trace_bio - Add a trace for a bio oriented action
711 * @q: queue the io is for
712 * @bio: the source bio
716 * Records an action against a bio. Will log the bio offset + size.
719 static void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
722 struct blk_trace *bt = q->blk_trace;
727 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what,
728 !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
731 static void blk_add_trace_bio_bounce(struct request_queue *q, struct bio *bio)
733 blk_add_trace_bio(q, bio, BLK_TA_BOUNCE);
736 static void blk_add_trace_bio_complete(struct request_queue *q, struct bio *bio)
738 blk_add_trace_bio(q, bio, BLK_TA_COMPLETE);
741 static void blk_add_trace_bio_backmerge(struct request_queue *q,
744 blk_add_trace_bio(q, bio, BLK_TA_BACKMERGE);
747 static void blk_add_trace_bio_frontmerge(struct request_queue *q,
750 blk_add_trace_bio(q, bio, BLK_TA_FRONTMERGE);
753 static void blk_add_trace_bio_queue(struct request_queue *q, struct bio *bio)
755 blk_add_trace_bio(q, bio, BLK_TA_QUEUE);
758 static void blk_add_trace_getrq(struct request_queue *q,
759 struct bio *bio, int rw)
762 blk_add_trace_bio(q, bio, BLK_TA_GETRQ);
764 struct blk_trace *bt = q->blk_trace;
767 __blk_add_trace(bt, 0, 0, rw, BLK_TA_GETRQ, 0, 0, NULL);
772 static void blk_add_trace_sleeprq(struct request_queue *q,
773 struct bio *bio, int rw)
776 blk_add_trace_bio(q, bio, BLK_TA_SLEEPRQ);
778 struct blk_trace *bt = q->blk_trace;
781 __blk_add_trace(bt, 0, 0, rw, BLK_TA_SLEEPRQ,
786 static void blk_add_trace_plug(struct request_queue *q)
788 struct blk_trace *bt = q->blk_trace;
791 __blk_add_trace(bt, 0, 0, 0, BLK_TA_PLUG, 0, 0, NULL);
794 static void blk_add_trace_unplug_io(struct request_queue *q)
796 struct blk_trace *bt = q->blk_trace;
799 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
800 __be64 rpdu = cpu_to_be64(pdu);
802 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_IO, 0,
803 sizeof(rpdu), &rpdu);
807 static void blk_add_trace_unplug_timer(struct request_queue *q)
809 struct blk_trace *bt = q->blk_trace;
812 unsigned int pdu = q->rq.count[READ] + q->rq.count[WRITE];
813 __be64 rpdu = cpu_to_be64(pdu);
815 __blk_add_trace(bt, 0, 0, 0, BLK_TA_UNPLUG_TIMER, 0,
816 sizeof(rpdu), &rpdu);
820 static void blk_add_trace_split(struct request_queue *q, struct bio *bio,
823 struct blk_trace *bt = q->blk_trace;
826 __be64 rpdu = cpu_to_be64(pdu);
828 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
829 BLK_TA_SPLIT, !bio_flagged(bio, BIO_UPTODATE),
830 sizeof(rpdu), &rpdu);
835 * blk_add_trace_remap - Add a trace for a remap operation
836 * @q: queue the io is for
837 * @bio: the source bio
838 * @dev: target device
839 * @from: source sector
842 * Device mapper or raid target sometimes need to split a bio because
843 * it spans a stripe (or similar). Add a trace for that action.
846 static void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
847 dev_t dev, sector_t from)
849 struct blk_trace *bt = q->blk_trace;
850 struct blk_io_trace_remap r;
855 r.device_from = cpu_to_be32(dev);
856 r.device_to = cpu_to_be32(bio->bi_bdev->bd_dev);
857 r.sector_from = cpu_to_be64(from);
859 __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw,
860 BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE),
865 * blk_add_driver_data - Add binary message with driver-specific data
866 * @q: queue the io is for
868 * @data: driver-specific data
869 * @len: length of driver-specific data
872 * Some drivers might want to write driver-specific data per request.
875 void blk_add_driver_data(struct request_queue *q,
877 void *data, size_t len)
879 struct blk_trace *bt = q->blk_trace;
884 if (blk_pc_request(rq))
885 __blk_add_trace(bt, 0, blk_rq_bytes(rq), 0,
886 BLK_TA_DRV_DATA, rq->errors, len, data);
888 __blk_add_trace(bt, blk_rq_pos(rq), blk_rq_bytes(rq), 0,
889 BLK_TA_DRV_DATA, rq->errors, len, data);
891 EXPORT_SYMBOL_GPL(blk_add_driver_data);
893 static void blk_register_tracepoints(void)
897 ret = register_trace_block_rq_abort(blk_add_trace_rq_abort);
899 ret = register_trace_block_rq_insert(blk_add_trace_rq_insert);
901 ret = register_trace_block_rq_issue(blk_add_trace_rq_issue);
903 ret = register_trace_block_rq_requeue(blk_add_trace_rq_requeue);
905 ret = register_trace_block_rq_complete(blk_add_trace_rq_complete);
907 ret = register_trace_block_bio_bounce(blk_add_trace_bio_bounce);
909 ret = register_trace_block_bio_complete(blk_add_trace_bio_complete);
911 ret = register_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
913 ret = register_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
915 ret = register_trace_block_bio_queue(blk_add_trace_bio_queue);
917 ret = register_trace_block_getrq(blk_add_trace_getrq);
919 ret = register_trace_block_sleeprq(blk_add_trace_sleeprq);
921 ret = register_trace_block_plug(blk_add_trace_plug);
923 ret = register_trace_block_unplug_timer(blk_add_trace_unplug_timer);
925 ret = register_trace_block_unplug_io(blk_add_trace_unplug_io);
927 ret = register_trace_block_split(blk_add_trace_split);
929 ret = register_trace_block_remap(blk_add_trace_remap);
933 static void blk_unregister_tracepoints(void)
935 unregister_trace_block_remap(blk_add_trace_remap);
936 unregister_trace_block_split(blk_add_trace_split);
937 unregister_trace_block_unplug_io(blk_add_trace_unplug_io);
938 unregister_trace_block_unplug_timer(blk_add_trace_unplug_timer);
939 unregister_trace_block_plug(blk_add_trace_plug);
940 unregister_trace_block_sleeprq(blk_add_trace_sleeprq);
941 unregister_trace_block_getrq(blk_add_trace_getrq);
942 unregister_trace_block_bio_queue(blk_add_trace_bio_queue);
943 unregister_trace_block_bio_frontmerge(blk_add_trace_bio_frontmerge);
944 unregister_trace_block_bio_backmerge(blk_add_trace_bio_backmerge);
945 unregister_trace_block_bio_complete(blk_add_trace_bio_complete);
946 unregister_trace_block_bio_bounce(blk_add_trace_bio_bounce);
947 unregister_trace_block_rq_complete(blk_add_trace_rq_complete);
948 unregister_trace_block_rq_requeue(blk_add_trace_rq_requeue);
949 unregister_trace_block_rq_issue(blk_add_trace_rq_issue);
950 unregister_trace_block_rq_insert(blk_add_trace_rq_insert);
951 unregister_trace_block_rq_abort(blk_add_trace_rq_abort);
953 tracepoint_synchronize_unregister();
957 * struct blk_io_tracer formatting routines
960 static void fill_rwbs(char *rwbs, const struct blk_io_trace *t)
963 int tc = t->action >> BLK_TC_SHIFT;
965 if (t->action == BLK_TN_MESSAGE) {
970 if (tc & BLK_TC_DISCARD)
972 else if (tc & BLK_TC_WRITE)
979 if (tc & BLK_TC_AHEAD)
981 if (tc & BLK_TC_BARRIER)
983 if (tc & BLK_TC_SYNC)
985 if (tc & BLK_TC_META)
992 const struct blk_io_trace *te_blk_io_trace(const struct trace_entry *ent)
994 return (const struct blk_io_trace *)ent;
997 static inline const void *pdu_start(const struct trace_entry *ent)
999 return te_blk_io_trace(ent) + 1;
1002 static inline u32 t_action(const struct trace_entry *ent)
1004 return te_blk_io_trace(ent)->action;
1007 static inline u32 t_bytes(const struct trace_entry *ent)
1009 return te_blk_io_trace(ent)->bytes;
1012 static inline u32 t_sec(const struct trace_entry *ent)
1014 return te_blk_io_trace(ent)->bytes >> 9;
1017 static inline unsigned long long t_sector(const struct trace_entry *ent)
1019 return te_blk_io_trace(ent)->sector;
1022 static inline __u16 t_error(const struct trace_entry *ent)
1024 return te_blk_io_trace(ent)->error;
1027 static __u64 get_pdu_int(const struct trace_entry *ent)
1029 const __u64 *val = pdu_start(ent);
1030 return be64_to_cpu(*val);
1033 static void get_pdu_remap(const struct trace_entry *ent,
1034 struct blk_io_trace_remap *r)
1036 const struct blk_io_trace_remap *__r = pdu_start(ent);
1037 __u64 sector_from = __r->sector_from;
1039 r->device_from = be32_to_cpu(__r->device_from);
1040 r->device_to = be32_to_cpu(__r->device_to);
1041 r->sector_from = be64_to_cpu(sector_from);
1044 typedef int (blk_log_action_t) (struct trace_iterator *iter, const char *act);
1046 static int blk_log_action_classic(struct trace_iterator *iter, const char *act)
1049 unsigned long long ts = iter->ts;
1050 unsigned long nsec_rem = do_div(ts, NSEC_PER_SEC);
1051 unsigned secs = (unsigned long)ts;
1052 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1056 return trace_seq_printf(&iter->seq,
1057 "%3d,%-3d %2d %5d.%09lu %5u %2s %3s ",
1058 MAJOR(t->device), MINOR(t->device), iter->cpu,
1059 secs, nsec_rem, iter->ent->pid, act, rwbs);
1062 static int blk_log_action(struct trace_iterator *iter, const char *act)
1065 const struct blk_io_trace *t = te_blk_io_trace(iter->ent);
1068 return trace_seq_printf(&iter->seq, "%3d,%-3d %2s %3s ",
1069 MAJOR(t->device), MINOR(t->device), act, rwbs);
1072 static int blk_log_dump_pdu(struct trace_seq *s, const struct trace_entry *ent)
1074 const unsigned char *pdu_buf;
1078 pdu_buf = pdu_start(ent);
1079 pdu_len = te_blk_io_trace(ent)->pdu_len;
1084 /* find the last zero that needs to be printed */
1085 for (end = pdu_len - 1; end >= 0; end--)
1090 if (!trace_seq_putc(s, '('))
1093 for (i = 0; i < pdu_len; i++) {
1095 ret = trace_seq_printf(s, "%s%02x",
1096 i == 0 ? "" : " ", pdu_buf[i]);
1101 * stop when the rest is just zeroes and indicate so
1102 * with a ".." appended
1104 if (i == end && end != pdu_len - 1)
1105 return trace_seq_puts(s, " ..) ");
1108 return trace_seq_puts(s, ") ");
1111 static int blk_log_generic(struct trace_seq *s, const struct trace_entry *ent)
1113 char cmd[TASK_COMM_LEN];
1115 trace_find_cmdline(ent->pid, cmd);
1117 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1120 ret = trace_seq_printf(s, "%u ", t_bytes(ent));
1123 ret = blk_log_dump_pdu(s, ent);
1126 return trace_seq_printf(s, "[%s]\n", cmd);
1129 return trace_seq_printf(s, "%llu + %u [%s]\n",
1130 t_sector(ent), t_sec(ent), cmd);
1131 return trace_seq_printf(s, "[%s]\n", cmd);
1135 static int blk_log_with_error(struct trace_seq *s,
1136 const struct trace_entry *ent)
1138 if (t_action(ent) & BLK_TC_ACT(BLK_TC_PC)) {
1141 ret = blk_log_dump_pdu(s, ent);
1143 return trace_seq_printf(s, "[%d]\n", t_error(ent));
1147 return trace_seq_printf(s, "%llu + %u [%d]\n",
1149 t_sec(ent), t_error(ent));
1150 return trace_seq_printf(s, "%llu [%d]\n",
1151 t_sector(ent), t_error(ent));
1155 static int blk_log_remap(struct trace_seq *s, const struct trace_entry *ent)
1157 struct blk_io_trace_remap r = { .device_from = 0, };
1159 get_pdu_remap(ent, &r);
1160 return trace_seq_printf(s, "%llu + %u <- (%d,%d) %llu\n",
1161 t_sector(ent), t_sec(ent),
1162 MAJOR(r.device_from), MINOR(r.device_from),
1163 (unsigned long long)r.sector_from);
1166 static int blk_log_plug(struct trace_seq *s, const struct trace_entry *ent)
1168 char cmd[TASK_COMM_LEN];
1170 trace_find_cmdline(ent->pid, cmd);
1172 return trace_seq_printf(s, "[%s]\n", cmd);
1175 static int blk_log_unplug(struct trace_seq *s, const struct trace_entry *ent)
1177 char cmd[TASK_COMM_LEN];
1179 trace_find_cmdline(ent->pid, cmd);
1181 return trace_seq_printf(s, "[%s] %llu\n", cmd, get_pdu_int(ent));
1184 static int blk_log_split(struct trace_seq *s, const struct trace_entry *ent)
1186 char cmd[TASK_COMM_LEN];
1188 trace_find_cmdline(ent->pid, cmd);
1190 return trace_seq_printf(s, "%llu / %llu [%s]\n", t_sector(ent),
1191 get_pdu_int(ent), cmd);
1194 static int blk_log_msg(struct trace_seq *s, const struct trace_entry *ent)
1197 const struct blk_io_trace *t = te_blk_io_trace(ent);
1199 ret = trace_seq_putmem(s, t + 1, t->pdu_len);
1201 return trace_seq_putc(s, '\n');
1206 * struct tracer operations
1209 static void blk_tracer_print_header(struct seq_file *m)
1211 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1213 seq_puts(m, "# DEV CPU TIMESTAMP PID ACT FLG\n"
1217 static void blk_tracer_start(struct trace_array *tr)
1219 blk_tracer_enabled = true;
1222 static int blk_tracer_init(struct trace_array *tr)
1225 blk_tracer_start(tr);
1229 static void blk_tracer_stop(struct trace_array *tr)
1231 blk_tracer_enabled = false;
1234 static void blk_tracer_reset(struct trace_array *tr)
1236 blk_tracer_stop(tr);
1239 static const struct {
1241 int (*print)(struct trace_seq *s, const struct trace_entry *ent);
1243 [__BLK_TA_QUEUE] = {{ "Q", "queue" }, blk_log_generic },
1244 [__BLK_TA_BACKMERGE] = {{ "M", "backmerge" }, blk_log_generic },
1245 [__BLK_TA_FRONTMERGE] = {{ "F", "frontmerge" }, blk_log_generic },
1246 [__BLK_TA_GETRQ] = {{ "G", "getrq" }, blk_log_generic },
1247 [__BLK_TA_SLEEPRQ] = {{ "S", "sleeprq" }, blk_log_generic },
1248 [__BLK_TA_REQUEUE] = {{ "R", "requeue" }, blk_log_with_error },
1249 [__BLK_TA_ISSUE] = {{ "D", "issue" }, blk_log_generic },
1250 [__BLK_TA_COMPLETE] = {{ "C", "complete" }, blk_log_with_error },
1251 [__BLK_TA_PLUG] = {{ "P", "plug" }, blk_log_plug },
1252 [__BLK_TA_UNPLUG_IO] = {{ "U", "unplug_io" }, blk_log_unplug },
1253 [__BLK_TA_UNPLUG_TIMER] = {{ "UT", "unplug_timer" }, blk_log_unplug },
1254 [__BLK_TA_INSERT] = {{ "I", "insert" }, blk_log_generic },
1255 [__BLK_TA_SPLIT] = {{ "X", "split" }, blk_log_split },
1256 [__BLK_TA_BOUNCE] = {{ "B", "bounce" }, blk_log_generic },
1257 [__BLK_TA_REMAP] = {{ "A", "remap" }, blk_log_remap },
1260 static enum print_line_t print_one_line(struct trace_iterator *iter,
1263 struct trace_seq *s = &iter->seq;
1264 const struct blk_io_trace *t;
1268 blk_log_action_t *log_action;
1270 t = te_blk_io_trace(iter->ent);
1271 what = t->action & ((1 << BLK_TC_SHIFT) - 1);
1272 long_act = !!(trace_flags & TRACE_ITER_VERBOSE);
1273 log_action = classic ? &blk_log_action_classic : &blk_log_action;
1275 if (t->action == BLK_TN_MESSAGE) {
1276 ret = log_action(iter, long_act ? "message" : "m");
1278 ret = blk_log_msg(s, iter->ent);
1282 if (unlikely(what == 0 || what >= ARRAY_SIZE(what2act)))
1283 ret = trace_seq_printf(s, "Unknown action %x\n", what);
1285 ret = log_action(iter, what2act[what].act[long_act]);
1287 ret = what2act[what].print(s, iter->ent);
1290 return ret ? TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1293 static enum print_line_t blk_trace_event_print(struct trace_iterator *iter,
1296 return print_one_line(iter, false);
1299 static int blk_trace_synthesize_old_trace(struct trace_iterator *iter)
1301 struct trace_seq *s = &iter->seq;
1302 struct blk_io_trace *t = (struct blk_io_trace *)iter->ent;
1303 const int offset = offsetof(struct blk_io_trace, sector);
1304 struct blk_io_trace old = {
1305 .magic = BLK_IO_TRACE_MAGIC | BLK_IO_TRACE_VERSION,
1309 if (!trace_seq_putmem(s, &old, offset))
1311 return trace_seq_putmem(s, &t->sector,
1312 sizeof(old) - offset + t->pdu_len);
1315 static enum print_line_t
1316 blk_trace_event_print_binary(struct trace_iterator *iter, int flags)
1318 return blk_trace_synthesize_old_trace(iter) ?
1319 TRACE_TYPE_HANDLED : TRACE_TYPE_PARTIAL_LINE;
1322 static enum print_line_t blk_tracer_print_line(struct trace_iterator *iter)
1324 if (!(blk_tracer_flags.val & TRACE_BLK_OPT_CLASSIC))
1325 return TRACE_TYPE_UNHANDLED;
1327 return print_one_line(iter, true);
1330 static int blk_tracer_set_flag(u32 old_flags, u32 bit, int set)
1332 /* don't output context-info for blk_classic output */
1333 if (bit == TRACE_BLK_OPT_CLASSIC) {
1335 trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
1337 trace_flags |= TRACE_ITER_CONTEXT_INFO;
1342 static struct tracer blk_tracer __read_mostly = {
1344 .init = blk_tracer_init,
1345 .reset = blk_tracer_reset,
1346 .start = blk_tracer_start,
1347 .stop = blk_tracer_stop,
1348 .print_header = blk_tracer_print_header,
1349 .print_line = blk_tracer_print_line,
1350 .flags = &blk_tracer_flags,
1351 .set_flag = blk_tracer_set_flag,
1354 static struct trace_event trace_blk_event = {
1356 .trace = blk_trace_event_print,
1357 .binary = blk_trace_event_print_binary,
1360 static int __init init_blk_tracer(void)
1362 if (!register_ftrace_event(&trace_blk_event)) {
1363 pr_warning("Warning: could not register block events\n");
1367 if (register_tracer(&blk_tracer) != 0) {
1368 pr_warning("Warning: could not register the block tracer\n");
1369 unregister_ftrace_event(&trace_blk_event);
1376 device_initcall(init_blk_tracer);
1378 static int blk_trace_remove_queue(struct request_queue *q)
1380 struct blk_trace *bt;
1382 bt = xchg(&q->blk_trace, NULL);
1386 if (atomic_dec_and_test(&blk_probes_ref))
1387 blk_unregister_tracepoints();
1394 * Setup everything required to start tracing
1396 static int blk_trace_setup_queue(struct request_queue *q,
1397 struct block_device *bdev)
1399 struct blk_trace *old_bt, *bt = NULL;
1402 bt = kzalloc(sizeof(*bt), GFP_KERNEL);
1406 bt->msg_data = __alloc_percpu(BLK_TN_MAX_MSG, __alignof__(char));
1410 bt->dev = bdev->bd_dev;
1411 bt->act_mask = (u16)-1;
1413 blk_trace_setup_lba(bt, bdev);
1415 old_bt = xchg(&q->blk_trace, bt);
1416 if (old_bt != NULL) {
1417 (void)xchg(&q->blk_trace, old_bt);
1422 if (atomic_inc_return(&blk_probes_ref) == 1)
1423 blk_register_tracepoints();
1432 * sysfs interface to enable and configure tracing
1435 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1436 struct device_attribute *attr,
1438 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1439 struct device_attribute *attr,
1440 const char *buf, size_t count);
1441 #define BLK_TRACE_DEVICE_ATTR(_name) \
1442 DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, \
1443 sysfs_blk_trace_attr_show, \
1444 sysfs_blk_trace_attr_store)
1446 static BLK_TRACE_DEVICE_ATTR(enable);
1447 static BLK_TRACE_DEVICE_ATTR(act_mask);
1448 static BLK_TRACE_DEVICE_ATTR(pid);
1449 static BLK_TRACE_DEVICE_ATTR(start_lba);
1450 static BLK_TRACE_DEVICE_ATTR(end_lba);
1452 static struct attribute *blk_trace_attrs[] = {
1453 &dev_attr_enable.attr,
1454 &dev_attr_act_mask.attr,
1456 &dev_attr_start_lba.attr,
1457 &dev_attr_end_lba.attr,
1461 struct attribute_group blk_trace_attr_group = {
1463 .attrs = blk_trace_attrs,
1466 static const struct {
1470 { BLK_TC_READ, "read" },
1471 { BLK_TC_WRITE, "write" },
1472 { BLK_TC_BARRIER, "barrier" },
1473 { BLK_TC_SYNC, "sync" },
1474 { BLK_TC_QUEUE, "queue" },
1475 { BLK_TC_REQUEUE, "requeue" },
1476 { BLK_TC_ISSUE, "issue" },
1477 { BLK_TC_COMPLETE, "complete" },
1478 { BLK_TC_FS, "fs" },
1479 { BLK_TC_PC, "pc" },
1480 { BLK_TC_AHEAD, "ahead" },
1481 { BLK_TC_META, "meta" },
1482 { BLK_TC_DISCARD, "discard" },
1483 { BLK_TC_DRV_DATA, "drv_data" },
1486 static int blk_trace_str2mask(const char *str)
1490 char *buf, *s, *token;
1492 buf = kstrdup(str, GFP_KERNEL);
1498 token = strsep(&s, ",");
1505 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1506 if (strcasecmp(token, mask_maps[i].str) == 0) {
1507 mask |= mask_maps[i].mask;
1511 if (i == ARRAY_SIZE(mask_maps)) {
1521 static ssize_t blk_trace_mask2str(char *buf, int mask)
1526 for (i = 0; i < ARRAY_SIZE(mask_maps); i++) {
1527 if (mask & mask_maps[i].mask) {
1528 p += sprintf(p, "%s%s",
1529 (p == buf) ? "" : ",", mask_maps[i].str);
1537 static struct request_queue *blk_trace_get_queue(struct block_device *bdev)
1539 if (bdev->bd_disk == NULL)
1542 return bdev_get_queue(bdev);
1545 static ssize_t sysfs_blk_trace_attr_show(struct device *dev,
1546 struct device_attribute *attr,
1549 struct hd_struct *p = dev_to_part(dev);
1550 struct request_queue *q;
1551 struct block_device *bdev;
1552 ssize_t ret = -ENXIO;
1555 bdev = bdget(part_devt(p));
1557 goto out_unlock_kernel;
1559 q = blk_trace_get_queue(bdev);
1563 mutex_lock(&bdev->bd_mutex);
1565 if (attr == &dev_attr_enable) {
1566 ret = sprintf(buf, "%u\n", !!q->blk_trace);
1567 goto out_unlock_bdev;
1570 if (q->blk_trace == NULL)
1571 ret = sprintf(buf, "disabled\n");
1572 else if (attr == &dev_attr_act_mask)
1573 ret = blk_trace_mask2str(buf, q->blk_trace->act_mask);
1574 else if (attr == &dev_attr_pid)
1575 ret = sprintf(buf, "%u\n", q->blk_trace->pid);
1576 else if (attr == &dev_attr_start_lba)
1577 ret = sprintf(buf, "%llu\n", q->blk_trace->start_lba);
1578 else if (attr == &dev_attr_end_lba)
1579 ret = sprintf(buf, "%llu\n", q->blk_trace->end_lba);
1582 mutex_unlock(&bdev->bd_mutex);
1590 static ssize_t sysfs_blk_trace_attr_store(struct device *dev,
1591 struct device_attribute *attr,
1592 const char *buf, size_t count)
1594 struct block_device *bdev;
1595 struct request_queue *q;
1596 struct hd_struct *p;
1598 ssize_t ret = -EINVAL;
1603 if (attr == &dev_attr_act_mask) {
1604 if (sscanf(buf, "%llx", &value) != 1) {
1605 /* Assume it is a list of trace category names */
1606 ret = blk_trace_str2mask(buf);
1611 } else if (sscanf(buf, "%llu", &value) != 1)
1617 p = dev_to_part(dev);
1618 bdev = bdget(part_devt(p));
1620 goto out_unlock_kernel;
1622 q = blk_trace_get_queue(bdev);
1626 mutex_lock(&bdev->bd_mutex);
1628 if (attr == &dev_attr_enable) {
1630 ret = blk_trace_setup_queue(q, bdev);
1632 ret = blk_trace_remove_queue(q);
1633 goto out_unlock_bdev;
1637 if (q->blk_trace == NULL)
1638 ret = blk_trace_setup_queue(q, bdev);
1641 if (attr == &dev_attr_act_mask)
1642 q->blk_trace->act_mask = value;
1643 else if (attr == &dev_attr_pid)
1644 q->blk_trace->pid = value;
1645 else if (attr == &dev_attr_start_lba)
1646 q->blk_trace->start_lba = value;
1647 else if (attr == &dev_attr_end_lba)
1648 q->blk_trace->end_lba = value;
1652 mutex_unlock(&bdev->bd_mutex);
1658 return ret ? ret : count;
1661 int blk_trace_init_sysfs(struct device *dev)
1663 return sysfs_create_group(&dev->kobj, &blk_trace_attr_group);
1666 #endif /* CONFIG_BLK_DEV_IO_TRACE */
1668 #ifdef CONFIG_EVENT_TRACING
1670 void blk_dump_cmd(char *buf, struct request *rq)
1673 int len = rq->cmd_len;
1674 unsigned char *cmd = rq->cmd;
1676 if (!blk_pc_request(rq)) {
1681 for (end = len - 1; end >= 0; end--)
1686 for (i = 0; i < len; i++) {
1687 buf += sprintf(buf, "%s%02x", i == 0 ? "" : " ", cmd[i]);
1688 if (i == end && end != len - 1) {
1689 sprintf(buf, " ..");
1695 void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
1701 else if (rw & 1 << BIO_RW_DISCARD)
1708 if (rw & 1 << BIO_RW_AHEAD)
1710 if (rw & 1 << BIO_RW_BARRIER)
1712 if (rw & 1 << BIO_RW_SYNCIO)
1714 if (rw & 1 << BIO_RW_META)
1720 void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
1722 int rw = rq->cmd_flags & 0x03;
1725 if (blk_discard_rq(rq))
1726 rw |= (1 << BIO_RW_DISCARD);
1728 bytes = blk_rq_bytes(rq);
1730 blk_fill_rwbs(rwbs, rw, bytes);
1733 #endif /* CONFIG_EVENT_TRACING */