trace_workqueue.c 6.7 KB

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