virperf.c 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * virperf.c: methods for managing perf events
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *  Ren Qiaowei <qiaowei.ren@intel.com>
 */
#include <config.h>

#include <sys/ioctl.h>
#if defined HAVE_SYS_SYSCALL_H
# include <sys/syscall.h>
#endif

#include "virperf.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#include "virfile.h"
#include "virstring.h"
#include "virtypedparam.h"

VIR_LOG_INIT("util.perf");

#define VIR_FROM_THIS VIR_FROM_PERF

VIR_ENUM_IMPL(virPerfEvent, VIR_PERF_EVENT_LAST,
41
              "cmt", "mbmt", "mbml");
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

struct virPerfEvent {
    int type;
    int fd;
    bool enabled;
    union {
        /* cmt */
        struct {
            int scale;
        } cmt;
    } efields;
};
typedef struct virPerfEvent *virPerfEventPtr;

struct virPerf {
    struct virPerfEvent events[VIR_PERF_EVENT_LAST];
};

#if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)

# include <linux/perf_event.h>

static virPerfEventPtr
virPerfGetEvent(virPerfPtr perf,
                virPerfEventType type)
{
68 69 70
    if (!perf)
        return NULL;

71 72 73 74 75 76 77 78 79 80 81
    if (type >= VIR_PERF_EVENT_LAST) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Event '%d' is not supported"),
                       type);
        return NULL;
    }

    return perf->events + type;
}

static int
82
virPerfRdtEnable(virPerfEventPtr event,
83 84
                 pid_t pid)
{
85
    struct perf_event_attr rdt_attr;
86 87 88 89 90 91
    char *buf = NULL;
    char *tmp = NULL;
    unsigned int event_type, scale;

    if (virFileReadAll("/sys/devices/intel_cqm/type",
                       10, &buf) < 0)
92
        goto error;
93 94 95 96 97 98

    if ((tmp = strchr(buf, '\n')))
        *tmp = '\0';

    if (virStrToLong_ui(buf, NULL, 10, &event_type) < 0) {
        virReportSystemError(errno, "%s",
99
                             _("failed to get rdt event type"));
100
        goto error;
101 102 103
    }
    VIR_FREE(buf);

104 105 106 107 108 109
    memset(&rdt_attr, 0, sizeof(rdt_attr));
    rdt_attr.size = sizeof(rdt_attr);
    rdt_attr.type = event_type;
    rdt_attr.inherit = 1;
    rdt_attr.disabled = 1;
    rdt_attr.enable_on_exec = 0;
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    switch (event->type) {
    case VIR_PERF_EVENT_CMT:
        if (virFileReadAll("/sys/devices/intel_cqm/events/llc_occupancy.scale",
                           10, &buf) < 0)
            goto error;

        if (virStrToLong_ui(buf, NULL, 10, &scale) < 0) {
            virReportSystemError(errno, "%s",
                                 _("failed to get cmt scaling factor"));
            goto error;
        }
        event->efields.cmt.scale = scale;

        rdt_attr.config = 1;
        break;
    case VIR_PERF_EVENT_MBMT:
        rdt_attr.config = 2;
        break;
    case VIR_PERF_EVENT_MBML:
        rdt_attr.config = 3;
        break;
132 133
    }

134
    event->fd = syscall(__NR_perf_event_open, &rdt_attr, pid, -1, -1, 0);
135 136 137 138
    if (event->fd < 0) {
        virReportSystemError(errno,
                             _("Unable to open perf type=%d for pid=%d"),
                             event_type, pid);
139
        goto error;
140 141 142
    }

    if (ioctl(event->fd, PERF_EVENT_IOC_ENABLE) < 0) {
143 144 145
        virReportSystemError(errno,
                             _("Unable to enable perf event for %s"),
                             virPerfEventTypeToString(event->type));
146
        goto error;
147 148 149 150 151
    }

    event->enabled = true;
    return 0;

152
 error:
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    VIR_FORCE_CLOSE(event->fd);
    VIR_FREE(buf);
    return -1;
}

