trace_power.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <linux/ftrace.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 start_power_trace(struct trace_array *tr)
  21. {
  22. trace_power_enabled = 1;
  23. }
  24. static void stop_power_trace(struct trace_array *tr)
  25. {
  26. trace_power_enabled = 0;
  27. }
  28. static int power_trace_init(struct trace_array *tr)
  29. {
  30. int cpu;
  31. power_trace = tr;
  32. trace_power_enabled = 1;
  33. for_each_cpu(cpu, cpu_possible_mask)
  34. tracing_reset(tr, cpu);
  35. return 0;
  36. }
  37. static enum print_line_t power_print_line(struct trace_iterator *iter)
  38. {
  39. int ret = 0;
  40. struct trace_entry *entry = iter->ent;
  41. struct trace_power *field ;
  42. struct power_trace *it;
  43. struct trace_seq *s = &iter->seq;
  44. struct timespec stamp;
  45. struct timespec duration;
  46. trace_assign_type(field, entry);
  47. it = &field->state_data;
  48. stamp = ktime_to_timespec(it->stamp);
  49. duration = ktime_to_timespec(ktime_sub(it->end, it->stamp));
  50. if (entry->type == TRACE_POWER) {
  51. if (it->type == POWER_CSTATE)
  52. ret = trace_seq_printf(s, "[%5ld.%09ld] CSTATE: Going to C%i on cpu %i for %ld.%09ld\n",
  53. stamp.tv_sec,
  54. stamp.tv_nsec,
  55. it->state, iter->cpu,
  56. duration.tv_sec,
  57. duration.tv_nsec);
  58. if (it->type == POWER_PSTATE)
  59. ret = trace_seq_printf(s, "[%5ld.%09ld] PSTATE: Going to P%i on cpu %i\n",
  60. stamp.tv_sec,
  61. stamp.tv_nsec,
  62. it->state, iter->cpu);
  63. if (!ret)
  64. return TRACE_TYPE_PARTIAL_LINE;
  65. return TRACE_TYPE_HANDLED;
  66. }
  67. return TRACE_TYPE_UNHANDLED;
  68. }
  69. static struct tracer power_tracer __read_mostly =
  70. {
  71. .name = "power",
  72. .init = power_trace_init,
  73. .start = start_power_trace,
  74. .stop = stop_power_trace,
  75. .reset = stop_power_trace,
  76. .print_line = power_print_line,
  77. };
  78. static int init_power_trace(void)
  79. {
  80. return register_tracer(&power_tracer);
  81. }
  82. device_initcall(init_power_trace);
  83. void trace_power_start(struct power_trace *it, unsigned int type,
  84. unsigned int level)
  85. {
  86. if (!trace_power_enabled)
  87. return;
  88. memset(it, 0, sizeof(struct power_trace));
  89. it->state = level;
  90. it->type = type;
  91. it->stamp = ktime_get();
  92. }
  93. EXPORT_SYMBOL_GPL(trace_power_start);
  94. void trace_power_end(struct power_trace *it)
  95. {
  96. struct ring_buffer_event *event;
  97. struct trace_power *entry;
  98. struct trace_array_cpu *data;
  99. unsigned long irq_flags;
  100. struct trace_array *tr = power_trace;
  101. if (!trace_power_enabled)
  102. return;
  103. preempt_disable();
  104. it->end = ktime_get();
  105. data = tr->data[smp_processor_id()];
  106. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  107. &irq_flags);
  108. if (!event)
  109. goto out;
  110. entry = ring_buffer_event_data(event);
  111. tracing_generic_entry_update(&entry->ent, 0, 0);
  112. entry->ent.type = TRACE_POWER;
  113. entry->state_data = *it;
  114. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  115. trace_wake_up();
  116. out:
  117. preempt_enable();
  118. }
  119. EXPORT_SYMBOL_GPL(trace_power_end);
  120. void trace_power_mark(struct power_trace *it, unsigned int type,
  121. unsigned int level)
  122. {
  123. struct ring_buffer_event *event;
  124. struct trace_power *entry;
  125. struct trace_array_cpu *data;
  126. unsigned long irq_flags;
  127. struct trace_array *tr = power_trace;
  128. if (!trace_power_enabled)
  129. return;
  130. memset(it, 0, sizeof(struct power_trace));
  131. it->state = level;
  132. it->type = type;
  133. it->stamp = ktime_get();
  134. preempt_disable();
  135. it->end = it->stamp;
  136. data = tr->data[smp_processor_id()];
  137. event = ring_buffer_lock_reserve(tr->buffer, sizeof(*entry),
  138. &irq_flags);
  139. if (!event)
  140. goto out;
  141. entry = ring_buffer_event_data(event);
  142. tracing_generic_entry_update(&entry->ent, 0, 0);
  143. entry->ent.type = TRACE_POWER;
  144. entry->state_data = *it;
  145. ring_buffer_unlock_commit(tr->buffer, event, irq_flags);
  146. trace_wake_up();
  147. out:
  148. preempt_enable();
  149. }
  150. EXPORT_SYMBOL_GPL(trace_power_mark);