]>
Commit | Line | Data |
---|---|---|
e1d8aa9f FW |
1 | /* |
2 | * Workqueue statistical tracer. | |
3 | * | |
4 | * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com> | |
5 | * | |
6 | */ | |
7 | ||
8 | ||
fb39125f | 9 | #include <trace/events/workqueue.h> |
e1d8aa9f | 10 | #include <linux/list.h> |
3690b5e6 | 11 | #include <linux/percpu.h> |
5a0e3ad6 | 12 | #include <linux/slab.h> |
a3578000 | 13 | #include <linux/kref.h> |
e1d8aa9f FW |
14 | #include "trace_stat.h" |
15 | #include "trace.h" | |
16 | ||
17 | ||
18 | /* A cpu workqueue thread */ | |
19 | struct cpu_workqueue_stats { | |
20 | struct list_head list; | |
a3578000 | 21 | struct kref kref; |
e1d8aa9f | 22 | int cpu; |
ef18012b | 23 | pid_t pid; |
e1d8aa9f | 24 | /* Can be inserted from interrupt or user context, need to be atomic */ |
ef18012b | 25 | atomic_t inserted; |
e1d8aa9f FW |
26 | /* |
27 | * Don't need to be atomic, works are serialized in a single workqueue thread | |
28 | * on a single CPU. | |
29 | */ | |
ef18012b | 30 | unsigned int executed; |
e1d8aa9f FW |
31 | }; |
32 | ||
33 | /* List of workqueue threads on one cpu */ | |
34 | struct 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 | */ | |
3690b5e6 LJ |
42 | static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat); |
43 | #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu)) | |
e1d8aa9f | 44 | |
a3578000 LJ |
45 | static void cpu_workqueue_stat_free(struct kref *kref) |
46 | { | |
47 | kfree(container_of(kref, struct cpu_workqueue_stats, kref)); | |
48 | } | |
49 | ||
e1d8aa9f FW |
50 | /* Insertion of a work */ |
51 | static void | |
52 | probe_workqueue_insertion(struct task_struct *wq_thread, | |
53 | struct work_struct *work) | |
54 | { | |
55 | int cpu = cpumask_first(&wq_thread->cpus_allowed); | |
1fdfca9c | 56 | struct cpu_workqueue_stats *node; |
e1d8aa9f FW |
57 | unsigned long flags; |
58 | ||
3690b5e6 | 59 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
1fdfca9c | 60 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) { |
e1d8aa9f FW |
61 | if (node->pid == wq_thread->pid) { |
62 | atomic_inc(&node->inserted); | |
63 | goto found; | |
64 | } | |
65 | } | |
66 | pr_debug("trace_workqueue: entry not found\n"); | |
67 | found: | |
3690b5e6 | 68 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
69 | } |
70 | ||
71 | /* Execution of a work */ | |
72 | static void | |
73 | probe_workqueue_execution(struct task_struct *wq_thread, | |
74 | struct work_struct *work) | |
75 | { | |
76 | int cpu = cpumask_first(&wq_thread->cpus_allowed); | |
1fdfca9c | 77 | struct cpu_workqueue_stats *node; |
e1d8aa9f FW |
78 | unsigned long flags; |
79 | ||
3690b5e6 | 80 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
1fdfca9c | 81 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) { |
e1d8aa9f FW |
82 | if (node->pid == wq_thread->pid) { |
83 | node->executed++; | |
84 | goto found; | |
85 | } | |
86 | } | |
87 | pr_debug("trace_workqueue: entry not found\n"); | |
88 | found: | |
3690b5e6 | 89 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
90 | } |
91 | ||
92 | /* Creation of a cpu workqueue thread */ | |
93 | static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu) | |
94 | { | |
95 | struct cpu_workqueue_stats *cws; | |
96 | unsigned long flags; | |
97 | ||
bbcd3063 | 98 | WARN_ON(cpu < 0); |
e1d8aa9f FW |
99 | |
100 | /* Workqueues are sometimes created in atomic context */ | |
101 | cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC); | |
102 | if (!cws) { | |
103 | pr_warning("trace_workqueue: not enough memory\n"); | |
104 | return; | |
105 | } | |
e1d8aa9f | 106 | INIT_LIST_HEAD(&cws->list); |
a3578000 | 107 | kref_init(&cws->kref); |
e1d8aa9f | 108 | cws->cpu = cpu; |
e1d8aa9f FW |
109 | cws->pid = wq_thread->pid; |
110 | ||
3690b5e6 | 111 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
3690b5e6 LJ |
112 | list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list); |
113 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); | |
e1d8aa9f FW |
114 | } |
115 | ||
116 | /* Destruction of a cpu workqueue thread */ | |
117 | static void probe_workqueue_destruction(struct task_struct *wq_thread) | |
118 | { | |
119 | /* Workqueue only execute on one cpu */ | |
120 | int cpu = cpumask_first(&wq_thread->cpus_allowed); | |
121 | struct cpu_workqueue_stats *node, *next; | |
122 | unsigned long flags; | |
123 | ||
3690b5e6 LJ |
124 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
125 | list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list, | |
e1d8aa9f FW |
126 | list) { |
127 | if (node->pid == wq_thread->pid) { | |
128 | list_del(&node->list); | |
a3578000 | 129 | kref_put(&node->kref, cpu_workqueue_stat_free); |
e1d8aa9f FW |
130 | goto found; |
131 | } | |
132 | } | |
133 | ||
134 | pr_debug("trace_workqueue: don't find workqueue to destroy\n"); | |
135 | found: | |
3690b5e6 | 136 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
137 | |
138 | } | |
139 | ||
140 | static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu) | |
141 | { | |
142 | unsigned long flags; | |
143 | struct cpu_workqueue_stats *ret = NULL; | |
144 | ||
145 | ||
3690b5e6 | 146 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f | 147 | |
a3578000 | 148 | if (!list_empty(&workqueue_cpu_stat(cpu)->list)) { |
3690b5e6 | 149 | ret = list_entry(workqueue_cpu_stat(cpu)->list.next, |
e1d8aa9f | 150 | struct cpu_workqueue_stats, list); |
a3578000 LJ |
151 | kref_get(&ret->kref); |
152 | } | |
e1d8aa9f | 153 | |
3690b5e6 | 154 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
155 | |
156 | return ret; | |
157 | } | |
158 | ||
42548008 | 159 | static void *workqueue_stat_start(struct tracer_stat *trace) |
e1d8aa9f FW |
160 | { |
161 | int cpu; | |
162 | void *ret = NULL; | |
163 | ||
164 | for_each_possible_cpu(cpu) { | |
165 | ret = workqueue_stat_start_cpu(cpu); | |
166 | if (ret) | |
167 | return ret; | |
168 | } | |
169 | return NULL; | |
170 | } | |
171 | ||
172 | static void *workqueue_stat_next(void *prev, int idx) | |
173 | { | |
174 | struct cpu_workqueue_stats *prev_cws = prev; | |
a3578000 | 175 | struct cpu_workqueue_stats *ret; |
e1d8aa9f FW |
176 | int cpu = prev_cws->cpu; |
177 | unsigned long flags; | |
e1d8aa9f | 178 | |
3690b5e6 LJ |
179 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
180 | if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) { | |
181 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); | |
bbcd3063 KM |
182 | do { |
183 | cpu = cpumask_next(cpu, cpu_possible_mask); | |
184 | if (cpu >= nr_cpu_ids) | |
185 | return NULL; | |
186 | } while (!(ret = workqueue_stat_start_cpu(cpu))); | |
187 | return ret; | |
a3578000 LJ |
188 | } else { |
189 | ret = list_entry(prev_cws->list.next, | |
190 | struct cpu_workqueue_stats, list); | |
191 | kref_get(&ret->kref); | |
e1d8aa9f | 192 | } |
3690b5e6 | 193 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f | 194 | |
a3578000 | 195 | return ret; |
e1d8aa9f FW |
196 | } |
197 | ||
198 | static int workqueue_stat_show(struct seq_file *s, void *p) | |
199 | { | |
200 | struct cpu_workqueue_stats *cws = p; | |
889a6c36 KM |
201 | struct pid *pid; |
202 | struct task_struct *tsk; | |
203 | ||
204 | pid = find_get_pid(cws->pid); | |
205 | if (pid) { | |
206 | tsk = get_pid_task(pid, PIDTYPE_PID); | |
207 | if (tsk) { | |
208 | seq_printf(s, "%3d %6d %6u %s\n", cws->cpu, | |
209 | atomic_read(&cws->inserted), cws->executed, | |
210 | tsk->comm); | |
211 | put_task_struct(tsk); | |
212 | } | |
213 | put_pid(pid); | |
214 | } | |
e1d8aa9f | 215 | |
e1d8aa9f FW |
216 | return 0; |
217 | } | |
218 | ||
a3578000 LJ |
219 | static void workqueue_stat_release(void *stat) |
220 | { | |
221 | struct cpu_workqueue_stats *node = stat; | |
222 | ||
223 | kref_put(&node->kref, cpu_workqueue_stat_free); | |
224 | } | |
225 | ||
e1d8aa9f FW |
226 | static int workqueue_stat_headers(struct seq_file *s) |
227 | { | |
228 | seq_printf(s, "# CPU INSERTED EXECUTED NAME\n"); | |
2f63b840 | 229 | seq_printf(s, "# | | | |\n"); |
e1d8aa9f FW |
230 | return 0; |
231 | } | |
232 | ||
233 | struct tracer_stat workqueue_stats __read_mostly = { | |
234 | .name = "workqueues", | |
235 | .stat_start = workqueue_stat_start, | |
236 | .stat_next = workqueue_stat_next, | |
237 | .stat_show = workqueue_stat_show, | |
a3578000 | 238 | .stat_release = workqueue_stat_release, |
e1d8aa9f FW |
239 | .stat_headers = workqueue_stat_headers |
240 | }; | |
241 | ||
242 | ||
243 | int __init stat_workqueue_init(void) | |
244 | { | |
245 | if (register_stat_tracer(&workqueue_stats)) { | |
246 | pr_warning("Unable to register workqueue stat tracer\n"); | |
247 | return 1; | |
248 | } | |
249 | ||
250 | return 0; | |
251 | } | |
252 | fs_initcall(stat_workqueue_init); | |
253 | ||
254 | /* | |
255 | * Workqueues are created very early, just after pre-smp initcalls. | |
256 | * So we must register our tracepoints at this stage. | |
257 | */ | |
258 | int __init trace_workqueue_early_init(void) | |
259 | { | |
260 | int ret, cpu; | |
261 | ||
262 | ret = register_trace_workqueue_insertion(probe_workqueue_insertion); | |
263 | if (ret) | |
264 | goto out; | |
265 | ||
266 | ret = register_trace_workqueue_execution(probe_workqueue_execution); | |
267 | if (ret) | |
268 | goto no_insertion; | |
269 | ||
270 | ret = register_trace_workqueue_creation(probe_workqueue_creation); | |
271 | if (ret) | |
272 | goto no_execution; | |
273 | ||
274 | ret = register_trace_workqueue_destruction(probe_workqueue_destruction); | |
275 | if (ret) | |
276 | goto no_creation; | |
277 | ||
e1d8aa9f | 278 | for_each_possible_cpu(cpu) { |
3690b5e6 LJ |
279 | spin_lock_init(&workqueue_cpu_stat(cpu)->lock); |
280 | INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list); | |
e1d8aa9f FW |
281 | } |
282 | ||
283 | return 0; | |
284 | ||
285 | no_creation: | |
286 | unregister_trace_workqueue_creation(probe_workqueue_creation); | |
287 | no_execution: | |
288 | unregister_trace_workqueue_execution(probe_workqueue_execution); | |
289 | no_insertion: | |
290 | unregister_trace_workqueue_insertion(probe_workqueue_insertion); | |
291 | out: | |
292 | pr_warning("trace_workqueue: unable to trace workqueues\n"); | |
293 | ||
294 | return 1; | |
295 | } | |
296 | early_initcall(trace_workqueue_early_init); |