Blame src/libpfm4/perf_examples/evt2raw.c

Packit 577717
/*
Packit 577717
 * evt2raw.c - example which converts an event string (event + modifiers) to
Packit 577717
 * a raw event code usable by the perf tool.
Packit 577717
 *
Packit 577717
 * Copyright (c) 2010 IBM Corp.
Packit 577717
 * Contributed by Corey Ashford <cjashfor@us.ibm.com>
Packit 577717
 *
Packit 577717
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit 577717
 * of this software and associated documentation files (the "Software"), to deal
Packit 577717
 * in the Software without restriction, including without limitation the rights
Packit 577717
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 577717
 * of the Software, and to permit persons to whom the Software is furnished to do so,
Packit 577717
 * subject to the following conditions:
Packit 577717
 *
Packit 577717
 * The above copyright notice and this permission notice shall be included in all
Packit 577717
 * copies or substantial portions of the Software.
Packit 577717
 *
Packit 577717
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
Packit 577717
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
Packit 577717
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 577717
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Packit 577717
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
Packit 577717
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit 577717
 */
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include <string.h>
Packit 577717
#include <unistd.h>
Packit 577717
#include <err.h>
Packit 577717
#include <perfmon/pfmlib_perf_event.h>
Packit 577717
Packit 577717
static void
Packit 577717
usage(void)
Packit 577717
{
Packit 577717
	printf("usage: evt2raw [-v] <event>\n"
Packit 577717
		"<event> is the symbolic event, including modifiers, to "
Packit 577717
		"translate to a raw code.\n");
Packit 577717
}
Packit 577717
Packit 577717
#define MAX_MODIFIER_CHARS 5  /* u,k,h plus the colon and null terminator */
Packit 577717
int
Packit 577717
main(int argc, char **argv)
Packit 577717
{
Packit 577717
	int ret, c, verbose = 0;
Packit 577717
	struct perf_event_attr pea;
Packit 577717
	char *event_str, *fstr = NULL;
Packit 577717
	char modifiers[MAX_MODIFIER_CHARS];
Packit 577717
Packit 577717
	if (argc < 2) {
Packit 577717
		usage();
Packit 577717
		return 1;
Packit 577717
	}
Packit 577717
	while ( (c=getopt(argc, argv, "hv")) != -1) {
Packit 577717
		switch(c) {
Packit 577717
		case 'h':
Packit 577717
			usage();
Packit 577717
			exit(0);
Packit 577717
		case 'v':
Packit 577717
			verbose = 1;
Packit 577717
			break;
Packit 577717
		default:
Packit 577717
			exit(1);
Packit 577717
		}
Packit 577717
	}
Packit 577717
	event_str = argv[optind];
Packit 577717
Packit 577717
	ret = pfm_initialize();
Packit 577717
	if (ret != PFM_SUCCESS)
Packit 577717
		errx(1, "Internal error: pfm_initialize returned %s",
Packit 577717
			pfm_strerror(ret));
Packit 577717
Packit 577717
	pea.size = sizeof(struct perf_event_attr);
Packit 577717
	ret = pfm_get_perf_event_encoding(event_str, PFM_PLM0|PFM_PLM3|PFM_PLMH, &pea,
Packit 577717
		&fstr, NULL);
Packit 577717
	if (ret != PFM_SUCCESS)
Packit 577717
		errx(1, "Error: pfm_get_perf_encoding returned %s",
Packit 577717
			pfm_strerror(ret));
Packit 577717
Packit 577717
	if (pea.type != PERF_TYPE_RAW)
Packit 577717
		errx(1, "Error: %s is not a raw hardware event", event_str);
Packit 577717
Packit 577717
	modifiers[0] = '\0';
Packit 577717
	if (pea.exclude_user | pea.exclude_kernel | pea.exclude_hv) {
Packit 577717
		strcat(modifiers, ":");
Packit 577717
		if (!pea.exclude_user)
Packit 577717
			strcat(modifiers, "u");
Packit 577717
		if (!pea.exclude_kernel)
Packit 577717
			strcat(modifiers, "k");
Packit 577717
		if (!pea.exclude_hv)
Packit 577717
			strcat(modifiers, "h");
Packit 577717
	}
Packit 577717
Packit 577717
	if (verbose)
Packit 577717
		printf("r%"PRIx64"%s\t%s\n", pea.config, modifiers, fstr);
Packit 577717
	else
Packit 577717
		printf("r%"PRIx64"%s\n", pea.config, modifiers);
Packit 577717
Packit 577717
	if (fstr)
Packit 577717
		free(fstr);
Packit 577717
Packit 577717
	return 0;
Packit 577717
}