int
virPerfEventEnable(virPerfPtr perf,
                   virPerfEventType type,
                   pid_t pid)
{
    virPerfEventPtr event = virPerfGetEvent(perf, type);
    if (event == NULL)
        return -1;

    switch (type) {
    case VIR_PERF_EVENT_CMT:
169 170 171
    case VIR_PERF_EVENT_MBMT:
    case VIR_PERF_EVENT_MBML:
        if (virPerfRdtEnable(event, pid) < 0)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            return -1;
        break;
    case VIR_PERF_EVENT_LAST:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unexpected perf event type=%d"), type);
        return -1;
    }

    return 0;
}

int
virPerfEventDisable(virPerfPtr perf,
                    virPerfEventType type)
{
    virPerfEventPtr event = virPerfGetEvent(perf, type);
    if (event == NULL)
        return -1;

    if (ioctl(event->fd, PERF_EVENT_IOC_DISABLE) < 0) {
        virReportSystemError(errno,
                             _("Unable to disable perf event type=%d"),
                             event->type);
        return -1;
    }

    event->enabled = false;
    VIR_FORCE_CLOSE(event->fd);
    return 0;
}

bool virPerfEventIsEnabled(virPerfPtr perf,
                           virPerfEventType type)
{
    virPerfEventPtr event = virPerfGetEvent(perf, type);
    if (event == NULL)
        return false;

    return event->enabled;
}

int
virPerfReadEvent(virPerfPtr perf,
                 virPerfEventType type,
                 uint64_t *value)
{
    virPerfEventPtr event = virPerfGetEvent(perf, type);
    if (event == NULL || !event->enabled)
        return -1;

222
    if (saferead(event->fd, value, sizeof(uint64_t)) < 0) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236
        virReportSystemError(errno, "%s",
                             _("Unable to read cache data"));
        return -1;
    }

    if (type == VIR_PERF_EVENT_CMT)
        *value *= event->efields.cmt.scale;

    return 0;
}

#else
int
virPerfEventEnable(virPerfPtr perf ATTRIBUTE_UNUSED,
R
Roman Bogorodskiy 已提交
237
                   virPerfEventType type ATTRIBUTE_UNUSED,
238 239 240 241 242 243 244 245 246
                   pid_t pid ATTRIBUTE_UNUSED)
{
    virReportSystemError(ENXIO, "%s",
                         _("Perf not supported on this platform"));
    return -1;
}

int
virPerfEventDisable(virPerfPtr perf ATTRIBUTE_UNUSED,
R
Roman Bogorodskiy 已提交
247
                    virPerfEventType type ATTRIBUTE_UNUSED)
248 249 250 251 252 253 254
{
    virReportSystemError(ENXIO, "%s",
                         _("Perf not supported on this platform"));
    return -1;
}

bool
R
Roman Bogorodskiy 已提交
255 256
virPerfEventIsEnabled(virPerfPtr perf ATTRIBUTE_UNUSED,
                      virPerfEventType type ATTRIBUTE_UNUSED)
257 258 259 260 261
{
    return false;
}

int
R
Roman Bogorodskiy 已提交
262 263 264
virPerfReadEvent(virPerfPtr perf ATTRIBUTE_UNUSED,
                 virPerfEventType type ATTRIBUTE_UNUSED,
                 uint64_t *value ATTRIBUTE_UNUSED)
265 266 267 268 269 270 271
{
    virReportSystemError(ENXIO, "%s",
                         _("Perf not supported on this platform"));
    return -1;
}

#endif
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

virPerfPtr
virPerfNew(void)
{
    size_t i;
    virPerfPtr perf;

    if (VIR_ALLOC(perf) < 0)
        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;
    }

    return perf;
}

void
virPerfFree(virPerfPtr perf)
{
    size_t i;

    if (perf == NULL)
        return;

    for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
        if (perf->events[i].enabled)
            virPerfEventDisable(perf, i);
    }

    VIR_FREE(perf);
}