trace_event_profile.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * trace event based perf counter profiling
  3. *
  4. * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include "trace.h"
  9. /*
  10. * We can't use a size but a type in alloc_percpu()
  11. * So let's create a dummy type that matches the desired size
  12. */
  13. typedef struct {char buf[FTRACE_MAX_PROFILE_SIZE];} profile_buf_t;
  14. char *trace_profile_buf;
  15. EXPORT_SYMBOL_GPL(trace_profile_buf);
  16. char *trace_profile_buf_nmi;
  17. EXPORT_SYMBOL_GPL(trace_profile_buf_nmi);
  18. /* Count the events in use (per event id, not per instance) */
  19. static int total_profile_count;
  20. static int ftrace_profile_enable_event(struct ftrace_event_call *event)
  21. {
  22. char *buf;
  23. int ret = -ENOMEM;
  24. if (atomic_inc_return(&event->profile_count))
  25. return 0;
  26. if (!total_profile_count++) {
  27. buf = (char *)alloc_percpu(profile_buf_t);
  28. if (!buf)
  29. goto fail_buf;
  30. rcu_assign_pointer(trace_profile_buf, buf);
  31. buf = (char *)alloc_percpu(profile_buf_t);
  32. if (!buf)
  33. goto fail_buf_nmi;
  34. rcu_assign_pointer(trace_profile_buf_nmi, buf);
  35. }
  36. ret = event->profile_enable();
  37. if (!ret)
  38. return 0;
  39. kfree(trace_profile_buf_nmi);
  40. fail_buf_nmi:
  41. kfree(trace_profile_buf);
  42. fail_buf:
  43. total_profile_count--;
  44. atomic_dec(&event->profile_count);
  45. return ret;
  46. }
  47. int ftrace_profile_enable(int event_id)
  48. {
  49. struct ftrace_event_call *event;
  50. int ret = -EINVAL;
  51. mutex_lock(&event_mutex);
  52. list_for_each_entry(event, &ftrace_events, list) {
  53. if (event->id == event_id && event->profile_enable &&
  54. try_module_get(event->mod)) {
  55. ret = ftrace_profile_enable_event(event);
  56. break;
  57. }
  58. }
  59. mutex_unlock(&event_mutex);
  60. return ret;
  61. }
  62. static void ftrace_profile_disable_event(struct ftrace_event_call *event)
  63. {
  64. char *buf, *nmi_buf;
  65. if (!atomic_add_negative(-1, &event->profile_count))
  66. return;
  67. event->profile_disable();
  68. if (!--total_profile_count) {
  69. buf = trace_profile_buf;
  70. rcu_assign_pointer(trace_profile_buf, NULL);
  71. nmi_buf = trace_profile_buf_nmi;
  72. rcu_assign_pointer(trace_profile_buf_nmi, NULL);
  73. /*
  74. * Ensure every events in profiling have finished before
  75. * releasing the buffers
  76. */
  77. synchronize_sched();
  78. free_percpu(buf);
  79. free_percpu(nmi_buf);
  80. }
  81. }
  82. void ftrace_profile_disable(int event_id)
  83. {
  84. struct ftrace_event_call *event;
  85. mutex_lock(&event_mutex);
  86. list_for_each_entry(event, &ftrace_events, list) {
  87. if (event->id == event_id) {
  88. ftrace_profile_disable_event(event);
  89. module_put(event->mod);
  90. break;
  91. }
  92. }
  93. mutex_unlock(&event_mutex);
  94. }