| 1 | |
| 2 | * Workqueue statistical tracer.
|
| 3 | *
|
| 4 | * Copyright (C) 2008 Frederic Weisbecker <[email protected]>
|
| 5 | *
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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"
|
| 15 | #include "trace.h"
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 | struct cpu_workqueue_stats {
|
| 20 | struct list_head list;
|
| 21 | struct kref kref;
|
| 22 | int cpu;
|
| 23 | pid_t pid;
|
| 24 |
|
| 25 | atomic_t inserted;
|
| 26 | |
| 27 | * Don't need to be atomic, works are serialized in a single workqueue thread
|
| 28 | * on a single CPU.
|
| 29 |
|
| 30 | unsigned int executed;
|
| 31 | };
|
| 32 |
|
| 33 |
|
| 34 | struct workqueue_global_stats {
|
| 35 | struct list_head list;
|
| 36 | spinlock_t lock;
|
| 37 | };
|
| 38 |
|
| 39 | |
| 40 | * never freed.
|
| 41 |
|
| 42 | static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat);
|
| 43 | #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
|
| 44 |
|
| 45 | static void cpu_workqueue_stat_free(struct kref *kref)
|
| 46 | {
|
| 47 | kfree(container_of(kref, struct cpu_workqueue_stats, kref));
|
| 48 | }
|
| 49 |
|
| 50 |
|
| 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);
|
| 56 | struct cpu_workqueue_stats *node;
|
| 57 | unsigned long flags;
|
| 58 |
|
| 59 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 60 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
|
| 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:
|
| 68 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 69 | }
|
| 70 |
|
| 71 |
|
| 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);
|
| 77 | struct cpu_workqueue_stats *node;
|
| 78 | unsigned long flags;
|
| 79 |
|
| 80 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 81 | list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
|
| 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:
|
| 89 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 90 | }
|
| 91 |
|
| 92 |
|
| 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 |
|
| 98 | WARN_ON(cpu < 0);
|
| 99 |
|
| 100 |
|
| 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 | }
|
| 106 | INIT_LIST_HEAD(&cws->list);
|
| 107 | kref_init(&cws->kref);
|
| 108 | cws->cpu = cpu;
|
| 109 | cws->pid = wq_thread->pid;
|
| 110 |
|
| 111 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 112 | list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
|
| 113 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 114 | }
|
| 115 |
|
| 116 |
|
| 117 | static void probe_workqueue_destruction(struct task_struct *wq_thread)
|
| 118 | {
|
| 119 |
|
| 120 | int cpu = cpumask_first(&wq_thread->cpus_allowed);
|
| 121 | struct cpu_workqueue_stats *node, *next;
|
| 122 | unsigned long flags;
|
| 123 |
|
| 124 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 125 | list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
|
| 126 | list) {
|
| 127 | if (node->pid == wq_thread->pid) {
|
| 128 | list_del(&node->list);
|
| 129 | kref_put(&node->kref, cpu_workqueue_stat_free);
|
| 130 | goto found;
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | pr_debug("trace_workqueue: don't find workqueue to destroy\n");
|
| 135 | found:
|
| 136 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 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 |
|
| 146 | spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 147 |
|
| 148 | if (!list_empty(&workqueue_cpu_stat(cpu)->list)) {
|
| 149 | ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
|
| 150 | struct cpu_workqueue_stats, list);
|
| 151 | kref_get(&ret->kref);
|
| 152 | }
|
| 153 |
|
| 154 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 155 |
|
| 156 | return ret;
|
| 157 | }
|
| 158 |
|
| 159 | static void *workqueue_stat_start(struct tracer_stat *trace)
|
| 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;
|
| 175 | struct cpu_workqueue_stats *ret;
|
| 176 | int cpu = prev_cws->cpu;
|
| 177 | unsigned long flags;
|
| 178 |
|
| 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);
|
| 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;
|
| 188 | } else {
|
| 189 | ret = list_entry(prev_cws->list.next,
|
| 190 | struct cpu_workqueue_stats, list);
|
| 191 | kref_get(&ret->kref);
|
| 192 | }
|
| 193 | spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
|
| 194 |
|
| 195 | return ret;
|
| 196 | }
|
| 197 |
|
| 198 | static int workqueue_stat_show(struct seq_file *s, void *p)
|
| 199 | {
|
| 200 | struct cpu_workqueue_stats *cws = p;
|
| 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 | }
|
| 215 |
|
| 216 | return 0;
|
| 217 | }
|
| 218 |
|
| 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 |
|
| 226 | static int workqueue_stat_headers(struct seq_file *s)
|
| 227 | {
|
| 228 | seq_printf(s, "# CPU INSERTED EXECUTED NAME\n");
|
| 229 | seq_printf(s, "# | | | |\n");
|
| 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,
|
| 238 | .stat_release = workqueue_stat_release,
|
| 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 |
|
| 278 | for_each_possible_cpu(cpu) {
|
| 279 | spin_lock_init(&workqueue_cpu_stat(cpu)->lock);
|
| 280 | INIT_LIST_HEAD(&workqueue_cpu_stat(cpu)->list);
|
| 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);
|