spu_profiler.c 5.7 KB

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