trace_power.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 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. 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 = trace_buffer_lock_reserve(tr, TRACE_POWER,
  106. sizeof(*entry), 0, 0);
  107. if (!event)
  108. goto out;
  109. entry = ring_buffer_event_data(event);
  110. entry->state_data = *it;
  111. trace_buffer_unlock_commit(tr, event, 0, 0);
  112. out:
  113. preempt_enable();
  114. }
  115. EXPORT_SYMBOL_GPL(trace_power_end);
  116. void trace_power_mark(struct power_trace *it, unsigned int type,
  117. unsigned int level)
  118. {
  119. struct ring_buffer_event *event;
  120. struct trace_power *entry;
  121. struct trace_array_cpu *data;
  122. struct trace_array *tr = power_trace;
  123. if (!trace_power_enabled)
  124. return;
  125. memset(it, 0, sizeof(struct power_trace));
  126. it->state = level;
  127. it->type = type;
  128. it->stamp = ktime_get();
  129. preempt_disable();
  130. it->end = it->stamp;
  131. data = tr->data[smp_processor_id()];
  132. event = trace_buffer_lock_reserve(tr, TRACE_POWER,
  133. sizeof(*entry), 0, 0);
  134. if (!event)
  135. goto out;
  136. entry = ring_buffer_event_data(event);
  137. entry->state_data = *it;
  138. trace_buffer_unlock_commit(tr, event, 0, 0);
  139. out:
  140. preempt_enable();
  141. }
  142. EXPORT_SYMBOL_GPL(trace_power_mark);