pmckern.h revision 146799
1145256Sjkoshy/*- 2146799Sjkoshy * Copyright (c) 2003-2005, Joseph Koshy 3145256Sjkoshy * All rights reserved. 4145256Sjkoshy * 5145256Sjkoshy * Redistribution and use in source and binary forms, with or without 6145256Sjkoshy * modification, are permitted provided that the following conditions 7145256Sjkoshy * are met: 8145256Sjkoshy * 1. Redistributions of source code must retain the above copyright 9145256Sjkoshy * notice, this list of conditions and the following disclaimer. 10145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 11145256Sjkoshy * notice, this list of conditions and the following disclaimer in the 12145256Sjkoshy * documentation and/or other materials provided with the distribution. 13145256Sjkoshy * 14145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17145256Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24145256Sjkoshy * SUCH DAMAGE. 25145256Sjkoshy * 26145256Sjkoshy * $FreeBSD: head/sys/sys/pmckern.h 146799 2005-05-30 06:29:29Z jkoshy $ 27145256Sjkoshy */ 28145256Sjkoshy 29145256Sjkoshy/* 30145256Sjkoshy * PMC interface used by the base kernel. 31145256Sjkoshy */ 32145256Sjkoshy 33145256Sjkoshy#ifndef _SYS_PMCKERN_H_ 34145256Sjkoshy#define _SYS_PMCKERN_H_ 35145256Sjkoshy 36145256Sjkoshy#include <sys/param.h> 37145256Sjkoshy#include <sys/kernel.h> 38145256Sjkoshy#include <sys/lock.h> 39145256Sjkoshy#include <sys/proc.h> 40145256Sjkoshy#include <sys/sx.h> 41145256Sjkoshy 42146799Sjkoshy#define PMC_FN_PROCESS_EXEC 1 43146799Sjkoshy#define PMC_FN_CSW_IN 2 44146799Sjkoshy#define PMC_FN_CSW_OUT 3 45146799Sjkoshy#define PMC_FN_DO_SAMPLES 4 46145256Sjkoshy 47146799Sjkoshy#define PMC_FN_PROCESS_EXIT 5 /* obsolete */ 48146799Sjkoshy#define PMC_FN_PROCESS_FORK 6 /* obsolete */ 49146799Sjkoshy 50145256Sjkoshy/* hook */ 51145256Sjkoshyextern int (*pmc_hook)(struct thread *_td, int _function, void *_arg); 52146799Sjkoshyextern int (*pmc_intr)(int _cpu, uintptr_t _pc, int _usermode); 53145256Sjkoshy 54145256Sjkoshy/* SX lock protecting the hook */ 55145256Sjkoshyextern struct sx pmc_sx; 56145256Sjkoshy 57146799Sjkoshy/* Per-cpu flags indicating availability of sampling data */ 58146799Sjkoshyextern cpumask_t pmc_cpumask; 59146799Sjkoshy 60146799Sjkoshy/* Hook invocation; for use within the kernel */ 61145256Sjkoshy#define PMC_CALL_HOOK(t, cmd, arg) \ 62145256Sjkoshydo { \ 63145256Sjkoshy sx_slock(&pmc_sx); \ 64145256Sjkoshy if (pmc_hook != NULL) \ 65145256Sjkoshy (pmc_hook)((t), (cmd), (arg)); \ 66145256Sjkoshy sx_sunlock(&pmc_sx); \ 67145256Sjkoshy} while (0) 68145256Sjkoshy 69146799Sjkoshy/* Hook invocation that needs an exclusive lock */ 70145256Sjkoshy#define PMC_CALL_HOOK_X(t, cmd, arg) \ 71145256Sjkoshydo { \ 72145256Sjkoshy sx_xlock(&pmc_sx); \ 73145256Sjkoshy if (pmc_hook != NULL) \ 74145256Sjkoshy (pmc_hook)((t), (cmd), (arg)); \ 75145256Sjkoshy sx_xunlock(&pmc_sx); \ 76145256Sjkoshy} while (0) 77145256Sjkoshy 78146799Sjkoshy/* 79146799Sjkoshy * Some hook invocations (e.g., from context switch and clock handling 80146799Sjkoshy * code) need to be lock-free. 81146799Sjkoshy */ 82146799Sjkoshy#define PMC_CALL_HOOK_UNLOCKED(t, cmd, arg) \ 83145256Sjkoshydo { \ 84145256Sjkoshy if (pmc_hook != NULL) \ 85146799Sjkoshy (pmc_hook)((t), (cmd), (arg)); \ 86145256Sjkoshy} while (0) 87145256Sjkoshy 88146799Sjkoshy#define PMC_SWITCH_CONTEXT(t,cmd) PMC_CALL_HOOK_UNLOCKED(t,cmd,NULL) 89145256Sjkoshy 90146799Sjkoshy/* Check if a process is using HWPMCs.*/ 91145256Sjkoshy#define PMC_PROC_IS_USING_PMCS(p) \ 92145256Sjkoshy (__predict_false(atomic_load_acq_int(&(p)->p_flag) & \ 93145256Sjkoshy P_HWPMC)) 94145256Sjkoshy 95146799Sjkoshy/* Check if a CPU has recorded samples. */ 96146799Sjkoshy#define PMC_CPU_HAS_SAMPLES(C) (__predict_false(pmc_cpumask & (1 << (C)))) 97146799Sjkoshy 98145256Sjkoshy/* helper functions */ 99145256Sjkoshyint pmc_cpu_is_disabled(int _cpu); 100145256Sjkoshyint pmc_cpu_is_logical(int _cpu); 101145256Sjkoshy 102145256Sjkoshy#endif /* _SYS_PMCKERN_H_ */ 103