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 ftrace_event_call
*call
= &event_power
;
40 struct ring_buffer_event
*event
;
41 struct trace_power
*entry
;
42 struct trace_array_cpu
*data
;
43 struct trace_array
*tr
= power_trace
;
45 if (!trace_power_enabled
)
49 it
->end
= ktime_get();
50 data
= tr
->data
[smp_processor_id()];
52 event
= trace_buffer_lock_reserve(tr
, TRACE_POWER
,
53 sizeof(*entry
), 0, 0);
56 entry
= ring_buffer_event_data(event
);
57 entry
->state_data
= *it
;
58 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
59 trace_buffer_unlock_commit(tr
, event
, 0, 0);
64 static void probe_power_mark(struct power_trace
*it
, unsigned int type
,
67 struct ftrace_event_call
*call
= &event_power
;
68 struct ring_buffer_event
*event
;
69 struct trace_power
*entry
;
70 struct trace_array_cpu
*data
;
71 struct trace_array
*tr
= power_trace
;
73 if (!trace_power_enabled
)
76 memset(it
, 0, sizeof(struct power_trace
));
79 it
->stamp
= ktime_get();
82 data
= tr
->data
[smp_processor_id()];
84 event
= trace_buffer_lock_reserve(tr
, TRACE_POWER
,
85 sizeof(*entry
), 0, 0);
88 entry
= ring_buffer_event_data(event
);
89 entry
->state_data
= *it
;
90 if (!filter_check_discard(call
, entry
, tr
->buffer
, event
))
91 trace_buffer_unlock_commit(tr
, event
, 0, 0);
96 static int tracing_power_register(void)
100 ret
= register_trace_power_start(probe_power_start
);
102 pr_info("power trace: Couldn't activate tracepoint"
103 " probe to trace_power_start\n");
106 ret
= register_trace_power_end(probe_power_end
);
108 pr_info("power trace: Couldn't activate tracepoint"
109 " probe to trace_power_end\n");
112 ret
= register_trace_power_mark(probe_power_mark
);
114 pr_info("power trace: Couldn't activate tracepoint"
115 " probe to trace_power_mark\n");
120 unregister_trace_power_end(probe_power_end
);
122 unregister_trace_power_start(probe_power_start
);
126 static void start_power_trace(struct trace_array
*tr
)
128 trace_power_enabled
= 1;
131 static void stop_power_trace(struct trace_array
*tr
)
133 trace_power_enabled
= 0;
136 static void power_trace_reset(struct trace_array
*tr
)
138 trace_power_enabled
= 0;
139 unregister_trace_power_start(probe_power_start
);
140 unregister_trace_power_end(probe_power_end
);
141 unregister_trace_power_mark(probe_power_mark
);
145 static int power_trace_init(struct trace_array
*tr
)
150 trace_power_enabled
= 1;
151 tracing_power_register();
153 for_each_cpu(cpu
, cpu_possible_mask
)
154 tracing_reset(tr
, cpu
);
158 static enum print_line_t
power_print_line(struct trace_iterator
*iter
)
161 struct trace_entry
*entry
= iter
->ent
;
162 struct trace_power
*field
;
163 struct power_trace
*it
;
164 struct trace_seq
*s
= &iter
->seq
;
165 struct timespec stamp
;
166 struct timespec duration
;
168 trace_assign_type(field
, entry
);
169 it
= &field
->state_data
;
170 stamp
= ktime_to_timespec(it
->stamp
);
171 duration
= ktime_to_timespec(ktime_sub(it
->end
, it
->stamp
));
173 if (entry
->type
== TRACE_POWER
) {
174 if (it
->type
== POWER_CSTATE
)
175 ret
= trace_seq_printf(s
, "[%5ld.%09ld] CSTATE: Going to C%i on cpu %i for %ld.%09ld\n",
178 it
->state
, iter
->cpu
,
181 if (it
->type
== POWER_PSTATE
)
182 ret
= trace_seq_printf(s
, "[%5ld.%09ld] PSTATE: Going to P%i on cpu %i\n",
185 it
->state
, iter
->cpu
);
187 return TRACE_TYPE_PARTIAL_LINE
;
188 return TRACE_TYPE_HANDLED
;
190 return TRACE_TYPE_UNHANDLED
;
193 static void power_print_header(struct seq_file
*s
)
195 seq_puts(s
, "# TIMESTAMP STATE EVENT\n");
196 seq_puts(s
, "# | | |\n");
199 static struct tracer power_tracer __read_mostly
=
202 .init
= power_trace_init
,
203 .start
= start_power_trace
,
204 .stop
= stop_power_trace
,
205 .reset
= power_trace_reset
,
206 .print_line
= power_print_line
,
207 .print_header
= power_print_header
,
210 static int init_power_trace(void)
212 return register_tracer(&power_tracer
);
214 device_initcall(init_power_trace
);