oprofile_perf.c 7.0 KB

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