summaryrefslogtreecommitdiff
path: root/arch/x86/machine
diff options
context:
space:
mode:
authorRemy Noel <mocramis@gmail.com>2018-04-30 20:55:28 +0200
committerRemy Noel <mocramis@gmail.com>2018-05-03 14:21:10 +0200
commit8f7aeebfc21af051ecb914d286564a10492927a0 (patch)
tree3a4e131977aefbb2cdce854b52381d64eea46f7e /arch/x86/machine
parentc4ba76cc4f84a25b259e9089b5e49a6edc015f1d (diff)
perfmon: add polling overflow implementation in perfmon
Diffstat (limited to 'arch/x86/machine')
-rw-r--r--arch/x86/machine/pmu_amd.c38
-rw-r--r--arch/x86/machine/pmu_intel.c49
-rw-r--r--arch/x86/machine/trap.c1
3 files changed, 17 insertions, 71 deletions
diff --git a/arch/x86/machine/pmu_amd.c b/arch/x86/machine/pmu_amd.c
index 34df620..8e56bfa 100644
--- a/arch/x86/machine/pmu_amd.c
+++ b/arch/x86/machine/pmu_amd.c
@@ -21,6 +21,7 @@
#include <stdint.h>
#include <kern/init.h>
+#include <kern/clock.h>
#include <kern/log.h>
#include <kern/perfmon.h>
#include <machine/cpu.h>
@@ -175,7 +176,6 @@ pmu_amd_start(unsigned int pmc_id, unsigned int raw_event_id)
/* TODO Handle PERFMON_EF_KERN/PERFMON_EF_USER */
high = code->event_select >> 8;
low = PMU_AMD_EVTSEL_EN
- | PMU_AMD_EVTSEL_INT
| PMU_AMD_EVTSEL_OS
| PMU_AMD_EVTSEL_USR
| (code->umask << 8)
@@ -205,38 +205,6 @@ pmu_amd_write(unsigned int pmc_id, uint64_t value)
cpu_set_msr64(PMU_AMD_MSR_PERCTR0 + pmc_id, value);
}
-/*
- * TODO Make the perfmon module handle basic overflow handling by polling
- * counters.
- */
-static void
-pmu_amd_handle_of_intr_v1(void)
-{
- struct pmu_amd *pmu;
- uint64_t value, prev;
- unsigned int mask;
-
- pmu = pmu_amd_get();
-
- for (unsigned int pmc_id = 0; pmc_id != PMU_AMD_NR_PMCS; pmc_id++) {
- mask = (1U << pmc_id);
-
- if (pmu->pmc_bm & mask) {
- continue;
- }
-
- value = pmu_amd_read(pmc_id);
- prev = perfmon_cpu_pmc_get_prev(pmc_id);
-
- if (prev > value) {
- /* Overflow */
- perfmon_cpu_pmc_inc_of(pmc_id);
- /* Prevents us from overflowing twice */
- perfmon_cpu_pmc_set_prev(pmc_id, value);
- }
- }
-}
-
static int __init
pmu_amd_setup(void)
{
@@ -259,7 +227,9 @@ pmu_amd_setup(void)
pmu->pmc_bm = (1U << PMU_AMD_NR_PMCS) - 1;
pmu_driver.pmc_width = PMU_AMD_PMC_WIDTH;
- pmu_driver.of_max_ticks = 1UL << (pmu_driver.pmc_width - 1);
+ /* Set max_tick to half the number of instruction per seconds. */
+ pmu_driver.of_max_ticks =
+ (1UL << (pmu_driver.pmc_width - 1)) / (cpu_get_freq() / CLOCK_FREQ);
pmu_driver.ops.info = pmu_amd_info;
pmu_driver.ops.translate = pmu_amd_translate;
diff --git a/arch/x86/machine/pmu_intel.c b/arch/x86/machine/pmu_intel.c
index ccc2294..e5d9ef7 100644
--- a/arch/x86/machine/pmu_intel.c
+++ b/arch/x86/machine/pmu_intel.c
@@ -21,6 +21,7 @@
#include <stdint.h>
#include <kern/init.h>
+#include <kern/clock.h>
#include <kern/log.h>
#include <kern/perfmon.h>
#include <machine/cpu.h>
@@ -174,7 +175,7 @@ pmu_intel_info(void)
pmu = pmu_intel_get();
nr_events = pmu_popcount(pmu->events);
- log_info("pmu: driver: intel, architectural v%d\n"
+ log_info("pmu: driver: intel, architectural v%d "
"pmu: nr_pmcs: %u, pmc_width: %u, events: %#x, nr_events: %u\n",
pmu->version, pmu->nr_pmcs, pmu->pmc_width, pmu->events,
nr_events);
@@ -233,19 +234,23 @@ static void
pmu_intel_start(unsigned int pmc_id, unsigned int raw_event_id)
{
const struct pmu_intel_event_code *code;
+ struct pmu_intel *pmu;
uint32_t evtsel;
assert(raw_event_id < ARRAY_SIZE(pmu_intel_event_codes));
code = &pmu_intel_event_codes[raw_event_id];
+ pmu = pmu_intel_get();
/* TODO Handle PERFMON_EF_KERN/PERFMON_EF_USER */
evtsel = PMU_INTEL_EVTSEL_EN
| PMU_INTEL_EVTSEL_OS
| PMU_INTEL_EVTSEL_USR
- | PMU_INTEL_EVTSEL_INT
| (code->umask << 8)
| code->event_select;
+ if (pmu->version >= 2) {
+ evtsel |= PMU_INTEL_EVTSEL_INT;
+ }
cpu_set_msr(PMU_INTEL_MSR_EVTSEL0 + pmc_id, 0, evtsel);
}
@@ -267,38 +272,6 @@ pmu_intel_write(unsigned int pmc_id, uint64_t value)
cpu_set_msr64(PMU_INTEL_MSR_PMC0 + pmc_id, value);
}
-/*
- * TODO Make the perfmon module handle basic overflow handling by polling
- * counters.
- */
-static void
-pmu_intel_handle_of_intr_v1(void)
-{
- struct pmu_intel *pmu;
- unsigned int mask;
- uint64_t value;
- uint64_t prev;
-
- pmu = pmu_intel_get();
-
- for (unsigned int pmc_id = 0; pmc_id != pmu->nr_pmcs; pmc_id++) {
- mask = (1U << pmc_id);
- if (pmu->pmc_bm & mask) {
- /* counter not enabled: can't overflow. */
- continue;
- }
-
- value = pmu_intel_read(pmc_id);
- prev = perfmon_cpu_pmc_get_prev(pmc_id);
- if (prev > value) {
- /* Overflow */
- perfmon_cpu_pmc_inc_of(pmc_id);
- /* Prevents us from overflowing twice */
- perfmon_cpu_pmc_set_prev(pmc_id, value);
- }
- }
-}
-
static int
pmu_intel_consume_bits(uint64_t *bits)
{
@@ -330,7 +303,7 @@ pmu_intel_handle_of_intr_v2(void)
pmu_intel_ack_status(status);
pmu = pmu_intel_get();
- status &= ((1U << pmu->pmc_width) - 1);
+ status &= ((1ULL << pmu->pmc_width) - 1);
for (;;) {
pmc_id = pmu_intel_consume_bits(&status);
@@ -366,7 +339,6 @@ pmu_intel_setup(void)
cpu_cpuid(&eax, &ebx, &ecx, &edx);
pmu->version = eax & PMU_INTEL_ID_VERSION_MASK;
- /* TODO Check this */
if (pmu->version == 0) {
return ENODEV;
}
@@ -396,7 +368,10 @@ pmu_intel_setup(void)
pmu_driver.ops.handle_of_intr = pmu_intel_handle_of_intr_v2;
pmu_driver.of_max_ticks = 0;
} else {
- pmu_driver.of_max_ticks = 1UL << (pmu_driver.pmc_width - 1);
+ /* Set max_tick to half the number of instruction per seconds. */
+ pmu_driver.ops.handle_of_intr = NULL;
+ pmu_driver.of_max_ticks =
+ (1ULL << (pmu_driver.pmc_width - 1)) / (cpu_get_freq() / CLOCK_FREQ);
}
return perfmon_pmu_register(&pmu_driver);
diff --git a/arch/x86/machine/trap.c b/arch/x86/machine/trap.c
index 101adf8..a7a5cbd 100644
--- a/arch/x86/machine/trap.c
+++ b/arch/x86/machine/trap.c
@@ -214,6 +214,7 @@ trap_setup(void)
trap_install(TRAP_LAPIC_TIMER, TRAP_HF_INTR, lapic_timer_intr);
trap_install(TRAP_LAPIC_ERROR, TRAP_HF_INTR, lapic_error_intr);
trap_install(TRAP_LAPIC_SPURIOUS, TRAP_HF_INTR, lapic_spurious_intr);
+ trap_install(TRAP_LAPIC_PMC_OF, TRAP_HF_INTR, lapic_pmc_of_intr);
return 0;
}