git: ca341f3cf52f - main - hwpmc: Permit the minimum sampling count to be set as a sysctl.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Thu, 09 Jun 2022 18:06:11 UTC
The branch main has been updated by jhb:

URL: https://cgit.FreeBSD.org/src/commit/?id=ca341f3cf52f8cb036d93cd611d8f3f5b552ea8e

commit ca341f3cf52f8cb036d93cd611d8f3f5b552ea8e
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2022-06-09 18:05:34 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2022-06-09 18:05:34 +0000

    hwpmc: Permit the minimum sampling count to be set as a sysctl.
    
    A rarely occurring event (e.g. an event that occurs less than 1000
    times during execution of a program) may require a lower minimum
    threshold than 1000.  Replace the hardcoded 1000 with a sysctl that
    the administrator can use to permit smaller sampling count values.
    
    Reviewed by:    mhorne, mav
    Sponsored by:   University of Cambridge, Google, Inc.
    Differential Revision:  https://reviews.freebsd.org/D35400
---
 share/man/man4/hwpmc.4    |  3 +++
 sys/dev/hwpmc/hwpmc_mod.c | 38 ++++++++++++++++++++++++++------------
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/share/man/man4/hwpmc.4 b/share/man/man4/hwpmc.4
index 77fe4b9a075a..806c2ee701c5 100644
--- a/share/man/man4/hwpmc.4
+++ b/share/man/man4/hwpmc.4
@@ -416,6 +416,9 @@ The size in kilobytes of each log buffer used by
 .Nm Ns 's
 logging function.
 The default buffer size is 4KB.
+.It Va kern.hwpmc.mincount Pq integer, read-write
+The minimum sampling rate for sampling mode PMCs.
+The default count is 1000 events.
 .It Va kern.hwpmc.mtxpoolsize Pq integer, read-only
 The size of the spin mutex pool used by the PMC driver.
 The default is 32.
diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
index 18b8bb1674a3..21987c310b94 100644
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -363,6 +363,14 @@ SYSCTL_INT(_kern_hwpmc, OID_AUTO, threadfreelist_max, CTLFLAG_RW,
     "maximum number of available thread entries before freeing some");
 
 
+/*
+ * kern.hwpmc.mincount -- minimum sample count
+ */
+static u_int pmc_mincount = 1000;
+SYSCTL_INT(_kern_hwpmc, OID_AUTO, mincount, CTLFLAG_RWTUN,
+    &pmc_mincount, 0,
+    "minimum count for sampling counters");
+
 /*
  * security.bsd.unprivileged_syspmcs -- allow non-root processes to
  * allocate system-wide PMCs.
@@ -3950,13 +3958,16 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 		/* XXX set lower bound on sampling for process counters */
 		if (PMC_IS_SAMPLING_MODE(mode)) {
 			/*
-			 * Don't permit requested sample rate to be less than 1000
+			 * Don't permit requested sample rate to be
+			 * less than pmc_mincount.
 			 */
-			if (pa.pm_count < 1000)
-				log(LOG_WARNING,
-					"pmcallocate: passed sample rate %ju - setting to 1000\n",
-					(uintmax_t)pa.pm_count);
-			pmc->pm_sc.pm_reloadcount = MAX(1000, pa.pm_count);
+			if (pa.pm_count < MAX(1, pmc_mincount))
+				log(LOG_WARNING, "pmcallocate: passed sample "
+				    "rate %ju - setting to %u\n",
+				    (uintmax_t)pa.pm_count,
+				    MAX(1, pmc_mincount));
+			pmc->pm_sc.pm_reloadcount = MAX(MAX(1, pmc_mincount),
+			    pa.pm_count);
 		} else
 			pmc->pm_sc.pm_initial = pa.pm_count;
 
@@ -4474,13 +4485,16 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 
 		if (PMC_IS_SAMPLING_MODE(PMC_TO_MODE(pm))) {
 			/*
-			 * Don't permit requested sample rate to be less than 1000
+			 * Don't permit requested sample rate to be
+			 * less than pmc_mincount.
 			 */
-			if (sc.pm_count < 1000)
-				log(LOG_WARNING,
-					"pmcsetcount: passed sample rate %ju - setting to 1000\n",
-					(uintmax_t)sc.pm_count);
-			pm->pm_sc.pm_reloadcount = MAX(1000, sc.pm_count);
+			if (sc.pm_count < MAX(1, pmc_mincount))
+				log(LOG_WARNING, "pmcsetcount: passed sample "
+				    "rate %ju - setting to %u\n",
+				    (uintmax_t)sc.pm_count,
+				    MAX(1, pmc_mincount));
+			pm->pm_sc.pm_reloadcount = MAX(MAX(1, pmc_mincount),
+			    sc.pm_count);
 		} else
 			pm->pm_sc.pm_initial = sc.pm_count;
 	}