blob: a7cc3793baf6897d2535737a52322552040d2737 [file] [log] [blame]
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +01001/*
2 * Workqueue statistical tracer.
3 *
4 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
5 *
6 */
7
8
Zhaoleifb39125f2009-04-17 15:15:51 +08009#include <trace/events/workqueue.h>
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010010#include <linux/list.h>
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080011#include <linux/percpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Lai Jiangshana3578002009-07-06 16:10:23 +080013#include <linux/kref.h>
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010014#include "trace_stat.h"
15#include "trace.h"
16
17
18/* A cpu workqueue thread */
19struct cpu_workqueue_stats {
20 struct list_head list;
Lai Jiangshana3578002009-07-06 16:10:23 +080021 struct kref kref;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010022 int cpu;
Steven Rostedtef180122009-03-10 14:10:56 -040023 pid_t pid;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010024/* Can be inserted from interrupt or user context, need to be atomic */
Steven Rostedtef180122009-03-10 14:10:56 -040025 atomic_t inserted;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010026/*
27 * Don't need to be atomic, works are serialized in a single workqueue thread
28 * on a single CPU.
29 */
Steven Rostedtef180122009-03-10 14:10:56 -040030 unsigned int executed;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010031};
32
33/* List of workqueue threads on one cpu */
34struct workqueue_global_stats {
35 struct list_head list;
36 spinlock_t lock;
37};
38
39/* Don't need a global lock because allocated before the workqueues, and
40 * never freed.
41 */
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080042static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat);
43#define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010044
Lai Jiangshana3578002009-07-06 16:10:23 +080045static void cpu_workqueue_stat_free(struct kref *kref)
46{
47 kfree(container_of(kref, struct cpu_workqueue_stats, kref));
48}
49
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010050/* Insertion of a work */
51static void
Steven Rostedt38516ab2010-04-20 17:04:50 -040052probe_workqueue_insertion(void *ignore,
53 struct task_struct *wq_thread,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010054 struct work_struct *work)
55{
56 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080057 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010058 unsigned long flags;
59
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080060 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080061 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010062 if (node->pid == wq_thread->pid) {
63 atomic_inc(&node->inserted);
64 goto found;
65 }
66 }
67 pr_debug("trace_workqueue: entry not found\n");
68found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080069 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010070}
71
72/* Execution of a work */
73static void
Steven Rostedt38516ab2010-04-20 17:04:50 -040074probe_workqueue_execution(void *ignore,
75 struct task_struct *wq_thread,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010076 struct work_struct *work)
77{
78 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080079 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010080 unsigned long flags;
81
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080082 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080083 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010084 if (node->pid == wq_thread->pid) {
85 node->executed++;
86 goto found;
87 }
88 }
89 pr_debug("trace_workqueue: entry not found\n");
90found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080091 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010092}
93
94/* Creation of a cpu workqueue thread */
Steven Rostedt38516ab2010-04-20 17:04:50 -040095static void probe_workqueue_creation(void *ignore,
96 struct task_struct *wq_thread, int cpu)
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010097{
98 struct cpu_workqueue_stats *cws;
99 unsigned long flags;
100
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +0900101 WARN_ON(cpu < 0);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100102
103 /* Workqueues are sometimes created in atomic context */
104 cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC);
105 if (!cws) {
106 pr_warning("trace_workqueue: not enough memory\n");
107 return;
108 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100109 INIT_LIST_HEAD(&cws->list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800110 kref_init(&cws->kref);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100111 cws->cpu = cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100112 cws->pid = wq_thread->pid;
113
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800114 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800115 list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
116 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100117}
118
119/* Destruction of a cpu workqueue thread */
Steven Rostedt38516ab2010-04-20 17:04:50 -0400120static void
121probe_workqueue_destruction(void *ignore, struct task_struct *wq_thread)
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100122{
123 /* Workqueue only execute on one cpu */
124 int cpu = cpumask_first(&wq_thread->cpus_allowed);
125 struct cpu_workqueue_stats *node, *next;
126 unsigned long flags;
127
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800128 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
129 list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100130 list) {
131 if (node->pid == wq_thread->pid) {
132 list_del(&node->list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800133 kref_put(&node->kref, cpu_workqueue_stat_free);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100134 goto found;
135 }
136 }
137
138 pr_debug("trace_workqueue: don't find workqueue to destroy\n");
139found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800140 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100141
142}
143
144static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
145{
146 unsigned long flags;
147 struct cpu_workqueue_stats *ret = NULL;
148
149
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800150 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100151
Lai Jiangshana3578002009-07-06 16:10:23 +0800152 if (!list_empty(&workqueue_cpu_stat(cpu)->list)) {
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800153 ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100154 struct cpu_workqueue_stats, list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800155 kref_get(&ret->kref);
156 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100157
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800158 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100159
160 return ret;
161}
162
Steven Rostedt42548002009-03-24 13:38:36 -0400163static void *workqueue_stat_start(struct tracer_stat *trace)
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100164{
165 int cpu;
166 void *ret = NULL;
167
168 for_each_possible_cpu(cpu) {
169 ret = workqueue_stat_start_cpu(cpu);
170 if (ret)
171 return ret;
172 }
173 return NULL;
174}
175
176static void *workqueue_stat_next(void *prev, int idx)
177{
178 struct cpu_workqueue_stats *prev_cws = prev;
Lai Jiangshana3578002009-07-06 16:10:23 +0800179 struct cpu_workqueue_stats *ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100180 int cpu = prev_cws->cpu;
181 unsigned long flags;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100182
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800183 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
184 if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
185 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +0900186 do {
187 cpu = cpumask_next(cpu, cpu_possible_mask);
188 if (cpu >= nr_cpu_ids)
189 return NULL;
190 } while (!(ret = workqueue_stat_start_cpu(cpu)));
191 return ret;
Lai Jiangshana3578002009-07-06 16:10:23 +0800192 } else {
193 ret = list_entry(prev_cws->list.next,
194 struct cpu_workqueue_stats, list);
195 kref_get(&ret->kref);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100196 }
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800197 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100198
Lai Jiangshana3578002009-07-06 16:10:23 +0800199 return ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100200}
201
202static int workqueue_stat_show(struct seq_file *s, void *p)
203{
204 struct cpu_workqueue_stats *cws = p;
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900205 struct pid *pid;
206 struct task_struct *tsk;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100207
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900208 pid = find_get_pid(cws->pid);
209 if (pid) {
210 tsk = get_pid_task(pid, PIDTYPE_PID);
211 if (tsk) {
212 seq_printf(s, "%3d %6d %6u %s\n", cws->cpu,
213 atomic_read(&cws->inserted), cws->executed,
214 tsk->comm);
215 put_task_struct(tsk);
216 }
217 put_pid(pid);
218 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100219
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100220 return 0;
221}
222
Lai Jiangshana3578002009-07-06 16:10:23 +0800223static void workqueue_stat_release(void *stat)
224{
225 struct cpu_workqueue_stats *node = stat;
226
227 kref_put(&node->kref, cpu_workqueue_stat_free);
228}
229
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100230static int workqueue_stat_headers(struct seq_file *s)
231{
232 seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
Lai Jiangshan2f63b842009-03-25 16:59:18 +0800233 seq_printf(s, "# | | | |\n");
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100234 return 0;
235}
236
237struct tracer_stat workqueue_stats __read_mostly = {
238 .name = "workqueues",
239 .stat_start = workqueue_stat_start,
240 .stat_next = workqueue_stat_next,
241 .stat_show = workqueue_stat_show,
Lai Jiangshana3578002009-07-06 16:10:23 +0800242 .stat_release = workqueue_stat_release,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100243 .stat_headers = workqueue_stat_headers
244};
245
246
247int __init stat_workqueue_init(void)
248{
249 if (register_stat_tracer(&workqueue_stats)) {
250 pr_warning("Unable to register workqueue stat tracer\n");
251 return 1;
252 }
253
254 return 0;
255}
256fs_initcall(stat_workqueue_init);
257
258/*
259 * Workqueues are created very early, just after pre-smp initcalls.
260 * So we must register our tracepoints at this stage.
261 */
262int __init trace_workqueue_early_init(void)
263{
264 int ret, cpu;
265
Steven Rostedt38516ab2010-04-20 17:04:50 -0400266 ret = register_trace_workqueue_insertion(probe_workqueue_insertion, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100267 if (ret)
268 goto out;
269
Steven Rostedt38516ab2010-04-20 17:04:50 -0400270 ret = register_trace_workqueue_execution(probe_workqueue_execution, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100271 if (ret)
272 goto no_insertion;
273
Steven Rostedt38516ab2010-04-20 17:04:50 -0400274 ret = register_trace_workqueue_creation(probe_workqueue_creation, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100275 if (ret)
276 goto no_execution;
277
Steven Rostedt38516ab2010-04-20 17:04:50 -0400278 ret = register_trace_workqueue_destruction(probe_workqueue_destruction, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100279 if (ret)
280 goto no_creation;
281
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100282 for_each_possible_cpu(cpu) {
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800283 spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
284 INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100285 }
286
287 return 0;
288
289no_creation:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400290 unregister_trace_workqueue_creation(probe_workqueue_creation, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100291no_execution:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400292 unregister_trace_workqueue_execution(probe_workqueue_execution, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100293no_insertion:
Steven Rostedt38516ab2010-04-20 17:04:50 -0400294 unregister_trace_workqueue_insertion(probe_workqueue_insertion, NULL);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100295out:
296 pr_warning("trace_workqueue: unable to trace workqueues\n");
297
298 return 1;
299}
300early_initcall(trace_workqueue_early_init);