parse-events.c 7.3 KB
Newer Older
1 2 3 4 5 6

#include "../perf.h"
#include "util.h"
#include "parse-options.h"
#include "parse-events.h"
#include "exec_cmd.h"
7
#include "string.h"
8

9 10
extern char *strcasestr(const char *haystack, const char *needle);

11
int					nr_counters;
12

13
struct perf_counter_attr		attrs[MAX_COUNTERS];
14 15

struct event_symbol {
16 17
	u8	type;
	u64	config;
18
	char	*symbol;
19
	char	*alias;
20 21
};

22 23
#define CHW(x) .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_##x
#define CSW(x) .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_##x
24

25
static struct event_symbol event_symbols[] = {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  { CHW(CPU_CYCLES),		"cpu-cycles",		"cycles"	},
  { CHW(INSTRUCTIONS),		"instructions",		""		},
  { CHW(CACHE_REFERENCES),	"cache-references",	""		},
  { CHW(CACHE_MISSES),		"cache-misses",		""		},
  { CHW(BRANCH_INSTRUCTIONS),	"branch-instructions",	"branches"	},
  { CHW(BRANCH_MISSES),		"branch-misses",	""		},
  { CHW(BUS_CYCLES),		"bus-cycles",		""		},

  { CSW(CPU_CLOCK),		"cpu-clock",		""		},
  { CSW(TASK_CLOCK),		"task-clock",		""		},
  { CSW(PAGE_FAULTS),		"page-faults",		""		},
  { CSW(PAGE_FAULTS),		"faults",		""		},
  { CSW(PAGE_FAULTS_MIN),	"minor-faults",		""		},
  { CSW(PAGE_FAULTS_MAJ),	"major-faults",		""		},
  { CSW(CONTEXT_SWITCHES),	"context-switches",	"cs"		},
  { CSW(CPU_MIGRATIONS),	"cpu-migrations",	"migrations"	},
42 43
};

