summaryrefslogtreecommitdiffstats
blob: 630d142bf70e610ea8f4c248a13f75c9ca23379f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 * Copyright (C) ARM Limited 2012. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

static void marshal_summary(long long timestamp, long long uptime) {
	int cpu = 0;
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_SUMMARY);
	gator_buffer_write_packed_int64(cpu, BACKTRACE_BUF, timestamp);
	gator_buffer_write_packed_int64(cpu, BACKTRACE_BUF, uptime);
}

static bool marshal_cookie_header(char* text) {
	int cpu = smp_processor_id();
	return buffer_check_space(cpu, BACKTRACE_BUF, strlen(text) + 2 * MAXSIZE_PACK32);
}

static void marshal_cookie(int cookie, char* text) {
	int cpu = smp_processor_id();
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_COOKIE);
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, cookie);
	gator_buffer_write_string(cpu, BACKTRACE_BUF, text);
}

static void marshal_pid_name(int pid, char* name) {
	unsigned long flags, cpu;
	local_irq_save(flags);
	cpu = smp_processor_id();
	if (buffer_check_space(cpu, BACKTRACE_BUF, TASK_COMM_LEN + 2 * MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_PID_NAME);
		gator_buffer_write_packed_int64(cpu, BACKTRACE_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, pid);
		gator_buffer_write_string(cpu, BACKTRACE_BUF, name);
	}
	local_irq_restore(flags);
}

static bool marshal_backtrace_header(int exec_cookie, int tgid, int pid, int inKernel) {
	int cpu = smp_processor_id();
	if (buffer_check_space(cpu, BACKTRACE_BUF, gator_backtrace_depth * 2 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_START_BACKTRACE);
		gator_buffer_write_packed_int64(cpu, BACKTRACE_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, exec_cookie);
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, tgid); 
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, pid);
		gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, inKernel);
		return true;
	}

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, BACKTRACE_BUF);

	return false;
}

static void marshal_backtrace(int address, int cookie) {
	int cpu = smp_processor_id();
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, address);
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, cookie);
}

static void marshal_backtrace_footer(void) {
	int cpu = smp_processor_id();
	gator_buffer_write_packed_int(cpu, BACKTRACE_BUF, MESSAGE_END_BACKTRACE);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, BACKTRACE_BUF);
}

static bool marshal_event_header(void) {
	unsigned long flags, cpu = smp_processor_id();
	bool retval = false;
	
	local_irq_save(flags);
	if (buffer_check_space(cpu, COUNTER_BUF, MAXSIZE_PACK32 + MAXSIZE_PACK64)) {
		gator_buffer_write_packed_int(cpu, COUNTER_BUF, 0); // key of zero indicates a timestamp
		gator_buffer_write_packed_int64(cpu, COUNTER_BUF, gator_get_time());
		retval = true;
	}
	local_irq_restore(flags);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, COUNTER_BUF);

	return retval;
}

static void marshal_event(int len, int* buffer) {
	unsigned long i, flags, cpu = smp_processor_id();

	if (len <= 0)
		return;
	
	// length must be even since all data is a (key, value) pair
	if (len & 0x1) {
		pr_err("gator: invalid counter data detected and discarded");
		return;
	}

	// events must be written in key,value pairs
	for (i = 0; i < len; i += 2) {
		local_irq_save(flags);
		if (!buffer_check_space(cpu, COUNTER_BUF, MAXSIZE_PACK32 * 2)) {
			local_irq_restore(flags);
			break;
		}
		gator_buffer_write_packed_int(cpu, COUNTER_BUF, buffer[i]);
		gator_buffer_write_packed_int(cpu, COUNTER_BUF, buffer[i + 1]);
		local_irq_restore(flags);
	}

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, COUNTER_BUF);
}

