Page MenuHomeFreeBSD

D61372.1795555116.diff
No OneTemporary

Authored By
Unknown
Size
25 KB
Referenced Files
None
Subscribers
None

D61372.1795555116.diff

diff --git a/UPDATING b/UPDATING
--- a/UPDATING
+++ b/UPDATING
@@ -27,6 +27,18 @@
world, or to merely disable the most expensive debugging functionality
at runtime, run "ln -s 'abort:false,junk:false' /etc/malloc.conf".)
+20261123:
+ hwpmc(4) writes the new PMCLOG_TYPE_RATECHANGE log record when the
+ sample rate limit changes. PMC_VERSION_MINOR is now 4. An older
+ libpmc(3) stops at this record with an error. Rebuild libpmc and
+ pmcstat(8) to read these logs.
+
+ The kernel and the hwpmc module must have the same PMC_VERSION, so
+ rebuild both. A module of a different version does not load. A
+ new libpmc that runs against an older hwpmc module gets
+ EPROGMISMATCH from pmc_init(3). Thus build and install world and
+ kernel together.
+
20261102:
The USB stack now supports SuperSpeedPlus links. This adds new values
to enum usb_dev_speed and enum usb_revision, turns USB_SPEED_MAX and
diff --git a/lib/libpmc/libpmc_json.cc b/lib/libpmc/libpmc_json.cc
--- a/lib/libpmc/libpmc_json.cc
+++ b/lib/libpmc/libpmc_json.cc
@@ -67,6 +67,7 @@
"{\"type\": \"thr_create\"",
"{\"type\": \"thr_exit\"",
"{\"type\": \"proc_create\"",
+ "{\"type\": \"ratechange\"",
};
static string
@@ -329,6 +330,22 @@
return string(eventbuf);
}
+static string
+ratechange_to_json(struct pmclog_ev *ev)
+{
+ char eventbuf[256];
+ string startent;
+
+ startent = startentry(ev);
+ snprintf(eventbuf, sizeof(eventbuf),
+ "%s, \"rate\": \"%u\", \"pertick\": \"%u\", "
+ "\"flags\": \"0x%x\", \"intrns\": \"%u\"}\n",
+ startent.c_str(), ev->pl_u.pl_rc.pl_rate,
+ ev->pl_u.pl_rc.pl_pertick, ev->pl_u.pl_rc.pl_flags,
+ ev->pl_u.pl_rc.pl_intrns);
+ return (string(eventbuf));
+}
+
static string
stub_to_json(struct pmclog_ev *ev)
{
@@ -363,6 +380,7 @@
threadcreate_to_json,
threadexit_to_json,
proccreate_to_json,
+ ratechange_to_json,
};
string
@@ -388,6 +406,7 @@
case PMCLOG_TYPE_THR_CREATE:
case PMCLOG_TYPE_THR_EXIT:
case PMCLOG_TYPE_PROC_CREATE:
+ case PMCLOG_TYPE_RATECHANGE:
return jsonconvert[ev->pl_type](ev);
default:
errx(EX_USAGE, "ERROR: unrecognized event type: %d\n", ev->pl_type);
diff --git a/lib/libpmc/pmclog.h b/lib/libpmc/pmclog.h
--- a/lib/libpmc/pmclog.h
+++ b/lib/libpmc/pmclog.h
@@ -168,6 +168,13 @@
pid_t pl_tid;
};
+struct pmclog_ev_ratechange {
+ uint32_t pl_rate; /* Samples per second. */
+ uint32_t pl_pertick; /* Samples per tick. */
+ uint32_t pl_flags; /* PMCLOG_RATE_* */
+ uint32_t pl_intrns; /* Interrupt time, in ns. */
+};
+
struct pmclog_ev_userdata {
uint32_t pl_userdata;
};
@@ -199,6 +206,7 @@
struct pmclog_ev_procexec pl_x;
struct pmclog_ev_procexit pl_e;
struct pmclog_ev_procfork pl_f;
+ struct pmclog_ev_ratechange pl_rc;
struct pmclog_ev_sysexit pl_se;
struct pmclog_ev_threadcreate pl_tc;
struct pmclog_ev_threadexit pl_te;
diff --git a/lib/libpmc/pmclog.3 b/lib/libpmc/pmclog.3
--- a/lib/libpmc/pmclog.3
+++ b/lib/libpmc/pmclog.3
@@ -21,7 +21,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd March 26, 2006
+.Dd November 23, 2026
.Dt PMCLOG 3
.Os
.Sh NAME
@@ -97,6 +97,7 @@
struct pmclog_ev_procexec pl_x;
struct pmclog_ev_procexit pl_e;
struct pmclog_ev_procfork pl_f;
+ struct pmclog_ev_ratechange pl_rc;
struct pmclog_ev_sysexit pl_e;
struct pmclog_ev_userdata pl_u;
} pl_u;
@@ -215,6 +216,47 @@
A record describing a
.Xr fork 2
by a target process.
+.It Dv PMCLOG_TYPE_RATECHANGE
+A record that reports a new sample rate limit.
+.Xr hwpmc 4
+writes this record before the next sample of the log after the
+limit changes.
+Field
+.Va pl_tsc
+contains the time of the change.
+Field
+.Va pl_u.pl_rc.pl_rate
+contains the new value of
+.Va kern.hwpmc.max_sample_rate ,
+in samples per second for one PMC on one CPU.
+Field
+.Va pl_u.pl_rc.pl_pertick
+contains the new limit of samples per clock tick.
+Field
+.Va pl_u.pl_rc.pl_flags
+contains the cause of the change:
+.Bl -tag -width ".Dv PMCLOG_RATE_INTRTIME"
+.It Dv PMCLOG_RATE_INTRTIME
+The interrupt time was above the limit of
+.Va kern.hwpmc.cpu_time_max_percent .
+Field
+.Va pl_u.pl_rc.pl_intrns
+contains the average interrupt time, in nanoseconds.
+.It Dv PMCLOG_RATE_SYSCTL
+An administrator changed
+.Va kern.hwpmc.max_sample_rate .
+Field
+.Va pl_u.pl_rc.pl_intrns
+is zero.
+.El
+.Pp
+Samples that the PMC took before the change can follow this record.
+Use
+.Va pl_tsc
+to put the samples in order.
+Kernels before
+.Dv PMC_VERSION_MINOR
+4 do not write this record.
.It Dv PMCLOG_TYPE_SYSEXIT
A record describing a process exit, sent to processes
owning system-wide sampling PMCs.
diff --git a/lib/libpmc/pmclog.c b/lib/libpmc/pmclog.c
--- a/lib/libpmc/pmclog.c
+++ b/lib/libpmc/pmclog.c
@@ -414,6 +414,12 @@
PMCLOG_READ32(le,ev->pl_u.pl_f.pl_oldpid);
PMCLOG_READ32(le,ev->pl_u.pl_f.pl_newpid);
break;
+ case PMCLOG_TYPE_RATECHANGE:
+ PMCLOG_READ32(le, ev->pl_u.pl_rc.pl_rate);
+ PMCLOG_READ32(le, ev->pl_u.pl_rc.pl_pertick);
+ PMCLOG_READ32(le, ev->pl_u.pl_rc.pl_flags);
+ PMCLOG_READ32(le, ev->pl_u.pl_rc.pl_intrns);
+ break;
case PMCLOG_TYPE_SYSEXIT:
PMCLOG_READ32(le,ev->pl_u.pl_se.pl_pid);
break;
diff --git a/share/man/man4/hwpmc.4 b/share/man/man4/hwpmc.4
--- a/share/man/man4/hwpmc.4
+++ b/share/man/man4/hwpmc.4
@@ -429,6 +429,19 @@
.It Va kern.hwpmc.callchaindepth Pq integer, read-only
The maximum number of call chain records to capture per sample.
The default is 128.
+.It Va kern.hwpmc.cpu_time_max_percent Pq integer, read-write
+The maximum percentage of CPU time that the PMC interrupt handler can use.
+The driver measures the time of each interrupt.
+When the average time is above this percentage of the time between two
+samples at
+.Va kern.hwpmc.max_sample_rate ,
+the driver decreases
+.Va kern.hwpmc.max_sample_rate
+and logs a message.
+The driver does not increase the rate again.
+The values 0 and 100 disable this limit.
+This limit is available only on x86.
+The default is 25.
.It Va kern.hwpmc.debugflags Pq string, read-write
(Only available if the
.Nm
@@ -466,9 +479,15 @@
When a PMC interrupts more times than this limit in one clock tick, the
driver drops the sample, stops the PMC, and counts the drop.
The driver starts the PMC again at the next clock tick.
-The value 0 disables the limit.
+The value 0 disables the limit and the limit of
+.Va kern.hwpmc.cpu_time_max_percent .
Without the limit, a short sample period can use all the CPU time in the
interrupt handler.
+After a change of this limit, the driver writes a
+.Dv PMCLOG_TYPE_RATECHANGE
+record to each log file before the next sample in that log.
+See
+.Xr pmclog 3 .
The default is 100000.
.It Va kern.hwpmc.mincount Pq integer, read-write
The minimum sampling rate for sampling mode PMCs.
@@ -552,6 +571,15 @@
An administrator set
.Va kern.hwpmc.max_sample_rate
to 0.
+.It "hwpmc: interrupt time limit disabled, sampling can stall the system"
+An administrator set
+.Va kern.hwpmc.cpu_time_max_percent
+to 0 or 100.
+.It "hwpmc: interrupt took too long (%u ns > %u ns), lowering kern.hwpmc.max_sample_rate to %u"
+The average time of the PMC interrupt handler was above the limit of
+.Va kern.hwpmc.cpu_time_max_percent .
+The driver decreased
+.Va kern.hwpmc.max_sample_rate .
.It "hwpmc: tunable hashsize=%d must be greater than zero."
A negative value was supplied for tunable
.Va kern.hwpmc.hashsize .
diff --git a/sys/dev/hwpmc/hwpmc_logging.c b/sys/dev/hwpmc/hwpmc_logging.c
--- a/sys/dev/hwpmc/hwpmc_logging.c
+++ b/sys/dev/hwpmc/hwpmc_logging.c
@@ -209,6 +209,7 @@
2*sizeof(uintptr_t));
CTASSERT(sizeof(struct pmclog_procexit) == 5*4 + 8 + TSDELTA);
CTASSERT(sizeof(struct pmclog_procfork) == 5*4 + TSDELTA);
+CTASSERT(sizeof(struct pmclog_ratechange) == 7 * 4 + TSDELTA);
CTASSERT(sizeof(struct pmclog_sysexit) == 6*4);
CTASSERT(sizeof(struct pmclog_userdata) == 6*4);
@@ -1225,6 +1226,21 @@
PMCLOG_DESPATCH(po);
}
+/* Log a rate limit change. Return ENOMEM if full. */
+int
+pmclog_process_ratechange(struct pmc_owner *po, uint64_t tsc, uint32_t rate,
+ uint32_t pertick, uint32_t flags, uint32_t intrns)
+{
+ _PMCLOG_RESERVE_SAFE(po, PMCLOG_TYPE_RATECHANGE,
+ sizeof(struct pmclog_ratechange), return (ENOMEM), tsc);
+ PMCLOG_EMIT32(rate);
+ PMCLOG_EMIT32(pertick);
+ PMCLOG_EMIT32(flags);
+ PMCLOG_EMIT32(intrns);
+ PMCLOG_DESPATCH_SAFE(po);
+ return (0);
+}
+
/*
* Log a process exit event of the form suitable for system-wide PMCs.
*/
diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -200,6 +200,7 @@
static int pmc_debugflags_sysctl_handler(SYSCTL_HANDLER_ARGS);
static int pmc_debugflags_parse(char *newstr, char *fence);
#endif
+static int pmc_cpu_time_max_percent_sysctl_handler(SYSCTL_HANDLER_ARGS);
static int pmc_max_sample_rate_sysctl_handler(SYSCTL_HANDLER_ARGS);
static void pmc_multipart_add(struct pmc_sample *ps, int type,
@@ -244,6 +245,7 @@
static void pmc_log_all_process_mappings(struct pmc_owner *po);
static void pmc_log_kernel_mappings(struct pmc *pm);
static void pmc_log_process_mappings(struct pmc_owner *po, struct proc *p);
+static void pmc_log_ratechange(struct pmc_owner *po);
static void pmc_maybe_remove_owner(struct pmc_owner *po);
static void pmc_post_callchain_callback(void);
static void pmc_process_allproc(struct pmc *pm);
@@ -324,6 +326,10 @@
SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, intr_throttled, CTLFLAG_RW,
&pmc_stats.pm_intr_throttled,
"# of times a PMC stopped at the sample rate limit");
+SYSCTL_COUNTER_U64(_kern_hwpmc_stats, OID_AUTO, rate_lowered, CTLFLAG_RW,
+ &pmc_stats.pm_rate_lowered,
+ "# of times the interrupt time limit lowered max_sample_rate");
+
/* Per-PMC, per-CPU rate limit. Zero disables it. */
static u_int pmc_max_sample_rate = PMC_MAX_SAMPLE_RATE;
static u_int pmc_max_samples_per_tick;
@@ -332,6 +338,26 @@
pmc_max_sample_rate_sysctl_handler, "IU",
"maximum samples per second for one PMC on one CPU (0: no limit)");
+/* Interrupt CPU budget. Zero and 100 disable it. */
+static u_int pmc_cpu_time_max_percent = PMC_CPU_TIME_MAX_PERCENT;
+static u_int pmc_intr_allowed_ns; /* 0: limit disabled */
+static u_int pmc_rate_lowered; /* Report pending. */
+static u_int pmc_report_avg_ns, pmc_report_allowed_ns;
+SYSCTL_PROC(_kern_hwpmc, OID_AUTO, cpu_time_max_percent,
+ CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL, 0,
+ pmc_cpu_time_max_percent_sysctl_handler, "IU",
+ "maximum percent of CPU time for the PMC interrupt (0, 100: no limit)");
+
+/* Per-CPU interrupt time sum, in ns. */
+#define PMC_INTR_NSAMPLES 128
+DPCPU_DEFINE_STATIC(uint64_t, pmc_intr_running_ns);
+
+/* Last rate limit change, for RATECHANGE log records. */
+static u_int pmc_rate_gen; /* Change count. */
+static uint64_t pmc_rate_tsc;
+static u_int pmc_rate_flags; /* PMCLOG_RATE_* */
+static u_int pmc_rate_intrns;
+
DPCPU_DEFINE_STATIC(int, pmc_tick_seq); /* Tick number. */
DPCPU_DEFINE_STATIC(uint8_t, pmc_throttled); /* Restart next tick. */
@@ -628,16 +654,38 @@
}
#endif
-/* Rate limit. */
+/* Rate and time limits. */
static void
pmc_update_sample_limit(void)
{
- u_int rate;
+ uint64_t allowed;
+ u_int pct, rate;
rate = atomic_load_int(&pmc_max_sample_rate);
+ pct = atomic_load_int(&pmc_cpu_time_max_percent);
/* Avoid 32-bit overflow. */
atomic_store_int(&pmc_max_samples_per_tick,
rate == 0 ? 0 : howmany((uint64_t)rate, hz));
+
+ /* CPU time budget per sample. */
+ allowed = 0;
+ if (rate != 0 && pct != 0 && pct < 100) {
+ allowed = 1000000000ULL / rate * pct / 100;
+ if (allowed == 0)
+ allowed = 1;
+ }
+ atomic_store_int(&pmc_intr_allowed_ns, MIN(allowed, UINT_MAX));
+}
+
+/* Record a rate limit change. NMI-safe. */
+static void
+pmc_note_ratechange(u_int flags, u_int intrns)
+{
+ pmc_rate_tsc = pmc_rdtsc();
+ pmc_rate_flags = flags;
+ pmc_rate_intrns = intrns;
+ /* Release: readers see the values of this change or a later one. */
+ atomic_add_rel_int(&pmc_rate_gen, 1);
}
static int
@@ -653,11 +701,84 @@
if (rate == 0 && pmc_max_sample_rate != 0)
log(LOG_WARNING,
"hwpmc: sample rate limit disabled, sampling can stall the system\n");
- atomic_store_int(&pmc_max_sample_rate, rate);
+ if (atomic_swap_int(&pmc_max_sample_rate, rate) != rate) {
+ pmc_update_sample_limit();
+ pmc_note_ratechange(PMCLOG_RATE_SYSCTL, 0);
+ }
+ return (0);
+}
+
+static int
+pmc_cpu_time_max_percent_sysctl_handler(SYSCTL_HANDLER_ARGS)
+{
+ u_int pct;
+ int error;
+
+ pct = pmc_cpu_time_max_percent;
+ error = sysctl_handle_int(oidp, &pct, 0, req);
+ if (error != 0 || req->newptr == NULL)
+ return (error);
+ if (pct > 100)
+ return (EINVAL);
+ if ((pct == 0 || pct == 100) && pmc_cpu_time_max_percent != 0 &&
+ pmc_cpu_time_max_percent != 100)
+ log(LOG_WARNING,
+ "hwpmc: interrupt time limit disabled, sampling can stall the system\n");
+ atomic_store_int(&pmc_cpu_time_max_percent, pct);
pmc_update_sample_limit();
return (0);
}
+/* NMI-safe rate cut. No locks. */
+void
+pmc_intr_took(uint64_t ns)
+{
+ uint64_t avg, budget, running;
+ u_int allowed, old, per_tick, pct, rate;
+
+ allowed = atomic_load_int(&pmc_intr_allowed_ns);
+ if (allowed == 0)
+ return;
+
+ running = DPCPU_GET(pmc_intr_running_ns);
+ running -= running / PMC_INTR_NSAMPLES;
+ running += ns;
+ DPCPU_SET(pmc_intr_running_ns, running);
+
+ avg = running / PMC_INTR_NSAMPLES;
+ if (avg <= allowed)
+ return;
+
+ pct = atomic_load_int(&pmc_cpu_time_max_percent);
+ avg += avg / 4;
+ budget = (1000000000 / hz / 100) * pct;
+ per_tick = avg < budget ? budget / avg : 1;
+ atomic_store_int(&pmc_intr_allowed_ns, MIN(avg, UINT_MAX));
+ for (;;) {
+ old = atomic_load_int(&pmc_max_samples_per_tick);
+ if (old != 0 && per_tick >= old)
+ return;
+ if (atomic_cmpset_int(&pmc_max_samples_per_tick, old, per_tick))
+ break;
+ }
+ rate = per_tick * hz;
+ for (;;) {
+ old = atomic_load_int(&pmc_max_sample_rate);
+ if (old != 0 && rate >= old)
+ break;
+ if (atomic_cmpset_int(&pmc_max_sample_rate, old, rate))
+ break;
+ }
+ counter_u64_add(pmc_stats.pm_rate_lowered, 1);
+
+ /* Log NMI time at hardclock. */
+ pmc_report_avg_ns = MIN(running / PMC_INTR_NSAMPLES, UINT_MAX);
+ pmc_report_allowed_ns = allowed;
+ pmc_note_ratechange(PMCLOG_RATE_INTRTIME, pmc_report_avg_ns);
+ atomic_store_int(&pmc_rate_lowered, 1);
+ DPCPU_SET(pmc_sampled, 1);
+}
+
/*
* Map a row index to a classdep structure and return the adjusted row
* index for the PMC class index.
@@ -2347,6 +2468,12 @@
DPCPU_SET(pmc_throttled, 0);
pmc_restart_stalled(cpu);
}
+ if (atomic_load_int(&pmc_rate_lowered) != 0 &&
+ atomic_cmpset_int(&pmc_rate_lowered, 1, 0))
+ log(LOG_INFO,
+ "hwpmc: interrupt took too long (%u ns > %u ns), lowering kern.hwpmc.max_sample_rate to %u\n",
+ pmc_report_avg_ns, pmc_report_allowed_ns,
+ atomic_load_int(&pmc_max_sample_rate));
break;
case PMC_FN_MMAP:
@@ -2454,6 +2581,8 @@
/* Allocate space for N pointers and one descriptor struct. */
po = malloc(sizeof(struct pmc_owner), M_PMC, M_WAITOK | M_ZERO);
po->po_owner = p;
+ /* Log only later changes. */
+ po->po_rate_gen = atomic_load_acq_int(&pmc_rate_gen);
LIST_INSERT_HEAD(poh, po, po_next); /* insert into hash table */
TAILQ_INIT(&po->po_logbuffers);
@@ -5291,8 +5420,10 @@
td = FIRST_THREAD_IN_PROC(po->po_owner);
addupc_intr(td, ps->ps_pc[0], 1);
}
- } else
+ } else {
+ pmc_log_ratechange(po);
pmclog_process_callchain(pm, ps);
+ }
entrydone:
ps->ps_nsamples = 0; /* mark entry as free */
@@ -5313,6 +5444,25 @@
}
+/* Log a rate limit change once in each log, before the next sample. */
+static void
+pmc_log_ratechange(struct pmc_owner *po)
+{
+ u_int gen, old, pertick, rate;
+
+ gen = atomic_load_acq_int(&pmc_rate_gen);
+ old = atomic_load_int(&po->po_rate_gen);
+ if (old == gen || atomic_cmpset_int(&po->po_rate_gen, old, gen) == 0)
+ return;
+ /* A cut lowers the tick limit first, so read it first. */
+ pertick = atomic_load_acq_int(&pmc_max_samples_per_tick);
+ rate = atomic_load_int(&pmc_max_sample_rate);
+ if (pmclog_process_ratechange(po, pmc_rate_tsc, rate, pertick,
+ pmc_rate_flags, pmc_rate_intrns) != 0)
+ /* Log full: try again at the next sample. */
+ (void)atomic_cmpset_int(&po->po_rate_gen, gen, old);
+}
+
/* Check the tick limit. */
static bool
pmc_over_limit(struct pmc *pm, int cpu)
@@ -5852,6 +6002,7 @@
pmc_stats.pm_merges = counter_u64_alloc(M_WAITOK);
pmc_stats.pm_overwrites = counter_u64_alloc(M_WAITOK);
pmc_stats.pm_intr_throttled = counter_u64_alloc(M_WAITOK);
+ pmc_stats.pm_rate_lowered = counter_u64_alloc(M_WAITOK);
#ifdef HWPMC_DEBUG
/* parse debug flags first */
@@ -6269,6 +6420,7 @@
counter_u64_free(pmc_stats.pm_merges);
counter_u64_free(pmc_stats.pm_overwrites);
counter_u64_free(pmc_stats.pm_intr_throttled);
+ counter_u64_free(pmc_stats.pm_rate_lowered);
sx_xunlock(&pmc_sx); /* we are done */
}
diff --git a/sys/dev/hwpmc/hwpmc_x86.c b/sys/dev/hwpmc/hwpmc_x86.c
--- a/sys/dev/hwpmc/hwpmc_x86.c
+++ b/sys/dev/hwpmc/hwpmc_x86.c
@@ -38,6 +38,7 @@
#include <vm/vm.h>
#include <vm/pmap.h>
+#include <machine/clock.h>
#include <machine/cpu.h>
#include <machine/cputypes.h>
#include <machine/intr_machdep.h> /* For x86/apicvar.h */
@@ -224,6 +225,23 @@
return (n);
}
+/* TSC scale: 32.32 ns per cycle. */
+static int (*pmc_x86_md_intr)(struct trapframe *);
+static uint64_t pmc_x86_tsc_scale;
+
+static int
+pmc_x86_intr(struct trapframe *tf)
+{
+ uint64_t t0;
+ int rv;
+
+ t0 = rdtsc();
+ rv = pmc_x86_md_intr(tf);
+ if (rv != 0 && pmc_x86_tsc_scale != 0)
+ pmc_intr_took(((rdtsc() - t0) * pmc_x86_tsc_scale) >> 32);
+ return (rv);
+}
+
/*
* Machine dependent initialization for x86 class platforms.
*/
@@ -243,7 +261,10 @@
if (md == NULL)
return (NULL);
- nmi_register_handler(md->pmd_intr);
+ pmc_x86_md_intr = md->pmd_intr;
+ pmc_x86_tsc_scale = tsc_freq != 0 ?
+ ((uint64_t)1000000000 << 32) / tsc_freq : 0;
+ nmi_register_handler(pmc_x86_intr);
/* disallow sampling if we do not have an LAPIC */
if (!lapic_enable_pcint())
for (i = 0; i < md->pmd_nclass; i++) {
@@ -260,7 +281,7 @@
{
if (md != NULL) {
lapic_disable_pcint();
- nmi_remove_handler(md->pmd_intr);
+ nmi_remove_handler(pmc_x86_intr);
}
if (cpu_vendor_id == CPU_VENDOR_AMD ||
cpu_vendor_id == CPU_VENDOR_HYGON)
diff --git a/sys/sys/pmc.h b/sys/sys/pmc.h
--- a/sys/sys/pmc.h
+++ b/sys/sys/pmc.h
@@ -60,7 +60,7 @@
* The patch version is incremented for every bug fix.
*/
#define PMC_VERSION_MAJOR 0x0A
-#define PMC_VERSION_MINOR 0x03
+#define PMC_VERSION_MINOR 0x04
#define PMC_VERSION_PATCH 0x0000
#define PMC_VERSION (PMC_VERSION_MAJOR << 24 | \
@@ -579,6 +579,7 @@
counter_u64_t pm_merges; /* merged k+u */
counter_u64_t pm_overwrites; /* UR overwrites */
counter_u64_t pm_intr_throttled; /* Rate stops. */
+ counter_u64_t pm_rate_lowered; /* Rate cuts. */
};
#endif
@@ -671,6 +672,7 @@
#define PMC_NLOGBUFFERS_PCPU_MEM_MAX (32 * 1024)
#define PMC_NSAMPLES 256
#define PMC_MAX_SAMPLE_RATE 100000
+#define PMC_CPU_TIME_MAX_PERCENT 25
#define PMC_CALLCHAIN_DEPTH 128
#define PMC_THREADLIST_MAX 128
@@ -893,6 +895,7 @@
int po_error; /* recorded error */
short po_sscount; /* # SS PMCs owned */
short po_logprocmaps; /* global mappings done */
+ u_int po_rate_gen; /* Logged rate limit change. */
struct pmclog_buffer *po_curbuf[MAXCPU]; /* current log buffer */
};
@@ -1270,6 +1273,7 @@
struct trapframe *_tf, struct pmc_multipart *mp);
int pmc_process_interrupt(int _ring, struct pmc *_pm,
struct trapframe *_tf);
+void pmc_intr_took(uint64_t _ns);
int pmc_save_kernel_callchain(uintptr_t *_cc, int _maxsamples,
struct trapframe *_tf);
int pmc_save_user_callchain(uintptr_t *_cc, int _maxsamples,
diff --git a/sys/sys/pmclog.h b/sys/sys/pmclog.h
--- a/sys/sys/pmclog.h
+++ b/sys/sys/pmclog.h
@@ -71,7 +71,11 @@
*/
PMCLOG_TYPE_THR_CREATE = 18,
PMCLOG_TYPE_THR_EXIT = 19,
- PMCLOG_TYPE_PROC_CREATE = 20
+ PMCLOG_TYPE_PROC_CREATE = 20,
+ /*
+ * V10.4 ABI
+ */
+ PMCLOG_TYPE_RATECHANGE = 21
};
/*
@@ -261,6 +265,18 @@
uint32_t pl_pad;
} __packed;
+/* New sample rate limit. pl_tsc is the time of the change. */
+struct pmclog_ratechange {
+ PMCLOG_ENTRY_HEADER
+ uint32_t pl_rate; /* Samples per second. */
+ uint32_t pl_pertick; /* Samples per tick. */
+ uint32_t pl_flags; /* PMCLOG_RATE_* */
+ uint32_t pl_intrns; /* Interrupt time, in ns. */
+};
+
+#define PMCLOG_RATE_INTRTIME 0x0001 /* Interrupt time limit. */
+#define PMCLOG_RATE_SYSCTL 0x0002 /* kern.hwpmc.max_sample_rate. */
+
struct pmclog_userdata {
PMCLOG_ENTRY_HEADER
uint32_t pl_userdata;
@@ -292,6 +308,7 @@
struct pmclog_procexec pl_x;
struct pmclog_procexit pl_e;
struct pmclog_procfork pl_f;
+ struct pmclog_ratechange pl_rc;
struct pmclog_sysexit pl_se;
struct pmclog_threadcreate pl_tc;
struct pmclog_threadexit pl_te;
@@ -338,6 +355,8 @@
uintfptr_t _baseaddr, uintptr_t _dynaddr, char *_path);
void pmclog_process_procexit(struct pmc *_pm, struct pmc_process *_pp);
void pmclog_process_procfork(struct pmc_owner *_po, pid_t _oldpid, pid_t _newpid);
+int pmclog_process_ratechange(struct pmc_owner *_po, uint64_t _tsc,
+ uint32_t _rate, uint32_t _pertick, uint32_t _flags, uint32_t _intrns);
void pmclog_process_sysexit(struct pmc_owner *_po, pid_t _pid);
void pmclog_process_threadcreate(struct pmc_owner *_po, struct thread *td, int sync);
void pmclog_process_threadexit(struct pmc_owner *_po, struct thread *td);
diff --git a/tests/sys/pmc/pmc_throttle_test.c b/tests/sys/pmc/pmc_throttle_test.c
--- a/tests/sys/pmc/pmc_throttle_test.c
+++ b/tests/sys/pmc/pmc_throttle_test.c
@@ -152,6 +152,7 @@
struct saved_sysctls {
u_int rate;
+ u_int pct;
u_int mincount;
};
@@ -163,6 +164,7 @@
sv.mincount = hwpmc_get("mincount");
sv.rate = hwpmc_get("max_sample_rate");
+ sv.pct = hwpmc_get("cpu_time_max_percent");
fd = open(SAVE_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0600);
ATF_REQUIRE(fd >= 0);
ATF_REQUIRE(write(fd, &sv, sizeof(sv)) == sizeof(sv));
@@ -180,6 +182,7 @@
return;
if (read(fd, &sv, sizeof(sv)) == sizeof(sv)) {
(void)hwpmc_write("max_sample_rate", sv.rate);
+ (void)hwpmc_write("cpu_time_max_percent", sv.pct);
(void)hwpmc_write("mincount", sv.mincount);
}
close(fd);
@@ -347,6 +350,78 @@
sysctls_restore();
}
+/* A 1% CPU budget must cut the rate. */
+ATF_TC_WITH_CLEANUP(time_limited);
+ATF_TC_HEAD(time_limited, tc)
+{
+ atf_tc_set_md_var(tc, "require.user", "root");
+ atf_tc_set_md_var(tc, "timeout", "30");
+}
+ATF_TC_BODY(time_limited, tc)
+{
+ struct pmclog_ev ev;
+ uint64_t lowered0, lowered1, nrec, throttled;
+ double rate, bound;
+ u_int lastrate, newrate;
+ void *parser;
+ int fd, hz;
+
+ hz = throttle_setup(100);
+ hwpmc_set("cpu_time_max_percent", 1);
+
+ lowered0 = stat_get("rate_lowered");
+ rate = measure_log(&throttled, "rate.log", NULL);
+ lowered1 = stat_get("rate_lowered");
+ newrate = hwpmc_get("max_sample_rate");
+
+ sysctls_restore();
+
+ /* Each cut before the last sample has a record. */
+ fd = open("rate.log", O_RDONLY);
+ ATF_REQUIRE(fd >= 0);
+ parser = pmclog_open(fd);
+ ATF_REQUIRE(parser != NULL);
+ nrec = 0;
+ lastrate = 0;
+ while (pmclog_read(parser, &ev) == 0) {
+ if (ev.pl_type != PMCLOG_TYPE_RATECHANGE)
+ continue;
+ nrec++;
+ ATF_CHECK_MSG(ev.pl_u.pl_rc.pl_flags == PMCLOG_RATE_INTRTIME,
+ "flags 0x%x", ev.pl_u.pl_rc.pl_flags);
+ ATF_CHECK_MSG(ev.pl_u.pl_rc.pl_pertick > 0 &&
+ ev.pl_u.pl_rc.pl_rate <= ev.pl_u.pl_rc.pl_pertick * hz,
+ "rate %u, pertick %u", ev.pl_u.pl_rc.pl_rate,
+ ev.pl_u.pl_rc.pl_pertick);
+ ATF_CHECK_MSG(ev.pl_u.pl_rc.pl_intrns > 0, "no interrupt time");
+ lastrate = ev.pl_u.pl_rc.pl_rate;
+ }
+ ATF_CHECK_MSG(ev.pl_state == PMCLOG_EOF, "log state %d",
+ ev.pl_state);
+ pmclog_close(parser);
+ close(fd);
+ printf("ratechange records %ju, last rate %u/s\n", (uintmax_t)nrec,
+ lastrate);
+ ATF_CHECK_MSG(nrec > 0, "no RATECHANGE record in the log");
+ ATF_CHECK_MSG(lastrate >= newrate && lastrate < 100 * (u_int)hz,
+ "last record rate %u, limit %u", lastrate, newrate);
+
+ bound = 1.5 * (newrate + hz);
+ printf("hz %d, rate %.0f/s, new limit %u/s, lowered %ju\n", hz,
+ rate, newrate, (uintmax_t)(lowered1 - lowered0));
+ ATF_CHECK_MSG(lowered1 > lowered0, "the rate was not lowered");
+ ATF_CHECK_MSG(newrate < 100 * (u_int)hz,
+ "max_sample_rate %u was not lowered", newrate);
+ ATF_CHECK_MSG(rate <= bound, "rate %.0f/s is above %.0f/s", rate,
+ bound);
+}
+
+ATF_TC_CLEANUP(time_limited, tc)
+{
+
+ sysctls_restore();
+}
+
/* One throttle per tick. */
ATF_TC_WITH_CLEANUP(short_period);
ATF_TC_HEAD(short_period, tc)
@@ -420,6 +495,7 @@
{
ATF_TP_ADD_TC(tp, rate_limited);
ATF_TP_ADD_TC(tp, rate_unlimited);
+ ATF_TP_ADD_TC(tp, time_limited);
ATF_TP_ADD_TC(tp, short_period);
ATF_TP_ADD_TC(tp, flush_loop);
return (atf_no_error());
diff --git a/usr.sbin/pmc/view.cc b/usr.sbin/pmc/view.cc
--- a/usr.sbin/pmc/view.cc
+++ b/usr.sbin/pmc/view.cc
@@ -313,6 +313,7 @@
case PMCLOG_TYPE_USERDATA:
case PMCLOG_TYPE_PROCCSW:
case PMCLOG_TYPE_DROPNOTIFY:
+ case PMCLOG_TYPE_RATECHANGE:
return;
};
}
diff --git a/usr.sbin/pmcstat/pmcstat_log.c b/usr.sbin/pmcstat/pmcstat_log.c
--- a/usr.sbin/pmcstat/pmcstat_log.c
+++ b/usr.sbin/pmcstat/pmcstat_log.c
@@ -497,7 +497,7 @@
pmcstat_print_log(void)
{
struct pmclog_ev ev;
- uint32_t npc;
+ uint32_t npc, rcf;
while (pmclog_read(args.pa_logparser, &ev) == 0) {
assert(ev.pl_state == PMCLOG_OK);
@@ -604,6 +604,15 @@
ev.pl_u.pl_f.pl_oldpid,
ev.pl_u.pl_f.pl_newpid);
break;
+ case PMCLOG_TYPE_RATECHANGE:
+ rcf = ev.pl_u.pl_rc.pl_flags;
+ PMCSTAT_PRINT_ENTRY("ratechange",
+ "rate=%u pertick=%u intrns=%u%s%s",
+ ev.pl_u.pl_rc.pl_rate, ev.pl_u.pl_rc.pl_pertick,
+ ev.pl_u.pl_rc.pl_intrns,
+ (rcf & PMCLOG_RATE_INTRTIME) != 0 ? " intr" : "",
+ (rcf & PMCLOG_RATE_SYSCTL) != 0 ? " sysctl" : "");
+ break;
case PMCLOG_TYPE_USERDATA:
PMCSTAT_PRINT_ENTRY("userdata","0x%x",
ev.pl_u.pl_u.pl_userdata);

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 24, 9:18 PM (10 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41964009
Default Alt Text
D61372.1795555116.diff (25 KB)

Event Timeline