mmiotrace: count events lost due to not recording
[linux-2.6] / kernel / trace / trace_mmiotrace.c
1 /*
2  * Memory mapped I/O tracing
3  *
4  * Copyright (C) 2008 Pekka Paalanen <pq@iki.fi>
5  */
6
7 #define DEBUG 1
8
9 #include <linux/kernel.h>
10 #include <linux/mmiotrace.h>
11 #include <linux/pci.h>
12 #include <asm/atomic.h>
13
14 #include "trace.h"
15 #include "trace_output.h"
16
17 struct header_iter {
18         struct pci_dev *dev;
19 };
20
21 static struct trace_array *mmio_trace_array;
22 static bool overrun_detected;
23 static unsigned long prev_overruns;
24 static atomic_t dropped_count;
25
26 static void mmio_reset_data(struct trace_array *tr)
27 {
28         overrun_detected = false;
29         prev_overruns = 0;
30
31         tracing_reset_online_cpus(tr);
32 }
33
34 static int mmio_trace_init(struct trace_array *tr)
35 {
36         pr_debug("in %s\n", __func__);
37         mmio_trace_array = tr;
38
39         mmio_reset_data(tr);
40         enable_mmiotrace();
41         return 0;
42 }
43
44 static void mmio_trace_reset(struct trace_array *tr)
45 {
46         pr_debug("in %s\n", __func__);
47
48         disable_mmiotrace();
49         mmio_reset_data(tr);
50         mmio_trace_array = NULL;
51 }
52
53 static void mmio_trace_start(struct trace_array *tr)
54 {
55         pr_debug("in %s\n", __func__);
56         mmio_reset_data(tr);
57 }
58
59 static int mmio_print_pcidev(struct trace_seq *s, const struct pci_dev *dev)
60 {
61         int ret = 0;
62         int i;
63         resource_size_t start, end;
64         const struct pci_driver *drv = pci_dev_driver(dev);
65
66         /* XXX: incomplete checks for trace_seq_printf() return value */
67         ret += trace_seq_printf(s, "PCIDEV %02x%02x %04x%04x %x",
68                                 dev->bus->number, dev->devfn,
69                                 dev->vendor, dev->device, dev->irq);
70         /*
71          * XXX: is pci_resource_to_user() appropriate, since we are
72          * supposed to interpret the __ioremap() phys_addr argument based on
73          * these printed values?
74          */
75         for (i = 0; i < 7; i++) {
76                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
77                 ret += trace_seq_printf(s, " %llx",
78                         (unsigned long long)(start |
79                         (dev->resource[i].flags & PCI_REGION_FLAG_MASK)));
80         }
81         for (i = 0; i < 7; i++) {
82                 pci_resource_to_user(dev, i, &dev->resource[i], &start, &end);
83                 ret += trace_seq_printf(s, " %llx",
84                         dev->resource[i].start < dev->resource[i].end ?
85                         (unsigned long long)(end - start) + 1 : 0);
86         }
87         if (drv)
88                 ret += trace_seq_printf(s, " %s\n", drv->name);
89         else
90                 ret += trace_seq_printf(s, " \n");
91         return ret;
92 }
93
94 static void destroy_header_iter(struct header_iter *hiter)
95 {
96         if (!hiter)
97                 return;
98         pci_dev_put(hiter->dev);
99         kfree(hiter);
100 }
101
102 static void mmio_pipe_open(struct trace_iterator *iter)
103 {
104         struct header_iter *hiter;
105         struct trace_seq *s = &iter->seq;
106
107         trace_seq_printf(s, "VERSION 20070824\n");
108
109         hiter = kzalloc(sizeof(*hiter), GFP_KERNEL);
110         if (!hiter)
111                 return;
112
113         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
114         iter->private = hiter;
115 }
116
117 /* XXX: This is not called when the pipe is closed! */
118 static void mmio_close(struct trace_iterator *iter)
119 {
120         struct header_iter *hiter = iter->private;
121         destroy_header_iter(hiter);
122         iter->private = NULL;
123 }
124
125 static unsigned long count_overruns(struct trace_iterator *iter)
126 {
127         unsigned long cnt = atomic_xchg(&dropped_count, 0);
128         unsigned long over = ring_buffer_overruns(iter->tr->buffer);
129
130         if (over > prev_overruns)
131                 cnt += over - prev_overruns;
132         prev_overruns = over;
133         return cnt;
134 }
135
136 static ssize_t mmio_read(struct trace_iterator *iter, struct file *filp,
137                                 char __user *ubuf, size_t cnt, loff_t *ppos)
138 {
139         ssize_t ret;
140         struct header_iter *hiter = iter->private;
141         struct trace_seq *s = &iter->seq;
142         unsigned long n;
143
144         n = count_overruns(iter);
145         if (n) {
146                 /* XXX: This is later than where events were lost. */
147                 trace_seq_printf(s, "MARK 0.000000 Lost %lu events.\n", n);
148                 if (!overrun_detected)
149                         pr_warning("mmiotrace has lost events.\n");
150                 overrun_detected = true;
151                 goto print_out;
152         }
153
154         if (!hiter)
155                 return 0;
156
157         mmio_print_pcidev(s, hiter->dev);
158         hiter->dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, hiter->dev);
159
160         if (!hiter->dev) {
161                 destroy_header_iter(hiter);
162                 iter->private = NULL;
163         }
164
165 print_out:
166         ret = trace_seq_to_user(s, ubuf, cnt);
167         return (ret == -EBUSY) ? 0 : ret;
168 }
169
170 static enum print_line_t mmio_print_rw(struct trace_iterator *iter)
171 {
172         struct trace_entry *entry = iter->ent;
173         struct trace_mmiotrace_rw *field;
174         struct mmiotrace_rw *rw;
175         struct trace_seq *s     = &iter->seq;
176         unsigned long long t    = ns2usecs(iter->ts);
177         unsigned long usec_rem  = do_div(t, 1000000ULL);
178         unsigned secs           = (unsigned long)t;
179         int ret = 1;
180
181         trace_assign_type(field, entry);
182         rw = &field->rw;
183
184         switch (rw->opcode) {
185         case MMIO_READ:
186                 ret = trace_seq_printf(s,
187                         "R %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
188                         rw->width, secs, usec_rem, rw->map_id,
189                         (unsigned long long)rw->phys,
190                         rw->value, rw->pc, 0);
191                 break;
192         case MMIO_WRITE:
193                 ret = trace_seq_printf(s,
194                         "W %d %lu.%06lu %d 0x%llx 0x%lx 0x%lx %d\n",
195                         rw->width, secs, usec_rem, rw->map_id,
196                         (unsigned long long)rw->phys,
197                         rw->value, rw->pc, 0);
198                 break;
199         case MMIO_UNKNOWN_OP:
200                 ret = trace_seq_printf(s,
201                         "UNKNOWN %lu.%06lu %d 0x%llx %02x,%02x,%02x 0x%lx %d\n",
202                         secs, usec_rem, rw->map_id,
203                         (unsigned long long)rw->phys,
204                         (rw->value >> 16) & 0xff, (rw->value >> 8) & 0xff,
205                         (rw->value >> 0) & 0xff, rw->pc, 0);
206                 break;
207         default:
208                 ret = trace_seq_printf(s, "rw what?\n");
209                 break;
210         }
211         if (ret)
212                 return TRACE_TYPE_HANDLED;
213         return TRACE_TYPE_PARTIAL_LINE;
214 }
215
216 static enum print_line_t mmio_print_map(struct trace_iterator *iter)
217 {
218         struct trace_entry *entry = iter->ent;
219         struct trace_mmiotrace_map *field;
220         struct mmiotrace_map *m;
221         struct trace_seq *s     = &iter->seq;
222         unsigned long long t    = ns2usecs(iter->ts);
223         unsigned long usec_rem  = do_div(t, 1000000ULL);
224         unsigned secs           = (unsigned long)t;
225         int ret;
226
227         trace_assign_type(field, entry);
228         m = &field->map;
229
230         switch (m->opcode) {
231         case MMIO_PROBE:
232                 ret = trace_seq_printf(s,
233                         "MAP %lu.%06lu %d 0x%llx 0x%lx 0x%lx 0x%lx %d\n",
234                         secs, usec_rem, m->map_id,
235                         (unsigned long long)m->phys, m->virt, m->len,
236                         0UL, 0);
237                 break;
238         case MMIO_UNPROBE:
239                 ret = trace_seq_printf(s,
240                         "UNMAP %lu.%06lu %d 0x%lx %d\n",
241                         secs, usec_rem, m->map_id, 0UL, 0);
242                 break;
243         default:
244                 ret = trace_seq_printf(s, "map what?\n");
245                 break;
246         }
247         if (ret)
248                 return TRACE_TYPE_HANDLED;
249         return TRACE_TYPE_PARTIAL_LINE;
250 }
251
252 static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
253 {
254         struct trace_entry *entry = iter->ent;
255         struct print_entry *print = (struct print_entry *)entry;
256         const char *msg         = print->buf;
257         struct trace_seq *s     = &iter->seq;
258         unsigned long long t    = ns2usecs(iter->ts);
259         unsigned long usec_rem  = do_div(t, 1000000ULL);
260         unsigned secs           = (unsigned long)t;
261         int ret;
262
263         /* The trailing newline must be in the message. */
264         ret = trace_seq_printf(s, "MARK %lu.%06lu %s", secs, usec_rem, msg);
265         if (!ret)
266                 return TRACE_TYPE_PARTIAL_LINE;
267
268         return TRACE_TYPE_HANDLED;
269 }
270
271 static enum print_line_t mmio_print_line(struct trace_iterator *iter)
272 {
273         switch (iter->ent->type) {
274         case TRACE_MMIO_RW:
275                 return mmio_print_rw(iter);
276         case TRACE_MMIO_MAP:
277                 return mmio_print_map(iter);
278         case TRACE_PRINT:
279                 return mmio_print_mark(iter);
280         default:
281                 return TRACE_TYPE_HANDLED; /* ignore unknown entries */
282         }
283 }
284
285 static struct tracer mmio_tracer __read_mostly =
286 {
287         .name           = "mmiotrace",
288         .init           = mmio_trace_init,
289         .reset          = mmio_trace_reset,
290         .start          = mmio_trace_start,
291         .pipe_open      = mmio_pipe_open,
292         .close          = mmio_close,
293         .read           = mmio_read,
294         .print_line     = mmio_print_line,
295 };
296
297 __init static int init_mmio_trace(void)
298 {
299         return register_tracer(&mmio_tracer);
300 }
301 device_initcall(init_mmio_trace);
302
303 static void __trace_mmiotrace_rw(struct trace_array *tr,
304                                 struct trace_array_cpu *data,
305                                 struct mmiotrace_rw *rw)
306 {
307         struct ring_buffer_event *event;
308         struct trace_mmiotrace_rw *entry;
309         unsigned long irq_flags;
310
311         event   = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
312                                            &irq_flags);
313         if (!event) {
314                 atomic_inc(&dropped_count);
315                 return;
316         }
317         entry   = ring_buffer_event_data(event);
318         tracing_generic_entry_update(&entry->ent, 0, preempt_count());
319         entry->ent.type                 = TRACE_MMIO_RW;
320         entry->rw                       = *rw;
321         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
322
323         trace_wake_up();
324 }
325
326 void mmio_trace_rw(struct mmiotrace_rw *rw)
327 {
328         struct trace_array *tr = mmio_trace_array;
329         struct trace_array_cpu *data = tr->data[smp_processor_id()];
330         __trace_mmiotrace_rw(tr, data, rw);
331 }
332
333 static void __trace_mmiotrace_map(struct trace_array *tr,
334                                 struct trace_array_cpu *data,
335                                 struct mmiotrace_map *map)
336 {
337         struct ring_buffer_event *event;
338         struct trace_mmiotrace_map *entry;
339         unsigned long irq_flags;
340
341         event   = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
342                                            &irq_flags);
343         if (!event) {
344                 atomic_inc(&dropped_count);
345                 return;
346         }
347         entry   = ring_buffer_event_data(event);
348         tracing_generic_entry_update(&entry->ent, 0, preempt_count());
349         entry->ent.type                 = TRACE_MMIO_MAP;
350         entry->map                      = *map;
351         ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
352
353         trace_wake_up();
354 }
355
356 void mmio_trace_mapping(struct mmiotrace_map *map)
357 {
358         struct trace_array *tr = mmio_trace_array;
359         struct trace_array_cpu *data;
360
361         preempt_disable();
362         data = tr->data[smp_processor_id()];
363         __trace_mmiotrace_map(tr, data, map);
364         preempt_enable();
365 }
366
367 int mmio_trace_printk(const char *fmt, va_list args)
368 {
369         return trace_vprintk(0, -1, fmt, args);
370 }