2 * Cell Broadband Engine OProfile Support
4 * (C) Copyright IBM Corporation 2006
6 * Author: Maynard Johnson <maynardj@us.ibm.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 /* The purpose of this file is to handle SPU event task switching
15 * and to record SPU context information into the OProfile
18 * Additionally, the spu_sync_buffer function is provided as a helper
19 * for recoding actual SPU program counter samples to the event buffer.
21 #include <linux/dcookies.h>
22 #include <linux/kref.h>
24 #include <linux/module.h>
25 #include <linux/notifier.h>
26 #include <linux/numa.h>
27 #include <linux/oprofile.h>
28 #include <linux/spinlock.h>
31 #define RELEASE_ALL 9999
33 static DEFINE_SPINLOCK(buffer_lock);
34 static DEFINE_SPINLOCK(cache_lock);
35 static int num_spu_nodes;
36 int spu_prof_num_nodes;
37 int last_guard_val[MAX_NUMNODES * 8];
39 /* Container for caching information about an active SPU task. */
41 struct vma_to_fileoffset_map *map;
42 struct spu *the_spu; /* needed to access pointer to local_store */
43 struct kref cache_ref;
46 static struct cached_info *spu_info[MAX_NUMNODES * 8];
48 static void destroy_cached_info(struct kref *kref)
50 struct cached_info *info;
52 info = container_of(kref, struct cached_info, cache_ref);
53 vma_map_free(info->map);
55 module_put(THIS_MODULE);
58 /* Return the cached_info for the passed SPU number.
59 * ATTENTION: Callers are responsible for obtaining the
60 * cache_lock if needed prior to invoking this function.
62 static struct cached_info *get_cached_info(struct spu *the_spu, int spu_num)
65 struct cached_info *ret_info;
67 if (spu_num >= num_spu_nodes) {
68 printk(KERN_ERR "SPU_PROF: "
69 "%s, line %d: Invalid index %d into spu info cache\n",
70 __FUNCTION__, __LINE__, spu_num);
74 if (!spu_info[spu_num] && the_spu) {
75 ref = spu_get_profile_private_kref(the_spu->ctx);
77 spu_info[spu_num] = container_of(ref, struct cached_info, cache_ref);
78 kref_get(&spu_info[spu_num]->cache_ref);
82 ret_info = spu_info[spu_num];
88 /* Looks for cached info for the passed spu. If not found, the
89 * cached info is created for the passed spu.
90 * Returns 0 for success; otherwise, -1 for error.
93 prepare_cached_spu_info(struct spu *spu, unsigned long objectId)
96 struct vma_to_fileoffset_map *new_map;
98 struct cached_info *info;
100 /* We won't bother getting cache_lock here since
101 * don't do anything with the cached_info that's returned.
103 info = get_cached_info(spu, spu->number);
106 pr_debug("Found cached SPU info.\n");
110 /* Create cached_info and set spu_info[spu->number] to point to it.
111 * spu->number is a system-wide value, not a per-node value.
113 info = kzalloc(sizeof(struct cached_info), GFP_KERNEL);
115 printk(KERN_ERR "SPU_PROF: "
116 "%s, line %d: create vma_map failed\n",
117 __FUNCTION__, __LINE__);
121 new_map = create_vma_map(spu, objectId);
123 printk(KERN_ERR "SPU_PROF: "
124 "%s, line %d: create vma_map failed\n",
125 __FUNCTION__, __LINE__);
130 pr_debug("Created vma_map\n");
133 kref_init(&info->cache_ref);
134 spin_lock_irqsave(&cache_lock, flags);
135 spu_info[spu->number] = info;
136 /* Increment count before passing off ref to SPUFS. */
137 kref_get(&info->cache_ref);
139 /* We increment the module refcount here since SPUFS is
140 * responsible for the final destruction of the cached_info,
141 * and it must be able to access the destroy_cached_info()
142 * function defined in the OProfile module. We decrement
143 * the module refcount in destroy_cached_info.
145 try_module_get(THIS_MODULE);
146 spu_set_profile_private_kref(spu->ctx, &info->cache_ref,
147 destroy_cached_info);
148 spin_unlock_irqrestore(&cache_lock, flags);
158 * NOTE: The caller is responsible for locking the
159 * cache_lock prior to calling this function.
161 static int release_cached_info(int spu_index)
165 if (spu_index == RELEASE_ALL) {
169 if (spu_index >= num_spu_nodes) {
170 printk(KERN_ERR "SPU_PROF: "
172 "Invalid index %d into spu info cache\n",
173 __FUNCTION__, __LINE__, spu_index);
179 for (; index < end; index++) {
180 if (spu_info[index]) {
181 kref_put(&spu_info[index]->cache_ref,
182 destroy_cached_info);
183 spu_info[index] = NULL;
191 /* The source code for fast_get_dcookie was "borrowed"
192 * from drivers/oprofile/buffer_sync.c.
195 /* Optimisation. We can manage without taking the dcookie sem
196 * because we cannot reach this code without at least one
197 * dcookie user still being registered (namely, the reader
198 * of the event buffer).
200 static inline unsigned long fast_get_dcookie(struct dentry *dentry,
201 struct vfsmount *vfsmnt)
203 unsigned long cookie;
205 if (dentry->d_cookie)
206 return (unsigned long)dentry;
207 get_dcookie(dentry, vfsmnt, &cookie);
211 /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
212 * which corresponds loosely to "application name". Also, determine
213 * the offset for the SPU ELF object. If computed offset is
214 * non-zero, it implies an embedded SPU object; otherwise, it's a
215 * separate SPU binary, in which case we retrieve it's dcookie.
216 * For the embedded case, we must determine if SPU ELF is embedded
217 * in the executable application or another file (i.e., shared lib).
218 * If embedded in a shared lib, we must get the dcookie and return
219 * that to the caller.
222 get_exec_dcookie_and_offset(struct spu *spu, unsigned int *offsetp,
223 unsigned long *spu_bin_dcookie,
224 unsigned long spu_ref)
226 unsigned long app_cookie = 0;
227 unsigned int my_offset = 0;
228 struct file *app = NULL;
229 struct vm_area_struct *vma;
230 struct mm_struct *mm = spu->mm;
235 down_read(&mm->mmap_sem);
237 for (vma = mm->mmap; vma; vma = vma->vm_next) {
240 if (!(vma->vm_flags & VM_EXECUTABLE))
242 app_cookie = fast_get_dcookie(vma->vm_file->f_dentry,
243 vma->vm_file->f_vfsmnt);
244 pr_debug("got dcookie for %s\n",
245 vma->vm_file->f_dentry->d_name.name);
250 for (vma = mm->mmap; vma; vma = vma->vm_next) {
251 if (vma->vm_start > spu_ref || vma->vm_end <= spu_ref)
253 my_offset = spu_ref - vma->vm_start;
255 goto fail_no_image_cookie;
257 pr_debug("Found spu ELF at %X(object-id:%lx) for file %s\n",
259 vma->vm_file->f_dentry->d_name.name);
260 *offsetp = my_offset;
264 *spu_bin_dcookie = fast_get_dcookie(vma->vm_file->f_dentry,
265 vma->vm_file->f_vfsmnt);
266 pr_debug("got dcookie for %s\n", vma->vm_file->f_dentry->d_name.name);
268 up_read(&mm->mmap_sem);
273 fail_no_image_cookie:
274 up_read(&mm->mmap_sem);
276 printk(KERN_ERR "SPU_PROF: "
277 "%s, line %d: Cannot find dcookie for SPU binary\n",
278 __FUNCTION__, __LINE__);
284 /* This function finds or creates cached context information for the
285 * passed SPU and records SPU context information into the OProfile
288 static int process_context_switch(struct spu *spu, unsigned long objectId)
292 unsigned int offset = 0;
293 unsigned long spu_cookie = 0, app_dcookie;
295 retval = prepare_cached_spu_info(spu, objectId);
299 /* Get dcookie first because a mutex_lock is taken in that
300 * code path, so interrupts must not be disabled.
302 app_dcookie = get_exec_dcookie_and_offset(spu, &offset, &spu_cookie, objectId);
303 if (!app_dcookie || !spu_cookie) {
308 /* Record context info in event buffer */
309 spin_lock_irqsave(&buffer_lock, flags);
310 add_event_entry(ESCAPE_CODE);
311 add_event_entry(SPU_CTX_SWITCH_CODE);
312 add_event_entry(spu->number);
313 add_event_entry(spu->pid);
314 add_event_entry(spu->tgid);
315 add_event_entry(app_dcookie);
316 add_event_entry(spu_cookie);
317 add_event_entry(offset);
318 spin_unlock_irqrestore(&buffer_lock, flags);
319 smp_wmb(); /* insure spu event buffer updates are written */
320 /* don't want entries intermingled... */
326 * This function is invoked on either a bind_context or unbind_context.
327 * If called for an unbind_context, the val arg is 0; otherwise,
328 * it is the object-id value for the spu context.
329 * The data arg is of type 'struct spu *'.
331 static int spu_active_notify(struct notifier_block *self, unsigned long val,
336 struct spu *the_spu = data;
338 pr_debug("SPU event notification arrived\n");
340 spin_lock_irqsave(&cache_lock, flags);
341 retval = release_cached_info(the_spu->number);
342 spin_unlock_irqrestore(&cache_lock, flags);
344 retval = process_context_switch(the_spu, val);
349 static struct notifier_block spu_active = {
350 .notifier_call = spu_active_notify,
353 static int number_of_online_nodes(void)
357 for_each_online_cpu(cpu) {
358 tmp = cbe_cpu_to_node(cpu) + 1;
365 /* The main purpose of this function is to synchronize
366 * OProfile with SPUFS by registering to be notified of
369 * NOTE: When profiling SPUs, we must ensure that only
370 * spu_sync_start is invoked and not the generic sync_start
371 * in drivers/oprofile/oprof.c. A return value of
372 * SKIP_GENERIC_SYNC or SYNC_START_ERROR will
375 int spu_sync_start(void)
378 int ret = SKIP_GENERIC_SYNC;
380 unsigned long flags = 0;
382 spu_prof_num_nodes = number_of_online_nodes();
383 num_spu_nodes = spu_prof_num_nodes * 8;
385 spin_lock_irqsave(&buffer_lock, flags);
386 add_event_entry(ESCAPE_CODE);
387 add_event_entry(SPU_PROFILING_CODE);
388 add_event_entry(num_spu_nodes);
389 spin_unlock_irqrestore(&buffer_lock, flags);
391 /* Register for SPU events */
392 register_ret = spu_switch_event_register(&spu_active);
394 ret = SYNC_START_ERROR;
398 for (k = 0; k < (MAX_NUMNODES * 8); k++)
399 last_guard_val[k] = 0;
400 pr_debug("spu_sync_start -- running.\n");
405 /* Record SPU program counter samples to the oprofile event buffer. */
406 void spu_sync_buffer(int spu_num, unsigned int *samples,
409 unsigned long long file_offset;
412 struct vma_to_fileoffset_map *map;
414 unsigned long long spu_num_ll = spu_num;
415 unsigned long long spu_num_shifted = spu_num_ll << 32;
416 struct cached_info *c_info;
418 /* We need to obtain the cache_lock here because it's
419 * possible that after getting the cached_info, the SPU job
420 * corresponding to this cached_info may end, thus resulting
421 * in the destruction of the cached_info.
423 spin_lock_irqsave(&cache_lock, flags);
424 c_info = get_cached_info(NULL, spu_num);
426 /* This legitimately happens when the SPU task ends before all
427 * samples are recorded.
428 * No big deal -- so we just drop a few samples.
430 pr_debug("SPU_PROF: No cached SPU contex "
431 "for SPU #%d. Dropping samples.\n", spu_num);
436 the_spu = c_info->the_spu;
437 spin_lock(&buffer_lock);
438 for (i = 0; i < num_samples; i++) {
439 unsigned int sample = *(samples+i);
444 file_offset = vma_map_lookup( map, sample, the_spu, &grd_val);
446 /* If overlays are used by this SPU application, the guard
447 * value is non-zero, indicating which overlay section is in
448 * use. We need to discard samples taken during the time
449 * period which an overlay occurs (i.e., guard value changes).
451 if (grd_val && grd_val != last_guard_val[spu_num]) {
452 last_guard_val[spu_num] = grd_val;
453 /* Drop the rest of the samples. */
457 add_event_entry(file_offset | spu_num_shifted);
459 spin_unlock(&buffer_lock);
461 spin_unlock_irqrestore(&cache_lock, flags);
465 int spu_sync_stop(void)
467 unsigned long flags = 0;
468 int ret = spu_switch_event_unregister(&spu_active);
470 printk(KERN_ERR "SPU_PROF: "
471 "%s, line %d: spu_switch_event_unregister returned %d\n",
472 __FUNCTION__, __LINE__, ret);
476 spin_lock_irqsave(&cache_lock, flags);
477 ret = release_cached_info(RELEASE_ALL);
478 spin_unlock_irqrestore(&cache_lock, flags);
480 pr_debug("spu_sync_stop -- done.\n");