trace_workqueue.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. /* 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);
  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. INIT_LIST_HEAD(&cws->list);
  90. cws->cpu = cpu;
  91. cws->pid = wq_thread->pid;
  92. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  93. if (list_empty(&workqueue_cpu_stat(cpu)->list))
  94. cws->first_entry = true;
  95. list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
  96. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  97. }
  98. /* Destruction of a cpu workqueue thread */
  99. static void probe_workqueue_destruction(struct task_struct *wq_thread)
  100. {
  101. /* Workqueue only execute on one cpu */
  102. int cpu = cpumask_first(&wq_thread->cpus_allowed);
  103. struct cpu_workqueue_stats *node, *next;
  104. unsigned long flags;
  105. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  106. list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
  107. list) {
  108. if (node->pid == wq_thread->pid) {
  109. list_del(&node->list);
  110. kfree(node);
  111. goto found;
  112. }
  113. }
  114. pr_debug("trace_workqueue: don't find workqueue to destroy\n");
  115. found:
  116. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  117. }
  118. static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
  119. {
  120. unsigned long flags;
  121. struct cpu_workqueue_stats *ret = NULL;
  122. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  123. if (!list_empty(&workqueue_cpu_stat(cpu)->list))
  124. ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
  125. struct cpu_workqueue_stats, list);
  126. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  127. return ret;
  128. }
  129. static void *workqueue_stat_start(struct tracer_stat *trace)
  130. {
  131. int cpu;
  132. void *ret = NULL;
  133. for_each_possible_cpu(cpu) {
  134. ret = workqueue_stat_start_cpu(cpu);
  135. if (ret)
  136. return ret;
  137. }
  138. return NULL;
  139. }
  140. static void *workqueue_stat_next(void *prev, int idx)
  141. {
  142. struct cpu_workqueue_stats *prev_cws = prev;
  143. int cpu = prev_cws->cpu;
  144. unsigned long flags;
  145. void *ret = NULL;
  146. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  147. if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
  148. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  149. do {
  150. cpu = cpumask_next(cpu, cpu_possible_mask);
  151. if (cpu >= nr_cpu_ids)
  152. return NULL;
  153. } while (!(ret = workqueue_stat_start_cpu(cpu)));
  154. return ret;
  155. }
  156. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  157. return list_entry(prev_cws->list.next, struct cpu_workqueue_stats,
  158. list);
  159. }
  160. static int workqueue_stat_show(struct seq_file *s, void *p)
  161. {
  162. struct cpu_workqueue_stats *cws = p;
  163. unsigned long flags;
  164. int cpu = cws->cpu;
  165. struct pid *pid;
  166. struct task_struct *tsk;
  167. spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
  168. if (&cws->list == workqueue_cpu_stat(cpu)->list.next)
  169. seq_printf(s, "\n");
  170. spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
  171. pid = find_get_pid(cws->pid);
  172. if (pid) {
  173. tsk = get_pid_task(pid, PIDTYPE_PID);
  174. if (tsk) {
  175. seq_printf(s, "%3d %6d %6u %s\n", cws->cpu,
  176. atomic_read(&cws->inserted), cws->executed,
  177. tsk->comm);
  178. put_task_struct(tsk);
  179. }
  180. put_pid(pid);
  181. }
  182. return 0;
  183. }
  184. static int workqueue_stat_headers(struct seq_file *s)
  185. {
  186. seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
  187. seq_printf(s, "# | | | |\n");
  188. return 0;
  189. }
  190. struct tracer_stat workqueue_stats __read_mostly = {
  191. .name = "workqueues",
  192. .stat_start = workqueue_stat_start,
  193. .stat_next = workqueue_stat_next,
  194. .stat_show = workqueue_stat_show,
  195. .stat_headers = workqueue_stat_headers
  196. };
  197. int __init stat_workqueue_init(void)
  198. {
  199. if (register_stat_tracer(&workqueue_stats)) {
  200. pr_warning("Unable to register workqueue stat tracer\n");
  201. return 1;
  202. }
  203. return 0;
  204. }
  205. fs_initcall(stat_workqueue_init);
  206. /*
  207. * Workqueues are created very early, just after pre-smp initcalls.
  208. * So we must register our tracepoints at this stage.
  209. */
  210. int __init trace_workqueue_early_init(void)
  211. {
  212. int ret, cpu;
  213. ret = register_trace_workqueue_insertion(probe_workqueue_insertion);
  214. if (ret)
  215. goto out;
  216. ret = register_trace_workqueue_execution(probe_workqueue_execution);
  217. if (ret)
  218. goto no_insertion;
  219. ret = register_trace_workqueue_creation(probe_workqueue_creation);
  220. if (ret)
  221. goto no_execution;
  222. ret = register_trace_workqueue_destruction(probe_workqueue_destruction);
  223. if (ret)
  224. goto no_creation;
  225. for_each_possible_cpu(cpu) {
  226. spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
  227. INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
  228. }
  229. return 0;
  230. no_creation:
  231. unregister_trace_workqueue_creation(probe_workqueue_creation);
  232. no_execution:
  233. unregister_trace_workqueue_execution(probe_workqueue_execution);
  234. no_insertion:
  235. unregister_trace_workqueue_insertion(probe_workqueue_insertion);
  236. out:
  237. pr_warning("trace_workqueue: unable to trace workqueues\n");
  238. return 1;
  239. }
  240. early_initcall(trace_workqueue_early_init);