blob: 40cafb07dffd11e533b17ef5c7fcf6546aef00f5 [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>
Lai Jiangshana3578002009-07-06 16:10:23 +080012#include <linux/kref.h>
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010013#include "trace_stat.h"
14#include "trace.h"
15
16
17/* A cpu workqueue thread */
18struct cpu_workqueue_stats {
19 struct list_head list;
Lai Jiangshana3578002009-07-06 16:10:23 +080020 struct kref kref;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010021 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
Lai Jiangshana3578002009-07-06 16:10:23 +080044static void cpu_workqueue_stat_free(struct kref *kref)
45{
46 kfree(container_of(kref, struct cpu_workqueue_stats, kref));
47}
48
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010049/* Insertion of a work */
50static void
51probe_workqueue_insertion(struct task_struct *wq_thread,
52 struct work_struct *work)
53{
54 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080055 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010056 unsigned long flags;
57
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080058 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080059 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010060 if (node->pid == wq_thread->pid) {
61 atomic_inc(&node->inserted);
62 goto found;
63 }
64 }
65 pr_debug("trace_workqueue: entry not found\n");
66found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080067 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010068}
69
70/* Execution of a work */
71static void
72probe_workqueue_execution(struct task_struct *wq_thread,
73 struct work_struct *work)
74{
75 int cpu = cpumask_first(&wq_thread->cpus_allowed);
Zhaolei1fdfca92009-04-20 14:58:26 +080076 struct cpu_workqueue_stats *node;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010077 unsigned long flags;
78
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080079 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Zhaolei1fdfca92009-04-20 14:58:26 +080080 list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010081 if (node->pid == wq_thread->pid) {
82 node->executed++;
83 goto found;
84 }
85 }
86 pr_debug("trace_workqueue: entry not found\n");
87found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +080088 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010089}
90
91/* Creation of a cpu workqueue thread */
92static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu)
93{
94 struct cpu_workqueue_stats *cws;
95 unsigned long flags;
96
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +090097 WARN_ON(cpu < 0);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +010098
99 /* Workqueues are sometimes created in atomic context */
100 cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC);
101 if (!cws) {
102 pr_warning("trace_workqueue: not enough memory\n");
103 return;
104 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100105 INIT_LIST_HEAD(&cws->list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800106 kref_init(&cws->kref);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100107 cws->cpu = cpu;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100108 cws->pid = wq_thread->pid;
109
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800110 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800111 list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
112 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100113}
114
115/* Destruction of a cpu workqueue thread */
116static void probe_workqueue_destruction(struct task_struct *wq_thread)
117{
118 /* Workqueue only execute on one cpu */
119 int cpu = cpumask_first(&wq_thread->cpus_allowed);
120 struct cpu_workqueue_stats *node, *next;
121 unsigned long flags;
122
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800123 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
124 list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100125 list) {
126 if (node->pid == wq_thread->pid) {
127 list_del(&node->list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800128 kref_put(&node->kref, cpu_workqueue_stat_free);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100129 goto found;
130 }
131 }
132
133 pr_debug("trace_workqueue: don't find workqueue to destroy\n");
134found:
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800135 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100136
137}
138
139static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
140{
141 unsigned long flags;
142 struct cpu_workqueue_stats *ret = NULL;
143
144
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800145 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100146
Lai Jiangshana3578002009-07-06 16:10:23 +0800147 if (!list_empty(&workqueue_cpu_stat(cpu)->list)) {
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800148 ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100149 struct cpu_workqueue_stats, list);
Lai Jiangshana3578002009-07-06 16:10:23 +0800150 kref_get(&ret->kref);
151 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100152
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800153 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100154
155 return ret;
156}
157
Steven Rostedt42548002009-03-24 13:38:36 -0400158static void *workqueue_stat_start(struct tracer_stat *trace)
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100159{
160 int cpu;
161 void *ret = NULL;
162
163 for_each_possible_cpu(cpu) {
164 ret = workqueue_stat_start_cpu(cpu);
165 if (ret)
166 return ret;
167 }
168 return NULL;
169}
170
171static void *workqueue_stat_next(void *prev, int idx)
172{
173 struct cpu_workqueue_stats *prev_cws = prev;
Lai Jiangshana3578002009-07-06 16:10:23 +0800174 struct cpu_workqueue_stats *ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100175 int cpu = prev_cws->cpu;
176 unsigned long flags;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100177
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800178 spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
179 if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
180 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
KOSAKI Motohirobbcd3062009-03-10 10:49:53 +0900181 do {
182 cpu = cpumask_next(cpu, cpu_possible_mask);
183 if (cpu >= nr_cpu_ids)
184 return NULL;
185 } while (!(ret = workqueue_stat_start_cpu(cpu)));
186 return ret;
Lai Jiangshana3578002009-07-06 16:10:23 +0800187 } else {
188 ret = list_entry(prev_cws->list.next,
189 struct cpu_workqueue_stats, list);
190 kref_get(&ret->kref);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100191 }
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800192 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100193
Lai Jiangshana3578002009-07-06 16:10:23 +0800194 return ret;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100195}
196
197static int workqueue_stat_show(struct seq_file *s, void *p)
198{
199 struct cpu_workqueue_stats *cws = p;
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900200 struct pid *pid;
201 struct task_struct *tsk;
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100202
KOSAKI Motohiro889a6c32009-03-13 09:03:04 +0900203 pid = find_get_pid(cws->pid);
204 if (pid) {
205 tsk = get_pid_task(pid, PIDTYPE_PID);
206 if (tsk) {
207 seq_printf(s, "%3d %6d %6u %s\n", cws->cpu,
208 atomic_read(&cws->inserted), cws->executed,
209 tsk->comm);
210 put_task_struct(tsk);
211 }
212 put_pid(pid);
213 }
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100214
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100215 return 0;
216}
217
Lai Jiangshana3578002009-07-06 16:10:23 +0800218static void workqueue_stat_release(void *stat)
219{
220 struct cpu_workqueue_stats *node = stat;
221
222 kref_put(&node->kref, cpu_workqueue_stat_free);
223}
224
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100225static int workqueue_stat_headers(struct seq_file *s)
226{
227 seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
Lai Jiangshan2f63b842009-03-25 16:59:18 +0800228 seq_printf(s, "# | | | |\n");
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100229 return 0;
230}
231
232struct tracer_stat workqueue_stats __read_mostly = {
233 .name = "workqueues",
234 .stat_start = workqueue_stat_start,
235 .stat_next = workqueue_stat_next,
236 .stat_show = workqueue_stat_show,
Lai Jiangshana3578002009-07-06 16:10:23 +0800237 .stat_release = workqueue_stat_release,
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100238 .stat_headers = workqueue_stat_headers
239};
240
241
242int __init stat_workqueue_init(void)
243{
244 if (register_stat_tracer(&workqueue_stats)) {
245 pr_warning("Unable to register workqueue stat tracer\n");
246 return 1;
247 }
248
249 return 0;
250}
251fs_initcall(stat_workqueue_init);
252
253/*
254 * Workqueues are created very early, just after pre-smp initcalls.
255 * So we must register our tracepoints at this stage.
256 */
257int __init trace_workqueue_early_init(void)
258{
259 int ret, cpu;
260
261 ret = register_trace_workqueue_insertion(probe_workqueue_insertion);
262 if (ret)
263 goto out;
264
265 ret = register_trace_workqueue_execution(probe_workqueue_execution);
266 if (ret)
267 goto no_insertion;
268
269 ret = register_trace_workqueue_creation(probe_workqueue_creation);
270 if (ret)
271 goto no_execution;
272
273 ret = register_trace_workqueue_destruction(probe_workqueue_destruction);
274 if (ret)
275 goto no_creation;
276
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100277 for_each_possible_cpu(cpu) {
Lai Jiangshan3690b5e2009-01-16 16:32:25 +0800278 spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
279 INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
Frederic Weisbeckere1d8aa92009-01-12 23:15:46 +0100280 }
281
282 return 0;
283
284no_creation:
285 unregister_trace_workqueue_creation(probe_workqueue_creation);
286no_execution:
287 unregister_trace_workqueue_execution(probe_workqueue_execution);
288no_insertion:
289 unregister_trace_workqueue_insertion(probe_workqueue_insertion);
290out:
291 pr_warning("trace_workqueue: unable to trace workqueues\n");
292
293 return 1;
294}
295early_initcall(trace_workqueue_early_init);