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

Packit 577717
/*
Packit 577717
 * detect_pmu_regs.c - detect unavailable PMD/PMC registers based on perfmon3 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
int
Packit 577717
get_sif(int flags, pfarg_sinfo_t *sif)
Packit 577717
{
Packit 577717
	int fd;
Packit 577717
Packit 577717
	/* initialize as all available */
Packit 577717
	if (sif) {
Packit 577717
		memset(sif->sif_avail_pmcs, 0xff, sizeof(sif->sif_avail_pmcs));
Packit 577717
		memset(sif->sif_avail_pmds, 0xff, sizeof(sif->sif_avail_pmds));
Packit 577717
	}
Packit 577717
	fd = pfm_create(flags, sif);
Packit 577717
	if (fd > -1)
Packit 577717
		close(fd);
Packit 577717
	return fd > -1 ? 0 : -1;
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 from the
Packit 577717
 * information returned by pfm_create_session().
Packit 577717
 *
Packit 577717
 * arguments:
Packit 577717
 * 	sif: pfarg_sinfo_t pointer
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
void
Packit 577717
detect_unavail_pmu_regs(pfarg_sinfo_t *sif, pfmlib_regmask_t *r_pmcs, pfmlib_regmask_t *r_pmds)
Packit 577717
{
Packit 577717
	int i, j, max;
Packit 577717
Packit 577717
	if (r_pmcs) {
Packit 577717
		memset(r_pmcs, 0, sizeof(*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 ((sif->sif_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
		memset(r_pmds, 0, sizeof(*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 ((sif->sif_avail_pmds[i] & (1ULL << j)) == 0)
Packit 577717
					pfm_regmask_set(r_pmds, (i<<6)+j);
Packit 577717
			}
Packit 577717
		}
Packit 577717
	}
Packit 577717
}