]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/sputrace.c
powerpc/spufs: sputrace: Only enable logging on open(), prevent multiple openers
[linux-2.6-omap-h63xx.git] / arch / powerpc / platforms / cell / spufs / sputrace.c
1 /*
2  * Copyright (C) 2007 IBM Deutschland Entwicklung GmbH
3  *      Released under GPL v2.
4  *
5  * Partially based on net/ipv4/tcp_probe.c.
6  *
7  * Simple tracing facility for spu contexts.
8  */
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/marker.h>
13 #include <linux/proc_fs.h>
14 #include <linux/wait.h>
15 #include <asm/atomic.h>
16 #include <asm/uaccess.h>
17 #include "spufs.h"
18
19 struct spu_probe {
20         const char *name;
21         const char *format;
22         marker_probe_func *probe_func;
23 };
24
25 struct sputrace {
26         ktime_t tstamp;
27         int owner_tid; /* owner */
28         int curr_tid;
29         const char *name;
30         int number;
31 };
32
33 static int bufsize __read_mostly = 16384;
34 MODULE_PARM_DESC(bufsize, "Log buffer size (number of records)");
35 module_param(bufsize, int, 0);
36
37
38 static DEFINE_SPINLOCK(sputrace_lock);
39 static DECLARE_WAIT_QUEUE_HEAD(sputrace_wait);
40 static ktime_t sputrace_start;
41 static unsigned long sputrace_head, sputrace_tail;
42 static struct sputrace *sputrace_log;
43 static int sputrace_logging;
44
45 static int sputrace_used(void)
46 {
47         return (sputrace_head - sputrace_tail) % bufsize;
48 }
49
50 static inline int sputrace_avail(void)
51 {
52         return bufsize - sputrace_used();
53 }
54
55 static int sputrace_sprint(char *tbuf, int n)
56 {
57         const struct sputrace *t = sputrace_log + sputrace_tail % bufsize;
58         struct timespec tv =
59                 ktime_to_timespec(ktime_sub(t->tstamp, sputrace_start));
60
61         return snprintf(tbuf, n,
62                 "[%lu.%09lu] %d: %s (ctxthread = %d, spu = %d)\n",
63                 (unsigned long) tv.tv_sec,
64                 (unsigned long) tv.tv_nsec,
65                 t->curr_tid,
66                 t->name,
67                 t->owner_tid,
68                 t->number);
69 }
70
71 static ssize_t sputrace_read(struct file *file, char __user *buf,
72                 size_t len, loff_t *ppos)
73 {
74         int error = 0, cnt = 0;
75
76         if (!buf || len < 0)
77                 return -EINVAL;
78
79         while (cnt < len) {
80                 char tbuf[128];
81                 int width;
82
83                 error = wait_event_interruptible(sputrace_wait,
84                                                  sputrace_used() > 0);
85                 if (error)
86                         break;
87
88                 spin_lock(&sputrace_lock);
89                 if (sputrace_head == sputrace_tail) {
90                         spin_unlock(&sputrace_lock);
91                         continue;
92                 }
93
94                 width = sputrace_sprint(tbuf, sizeof(tbuf));
95                 if (width < len)
96                         sputrace_tail = (sputrace_tail + 1) % bufsize;
97                 spin_unlock(&sputrace_lock);
98
99                 if (width >= len)
100                         break;
101
102                 error = copy_to_user(buf + cnt, tbuf, width);
103                 if (error)
104                         break;
105                 cnt += width;
106         }
107
108         return cnt == 0 ? error : cnt;
109 }
110
111 static int sputrace_open(struct inode *inode, struct file *file)
112 {
113         int rc;
114
115         spin_lock(&sputrace_lock);
116         if (sputrace_logging) {
117                 rc = -EBUSY;
118                 goto out;
119         }
120
121         sputrace_logging = 1;
122         sputrace_head = sputrace_tail = 0;
123         sputrace_start = ktime_get();
124         rc = 0;
125
126 out:
127         spin_unlock(&sputrace_lock);
128         return rc;
129 }
130
131 static int sputrace_release(struct inode *inode, struct file *file)
132 {
133         spin_lock(&sputrace_lock);
134         sputrace_logging = 0;
135         spin_unlock(&sputrace_lock);
136         return 0;
137 }
138
139 static const struct file_operations sputrace_fops = {
140         .owner   = THIS_MODULE,
141         .open    = sputrace_open,
142         .read    = sputrace_read,
143         .release = sputrace_release,
144 };
145
146 static void sputrace_log_item(const char *name, struct spu_context *ctx,
147                 struct spu *spu)
148 {
149         spin_lock(&sputrace_lock);
150
151         if (!sputrace_logging) {
152                 spin_unlock(&sputrace_lock);
153                 return;
154         }
155
156         if (sputrace_avail() > 1) {
157                 struct sputrace *t = sputrace_log + sputrace_head;
158
159                 t->tstamp = ktime_get();
160                 t->owner_tid = ctx->tid;
161                 t->name = name;
162                 t->curr_tid = current->pid;
163                 t->number = spu ? spu->number : -1;
164
165                 sputrace_head = (sputrace_head + 1) % bufsize;
166         } else {
167                 printk(KERN_WARNING
168                        "sputrace: lost samples due to full buffer.\n");
169         }
170         spin_unlock(&sputrace_lock);
171
172         wake_up(&sputrace_wait);
173 }
174
175 static void spu_context_event(void *probe_private, void *call_data,
176                 const char *format, va_list *args)
177 {
178         struct spu_probe *p = probe_private;
179         struct spu_context *ctx;
180         struct spu *spu;
181
182         ctx = va_arg(*args, struct spu_context *);
183         spu = va_arg(*args, struct spu *);
184
185         sputrace_log_item(p->name, ctx, spu);
186 }
187
188 static void spu_context_nospu_event(void *probe_private, void *call_data,
189                 const char *format, va_list *args)
190 {
191         struct spu_probe *p = probe_private;
192         struct spu_context *ctx;
193
194         ctx = va_arg(*args, struct spu_context *);
195
196         sputrace_log_item(p->name, ctx, NULL);
197 }
198
199 struct spu_probe spu_probes[] = {
200         { "spu_bind_context__enter", "ctx %p spu %p", spu_context_event },
201         { "spu_unbind_context__enter", "ctx %p spu %p", spu_context_event },
202         { "spu_get_idle__enter", "ctx %p", spu_context_nospu_event },
203         { "spu_get_idle__found", "ctx %p spu %p", spu_context_event },
204         { "spu_get_idle__not_found", "ctx %p", spu_context_nospu_event },
205         { "spu_find_victim__enter", "ctx %p", spu_context_nospu_event },
206         { "spusched_tick__preempt", "ctx %p spu %p", spu_context_event },
207         { "spusched_tick__newslice", "ctx %p", spu_context_nospu_event },
208         { "spu_yield__enter", "ctx %p", spu_context_nospu_event },
209         { "spu_deactivate__enter", "ctx %p", spu_context_nospu_event },
210         { "__spu_deactivate__unload", "ctx %p spu %p", spu_context_event },
211         { "spufs_ps_fault__enter", "ctx %p", spu_context_nospu_event },
212         { "spufs_ps_fault__sleep", "ctx %p", spu_context_nospu_event },
213         { "spufs_ps_fault__wake", "ctx %p spu %p", spu_context_event },
214         { "spufs_ps_fault__insert", "ctx %p spu %p", spu_context_event },
215         { "spu_acquire_saved__enter", "ctx %p", spu_context_nospu_event },
216         { "destroy_spu_context__enter", "ctx %p", spu_context_nospu_event },
217         { "spufs_stop_callback__enter", "ctx %p spu %p", spu_context_event },
218 };
219
220 static int __init sputrace_init(void)
221 {
222         struct proc_dir_entry *entry;
223         int i, error = -ENOMEM;
224
225         sputrace_log = kcalloc(bufsize, sizeof(struct sputrace), GFP_KERNEL);
226         if (!sputrace_log)
227                 goto out;
228
229         entry = proc_create("sputrace", S_IRUSR, NULL, &sputrace_fops);
230         if (!entry)
231                 goto out_free_log;
232
233         for (i = 0; i < ARRAY_SIZE(spu_probes); i++) {
234                 struct spu_probe *p = &spu_probes[i];
235
236                 error = marker_probe_register(p->name, p->format,
237                                               p->probe_func, p);
238                 if (error)
239                         printk(KERN_INFO "Unable to register probe %s\n",
240                                         p->name);
241         }
242
243         return 0;
244
245 out_free_log:
246         kfree(sputrace_log);
247 out:
248         return -ENOMEM;
249 }
250
251 static void __exit sputrace_exit(void)
252 {
253         int i;
254
255         for (i = 0; i < ARRAY_SIZE(spu_probes); i++)
256                 marker_probe_unregister(spu_probes[i].name,
257                         spu_probes[i].probe_func, &spu_probes[i]);
258
259         remove_proc_entry("sputrace", NULL);
260         kfree(sputrace_log);
261 }
262
263 module_init(sputrace_init);
264 module_exit(sputrace_exit);
265
266 MODULE_LICENSE("GPL");