trace_workqueue.c 7.1 KB

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