Blame src/libpfm-3.y/examples_v2.x/detect_pmcs.c

Packit 577717
/*
Packit 577717
 * detect_pmu_regs.c - detect unavailable PMD/PMC registers based on perfmon2 information
Packit 577717
 *
Packit 577717
 * Copyright (c) 2006-2007 Hewlett-Packard Development Company, L.P.
Packit 577717
 * Contributed by Stephane Eranian <eranian@hpl.hp.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
 * This file is part of libpfm, a performance monitoring support library for
Packit 577717
 * applications on Linux.
Packit 577717
 */
Packit 577717
#include <sys/types.h>
Packit 577717
#include <stdio.h>
Packit 577717
#include <stdlib.h>
Packit 577717
#include <errno.h>
Packit 577717
#include <string.h>
Packit 577717
#include <unistd.h>
Packit 577717
Packit 577717
#include <perfmon/perfmon.h>
Packit 577717
#include <perfmon/pfmlib.h>
Packit 577717
Packit 577717
/*
Packit 577717
 * The goal of this function is to help pfm_dispatch_events()
Packit 577717
 * in situations where not all PMC/PMD registers are available.
Packit 577717
 *
Packit 577717
 * It builds bitmasks of *unavailable* PMC/PMD registers.
Packit 577717
 * It can use an existing perfmon context file descriptor or if
Packit 577717
 * non is passed, it will create a temporary context to retrieve
Packit 577717
 * the information.
Packit 577717
 *
Packit 577717
 * Note that there is no guarantee that the registers marked
Packit 577717
 * as available will actually be available by the time the perfmon
Packit 577717
 * context is loaded. 
Packit 577717
 *
Packit 577717
 * arguments:
Packit 577717
 * 	fd : a perfmon context file descriptor, or -1
Packit 577717
 * 	r_pmcs: a bitmask for PMC availability, NULL if not needed
Packit 577717
 * 	r_pmcs: a bitmask for PMD availability, NULL if not needed
Packit 577717
 *
Packit 577717
 * return:
Packit 577717
 * 	-1: invalid file descriptor passed or cannot retrieve information
Packit 577717
 * 	 0: success
Packit 577717
 */
Packit 577717
int
Packit 577717
detect_unavail_pmu_regs(int fd, pfmlib_regmask_t *r_pmcs, pfmlib_regmask_t *r_pmds)
Packit 577717
{
Packit 577717
	pfarg_ctx_t ctx;
Packit 577717
	pfarg_setinfo_t	setf;
Packit 577717
	int ret, i, j, myfd, max;
Packit 577717
Packit 577717
	memset(&ctx, 0, sizeof(ctx));
Packit 577717
	memset(&setf, 0, sizeof(setf));
Packit 577717
	if (r_pmcs)
Packit 577717
		memset(r_pmcs, 0, sizeof(*r_pmcs));
Packit 577717
	if (r_pmds)
Packit 577717
		memset(r_pmds, 0, sizeof(*r_pmds));
Packit 577717
	/*
Packit 577717
	 * if no context descriptor is passed, then create
Packit 577717
	 * a temporary context
Packit 577717
	 */
Packit 577717
	if (fd == -1) {
Packit 577717
		myfd = pfm_create_context(&ctx, NULL, NULL, 0);
Packit 577717
		if (myfd == -1)
Packit 577717
			return -1;
Packit 577717
	} else {
Packit 577717
		myfd = fd;
Packit 577717
	}
Packit 577717
	/*
Packit 577717
	 * retrieve available register bitmasks from set0
Packit 577717
	 * which is guaranteed to exist for every context
Packit 577717
	 *
Packit 577717
	 * if myfd is bogus (passed by user) then we return
Packit 577717
	 * an error.
Packit 577717
	 */
Packit 577717
	ret = pfm_getinfo_evtsets(myfd, &setf, 1);
Packit 577717
	if (ret == 0) {
Packit 577717
		if (r_pmcs) {
Packit 577717
			max = PFMLIB_REG_BV < PFM_PMC_BV ? PFMLIB_REG_BV : PFM_PMC_BV;
Packit 577717
			for(i=0; i < max; i++) {
Packit 577717
				for(j=0; j < 64; j++) {
Packit 577717
					if ((setf.set_avail_pmcs[i] & (1ULL << j)) == 0)
Packit 577717
						pfm_regmask_set(r_pmcs, (i<<6)+j);
Packit 577717
				}
Packit 577717
			}
Packit 577717
		}
Packit 577717
		if (r_pmds) {
Packit 577717
			max = PFMLIB_REG_BV < PFM_PMD_BV ? PFMLIB_REG_BV : PFM_PMD_BV;
Packit 577717
			for(i=0; i < max; i++) {
Packit 577717
				for(j=0; j < 64; j++) {
Packit 577717
					if ((setf.set_avail_pmds[i] & (1ULL << j)) == 0)
Packit 577717
						pfm_regmask_set(r_pmds, (i<<6)+j);
Packit 577717
				}
Packit 577717
			}
Packit 577717
		}
Packit 577717
	}
Packit 577717
	if (fd == -1)
Packit 577717
		close(myfd);
Packit 577717
	return ret;
Packit 577717
}