trace_workqueue.c 7.2 KB

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