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