blktrace_api.h 7.6 KB

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