2 * Workqueue statistical tracer.
4 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
9 #include <trace/events/workqueue.h>
10 #include <linux/list.h>
11 #include <linux/percpu.h>
12 #include <linux/slab.h>
13 #include <linux/kref.h>
14 #include "trace_stat.h"
18 /* A cpu workqueue thread */
19 struct cpu_workqueue_stats
{
20 struct list_head list
;
24 /* Can be inserted from interrupt or user context, need to be atomic */
27 * Don't need to be atomic, works are serialized in a single workqueue thread
30 unsigned int executed
;
33 /* List of workqueue threads on one cpu */
34 struct workqueue_global_stats
{
35 struct list_head list
;
39 /* Don't need a global lock because allocated before the workqueues, and
42 static DEFINE_PER_CPU(struct workqueue_global_stats
, all_workqueue_stat
);
43 #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
45 static void cpu_workqueue_stat_free(struct kref
*kref
)
47 kfree(container_of(kref
, struct cpu_workqueue_stats
, kref
));
50 /* Insertion of a work */
52 probe_workqueue_insertion(void *ignore
,
53 struct task_struct
*wq_thread
,
54 struct work_struct
*work
)
56 int cpu
= cpumask_first(&wq_thread
->cpus_allowed
);
57 struct cpu_workqueue_stats
*node
;
60 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
61 list_for_each_entry(node
, &workqueue_cpu_stat(cpu
)->list
, list
) {
62 if (node
->pid
== wq_thread
->pid
) {
63 atomic_inc(&node
->inserted
);
67 pr_debug("trace_workqueue: entry not found\n");
69 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
72 /* Execution of a work */
74 probe_workqueue_execution(void *ignore
,
75 struct task_struct
*wq_thread
,
76 struct work_struct
*work
)
78 int cpu
= cpumask_first(&wq_thread
->cpus_allowed
);
79 struct cpu_workqueue_stats
*node
;
82 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
83 list_for_each_entry(node
, &workqueue_cpu_stat(cpu
)->list
, list
) {
84 if (node
->pid
== wq_thread
->pid
) {
89 pr_debug("trace_workqueue: entry not found\n");
91 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
94 /* Creation of a cpu workqueue thread */
95 static void probe_workqueue_creation(void *ignore
,
96 struct task_struct
*wq_thread
, int cpu
)
98 struct cpu_workqueue_stats
*cws
;
103 /* Workqueues are sometimes created in atomic context */
104 cws
= kzalloc(sizeof(struct cpu_workqueue_stats
), GFP_ATOMIC
);
106 pr_warning("trace_workqueue: not enough memory\n");
109 INIT_LIST_HEAD(&cws
->list
);
110 kref_init(&cws
->kref
);
112 cws
->pid
= wq_thread
->pid
;
114 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
115 list_add_tail(&cws
->list
, &workqueue_cpu_stat(cpu
)->list
);
116 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
119 /* Destruction of a cpu workqueue thread */
121 probe_workqueue_destruction(void *ignore
, struct task_struct
*wq_thread
)
123 /* Workqueue only execute on one cpu */
124 int cpu
= cpumask_first(&wq_thread
->cpus_allowed
);
125 struct cpu_workqueue_stats
*node
, *next
;
128 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
129 list_for_each_entry_safe(node
, next
, &workqueue_cpu_stat(cpu
)->list
,
131 if (node
->pid
== wq_thread
->pid
) {
132 list_del(&node
->list
);
133 kref_put(&node
->kref
, cpu_workqueue_stat_free
);
138 pr_debug("trace_workqueue: don't find workqueue to destroy\n");
140 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
144 static struct cpu_workqueue_stats
*workqueue_stat_start_cpu(int cpu
)
147 struct cpu_workqueue_stats
*ret
= NULL
;
150 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
152 if (!list_empty(&workqueue_cpu_stat(cpu
)->list
)) {
153 ret
= list_entry(workqueue_cpu_stat(cpu
)->list
.next
,
154 struct cpu_workqueue_stats
, list
);
155 kref_get(&ret
->kref
);
158 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
163 static void *workqueue_stat_start(struct tracer_stat
*trace
)
168 for_each_possible_cpu(cpu
) {
169 ret
= workqueue_stat_start_cpu(cpu
);
176 static void *workqueue_stat_next(void *prev
, int idx
)
178 struct cpu_workqueue_stats
*prev_cws
= prev
;
179 struct cpu_workqueue_stats
*ret
;
180 int cpu
= prev_cws
->cpu
;
183 spin_lock_irqsave(&workqueue_cpu_stat(cpu
)->lock
, flags
);
184 if (list_is_last(&prev_cws
->list
, &workqueue_cpu_stat(cpu
)->list
)) {
185 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
187 cpu
= cpumask_next(cpu
, cpu_possible_mask
);
188 if (cpu
>= nr_cpu_ids
)
190 } while (!(ret
= workqueue_stat_start_cpu(cpu
)));
193 ret
= list_entry(prev_cws
->list
.next
,
194 struct cpu_workqueue_stats
, list
);
195 kref_get(&ret
->kref
);
197 spin_unlock_irqrestore(&workqueue_cpu_stat(cpu
)->lock
, flags
);
202 static int workqueue_stat_show(struct seq_file
*s
, void *p
)
204 struct cpu_workqueue_stats
*cws
= p
;
206 struct task_struct
*tsk
;
208 pid
= find_get_pid(cws
->pid
);
210 tsk
= get_pid_task(pid
, PIDTYPE_PID
);
212 seq_printf(s
, "%3d %6d %6u %s\n", cws
->cpu
,
213 atomic_read(&cws
->inserted
), cws
->executed
,
215 put_task_struct(tsk
);
223 static void workqueue_stat_release(void *stat
)
225 struct cpu_workqueue_stats
*node
= stat
;
227 kref_put(&node
->kref
, cpu_workqueue_stat_free
);
230 static int workqueue_stat_headers(struct seq_file
*s
)
232 seq_printf(s
, "# CPU INSERTED EXECUTED NAME\n");
233 seq_printf(s
, "# | | | |\n");
237 struct tracer_stat workqueue_stats __read_mostly
= {
238 .name
= "workqueues",
239 .stat_start
= workqueue_stat_start
,
240 .stat_next
= workqueue_stat_next
,
241 .stat_show
= workqueue_stat_show
,
242 .stat_release
= workqueue_stat_release
,
243 .stat_headers
= workqueue_stat_headers
247 int __init
stat_workqueue_init(void)
249 if (register_stat_tracer(&workqueue_stats
)) {
250 pr_warning("Unable to register workqueue stat tracer\n");
256 fs_initcall(stat_workqueue_init
);
259 * Workqueues are created very early, just after pre-smp initcalls.
260 * So we must register our tracepoints at this stage.
262 int __init
trace_workqueue_early_init(void)
266 for_each_possible_cpu(cpu
) {
267 spin_lock_init(&workqueue_cpu_stat(cpu
)->lock
);
268 INIT_LIST_HEAD(&workqueue_cpu_stat(cpu
)->list
);
271 ret
= register_trace_workqueue_insertion(probe_workqueue_insertion
, NULL
);
275 ret
= register_trace_workqueue_execution(probe_workqueue_execution
, NULL
);
279 ret
= register_trace_workqueue_creation(probe_workqueue_creation
, NULL
);
283 ret
= register_trace_workqueue_destruction(probe_workqueue_destruction
, NULL
);
290 unregister_trace_workqueue_creation(probe_workqueue_creation
, NULL
);
292 unregister_trace_workqueue_execution(probe_workqueue_execution
, NULL
);
294 unregister_trace_workqueue_insertion(probe_workqueue_insertion
, NULL
);
296 pr_warning("trace_workqueue: unable to trace workqueues\n");
300 early_initcall(trace_workqueue_early_init
);