From 5fca70ef57d735e023e672d7f39a513e0c750951 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Fri, 16 Dec 2016 10:54:38 +0000 Subject: [PATCH] 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 --- src/util/virperf.c | 179 +++++++++++++++++++++++---------------------- 1 file changed, 93 insertions(+), 86 deletions(-) diff --git a/src/util/virperf.c b/src/util/virperf.c index e39cebbe0f..e26e4db8d7 100644 --- a/src/util/virperf.c +++ b/src/util/virperf.c @@ -71,102 +71,109 @@ struct virPerf { # include struct virPerfEventAttr { - int type; unsigned int attrType; unsigned long long attrConfig; }; static struct virPerfEventAttr attrs[] = { - {.type = VIR_PERF_EVENT_CMT, .attrType = 0, .attrConfig = 1}, - {.type = VIR_PERF_EVENT_MBMT, .attrType = 0, .attrConfig = 2}, - {.type = VIR_PERF_EVENT_MBML, .attrType = 0, .attrConfig = 3}, - {.type = VIR_PERF_EVENT_CPU_CYCLES, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_CPU_CYCLES}, - {.type = VIR_PERF_EVENT_INSTRUCTIONS, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_INSTRUCTIONS}, - {.type = VIR_PERF_EVENT_CACHE_REFERENCES, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_CACHE_REFERENCES}, - {.type = VIR_PERF_EVENT_CACHE_MISSES, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_CACHE_MISSES}, - {.type = VIR_PERF_EVENT_BRANCH_INSTRUCTIONS, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_BRANCH_INSTRUCTIONS}, - {.type = VIR_PERF_EVENT_BRANCH_MISSES, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_BRANCH_MISSES}, - {.type = VIR_PERF_EVENT_BUS_CYCLES, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_BUS_CYCLES}, - {.type = VIR_PERF_EVENT_STALLED_CYCLES_FRONTEND, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND}, - {.type = VIR_PERF_EVENT_STALLED_CYCLES_BACKEND, - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_BACKEND}, - {.type = VIR_PERF_EVENT_REF_CPU_CYCLES, + [VIR_PERF_EVENT_CMT] = { + .attrType = 0, + .attrConfig = 1 + }, + [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, + .attrConfig = PERF_COUNT_HW_CPU_CYCLES + }, + [VIR_PERF_EVENT_INSTRUCTIONS] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_INSTRUCTIONS + }, + [VIR_PERF_EVENT_CACHE_REFERENCES] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_CACHE_REFERENCES + }, + [VIR_PERF_EVENT_CACHE_MISSES] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_CACHE_MISSES + }, + [VIR_PERF_EVENT_BRANCH_INSTRUCTIONS] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_BRANCH_INSTRUCTIONS + }, + [VIR_PERF_EVENT_BRANCH_MISSES] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_BRANCH_MISSES + }, + [VIR_PERF_EVENT_BUS_CYCLES] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_BUS_CYCLES + }, + [VIR_PERF_EVENT_STALLED_CYCLES_FRONTEND] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND + }, + [VIR_PERF_EVENT_STALLED_CYCLES_BACKEND] = { + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_STALLED_CYCLES_BACKEND + }, + [VIR_PERF_EVENT_REF_CPU_CYCLES] = { # ifdef PERF_COUNT_HW_REF_CPU_CYCLES - .attrType = PERF_TYPE_HARDWARE, - .attrConfig = PERF_COUNT_HW_REF_CPU_CYCLES + .attrType = PERF_TYPE_HARDWARE, + .attrConfig = PERF_COUNT_HW_REF_CPU_CYCLES # else - .attrType = 0, - .attrConfig = 0, + .attrType = 0, + .attrConfig = 0, # endif }, - {.type = VIR_PERF_EVENT_CPU_CLOCK, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_CPU_CLOCK}, - {.type = VIR_PERF_EVENT_TASK_CLOCK, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_TASK_CLOCK}, - {.type = VIR_PERF_EVENT_PAGE_FAULTS, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_PAGE_FAULTS}, - {.type = VIR_PERF_EVENT_CONTEXT_SWITCHES, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_CONTEXT_SWITCHES}, - {.type = VIR_PERF_EVENT_CPU_MIGRATIONS, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_CPU_MIGRATIONS}, - {.type = VIR_PERF_EVENT_PAGE_FAULTS_MIN, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MIN}, - {.type = VIR_PERF_EVENT_PAGE_FAULTS_MAJ, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MAJ}, - {.type = VIR_PERF_EVENT_ALIGNMENT_FAULTS, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_ALIGNMENT_FAULTS}, - {.type = VIR_PERF_EVENT_EMULATION_FAULTS, - .attrType = PERF_TYPE_SOFTWARE, - .attrConfig = PERF_COUNT_SW_EMULATION_FAULTS}, + [VIR_PERF_EVENT_CPU_CLOCK] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_CPU_CLOCK + }, + [VIR_PERF_EVENT_TASK_CLOCK] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_TASK_CLOCK + }, + [VIR_PERF_EVENT_PAGE_FAULTS] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_PAGE_FAULTS + }, + [VIR_PERF_EVENT_CONTEXT_SWITCHES] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_CONTEXT_SWITCHES + }, + [VIR_PERF_EVENT_CPU_MIGRATIONS] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_CPU_MIGRATIONS + }, + [VIR_PERF_EVENT_PAGE_FAULTS_MIN] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MIN + }, + [VIR_PERF_EVENT_PAGE_FAULTS_MAJ] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_PAGE_FAULTS_MAJ + }, + [VIR_PERF_EVENT_ALIGNMENT_FAULTS] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_ALIGNMENT_FAULTS + }, + [VIR_PERF_EVENT_EMULATION_FAULTS] = { + .attrType = PERF_TYPE_SOFTWARE, + .attrConfig = PERF_COUNT_SW_EMULATION_FAULTS + }, }; +verify(ARRAY_CARDINALITY(attrs) == VIR_PERF_EVENT_LAST); 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 virPerfRdtAttrInit(void) { @@ -224,9 +231,9 @@ virPerfEventEnable(virPerfPtr perf, char *buf = NULL; struct perf_event_attr attr; virPerfEventPtr event = virPerfGetEvent(perf, type); - virPerfEventAttrPtr event_attr = virPerfGetEventAttr(type); + virPerfEventAttrPtr event_attr = &attrs[type]; - if (!event || !event_attr) + if (!event) return -1; if (event->enabled) @@ -237,7 +244,7 @@ virPerfEventEnable(virPerfPtr perf, type == VIR_PERF_EVENT_MBML)) { virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, _("unable to enable host cpu perf event for %s"), - virPerfEventTypeToString(event->type)); + virPerfEventTypeToString(type)); return -1; }