FreeBSD/sys/dev/hwpmc/hwpmc_mod.c
/*
 * Attach a process to a PMC.
 */
Attach a process to a PMC.
Attach a process to a PMC .
static int
pmc_attach_one_process(struct proc *p, struct pmc *pm)
{
	int ri;
	char *fullpath, *freepath;
	struct pmc_process	*pp;
	sx_assert(&pmc_sx, SX_XLOCKED);
	PMCDBG5(PRC,ATT,2, "attach-one pm=%p ri=%d proc=%p (%d, %s)", pm,
	    PMC_TO_ROWINDEX(pm), p, p->p_pid, p->p_comm);
	ri = PMC_TO_ROWINDEX(pm);
	if ((pp = pmc_find_process_descriptor(p, PMC_FLAG_ALLOCATE)) == NULL)
		return ENOMEM;
	if (pp->pp_pmcs[ri].pp_pmc == pm) 
		return EEXIST;
	if (pp->pp_pmcs[ri].pp_pmc != NULL)
		return EBUSY;
	pmc_link_target_process(pm, pp);
	if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)) &&
	    (pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) == 0)
		pm->pm_flags |= PMC_F_NEEDS_LOGFILE;
	pm->pm_flags |= PMC_F_ATTACH_DONE; 
	if (pm->pm_owner->po_flags & PMC_PO_OWNS_LOGFILE) {
		if (p->p_flag & P_KPROC) {
			fullpath = kernelname;
			freepath = NULL;
		} else {
			pmc_getfilename(p->p_textvp, &fullpath, &freepath);
			pmclog_process_pmcattach(pm, p->p_pid, fullpath);
		}
		free(freepath, M_TEMP);
		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm)))
			pmc_log_process_mappings(pm->pm_owner, p);
	}
	PROC_LOCK(p);
	p->p_flag |= P_HWPMC;
	PROC_UNLOCK(p);
	return 0;
}
static int pmc_attach_one_process ( struct proc * p , struct pmc * pm ) { int ri ; char * fullpath , * freepath ; struct pmc_process * pp ; sx_assert ( & pmc_sx , SX_XLOCKED ) ; PMCDBG5 ( PRC , ATT , 2 , " " , pm , PMC_TO_ROWINDEX ( pm ) , p , p -> p_pid , p -> p_comm ) ; ri = PMC_TO_ROWINDEX ( pm ) ; if ( ( pp = pmc_find_process_descriptor ( p , PMC_FLAG_ALLOCATE ) ) == NULL ) return ENOMEM ; if ( pp -> pp_pmcs [ ri ] . pp_pmc == pm ) return EEXIST ; if ( pp -> pp_pmcs [ ri ] . pp_pmc != NULL ) return EBUSY ; pmc_link_target_process ( pm , pp ) ; if ( PMC_IS_SAMPLING_MODE ( PMC_TO_MODE ( pm ) ) && ( pm -> pm_flags & PMC_F_ATTACHED_TO_OWNER ) == 0 ) pm -> pm_flags |= PMC_F_NEEDS_LOGFILE ; pm -> pm_flags |= PMC_F_ATTACH_DONE ; if ( pm -> pm_owner -> po_flags & PMC_PO_OWNS_LOGFILE ) { if ( p -> p_flag & P_KPROC ) { fullpath = kernelname ; freepath = NULL ; } else { pmc_getfilename ( p -> p_textvp , & fullpath , & freepath ) ; pmclog_process_pmcattach ( pm , p -> p_pid , fullpath ) ; } free ( freepath , M_TEMP ) ; if ( PMC_IS_SAMPLING_MODE ( PMC_TO_MODE ( pm ) ) ) pmc_log_process_mappings ( pm -> pm_owner , p ) ; } PROC_LOCK ( p ) ; p -> p_flag |= P_HWPMC ; PROC_UNLOCK ( p ) ; return 0 ; }
Attach a process to a PMC.
Attach a process to a PMC .
/*
	 * Locate the process descriptor corresponding to process 'p',
	 * allocating space as needed.
	 *
	 * Verify that rowindex 'pm_rowindex' is free in the process
	 * descriptor.
	 *
	 * If not, allocate space for a descriptor and link the
	 * process descriptor and PMC.
	 */ /* already present at slot [ri] */ /* mark as attached */ /* issue an attach event to a configured log file */ /* mark process as using HWPMCs */