trace_workqueue.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Workqueue statistical tracer.
  3. *
  4. * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
  5. *
  6. */
  7. #include <trace/workqueue.h>
  8. #include <linux/list.h>
  9. #include <linux/percpu.h>
  10. #include "trace_stat.h"
  11. #include "trace.h"
  12. /* A cpu workqueue thread */
  13. struct cpu_workqueue_stats {
  14. struct list_head list;
  15. /* Useful to know if we print the cpu headers */
  16. bool first_entry;
  17. int cpu;
  18. pid_t pid;
  19. /* Can be inserted from interrupt or user context, need to be atomic */
  20. atomic_t inserted;
  21. /*
  22. * Don't need to be atomic, works are serialized in a single workqueue thread
  23. * on a single CPU.
  24. */
  25. unsigned int executed;
  26. };
  27. /* List of workqueue threads on one cpu */
  28. struct workqueue_global_stats {
  29. struct list_head list;
  30. spinlock_t lock;
  31. };
  32. /* Don't need a global lock because allocated before the workqueues, and
  33. * never freed.
  34. */
  35. static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat);
  36. #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
  37. /* Insertion of a work */
  38. static void
  39. probe_workqueue_insertion(struct task_struct *wq_thread,
  40. struct work_struct *work)
  41. {
  42. int cpu = cpumask_first(&wq_thread->cpus_allowed);
  43. struct cpu_workqueue_stats *node, *next;
  44. unsigned long flags;
  45. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  46. list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
  47. list) {
  48. if (node->pid == wq_thread->pid) {
  49. atomic_inc(&node->inserted);
  50. goto found;
  51. }
  52. }
  53. pr_debug("trace_workqueue: entry not found\n");
  54. found:
  55. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  56. }
  57. /* Execution of a work */
  58. static void
  59. probe_workqueue_execution(struct task_struct *wq_thread,
  60. struct work_struct *work)
  61. {
  62. int cpu = cpumask_first(&wq_thread->cpus_allowed);
  63. struct cpu_workqueue_stats *node, *next;
  64. unsigned long flags;
  65. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  66. list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
  67. list) {
  68. if (node->pid == wq_thread->pid) {
  69. node->executed++;
  70. goto found;
  71. }
  72. }
  73. pr_debug("trace_workqueue: entry not found\n");
  74. found:
  75. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  76. }
  77. /* Creation of a cpu workqueue thread */
  78. static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu)
  79. {
  80. struct cpu_workqueue_stats *cws;
  81. unsigned long flags;
  82. WARN_ON(cpu < 0 || cpu >= num_possible_cpus());
  83. /* Workqueues are sometimes created in atomic context */
  84. cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC);
  85. if (!cws) {
  86. pr_warning("trace_workqueue: not enough memory\n");
  87. return;
  88. }
  89. tracing_record_cmdline(wq_thread);
  90. INIT_LIST_HEAD(&cws->list);
  91. cws->cpu = cpu;
  92. cws->pid = wq_thread->pid;
  93. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  94. if (list_empty(&workqueue_cpu_stat(cpu)->list))
  95. cws->first_entry = true;
  96. list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
  97. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  98. }
  99. /* Destruction of a cpu workqueue thread */
  100. static void probe_workqueue_destruction(struct task_struct *wq_thread)
  101. {
  102. /* Workqueue only execute on one cpu */
  103. int cpu = cpumask_first(&wq_thread->cpus_allowed);
  104. struct cpu_workqueue_stats *node, *next;
  105. unsigned long flags;
  106. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  107. list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
  108. list) {
  109. if (node->pid == wq_thread->pid) {
  110. list_del(&node->list);
  111. kfree(node);
  112. goto found;
  113. }
  114. }
  115. pr_debug("trace_workqueue: don't find workqueue to destroy\n");
  116. found:
  117. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  118. }
  119. static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
  120. {
  121. unsigned long flags;
  122. struct cpu_workqueue_stats *ret = NULL;
  123. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  124. if (!list_empty(&workqueue_cpu_stat(cpu)->list))
  125. ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
  126. struct cpu_workqueue_stats, list);
  127. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  128. return ret;
  129. }
  130. static void *workqueue_stat_start(void)
  131. {
  132. int cpu;
  133. void *ret = NULL;
  134. for_each_possible_cpu(cpu) {
  135. ret = workqueue_stat_start_cpu(cpu);
  136. if (ret)
  137. return ret;
  138. }
  139. return NULL;
  140. }
  141. static void *workqueue_stat_next(void *prev, int idx)
  142. {
  143. struct cpu_workqueue_stats *prev_cws = prev;
  144. int cpu = prev_cws->cpu;
  145. unsigned long flags;
  146. void *ret = NULL;
  147. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  148. if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
  149. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  150. for (++cpu ; cpu < num_possible_cpus(); cpu++) {
  151. ret = workqueue_stat_start_cpu(cpu);
  152. if (ret)
  153. return ret;
  154. }
  155. return NULL;
  156. }
  157. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  158. return list_entry(prev_cws->list.next, struct cpu_workqueue_stats,
  159. list);
  160. }
  161. static int workqueue_stat_show(struct seq_file *s, void *p)
  162. {
  163. struct cpu_workqueue_stats *cws = p;
  164. unsigned long flags;
  165. int cpu = cws->cpu;
  166. seq_printf(s, "%3d %6d %6u %s\n", cws->cpu,
  167. atomic_read(&cws->inserted),
  168. cws->executed,
  169. trace_find_cmdline(cws->pid));
  170. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  171. if (&cws->list == workqueue_cpu_stat(cpu)->list.next)
  172. seq_printf(s, "\n");
  173. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  174. return 0;
  175. }
  176. static int workqueue_stat_headers(struct seq_file *s)
  177. {
  178. seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
  179. seq_printf(s, "# | | | |\n\n");
  180. return 0;
  181. }
  182. struct tracer_stat workqueue_stats __read_mostly = {
  183. .name = "workqueues",
  184. .stat_start = workqueue_stat_start,
  185. .stat_next = workqueue_stat_next,
  186. .stat_show = workqueue_stat_show,
  187. .stat_headers = workqueue_stat_headers
  188. };
  189. int __init stat_workqueue_init(void)
  190. {
  191. if (register_stat_tracer(&workqueue_stats)) {
  192. pr_warning("Unable to register workqueue stat tracer\n");
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. fs_initcall(stat_workqueue_init);
  198. /*
  199. * Workqueues are created very early, just after pre-smp initcalls.
  200. * So we must register our tracepoints at this stage.
  201. */
  202. int __init trace_workqueue_early_init(void)
  203. {
  204. int ret, cpu;
  205. ret = register_trace_workqueue_insertion(probe_workqueue_insertion);
  206. if (ret)
  207. goto out;
  208. ret = register_trace_workqueue_execution(probe_workqueue_execution);
  209. if (ret)
  210. goto no_insertion;
  211. ret = register_trace_workqueue_creation(probe_workqueue_creation);
  212. if (ret)
  213. goto no_execution;
  214. ret = register_trace_workqueue_destruction(probe_workqueue_destruction);
  215. if (ret)
  216. goto no_creation;
  217. for_each_possible_cpu(cpu) {
  218. spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
  219. INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
  220. }
  221. return 0;
  222. no_creation:
  223. unregister_trace_workqueue_creation(probe_workqueue_creation);
  224. no_execution:
  225. unregister_trace_workqueue_execution(probe_workqueue_execution);
  226. no_insertion:
  227. unregister_trace_workqueue_insertion(probe_workqueue_insertion);
  228. out:
  229. pr_warning("trace_workqueue: unable to trace workqueues\n");
  230. return 1;
  231. }
  232. early_initcall(trace_workqueue_early_init);