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