trace_power.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * ring buffer based C-state tracer
  3. *
  4. * Arjan van de Ven <arjan@linux.intel.com>
  5. * Copyright (C) 2008 Intel Corporation
  6. *
  7. * Much is borrowed from trace_boot.c which is
  8. * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
  9. *
  10. */
  11. #include <linux/init.h>
  12. #include <linux/debugfs.h>
  13. #include <trace/power.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/module.h>
  16. #include "trace.h"
  17. #include "trace_output.h"
  18. static struct trace_array *power_trace;
  19. static int __read_mostly trace_power_enabled;
  20. static void probe_power_start(struct power_trace *it, unsigned int type,
  21. unsigned int level)
  22. {
  23. if (!trace_power_enabled)
  24. return;
  25. memset(it, 0, sizeof(struct power_trace));
  26. it->state = level;
  27. it->type = type;
  28. it->stamp = ktime_get();
  29. }
  30. static void probe_power_end(struct power_trace *it)
  31. {
  32. struct ring_buffer_event *event;
  33. struct trace_power *entry;
  34. struct trace_array_cpu *data;
  35. struct trace_array *tr = power_trace;
  36. if (!trace_power_enabled)
  37. return;
  38. preempt_disable();
  39. it->end = ktime_get();
  40. data = tr->data[smp_processor_id()];
  41. event = trace_buffer_lock_reserve(tr, TRACE_POWER,
  42. sizeof(*entry), 0, 0);
  43. if (!event)
  44. goto out;
  45. entry = ring_buffer_event_data(event);
  46. entry->state_data = *it;
  47. trace_buffer_unlock_commit(tr, event, 0, 0);
  48. out:
  49. preempt_enable();
  50. }
  51. static void probe_power_mark(struct power_trace *it, unsigned int type,
  52. unsigned int level)
  53. {
  54. struct ring_buffer_event *event;
  55. struct trace_power *entry;
  56. struct trace_array_cpu *data;
  57. struct trace_array *tr = power_trace;
  58. if (!trace_power_enabled)
  59. return;
  60. memset(it, 0, sizeof(struct power_trace));
  61. it->state = level;
  62. it->type = type;
  63. it->stamp = ktime_get();
  64. preempt_disable();
  65. it->end = it->stamp;
  66. data = tr->data[smp_processor_id()];
  67. event = trace_buffer_lock_reserve(tr, TRACE_POWER,
  68. sizeof(*entry), 0, 0);
  69. if (!event)
  70. goto out;
  71. entry = ring_buffer_event_data(event);
  72. entry->state_data = *it;
  73. trace_buffer_unlock_commit(tr, event, 0, 0);
  74. out:
  75. preempt_enable();
  76. }
  77. static int tracing_power_register(void)
  78. {
  79. int ret;
  80. ret = register_trace_power_start(probe_power_start);
  81. if (ret) {
  82. pr_info("power trace: Couldn't activate tracepoint"
  83. " probe to trace_power_start\n");
  84. return ret;
  85. }
  86. ret = register_trace_power_end(probe_power_end);
  87. if (ret) {
  88. pr_info("power trace: Couldn't activate tracepoint"
  89. " probe to trace_power_end\n");
  90. goto fail_start;
  91. }
  92. ret = register_trace_power_mark(probe_power_mark);
  93. if (ret) {
  94. pr_info("power trace: Couldn't activate tracepoint"
  95. " probe to trace_power_mark\n");
  96. goto fail_end;
  97. }
  98. return ret;
  99. fail_end:
  100. unregister_trace_power_end(probe_power_end);
  101. fail_start:
  102. unregister_trace_power_start(probe_power_start);
  103. return ret;
  104. }
  105. static void start_power_trace(struct trace_array *tr)
  106. {
  107. trace_power_enabled = 1;
  108. tracing_power_register();
  109. }
  110. static void stop_power_trace(struct trace_array *tr)
  111. {
  112. trace_power_enabled = 0;
  113. unregister_trace_power_start(probe_power_start);
  114. unregister_trace_power_end(probe_power_end);
  115. unregister_trace_power_mark(probe_power_mark);
  116. }
  117. static int power_trace_init(struct trace_array *tr)
  118. {
  119. int cpu;
  120. power_trace = tr;
  121. trace_power_enabled = 1;
  122. tracing_power_register();
  123. for_each_cpu(cpu, cpu_possible_mask)
  124. tracing_reset(tr, cpu);
  125. return 0;
  126. }
  127. static enum print_line_t power_print_line(struct trace_iterator *iter)
  128. {
  129. int ret = 0;
  130. struct trace_entry *entry = iter->ent;
  131. struct trace_power *field ;
  132. struct power_trace *it;
  133. struct trace_seq *s = &iter->seq;
  134. struct timespec stamp;
  135. struct timespec duration;
  136. trace_assign_type(field, entry);
  137. it = &field->state_data;
  138. stamp = ktime_to_timespec(it->stamp);
  139. duration = ktime_to_timespec(ktime_sub(it->end, it->stamp));
  140. if (entry->type == TRACE_POWER) {
  141. if (it->type == POWER_CSTATE)
  142. ret = trace_seq_printf(s, "[%5ld.%09ld] CSTATE: Going to C%i on cpu %i for %ld.%09ld\n",
  143. stamp.tv_sec,
  144. stamp.tv_nsec,
  145. it->state, iter->cpu,
  146. duration.tv_sec,
  147. duration.tv_nsec);
  148. if (it->type == POWER_PSTATE)
  149. ret = trace_seq_printf(s, "[%5ld.%09ld] PSTATE: Going to P%i on cpu %i\n",
  150. stamp.tv_sec,
  151. stamp.tv_nsec,
  152. it->state, iter->cpu);
  153. if (!ret)
  154. return TRACE_TYPE_PARTIAL_LINE;
  155. return TRACE_TYPE_HANDLED;
  156. }
  157. return TRACE_TYPE_UNHANDLED;
  158. }
  159. static struct tracer power_tracer __read_mostly =
  160. {
  161. .name = "power",
  162. .init = power_trace_init,
  163. .start = start_power_trace,
  164. .stop = stop_power_trace,
  165. .reset = stop_power_trace,
  166. .print_line = power_print_line,
  167. };
  168. static int init_power_trace(void)
  169. {
  170. return register_tracer(&power_tracer);
  171. }
  172. device_initcall(init_power_trace);