spu_profiler.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Cell Broadband Engine OProfile Support
  3. *
  4. * (C) Copyright IBM Corporation 2006
  5. *
  6. * Authors: Maynard Johnson <maynardj@us.ibm.com>
  7. * Carl Love <carll@us.ibm.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/hrtimer.h>
  15. #include <linux/smp.h>
  16. #include <linux/slab.h>
  17. #include <asm/cell-pmu.h>
  18. #include <asm/time.h>
  19. #include "pr_util.h"
  20. #define SCALE_SHIFT 14
  21. static u32 *samples;
  22. /* spu_prof_running is a flag used to indicate if spu profiling is enabled
  23. * or not. It is set by the routines start_spu_profiling_cycles() and
  24. * start_spu_profiling_events(). The flag is cleared by the routines
  25. * stop_spu_profiling_cycles() and stop_spu_profiling_events(). These
  26. * routines are called via global_start() and global_stop() which are called in
  27. * op_powerpc_start() and op_powerpc_stop(). These routines are called once
  28. * per system as a result of the user starting/stopping oprofile. Hence, only
  29. * one CPU per user at a time will be changing the value of spu_prof_running.
  30. * In general, OProfile does not protect against multiple users trying to run
  31. * OProfile at a time.
  32. */
  33. int spu_prof_running;
  34. static unsigned int profiling_interval;
  35. #define NUM_SPU_BITS_TRBUF 16
  36. #define SPUS_PER_TB_ENTRY 4
  37. #define SPU_PC_MASK 0xFFFF
  38. DEFINE_SPINLOCK(oprof_spu_smpl_arry_lck);
  39. static unsigned long oprof_spu_smpl_arry_lck_flags;
  40. void set_spu_profiling_frequency(unsigned int freq_khz, unsigned int cycles_reset)
  41. {
  42. unsigned long ns_per_cyc;
  43. if (!freq_khz)
  44. freq_khz = ppc_proc_freq/1000;
  45. /* To calculate a timeout in nanoseconds, the basic
  46. * formula is ns = cycles_reset * (NSEC_PER_SEC / cpu frequency).
  47. * To avoid floating point math, we use the scale math
  48. * technique as described in linux/jiffies.h. We use
  49. * a scale factor of SCALE_SHIFT, which provides 4 decimal places
  50. * of precision. This is close enough for the purpose at hand.
  51. *
  52. * The value of the timeout should be small enough that the hw
  53. * trace buffer will not get more than about 1/3 full for the
  54. * maximum user specified (the LFSR value) hw sampling frequency.
  55. * This is to ensure the trace buffer will never fill even if the
  56. * kernel thread scheduling varies under a heavy system load.
  57. */
  58. ns_per_cyc = (USEC_PER_SEC << SCALE_SHIFT)/freq_khz;
  59. profiling_interval = (ns_per_cyc * cycles_reset) >> SCALE_SHIFT;
  60. }
  61. /*
  62. * Extract SPU PC from trace buffer entry
  63. */
  64. static void spu_pc_extract(int cpu, int entry)
  65. {
  66. /* the trace buffer is 128 bits */
  67. u64 trace_buffer[2];
  68. u64 spu_mask;
  69. int spu;
  70. spu_mask = SPU_PC_MASK;
  71. /* Each SPU PC is 16 bits; hence, four spus in each of
  72. * the two 64-bit buffer entries that make up the
  73. * 128-bit trace_buffer entry. Process two 64-bit values
  74. * simultaneously.
  75. * trace[0] SPU PC contents are: 0 1 2 3
  76. * trace[1] SPU PC contents are: 4 5 6 7
  77. */
  78. cbe_read_trace_buffer(cpu, trace_buffer);
  79. for (spu = SPUS_PER_TB_ENTRY-1; spu >= 0; spu--) {
  80. /* spu PC trace entry is upper 16 bits of the
  81. * 18 bit SPU program counter
  82. */
  83. samples[spu * TRACE_ARRAY_SIZE + entry]
  84. = (spu_mask & trace_buffer[0]) << 2;
  85. samples[(spu + SPUS_PER_TB_ENTRY) * TRACE_ARRAY_SIZE + entry]
  86. = (spu_mask & trace_buffer[1]) << 2;
  87. trace_buffer[0] = trace_buffer[0] >> NUM_SPU_BITS_TRBUF;
  88. trace_buffer[1] = trace_buffer[1] >> NUM_SPU_BITS_TRBUF;
  89. }
  90. }
  91. static int cell_spu_pc_collection(int cpu)
  92. {
  93. u32 trace_addr;
  94. int entry;
  95. /* process the collected SPU PC for the node */
  96. entry = 0;
  97. trace_addr = cbe_read_pm(cpu, trace_address);
  98. while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
  99. /* there is data in the trace buffer to process */
  100. spu_pc_extract(cpu, entry);
  101. entry++;
  102. if (entry >= TRACE_ARRAY_SIZE)
  103. /* spu_samples is full */
  104. break;
  105. trace_addr = cbe_read_pm(cpu, trace_address);
  106. }
  107. return entry;
  108. }
  109. static enum hrtimer_restart profile_spus(struct hrtimer *timer)
  110. {
  111. ktime_t kt;
  112. int cpu, node, k, num_samples, spu_num;
  113. if (!spu_prof_running)
  114. goto stop;
  115. for_each_online_cpu(cpu) {
  116. if (cbe_get_hw_thread_id(cpu))
  117. continue;
  118. node = cbe_cpu_to_node(cpu);
  119. /* There should only be one kernel thread at a time processing
  120. * the samples. In the very unlikely case that the processing
  121. * is taking a very long time and multiple kernel threads are
  122. * started to process the samples. Make sure only one kernel
  123. * thread is working on the samples array at a time. The
  124. * sample array must be loaded and then processed for a given
  125. * cpu. The sample array is not per cpu.
  126. */
  127. spin_lock_irqsave(&oprof_spu_smpl_arry_lck,
  128. oprof_spu_smpl_arry_lck_flags);
  129. num_samples = cell_spu_pc_collection(cpu);
  130. if (num_samples == 0) {
  131. spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
  132. oprof_spu_smpl_arry_lck_flags);
  133. continue;
  134. }
  135. for (k = 0; k < SPUS_PER_NODE; k++) {
  136. spu_num = k + (node * SPUS_PER_NODE);
  137. spu_sync_buffer(spu_num,
  138. samples + (k * TRACE_ARRAY_SIZE),
  139. num_samples);
  140. }
  141. spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
  142. oprof_spu_smpl_arry_lck_flags);
  143. }
  144. smp_wmb(); /* insure spu event buffer updates are written */
  145. /* don't want events intermingled... */
  146. kt = ktime_set(0, profiling_interval);
  147. if (!spu_prof_running)
  148. goto stop;
  149. hrtimer_forward(timer, timer->base->get_time(), kt);
  150. return HRTIMER_RESTART;
  151. stop:
  152. printk(KERN_INFO "SPU_PROF: spu-prof timer ending\n");
  153. return HRTIMER_NORESTART;
  154. }
  155. static struct hrtimer timer;
  156. /*
  157. * Entry point for SPU cycle profiling.
  158. * NOTE: SPU profiling is done system-wide, not per-CPU.
  159. *
  160. * cycles_reset is the count value specified by the user when
  161. * setting up OProfile to count SPU_CYCLES.
  162. */
  163. int start_spu_profiling_cycles(unsigned int cycles_reset)
  164. {
  165. ktime_t kt;
  166. pr_debug("timer resolution: %lu\n", TICK_NSEC);
  167. kt = ktime_set(0, profiling_interval);
  168. hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  169. hrtimer_set_expires(&timer, kt);
  170. timer.function = profile_spus;
  171. /* Allocate arrays for collecting SPU PC samples */
  172. samples = kzalloc(SPUS_PER_NODE *
  173. TRACE_ARRAY_SIZE * sizeof(u32), GFP_KERNEL);
  174. if (!samples)
  175. return -ENOMEM;
  176. spu_prof_running = 1;
  177. hrtimer_start(&timer, kt, HRTIMER_MODE_REL);
  178. schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
  179. return 0;
  180. }
  181. /*
  182. * Entry point for SPU event profiling.
  183. * NOTE: SPU profiling is done system-wide, not per-CPU.
  184. *
  185. * cycles_reset is the count value specified by the user when
  186. * setting up OProfile to count SPU_CYCLES.
  187. */
  188. void start_spu_profiling_events(void)
  189. {
  190. spu_prof_running = 1;
  191. schedule_delayed_work(&spu_work, DEFAULT_TIMER_EXPIRE);
  192. return;
  193. }
  194. void stop_spu_profiling_cycles(void)
  195. {
  196. spu_prof_running = 0;
  197. hrtimer_cancel(&timer);
  198. kfree(samples);
  199. pr_debug("SPU_PROF: stop_spu_profiling_cycles issued\n");
  200. }
  201. void stop_spu_profiling_events(void)
  202. {
  203. spu_prof_running = 0;
  204. }