blktrace_api.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef BLKTRACE_H
  2. #define BLKTRACE_H
  3. #include <linux/blkdev.h>
  4. #include <linux/relay.h>
  5. /*
  6. * Trace categories
  7. */
  8. enum blktrace_cat {
  9. BLK_TC_READ = 1 << 0, /* reads */
  10. BLK_TC_WRITE = 1 << 1, /* writes */
  11. BLK_TC_BARRIER = 1 << 2, /* barrier */
  12. BLK_TC_SYNC = 1 << 3, /* sync IO */
  13. BLK_TC_QUEUE = 1 << 4, /* queueing/merging */
  14. BLK_TC_REQUEUE = 1 << 5, /* requeueing */
  15. BLK_TC_ISSUE = 1 << 6, /* issue */
  16. BLK_TC_COMPLETE = 1 << 7, /* completions */
  17. BLK_TC_FS = 1 << 8, /* fs requests */
  18. BLK_TC_PC = 1 << 9, /* pc requests */
  19. BLK_TC_NOTIFY = 1 << 10, /* special message */
  20. BLK_TC_AHEAD = 1 << 11, /* readahead */
  21. BLK_TC_META = 1 << 12, /* metadata */
  22. BLK_TC_END = 1 << 15, /* only 16-bits, reminder */
  23. };
  24. #define BLK_TC_SHIFT (16)
  25. #define BLK_TC_ACT(act) ((act) << BLK_TC_SHIFT)
  26. /*
  27. * Basic trace actions
  28. */
  29. enum blktrace_act {
  30. __BLK_TA_QUEUE = 1, /* queued */
  31. __BLK_TA_BACKMERGE, /* back merged to existing rq */
  32. __BLK_TA_FRONTMERGE, /* front merge to existing rq */
  33. __BLK_TA_GETRQ, /* allocated new request */
  34. __BLK_TA_SLEEPRQ, /* sleeping on rq allocation */
  35. __BLK_TA_REQUEUE, /* request requeued */
  36. __BLK_TA_ISSUE, /* sent to driver */
  37. __BLK_TA_COMPLETE, /* completed by driver */
  38. __BLK_TA_PLUG, /* queue was plugged */
  39. __BLK_TA_UNPLUG_IO, /* queue was unplugged by io */
  40. __BLK_TA_UNPLUG_TIMER, /* queue was unplugged by timer */
  41. __BLK_TA_INSERT, /* insert request */
  42. __BLK_TA_SPLIT, /* bio was split */
  43. __BLK_TA_BOUNCE, /* bio was bounced */
  44. __BLK_TA_REMAP, /* bio was remapped */
  45. };
  46. /*
  47. * Trace actions in full. Additionally, read or write is masked
  48. */
  49. #define BLK_TA_QUEUE (__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE))
  50. #define BLK_TA_BACKMERGE (__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
  51. #define BLK_TA_FRONTMERGE (__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE))
  52. #define BLK_TA_GETRQ (__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE))
  53. #define BLK_TA_SLEEPRQ (__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE))
  54. #define BLK_TA_REQUEUE (__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE))
  55. #define BLK_TA_ISSUE (__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE))
  56. #define BLK_TA_COMPLETE (__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE))
  57. #define BLK_TA_PLUG (__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE))
  58. #define BLK_TA_UNPLUG_IO (__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE))
  59. #define BLK_TA_UNPLUG_TIMER (__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE))
  60. #define BLK_TA_INSERT (__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE))
  61. #define BLK_TA_SPLIT (__BLK_TA_SPLIT)
  62. #define BLK_TA_BOUNCE (__BLK_TA_BOUNCE)
  63. #define BLK_TA_REMAP (__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE))
  64. #define BLK_IO_TRACE_MAGIC 0x65617400
  65. #define BLK_IO_TRACE_VERSION 0x07
  66. /*
  67. * The trace itself
  68. */
  69. struct blk_io_trace {
  70. u32 magic; /* MAGIC << 8 | version */
  71. u32 sequence; /* event number */
  72. u64 time; /* in microseconds */
  73. u64 sector; /* disk offset */
  74. u32 bytes; /* transfer length */
  75. u32 action; /* what happened */
  76. u32 pid; /* who did it */
  77. u32 device; /* device number */
  78. u32 cpu; /* on what cpu did it happen */
  79. u16 error; /* completion error */
  80. u16 pdu_len; /* length of data after this trace */
  81. };
  82. /*
  83. * The remap event
  84. */
  85. struct blk_io_trace_remap {
  86. __be32 device;
  87. u32 __pad;
  88. __be64 sector;
  89. };
  90. enum {
  91. Blktrace_setup = 1,
  92. Blktrace_running,
  93. Blktrace_stopped,
  94. };
  95. struct blk_trace {
  96. int trace_state;
  97. struct rchan *rchan;
  98. unsigned long *sequence;
  99. u16 act_mask;
  100. u64 start_lba;
  101. u64 end_lba;
  102. u32 pid;
  103. u32 dev;
  104. struct dentry *dir;
  105. struct dentry *dropped_file;
  106. atomic_t dropped;
  107. };
  108. /*
  109. * User setup structure passed with BLKTRACESTART
  110. */
  111. struct blk_user_trace_setup {
  112. char name[BDEVNAME_SIZE]; /* output */
  113. u16 act_mask; /* input */
  114. u32 buf_size; /* input */
  115. u32 buf_nr; /* input */
  116. u64 start_lba;
  117. u64 end_lba;
  118. u32 pid;
  119. };
  120. #if defined(CONFIG_BLK_DEV_IO_TRACE)
  121. extern int blk_trace_ioctl(struct block_device *, unsigned, char __user *);
  122. extern void blk_trace_shutdown(request_queue_t *);
  123. extern void __blk_add_trace(struct blk_trace *, sector_t, int, int, u32, int, int, void *);
  124. /**
  125. * blk_add_trace_rq - Add a trace for a request oriented action
  126. * @q: queue the io is for
  127. * @rq: the source request
  128. * @what: the action
  129. *
  130. * Description:
  131. * Records an action against a request. Will log the bio offset + size.
  132. *
  133. **/
  134. static inline void blk_add_trace_rq(struct request_queue *q, struct request *rq,
  135. u32 what)
  136. {
  137. struct blk_trace *bt = q->blk_trace;
  138. int rw = rq->cmd_flags & 0x03;
  139. if (likely(!bt))
  140. return;
  141. if (blk_pc_request(rq)) {
  142. what |= BLK_TC_ACT(BLK_TC_PC);
  143. __blk_add_trace(bt, 0, rq->data_len, rw, what, rq->errors, sizeof(rq->cmd), rq->cmd);
  144. } else {
  145. what |= BLK_TC_ACT(BLK_TC_FS);
  146. __blk_add_trace(bt, rq->hard_sector, rq->hard_nr_sectors << 9, rw, what, rq->errors, 0, NULL);
  147. }
  148. }
  149. /**
  150. * blk_add_trace_bio - Add a trace for a bio oriented action
  151. * @q: queue the io is for
  152. * @bio: the source bio
  153. * @what: the action
  154. *
  155. * Description:
  156. * Records an action against a bio. Will log the bio offset + size.
  157. *
  158. **/
  159. static inline void blk_add_trace_bio(struct request_queue *q, struct bio *bio,
  160. u32 what)
  161. {
  162. struct blk_trace *bt = q->blk_trace;
  163. if (likely(!bt))
  164. return;
  165. __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), 0, NULL);
  166. }
  167. /**
  168. * blk_add_trace_generic - Add a trace for a generic action
  169. * @q: queue the io is for
  170. * @bio: the source bio
  171. * @rw: the data direction
  172. * @what: the action
  173. *
  174. * Description:
  175. * Records a simple trace
  176. *
  177. **/
  178. static inline void blk_add_trace_generic(struct request_queue *q,
  179. struct bio *bio, int rw, u32 what)
  180. {
  181. struct blk_trace *bt = q->blk_trace;
  182. if (likely(!bt))
  183. return;
  184. if (bio)
  185. blk_add_trace_bio(q, bio, what);
  186. else
  187. __blk_add_trace(bt, 0, 0, rw, what, 0, 0, NULL);
  188. }
  189. /**
  190. * blk_add_trace_pdu_int - Add a trace for a bio with an integer payload
  191. * @q: queue the io is for
  192. * @what: the action
  193. * @bio: the source bio
  194. * @pdu: the integer payload
  195. *
  196. * Description:
  197. * Adds a trace with some integer payload. This might be an unplug
  198. * option given as the action, with the depth at unplug time given
  199. * as the payload
  200. *
  201. **/
  202. static inline void blk_add_trace_pdu_int(struct request_queue *q, u32 what,
  203. struct bio *bio, unsigned int pdu)
  204. {
  205. struct blk_trace *bt = q->blk_trace;
  206. __be64 rpdu = cpu_to_be64(pdu);
  207. if (likely(!bt))
  208. return;
  209. if (bio)
  210. __blk_add_trace(bt, bio->bi_sector, bio->bi_size, bio->bi_rw, what, !bio_flagged(bio, BIO_UPTODATE), sizeof(rpdu), &rpdu);
  211. else
  212. __blk_add_trace(bt, 0, 0, 0, what, 0, sizeof(rpdu), &rpdu);
  213. }
  214. /**
  215. * blk_add_trace_remap - Add a trace for a remap operation
  216. * @q: queue the io is for
  217. * @bio: the source bio
  218. * @dev: target device
  219. * @from: source sector
  220. * @to: target sector
  221. *
  222. * Description:
  223. * Device mapper or raid target sometimes need to split a bio because
  224. * it spans a stripe (or similar). Add a trace for that action.
  225. *
  226. **/
  227. static inline void blk_add_trace_remap(struct request_queue *q, struct bio *bio,
  228. dev_t dev, sector_t from, sector_t to)
  229. {
  230. struct blk_trace *bt = q->blk_trace;
  231. struct blk_io_trace_remap r;
  232. if (likely(!bt))
  233. return;
  234. r.device = cpu_to_be32(dev);
  235. r.sector = cpu_to_be64(to);
  236. __blk_add_trace(bt, from, bio->bi_size, bio->bi_rw, BLK_TA_REMAP, !bio_flagged(bio, BIO_UPTODATE), sizeof(r), &r);
  237. }
  238. #else /* !CONFIG_BLK_DEV_IO_TRACE */
  239. #define blk_trace_ioctl(bdev, cmd, arg) (-ENOTTY)
  240. #define blk_trace_shutdown(q) do { } while (0)
  241. #define blk_add_trace_rq(q, rq, what) do { } while (0)
  242. #define blk_add_trace_bio(q, rq, what) do { } while (0)
  243. #define blk_add_trace_generic(q, rq, rw, what) do { } while (0)
  244. #define blk_add_trace_pdu_int(q, what, bio, pdu) do { } while (0)
  245. #define blk_add_trace_remap(q, bio, dev, f, t) do {} while (0)
  246. #endif /* CONFIG_BLK_DEV_IO_TRACE */
  247. #endif