[PATCH v2 05/32] perf/x86/intel/cqm: encapsulate per-package RMIDs
From: David Carrillo-Cisneros
Date: Wed May 11 2016 - 19:02:55 EST
A raw RMID value is encapsulated in a Package RMID (prmid) structure
that provides atomic updates and caches recent reads.
Also, introduce __cqm_prmid_update, a helper function that reads
occupancy of a RMID while keeping track of last_read_time to
avoid multiple reads within a short period of time when moderately
stale values are acceptable, as is the case for some scenarios in this
series (e.g. reading occupancy for limbo RMIDs, or multiple transversals
of the RMID hierarchy).
Reviewed-by: Stephane Eranian <eranian@xxxxxxxxxx>
Signed-off-by: David Carrillo-Cisneros <davidcc@xxxxxxxxxx>
---
arch/x86/events/intel/cqm.c | 51 +++++++++++++++++++++++++++++++++++++++++++++
arch/x86/events/intel/cqm.h | 18 ++++++++++++++++
2 files changed, 69 insertions(+)
diff --git a/arch/x86/events/intel/cqm.c b/arch/x86/events/intel/cqm.c
index 0c4f3fe..2daee37 100644
--- a/arch/x86/events/intel/cqm.c
+++ b/arch/x86/events/intel/cqm.c
@@ -18,3 +18,54 @@
#define QOS_L3_OCCUP_EVENT_ID (1 << 0)
#define QOS_EVENT_MASK QOS_L3_OCCUP_EVENT_ID
+
+/*
+ * Update if enough time has passed since last read.
+ *
+ * Must be called in a cpu in the package where prmid belongs.
+ * This function is safe to be called concurrently since it is guaranteed
+ * that entry->last_read_value is updated to a occupancy value obtained
+ * after the time set in entry->last_read_time .
+ * Return 1 if value was updated, 0 if not, negative number if error.
+ */
+static inline int __cqm_prmid_update(struct prmid *prmid,
+ unsigned long jiffies_min_delta)
+{
+ unsigned long now = jiffies;
+ unsigned long last_read_time;
+ u64 val;
+
+ /*
+ * Shortcut the calculation of elapsed time for the
+ * case jiffies_min_delta == 0
+ */
+ if (jiffies_min_delta > 0) {
+ last_read_time = atomic64_read(&prmid->last_read_time);
+ if (time_after(last_read_time + jiffies_min_delta, now))
+ return 0;
+ }
+
+ wrmsr(MSR_IA32_QM_EVTSEL, QOS_L3_OCCUP_EVENT_ID, prmid->rmid);
+ rdmsrl(MSR_IA32_QM_CTR, val);
+
+ /*
+ * Ignore this reading on error states and do not update the value.
+ */
+ WARN_ON_ONCE(val & (RMID_VAL_ERROR | RMID_VAL_UNAVAIL));
+ if (val & RMID_VAL_ERROR)
+ return -EINVAL;
+ if (val & RMID_VAL_UNAVAIL)
+ return -ENODATA;
+
+ atomic64_set(&prmid->last_read_value, val);
+ /*
+ * Protect last_read_time from being updated before last_read_value is.
+ * So reader always receive an updated value even if sometimes values
+ * are updated twice.
+ */
+ smp_wmb();
+
+ atomic64_set(&prmid->last_read_time, now);
+
+ return 1;
+}
diff --git a/arch/x86/events/intel/cqm.h b/arch/x86/events/intel/cqm.h
index bb906c7..06964cd 100644
--- a/arch/x86/events/intel/cqm.h
+++ b/arch/x86/events/intel/cqm.h
@@ -23,6 +23,24 @@
#include <asm/topology.h>
/*
+ * struct prmid: Package RMID. Per-package wrapper for a rmid.
+ * @last_read_value: Least read value.
+ * @last_read_time: Time last read, used when throtling read rate.
+ * @pool_entry: Attaches to a prmid pool in cqm_pkg_data.
+ * @rmid: The rmid value to be programed in hardware.
+ *
+ * Its accesors ensure that CQM events for this rmid are read atomically and
+ * allow to throtle the frequency of reads to up to one each
+ * __rmid_min_update_time ms.
+ */
+struct prmid {
+ atomic64_t last_read_value;
+ atomic64_t last_read_time;
+ struct list_head pool_entry;
+ u32 rmid;
+};
+
+/*
* Time between execution of rotation logic. The frequency of execution does
* not affect the rate at which RMIDs are recycled, except by the delay by the
* delay updating the prmid's and their pools.
--
2.8.0.rc3.226.g39d4020