spu_profiler.c 6.8 KB

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