oprofile_perf.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright 2010 ARM Ltd.
  3. *
  4. * Perf-events backend for OProfile.
  5. */
  6. #include <linux/perf_event.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/oprofile.h>
  9. #include <linux/slab.h>
  10. /*
  11. * Per performance monitor configuration as set via oprofilefs.
  12. */
  13. struct op_counter_config {
  14. unsigned long count;
  15. unsigned long enabled;
  16. unsigned long event;
  17. unsigned long unit_mask;
  18. unsigned long kernel;
  19. unsigned long user;
  20. struct perf_event_attr attr;
  21. };
  22. static int oprofile_perf_enabled;
  23. static DEFINE_MUTEX(oprofile_perf_mutex);
  24. static struct op_counter_config *counter_config;
  25. static struct perf_event **perf_events[nr_cpumask_bits];
  26. static int num_counters;
  27. /*
  28. * Overflow callback for oprofile.
  29. */
  30. static void op_overflow_handler(struct perf_event *event, int unused,
  31. struct perf_sample_data *data, struct pt_regs *regs)
  32. {
  33. int id;
  34. u32 cpu = smp_processor_id();
  35. for (id = 0; id < num_counters; ++id)
  36. if (perf_events[cpu][id] == event)
  37. break;
  38. if (id != num_counters)
  39. oprofile_add_sample(regs, id);
  40. else
  41. pr_warning("oprofile: ignoring spurious overflow "
  42. "on cpu %u\n", cpu);
  43. }
  44. /*
  45. * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
  46. * settings in counter_config. Attributes are created as `pinned' events and
  47. * so are permanently scheduled on the PMU.
  48. */
  49. static void op_perf_setup(void)
  50. {
  51. int i;
  52. u32 size = sizeof(struct perf_event_attr);
  53. struct perf_event_attr *attr;
  54. for (i = 0; i < num_counters; ++i) {
  55. attr = &counter_config[i].attr;
  56. memset(attr, 0, size);
  57. attr->type = PERF_TYPE_RAW;
  58. attr->size = size;
  59. attr->config = counter_config[i].event;
  60. attr->sample_period = counter_config[i].count;
  61. attr->pinned = 1;
  62. }
  63. }
  64. static int op_create_counter(int cpu, int event)
  65. {
  66. struct perf_event *pevent;
  67. if (!counter_config[event].enabled || perf_events[cpu][event])
  68. return 0;
  69. pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
  70. cpu, NULL,
  71. op_overflow_handler);
  72. if (IS_ERR(pevent))
  73. return PTR_ERR(pevent);
  74. if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
  75. perf_event_release_kernel(pevent);
  76. pr_warning("oprofile: failed to enable event %d "
  77. "on CPU %d\n", event, cpu);
  78. return -EBUSY;
  79. }
  80. perf_events[cpu][event] = pevent;
  81. return 0;
  82. }
  83. static void op_destroy_counter(int cpu, int event)
  84. {
  85. struct perf_event *pevent = perf_events[cpu][event];
  86. if (pevent) {
  87. perf_event_release_kernel(pevent);
  88. perf_events[cpu][event] = NULL;
  89. }
  90. }
  91. /*
  92. * Called by oprofile_perf_start to create active perf events based on the
  93. * perviously configured attributes.
  94. */
  95. static int op_perf_start(void)
  96. {
  97. int cpu, event, ret = 0;
  98. for_each_online_cpu(cpu) {
  99. for (event = 0; event < num_counters; ++event) {
  100. ret = op_create_counter(cpu, event);
  101. if (ret)
  102. return ret;
  103. }
  104. }
  105. return ret;
  106. }
  107. /*
  108. * Called by oprofile_perf_stop at the end of a profiling run.
  109. */
  110. static void op_perf_stop(void)
  111. {
  112. int cpu, event;
  113. for_each_online_cpu(cpu)
  114. for (event = 0; event < num_counters; ++event)
  115. op_destroy_counter(cpu, event);
  116. }
  117. static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root)
  118. {
  119. unsigned int i;
  120. for (i = 0; i < num_counters; i++) {
  121. struct dentry *dir;
  122. char buf[4];
  123. snprintf(buf, sizeof buf, "%d", i);
  124. dir = oprofilefs_mkdir(sb, root, buf);
  125. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  126. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  127. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  128. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  129. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  130. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  131. }
  132. return 0;
  133. }
  134. static int oprofile_perf_setup(void)
  135. {
  136. spin_lock(&oprofilefs_lock);
  137. op_perf_setup();
  138. spin_unlock(&oprofilefs_lock);
  139. return 0;
  140. }
  141. static int oprofile_perf_start(void)
  142. {
  143. int ret = -EBUSY;
  144. mutex_lock(&oprofile_perf_mutex);
  145. if (!oprofile_perf_enabled) {
  146. ret = 0;
  147. op_perf_start();
  148. oprofile_perf_enabled = 1;
  149. }
  150. mutex_unlock(&oprofile_perf_mutex);
  151. return ret;
  152. }
  153. static void oprofile_perf_stop(void)
  154. {
  155. mutex_lock(&oprofile_perf_mutex);
  156. if (oprofile_perf_enabled)
  157. op_perf_stop();
  158. oprofile_perf_enabled = 0;
  159. mutex_unlock(&oprofile_perf_mutex);
  160. }
  161. #ifdef CONFIG_PM
  162. static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
  163. {
  164. mutex_lock(&oprofile_perf_mutex);
  165. if (oprofile_perf_enabled)
  166. op_perf_stop();
  167. mutex_unlock(&oprofile_perf_mutex);
  168. return 0;
  169. }
  170. static int oprofile_perf_resume(struct platform_device *dev)
  171. {
  172. mutex_lock(&oprofile_perf_mutex);
  173. if (oprofile_perf_enabled && op_perf_start())
  174. oprofile_perf_enabled = 0;
  175. mutex_unlock(&oprofile_perf_mutex);
  176. return 0;
  177. }
  178. static struct platform_driver oprofile_driver = {
  179. .driver = {
  180. .name = "oprofile-perf",
  181. },
  182. .resume = oprofile_perf_resume,
  183. .suspend = oprofile_perf_suspend,
  184. };
  185. static struct platform_device *oprofile_pdev;
  186. static int __init init_driverfs(void)
  187. {
  188. int ret;
  189. ret = platform_driver_register(&oprofile_driver);
  190. if (ret)
  191. return ret;
  192. oprofile_pdev = platform_device_register_simple(
  193. oprofile_driver.driver.name, 0, NULL, 0);
  194. if (IS_ERR(oprofile_pdev)) {
  195. ret = PTR_ERR(oprofile_pdev);
  196. platform_driver_unregister(&oprofile_driver);
  197. }
  198. return ret;
  199. }
  200. static void exit_driverfs(void)
  201. {
  202. platform_device_unregister(oprofile_pdev);
  203. platform_driver_unregister(&oprofile_driver);
  204. }
  205. #else
  206. static inline int init_driverfs(void) { return 0; }
  207. static inline void exit_driverfs(void) { }
  208. #endif /* CONFIG_PM */
  209. void oprofile_perf_exit(void)
  210. {
  211. int cpu, id;
  212. struct perf_event *event;
  213. for_each_possible_cpu(cpu) {
  214. for (id = 0; id < num_counters; ++id) {
  215. event = perf_events[cpu][id];
  216. if (event)
  217. perf_event_release_kernel(event);
  218. }
  219. kfree(perf_events[cpu]);
  220. }
  221. kfree(counter_config);
  222. exit_driverfs();
  223. }
  224. int __init oprofile_perf_init(struct oprofile_operations *ops)
  225. {
  226. int cpu, ret = 0;
  227. ret = init_driverfs();
  228. if (ret)
  229. return ret;
  230. memset(&perf_events, 0, sizeof(perf_events));
  231. num_counters = perf_num_counters();
  232. if (num_counters <= 0) {
  233. pr_info("oprofile: no performance counters\n");
  234. ret = -ENODEV;
  235. goto out;
  236. }
  237. counter_config = kcalloc(num_counters,
  238. sizeof(struct op_counter_config), GFP_KERNEL);
  239. if (!counter_config) {
  240. pr_info("oprofile: failed to allocate %d "
  241. "counters\n", num_counters);
  242. ret = -ENOMEM;
  243. num_counters = 0;
  244. goto out;
  245. }
  246. for_each_possible_cpu(cpu) {
  247. perf_events[cpu] = kcalloc(num_counters,
  248. sizeof(struct perf_event *), GFP_KERNEL);
  249. if (!perf_events[cpu]) {
  250. pr_info("oprofile: failed to allocate %d perf events "
  251. "for cpu %d\n", num_counters, cpu);
  252. ret = -ENOMEM;
  253. goto out;
  254. }
  255. }
  256. ops->create_files = oprofile_perf_create_files;
  257. ops->setup = oprofile_perf_setup;
  258. ops->start = oprofile_perf_start;
  259. ops->stop = oprofile_perf_stop;
  260. ops->shutdown = oprofile_perf_stop;
  261. ops->cpu_type = op_name_from_perf_id();
  262. if (!ops->cpu_type)
  263. ret = -ENODEV;
  264. else
  265. pr_info("oprofile: using %s\n", ops->cpu_type);
  266. out:
  267. if (ret)
  268. oprofile_perf_exit();
  269. return ret;
  270. }