trace_power.c 4.0 KB

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