perf: get rid of pointless virPerfGetEventAttr() method

The virPerfGetEventAttr method contains a totally pointless
loop. Remove it, verify the array size statically, and then
just use an array index to access the perf event.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-12-16 10:54:38 +00:00
parent c3d1df7b85
commit 5fca70ef57

View File

@ -71,43 +71,60 @@ struct virPerf {
# include <linux/perf_event.h> # include <linux/perf_event.h>
struct virPerfEventAttr { struct virPerfEventAttr {
int type;
unsigned int attrType; unsigned int attrType;
unsigned long long attrConfig; unsigned long long attrConfig;
}; };
static struct virPerfEventAttr attrs[] = { static struct virPerfEventAttr attrs[] = {
{.type = VIR_PERF_EVENT_CMT, .attrType = 0, .attrConfig = 1}, [VIR_PERF_EVENT_CMT] = {
{.type = VIR_PERF_EVENT_MBMT, .attrType = 0, .attrConfig = 2}, .attrType = 0,
{.type = VIR_PERF_EVENT_MBML, .attrType = 0, .attrConfig = 3}, .attrConfig = 1
{.type = VIR_PERF_EVENT_CPU_CYCLES, },
[VIR_PERF_EVENT_MBMT] = {
.attrType = 0,
.attrConfig = 2
},
[VIR_PERF_EVENT_MBML] {
.attrType = 0,
.attrConfig = 3
},
[VIR_PERF_EVENT_CPU_CYCLES] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_CPU_CYCLES}, .attrConfig = PERF_COUNT_HW_CPU_CYCLES
{.type = VIR_PERF_EVENT_INSTRUCTIONS, },
[VIR_PERF_EVENT_INSTRUCTIONS] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_INSTRUCTIONS}, .attrConfig = PERF_COUNT_HW_INSTRUCTIONS
{.type = VIR_PERF_EVENT_CACHE_REFERENCES, },
[VIR_PERF_EVENT_CACHE_REFERENCES] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_CACHE_REFERENCES}, .attrConfig = PERF_COUNT_HW_CACHE_REFERENCES
{.type = VIR_PERF_EVENT_CACHE_MISSES, },
[VIR_PERF_EVENT_CACHE_MISSES] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_CACHE_MISSES}, .attrConfig = PERF_COUNT_HW_CACHE_MISSES
{.type = VIR_PERF_EVENT_BRANCH_INSTRUCTIONS, },
[VIR_PERF_EVENT_BRANCH_INSTRUCTIONS] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_BRANCH_INSTRUCTIONS}, .attrConfig = PERF_COUNT_HW_BRANCH_INSTRUCTIONS
{.type = VIR_PERF_EVENT_BRANCH_MISSES, },
[VIR_PERF_EVENT_BRANCH_MISSES] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_BRANCH_MISSES}, .attrConfig = PERF_COUNT_HW_BRANCH_MISSES
{.type = VIR_PERF_EVENT_BUS_CYCLES, },
[VIR_PERF_EVENT_BUS_CYCLES] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_BUS_CYCLES}, .attrConfig = PERF_COUNT_HW_BUS_CYCLES
{.type = VIR_PERF_EVENT_STALLED_CYCLES_FRONTEND, },
[VIR_PERF_EVENT_STALLED_CYCLES_FRONTEND] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND}, .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
{.type = VIR_PERF_EVENT_STALLED_CYCLES_BACKEND, },
[VIR_PERF_EVENT_STALLED_CYCLES_BACKEND] = {
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_STALLED_CYCLES_BACKEND}, .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_BACKEND
{.type = VIR_PERF_EVENT_REF_CPU_CYCLES, },
[VIR_PERF_EVENT_REF_CPU_CYCLES] = {
# ifdef PERF_COUNT_HW_REF_CPU_CYCLES # ifdef PERF_COUNT_HW_REF_CPU_CYCLES
.attrType = PERF_TYPE_HARDWARE, .attrType = PERF_TYPE_HARDWARE,
.attrConfig = PERF_COUNT_HW_REF_CPU_CYCLES .attrConfig = PERF_COUNT_HW_REF_CPU_CYCLES
@ -116,57 +133,47 @@ static struct virPerfEventAttr attrs[] = {
.attrConfig = 0, .attrConfig = 0,
# endif # endif
}, },
{.type = VIR_PERF_EVENT_CPU_CLOCK, [VIR_PERF_EVENT_CPU_CLOCK] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_CPU_CLOCK}, .attrConfig = PERF_COUNT_SW_CPU_CLOCK
{.type = VIR_PERF_EVENT_TASK_CLOCK, },
[VIR_PERF_EVENT_TASK_CLOCK] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_TASK_CLOCK}, .attrConfig = PERF_COUNT_SW_TASK_CLOCK
{.type = VIR_PERF_EVENT_PAGE_FAULTS, },
[VIR_PERF_EVENT_PAGE_FAULTS] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_PAGE_FAULTS}, .attrConfig = PERF_COUNT_SW_PAGE_FAULTS
{.type = VIR_PERF_EVENT_CONTEXT_SWITCHES, },
[VIR_PERF_EVENT_CONTEXT_SWITCHES] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_CONTEXT_SWITCHES}, .attrConfig = PERF_COUNT_SW_CONTEXT_SWITCHES
{.type = VIR_PERF_EVENT_CPU_MIGRATIONS, },
[VIR_PERF_EVENT_CPU_MIGRATIONS] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_CPU_MIGRATIONS}, .attrConfig = PERF_COUNT_SW_CPU_MIGRATIONS
{.type = VIR_PERF_EVENT_PAGE_FAULTS_MIN, },
[VIR_PERF_EVENT_PAGE_FAULTS_MIN] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MIN}, .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MIN
{.type = VIR_PERF_EVENT_PAGE_FAULTS_MAJ, },
[VIR_PERF_EVENT_PAGE_FAULTS_MAJ] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MAJ}, .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MAJ
{.type = VIR_PERF_EVENT_ALIGNMENT_FAULTS, },
[VIR_PERF_EVENT_ALIGNMENT_FAULTS] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_ALIGNMENT_FAULTS}, .attrConfig = PERF_COUNT_SW_ALIGNMENT_FAULTS
{.type = VIR_PERF_EVENT_EMULATION_FAULTS, },
[VIR_PERF_EVENT_EMULATION_FAULTS] = {
.attrType = PERF_TYPE_SOFTWARE, .attrType = PERF_TYPE_SOFTWARE,
.attrConfig = PERF_COUNT_SW_EMULATION_FAULTS}, .attrConfig = PERF_COUNT_SW_EMULATION_FAULTS
},
}; };
verify(ARRAY_CARDINALITY(attrs) == VIR_PERF_EVENT_LAST);
typedef struct virPerfEventAttr *virPerfEventAttrPtr; typedef struct virPerfEventAttr *virPerfEventAttrPtr;
static virPerfEventAttrPtr
virPerfGetEventAttr(virPerfEventType type)
{
size_t i;
if (type >= VIR_PERF_EVENT_LAST) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Event '%d' is not supported"),
type);
return NULL;
}
for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
if (i == type)
return attrs + i;
}
return NULL;
}
static int static int
virPerfRdtAttrInit(void) virPerfRdtAttrInit(void)
{ {
@ -224,9 +231,9 @@ virPerfEventEnable(virPerfPtr perf,
char *buf = NULL; char *buf = NULL;
struct perf_event_attr attr; struct perf_event_attr attr;
virPerfEventPtr event = virPerfGetEvent(perf, type); virPerfEventPtr event = virPerfGetEvent(perf, type);
virPerfEventAttrPtr event_attr = virPerfGetEventAttr(type); virPerfEventAttrPtr event_attr = &attrs[type];
if (!event || !event_attr) if (!event)
return -1; return -1;
if (event->enabled) if (event->enabled)
@ -237,7 +244,7 @@ virPerfEventEnable(virPerfPtr perf,
type == VIR_PERF_EVENT_MBML)) { type == VIR_PERF_EVENT_MBML)) {
virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
_("unable to enable host cpu perf event for %s"), _("unable to enable host cpu perf event for %s"),
virPerfEventTypeToString(event->type)); virPerfEventTypeToString(type));
return -1; return -1;
} }