FreeBSD/sys/dev/hwpmc/hwpmc_mod.c
/*
 * Check if a PMC at row index 'ri' can be allocated to the current
 * process.
 *
 * Allocation can fail if:
 *   - the current process is already being profiled by a PMC at index 'ri',
 *     attached to it via OP_PMCATTACH.
 *   - the current process has already allocated a PMC at index 'ri'
 *     via OP_ALLOCATE.
 */
Check if a PMC at row index 'ri' can be allocated to the current
process.

Allocation can fail if:
- the current process is already being profiled by a PMC at index 'ri',
attached to it via OP_PMCATTACH.
- the current process has already allocated a PMC at index 'ri'
via OP_ALLOCATE.
Check if a PMC at row index ' ri ' can be allocated to the current process . Allocation can fail if : - the current process is already being profiled by a PMC at index ' ri ' attached to it via OP_PMCATTACH . - the current process has already allocated a PMC at index ' ri ' via OP_ALLOCATE .
static int
pmc_can_allocate_rowindex(struct proc *p, unsigned int ri, int cpu)
{
	enum pmc_mode mode;
	struct pmc *pm;
	struct pmc_owner *po;
	struct pmc_process *pp;
	PMCDBG5(PMC,ALR,1, "can-allocate-rowindex proc=%p (%d, %s) ri=%d "
	    "cpu=%d", p, p->p_pid, p->p_comm, ri, cpu);
	if ((po = pmc_find_owner_descriptor(p)) != NULL)
		LIST_FOREACH(pm, &po->po_pmcs, pm_next) {
		    if (PMC_TO_ROWINDEX(pm) == ri) {
			    mode = PMC_TO_MODE(pm);
			    if (PMC_IS_VIRTUAL_MODE(mode))
				    return EEXIST;
			    if (PMC_IS_SYSTEM_MODE(mode) &&
				(int) PMC_TO_CPU(pm) == cpu)
				    return EEXIST;
		    }
	        }
	if ((pp = pmc_find_process_descriptor(p, 0)) != NULL)
		if (pp->pp_pmcs[ri].pp_pmc)
			return EEXIST;
	PMCDBG4(PMC,ALR,2, "can-allocate-rowindex proc=%p (%d, %s) ri=%d ok",
	    p, p->p_pid, p->p_comm, ri);
	return 0;
}
static int pmc_can_allocate_rowindex ( struct proc * p , unsigned int ri , int cpu ) { enum pmc_mode mode ; struct pmc * pm ; struct pmc_owner * po ; struct pmc_process * pp ; PMCDBG5 ( PMC , ALR , 1 , " " " " , p , p -> p_pid , p -> p_comm , ri , cpu ) ; if ( ( po = pmc_find_owner_descriptor ( p ) ) != NULL ) LIST_FOREACH ( pm , & po -> po_pmcs , pm_next ) { if ( PMC_TO_ROWINDEX ( pm ) == ri ) { mode = PMC_TO_MODE ( pm ) ; if ( PMC_IS_VIRTUAL_MODE ( mode ) ) return EEXIST ; if ( PMC_IS_SYSTEM_MODE ( mode ) && ( int ) PMC_TO_CPU ( pm ) == cpu ) return EEXIST ; } } if ( ( pp = pmc_find_process_descriptor ( p , 0 ) ) != NULL ) if ( pp -> pp_pmcs [ ri ] . pp_pmc ) return EEXIST ; PMCDBG4 ( PMC , ALR , 2 , " " , p , p -> p_pid , p -> p_comm , ri ) ; return 0 ; }
Check if a PMC at row index 'ri' can be allocated to the current
 process.
Check if a PMC at row index ' ri ' can be allocated to the current process .
/*
	 * We shouldn't have already allocated a process-mode PMC at
	 * row index 'ri'.
	 *
	 * We shouldn't have allocated a system-wide PMC on the same
	 * CPU and same RI.
	 */ /*
	 * We also shouldn't be the target of any PMC at this index
	 * since otherwise a PMC_ATTACH to ourselves will fail.
	 */