blktrace_api.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #ifndef BLKTRACE_H
  2. #define BLKTRACE_H
  3. #include <linux/types.h>
  4. #ifdef __KERNEL__
  5. #include <linux/blkdev.h>
  6. #include <linux/relay.h>
  7. #include <linux/compat.h>
  8. #endif
  9. /*
  10. * Trace categories
  11. */
  12. enum blktrace_cat {
  13. BLK_TC_READ = 1 << 0, /* reads */
  14. BLK_TC_WRITE = 1 << 1, /* writes */
  15. BLK_TC_FLUSH = 1 << 2, /* flush */
  16. BLK_TC_SYNC = 1 << 3, /* sync IO */
  17. BLK_TC_SYNCIO = BLK_TC_SYNC,
  18. BLK_TC_QUEUE = 1 << 4, /* queueing/merging */
  19. BLK_TC_REQUEUE = 1 << 5, /* requeueing */
  20. BLK_TC_ISSUE = 1 << 6, /* issue */
  21. BLK_TC_COMPLETE = 1 << 7, /* completions */
  22. BLK_TC_FS = 1 << 8, /* fs requests */
  23. BLK_TC_PC = 1 << 9, /* pc requests */
  24. BLK_TC_NOTIFY = 1 << 10, /* special message */
  25. BLK_TC_AHEAD = 1 << 11, /* readahead */
  26. BLK_TC_META = 1 << 12, /* metadata */
  27. BLK_TC_DISCARD = 1 << 13, /* discard requests */
  28. BLK_TC_DRV_DATA = 1 << 14, /* binary per-driver data */
  29. BLK_TC_FUA = 1 << 15, /* fua requests */
  30. BLK_TC_END = 1 << 15, /* we've run out of bits! */
  31. };
  32. #define BLK_TC_SHIFT (16)
  33. #define BLK_TC_ACT(act) ((act) << BLK_TC_SHIFT)
  34. /*
  35. * Basic trace actions
  36. */
  37. enum blktrace_act {
  38. __BLK_TA_QUEUE = 1, /* queued */
  39. __BLK_TA_BACKMERGE, /* back merged to existing rq */
  40. __BLK_TA_FRONTMERGE, /* front merge to existing rq */
  41. __BLK_TA_GETRQ, /* allocated new request */
  42. __BLK_TA_SLEEPRQ, /* sleeping on rq allocation */
  43. __BLK_TA_REQUEUE, /* request requeued */
  44. __BLK_TA_ISSUE, /* sent to driver */
  45. __BLK_TA_COMPLETE, /* completed by driver */
  46. __BLK_TA_PLUG, /* queue was plugged */
  47. __BLK_TA_UNPLUG_IO, /* queue was unplugged by io */
  48. __BLK_TA_UNPLUG_TIMER, /* queue was unplugged by timer */
  49. __BLK_TA_INSERT, /* insert request */
  50. __BLK_TA_SPLIT, /* bio was split */
  51. __BLK_TA_BOUNCE, /* bio was bounced */
  52. __BLK_TA_REMAP, /* bio was remapped */
  53. __BLK_TA_ABORT, /* request aborted */
  54. __BLK_TA_DRV_DATA, /* driver-specific binary data */
  55. };
  56. /*
  57. * Notify events.
  58. */
  59. enum blktrace_notify {
  60. __BLK_TN_PROCESS = 0, /* establish pid/name mapping */
  61. __BLK_TN_TIMESTAMP, /* include system clock */
  62. __BLK_TN_MESSAGE, /* Character string message */
  63. };
  64. /*
  65. * Trace actions in full. Additionally, read or write is masked
  66. */
  67. #define BLK_TA_QUEUE (__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE))
  68. #define BLK_TA_BACKMERGE (__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
  69. #define BLK_TA_FRONTMERGE (__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
  70. #define BLK_TA_GETRQ (__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE))
  71. #define BLK_TA_SLEEPRQ (__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE))
  72. #define BLK_TA_REQUEUE (__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE))
  73. #define BLK_TA_ISSUE (__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE))
  74. #define BLK_TA_COMPLETE (__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE))
  75. #define BLK_TA_PLUG (__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE))
  76. #define BLK_TA_UNPLUG_IO (__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE))
  77. #define BLK_TA_UNPLUG_TIMER (__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE))
  78. #define BLK_TA_INSERT (__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE))
  79. #define BLK_TA_SPLIT (__BLK_TA_SPLIT)
  80. #define BLK_TA_BOUNCE (__BLK_TA_BOUNCE)
  81. #define BLK_TA_REMAP (__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE))
  82. #define BLK_TA_ABORT (__BLK_TA_ABORT | BLK_TC_ACT(BLK_TC_QUEUE))
  83. #define BLK_TA_DRV_DATA (__BLK_TA_DRV_DATA | BLK_TC_ACT(BLK_TC_DRV_DATA))
  84. #define BLK_TN_PROCESS (__BLK_TN_PROCESS | BLK_TC_ACT(BLK_TC_NOTIFY))
  85. #define BLK_TN_TIMESTAMP (__BLK_TN_TIMESTAMP | BLK_TC_ACT(BLK_TC_NOTIFY))
  86. #define BLK_TN_MESSAGE (__BLK_TN_MESSAGE | BLK_TC_ACT(BLK_TC_NOTIFY))
  87. #define BLK_IO_TRACE_MAGIC 0x65617400
  88. #define BLK_IO_TRACE_VERSION 0x07
  89. /*
  90. * The trace itself
  91. */
  92. struct blk_io_trace {
  93. __u32 magic; /* MAGIC << 8 | version */
  94. __u32 sequence; /* event number */
  95. __u64 time; /* in microseconds */
  96. __u64 sector; /* disk offset */
  97. __u32 bytes; /* transfer length */
  98. __u32 action; /* what happened */
  99. __u32 pid; /* who did it */
  100. __u32 device; /* device number */
  101. __u32 cpu; /* on what cpu did it happen */
  102. __u16 error; /* completion error */
  103. __u16 pdu_len; /* length of data after this trace */
  104. };
  105. /*
  106. * The remap event
  107. */
  108. struct blk_io_trace_remap {
  109. __be32 device_from;
  110. __be32 device_to;
  111. __be64 sector_from;
  112. };
  113. enum {
  114. Blktrace_setup = 1,
  115. Blktrace_running,
  116. Blktrace_stopped,
  117. };
  118. #define BLKTRACE_BDEV_SIZE 32
  119. /*
  120. * User setup structure passed with BLKTRACESTART
  121. */
  122. struct blk_user_trace_setup {
  123. char name[BLKTRACE_BDEV_SIZE]; /* output */
  124. __u16 act_mask; /* input */
  125. __u32 buf_size; /* input */
  126. __u32 buf_nr; /* input */
  127. __u64 start_lba;
  128. __u64 end_lba;
  129. __u32 pid;
  130. };
  131. #ifdef __KERNEL__
  132. #if defined(CONFIG_BLK_DEV_IO_TRACE)
  133. #include <linux/sysfs.h>
  134. struct blk_trace {
  135. int trace_state;
  136. struct rchan *rchan;
  137. unsigned long __percpu *sequence;
  138. unsigned char __percpu *msg_data;
  139. u16 act_mask;
  140. u64 start_lba;
  141. u64 end_lba;
  142. u32 pid;
  143. u32 dev;
  144. struct dentry *dir;
  145. struct dentry *dropped_file;
  146. struct dentry *msg_file;
  147. atomic_t dropped;
  148. };
  149. extern int blk_trace_ioctl(struct block_device *, unsigned, char __user *);
  150. extern void blk_trace_shutdown(struct request_queue *);
  151. extern int do_blk_trace_setup(struct request_queue *q, char *name,
  152. dev_t dev, struct block_device *bdev,
  153. struct blk_user_trace_setup *buts);
  154. extern __printf(2, 3)
  155. void __trace_note_message(struct blk_trace *, const char *fmt, ...);
  156. /**
  157. * blk_add_trace_msg - Add a (simple) message to the blktrace stream
  158. * @q: queue the io is for
  159. * @fmt: format to print message in
  160. * args... Variable argument list for format
  161. *
  162. * Description:
  163. * Records a (simple) message onto the blktrace stream.
  164. *
  165. * NOTE: BLK_TN_MAX_MSG characters are output at most.
  166. * NOTE: Can not use 'static inline' due to presence of var args...
  167. *
  168. **/
  169. #define blk_add_trace_msg(q, fmt, ...) \
  170. do { \
  171. struct blk_trace *bt = (q)->blk_trace; \
  172. if (unlikely(bt)) \
  173. __trace_note_message(bt, fmt, ##__VA_ARGS__); \
  174. } while (0)
  175. #define BLK_TN_MAX_MSG 128
  176. extern void blk_add_driver_data(struct request_queue *q, struct request *rq,
  177. void *data, size_t len);
  178. extern int blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
  179. struct block_device *bdev,
  180. char __user *arg);
  181. extern int blk_trace_startstop(struct request_queue *q, int start);
  182. extern int blk_trace_remove(struct request_queue *q);
  183. extern void blk_trace_remove_sysfs(struct device *dev);
  184. extern int blk_trace_init_sysfs(struct device *dev);
  185. extern struct attribute_group blk_trace_attr_group;
  186. #else /* !CONFIG_BLK_DEV_IO_TRACE */
  187. # define blk_trace_ioctl(bdev, cmd, arg) (-ENOTTY)
  188. # define blk_trace_shutdown(q) do { } while (0)
  189. # define do_blk_trace_setup(q, name, dev, bdev, buts) (-ENOTTY)
  190. # define blk_add_driver_data(q, rq, data, len) do {} while (0)
  191. # define blk_trace_setup(q, name, dev, bdev, arg) (-ENOTTY)
  192. # define blk_trace_startstop(q, start) (-ENOTTY)
  193. # define blk_trace_remove(q) (-ENOTTY)
  194. # define blk_add_trace_msg(q, fmt, ...) do { } while (0)
  195. # define blk_trace_remove_sysfs(dev) do { } while (0)
  196. static inline int blk_trace_init_sysfs(struct device *dev)
  197. {
  198. return 0;
  199. }
  200. #endif /* CONFIG_BLK_DEV_IO_TRACE */
  201. #ifdef CONFIG_COMPAT
  202. struct compat_blk_user_trace_setup {
  203. char name[32];
  204. u16 act_mask;
  205. u32 buf_size;
  206. u32 buf_nr;
  207. compat_u64 start_lba;
  208. compat_u64 end_lba;
  209. u32 pid;
  210. };
  211. #define BLKTRACESETUP32 _IOWR(0x12, 115, struct compat_blk_user_trace_setup)
  212. #endif
  213. #if defined(CONFIG_EVENT_TRACING) && defined(CONFIG_BLOCK)
  214. static inline int blk_cmd_buf_len(struct request *rq)
  215. {
  216. return (rq->cmd_type == REQ_TYPE_BLOCK_PC) ? rq->cmd_len * 3 : 1;
  217. }
  218. extern void blk_dump_cmd(char *buf, struct request *rq);
  219. extern void blk_fill_rwbs(char *rwbs, u32 rw, int bytes);
  220. #endif /* CONFIG_EVENT_TRACING && CONFIG_BLOCK */
  221. #endif /* __KERNEL__ */
  222. #endif