]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/platforms/cell/spufs/sputrace.c
2b1953f6f12e0a7bdab67267fd4ac7b6f8f6e7d8
[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
44 static int sputrace_used(void)
45 {
46         return (sputrace_head - sputrace_tail) % bufsize;
47 }
48
49 static inline int sputrace_avail(void)
50 {
51         return bufsize - sputrace_used();
52 }
53
54 static int sputrace_sprint(char *tbuf, int n)
55 {
56         const struct sputrace *t = sputrace_log + sputrace_tail % bufsize;
57         struct timespec tv =
58                 ktime_to_timespec(ktime_sub(t->tstamp, sputrace_start));
59
60         return snprintf(tbuf, n,
61                 "[%lu.%09lu] %d: %s (thread = %d, spu = %d)\n",
62                 (unsigned long) tv.tv_sec,
63                 (unsigned long) tv.tv_nsec,
64                 t->owner_tid,
65                 t->name,
66                 t->curr_tid,
67                 t->number);
68 }
69
70 static ssize_t sputrace_read(struct file *file, char __user *buf,
71                 size_t len, loff_t *ppos)
72 {
73         int error = 0, cnt = 0;
74
75         if (!buf || len < 0)
76                 return -EINVAL;
77
78         while (cnt < len) {
79                 char tbuf[128];
80                 int width;
81
82                 error = wait_event_interruptible(sputrace_wait,
83                                                  sputrace_used() > 0);
84                 if (error)
85                         break;
86
87                 spin_lock(&sputrace_lock);
88                 if (sputrace_head == sputrace_tail) {
89                         spin_unlock(&sputrace_lock);
90                         continue;
91                 }
92
93                 width = sputrace_sprint(tbuf, sizeof(tbuf));
94                 if (width < len)
95                         sputrace_tail = (sputrace_tail + 1) % bufsize;
96                 spin_unlock(&sputrace_lock);
97
98                 if (width >= len)
99                         break;
100
101                 error = copy_to_user(buf + cnt, tbuf, width);
102                 if (error)
103                         break;
104                 cnt += width;
105         }
106
107         return cnt == 0 ? error : cnt;
108 }
109
110 static int sputrace_open(struct inode *inode, struct file *file)
111 {
112         spin_lock(&sputrace_lock);
113         sputrace_head = sputrace_tail = 0;
114         sputrace_start = ktime_get();
115         spin_unlock(&sputrace_lock);
116
117         return 0;
118 }
119
120 static const struct file_operations sputrace_fops = {
121         .owner  = THIS_MODULE,
122         .open   = sputrace_open,
123         .read   = sputrace_read,
124 };
125
126 static void sputrace_log_item(const char *name, struct spu_context *ctx,
127                 struct spu *spu)
128 {
129         spin_lock(&sputrace_lock);
130         if (sputrace_avail() > 1) {
131                 struct sputrace *t = sputrace_log + sputrace_head;
132
133                 t->tstamp = ktime_get();
134                 t->owner_tid = ctx->tid;
135                 t->name = name;
136                 t->curr_tid = current->pid;
137                 t->number = spu ? spu->number : -1;
138
139                 sputrace_head = (sputrace_head + 1) % bufsize;
140         } else {
141                 printk(KERN_WARNING
142                        "sputrace: lost samples due to full buffer.\n");
143         }
144         spin_unlock(&sputrace_lock);
145
146         wake_up(&sputrace_wait);
147 }
148
149 static void spu_context_event(const struct marker *mdata,
150                 void *private, const char *format, ...)
151 {
152         struct spu_probe *p = mdata->private;
153         va_list ap;
154         struct spu_context *ctx;
155         struct spu *spu;
156
157         va_start(ap, format);
158         ctx = va_arg(ap, struct spu_context *);
159         spu = va_arg(ap, struct spu *);
160
161         sputrace_log_item(p->name, ctx, spu);
162         va_end(ap);
163 }
164
165 static void spu_context_nospu_event(const struct marker *mdata,
166                 void *private, const char *format, ...)
167 {
168         struct spu_probe *p = mdata->private;
169         va_list ap;
170         struct spu_context *ctx;
171
172         va_start(ap, format);
173         ctx = va_arg(ap, struct spu_context *);
174
175         sputrace_log_item(p->name, ctx, NULL);
176         va_end(ap);
177 }
178
179 struct spu_probe spu_probes[] = {
180         { "spu_bind_context__enter", "%p %p", spu_context_event },
181         { "spu_unbind_context__enter", "%p %p", spu_context_event },
182         { "spu_get_idle__enter", "%p", spu_context_nospu_event },
183         { "spu_get_idle__found", "%p %p", spu_context_event },
184         { "spu_get_idle__not_found", "%p", spu_context_nospu_event },
185         { "spu_find_victim__enter", "%p", spu_context_nospu_event },
186         { "spusched_tick__preempt", "%p %p", spu_context_event },
187         { "spusched_tick__newslice", "%p", spu_context_nospu_event },
188         { "spu_yield__enter", "%p", spu_context_nospu_event },
189         { "spu_deactivate__enter", "%p", spu_context_nospu_event },
190         { "__spu_deactivate__unload", "%p %p", spu_context_event },
191         { "spufs_ps_nopfn__enter", "%p", spu_context_nospu_event },
192         { "spufs_ps_nopfn__sleep", "%p", spu_context_nospu_event },
193         { "spufs_ps_nopfn__wake", "%p %p", spu_context_event },
194         { "spufs_ps_nopfn__insert", "%p %p", spu_context_event },
195         { "spu_acquire_saved__enter", "%p", spu_context_nospu_event },
196         { "destroy_spu_context__enter", "%p", spu_context_nospu_event },
197 };
198
199 static int __init sputrace_init(void)
200 {
201         struct proc_dir_entry *entry;
202         int i, error = -ENOMEM;
203
204         sputrace_log = kcalloc(sizeof(struct sputrace),
205                                 bufsize, GFP_KERNEL);
206         if (!sputrace_log)
207                 goto out;
208
209         entry = create_proc_entry("sputrace", S_IRUSR, NULL);
210         if (!entry)
211                 goto out_free_log;
212         entry->proc_fops = &sputrace_fops;
213
214         for (i = 0; i < ARRAY_SIZE(spu_probes); i++) {
215                 struct spu_probe *p = &spu_probes[i];
216
217                 error = marker_probe_register(p->name, p->format,
218                                               p->probe_func, p);
219                 if (error)
220                         printk(KERN_INFO "Unable to register probe %s\n",
221                                         p->name);
222
223                 error = marker_arm(p->name);
224                 if (error)
225                         printk(KERN_INFO "Unable to arm probe %s\n", p->name);
226         }
227
228         return 0;
229
230 out_free_log:
231         kfree(sputrace_log);
232 out:
233         return -ENOMEM;
234 }
235
236 static void __exit sputrace_exit(void)
237 {
238         int i;
239
240         for (i = 0; i < ARRAY_SIZE(spu_probes); i++)
241                 marker_probe_unregister(spu_probes[i].name);
242
243         remove_proc_entry("sputrace", NULL);
244         kfree(sputrace_log);
245 }
246
247 module_init(sputrace_init);
248 module_exit(sputrace_exit);
249
250 MODULE_LICENSE("GPL");