static void marshal_event64(int len, long long* buffer64) {
	unsigned long i, flags, cpu = smp_processor_id();

	if (len <= 0)
		return;
	
	// length must be even since all data is a (key, value) pair
	if (len & 0x1) {
		pr_err("gator: invalid counter data detected and discarded");
		return;
	}

	// events must be written in key,value pairs
	for (i = 0; i < len; i += 2) {
		local_irq_save(flags);
		if (!buffer_check_space(cpu, COUNTER_BUF, MAXSIZE_PACK64 * 2)) {
			local_irq_restore(flags);
			break;
		}
		gator_buffer_write_packed_int64(cpu, COUNTER_BUF, buffer64[i]);
		gator_buffer_write_packed_int64(cpu, COUNTER_BUF, buffer64[i + 1]);
		local_irq_restore(flags);
	}

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, COUNTER_BUF);
}

#if GATOR_CPU_FREQ_SUPPORT
static void marshal_event_single(int core, int key, int value) {
	unsigned long flags, cpu;
	
	local_irq_save(flags);
	cpu = smp_processor_id();
	if (buffer_check_space(cpu, COUNTER2_BUF, MAXSIZE_PACK64 + MAXSIZE_PACK32 * 3)) {
		gator_buffer_write_packed_int64(cpu, COUNTER2_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, COUNTER2_BUF, core);
		gator_buffer_write_packed_int(cpu, COUNTER2_BUF, key);
		gator_buffer_write_packed_int(cpu, COUNTER2_BUF, value);
	}
	local_irq_restore(flags);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, COUNTER2_BUF);
}
#endif

static void marshal_sched_gpu(int type, int unit, int core, int tgid, int pid) {
	unsigned long cpu = smp_processor_id(), flags;

	if (!per_cpu(gator_buffer, cpu)[GPU_TRACE_BUF])
		return;

	local_irq_save(flags);
	if (buffer_check_space(cpu, GPU_TRACE_BUF, MAXSIZE_PACK64 + 5 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, GPU_TRACE_BUF, type);
		gator_buffer_write_packed_int64(cpu, GPU_TRACE_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, GPU_TRACE_BUF, unit);
		gator_buffer_write_packed_int(cpu, GPU_TRACE_BUF, core);
		gator_buffer_write_packed_int(cpu, GPU_TRACE_BUF, tgid);
		gator_buffer_write_packed_int(cpu, GPU_TRACE_BUF, pid);
	}
	local_irq_restore(flags);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, GPU_TRACE_BUF);
}

static void marshal_sched_trace(int type, int pid, int tgid, int cookie, int state) {
	unsigned long cpu = smp_processor_id(), flags;

	if (!per_cpu(gator_buffer, cpu)[SCHED_TRACE_BUF])
		return;

	local_irq_save(flags);
	if (buffer_check_space(cpu, SCHED_TRACE_BUF, MAXSIZE_PACK64 + 5 * MAXSIZE_PACK32)) {
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, type);
		gator_buffer_write_packed_int64(cpu, SCHED_TRACE_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, pid);
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, tgid);
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, cookie);
		gator_buffer_write_packed_int(cpu, SCHED_TRACE_BUF, state);
	}
	local_irq_restore(flags);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, SCHED_TRACE_BUF);
}

#if GATOR_CPU_FREQ_SUPPORT
static void marshal_wfi(int core, int state) {
	unsigned long flags, cpu;
	
	local_irq_save(flags);
	cpu = smp_processor_id();
	if (buffer_check_space(cpu, WFI_BUF, MAXSIZE_PACK64 + MAXSIZE_PACK32 * 2)) {
		gator_buffer_write_packed_int64(cpu, WFI_BUF, gator_get_time());
		gator_buffer_write_packed_int(cpu, WFI_BUF, core);
		gator_buffer_write_packed_int(cpu, WFI_BUF, state);
	}
	local_irq_restore(flags);

	// Check and commit; commit is set to occur once buffer is 3/4 full
	buffer_check(cpu, WFI_BUF);
}
#endif

static void marshal_frame(int cpu, int buftype, int frame) {
	// add response type
	if (gator_response_type > 0) {
		gator_buffer_write_packed_int(cpu, buftype, gator_response_type);
	}

	// leave space for 4-byte unpacked length
	per_cpu(gator_buffer_write, cpu)[buftype] = (per_cpu(gator_buffer_write, cpu)[buftype] + 4) & gator_buffer_mask[buftype];

	// add frame type and core number
	gator_buffer_write_packed_int(cpu, buftype, frame);
	gator_buffer_write_packed_int(cpu, buftype, cpu);
}