trace_workqueue.c 7.4 KB

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