virperf.c 10.6 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
/*
 * 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 "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,
Q
Qiaowei Ren 已提交
40 41
              "cmt", "mbmt", "mbml",
              "cpu_cycles", "instructions",
42
              "cache_references", "cache_misses",
43
              "branch_instructions", "branch_misses",
44
              "bus_cycles", "stalled_cycles_frontend",
45
              "stalled_cycles_backend", "ref_cpu_cycles",
46
              "cpu_clock", "task_clock", "page_faults",
47
              "context_switches", "cpu_migrations",
48
              "page_faults_min", "page_faults_maj",
49
              "alignment_faults", "emulation_faults");
50 51 52 53 54 55 56 57 58 59 60 61 62

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

63
struct _virPerf {
64 65 66 67 68 69 70
    struct virPerfEvent events[VIR_PERF_EVENT_LAST];
};

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

# include <linux/perf_event.h>

71 72 73 74 75 76
struct virPerfEventAttr {
    unsigned int attrType;
    unsigned long long attrConfig;
};

static struct virPerfEventAttr attrs[] = {
77 78 79 80 81 82 83 84
    [VIR_PERF_EVENT_CMT] = {
        .attrType = 0,
        .attrConfig = 1
    },
    [VIR_PERF_EVENT_MBMT] = {
        .attrType = 0,
        .attrConfig = 2
    },
85
    [VIR_PERF_EVENT_MBML] = {
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        .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] = {
126
# ifdef PERF_COUNT_HW_REF_CPU_CYCLES
127 128
        .attrType = PERF_TYPE_HARDWARE,
        .attrConfig = PERF_COUNT_HW_REF_CPU_CYCLES
129
# else
130 131
        .attrType = 0,
        .attrConfig = 0,
132 133
# endif
    },
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    [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
    },
170
};
171
verify(ARRAY_CARDINALITY(attrs) == VIR_PERF_EVENT_LAST);
172 173
typedef struct virPerfEventAttr *virPerfEventAttrPtr;

174

175
static int
176
virPerfRdtAttrInit(void)
177 178 179
{
    char *buf = NULL;
    char *tmp = NULL;
180
    unsigned int attr_type = 0;
181

182
    if (virFileReadAllQuiet("/sys/devices/intel_cqm/type", 10, &buf) < 0)
183
        goto error;
184 185 186 187

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

188
    if (virStrToLong_ui(buf, NULL, 10, &attr_type) < 0) {
189
        virReportSystemError(errno, "%s",
190
                             _("failed to get rdt event type"));
191
        goto error;
192 193 194
    }
    VIR_FREE(buf);

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    attrs[VIR_PERF_EVENT_CMT].attrType = attr_type;
    attrs[VIR_PERF_EVENT_MBMT].attrType = attr_type;
    attrs[VIR_PERF_EVENT_MBML].attrType = attr_type;

    return 0;

 error:
    VIR_FREE(buf);
    return -1;
}


int
virPerfEventEnable(virPerfPtr perf,
                   virPerfEventType type,
                   pid_t pid)
{
    char *buf = NULL;
    struct perf_event_attr attr;
214
    virPerfEventPtr event = &(perf->events[type]);
215
    virPerfEventAttrPtr event_attr = &attrs[type];
216

217 218 219
    if (event->enabled)
        return 0;

220 221 222
    if (event_attr->attrType == 0 && (type == VIR_PERF_EVENT_CMT ||
                                       type == VIR_PERF_EVENT_MBMT ||
                                       type == VIR_PERF_EVENT_MBML)) {
223
        virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED,
224
                       _("unable to enable host cpu perf event for %s"),
225
                       virPerfEventTypeToString(type));
226 227 228 229
        return -1;
    }

    if (type == VIR_PERF_EVENT_CMT) {
230 231 232 233
        if (virFileReadAll("/sys/devices/intel_cqm/events/llc_occupancy.scale",
                           10, &buf) < 0)
            goto error;

234
        if (virStrToLong_i(buf, NULL, 10, &event->efields.cmt.scale) < 0) {
235 236 237 238
            virReportSystemError(errno, "%s",
                                 _("failed to get cmt scaling factor"));
            goto error;
        }
239 240

        VIR_FREE(buf);
241 242
    }

243 244 245 246 247 248 249 250 251
    memset(&attr, 0, sizeof(attr));
    attr.size = sizeof(attr);
    attr.inherit = 1;
    attr.disabled = 1;
    attr.enable_on_exec = 0;
    attr.type = event_attr->attrType;
    attr.config = event_attr->attrConfig;

    event->fd = syscall(__NR_perf_event_open, &attr, pid, -1, -1, 0);
252 253
    if (event->fd < 0) {
        virReportSystemError(errno,
254
                             _("unable to open host cpu perf event for %s"),
255
                             virPerfEventTypeToString(type));
256
        goto error;
257 258 259
    }

    if (ioctl(event->fd, PERF_EVENT_IOC_ENABLE) < 0) {
260
        virReportSystemError(errno,
261
                             _("unable to enable host cpu perf event for %s"),
262
                             virPerfEventTypeToString(type));
263
        goto error;
264 265 266 267 268
    }

    event->enabled = true;
    return 0;

269
 error:
270 271 272 273 274 275 276 277 278
    VIR_FORCE_CLOSE(event->fd);
    VIR_FREE(buf);
    return -1;
}

int
virPerfEventDisable(virPerfPtr perf,
                    virPerfEventType type)
{
279
    virPerfEventPtr event = &(perf->events[type]);
280

281 282 283
    if (!event->enabled)
        return 0;

284 285
    if (ioctl(event->fd, PERF_EVENT_IOC_DISABLE) < 0) {
        virReportSystemError(errno,
286
                             _("unable to disable host cpu perf event for %s"),
287
                             virPerfEventTypeToString(type));
288 289 290 291 292 293 294 295 296 297 298
        return -1;
    }

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

bool virPerfEventIsEnabled(virPerfPtr perf,
                           virPerfEventType type)
{
299
    return perf && perf->events[type].enabled;
300 301 302 303 304 305 306
}

int
virPerfReadEvent(virPerfPtr perf,
                 virPerfEventType type,
                 uint64_t *value)
{
307 308
    virPerfEventPtr event = &perf->events[type];
    if (!event->enabled)
309 310
        return -1;

311
    if (saferead(event->fd, value, sizeof(uint64_t)) < 0) {
312 313 314 315 316 317 318 319 320 321 322 323
        virReportSystemError(errno, "%s",
                             _("Unable to read cache data"));
        return -1;
    }

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

    return 0;
}

#else
324 325 326 327 328 329 330
static int
virPerfRdtAttrInit(void)
{
    return 0;
}


331 332
int
virPerfEventEnable(virPerfPtr perf ATTRIBUTE_UNUSED,
R
Roman Bogorodskiy 已提交
333
                   virPerfEventType type ATTRIBUTE_UNUSED,
334 335 336 337 338 339 340 341 342
                   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 已提交
343
                    virPerfEventType type ATTRIBUTE_UNUSED)
344 345 346 347 348 349 350
{
    virReportSystemError(ENXIO, "%s",
                         _("Perf not supported on this platform"));
    return -1;
}

bool
R
Roman Bogorodskiy 已提交
351 352
virPerfEventIsEnabled(virPerfPtr perf ATTRIBUTE_UNUSED,
                      virPerfEventType type ATTRIBUTE_UNUSED)
353 354 355 356 357
{
    return false;
}

int
R
Roman Bogorodskiy 已提交
358 359 360
virPerfReadEvent(virPerfPtr perf ATTRIBUTE_UNUSED,
                 virPerfEventType type ATTRIBUTE_UNUSED,
                 uint64_t *value ATTRIBUTE_UNUSED)
361 362 363 364 365 366 367
{
    virReportSystemError(ENXIO, "%s",
                         _("Perf not supported on this platform"));
    return -1;
}

#endif
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

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].fd = -1;
        perf->events[i].enabled = false;
    }

383 384 385
    if (virPerfRdtAttrInit() < 0)
        virResetLastError();

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    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);
}