oprofile_perf.c 7.1 KB

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