blob: 128b64b93f1424bb4dfe4ada9aa0a660b8846725 [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
Zhaoleifb391252009-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>
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010012#include "trace_stat.h"
13#include "trace.h"
14
15
16/* A cpu workqueue thread */
17struct cpu_workqueue_stats {
18 struct list_head list;
19/* Useful to know if we print the cpu headers */
20 bool first_entry;
21 int cpu;
Steven Rostedtef180122009-03-10 14:10:56 -040022 pid_t pid;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010023/* Can be inserted from interrupt or user context, need to be atomic */
Steven Rostedtef180122009-03-10 14:10:56 -040024 atomic_t inserted;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010025/*
26 * Don't need to be atomic, works are serialized in a single workqueue thread
27 * on a single CPU.
28 */
Steven Rostedtef180122009-03-10 14:10:56 -040029 unsigned int executed;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010030};
31
32/* List of workqueue threads on one cpu */
33struct workqueue_global_stats {
34 struct list_head list;
35 spinlock_t lock;
36};
37
38/* Don't need a global lock because allocated before the workqueues, and
39 * never freed.
40 */
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080041static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat);
42#define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010043
44/* Insertion of a work */
45static void
46probe_workqueue_insertion(struct task_struct *wq_thread,
47 struct work_struct *work)
48{
49 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080050 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010051 unsigned long flags;
52
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080053 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080054 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010055 if (node->pid == wq_thread->pid) {
56 atomic_inc(&node->inserted);
57 goto found;
58 }
59 }
60 pr_debug("trace_workqueue: entry not found\n");
61found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080062 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010063}
64
65/* Execution of a work */
66static void
67probe_workqueue_execution(struct task_struct *wq_thread,
68 struct work_struct *work)
69{
70 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080071 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010072 unsigned long flags;
73
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080074 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080075 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010076 if (node->pid == wq_thread->pid) {
77 node->executed++;
78 goto found;
79 }
80 }
81 pr_debug("trace_workqueue: entry not found\n");
82found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080083 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010084}
85
86/* Creation of a cpu workqueue thread */
87static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu)
88{
89 struct cpu_workqueue_stats *cws;
90 unsigned long flags;
91
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +090092 WARN_ON(cpu < 0);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010093
94 /* Workqueues are sometimes created in atomic context */
95 cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC);
96 if (!cws) {
97 pr_warning("trace_workqueue: not enough memory\n");
98 return;
99 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100100 INIT_LIST_HEAD(&cws->list);
101 cws->cpu = cpu;
102
103 cws->pid = wq_thread->pid;
104
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800105 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
106 if (list_empty(&workqueue_cpu_stat(cpu)->list))
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100107 cws->first_entry = true;
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800108 list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
109 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100110}
111
112/* Destruction of a cpu workqueue thread */
113static void probe_workqueue_destruction(struct task_struct *wq_thread)
114{
115 /* Workqueue only execute on one cpu */
116 int cpu = cpumask_first(&wq_thread->cpus_allowed);
117 struct cpu_workqueue_stats *node, *next;
118 unsigned long flags;
119
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800120 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
121 list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100122 list) {
123 if (node->pid == wq_thread->pid) {
124 list_del(&node->list);
125 kfree(node);
126 goto found;
127 }
128 }
129
130 pr_debug("trace_workqueue: don't find workqueue to destroy\n");
131found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800132 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100133
134}
135
136static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
137{
138 unsigned long flags;
139 struct cpu_workqueue_stats *ret = NULL;
140
141
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800142 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100143
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800144 if (!list_empty(&workqueue_cpu_stat(cpu)->list))
145 ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100146 struct cpu_workqueue_stats, list);
147
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800148 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100149
150 return ret;
151}
152
Steven Rostedt42548002009-03-24 13:38:36 -0400153static void *workqueue_stat_start(struct tracer_stat *trace)
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100154{
155 int cpu;
156 void *ret = NULL;
157
158 for_each_possible_cpu(cpu) {
159 ret = workqueue_stat_start_cpu(cpu);
160 if (ret)
161 return ret;
162 }
163 return NULL;
164}
165
166static void *workqueue_stat_next(void *prev, int idx)
167{
168 struct cpu_workqueue_stats *prev_cws = prev;
169 int cpu = prev_cws->cpu;
170 unsigned long flags;
171 void *ret = NULL;
172
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800173 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
174 if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
175 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +0900176 do {
177 cpu = cpumask_next(cpu, cpu_possible_mask);
178 if (cpu >= nr_cpu_ids)
179 return NULL;
180 } while (!(ret = workqueue_stat_start_cpu(cpu)));
181 return ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100182 }
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800183 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100184
185 return list_entry(prev_cws->list.next, struct cpu_workqueue_stats,
186 list);
187}
188
189static int workqueue_stat_show(struct seq_file *s, void *p)
190{
191 struct cpu_workqueue_stats *cws = p;
192 unsigned long flags;
193 int cpu = cws->cpu;
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900194 struct pid *pid;
195 struct task_struct *tsk;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100196
Lai Jiangshan2f63b842009-03-25 16:59:18 +0800197 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
198 if (&cws->list == workqueue_cpu_stat(cpu)->list.next)
199 seq_printf(s, "\n");
200 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
201
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900202 pid = find_get_pid(cws->pid);
203 if (pid) {
204 tsk = get_pid_task(pid, PIDTYPE_PID);
205 if (tsk) {
206 seq_printf(s, "%3d %6d %6u %s\n", cws->cpu,
207 atomic_read(&cws->inserted), cws->executed,
208 tsk->comm);
209 put_task_struct(tsk);
210 }
211 put_pid(pid);
212 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100213
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100214 return 0;
215}
216
217static int workqueue_stat_headers(struct seq_file *s)
218{
219 seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
Lai Jiangshan2f63b842009-03-25 16:59:18 +0800220 seq_printf(s, "# | | | |\n");
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100221 return 0;
222}
223
224struct tracer_stat workqueue_stats __read_mostly = {
225 .name = "workqueues",
226 .stat_start = workqueue_stat_start,
227 .stat_next = workqueue_stat_next,
228 .stat_show = workqueue_stat_show,
229 .stat_headers = workqueue_stat_headers
230};
231
232
233int __init stat_workqueue_init(void)
234{
235 if (register_stat_tracer(&workqueue_stats)) {
236 pr_warning("Unable to register workqueue stat tracer\n");
237 return 1;
238 }
239
240 return 0;
241}
242fs_initcall(stat_workqueue_init);
243
244/*
245 * Workqueues are created very early, just after pre-smp initcalls.
246 * So we must register our tracepoints at this stage.
247 */
248int __init trace_workqueue_early_init(void)
249{
250 int ret, cpu;
251
252 ret = register_trace_workqueue_insertion(probe_workqueue_insertion);
253 if (ret)
254 goto out;
255
256 ret = register_trace_workqueue_execution(probe_workqueue_execution);
257 if (ret)
258 goto no_insertion;
259
260 ret = register_trace_workqueue_creation(probe_workqueue_creation);
261 if (ret)
262 goto no_execution;
263
264 ret = register_trace_workqueue_destruction(probe_workqueue_destruction);
265 if (ret)
266 goto no_creation;
267
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100268 for_each_possible_cpu(cpu) {
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800269 spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
270 INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100271 }
272
273 return 0;
274
275no_creation:
276 unregister_trace_workqueue_creation(probe_workqueue_creation);
277no_execution:
278 unregister_trace_workqueue_execution(probe_workqueue_execution);
279no_insertion:
280 unregister_trace_workqueue_insertion(probe_workqueue_insertion);
281out:
282 pr_warning("trace_workqueue: unable to trace workqueues\n");
283
284 return 1;
285}
286early_initcall(trace_workqueue_early_init);