trace_power.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * ring buffer based C-state tracer
 *
 * Arjan van de Ven <arjan@linux.intel.com>
 * Copyright (C) 2008 Intel Corporation
 *
 * Much is borrowed from trace_boot.c which is
 * Copyright (C) 2008 Frederic Weisbecker <fweisbec@gmail.com>
 *
 */

#include <linux/init.h>
#include <linux/debugfs.h>
14
#include <trace/power.h>
15 16 17 18
#include <linux/kallsyms.h>
#include <linux/module.h>

#include "trace.h"
19
#include "trace_output.h"
20 21 22 23

static struct trace_array *power_trace;
static int __read_mostly trace_power_enabled;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
static void probe_power_start(struct power_trace *it, unsigned int type,
				unsigned int level)
{
	if (!trace_power_enabled)
		return;

	memset(it, 0, sizeof(struct power_trace));
	it->state = level;
	it->type = type;
	it->stamp = ktime_get();
}


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 ring_buffer *buffer;
42 43 44 45 46 47 48
	struct trace_power *entry;
	struct trace_array_cpu *data;
	struct trace_array *tr = power_trace;

	if (!trace_power_enabled)
		return;

49 50
	buffer = tr->buffer;

51 52 53 54
	preempt_disable();
	it->end = ktime_get();
	data = tr->data[smp_processor_id()];

55
	event = trace_buffer_lock_reserve(buffer, TRACE_POWER,
56 57 58 59 60
					  sizeof(*entry), 0, 0);
	if (!event)
		goto out;
	entry	= ring_buffer_event_data(event);
	entry->state_data = *it;
61 62
	if (!filter_check_discard(call, entry, buffer, event))
		trace_buffer_unlock_commit(buffer, event, 0, 0);
63 64 65 66 67 68 69
 out:
	preempt_enable();
}

static void probe_power_mark(struct power_trace *it, unsigned int type,
				unsigned int level)
{
70
	struct ftrace_event_call *call = &event_power;
71
	struct ring_buffer_event *event;
72
	struct ring_buffer *buffer;
73 74 75 76 77 78 79
	struct trace_power *entry;
	struct trace_array_cpu *data;
	struct trace_array *tr = power_trace;

	if (!trace_power_enabled)
		return;

80 81
	buffer = tr->buffer;

82 83 84 85 86 87 88 89
	memset(it, 0, sizeof(struct power_trace));
	it->state = level;
	it->type = type;
	it->stamp = ktime_get();
	preempt_disable();
	it->end = it->stamp;
	data = tr->data[smp_processor_id()];

90
	event = trace_buffer_lock_reserve(buffer, TRACE_POWER,
91 92 93 94 95
					  sizeof(*entry), 0, 0);
	if (!event)
		goto out;
	entry	= ring_buffer_event_data(event);
	entry->state_data = *it;
96 97
	if (!filter_check_discard(call, entry, buffer, event))
		trace_buffer_unlock_commit(buffer, event, 0, 0);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
 out:
	preempt_enable();
}

static int tracing_power_register(void)
{
	int ret;

	ret = register_trace_power_start(probe_power_start);
	if (ret) {
		pr_info("power trace: Couldn't activate tracepoint"
			" probe to trace_power_start\n");
		return ret;
	}
	ret = register_trace_power_end(probe_power_end);
	if (ret) {
		pr_info("power trace: Couldn't activate tracepoint"
			" probe to trace_power_end\n");
		goto fail_start;
	}
	ret = register_trace_power_mark(probe_power_mark);
	if (ret) {
		pr_info("power trace: Couldn't activate tracepoint"
			" probe to trace_power_mark\n");
		goto fail_end;
	}
	return ret;
fail_end:
	unregister_trace_power_end(probe_power_end);
fail_start:
	unregister_trace_power_start(probe_power_start);
	return ret;
}
131 132 133 134 135 136 137

static void start_power_trace(struct trace_array *tr)
{
	trace_power_enabled = 1;
}

static void stop_power_trace(struct trace_array *tr)
138 139 140 141 142
{
	trace_power_enabled = 0;
}

static void power_trace_reset(struct trace_array *tr)
143 144
{
	trace_power_enabled = 0;
145 146 147
	unregister_trace_power_start(probe_power_start);
	unregister_trace_power_end(probe_power_end);
	unregister_trace_power_mark(probe_power_mark);
148 149 150 151 152 153 154 155
}


static int power_trace_init(struct trace_array *tr)
{
	power_trace = tr;

	trace_power_enabled = 1;
156
	tracing_power_register();
157

158
	tracing_reset_online_cpus(tr);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	return 0;
}

static enum print_line_t power_print_line(struct trace_iterator *iter)
{
	int ret = 0;
	struct trace_entry *entry = iter->ent;
	struct trace_power *field ;
	struct power_trace *it;
	struct trace_seq *s = &iter->seq;
	struct timespec stamp;
	struct timespec duration;

	trace_assign_type(field, entry);
	it = &field->state_data;
	stamp = ktime_to_timespec(it->stamp);
	duration = ktime_to_timespec(ktime_sub(it->end, it->stamp));

	if (entry->type == TRACE_POWER) {
		if (it->type == POWER_CSTATE)
			ret = trace_seq_printf(s, "[%5ld.%09ld] CSTATE: Going to C%i on cpu %i for %ld.%09ld\n",
					  stamp.tv_sec,
					  stamp.tv_nsec,
					  it->state, iter->cpu,
					  duration.tv_sec,
					  duration.tv_nsec);
		if (it->type == POWER_PSTATE)
			ret = trace_seq_printf(s, "[%5ld.%09ld] PSTATE: Going to P%i on cpu %i\n",
					  stamp.tv_sec,
					  stamp.tv_nsec,
					  it->state, iter->cpu);
		if (!ret)
			return TRACE_TYPE_PARTIAL_LINE;
		return TRACE_TYPE_HANDLED;
	}
	return TRACE_TYPE_UNHANDLED;
}

Z
Zhaolei 已提交
197 198 199 200 201 202
static void power_print_header(struct seq_file *s)
{
	seq_puts(s, "#   TIMESTAMP      STATE  EVENT\n");
	seq_puts(s, "#       |            |      |\n");
}

203 204 205 206 207 208
static struct tracer power_tracer __read_mostly =
{
	.name		= "power",
	.init		= power_trace_init,
	.start		= start_power_trace,
	.stop		= stop_power_trace,
209
	.reset		= power_trace_reset,
210
	.print_line	= power_print_line,
Z
Zhaolei 已提交
211
	.print_header	= power_print_header,
212 213 214 215 216 217 218
};

static int init_power_trace(void)
{
	return register_tracer(&power_tracer);
}
device_initcall(init_power_trace);