trace_workqueue.c 7.3 KB

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