44 45 46 47 48 49 50 51 52
#define __PERF_COUNTER_FIELD(config, name) \
	((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT)

#define PERF_COUNTER_RAW(config)	__PERF_COUNTER_FIELD(config, RAW)
#define PERF_COUNTER_CONFIG(config)	__PERF_COUNTER_FIELD(config, CONFIG)
#define PERF_COUNTER_TYPE(config)	__PERF_COUNTER_FIELD(config, TYPE)
#define PERF_COUNTER_ID(config)		__PERF_COUNTER_FIELD(config, EVENT)

static char *hw_event_names[] = {
53
	"cycles",
54
	"instructions",
55 56
	"cache-references",
	"cache-misses",
57
	"branches",
58 59
	"branch-misses",
	"bus-cycles",
60 61 62
};

static char *sw_event_names[] = {
I
Ingo Molnar 已提交
63 64
	"cpu-clock-msecs",
	"task-clock-msecs",
65 66 67 68 69
	"page-faults",
	"context-switches",
	"CPU-migrations",
	"minor-faults",
	"major-faults",
70 71
};

72 73 74
#define MAX_ALIASES 8

static char *hw_cache [][MAX_ALIASES] = {
75
	{ "L1-data"		, "l1-d", "l1d"					},
76 77 78 79 80
	{ "L1-instruction"	, "l1-i", "l1i"					},
	{ "L2"			, "l2"						},
	{ "Data-TLB"		, "dtlb", "d-tlb"				},
	{ "Instruction-TLB"	, "itlb", "i-tlb"				},
	{ "Branch"		, "bpu" , "btb", "bpc"				},
81 82 83
};

static char *hw_cache_op [][MAX_ALIASES] = {
84 85 86
	{ "Load"		, "read"					},
	{ "Store"		, "write"					},
	{ "Prefetch"		, "speculative-read", "speculative-load"	},
87 88 89
};

static char *hw_cache_result [][MAX_ALIASES] = {
90 91
	{ "Reference"		, "ops", "access"				},
	{ "Miss"								},
92 93
};

94
char *event_name(int counter)
95
{
96
	u64 config = attrs[counter].config;
97
	int type = attrs[counter].type;
98 99
	static char buf[32];

100 101
	if (attrs[counter].type == PERF_TYPE_RAW) {
		sprintf(buf, "raw 0x%llx", config);
102 103 104 105 106
		return buf;
	}

	switch (type) {
	case PERF_TYPE_HARDWARE:
107
		if (config < PERF_COUNT_HW_MAX)
108
			return hw_event_names[config];
109 110
		return "unknown-hardware";

111
	case PERF_TYPE_HW_CACHE: {
112
		u8 cache_type, cache_op, cache_result;
113 114 115 116 117 118 119
		static char name[100];

		cache_type   = (config >>  0) & 0xff;
		if (cache_type > PERF_COUNT_HW_CACHE_MAX)
			return "unknown-ext-hardware-cache-type";

		cache_op     = (config >>  8) & 0xff;
120 121
		if (cache_op > PERF_COUNT_HW_CACHE_OP_MAX)
			return "unknown-ext-hardware-cache-op";
122 123

		cache_result = (config >> 16) & 0xff;
124 125
		if (cache_result > PERF_COUNT_HW_CACHE_RESULT_MAX)
			return "unknown-ext-hardware-cache-result";
126

127
		sprintf(name, "%s-Cache-%s-%ses",
128 129 130 131 132 133 134
			hw_cache[cache_type][0],
			hw_cache_op[cache_op][0],
			hw_cache_result[cache_result][0]);

		return name;
	}

135
	case PERF_TYPE_SOFTWARE:
136
		if (config < PERF_COUNT_SW_MAX)
137
			return sw_event_names[config];
138 139 140 141 142 143 144 145 146
		return "unknown-software";

	default:
		break;
	}

	return "unknown";
}

147 148 149 150 151 152 153 154 155 156 157 158 159
static int parse_aliases(const char *str, char *names[][MAX_ALIASES], int size)
{
	int i, j;

	for (i = 0; i < size; i++) {
		for (j = 0; j < MAX_ALIASES; j++) {
			if (!names[i][j])
				break;
			if (strcasestr(str, names[i][j]))
				return i;
		}
	}

160
	return -1;
161 162 163 164
}

static int parse_generic_hw_symbols(const char *str, struct perf_counter_attr *attr)
{
165
	int cache_type = -1, cache_op = 0, cache_result = 0;
166 167 168 169 170 171 172 173 174 175 176 177 178

	cache_type = parse_aliases(str, hw_cache, PERF_COUNT_HW_CACHE_MAX);
	/*
	 * No fallback - if we cannot get a clear cache type
	 * then bail out:
	 */
	if (cache_type == -1)
		return -EINVAL;

	cache_op = parse_aliases(str, hw_cache_op, PERF_COUNT_HW_CACHE_OP_MAX);
	/*
	 * Fall back to reads:
	 */
179 180
	if (cache_op == -1)
		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

	cache_result = parse_aliases(str, hw_cache_result,
					PERF_COUNT_HW_CACHE_RESULT_MAX);
	/*
	 * Fall back to accesses:
	 */
	if (cache_result == -1)
		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;

	attr->config = cache_type | (cache_op << 8) | (cache_result << 16);
	attr->type = PERF_TYPE_HW_CACHE;

	return 0;
}

196 197 198 199 200 201 202 203 204 205 206 207 208
static int check_events(const char *str, unsigned int i)
{
	if (!strncmp(str, event_symbols[i].symbol,
		     strlen(event_symbols[i].symbol)))
		return 1;

	if (strlen(event_symbols[i].alias))
		if (!strncmp(str, event_symbols[i].alias,
		      strlen(event_symbols[i].alias)))
			return 1;
	return 0;
}

209 210 211 212
/*
 * Each event can have multiple symbolic names.
 * Symbolic names are (almost) exactly matched.
 */
213
static int parse_event_symbols(const char *str, struct perf_counter_attr *attr)
214
{
215
	u64 config, id;
216 217
	int type;
	unsigned int i;
218
	const char *sep, *pstr;
219

220 221 222 223 224 225
	if (str[0] == 'r' && hex2u64(str + 1, &config) > 0) {
		attr->type = PERF_TYPE_RAW;
		attr->config = config;

		return 0;
	}
226

227 228 229 230 231 232 233 234 235 236
	pstr = str;
	sep = strchr(pstr, ':');
	if (sep) {
		type = atoi(pstr);
		pstr = sep + 1;
		id = atoi(pstr);
		sep = strchr(pstr, ':');
		if (sep) {
			pstr = sep + 1;
			if (strchr(pstr, 'k'))
237
				attr->exclude_user = 1;
238
			if (strchr(pstr, 'u'))
239
				attr->exclude_kernel = 1;
240
		}
241 242 243 244
		attr->type = type;
		attr->config = id;

		return 0;
245
	}
246 247

	for (i = 0; i < ARRAY_SIZE(event_symbols); i++) {
248
		if (check_events(str, i)) {
249 250 251 252 253
			attr->type = event_symbols[i].type;
			attr->config = event_symbols[i].config;

			return 0;
		}
254 255
	}

256
	return parse_generic_hw_symbols(str, attr);
257 258 259 260
}

int parse_events(const struct option *opt, const char *str, int unset)
{
261 262
	struct perf_counter_attr attr;
	int ret;
263

264
	memset(&attr, 0, sizeof(attr));
265 266 267 268
again:
	if (nr_counters == MAX_COUNTERS)
		return -1;

269
	ret = parse_event_symbols(str, &attr);
270 271
	if (ret < 0)
		return ret;
272

273
	attrs[nr_counters] = attr;
274 275 276 277 278 279 280 281 282 283 284
	nr_counters++;

	str = strstr(str, ",");
	if (str) {
		str++;
		goto again;
	}

	return 0;
}

285 286 287 288 289 290 291 292
static const char * const event_type_descriptors[] = {
	"",
	"Hardware event",
	"Software event",
	"Tracepoint event",
	"Hardware cache event",
};

293
/*
294
 * Print the help text for the event symbols:
295
 */
296
void print_events(void)
297
{
298 299
	struct event_symbol *syms = event_symbols;
	unsigned int i, type, prev_type = -1;
300
	char name[40];
301

302 303
	fprintf(stderr, "\n");
	fprintf(stderr, "List of pre-defined events (to be used in -e):\n");
304

305 306 307 308
	for (i = 0; i < ARRAY_SIZE(event_symbols); i++, syms++) {
		type = syms->type + 1;
		if (type > ARRAY_SIZE(event_type_descriptors))
			type = 0;
309

310 311
		if (type != prev_type)
			fprintf(stderr, "\n");
312

313 314 315 316 317
		if (strlen(syms->alias))
			sprintf(name, "%s OR %s", syms->symbol, syms->alias);
		else
			strcpy(name, syms->symbol);
		fprintf(stderr, "  %-40s [%s]\n", name,
318
			event_type_descriptors[type]);
319

320
		prev_type = type;
321 322
	}

323
	fprintf(stderr, "\n");
324
	fprintf(stderr, "  %-40s [raw hardware event descriptor]\n",
325 326 327 328
		"rNNN");
	fprintf(stderr, "\n");

	exit(129);
329
}