The virPerfGetEvent method pointlessly checks for a NULL
parameter and the range of an enum value. The whole point
of using an enum is that we can avoid such checks. Just
replace calls to virPerfGetEvent, with perf->events[type]
array access.
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/util/virperf.c | 44 ++++++++------------------------------------
1 file changed, 8 insertions(+), 36 deletions(-)
diff --git a/src/util/virperf.c b/src/util/virperf.c
index 7366efc..a04bb67 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -50,7 +50,6 @@ VIR_ENUM_IMPL(virPerfEvent, VIR_PERF_EVENT_LAST,
"alignment_faults", "emulation_faults");
struct virPerfEvent {
- int type;
int fd;
bool enabled;
union {
@@ -188,23 +187,6 @@ virPerfRdtAttrInit(void)
}
-static virPerfEventPtr
-virPerfGetEvent(virPerfPtr perf,
- virPerfEventType type)
-{
- if (!perf)
- return NULL;
-
- if (type >= VIR_PERF_EVENT_LAST) {
- virReportError(VIR_ERR_INTERNAL_ERROR,
- _("Event '%d' is not supported"),
- type);
- return NULL;
- }
-
- return perf->events + type;
-}
-
int
virPerfEventEnable(virPerfPtr perf,
virPerfEventType type,
@@ -212,12 +194,9 @@ virPerfEventEnable(virPerfPtr perf,
{
char *buf = NULL;
struct perf_event_attr attr;
- virPerfEventPtr event = virPerfGetEvent(perf, type);
+ virPerfEventPtr event = &(perf->events[type]);
virPerfEventAttrPtr event_attr = &attrs[type];
- if (!event)
- return -1;
-
if (event->enabled)
return 0;
@@ -256,14 +235,14 @@ virPerfEventEnable(virPerfPtr perf,
if (event->fd < 0) {
virReportSystemError(errno,
_("unable to open host cpu perf event for %s"),
- virPerfEventTypeToString(event->type));
+ virPerfEventTypeToString(type));
goto error;
}
if (ioctl(event->fd, PERF_EVENT_IOC_ENABLE) < 0) {
virReportSystemError(errno,
_("unable to enable host cpu perf event for %s"),
- virPerfEventTypeToString(event->type));
+ virPerfEventTypeToString(type));
goto error;
}
@@ -280,9 +259,7 @@ int
virPerfEventDisable(virPerfPtr perf,
virPerfEventType type)
{
- virPerfEventPtr event = virPerfGetEvent(perf, type);
- if (event == NULL)
- return -1;
+ virPerfEventPtr event = &(perf->events[type]);
if (!event->enabled)
return 0;
@@ -290,7 +267,7 @@ virPerfEventDisable(virPerfPtr perf,
if (ioctl(event->fd, PERF_EVENT_IOC_DISABLE) < 0) {
virReportSystemError(errno,
_("unable to disable host cpu perf event for
%s"),
- virPerfEventTypeToString(event->type));
+ virPerfEventTypeToString(type));
return -1;
}
@@ -302,11 +279,7 @@ virPerfEventDisable(virPerfPtr perf,
bool virPerfEventIsEnabled(virPerfPtr perf,
virPerfEventType type)
{
- virPerfEventPtr event = virPerfGetEvent(perf, type);
- if (event == NULL)
- return false;
-
- return event->enabled;
+ return perf->events[type].enabled;
}
int
@@ -314,8 +287,8 @@ virPerfReadEvent(virPerfPtr perf,
virPerfEventType type,
uint64_t *value)
{
- virPerfEventPtr event = virPerfGetEvent(perf, type);
- if (event == NULL || !event->enabled)
+ virPerfEventPtr event = &perf->events[type];
+ if (!event->enabled)
return -1;
if (saferead(event->fd, value, sizeof(uint64_t)) < 0) {
@@ -386,7 +359,6 @@ virPerfNew(void)
return NULL;
for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
- perf->events[i].type = i;
perf->events[i].fd = -1;
perf->events[i].enabled = false;
}
--
2.9.3