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