aboutsummaryrefslogtreecommitdiff
path: root/driver/gator_events_perf_pmu.c
blob: da76a9cb409bbd508a209f8428d02ccd216d29ab (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * Copyright (C) ARM Limited 2010-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.
 */

#include <linux/slab.h>
#include <linux/perf_event.h>
#include "gator.h"

// gator_events_armvX.c is used for Linux 2.6.x
#if GATOR_PERF_PMU_SUPPORT

static const char *pmnc_name;
int pmnc_counters;
int ccnt = 0;

#define CNTMAX (6+1)

static DEFINE_MUTEX(perf_mutex);

unsigned long pmnc_enabled[CNTMAX];
unsigned long pmnc_event[CNTMAX];
unsigned long pmnc_count[CNTMAX];
unsigned long pmnc_key[CNTMAX];

static DEFINE_PER_CPU(int[CNTMAX], perfCurr);
static DEFINE_PER_CPU(int[CNTMAX], perfPrev);
static DEFINE_PER_CPU(int[CNTMAX], perfPrevDelta);
static DEFINE_PER_CPU(int[CNTMAX * 2], perfCnt);
static DEFINE_PER_CPU(struct perf_event *[CNTMAX], pevent);
static DEFINE_PER_CPU(struct perf_event_attr *[CNTMAX], pevent_attr);

static void gator_events_perf_pmu_stop(void);

static int gator_events_perf_pmu_create_files(struct super_block *sb, struct dentry *root)
{
	struct dentry *dir;
	int i;

	for (i = 0; i < pmnc_counters; i++) {
		char buf[40];
		if (i == 0) {
			snprintf(buf, sizeof buf, "%s_ccnt", pmnc_name);
		} else {
			snprintf(buf, sizeof buf, "%s_cnt%d", pmnc_name, i-1);
		}
		dir = gatorfs_mkdir(sb, root, buf);
		if (!dir) {
			return -1;
		}
		gatorfs_create_ulong(sb, dir, "enabled", &pmnc_enabled[i]);
		gatorfs_create_ulong(sb, dir, "count", &pmnc_count[i]);
		gatorfs_create_ro_ulong(sb, dir, "key", &pmnc_key[i]);
		if (i > 0) {
			gatorfs_create_ulong(sb, dir, "event", &pmnc_event[i]);
		}
	}

	return 0;
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
static void dummy_handler(struct perf_event *event, int unused, struct perf_sample_data *data, struct pt_regs *regs)
#else
static void dummy_handler(struct perf_event *event, struct perf_sample_data *data, struct pt_regs *regs)
#endif
{
// Required as perf_event_create_kernel_counter() requires an overflow handler, even though all we do is poll
}

static int gator_events_perf_pmu_online(int** buffer)
{
	int cnt, len = 0, cpu = smp_processor_id();

	// read the counters and toss the invalid data, return zero instead
	for (cnt = 0; cnt < pmnc_counters; cnt++) {
		struct perf_event * ev = per_cpu(pevent, cpu)[cnt];
		if (ev != NULL && ev->state == PERF_EVENT_STATE_ACTIVE) {
			ev->pmu->read(ev);
			per_cpu(perfPrev, cpu)[cnt] = per_cpu(perfCurr, cpu)[cnt] = local64_read(&ev->count);
			per_cpu(perfPrevDelta, cpu)[cnt] = 0;
			per_cpu(perfCnt, cpu)[len++] = pmnc_key[cnt];
			per_cpu(perfCnt, cpu)[len++] = 0;
		}
	}

	if (buffer)
		*buffer = per_cpu(perfCnt, cpu);

	return len;
}

static void gator_events_perf_pmu_online_dispatch(int cpu)
{
	int cnt;

	for (cnt = 0; cnt < pmnc_counters; cnt++) {
		if (per_cpu(pevent, cpu)[cnt] != NULL || per_cpu(pevent_attr, cpu)[cnt] == 0)
			continue;

#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 1, 0)
		per_cpu(pevent, cpu)[cnt] = perf_event_create_kernel_counter(per_cpu(pevent_attr, cpu)[cnt], cpu, 0, dummy_handler);
#else
		per_cpu(pevent, cpu)[cnt] = perf_event_create_kernel_counter(per_cpu(pevent_attr, cpu)[cnt], cpu, 0, dummy_handler, 0);
#endif
		if (IS_ERR(per_cpu(pevent, cpu)[cnt])) {
			pr_debug("gator: unable to online a counter on cpu %d\n", cpu);
			per_cpu(pevent, cpu)[cnt] = NULL;
			continue;
		}

		if (per_cpu(pevent, cpu)[cnt]->state != PERF_EVENT_STATE_ACTIVE) {
			pr_debug("gator: inactive counter on cpu %d\n", cpu);
			perf_event_release_kernel(per_cpu(pevent, cpu)[cnt]);
			per_cpu(pevent, cpu)[cnt] = NULL;
			continue;
		}
	}
}

static void gator_events_perf_pmu_offline_dispatch(int cpu)
{
	int cnt;
	struct perf_event * pe;

	for (cnt = 0; cnt < pmnc_counters; cnt++) {
		pe = NULL;
		mutex_lock(&perf_mutex);
		if (per_cpu(pevent, cpu)[cnt]) {
			pe = per_cpu(pevent, cpu)[cnt];
			per_cpu(pevent, cpu)[cnt] = NULL;
		}
		mutex_unlock(&perf_mutex);

		if (pe) {
			perf_event_release_kernel(pe);
		}
	}
}

static int gator_events_perf_pmu_start(void)
{
	int cnt, cpu;
	u32 size = sizeof(struct perf_event_attr);

	for_each_present_cpu(cpu) {
		for (cnt = 0; cnt < pmnc_counters; cnt++) {
			per_cpu(pevent, cpu)[cnt] = NULL;
			if (!pmnc_enabled[cnt]) // Skip disabled counters
				continue;

			per_cpu(perfPrev, cpu)[cnt] = 0;
			per_cpu(perfCurr, cpu)[cnt] = 0;
			per_cpu(perfPrevDelta, cpu)[cnt] = 0;
			per_cpu(pevent_attr, cpu)[cnt] = kmalloc(size, GFP_KERNEL);
			if (!per_cpu(pevent_attr, cpu)[cnt]) {
				gator_events_perf_pmu_stop();
				return -1;
			}

			memset(per_cpu(pevent_attr, cpu)[cnt], 0, size);
			per_cpu(pevent_attr, cpu)[cnt]->type = PERF_TYPE_RAW;
			per_cpu(pevent_attr, cpu)[cnt]->size = size;
			per_cpu(pevent_attr, cpu)[cnt]->config = pmnc_event[cnt];
			per_cpu(pevent_attr, cpu)[cnt]->sample_period = 0;
			per_cpu(pevent_attr, cpu)[cnt]->pinned = 1;

			// handle special case for ccnt
			if (cnt == ccnt) {
				per_cpu(pevent_attr, cpu)[cnt]->type = PERF_TYPE_HARDWARE;
				per_cpu(pevent_attr, cpu)[cnt]->config = PERF_COUNT_HW_CPU_CYCLES;
			}
		}
	}

	return 0;
}

static void gator_events_perf_pmu_stop(void)
{
	unsigned int cnt, cpu;

	for_each_present_cpu(cpu) {
		for (cnt = 0; cnt < pmnc_counters; cnt++) {
			if (per_cpu(pevent_attr, cpu)[cnt]) {
				kfree(per_cpu(pevent_attr, cpu)[cnt]);
				per_cpu(pevent_attr, cpu)[cnt] = NULL;
			}
		}
	}

	for (cnt = 0; cnt < pmnc_counters; cnt++) {
		pmnc_enabled[cnt] = 0;
		pmnc_event[cnt] = 0;
		pmnc_count[cnt] = 0;
	}
}

static int gator_events_perf_pmu_read(int **buffer)
{
	int cnt, delta, len = 0;
	int cpu = smp_processor_id();

	for (cnt = 0; cnt < pmnc_counters; cnt++) {
		struct perf_event * ev = per_cpu(pevent, cpu)[cnt];
		if (ev != NULL && ev->state == PERF_EVENT_STATE_ACTIVE) {
			ev->pmu->read(ev);
			per_cpu(perfCurr, cpu)[cnt] = local64_read(&ev->count);
			delta = per_cpu(perfCurr, cpu)[cnt] - per_cpu(perfPrev, cpu)[cnt];
			if (delta != per_cpu(perfPrevDelta, cpu)[cnt]) {
				per_cpu(perfPrevDelta, cpu)[cnt] = delta;
				per_cpu(perfPrev, cpu)[cnt] = per_cpu(perfCurr, cpu)[cnt];
				per_cpu(perfCnt, cpu)[len++] = pmnc_key[cnt];
				if (delta < 0)
					delta *= -1;
				per_cpu(perfCnt, cpu)[len++] = delta;
			}
		}
	}

	if (buffer)
		*buffer = per_cpu(perfCnt, cpu);

	return len;
}

static struct gator_interface gator_events_perf_pmu_interface = {
	.create_files = gator_events_perf_pmu_create_files,
	.start = gator_events_perf_pmu_start,
	.stop = gator_events_perf_pmu_stop,
	.online = gator_events_perf_pmu_online,
	.online_dispatch = gator_events_perf_pmu_online_dispatch,
	.offline_dispatch = gator_events_perf_pmu_offline_dispatch,
	.read = gator_events_perf_pmu_read,
};

int gator_events_perf_pmu_init(void)
{
	unsigned int cnt;

	switch (gator_cpuid()) {
	case ARM1136:
	case ARM1156:
	case ARM1176:
		pmnc_name = "ARM_ARM11";
		pmnc_counters = 3;
		ccnt = 2;
		break;
	case ARM11MPCORE:
		pmnc_name = "ARM_ARM11MPCore";
		pmnc_counters = 3;
		break;
	case CORTEX_A5:
		pmnc_name = "ARM_Cortex-A5";
		pmnc_counters = 2;
		break;
	case CORTEX_A7:
		pmnc_name = "ARM_Cortex-A7";
		pmnc_counters = 4;
		break;
	case CORTEX_A8:
		pmnc_name = "ARM_Cortex-A8";
		pmnc_counters = 4;
		break;
	case CORTEX_A9:
		pmnc_name = "ARM_Cortex-A9";
		pmnc_counters = 6;
		break;
	case CORTEX_A15:
		pmnc_name = "ARM_Cortex-A15";
		pmnc_counters = 6;
		break;
	case SCORPION:
		pmnc_name = "Scorpion";
		pmnc_counters = 4;
		break;
	case SCORPIONMP:
		pmnc_name = "ScorpionMP";
		pmnc_counters = 4;
		break;
	case KRAITSIM:
	case KRAIT:
		pmnc_name = "Krait";
		pmnc_counters = 4;
		break;
	default:
		return -1;
	}

	pmnc_counters++; // CNT[n] + CCNT

	for (cnt = 0; cnt < CNTMAX; cnt++) {
		pmnc_enabled[cnt] = 0;
		pmnc_event[cnt] = 0;
		pmnc_count[cnt] = 0;
		pmnc_key[cnt] = gator_events_get_key();
	}

	return gator_events_install(&gator_events_perf_pmu_interface);
}

gator_events_init(gator_events_perf_pmu_init);
#endif