slow-work-proc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Slow work debugging
  2. *
  3. * Copyright (C) 2009 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slow-work.h>
  13. #include <linux/fs.h>
  14. #include <linux/time.h>
  15. #include <linux/seq_file.h>
  16. #include "slow-work.h"
  17. #define ITERATOR_SHIFT (BITS_PER_LONG - 4)
  18. #define ITERATOR_SELECTOR (0xfUL << ITERATOR_SHIFT)
  19. #define ITERATOR_COUNTER (~ITERATOR_SELECTOR)
  20. void slow_work_new_thread_desc(struct slow_work *work, struct seq_file *m)
  21. {
  22. seq_puts(m, "Slow-work: New thread");
  23. }
  24. /*
  25. * Render the time mark field on a work item into a 5-char time with units plus
  26. * a space
  27. */
  28. static void slow_work_print_mark(struct seq_file *m, struct slow_work *work)
  29. {
  30. struct timespec now, diff;
  31. now = CURRENT_TIME;
  32. diff = timespec_sub(now, work->mark);
  33. if (diff.tv_sec < 0)
  34. seq_puts(m, " -ve ");
  35. else if (diff.tv_sec == 0 && diff.tv_nsec < 1000)
  36. seq_printf(m, "%3luns ", diff.tv_nsec);
  37. else if (diff.tv_sec == 0 && diff.tv_nsec < 1000000)
  38. seq_printf(m, "%3luus ", diff.tv_nsec / 1000);
  39. else if (diff.tv_sec == 0 && diff.tv_nsec < 1000000000)
  40. seq_printf(m, "%3lums ", diff.tv_nsec / 1000000);
  41. else if (diff.tv_sec <= 1)
  42. seq_puts(m, " 1s ");
  43. else if (diff.tv_sec < 60)
  44. seq_printf(m, "%4lus ", diff.tv_sec);
  45. else if (diff.tv_sec < 60 * 60)
  46. seq_printf(m, "%4lum ", diff.tv_sec / 60);
  47. else if (diff.tv_sec < 60 * 60 * 24)
  48. seq_printf(m, "%4luh ", diff.tv_sec / 3600);
  49. else
  50. seq_puts(m, "exces ");
  51. }
  52. /*
  53. * Describe a slow work item for /proc
  54. */
  55. static int slow_work_runqueue_show(struct seq_file *m, void *v)
  56. {
  57. struct slow_work *work;
  58. struct list_head *p = v;
  59. unsigned long id;
  60. switch ((unsigned long) v) {
  61. case 1:
  62. seq_puts(m, "THR PID ITEM ADDR FL MARK DESC\n");
  63. return 0;
  64. case 2:
  65. seq_puts(m, "=== ===== ================ == ===== ==========\n");
  66. return 0;
  67. case 3 ... 3 + SLOW_WORK_THREAD_LIMIT - 1:
  68. id = (unsigned long) v - 3;
  69. read_lock(&slow_work_execs_lock);
  70. work = slow_work_execs[id];
  71. if (work) {
  72. smp_read_barrier_depends();
  73. seq_printf(m, "%3lu %5d %16p %2lx ",
  74. id, slow_work_pids[id], work, work->flags);
  75. slow_work_print_mark(m, work);
  76. if (work->ops->desc)
  77. work->ops->desc(work, m);
  78. seq_putc(m, '\n');
  79. }
  80. read_unlock(&slow_work_execs_lock);
  81. return 0;
  82. default:
  83. work = list_entry(p, struct slow_work, link);
  84. seq_printf(m, "%3s - %16p %2lx ",
  85. work->flags & SLOW_WORK_VERY_SLOW ? "vsq" : "sq",
  86. work, work->flags);
  87. slow_work_print_mark(m, work);
  88. if (work->ops->desc)
  89. work->ops->desc(work, m);
  90. seq_putc(m, '\n');
  91. return 0;
  92. }
  93. }
  94. /*
  95. * map the iterator to a work item
  96. */
  97. static void *slow_work_runqueue_index(struct seq_file *m, loff_t *_pos)
  98. {
  99. struct list_head *p;
  100. unsigned long count, id;
  101. switch (*_pos >> ITERATOR_SHIFT) {
  102. case 0x0:
  103. if (*_pos == 0)
  104. *_pos = 1;
  105. if (*_pos < 3)
  106. return (void *)(unsigned long) *_pos;
  107. if (*_pos < 3 + SLOW_WORK_THREAD_LIMIT)
  108. for (id = *_pos - 3;
  109. id < SLOW_WORK_THREAD_LIMIT;
  110. id++, (*_pos)++)
  111. if (slow_work_execs[id])
  112. return (void *)(unsigned long) *_pos;
  113. *_pos = 0x1UL << ITERATOR_SHIFT;
  114. case 0x1:
  115. count = *_pos & ITERATOR_COUNTER;
  116. list_for_each(p, &slow_work_queue) {
  117. if (count == 0)
  118. return p;
  119. count--;
  120. }
  121. *_pos = 0x2UL << ITERATOR_SHIFT;
  122. case 0x2:
  123. count = *_pos & ITERATOR_COUNTER;
  124. list_for_each(p, &vslow_work_queue) {
  125. if (count == 0)
  126. return p;
  127. count--;
  128. }
  129. *_pos = 0x3UL << ITERATOR_SHIFT;
  130. default:
  131. return NULL;
  132. }
  133. }
  134. /*
  135. * set up the iterator to start reading from the first line
  136. */
  137. static void *slow_work_runqueue_start(struct seq_file *m, loff_t *_pos)
  138. {
  139. spin_lock_irq(&slow_work_queue_lock);
  140. return slow_work_runqueue_index(m, _pos);
  141. }
  142. /*
  143. * move to the next line
  144. */
  145. static void *slow_work_runqueue_next(struct seq_file *m, void *v, loff_t *_pos)
  146. {
  147. struct list_head *p = v;
  148. unsigned long selector = *_pos >> ITERATOR_SHIFT;
  149. (*_pos)++;
  150. switch (selector) {
  151. case 0x0:
  152. return slow_work_runqueue_index(m, _pos);
  153. case 0x1:
  154. if (*_pos >> ITERATOR_SHIFT == 0x1) {
  155. p = p->next;
  156. if (p != &slow_work_queue)
  157. return p;
  158. }
  159. *_pos = 0x2UL << ITERATOR_SHIFT;
  160. p = &vslow_work_queue;
  161. case 0x2:
  162. if (*_pos >> ITERATOR_SHIFT == 0x2) {
  163. p = p->next;
  164. if (p != &vslow_work_queue)
  165. return p;
  166. }
  167. *_pos = 0x3UL << ITERATOR_SHIFT;
  168. default:
  169. return NULL;
  170. }
  171. }
  172. /*
  173. * clean up after reading
  174. */
  175. static void slow_work_runqueue_stop(struct seq_file *m, void *v)
  176. {
  177. spin_unlock_irq(&slow_work_queue_lock);
  178. }
  179. static const struct seq_operations slow_work_runqueue_ops = {
  180. .start = slow_work_runqueue_start,
  181. .stop = slow_work_runqueue_stop,
  182. .next = slow_work_runqueue_next,
  183. .show = slow_work_runqueue_show,
  184. };
  185. /*
  186. * open "/proc/slow_work_rq" to list queue contents
  187. */
  188. static int slow_work_runqueue_open(struct inode *inode, struct file *file)
  189. {
  190. return seq_open(file, &slow_work_runqueue_ops);
  191. }
  192. const struct file_operations slow_work_runqueue_fops = {
  193. .owner = THIS_MODULE,
  194. .open = slow_work_runqueue_open,
  195. .read = seq_read,
  196. .llseek = seq_lseek,
  197. .release = seq_release,
  198. };