2 * ring buffer based C-state tracer
4 * Arjan van de Ven <arjan@linux.intel.com>
5 * Copyright (C) 2008 Intel Corporation
7 * Much is borrowed from trace_boot.c which is
8 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
12 #include <linux/init.h>
13 #include <linux/debugfs.h>
14 #include <trace/power.h>
15 #include <linux/kallsyms.h>
16 #include <linux/module.h>
19 #include "trace_output.h"
21 static struct trace_array *power_trace;
22 static int __read_mostly trace_power_enabled;
24 static void probe_power_start(struct power_trace *it, unsigned int type,
27 if (!trace_power_enabled)
30 memset(it, 0, sizeof(struct power_trace));
33 it->stamp = ktime_get();
37 static void probe_power_end(struct power_trace *it)
39 struct ring_buffer_event *event;
40 struct trace_power *entry;
41 struct trace_array_cpu *data;
42 struct trace_array *tr = power_trace;
44 if (!trace_power_enabled)
48 it->end = ktime_get();
49 data = tr->data[smp_processor_id()];
51 event = trace_buffer_lock_reserve(tr, TRACE_POWER,
52 sizeof(*entry), 0, 0);
55 entry = ring_buffer_event_data(event);
56 entry->state_data = *it;
57 trace_buffer_unlock_commit(tr, event, 0, 0);
62 static void probe_power_mark(struct power_trace *it, unsigned int type,
65 struct ring_buffer_event *event;
66 struct trace_power *entry;
67 struct trace_array_cpu *data;
68 struct trace_array *tr = power_trace;
70 if (!trace_power_enabled)
73 memset(it, 0, sizeof(struct power_trace));
76 it->stamp = ktime_get();
79 data = tr->data[smp_processor_id()];
81 event = trace_buffer_lock_reserve(tr, TRACE_POWER,
82 sizeof(*entry), 0, 0);
85 entry = ring_buffer_event_data(event);
86 entry->state_data = *it;
87 trace_buffer_unlock_commit(tr, event, 0, 0);
92 static int tracing_power_register(void)
96 ret = register_trace_power_start(probe_power_start);
98 pr_info("power trace: Couldn't activate tracepoint"
99 " probe to trace_power_start\n");
102 ret = register_trace_power_end(probe_power_end);
104 pr_info("power trace: Couldn't activate tracepoint"
105 " probe to trace_power_end\n");
108 ret = register_trace_power_mark(probe_power_mark);
110 pr_info("power trace: Couldn't activate tracepoint"
111 " probe to trace_power_mark\n");
116 unregister_trace_power_end(probe_power_end);
118 unregister_trace_power_start(probe_power_start);
122 static void start_power_trace(struct trace_array *tr)
124 trace_power_enabled = 1;
125 tracing_power_register();
128 static void stop_power_trace(struct trace_array *tr)
130 trace_power_enabled = 0;
131 unregister_trace_power_start(probe_power_start);
132 unregister_trace_power_end(probe_power_end);
133 unregister_trace_power_mark(probe_power_mark);
137 static int power_trace_init(struct trace_array *tr)
142 trace_power_enabled = 1;
143 tracing_power_register();
145 for_each_cpu(cpu, cpu_possible_mask)
146 tracing_reset(tr, cpu);
150 static enum print_line_t power_print_line(struct trace_iterator *iter)
153 struct trace_entry *entry = iter->ent;
154 struct trace_power *field ;
155 struct power_trace *it;
156 struct trace_seq *s = &iter->seq;
157 struct timespec stamp;
158 struct timespec duration;
160 trace_assign_type(field, entry);
161 it = &field->state_data;
162 stamp = ktime_to_timespec(it->stamp);
163 duration = ktime_to_timespec(ktime_sub(it->end, it->stamp));
165 if (entry->type == TRACE_POWER) {
166 if (it->type == POWER_CSTATE)
167 ret = trace_seq_printf(s, "[%5ld.%09ld] CSTATE: Going to C%i on cpu %i for %ld.%09ld\n",
170 it->state, iter->cpu,
173 if (it->type == POWER_PSTATE)
174 ret = trace_seq_printf(s, "[%5ld.%09ld] PSTATE: Going to P%i on cpu %i\n",
177 it->state, iter->cpu);
179 return TRACE_TYPE_PARTIAL_LINE;
180 return TRACE_TYPE_HANDLED;
182 return TRACE_TYPE_UNHANDLED;
185 static struct tracer power_tracer __read_mostly =
188 .init = power_trace_init,
189 .start = start_power_trace,
190 .stop = stop_power_trace,
191 .reset = stop_power_trace,
192 .print_line = power_print_line,
195 static int init_power_trace(void)
197 return register_tracer(&power_tracer);
199 device_initcall(init_power_trace);