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> |
a3578000 | 12 | #include <linux/kref.h> |
e1d8aa9f FW |
13 | #include "trace_stat.h" |
14 | #include "trace.h" | |
15 | ||
16 | ||
17 | /* A cpu workqueue thread */ | |
18 | struct cpu_workqueue_stats { | |
19 | struct list_head list; | |
a3578000 | 20 | struct kref kref; |
e1d8aa9f | 21 | int cpu; |
ef18012b | 22 | pid_t pid; |
e1d8aa9f | 23 | /* Can be inserted from interrupt or user context, need to be atomic */ |
ef18012b | 24 | atomic_t inserted; |
e1d8aa9f FW |
25 | /* |
26 | * Don't need to be atomic, works are serialized in a single workqueue thread | |
27 | * on a single CPU. | |
28 | */ | |
ef18012b | 29 | unsigned int executed; |
e1d8aa9f FW |
30 | }; |
31 | ||
32 | /* List of workqueue threads on one cpu */ | |
33 | struct 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 | */ | |
3690b5e6 LJ |
41 | static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat); |
42 | #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu)) | |
e1d8aa9f | 43 | |
a3578000 LJ |
44 | static void cpu_workqueue_stat_free(struct kref *kref) |
45 | { | |
46 | kfree(container_of(kref, struct cpu_workqueue_stats, kref)); | |
47 | } | |
48 | ||
e1d8aa9f FW |
49 | /* Insertion of a work */ |
50 | static void | |
51 | probe_workqueue_insertion(struct task_struct *wq_thread, | |
52 | struct work_struct *work) | |
53 | { | |
54 | int cpu = cpumask_first(&wq_thread->cpus_allowed); | |
1fdfca9c | 55 | struct cpu_workqueue_stats *node; |
e1d8aa9f FW |
56 | unsigned long flags; |
57 | ||
3690b5e6 | 58 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
1fdfca9c | 59 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) { |
e1d8aa9f FW |
60 | 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"); | |
66 | found: | |
3690b5e6 | 67 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
68 | } |
69 | ||
70 | /* Execution of a work */ | |
71 | static void | |
72 | probe_workqueue_execution(struct task_struct *wq_thread, | |
73 | struct work_struct *work) | |
74 | { | |
75 | int cpu = cpumask_first(&wq_thread->cpus_allowed); | |
1fdfca9c | 76 | struct cpu_workqueue_stats *node; |
e1d8aa9f FW |
77 | unsigned long flags; |
78 | ||
3690b5e6 | 79 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
1fdfca9c | 80 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) { |
e1d8aa9f FW |
81 | if (node->pid == wq_thread->pid) { |
82 | node->executed++; | |
83 | goto found; | |
84 | } | |
85 | } | |
86 | pr_debug("trace_workqueue: entry not found\n"); | |
87 | found: | |
3690b5e6 | 88 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
89 | } |
90 | ||
91 | /* Creation of a cpu workqueue thread */ | |
92 | static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu) | |
93 | { | |
94 | struct cpu_workqueue_stats *cws; | |
95 | unsigned long flags; | |
96 | ||
bbcd3063 | 97 | WARN_ON(cpu < 0); |
e1d8aa9f FW |
98 | |
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 | } | |
e1d8aa9f | 105 | INIT_LIST_HEAD(&cws->list); |
a3578000 | 106 | kref_init(&cws->kref); |
e1d8aa9f | 107 | cws->cpu = cpu; |
e1d8aa9f FW |
108 | cws->pid = wq_thread->pid; |
109 | ||
3690b5e6 | 110 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
3690b5e6 LJ |
111 | list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list); |
112 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); | |
e1d8aa9f FW |
113 | } |
114 | ||
115 | /* Destruction of a cpu workqueue thread */ | |
116 | static 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 | ||
3690b5e6 LJ |
123 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
124 | list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list, | |
e1d8aa9f FW |
125 | list) { |
126 | if (node->pid == wq_thread->pid) { | |
127 | list_del(&node->list); | |
a3578000 | 128 | kref_put(&node->kref, cpu_workqueue_stat_free); |
e1d8aa9f FW |
129 | goto found; |
130 | } | |
131 | } | |
132 | ||
133 | pr_debug("trace_workqueue: don't find workqueue to destroy\n"); | |
134 | found: | |
3690b5e6 | 135 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
136 | |
137 | } | |
138 | ||
139 | static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu) | |
140 | { | |
141 | unsigned long flags; | |
142 | struct cpu_workqueue_stats *ret = NULL; | |
143 | ||
144 | ||
3690b5e6 | 145 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f | 146 | |
a3578000 | 147 | if (!list_empty(&workqueue_cpu_stat(cpu)->list)) { |
3690b5e6 | 148 | ret = list_entry(workqueue_cpu_stat(cpu)->list.next, |
e1d8aa9f | 149 | struct cpu_workqueue_stats, list); |
a3578000 LJ |
150 | kref_get(&ret->kref); |
151 | } | |
e1d8aa9f | 152 | |
3690b5e6 | 153 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f FW |
154 | |
155 | return ret; | |
156 | } | |
157 | ||
42548008 | 158 | static void *workqueue_stat_start(struct tracer_stat *trace) |
e1d8aa9f FW |
159 | { |
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 | ||
171 | static void *workqueue_stat_next(void *prev, int idx) | |
172 | { | |
173 | struct cpu_workqueue_stats *prev_cws = prev; | |
a3578000 | 174 | struct cpu_workqueue_stats *ret; |
e1d8aa9f FW |
175 | int cpu = prev_cws->cpu; |
176 | unsigned long flags; | |
e1d8aa9f | 177 | |
3690b5e6 LJ |
178 | 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); | |
bbcd3063 KM |
181 | 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; | |
a3578000 LJ |
187 | } else { |
188 | ret = list_entry(prev_cws->list.next, | |
189 | struct cpu_workqueue_stats, list); | |
190 | kref_get(&ret->kref); | |
e1d8aa9f | 191 | } |
3690b5e6 | 192 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags); |
e1d8aa9f | 193 | |
a3578000 | 194 | return ret; |
e1d8aa9f FW |
195 | } |
196 | ||
197 | static int workqueue_stat_show(struct seq_file *s, void *p) | |
198 | { | |
199 | struct cpu_workqueue_stats *cws = p; | |
889a6c36 KM |
200 | struct pid *pid; |
201 | struct task_struct *tsk; | |
202 | ||
203 | 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 | } | |
e1d8aa9f | 214 | |
e1d8aa9f FW |
215 | return 0; |
216 | } | |
217 | ||
a3578000 LJ |
218 | static 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 | ||
e1d8aa9f FW |
225 | static int workqueue_stat_headers(struct seq_file *s) |
226 | { | |
227 | seq_printf(s, "# CPU INSERTED EXECUTED NAME\n"); | |
2f63b840 | 228 | seq_printf(s, "# | | | |\n"); |
e1d8aa9f FW |
229 | return 0; |
230 | } | |
231 | ||
232 | struct 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, | |
a3578000 | 237 | .stat_release = workqueue_stat_release, |
e1d8aa9f FW |
238 | .stat_headers = workqueue_stat_headers |
239 | }; | |
240 | ||
241 | ||
242 | int __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 | } | |
251 | fs_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 | */ | |
257 | int __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 | ||
e1d8aa9f | 277 | for_each_possible_cpu(cpu) { |
3690b5e6 LJ |
278 | spin_lock_init(&workqueue_cpu_stat(cpu)->lock); |
279 | INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list); | |
e1d8aa9f FW |
280 | } |
281 | ||
282 | return 0; | |
283 | ||
284 | no_creation: | |
285 | unregister_trace_workqueue_creation(probe_workqueue_creation); | |
286 | no_execution: | |
287 | unregister_trace_workqueue_execution(probe_workqueue_execution); | |
288 | no_insertion: | |
289 | unregister_trace_workqueue_insertion(probe_workqueue_insertion); | |
290 | out: | |
291 | pr_warning("trace_workqueue: unable to trace workqueues\n"); | |
292 | ||
293 | return 1; | |
294 | } | |
295 | early_initcall(trace_workqueue_early_init); |