perf: get rid of pointless virPerfGetEvent() method

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@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-12-16 10:58:29 +00:00
parent 5fca70ef57
commit bdcf6e4810
1 changed files with 8 additions and 36 deletions

View File

@ -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 {
@ -206,23 +205,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,
@ -230,12 +212,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;
@ -274,14 +253,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;
}
@ -298,9 +277,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;
@ -308,7 +285,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;
}
@ -320,11 +297,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
@ -332,8 +305,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) {
@ -404,7 +377,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;
}