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>
25 #include <linux/module.h>
26 #include <linux/notifier.h>
27 #include <linux/numa.h>
28 #include <linux/oprofile.h>
29 #include <linux/spinlock.h>
32 #define RELEASE_ALL 9999
34 static DEFINE_SPINLOCK(buffer_lock
);
35 static DEFINE_SPINLOCK(cache_lock
);
36 static int num_spu_nodes
;
37 int spu_prof_num_nodes
;
38 int last_guard_val
[MAX_NUMNODES
* 8];
40 /* Container for caching information about an active SPU task. */
42 struct vma_to_fileoffset_map
*map
;
43 struct spu
*the_spu
; /* needed to access pointer to local_store */
44 struct kref cache_ref
;
47 static struct cached_info
*spu_info
[MAX_NUMNODES
* 8];
49 static void destroy_cached_info(struct kref
*kref
)
51 struct cached_info
*info
;
53 info
= container_of(kref
, struct cached_info
, cache_ref
);
54 vma_map_free(info
->map
);
56 module_put(THIS_MODULE
);
59 /* Return the cached_info for the passed SPU number.
60 * ATTENTION: Callers are responsible for obtaining the
61 * cache_lock if needed prior to invoking this function.
63 static struct cached_info
*get_cached_info(struct spu
*the_spu
, int spu_num
)
66 struct cached_info
*ret_info
;
68 if (spu_num
>= num_spu_nodes
) {
69 printk(KERN_ERR
"SPU_PROF: "
70 "%s, line %d: Invalid index %d into spu info cache\n",
71 __FUNCTION__
, __LINE__
, spu_num
);
75 if (!spu_info
[spu_num
] && the_spu
) {
76 ref
= spu_get_profile_private_kref(the_spu
->ctx
);
78 spu_info
[spu_num
] = container_of(ref
, struct cached_info
, cache_ref
);
79 kref_get(&spu_info
[spu_num
]->cache_ref
);
83 ret_info
= spu_info
[spu_num
];
89 /* Looks for cached info for the passed spu. If not found, the
90 * cached info is created for the passed spu.
91 * Returns 0 for success; otherwise, -1 for error.
94 prepare_cached_spu_info(struct spu
*spu
, unsigned long objectId
)
97 struct vma_to_fileoffset_map
*new_map
;
99 struct cached_info
*info
;
101 /* We won't bother getting cache_lock here since
102 * don't do anything with the cached_info that's returned.
104 info
= get_cached_info(spu
, spu
->number
);
107 pr_debug("Found cached SPU info.\n");
111 /* Create cached_info and set spu_info[spu->number] to point to it.
112 * spu->number is a system-wide value, not a per-node value.
114 info
= kzalloc(sizeof(struct cached_info
), GFP_KERNEL
);
116 printk(KERN_ERR
"SPU_PROF: "
117 "%s, line %d: create vma_map failed\n",
118 __FUNCTION__
, __LINE__
);
122 new_map
= create_vma_map(spu
, objectId
);
124 printk(KERN_ERR
"SPU_PROF: "
125 "%s, line %d: create vma_map failed\n",
126 __FUNCTION__
, __LINE__
);
131 pr_debug("Created vma_map\n");
134 kref_init(&info
->cache_ref
);
135 spin_lock_irqsave(&cache_lock
, flags
);
136 spu_info
[spu
->number
] = info
;
137 /* Increment count before passing off ref to SPUFS. */
138 kref_get(&info
->cache_ref
);
140 /* We increment the module refcount here since SPUFS is
141 * responsible for the final destruction of the cached_info,
142 * and it must be able to access the destroy_cached_info()
143 * function defined in the OProfile module. We decrement
144 * the module refcount in destroy_cached_info.
146 try_module_get(THIS_MODULE
);
147 spu_set_profile_private_kref(spu
->ctx
, &info
->cache_ref
,
148 destroy_cached_info
);
149 spin_unlock_irqrestore(&cache_lock
, flags
);
159 * NOTE: The caller is responsible for locking the
160 * cache_lock prior to calling this function.
162 static int release_cached_info(int spu_index
)
166 if (spu_index
== RELEASE_ALL
) {
170 if (spu_index
>= num_spu_nodes
) {
171 printk(KERN_ERR
"SPU_PROF: "
173 "Invalid index %d into spu info cache\n",
174 __FUNCTION__
, __LINE__
, spu_index
);
180 for (; index
< end
; index
++) {
181 if (spu_info
[index
]) {
182 kref_put(&spu_info
[index
]->cache_ref
,
183 destroy_cached_info
);
184 spu_info
[index
] = NULL
;
192 /* The source code for fast_get_dcookie was "borrowed"
193 * from drivers/oprofile/buffer_sync.c.
196 /* Optimisation. We can manage without taking the dcookie sem
197 * because we cannot reach this code without at least one
198 * dcookie user still being registered (namely, the reader
199 * of the event buffer).
201 static inline unsigned long fast_get_dcookie(struct path
*path
)
203 unsigned long cookie
;
205 if (path
->dentry
->d_cookie
)
206 return (unsigned long)path
->dentry
;
207 get_dcookie(path
, &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_path
);
243 pr_debug("got dcookie for %s\n",
244 vma
->vm_file
->f_dentry
->d_name
.name
);
249 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
250 if (vma
->vm_start
> spu_ref
|| vma
->vm_end
<= spu_ref
)
252 my_offset
= spu_ref
- vma
->vm_start
;
254 goto fail_no_image_cookie
;
256 pr_debug("Found spu ELF at %X(object-id:%lx) for file %s\n",
258 vma
->vm_file
->f_dentry
->d_name
.name
);
259 *offsetp
= my_offset
;
263 *spu_bin_dcookie
= fast_get_dcookie(&vma
->vm_file
->f_path
);
264 pr_debug("got dcookie for %s\n", vma
->vm_file
->f_dentry
->d_name
.name
);
266 up_read(&mm
->mmap_sem
);
271 fail_no_image_cookie
:
272 up_read(&mm
->mmap_sem
);
274 printk(KERN_ERR
"SPU_PROF: "
275 "%s, line %d: Cannot find dcookie for SPU binary\n",
276 __FUNCTION__
, __LINE__
);
282 /* This function finds or creates cached context information for the
283 * passed SPU and records SPU context information into the OProfile
286 static int process_context_switch(struct spu
*spu
, unsigned long objectId
)
290 unsigned int offset
= 0;
291 unsigned long spu_cookie
= 0, app_dcookie
;
293 retval
= prepare_cached_spu_info(spu
, objectId
);
297 /* Get dcookie first because a mutex_lock is taken in that
298 * code path, so interrupts must not be disabled.
300 app_dcookie
= get_exec_dcookie_and_offset(spu
, &offset
, &spu_cookie
, objectId
);
301 if (!app_dcookie
|| !spu_cookie
) {
306 /* Record context info in event buffer */
307 spin_lock_irqsave(&buffer_lock
, flags
);
308 add_event_entry(ESCAPE_CODE
);
309 add_event_entry(SPU_CTX_SWITCH_CODE
);
310 add_event_entry(spu
->number
);
311 add_event_entry(spu
->pid
);
312 add_event_entry(spu
->tgid
);
313 add_event_entry(app_dcookie
);
314 add_event_entry(spu_cookie
);
315 add_event_entry(offset
);
316 spin_unlock_irqrestore(&buffer_lock
, flags
);
317 smp_wmb(); /* insure spu event buffer updates are written */
318 /* don't want entries intermingled... */
324 * This function is invoked on either a bind_context or unbind_context.
325 * If called for an unbind_context, the val arg is 0; otherwise,
326 * it is the object-id value for the spu context.
327 * The data arg is of type 'struct spu *'.
329 static int spu_active_notify(struct notifier_block
*self
, unsigned long val
,
334 struct spu
*the_spu
= data
;
336 pr_debug("SPU event notification arrived\n");
338 spin_lock_irqsave(&cache_lock
, flags
);
339 retval
= release_cached_info(the_spu
->number
);
340 spin_unlock_irqrestore(&cache_lock
, flags
);
342 retval
= process_context_switch(the_spu
, val
);
347 static struct notifier_block spu_active
= {
348 .notifier_call
= spu_active_notify
,
351 static int number_of_online_nodes(void)
355 for_each_online_cpu(cpu
) {
356 tmp
= cbe_cpu_to_node(cpu
) + 1;
363 /* The main purpose of this function is to synchronize
364 * OProfile with SPUFS by registering to be notified of
367 * NOTE: When profiling SPUs, we must ensure that only
368 * spu_sync_start is invoked and not the generic sync_start
369 * in drivers/oprofile/oprof.c. A return value of
370 * SKIP_GENERIC_SYNC or SYNC_START_ERROR will
373 int spu_sync_start(void)
376 int ret
= SKIP_GENERIC_SYNC
;
378 unsigned long flags
= 0;
380 spu_prof_num_nodes
= number_of_online_nodes();
381 num_spu_nodes
= spu_prof_num_nodes
* 8;
383 spin_lock_irqsave(&buffer_lock
, flags
);
384 add_event_entry(ESCAPE_CODE
);
385 add_event_entry(SPU_PROFILING_CODE
);
386 add_event_entry(num_spu_nodes
);
387 spin_unlock_irqrestore(&buffer_lock
, flags
);
389 /* Register for SPU events */
390 register_ret
= spu_switch_event_register(&spu_active
);
392 ret
= SYNC_START_ERROR
;
396 for (k
= 0; k
< (MAX_NUMNODES
* 8); k
++)
397 last_guard_val
[k
] = 0;
398 pr_debug("spu_sync_start -- running.\n");
403 /* Record SPU program counter samples to the oprofile event buffer. */
404 void spu_sync_buffer(int spu_num
, unsigned int *samples
,
407 unsigned long long file_offset
;
410 struct vma_to_fileoffset_map
*map
;
412 unsigned long long spu_num_ll
= spu_num
;
413 unsigned long long spu_num_shifted
= spu_num_ll
<< 32;
414 struct cached_info
*c_info
;
416 /* We need to obtain the cache_lock here because it's
417 * possible that after getting the cached_info, the SPU job
418 * corresponding to this cached_info may end, thus resulting
419 * in the destruction of the cached_info.
421 spin_lock_irqsave(&cache_lock
, flags
);
422 c_info
= get_cached_info(NULL
, spu_num
);
424 /* This legitimately happens when the SPU task ends before all
425 * samples are recorded.
426 * No big deal -- so we just drop a few samples.
428 pr_debug("SPU_PROF: No cached SPU contex "
429 "for SPU #%d. Dropping samples.\n", spu_num
);
434 the_spu
= c_info
->the_spu
;
435 spin_lock(&buffer_lock
);
436 for (i
= 0; i
< num_samples
; i
++) {
437 unsigned int sample
= *(samples
+i
);
442 file_offset
= vma_map_lookup( map
, sample
, the_spu
, &grd_val
);
444 /* If overlays are used by this SPU application, the guard
445 * value is non-zero, indicating which overlay section is in
446 * use. We need to discard samples taken during the time
447 * period which an overlay occurs (i.e., guard value changes).
449 if (grd_val
&& grd_val
!= last_guard_val
[spu_num
]) {
450 last_guard_val
[spu_num
] = grd_val
;
451 /* Drop the rest of the samples. */
455 add_event_entry(file_offset
| spu_num_shifted
);
457 spin_unlock(&buffer_lock
);
459 spin_unlock_irqrestore(&cache_lock
, flags
);
463 int spu_sync_stop(void)
465 unsigned long flags
= 0;
466 int ret
= spu_switch_event_unregister(&spu_active
);
468 printk(KERN_ERR
"SPU_PROF: "
469 "%s, line %d: spu_switch_event_unregister returned %d\n",
470 __FUNCTION__
, __LINE__
, ret
);
474 spin_lock_irqsave(&cache_lock
, flags
);
475 ret
= release_cached_info(RELEASE_ALL
);
476 spin_unlock_irqrestore(&cache_lock
, flags
);
478 pr_debug("spu_sync_stop -- done.\n");