vircgroup.c 96.4 KB
Newer Older
1
/*
2
 * vircgroup.c: methods for managing control cgroups
3
 *
4
 * Copyright (C) 2010-2015 Red Hat, Inc.
5 6
 * Copyright IBM Corp. 2008
 *
O
Osier Yang 已提交
7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22
 */
#include <config.h>

23
#ifdef __linux__
24
# include <mntent.h>
25
# include <sys/mount.h>
26 27
# include <fcntl.h>
# include <sys/stat.h>
28
# include <sys/sysmacros.h>
29 30 31 32 33
# include <sys/types.h>
# include <signal.h>
# include <dirent.h>
# include <unistd.h>
#endif /* __linux__ */
34

35
#define LIBVIRT_VIRCGROUPPRIV_H_ALLOW
36 37
#include "vircgrouppriv.h"

38
#include "virutil.h"
39
#include "viralloc.h"
40
#include "vircgroupbackend.h"
41
#include "virerror.h"
42
#include "virlog.h"
E
Eric Blake 已提交
43
#include "virfile.h"
44
#include "virhash.h"
45
#include "virhashcode.h"
46
#include "virstring.h"
47
#include "virsystemd.h"
48
#include "virtypedparam.h"
49
#include "virhostcpu.h"
50
#include "virthread.h"
51

52 53
VIR_LOG_INIT("util.cgroup");

54 55
#define VIR_FROM_THIS VIR_FROM_CGROUP

56
#define CGROUP_NB_TOTAL_CPU_STAT_PARAM 3
57
#define CGROUP_NB_PER_CPU_STAT_PARAM   1
58

59 60
VIR_ENUM_IMPL(virCgroupController,
              VIR_CGROUP_CONTROLLER_LAST,
R
Ryota Ozaki 已提交
61
              "cpu", "cpuacct", "cpuset", "memory", "devices",
62
              "freezer", "blkio", "net_cls", "perf_event",
63 64
              "name=systemd",
);
65

E
Eric Blake 已提交
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/**
 * virCgroupGetDevicePermsString:
 *
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits
 *
 * Returns string corresponding to the appropriate bits set.
 */
const char *
virCgroupGetDevicePermsString(int perms)
{
    if (perms & VIR_CGROUP_DEVICE_READ) {
        if (perms & VIR_CGROUP_DEVICE_WRITE) {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "rwm";
            else
                return "rw";
        } else {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "rm";
            else
                return "r";
        }
    } else {
        if (perms & VIR_CGROUP_DEVICE_WRITE) {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "wm";
            else
                return "w";
        } else {
            if (perms & VIR_CGROUP_DEVICE_MKNOD)
                return "m";
            else
                return "";
        }
    }
}


105
#ifdef __linux__
E
Eric Blake 已提交
106 107
bool
virCgroupAvailable(void)
108
{
109 110
    size_t i;
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
111

112
    if (!backends)
113 114
        return false;

115 116 117
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (backends[i] && backends[i]->available())
            return true;
118 119
    }

120
    return false;
121 122
}

E
Eric Blake 已提交
123 124 125 126 127 128

static int
virCgroupPartitionNeedsEscaping(const char *path)
{
    FILE *fp = NULL;
    int ret = 0;
129
    g_autofree char *line = NULL;
E
Eric Blake 已提交
130 131 132 133 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 170
    size_t buflen;

    /* If it starts with 'cgroup.' or a '_' of any
     * of the controller names from /proc/cgroups,
     * then we must prefix a '_'
     */
    if (STRPREFIX(path, "cgroup."))
        return 1;

    if (path[0] == '_' ||
        path[0] == '.')
        return 1;

    if (!(fp = fopen("/proc/cgroups", "r"))) {
        /* The API contract is that we return ENXIO
         * if cgroups are not available on a host */
        if (errno == ENOENT)
            errno = ENXIO;
        virReportSystemError(errno, "%s",
                             _("Cannot open /proc/cgroups"));
        return -1;
    }

    /*
     * Data looks like this:
     * #subsys_name hierarchy num_cgroups enabled
     * cpuset  2 4  1
     * cpu     3 48 1
     * cpuacct 3 48 1
     * memory  4 4  1
     * devices 5 4  1
     * freezer 6 4  1
     * net_cls 7 1  1
     */
    while (getline(&line, &buflen, fp) > 0) {
        char *tmp;
        size_t len;

        if (STRPREFIX(line, "#subsys_name"))
            continue;

171 172 173 174 175 176 177
        tmp = strchr(line, ' ');
        if (tmp) {
            *tmp = '\0';
            len = tmp - line;
        } else {
            len = strlen(line);
        }
E
Eric Blake 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191

        if (STRPREFIX(path, line) &&
            path[len] == '.') {
            ret = 1;
            goto cleanup;
        }
    }

    if (ferror(fp)) {
        virReportSystemError(errno, "%s",
                             _("Error while reading /proc/cgroups"));
        goto cleanup;
    }

192
 cleanup:
E
Eric Blake 已提交
193 194 195 196 197
    VIR_FORCE_FCLOSE(fp);
    return ret;
}


198
int
E
Eric Blake 已提交
199 200 201
virCgroupPartitionEscape(char **path)
{
    int rc;
202
    char *newstr = NULL;
E
Eric Blake 已提交
203 204 205 206

    if ((rc = virCgroupPartitionNeedsEscaping(*path)) <= 0)
        return rc;

207
    newstr = g_strdup_printf("_%s", *path);
E
Eric Blake 已提交
208

209 210 211
    VIR_FREE(*path);
    *path = newstr;

E
Eric Blake 已提交
212 213
    return 0;
}
E
Eric Blake 已提交
214 215


216 217 218 219
/*
 * Process /proc/mounts figuring out what controllers are
 * mounted and where
 */
220
static int
221
virCgroupDetectMounts(virCgroupPtr group)
222
{
223
    FILE *mounts = NULL;
224 225
    struct mntent entry;
    char buf[CGROUP_MAX_VAL];
226
    int ret = -1;
227
    size_t i;
228

229
    mounts = fopen("/proc/mounts", "r");
230
    if (mounts == NULL) {
231
        virReportSystemError(errno, "%s", _("Unable to open /proc/mounts"));
232
        return -1;
233 234 235
    }

    while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
236 237 238 239 240 241 242 243
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->detectMounts(group,
                                                 entry.mnt_type,
                                                 entry.mnt_opts,
                                                 entry.mnt_dir) < 0) {
                goto cleanup;
            }
244
        }
245 246
    }

247 248
    ret = 0;
 cleanup:
249
    VIR_FORCE_FCLOSE(mounts);
250
    return ret;
251 252
}

253 254

/*
255 256 257 258
 * virCgroupDetectPlacement:
 * @group: the group to process
 * @path: the relative path to append, not starting with '/'
 *
259 260
 * Process /proc/self/cgroup figuring out what cgroup
 * sub-path the current process is assigned to. ie not
261 262 263 264 265 266 267 268 269 270 271 272 273 274
 * necessarily in the root. The contents of this file
 * looks like
 *
 * 9:perf_event:/
 * 8:blkio:/
 * 7:net_cls:/
 * 6:freezer:/
 * 5:devices:/
 * 4:memory:/
 * 3:cpuacct,cpu:/
 * 2:cpuset:/
 * 1:name=systemd:/user/berrange/2
 *
 * It then appends @path to each detected path.
275
 */
E
Eric Blake 已提交
276 277 278 279
static int
virCgroupDetectPlacement(virCgroupPtr group,
                         pid_t pid,
                         const char *path)
280
{
281 282
    FILE *mapping  = NULL;
    char line[1024];
283
    int ret = -1;
284
    g_autofree char *procfile = NULL;
285

286
    VIR_DEBUG("Detecting placement for pid %lld path %s",
M
Michal Privoznik 已提交
287
              (long long) pid, path);
288
    if (pid == -1) {
289
        procfile = g_strdup("/proc/self/cgroup");
290
    } else {
291
        procfile = g_strdup_printf("/proc/%lld/cgroup", (long long)pid);
292 293 294
    }

    mapping = fopen(procfile, "r");
295
    if (mapping == NULL) {
296 297 298 299
        virReportSystemError(errno,
                             _("Unable to open '%s'"),
                             procfile);
        goto cleanup;
300 301
    }

302
    while (fgets(line, sizeof(line), mapping) != NULL) {
303
        size_t i;
304
        char *controllers = strchr(line, ':');
305 306
        char *selfpath = controllers ? strchr(controllers + 1, ':') : NULL;
        char *nl = selfpath ? strchr(selfpath, '\n') : NULL;
307

308
        if (!controllers || !selfpath)
309 310 311 312 313
            continue;

        if (nl)
            *nl = '\0';

314
        *selfpath = '\0';
315
        controllers++;
316
        selfpath++;
317

318 319 320 321 322 323
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->detectPlacement(group, path, controllers,
                                                    selfpath) < 0) {
                goto cleanup;
            }
324 325 326
        }
    }

327
    ret = 0;
328

329
 cleanup:
330
    VIR_FORCE_FCLOSE(mapping);
331
    return ret;
332 333
}

E
Eric Blake 已提交
334

335 336 337 338 339 340 341
static int
virCgroupDetect(virCgroupPtr group,
                pid_t pid,
                int controllers,
                const char *path,
                virCgroupPtr parent)
{
342
    size_t i;
343 344
    bool backendAvailable = false;
    int controllersAvailable = 0;
345
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
346 347 348 349

    VIR_DEBUG("group=%p controllers=%d path=%s parent=%p",
              group, controllers, path, parent);

350 351 352 353 354
    if (!backends)
        return -1;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (backends[i] && backends[i]->available()) {
355 356
            group->backends[i] = backends[i];
            backendAvailable = true;
357 358 359
        }
    }

360
    if (!backendAvailable) {
361 362 363 364 365
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no cgroup backend available"));
        return -1;
    }

366
    if (parent) {
367 368 369 370 371 372
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->copyMounts(group, parent) < 0) {
                return -1;
            }
        }
373 374 375 376 377
    } else {
        if (virCgroupDetectMounts(group) < 0)
            return -1;
    }

378 379 380
    /* In some cases we can copy part of the placement info
     * based on the parent cgroup...
     */
381 382 383 384 385 386 387 388
    if (parent || path[0] == '/') {
        for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
            if (group->backends[i] &&
                group->backends[i]->copyPlacement(group, path, parent) < 0) {
                return -1;
            }
        }
    }
389 390 391 392

    /* ... but use /proc/cgroups to fill in the rest */
    if (virCgroupDetectPlacement(group, pid, path) < 0)
        return -1;
393

394
    /* Check that for every mounted controller, we found our placement */
395 396 397 398 399 400
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->validatePlacement(group, pid) < 0) {
            return -1;
        }
    }
401

402 403
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i]) {
404 405
            int rc = group->backends[i]->detectControllers(group, controllers, parent,
                                                           controllersAvailable);
406 407 408 409 410 411 412 413 414 415 416 417 418
            if (rc < 0)
                return -1;
            controllersAvailable |= rc;
        }
    }

    /* Check that at least 1 controller is available */
    if (controllersAvailable == 0) {
        virReportSystemError(ENXIO, "%s",
                             _("At least one cgroup controller is required"));
        return -1;
    }

419
    return 0;
420 421
}

422

423
char *
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
virCgroupGetBlockDevString(const char *path)
{
    char *ret = NULL;
    struct stat sb;

    if (stat(path, &sb) < 0) {
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return NULL;
    }

    if (!S_ISBLK(sb.st_mode)) {
        virReportSystemError(EINVAL,
                             _("Path '%s' must be a block device"),
                             path);
        return NULL;
    }

    /* Automatically append space after the string since all callers
     * use it anyway */
445
    ret = g_strdup_printf("%d:%d ", major(sb.st_rdev), minor(sb.st_rdev));
446 447 448 449 450

    return ret;
}


451
int
452
virCgroupSetValueRaw(const char *path,
E
Eric Blake 已提交
453
                     const char *value)
454
{
455
    char *tmp;
456

457 458
    VIR_DEBUG("Set value '%s' to '%s'", path, value);
    if (virFileWriteStr(path, value, 0) < 0) {
459
        if (errno == EINVAL &&
460
            (tmp = strrchr(path, '/'))) {
461 462 463
            virReportSystemError(errno,
                                 _("Invalid value '%s' for '%s'"),
                                 value, tmp + 1);
464
            return -1;
465
        }
466
        virReportSystemError(errno,
467
                             _("Unable to write to '%s'"), path);
468
        return -1;
469 470
    }

471
    return 0;
472 473
}

E
Eric Blake 已提交
474

475
int
476
virCgroupGetValueRaw(const char *path,
E
Eric Blake 已提交
477
                     char **value)
478
{
479
    int rc;
480

481
    *value = NULL;
482

483
    VIR_DEBUG("Get value %s", path);
484

485
    if ((rc = virFileReadAll(path, 1024*1024, value)) < 0) {
486
        virReportSystemError(errno,
487
                             _("Unable to read from '%s'"), path);
488
        return -1;
489 490
    }

491 492 493
    /* Terminated with '\n' has sometimes harmful effects to the caller */
    if (rc > 0 && (*value)[rc - 1] == '\n')
        (*value)[rc - 1] = '\0';
494

495
    return 0;
496 497
}

E
Eric Blake 已提交
498

499 500 501 502 503 504
int
virCgroupSetValueStr(virCgroupPtr group,
                     int controller,
                     const char *key,
                     const char *value)
{
505
    g_autofree char *keypath = NULL;
506 507 508 509 510 511 512 513 514 515 516 517 518 519

    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
        return -1;

    return virCgroupSetValueRaw(keypath, value);
}


int
virCgroupGetValueStr(virCgroupPtr group,
                     int controller,
                     const char *key,
                     char **value)
{
520
    g_autofree char *keypath = NULL;
521 522 523 524 525 526 527 528

    if (virCgroupPathOfController(group, controller, key, &keypath) < 0)
        return -1;

    return virCgroupGetValueRaw(keypath, value);
}


529
int
530
virCgroupGetValueForBlkDev(const char *str,
531 532 533
                           const char *path,
                           char **value)
{
534
    g_autofree char *prefix = NULL;
535 536
    char **lines = NULL;
    int ret = -1;
537 538

    if (!(prefix = virCgroupGetBlockDevString(path)))
539
        goto error;
540 541

    if (!(lines = virStringSplit(str, "\n", -1)))
542
        goto error;
543

544
    *value = g_strdup(virStringListGetFirstWithPrefix(lines, prefix));
545

546 547 548 549
    ret = 0;
 error:
    virStringListFree(lines);
    return ret;
550 551 552
}


553
int
E
Eric Blake 已提交
554 555 556 557
virCgroupSetValueU64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     unsigned long long int value)
558
{
559
    g_autofree char *strval = NULL;
560

561
    strval = g_strdup_printf("%llu", value);
562

563
    return virCgroupSetValueStr(group, controller, key, strval);
564 565 566
}


567
int
E
Eric Blake 已提交
568 569 570 571
virCgroupSetValueI64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     long long int value)
572
{
573
    g_autofree char *strval = NULL;
574

575
    strval = g_strdup_printf("%lld", value);
576

577
    return virCgroupSetValueStr(group, controller, key, strval);
578 579
}

E
Eric Blake 已提交
580

581
int
E
Eric Blake 已提交
582 583 584 585
virCgroupGetValueI64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     long long int *value)
586
{
587
    g_autofree char *strval = NULL;
588

589
    if (virCgroupGetValueStr(group, controller, key, &strval) < 0)
590
        return -1;
591

592 593 594 595
    if (virStrToLong_ll(strval, NULL, 10, value) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse '%s' as an integer"),
                       strval);
596
        return -1;
597
    }
598

599
    return 0;
600 601
}

E
Eric Blake 已提交
602

603
int
E
Eric Blake 已提交
604 605 606 607
virCgroupGetValueU64(virCgroupPtr group,
                     int controller,
                     const char *key,
                     unsigned long long int *value)
608
{
609
    g_autofree char *strval = NULL;
610

611
    if (virCgroupGetValueStr(group, controller, key, &strval) < 0)
612
        return -1;
613

614 615 616 617
    if (virStrToLong_ull(strval, NULL, 10, value) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to parse '%s' as an integer"),
                       strval);
618
        return -1;
619
    }
620

621
    return 0;
622 623 624
}


E
Eric Blake 已提交
625 626 627 628 629
static int
virCgroupMakeGroup(virCgroupPtr parent,
                   virCgroupPtr group,
                   bool create,
                   unsigned int flags)
630
{
631 632 633 634 635 636 637 638
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->makeGroup(parent, group, create, flags) < 0) {
            virCgroupRemove(group);
            return -1;
        }
639 640
    }

641
    return 0;
642 643
}

644

645 646 647 648 649 650 651 652 653 654 655 656 657 658
/**
 * virCgroupNew:
 * @path: path for the new group
 * @parent: parent group, or NULL
 * @controllers: bitmask of controllers to activate
 *
 * Create a new cgroup storing it in @group.
 *
 * If @path starts with a '/' it is treated as an
 * absolute path, and @parent is ignored. Otherwise
 * it is treated as being relative to @parent. If
 * @parent is NULL, then the placement of the current
 * process is used.
 *
659
 * Returns 0 on success, -1 on error
660
 */
661
int
E
Eric Blake 已提交
662 663 664 665 666
virCgroupNew(pid_t pid,
             const char *path,
             virCgroupPtr parent,
             int controllers,
             virCgroupPtr *group)
667
{
668 669
    VIR_DEBUG("pid=%lld path=%s parent=%p controllers=%d group=%p",
              (long long) pid, path, parent, controllers, group);
670
    *group = NULL;
671

672 673
    if (VIR_ALLOC((*group)) < 0)
        goto error;
674

675
    if (path[0] == '/' || !parent) {
676
        (*group)->path = g_strdup(path);
677
    } else {
678 679
        (*group)->path = g_strdup_printf("%s%s%s", parent->path,
                                         STREQ(parent->path, "") ? "" : "/", path);
680 681
    }

682
    if (virCgroupDetect(*group, pid, controllers, path, parent) < 0)
683
        goto error;
684

685 686
    return 0;

687
 error:
688
    virCgroupFree(group);
689
    *group = NULL;
690

691
    return -1;
692
}
693

694

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
static int
virCgroupAddTaskInternal(virCgroupPtr group,
                         pid_t pid,
                         unsigned int flags)
{
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->addTask(group, pid, flags) < 0) {
            return -1;
        }
    }

    return 0;
}


713
/**
714
 * virCgroupAddProcess:
715
 *
716 717
 * @group: The cgroup to add a process to
 * @pid: The pid of the process to add
718
 *
719
 * Will add the process to all controllers, except the
720 721 722 723 724
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
725
virCgroupAddProcess(virCgroupPtr group, pid_t pid)
726
{
727
    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_PROCESS);
728 729 730
}

/**
731
 * virCgroupAddMachineProcess:
732
 *
733 734
 * @group: The cgroup to add a process to
 * @pid: The pid of the process to add
735
 *
736
 * Will add the process to all controllers, including the
737 738 739 740 741
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
742
virCgroupAddMachineProcess(virCgroupPtr group, pid_t pid)
743
{
744 745 746
    return virCgroupAddTaskInternal(group, pid,
                                    VIR_CGROUP_TASK_PROCESS |
                                    VIR_CGROUP_TASK_SYSTEMD);
747 748
}

749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
/**
 * virCgroupAddThread:
 *
 * @group: The cgroup to add a thread to
 * @pid: The pid of the thread to add
 *
 * Will add the thread to all controllers, except the
 * systemd unit controller.
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupAddThread(virCgroupPtr group,
                   pid_t pid)
{
764
    return virCgroupAddTaskInternal(group, pid, VIR_CGROUP_TASK_THREAD);
765 766
}

E
Eric Blake 已提交
767 768 769

static int
virCgroupSetPartitionSuffix(const char *path, char **res)
770
{
771
    char **tokens;
772
    size_t i;
773
    int ret = -1;
774

775
    if (!(tokens = virStringSplit(path, "/", 0)))
776
        return ret;
777

778
    for (i = 0; tokens[i] != NULL; i++) {
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
        /* Whitelist the 3 top level fixed dirs
         * NB i == 0 is "", since we have leading '/'
         */
        if (i == 1 &&
            (STREQ(tokens[i], "machine") ||
             STREQ(tokens[i], "system") ||
             STREQ(tokens[i], "user"))) {
            continue;
        }
        /* If there is no suffix set already, then
         * add ".partition"
         */
        if (STRNEQ(tokens[i], "") &&
            !strchr(tokens[i], '.')) {
            if (VIR_REALLOC_N(tokens[i],
794
                              strlen(tokens[i]) + strlen(".partition") + 1) < 0)
795
                goto cleanup;
796 797
            strcat(tokens[i], ".partition");
        }
798

799
        if (virCgroupPartitionEscape(&(tokens[i])) < 0)
800
            goto cleanup;
801 802
    }

803
    if (!(*res = virStringListJoin((const char **)tokens, "/")))
804
        goto cleanup;
805

806 807 808 809 810
    ret = 0;

 cleanup:
    virStringListFree(tokens);
    return ret;
811 812
}

E
Eric Blake 已提交
813

814 815 816 817 818 819 820
/**
 * virCgroupNewPartition:
 * @path: path for the partition
 * @create: true to create the cgroup tree
 * @controllers: mask of controllers to create
 *
 * Creates a new cgroup to represent the resource
821
 * partition path identified by @path.
822
 *
823
 * Returns 0 on success, -1 on failure
824
 */
E
Eric Blake 已提交
825 826 827 828 829
int
virCgroupNewPartition(const char *path,
                      bool create,
                      int controllers,
                      virCgroupPtr *group)
830
{
831
    int ret = -1;
832 833
    g_autofree char *parentPath = NULL;
    g_autofree char *newPath = NULL;
834
    virCgroupPtr parent = NULL;
835 836 837
    VIR_DEBUG("path=%s create=%d controllers=%x",
              path, create, controllers);

838 839 840 841 842 843
    if (path[0] != '/') {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Partition path '%s' must start with '/'"),
                       path);
        return -1;
    }
844

845
    if (virCgroupSetPartitionSuffix(path, &newPath) < 0)
846
        goto cleanup;
847

848
    if (STRNEQ(newPath, "/")) {
849
        char *tmp;
850
        parentPath = g_strdup(newPath);
851 852 853 854 855

        tmp = strrchr(parentPath, '/');
        tmp++;
        *tmp = '\0';

856
        if (virCgroupNew(-1, parentPath, NULL, controllers, &parent) < 0)
857
            goto cleanup;
858
    }
859

860 861 862 863
    if (virCgroupNew(-1, newPath, parent, controllers, group) < 0)
        goto cleanup;

    if (parent) {
864
        if (virCgroupMakeGroup(parent, *group, create, VIR_CGROUP_NONE) < 0)
865
            goto cleanup;
866 867
    }

868 869 870
    ret = 0;
 cleanup:
    if (ret != 0)
871 872
        virCgroupFree(group);
    virCgroupFree(&parent);
873
    return ret;
874 875
}

876

G
Gao feng 已提交
877
/**
878
* virCgroupNewSelf:
G
Gao feng 已提交
879 880 881
*
* @group: Pointer to returned virCgroupPtr
*
882 883 884
* Obtain a cgroup representing the config of the
* current process
*
885
* Returns 0 on success, or -1 on error
G
Gao feng 已提交
886
*/
E
Eric Blake 已提交
887 888
int
virCgroupNewSelf(virCgroupPtr *group)
G
Gao feng 已提交
889
{
890
    return virCgroupNewDetect(-1, -1, group);
G
Gao feng 已提交
891
}
892

893

894 895 896 897 898 899 900 901
/**
 * virCgroupNewDomainPartition:
 *
 * @partition: partition holding the domain
 * @driver: name of the driver
 * @name: name of the domain
 * @group: Pointer to returned virCgroupPtr
 *
902
 * Returns 0 on success, or -1 on error
903
 */
E
Eric Blake 已提交
904 905 906 907 908 909
int
virCgroupNewDomainPartition(virCgroupPtr partition,
                            const char *driver,
                            const char *name,
                            bool create,
                            virCgroupPtr *group)
910
{
911
    g_autofree char *grpname = NULL;
912

913
    grpname = g_strdup_printf("%s.libvirt-%s", name, driver);
914

915
    if (virCgroupPartitionEscape(&grpname) < 0)
916
        return -1;
917

918
    if (virCgroupNew(-1, grpname, partition, -1, group) < 0)
919
        return -1;
920 921 922 923 924 925 926 927 928 929 930

    /*
     * Create a cgroup with memory.use_hierarchy enabled to
     * surely account memory usage of lxc with ns subsystem
     * enabled. (To be exact, memory and ns subsystems are
     * enabled at the same time.)
     *
     * The reason why doing it here, not a upper group, say
     * a group for driver, is to avoid overhead to track
     * cumulative usage that we don't need.
     */
E
Eric Blake 已提交
931 932
    if (virCgroupMakeGroup(partition, *group, create,
                           VIR_CGROUP_MEM_HIERACHY) < 0) {
933
        virCgroupFree(group);
934
        return -1;
935 936
    }

937
    return 0;
938
}
939

E
Eric Blake 已提交
940

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
/**
 * virCgroupNewThread:
 *
 * @domain: group for the domain
 * @name: enum to generate the name for the new thread
 * @id: id of the vcpu or iothread
 * @create: true to create if not already existing
 * @group: Pointer to returned virCgroupPtr
 *
 * Returns 0 on success, or -1 on error
 */
int
virCgroupNewThread(virCgroupPtr domain,
                   virCgroupThreadName nameval,
                   int id,
                   bool create,
                   virCgroupPtr *group)
{
959
    g_autofree char *name = NULL;
960 961 962 963
    int controllers;

    switch (nameval) {
    case VIR_CGROUP_THREAD_VCPU:
964
        name = g_strdup_printf("vcpu%d", id);
965 966
        break;
    case VIR_CGROUP_THREAD_EMULATOR:
967
        name = g_strdup("emulator");
968 969
        break;
    case VIR_CGROUP_THREAD_IOTHREAD:
970
        name = g_strdup_printf("iothread%d", id);
971 972 973 974
        break;
    case VIR_CGROUP_THREAD_LAST:
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("unexpected name value %d"), nameval);
975
        return -1;
976 977 978 979 980 981 982
    }

    controllers = ((1 << VIR_CGROUP_CONTROLLER_CPU) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                   (1 << VIR_CGROUP_CONTROLLER_CPUSET));

    if (virCgroupNew(-1, name, domain, controllers, group) < 0)
983
        return -1;
984

985
    if (virCgroupMakeGroup(domain, *group, create, VIR_CGROUP_THREAD) < 0) {
986
        virCgroupFree(group);
987
        return -1;
988 989
    }

990
    return 0;
991 992 993
}


E
Eric Blake 已提交
994 995 996 997
int
virCgroupNewDetect(pid_t pid,
                   int controllers,
                   virCgroupPtr *group)
998
{
999
    return virCgroupNew(pid, "", NULL, controllers, group);
1000 1001
}

E
Eric Blake 已提交
1002

1003 1004 1005
/*
 * Returns 0 on success (but @group may be NULL), -1 on fatal error
 */
E
Eric Blake 已提交
1006 1007 1008 1009 1010
int
virCgroupNewDetectMachine(const char *name,
                          const char *drivername,
                          pid_t pid,
                          int controllers,
1011
                          char *machinename,
E
Eric Blake 已提交
1012
                          virCgroupPtr *group)
1013
{
1014 1015
    size_t i;

1016
    if (virCgroupNewDetect(pid, controllers, group) < 0) {
1017 1018 1019 1020 1021
        if (virCgroupNewIgnoreError())
            return 0;
        return -1;
    }

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if ((*group)->backends[i] &&
            !(*group)->backends[i]->validateMachineGroup(*group, name,
                                                         drivername,
                                                         machinename)) {
            VIR_DEBUG("Failed to validate machine name for '%s' driver '%s'",
                      name, drivername);
            virCgroupFree(group);
            return 0;
        }
1032 1033 1034 1035 1036
    }

    return 0;
}

E
Eric Blake 已提交
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
static int
virCgroupEnableMissingControllers(char *path,
                                  pid_t pidleader,
                                  int controllers,
                                  virCgroupPtr *group)
{
    virCgroupPtr parent = NULL;
    char *offset = path;
    int ret = -1;

    if (virCgroupNew(pidleader,
1049
                     "/",
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
                     NULL,
                     controllers,
                     &parent) < 0)
        return ret;

    for (;;) {
        virCgroupPtr tmp;
        char *t = strchr(offset + 1, '/');
        if (t)
            *t = '\0';

        if (virCgroupNew(pidleader,
                         path,
                         parent,
                         controllers,
                         &tmp) < 0)
            goto cleanup;

1068
        if (virCgroupMakeGroup(parent, tmp, true, VIR_CGROUP_SYSTEMD) < 0) {
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
            virCgroupFree(&tmp);
            goto cleanup;
        }
        if (t) {
            *t = '/';
            offset = t;
            virCgroupFree(&parent);
            parent = tmp;
        } else {
            *group = tmp;
            break;
        }
    }

    ret = 0;
 cleanup:
    virCgroupFree(&parent);
    return ret;
}


1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
/*
 * Returns 0 on success, -1 on fatal error, -2 on systemd not available
 */
static int
virCgroupNewMachineSystemd(const char *name,
                           const char *drivername,
                           const unsigned char *uuid,
                           const char *rootdir,
                           pid_t pidleader,
                           bool isContainer,
1100 1101
                           size_t nnicindexes,
                           int *nicindexes,
1102 1103
                           const char *partition,
                           int controllers,
1104
                           unsigned int maxthreads,
1105
                           virCgroupPtr *group)
1106
{
1107
    int rv;
1108
    virCgroupPtr init;
1109
    g_autofree char *path = NULL;
1110
    size_t i;
1111 1112 1113 1114 1115 1116 1117 1118

    VIR_DEBUG("Trying to setup machine '%s' via systemd", name);
    if ((rv = virSystemdCreateMachine(name,
                                      drivername,
                                      uuid,
                                      rootdir,
                                      pidleader,
                                      isContainer,
1119 1120
                                      nnicindexes,
                                      nicindexes,
1121 1122
                                      partition,
                                      maxthreads)) < 0)
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
        return rv;

    if (controllers != -1)
        controllers |= (1 << VIR_CGROUP_CONTROLLER_SYSTEMD);

    VIR_DEBUG("Detecting systemd placement");
    if (virCgroupNewDetect(pidleader,
                           controllers,
                           &init) < 0)
        return -1;
1133

1134 1135 1136 1137 1138 1139
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (init->backends[i] &&
            (path = init->backends[i]->stealPlacement(init))) {
            break;
        }
    }
1140
    virCgroupFree(&init);
1141 1142

    if (!path || STREQ(path, "/") || path[0] != '/') {
1143 1144
        VIR_DEBUG("Systemd didn't setup its controller, path=%s",
                  NULLSTR(path));
1145
        return -2;
1146 1147
    }

1148 1149
    if (virCgroupEnableMissingControllers(path, pidleader,
                                          controllers, group) < 0) {
1150
        return -1;
1151 1152
    }

1153
    if (virCgroupAddProcess(*group, pidleader) < 0) {
1154 1155 1156
        virErrorPtr saved;

        virErrorPreserveLast(&saved);
1157 1158
        virCgroupRemove(*group);
        virCgroupFree(group);
1159
        virErrorRestore(&saved);
1160 1161
    }

1162
    return 0;
1163
}
1164

E
Eric Blake 已提交
1165

1166 1167 1168
/*
 * Returns 0 on success, -1 on fatal error
 */
1169
int virCgroupTerminateMachine(const char *name)
1170
{
1171
    return virSystemdTerminateMachine(name);
1172 1173 1174
}


1175 1176 1177
static int
virCgroupNewMachineManual(const char *name,
                          const char *drivername,
1178
                          pid_t pidleader,
1179 1180 1181 1182
                          const char *partition,
                          int controllers,
                          virCgroupPtr *group)
{
1183 1184
    virCgroupPtr parent = NULL;
    int ret = -1;
1185 1186

    VIR_DEBUG("Fallback to non-systemd setup");
1187 1188 1189 1190 1191
    if (virCgroupNewPartition(partition,
                              STREQ(partition, "/machine"),
                              controllers,
                              &parent) < 0) {
        if (virCgroupNewIgnoreError())
1192
            goto done;
1193

1194
        goto cleanup;
1195 1196 1197 1198 1199 1200 1201
    }

    if (virCgroupNewDomainPartition(parent,
                                    drivername,
                                    name,
                                    true,
                                    group) < 0)
1202
        goto cleanup;
1203

1204
    if (virCgroupAddProcess(*group, pidleader) < 0) {
1205 1206 1207
        virErrorPtr saved;

        virErrorPreserveLast(&saved);
1208
        virCgroupRemove(*group);
1209
        virCgroupFree(group);
1210
        virErrorRestore(&saved);
1211 1212
    }

1213 1214 1215 1216
 done:
    ret = 0;

 cleanup:
1217
    virCgroupFree(&parent);
1218
    return ret;
1219 1220
}

E
Eric Blake 已提交
1221 1222 1223 1224 1225 1226 1227 1228

int
virCgroupNewMachine(const char *name,
                    const char *drivername,
                    const unsigned char *uuid,
                    const char *rootdir,
                    pid_t pidleader,
                    bool isContainer,
1229 1230
                    size_t nnicindexes,
                    int *nicindexes,
E
Eric Blake 已提交
1231 1232
                    const char *partition,
                    int controllers,
1233
                    unsigned int maxthreads,
E
Eric Blake 已提交
1234
                    virCgroupPtr *group)
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
{
    int rv;

    *group = NULL;

    if ((rv = virCgroupNewMachineSystemd(name,
                                         drivername,
                                         uuid,
                                         rootdir,
                                         pidleader,
                                         isContainer,
1246 1247
                                         nnicindexes,
                                         nicindexes,
1248 1249
                                         partition,
                                         controllers,
1250
                                         maxthreads,
1251 1252 1253 1254 1255 1256 1257 1258
                                         group)) == 0)
        return 0;

    if (rv == -1)
        return -1;

    return virCgroupNewMachineManual(name,
                                     drivername,
1259
                                     pidleader,
1260 1261 1262 1263 1264
                                     partition,
                                     controllers,
                                     group);
}

E
Eric Blake 已提交
1265 1266 1267

bool
virCgroupNewIgnoreError(void)
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
{
    if (virLastErrorIsSystemErrno(ENXIO) ||
        virLastErrorIsSystemErrno(EPERM) ||
        virLastErrorIsSystemErrno(EACCES)) {
        virResetLastError();
        VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
        return true;
    }
    return false;
}

E
Eric Blake 已提交
1279

E
Eric Blake 已提交
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
/**
 * virCgroupHasController: query whether a cgroup controller is present
 *
 * @cgroup: The group structure to be queried, or NULL
 * @controller: cgroup subsystem id
 *
 * Returns true if a cgroup controller is mounted and is associated
 * with this cgroup object.
 */
bool
virCgroupHasController(virCgroupPtr cgroup, int controller)
{
1292 1293
    size_t i;

E
Eric Blake 已提交
1294 1295 1296 1297
    if (!cgroup)
        return false;
    if (controller < 0 || controller >= VIR_CGROUP_CONTROLLER_LAST)
        return false;
1298

1299 1300 1301 1302 1303 1304 1305 1306
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i] &&
            cgroup->backends[i]->hasController(cgroup, controller)) {
            return true;
        }
    }

    return false;
E
Eric Blake 已提交
1307 1308 1309 1310 1311
}


int
virCgroupPathOfController(virCgroupPtr group,
1312
                          unsigned int controller,
E
Eric Blake 已提交
1313 1314 1315
                          const char *key,
                          char **path)
{
1316 1317 1318
    if (controller >= VIR_CGROUP_CONTROLLER_LAST) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Invalid controller id '%d'"), controller);
E
Eric Blake 已提交
1319 1320 1321
        return -1;
    }

1322 1323
    VIR_CGROUP_BACKEND_CALL(group, controller, pathOfController, -1,
                            controller, key, path);
E
Eric Blake 已提交
1324 1325 1326
}


1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
/**
 * virCgroupGetBlkioIoServiced:
 *
 * @group: The cgroup to get throughput for
 * @bytes_read: Pointer to returned bytes read
 * @bytes_write: Pointer to returned bytes written
 * @requests_read: Pointer to returned read io ops
 * @requests_write: Pointer to returned write io ops
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioIoServiced(virCgroupPtr group,
                            long long *bytes_read,
                            long long *bytes_write,
                            long long *requests_read,
                            long long *requests_write)
{
1345 1346
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioIoServiced, -1,
1347 1348
                            bytes_read, bytes_write,
                            requests_read, requests_write);
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
}


/**
 * virCgroupGetBlkioIoDeviceServiced:
 *
 * @group: The cgroup to get throughput for
 * @path: The device to get throughput for
 * @bytes_read: Pointer to returned bytes read
 * @bytes_write: Pointer to returned bytes written
 * @requests_read: Pointer to returned read io ops
 * @requests_write: Pointer to returned write io ops
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group,
                                  const char *path,
                                  long long *bytes_read,
                                  long long *bytes_write,
                                  long long *requests_read,
                                  long long *requests_write)
{
1372 1373
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioIoDeviceServiced, -1,
1374 1375
                            path, bytes_read, bytes_write,
                            requests_read, requests_write);
1376 1377 1378
}


1379 1380 1381 1382 1383 1384
/**
 * virCgroupSetBlkioWeight:
 *
 * @group: The cgroup to change io weight for
 * @weight: The Weight for this cgroup
 *
1385
 * Returns: 0 on success, -1 on error
1386
 */
E
Eric Blake 已提交
1387 1388
int
virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight)
1389
{
1390 1391
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioWeight, -1, weight);
1392 1393
}

E
Eric Blake 已提交
1394

1395 1396 1397 1398 1399 1400
/**
 * virCgroupGetBlkioWeight:
 *
 * @group: The cgroup to get weight for
 * @Weight: Pointer to returned weight
 *
1401
 * Returns: 0 on success, -1 on error
1402
 */
E
Eric Blake 已提交
1403 1404
int
virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight)
1405
{
1406 1407
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioWeight, -1, weight);
1408 1409
}

1410 1411 1412 1413 1414 1415 1416 1417
/**
 * virCgroupSetBlkioDeviceReadIops:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @riops: The new device read iops throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
1418
static int
1419 1420 1421 1422
virCgroupSetBlkioDeviceReadIops(virCgroupPtr group,
                                const char *path,
                                unsigned int riops)
{
1423 1424
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceReadIops, -1, path, riops);
1425 1426
}

E
Eric Blake 已提交
1427

1428
/**
1429 1430 1431 1432 1433 1434 1435
 * virCgroupSetBlkioDeviceWriteIops:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @wiops: The new device write iops throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
1436
static int
1437 1438 1439 1440
virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group,
                                 const char *path,
                                 unsigned int wiops)
{
1441 1442
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWriteIops, -1, path, wiops);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
}


/**
 * virCgroupSetBlkioDeviceReadBps:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @rbps: The new device read bps throttle, or 0 to clear
 *
 * Returns: 0 on success, -1 on error
 */
1454
static int
1455 1456 1457 1458
virCgroupSetBlkioDeviceReadBps(virCgroupPtr group,
                               const char *path,
                               unsigned long long rbps)
{
1459 1460
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceReadBps, -1, path, rbps);
1461 1462 1463 1464 1465 1466 1467
}

/**
 * virCgroupSetBlkioDeviceWriteBps:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
 * @wbps: The new device write bps throttle, or 0 to clear
1468
 *
1469 1470
 * Returns: 0 on success, -1 on error
 */
1471
static int
1472 1473 1474 1475
virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group,
                                const char *path,
                                unsigned long long wbps)
{
1476 1477
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWriteBps, -1, path, wbps);
1478 1479 1480 1481 1482 1483 1484
}


/**
 * virCgroupSetBlkioDeviceWeight:
 * @group: The cgroup to change block io setting for
 * @path: The path of device
1485 1486
 * @weight: The new device weight (100-1000),
 * (10-1000) after kernel 2.6.39, or 0 to clear
1487
 *
1488
 * Returns: 0 on success, -1 on error
1489
 */
1490
static int
E
Eric Blake 已提交
1491 1492 1493
virCgroupSetBlkioDeviceWeight(virCgroupPtr group,
                              const char *path,
                              unsigned int weight)
1494
{
1495 1496
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            setBlkioDeviceWeight, -1, path, weight);
1497
}
1498

1499 1500 1501 1502 1503 1504 1505 1506
/**
 * virCgroupGetBlkioDeviceReadIops:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @riops: Returned device read iops throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
1507
static int
1508 1509 1510 1511
virCgroupGetBlkioDeviceReadIops(virCgroupPtr group,
                                const char *path,
                                unsigned int *riops)
{
1512 1513
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceReadIops, -1, path, riops);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
}

/**
 * virCgroupGetBlkioDeviceWriteIops:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @wiops: Returned device write iops throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
1524
static int
1525 1526 1527 1528
virCgroupGetBlkioDeviceWriteIops(virCgroupPtr group,
                                 const char *path,
                                 unsigned int *wiops)
{
1529 1530
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWriteIops, -1, path, wiops);
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
}

/**
 * virCgroupGetBlkioDeviceReadBps:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @rbps: Returned device read bps throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
1541
static int
1542 1543 1544 1545
virCgroupGetBlkioDeviceReadBps(virCgroupPtr group,
                               const char *path,
                               unsigned long long *rbps)
{
1546 1547
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceReadBps, -1, path, rbps);
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
}

/**
 * virCgroupGetBlkioDeviceWriteBps:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @wbps: Returned device write bps throttle, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
1558
static int
1559 1560 1561 1562
virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group,
                                const char *path,
                                unsigned long long *wbps)
{
1563 1564
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWriteBps, -1, path, wbps);
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
}

/**
 * virCgroupGetBlkioDeviceWeight:
 * @group: The cgroup to gather block io setting for
 * @path: The path of device
 * @weight: Returned device weight, 0 if there is none
 *
 * Returns: 0 on success, -1 on error
 */
1575
static int
1576 1577 1578 1579
virCgroupGetBlkioDeviceWeight(virCgroupPtr group,
                              const char *path,
                              unsigned int *weight)
{
1580 1581
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_BLKIO,
                            getBlkioDeviceWeight, -1, path, weight);
1582 1583
}

1584

1585 1586 1587 1588 1589 1590 1591 1592
/**
 * virCgroupSetMemory:
 *
 * @group: The cgroup to change memory for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1593 1594
int
virCgroupSetMemory(virCgroupPtr group, unsigned long long kb)
1595
{
1596 1597
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemory, -1, kb);
1598 1599
}

E
Eric Blake 已提交
1600

1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
/**
 * virCgroupGetMemoryStat:
 *
 * @group: The cgroup to change memory for
 * @cache: page cache memory in KiB
 * @activeAnon: anonymous and swap cache memory in KiB
 * @inactiveAnon: anonymous and swap cache memory in KiB
 * @activeFile: file-backed memory in KiB
 * @inactiveFile: file-backed memory in KiB
 * @unevictable: memory that cannot be reclaimed KiB
 *
 * Returns: 0 on success, -1 on error
 */
int
virCgroupGetMemoryStat(virCgroupPtr group,
                       unsigned long long *cache,
                       unsigned long long *activeAnon,
                       unsigned long long *inactiveAnon,
                       unsigned long long *activeFile,
                       unsigned long long *inactiveFile,
                       unsigned long long *unevictable)
{
1623 1624
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryStat, -1, cache,
1625 1626 1627
                            activeAnon, inactiveAnon,
                            activeFile, inactiveFile,
                            unevictable);
1628 1629 1630
}


R
Ryota Ozaki 已提交
1631 1632 1633 1634 1635 1636 1637 1638
/**
 * virCgroupGetMemoryUsage:
 *
 * @group: The cgroup to change memory for
 * @kb: Pointer to returned used memory in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1639 1640
int
virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb)
R
Ryota Ozaki 已提交
1641
{
1642 1643
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryUsage, -1, kb);
R
Ryota Ozaki 已提交
1644 1645
}

E
Eric Blake 已提交
1646

1647 1648 1649 1650 1651 1652 1653 1654
/**
 * virCgroupSetMemoryHardLimit:
 *
 * @group: The cgroup to change memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1655 1656
int
virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb)
1657
{
1658 1659
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemoryHardLimit, -1, kb);
1660 1661
}

E
Eric Blake 已提交
1662

1663 1664 1665 1666 1667 1668 1669 1670
/**
 * virCgroupGetMemoryHardLimit:
 *
 * @group: The cgroup to get the memory hard limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1671 1672
int
virCgroupGetMemoryHardLimit(virCgroupPtr group, unsigned long long *kb)
1673
{
1674 1675
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemoryHardLimit, -1, kb);
1676 1677
}

E
Eric Blake 已提交
1678

1679 1680 1681 1682 1683 1684 1685 1686
/**
 * virCgroupSetMemorySoftLimit:
 *
 * @group: The cgroup to change memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1687 1688
int
virCgroupSetMemorySoftLimit(virCgroupPtr group, unsigned long long kb)
1689
{
1690 1691
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemorySoftLimit, -1, kb);
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
}


/**
 * virCgroupGetMemorySoftLimit:
 *
 * @group: The cgroup to get the memory soft limit for
 * @kb: The memory amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1703 1704
int
virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long *kb)
1705
{
1706 1707
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemorySoftLimit, -1, kb);
1708 1709
}

E
Eric Blake 已提交
1710

1711
/**
1712
 * virCgroupSetMemSwapHardLimit:
1713
 *
1714 1715
 * @group: The cgroup to change mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1716 1717 1718
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1719 1720
int
virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb)
1721
{
1722 1723
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            setMemSwapHardLimit, -1, kb);
1724 1725
}

E
Eric Blake 已提交
1726

1727
/**
1728
 * virCgroupGetMemSwapHardLimit:
1729
 *
1730 1731
 * @group: The cgroup to get mem+swap hard limit for
 * @kb: The mem+swap amount in kilobytes
1732 1733 1734
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1735 1736
int
virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb)
1737
{
1738 1739
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemSwapHardLimit, -1, kb);
1740 1741
}

E
Eric Blake 已提交
1742

G
Gao feng 已提交
1743 1744 1745 1746 1747 1748 1749 1750
/**
 * virCgroupGetMemSwapUsage:
 *
 * @group: The cgroup to get mem+swap usage for
 * @kb: The mem+swap amount in kilobytes
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1751 1752
int
virCgroupGetMemSwapUsage(virCgroupPtr group, unsigned long long *kb)
G
Gao feng 已提交
1753
{
1754 1755
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
                            getMemSwapUsage, -1, kb);
G
Gao feng 已提交
1756 1757
}

E
Eric Blake 已提交
1758

1759 1760 1761 1762 1763 1764 1765 1766
/**
 * virCgroupSetCpusetMems:
 *
 * @group: The cgroup to set cpuset.mems for
 * @mems: the numa nodes to set
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1767 1768
int
virCgroupSetCpusetMems(virCgroupPtr group, const char *mems)
1769
{
1770 1771
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetMems, -1, mems);
1772 1773
}

E
Eric Blake 已提交
1774

1775 1776 1777 1778 1779 1780 1781 1782
/**
 * virCgroupGetCpusetMems:
 *
 * @group: The cgroup to get cpuset.mems for
 * @mems: the numa nodes to get
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1783 1784
int
virCgroupGetCpusetMems(virCgroupPtr group, char **mems)
1785
{
1786 1787
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetMems, -1, mems);
1788 1789
}

E
Eric Blake 已提交
1790

1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
/**
 * virCgroupSetCpusetMemoryMigrate:
 *
 * @group: The cgroup to set cpuset.memory_migrate for
 * @migrate: Whether to migrate the memory on change or not
 *
 * Returns: 0 on success
 */
int
virCgroupSetCpusetMemoryMigrate(virCgroupPtr group, bool migrate)
{
1802 1803
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetMemoryMigrate, -1, migrate);
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817
}


/**
 * virCgroupGetCpusetMemoryMigrate:
 *
 * @group: The cgroup to get cpuset.memory_migrate for
 * @migrate: Migration setting
 *
 * Returns: 0 on success
 */
int
virCgroupGetCpusetMemoryMigrate(virCgroupPtr group, bool *migrate)
{
1818 1819
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetMemoryMigrate, -1, migrate);
1820 1821 1822
}


1823 1824 1825 1826 1827 1828
/**
 * virCgroupSetCpusetCpus:
 *
 * @group: The cgroup to set cpuset.cpus for
 * @cpus: the cpus to set
 *
N
Nitesh Konkar 已提交
1829
 * Returns: 0 on success
1830
 */
E
Eric Blake 已提交
1831 1832
int
virCgroupSetCpusetCpus(virCgroupPtr group, const char *cpus)
1833
{
1834 1835
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            setCpusetCpus, -1, cpus);
1836 1837
}

E
Eric Blake 已提交
1838

1839 1840 1841 1842 1843 1844
/**
 * virCgroupGetCpusetCpus:
 *
 * @group: The cgroup to get cpuset.cpus for
 * @cpus: the cpus to get
 *
N
Nitesh Konkar 已提交
1845
 * Returns: 0 on success
1846
 */
E
Eric Blake 已提交
1847 1848
int
virCgroupGetCpusetCpus(virCgroupPtr group, char **cpus)
1849
{
1850 1851
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUSET,
                            getCpusetCpus, -1, cpus);
1852 1853
}

E
Eric Blake 已提交
1854

1855 1856 1857
/**
 * virCgroupDenyAllDevices:
 *
1858
 * @group: The cgroup to deny all permissions, for all devices
1859 1860 1861
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1862 1863
int
virCgroupDenyAllDevices(virCgroupPtr group)
1864
{
1865 1866
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyAllDevices, -1);
1867 1868
}

1869 1870 1871
/**
 * virCgroupAllowAllDevices:
 *
1872
 * Allows the permission for all devices by setting lines similar
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
 * to these ones (obviously the 'm' permission is an example):
 *
 * 'b *:* m'
 * 'c *:* m'
 *
 * @group: The cgroup to allow devices for
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
 *
 * Returns: 0 on success
 */
int
virCgroupAllowAllDevices(virCgroupPtr group, int perms)
{
1886 1887
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowAllDevices, -1, perms);
1888 1889
}

E
Eric Blake 已提交
1890

1891 1892 1893 1894 1895
/**
 * virCgroupAllowDevice:
 *
 * @group: The cgroup to allow a device for
 * @type: The device type (i.e., 'c' or 'b')
1896 1897
 * @major: The major number of the device, a negative value means '*'
 * @minor: The minor number of the device, a negative value means '*'
1898
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1899 1900 1901
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1902 1903 1904
int
virCgroupAllowDevice(virCgroupPtr group, char type, int major, int minor,
                     int perms)
1905
{
1906 1907
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowDevice, -1, type, major, minor, perms);
1908
}
1909

E
Eric Blake 已提交
1910

1911 1912 1913 1914 1915
/**
 * virCgroupAllowDevicePath:
 *
 * @group: The cgroup to allow the device for
 * @path: the device to allow
1916
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
1917
 * @ignoreEacces: Ignore lack of permission (mostly for NFS mounts)
1918 1919 1920 1921
 *
 * Queries the type of device and its major/minor number, and
 * adds that to the cgroup ACL
 *
1922
 * Returns: 0 on success, 1 if path exists but is not a device or is not
1923
 * accessible, or * -1 on error
1924
 */
E
Eric Blake 已提交
1925
int
1926 1927 1928 1929
virCgroupAllowDevicePath(virCgroupPtr group,
                         const char *path,
                         int perms,
                         bool ignoreEacces)
1930 1931 1932
{
    struct stat sb;

1933
    if (stat(path, &sb) < 0) {
1934 1935 1936
        if (errno == EACCES && ignoreEacces)
            return 1;

1937 1938 1939 1940 1941
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return -1;
    }
1942 1943

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
1944
        return 1;
1945

1946 1947
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            allowDevice, -1,
1948 1949 1950 1951
                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
                            major(sb.st_rdev),
                            minor(sb.st_rdev),
                            perms);
1952
}
D
Daniel P. Berrange 已提交
1953

1954 1955 1956 1957 1958 1959

/**
 * virCgroupDenyDevice:
 *
 * @group: The cgroup to deny a device for
 * @type: The device type (i.e., 'c' or 'b')
1960 1961
 * @major: The major number of the device, a negative value means '*'
 * @minor: The minor number of the device, a negative value means '*'
1962
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to deny
1963 1964 1965
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
1966 1967 1968
int
virCgroupDenyDevice(virCgroupPtr group, char type, int major, int minor,
                    int perms)
1969
{
1970 1971
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyDevice, -1, type, major, minor, perms);
1972 1973
}

E
Eric Blake 已提交
1974

1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
/**
 * virCgroupDenyDevicePath:
 *
 * @group: The cgroup to deny the device for
 * @path: the device to deny
 * @perms: Bitwise or of VIR_CGROUP_DEVICE permission bits to allow
 * @ignoreEacces: Ignore lack of permission (mostly for NFS mounts)
 *
 * Queries the type of device and its major/minor number, and
 * removes it from the cgroup ACL
 *
 * Returns: 0 on success, 1 if path exists but is not a device or is not
 * accessible, or -1 on error.
 */
E
Eric Blake 已提交
1989
int
1990 1991 1992 1993
virCgroupDenyDevicePath(virCgroupPtr group,
                        const char *path,
                        int perms,
                        bool ignoreEacces)
1994 1995 1996
{
    struct stat sb;

1997
    if (stat(path, &sb) < 0) {
1998 1999 2000
        if (errno == EACCES && ignoreEacces)
            return 1;

2001 2002 2003 2004 2005
        virReportSystemError(errno,
                             _("Path '%s' is not accessible"),
                             path);
        return -1;
    }
2006 2007

    if (!S_ISCHR(sb.st_mode) && !S_ISBLK(sb.st_mode))
2008
        return 1;
2009

2010 2011
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_DEVICES,
                            denyDevice, -1,
2012 2013 2014 2015
                            S_ISCHR(sb.st_mode) ? 'c' : 'b',
                            major(sb.st_rdev),
                            minor(sb.st_rdev),
                            perms);
2016 2017
}

E
Eric Blake 已提交
2018

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
/* This function gets the sums of cpu time consumed by all vcpus.
 * For example, if there are 4 physical cpus, and 2 vcpus in a domain,
 * then for each vcpu, the cpuacct.usage_percpu looks like this:
 *   t0 t1 t2 t3
 * and we have 2 groups of such data:
 *   v\p   0   1   2   3
 *   0   t00 t01 t02 t03
 *   1   t10 t11 t12 t13
 * for each pcpu, the sum is cpu time consumed by all vcpus.
 *   s0 = t00 + t10
 *   s1 = t01 + t11
 *   s2 = t02 + t12
 *   s3 = t03 + t13
 */
static int
virCgroupGetPercpuVcpuSum(virCgroupPtr group,
2035
                          virBitmapPtr guestvcpus,
2036
                          unsigned long long *sum_cpu_time,
2037 2038
                          size_t nsum,
                          virBitmapPtr cpumap)
2039
{
2040
    int ret = -1;
2041
    ssize_t i = -1;
2042
    virCgroupPtr group_vcpu = NULL;
2043

2044
    while ((i = virBitmapNextSetBit(guestvcpus, i)) >= 0) {
2045
        g_autofree char *buf = NULL;
2046 2047
        char *pos;
        unsigned long long tmp;
2048
        ssize_t j;
2049

J
John Ferlan 已提交
2050 2051
        if (virCgroupNewThread(group, VIR_CGROUP_THREAD_VCPU, i,
                               false, &group_vcpu) < 0)
2052
            goto cleanup;
2053 2054

        if (virCgroupGetCpuacctPercpuUsage(group_vcpu, &buf) < 0)
2055
            goto cleanup;
2056 2057

        pos = buf;
2058 2059 2060
        for (j = virBitmapNextSetBit(cpumap, -1);
             j >= 0 && j < nsum;
             j = virBitmapNextSetBit(cpumap, j)) {
2061 2062 2063
            if (virStrToLong_ull(pos, &pos, 10, &tmp) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                               _("cpuacct parse error"));
2064
                goto cleanup;
2065 2066 2067
            }
            sum_cpu_time[j] += tmp;
        }
2068

2069
        virCgroupFree(&group_vcpu);
2070 2071
    }

2072 2073
    ret = 0;
 cleanup:
2074
    virCgroupFree(&group_vcpu);
2075
    return ret;
2076 2077 2078
}


2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
/**
 * virCgroupGetPercpuStats:
 * @cgroup: cgroup data structure
 * @params: typed parameter array where data is returned
 * @nparams: cardinality of @params
 * @start_cpu: offset of physical CPU to get data for
 * @ncpus: number of physical CPUs to get data for
 * @nvcpupids: number of vCPU threads for a domain (actual number of vcpus)
 *
 * This function is the worker that retrieves data in the appropriate format
 * for the terribly designed 'virDomainGetCPUStats' API. Sharing semantics with
 * the API, this function has two modes of operation depending on magic settings
 * of the input arguments. Please refer to docs of 'virDomainGetCPUStats' for
 * the usage patterns of the similarly named arguments.
 *
 * @nvcpupids determines the count of active vcpu threads for the vm. If the
 * threads could not be detected the percpu data is skipped.
 *
 * Please DON'T use this function anywhere else.
 */
2099 2100 2101 2102 2103
int
virCgroupGetPercpuStats(virCgroupPtr group,
                        virTypedParameterPtr params,
                        unsigned int nparams,
                        int start_cpu,
2104
                        unsigned int ncpus,
2105
                        virBitmapPtr guestvcpus)
2106
{
2107
    int ret = -1;
2108
    size_t i;
2109
    int need_cpus, total_cpus;
2110
    char *pos;
2111 2112
    g_autofree char *buf = NULL;
    g_autofree unsigned long long *sum_cpu_time = NULL;
2113 2114 2115
    virTypedParameterPtr ent;
    int param_idx;
    unsigned long long cpu_time;
2116
    virBitmapPtr cpumap = NULL;
2117 2118

    /* return the number of supported params */
2119
    if (nparams == 0 && ncpus != 0) {
2120
        if (!guestvcpus)
2121 2122 2123 2124
            return CGROUP_NB_PER_CPU_STAT_PARAM;
        else
            return CGROUP_NB_PER_CPU_STAT_PARAM + 1;
    }
2125 2126

    /* To parse account file, we need to know how many cpus are present.  */
2127
    if (!(cpumap = virHostCPUGetPresentBitmap()))
2128
        return -1;
2129

2130 2131
    total_cpus = virBitmapSize(cpumap);

2132
    /* return total number of cpus */
2133 2134 2135 2136
    if (ncpus == 0) {
        ret = total_cpus;
        goto cleanup;
    }
2137

2138
    if (start_cpu >= total_cpus) {
2139 2140
        virReportError(VIR_ERR_INVALID_ARG,
                       _("start_cpu %d larger than maximum of %d"),
2141
                       start_cpu, total_cpus - 1);
2142
        goto cleanup;
2143 2144 2145 2146
    }

    /* we get percpu cputime accounting info. */
    if (virCgroupGetCpuacctPercpuUsage(group, &buf))
2147
        goto cleanup;
2148 2149 2150 2151 2152 2153
    pos = buf;

    /* return percpu cputime in index 0 */
    param_idx = 0;

    /* number of cpus to compute */
J
Ján Tomko 已提交
2154
    need_cpus = MIN(total_cpus, start_cpu + ncpus);
2155

J
Ján Tomko 已提交
2156
    for (i = 0; i < need_cpus; i++) {
J
Ján Tomko 已提交
2157
        if (!virBitmapIsBitSet(cpumap, i)) {
2158 2159
            cpu_time = 0;
        } else if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
2160 2161
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("cpuacct parse error"));
2162
            goto cleanup;
2163 2164 2165 2166 2167 2168
        }
        if (i < start_cpu)
            continue;
        ent = &params[(i - start_cpu) * nparams + param_idx];
        if (virTypedParameterAssign(ent, VIR_DOMAIN_CPU_STATS_CPUTIME,
                                    VIR_TYPED_PARAM_ULLONG, cpu_time) < 0)
2169
            goto cleanup;
2170 2171
    }

2172
    /* return percpu vcputime in index 1 */
2173
    param_idx = 1;
2174

2175
    if (guestvcpus && param_idx < nparams) {
2176
        if (VIR_ALLOC_N(sum_cpu_time, need_cpus) < 0)
2177
            goto cleanup;
2178 2179
        if (virCgroupGetPercpuVcpuSum(group, guestvcpus, sum_cpu_time,
                                      need_cpus, cpumap) < 0)
2180
            goto cleanup;
2181 2182

        for (i = start_cpu; i < need_cpus; i++) {
2183 2184
            int idx = (i - start_cpu) * nparams + param_idx;
            if (virTypedParameterAssign(&params[idx],
2185 2186 2187
                                        VIR_DOMAIN_CPU_STATS_VCPUTIME,
                                        VIR_TYPED_PARAM_ULLONG,
                                        sum_cpu_time[i]) < 0)
2188
                goto cleanup;
2189 2190 2191
        }

        param_idx++;
2192 2193
    }

2194 2195 2196 2197 2198
    ret = param_idx;

 cleanup:
    virBitmapFree(cpumap);
    return ret;
2199 2200
}

2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250

int
virCgroupGetDomainTotalCpuStats(virCgroupPtr group,
                                virTypedParameterPtr params,
                                int nparams)
{
    unsigned long long cpu_time;
    int ret;

    if (nparams == 0) /* return supported number of params */
        return CGROUP_NB_TOTAL_CPU_STAT_PARAM;
    /* entry 0 is cputime */
    ret = virCgroupGetCpuacctUsage(group, &cpu_time);
    if (ret < 0) {
        virReportSystemError(-ret, "%s", _("unable to get cpu account"));
        return -1;
    }

    if (virTypedParameterAssign(&params[0], VIR_DOMAIN_CPU_STATS_CPUTIME,
                                VIR_TYPED_PARAM_ULLONG, cpu_time) < 0)
        return -1;

    if (nparams > 1) {
        unsigned long long user;
        unsigned long long sys;

        ret = virCgroupGetCpuacctStat(group, &user, &sys);
        if (ret < 0) {
            virReportSystemError(-ret, "%s", _("unable to get cpu account"));
            return -1;
        }

        if (virTypedParameterAssign(&params[1],
                                    VIR_DOMAIN_CPU_STATS_USERTIME,
                                    VIR_TYPED_PARAM_ULLONG, user) < 0)
            return -1;
        if (nparams > 2 &&
            virTypedParameterAssign(&params[2],
                                    VIR_DOMAIN_CPU_STATS_SYSTEMTIME,
                                    VIR_TYPED_PARAM_ULLONG, sys) < 0)
            return -1;

        if (nparams > CGROUP_NB_TOTAL_CPU_STAT_PARAM)
            nparams = CGROUP_NB_TOTAL_CPU_STAT_PARAM;
    }

    return nparams;
}


E
Eric Blake 已提交
2251 2252
int
virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares)
2253
{
2254 2255
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuShares, -1, shares);
2256 2257
}

E
Eric Blake 已提交
2258 2259 2260

int
virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares)
2261
{
2262 2263
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuShares, -1, shares);
2264
}
2265

E
Eric Blake 已提交
2266

2267 2268 2269 2270 2271 2272 2273 2274
/**
 * virCgroupSetCpuCfsPeriod:
 *
 * @group: The cgroup to change cpu.cfs_period_us for
 * @cfs_period: The bandwidth period in usecs
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
2275 2276
int
virCgroupSetCpuCfsPeriod(virCgroupPtr group, unsigned long long cfs_period)
2277
{
2278 2279
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuCfsPeriod, -1, cfs_period);
2280 2281
}

E
Eric Blake 已提交
2282

2283 2284 2285 2286 2287 2288 2289 2290
/**
 * virCgroupGetCpuCfsPeriod:
 *
 * @group: The cgroup to get cpu.cfs_period_us for
 * @cfs_period: Pointer to the returned bandwidth period in usecs
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
2291 2292
int
virCgroupGetCpuCfsPeriod(virCgroupPtr group, unsigned long long *cfs_period)
2293
{
2294 2295
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuCfsPeriod, -1, cfs_period);
2296 2297
}

E
Eric Blake 已提交
2298

2299 2300 2301 2302 2303 2304 2305 2306 2307
/**
 * virCgroupSetCpuCfsQuota:
 *
 * @group: The cgroup to change cpu.cfs_quota_us for
 * @cfs_quota: the cpu bandwidth (in usecs) that this tg will be allowed to
 *             consume over period
 *
 * Returns: 0 on success
 */
E
Eric Blake 已提交
2308 2309
int
virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota)
2310
{
2311 2312
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            setCpuCfsQuota, -1, cfs_quota);
2313 2314
}

E
Eric Blake 已提交
2315 2316 2317

int
virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage)
2318
{
2319 2320
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctPercpuUsage, -1, usage);
2321 2322
}

E
Eric Blake 已提交
2323

2324
int
E
Eric Blake 已提交
2325 2326 2327 2328 2329
virCgroupRemoveRecursively(char *grppath)
{
    DIR *grpdir;
    struct dirent *ent;
    int rc = 0;
E
Eric Blake 已提交
2330
    int direrr;
E
Eric Blake 已提交
2331

J
Ján Tomko 已提交
2332
    if (virDirOpenQuiet(&grpdir, grppath) < 0) {
E
Eric Blake 已提交
2333 2334 2335 2336 2337 2338 2339
        if (errno == ENOENT)
            return 0;
        rc = -errno;
        VIR_ERROR(_("Unable to open %s (%d)"), grppath, errno);
        return rc;
    }

E
Eric Blake 已提交
2340 2341 2342
    /* This is best-effort cleanup: we want to log failures with just
     * VIR_ERROR instead of normal virReportError */
    while ((direrr = virDirRead(grpdir, &ent, NULL)) > 0) {
2343
        g_autofree char *path = NULL;
E
Eric Blake 已提交
2344 2345 2346

        if (ent->d_type != DT_DIR) continue;

2347 2348
        path = g_strdup_printf("%s/%s", grppath, ent->d_name);

E
Eric Blake 已提交
2349 2350 2351 2352
        rc = virCgroupRemoveRecursively(path);
        if (rc != 0)
            break;
    }
E
Eric Blake 已提交
2353 2354 2355 2356 2357
    if (direrr < 0) {
        rc = -errno;
        VIR_ERROR(_("Failed to readdir for %s (%d)"), grppath, errno);
    }

J
Ján Tomko 已提交
2358
    VIR_DIR_CLOSE(grpdir);
E
Eric Blake 已提交
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384

    VIR_DEBUG("Removing cgroup %s", grppath);
    if (rmdir(grppath) != 0 && errno != ENOENT) {
        rc = -errno;
        VIR_ERROR(_("Unable to remove %s (%d)"), grppath, errno);
    }

    return rc;
}


/**
 * virCgroupRemove:
 *
 * @group: The group to be removed
 *
 * It first removes all child groups recursively
 * in depth first order and then removes @group
 * because the presence of the child groups
 * prevents removing @group.
 *
 * Returns: 0 on success
 */
int
virCgroupRemove(virCgroupPtr group)
{
2385 2386 2387
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
2388 2389 2390 2391
        if (group->backends[i]) {
            int rc = group->backends[i]->remove(group);
            if (rc < 0)
                return rc;
2392 2393 2394 2395
        }
    }

    return 0;
E
Eric Blake 已提交
2396 2397 2398
}


2399 2400 2401
/*
 * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error
 */
E
Eric Blake 已提交
2402
static int
2403 2404 2405 2406 2407
virCgroupKillInternal(virCgroupPtr group,
                      int signum,
                      virHashTablePtr pids,
                      int controller,
                      const char *taskFile)
2408
{
2409
    int ret = -1;
2410
    bool killedAny = false;
2411
    g_autofree char *keypath = NULL;
2412
    bool done = false;
E
Eric Blake 已提交
2413 2414 2415
    FILE *fp = NULL;
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
              group, group->path, signum, pids);
2416

2417
    if (virCgroupPathOfController(group, controller, taskFile, &keypath) < 0)
2418
        return -1;
2419 2420 2421 2422 2423 2424 2425

    /* PIDs may be forking as we kill them, so loop
     * until there are no new PIDs found
     */
    while (!done) {
        done = true;
        if (!(fp = fopen(keypath, "r"))) {
2426 2427 2428 2429 2430 2431
            if (errno == ENOENT) {
                VIR_DEBUG("No file %s, assuming done", keypath);
                killedAny = false;
                goto done;
            }

2432 2433 2434
            virReportSystemError(errno,
                                 _("Failed to read %s"),
                                 keypath);
2435 2436 2437
            goto cleanup;
        } else {
            while (!feof(fp)) {
M
Michal Privoznik 已提交
2438 2439
                long pid_value;
                if (fscanf(fp, "%ld", &pid_value) != 1) {
2440 2441
                    if (feof(fp))
                        break;
2442 2443 2444
                    virReportSystemError(errno,
                                         _("Failed to read %s"),
                                         keypath);
E
Eric Blake 已提交
2445
                    goto cleanup;
2446
                }
2447
                if (virHashLookup(pids, (void*)pid_value))
2448 2449
                    continue;

M
Michal Privoznik 已提交
2450
                VIR_DEBUG("pid=%ld", pid_value);
2451 2452
                /* Cgroups is a Linux concept, so this cast is safe.  */
                if (kill((pid_t)pid_value, signum) < 0) {
2453
                    if (errno != ESRCH) {
2454
                        virReportSystemError(errno,
M
Michal Privoznik 已提交
2455
                                             _("Failed to kill process %ld"),
2456
                                             pid_value);
2457 2458 2459 2460
                        goto cleanup;
                    }
                    /* Leave RC == 0 since we didn't kill one */
                } else {
2461
                    killedAny = true;
2462 2463 2464
                    done = false;
                }

2465
                ignore_value(virHashAddEntry(pids, (void*)pid_value, (void*)1));
2466 2467 2468 2469 2470
            }
            VIR_FORCE_FCLOSE(fp);
        }
    }

2471
 done:
2472
    ret = killedAny ? 1 : 0;
2473

2474
 cleanup:
E
Eric Blake 已提交
2475
    VIR_FORCE_FCLOSE(fp);
2476

2477
    return ret;
2478 2479 2480
}


E
Eric Blake 已提交
2481 2482
static uint32_t
virCgroupPidCode(const void *name, uint32_t seed)
2483
{
M
Michal Privoznik 已提交
2484
    long pid_value = (long)(intptr_t)name;
2485
    return virHashCodeGen(&pid_value, sizeof(pid_value), seed);
2486
}
E
Eric Blake 已提交
2487 2488 2489 2490


static bool
virCgroupPidEqual(const void *namea, const void *nameb)
2491 2492 2493
{
    return namea == nameb;
}
E
Eric Blake 已提交
2494 2495 2496 2497


static void *
virCgroupPidCopy(const void *name)
2498 2499 2500 2501
{
    return (void*)name;
}

E
Eric Blake 已提交
2502

2503 2504 2505 2506 2507 2508 2509
static char *
virCgroupPidPrintHuman(const void *name)
{
    return g_strdup_printf("%ld", (const long)name);
}


2510
int
E
Eric Blake 已提交
2511 2512 2513
virCgroupKillRecursiveInternal(virCgroupPtr group,
                               int signum,
                               virHashTablePtr pids,
2514 2515
                               int controller,
                               const char *taskFile,
E
Eric Blake 已提交
2516
                               bool dormdir)
2517
{
2518
    int ret = -1;
2519
    int rc;
2520
    bool killedAny = false;
2521
    g_autofree char *keypath = NULL;
2522
    DIR *dp = NULL;
2523
    virCgroupPtr subgroup = NULL;
2524
    struct dirent *ent;
E
Eric Blake 已提交
2525
    int direrr;
E
Eric Blake 已提交
2526 2527
    VIR_DEBUG("group=%p path=%s signum=%d pids=%p",
              group, group->path, signum, pids);
2528

2529
    if (virCgroupPathOfController(group, controller, "", &keypath) < 0)
2530
        return -1;
2531

2532 2533
    if ((rc = virCgroupKillInternal(group, signum, pids,
                                    controller, taskFile)) < 0) {
2534
        goto cleanup;
2535
    }
2536 2537
    if (rc == 1)
        killedAny = true;
2538

2539
    VIR_DEBUG("Iterate over children of %s (killedAny=%d)", keypath, killedAny);
J
Ján Tomko 已提交
2540
    if ((rc = virDirOpenIfExists(&dp, keypath)) < 0)
2541
        goto cleanup;
J
Ján Tomko 已提交
2542 2543 2544 2545 2546

    if (rc == 0) {
        VIR_DEBUG("Path %s does not exist, assuming done", keypath);
        killedAny = false;
        goto done;
2547 2548
    }

E
Eric Blake 已提交
2549
    while ((direrr = virDirRead(dp, &ent, keypath)) > 0) {
2550 2551 2552 2553 2554
        if (ent->d_type != DT_DIR)
            continue;

        VIR_DEBUG("Process subdir %s", ent->d_name);

2555
        if (virCgroupNew(-1, ent->d_name, group, -1, &subgroup) < 0)
2556 2557
            goto cleanup;

E
Eric Blake 已提交
2558
        if ((rc = virCgroupKillRecursiveInternal(subgroup, signum, pids,
2559
                                                 controller, taskFile, true)) < 0)
2560 2561
            goto cleanup;
        if (rc == 1)
2562
            killedAny = true;
2563 2564 2565

        if (dormdir)
            virCgroupRemove(subgroup);
2566

2567
        virCgroupFree(&subgroup);
2568
    }
E
Eric Blake 已提交
2569 2570
    if (direrr < 0)
        goto cleanup;
2571

2572
 done:
2573
    ret = killedAny ? 1 : 0;
2574

2575
 cleanup:
2576
    virCgroupFree(&subgroup);
J
Ján Tomko 已提交
2577
    VIR_DIR_CLOSE(dp);
2578
    return ret;
2579 2580
}

E
Eric Blake 已提交
2581 2582 2583

int
virCgroupKillRecursive(virCgroupPtr group, int signum)
2584
{
2585 2586 2587
    int ret = 0;
    int rc;
    size_t i;
2588
    bool backendAvailable = false;
2589
    virCgroupBackendPtr *backends = virCgroupBackendGetAll();
2590
    virHashTablePtr pids = virHashCreateFull(100,
2591
                                             NULL,
2592 2593 2594
                                             virCgroupPidCode,
                                             virCgroupPidEqual,
                                             virCgroupPidCopy,
2595
                                             virCgroupPidPrintHuman,
2596
                                             NULL);
2597

2598
    VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);
2599

2600
    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
2601 2602
        if (backends && backends[i] && backends[i]->available()) {
            backendAvailable = true;
2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
            rc = backends[i]->killRecursive(group, signum, pids);
            if (rc < 0) {
                ret = -1;
                goto cleanup;
            }
            if (rc > 0)
                ret = rc;
        }
    }

2613 2614 2615 2616 2617 2618
    if (!backends || !backendAvailable) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("no cgroup backend available"));
        goto cleanup;
    }

2619 2620
 cleanup:
    virHashFree(pids);
2621
    return ret;
2622 2623 2624
}


E
Eric Blake 已提交
2625 2626
int
virCgroupKillPainfully(virCgroupPtr group)
2627
{
2628
    size_t i;
2629
    int ret;
2630
    VIR_DEBUG("cgroup=%p path=%s", group, group->path);
2631
    for (i = 0; i < 15; i++) {
2632 2633 2634 2635 2636 2637
        int signum;
        if (i == 0)
            signum = SIGTERM;
        else if (i == 8)
            signum = SIGKILL;
        else
J
Ján Tomko 已提交
2638
            signum = 0; /* Just check for existence */
2639

2640 2641 2642 2643
        ret = virCgroupKillRecursive(group, signum);
        VIR_DEBUG("Iteration %zu rc=%d", i, ret);
        /* If ret == -1 we hit error, if 0 we ran out of PIDs */
        if (ret <= 0)
2644 2645
            break;

2646
        g_usleep(200 * 1000);
2647
    }
2648 2649
    VIR_DEBUG("Complete %d", ret);
    return ret;
2650
}
2651

E
Eric Blake 已提交
2652

2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
/**
 * virCgroupGetCpuCfsQuota:
 *
 * @group: The cgroup to get cpu.cfs_quota_us for
 * @cfs_quota: Pointer to the returned cpu bandwidth (in usecs) that this tg
 *             will be allowed to consume over period
 *
 * Returns: 0 on success
 */
int
virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota)
{
2665 2666
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPU,
                            getCpuCfsQuota, -1, cfs_quota);
2667 2668 2669 2670 2671 2672
}


int
virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage)
{
2673 2674
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctUsage, -1, usage);
2675 2676 2677 2678 2679 2680 2681
}


int
virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
                        unsigned long long *sys)
{
2682 2683
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_CPUACCT,
                            getCpuacctStat, -1, user, sys);
2684 2685 2686 2687 2688 2689
}


int
virCgroupSetFreezerState(virCgroupPtr group, const char *state)
{
2690 2691
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_FREEZER,
                            setFreezerState, -1, state);
2692 2693 2694 2695 2696 2697
}


int
virCgroupGetFreezerState(virCgroupPtr group, char **state)
{
2698 2699
    VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_FREEZER,
                            getFreezerState, -1, state);
2700 2701 2702
}


E
Eric Blake 已提交
2703
int
2704 2705
virCgroupBindMount(virCgroupPtr group, const char *oldroot,
                   const char *mountopts)
2706
{
2707 2708 2709 2710 2711 2712 2713 2714 2715 2716
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (group->backends[i] &&
            group->backends[i]->bindMount(group, oldroot, mountopts) < 0) {
            return -1;
        }
    }

    return 0;
2717
}
2718 2719


2720 2721 2722 2723 2724
int virCgroupSetOwner(virCgroupPtr cgroup,
                      uid_t uid,
                      gid_t gid,
                      int controllers)
{
2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i] &&
            cgroup->backends[i]->setOwner(cgroup, uid, gid, controllers) < 0) {
            return -1;
        }
    }

    return 0;
2735 2736 2737
}


2738 2739 2740 2741 2742 2743 2744 2745 2746 2747
/**
 * virCgroupSupportsCpuBW():
 * Check whether the host supports CFS bandwidth.
 *
 * Return true when CFS bandwidth is supported,
 * false when CFS bandwidth is not supported.
 */
bool
virCgroupSupportsCpuBW(virCgroupPtr cgroup)
{
2748 2749
    VIR_CGROUP_BACKEND_CALL(cgroup, VIR_CGROUP_CONTROLLER_CPU,
                            supportsCpuBW, false);
2750 2751
}

2752 2753 2754
int
virCgroupHasEmptyTasks(virCgroupPtr cgroup, int controller)
{
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
    size_t i;

    for (i = 0; i < VIR_CGROUP_BACKEND_TYPE_LAST; i++) {
        if (cgroup->backends[i]) {
            int rc = cgroup->backends[i]->hasEmptyTasks(cgroup, controller);
            if (rc <= 0)
                return rc;
        }
    }

    return 1;
2766
}
2767

2768 2769 2770
bool
virCgroupControllerAvailable(int controller)
{
2771 2772
    virCgroupPtr cgroup;
    bool ret = false;
2773 2774

    if (virCgroupNewSelf(&cgroup) < 0)
2775
        return ret;
2776

2777
    ret = virCgroupHasController(cgroup, controller);
2778
    virCgroupFree(&cgroup);
2779
    return ret;
2780 2781
}

2782
#else /* !__linux__ */
2783

2784 2785 2786 2787 2788 2789 2790
bool
virCgroupAvailable(void)
{
    return false;
}


2791
int
J
Ján Tomko 已提交
2792 2793 2794 2795
virCgroupNewPartition(const char *path G_GNUC_UNUSED,
                      bool create G_GNUC_UNUSED,
                      int controllers G_GNUC_UNUSED,
                      virCgroupPtr *group G_GNUC_UNUSED)
2796 2797 2798 2799 2800 2801 2802 2803
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2804
virCgroupNewSelf(virCgroupPtr *group G_GNUC_UNUSED)
2805 2806 2807 2808 2809 2810 2811 2812
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2813 2814 2815 2816 2817
virCgroupNewDomainPartition(virCgroupPtr partition G_GNUC_UNUSED,
                            const char *driver G_GNUC_UNUSED,
                            const char *name G_GNUC_UNUSED,
                            bool create G_GNUC_UNUSED,
                            virCgroupPtr *group G_GNUC_UNUSED)
2818 2819 2820 2821 2822 2823 2824
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2825
int
J
Ján Tomko 已提交
2826 2827 2828 2829 2830
virCgroupNewThread(virCgroupPtr domain G_GNUC_UNUSED,
                   virCgroupThreadName nameval G_GNUC_UNUSED,
                   int id G_GNUC_UNUSED,
                   bool create G_GNUC_UNUSED,
                   virCgroupPtr *group G_GNUC_UNUSED)
2831 2832 2833 2834 2835 2836 2837
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2838
int
J
Ján Tomko 已提交
2839 2840 2841
virCgroupNewDetect(pid_t pid G_GNUC_UNUSED,
                   int controllers G_GNUC_UNUSED,
                   virCgroupPtr *group G_GNUC_UNUSED)
2842 2843 2844 2845 2846 2847 2848
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2849
int
J
Ján Tomko 已提交
2850 2851 2852 2853 2854 2855
virCgroupNewDetectMachine(const char *name G_GNUC_UNUSED,
                          const char *drivername G_GNUC_UNUSED,
                          pid_t pid G_GNUC_UNUSED,
                          int controllers G_GNUC_UNUSED,
                          char *machinename G_GNUC_UNUSED,
                          virCgroupPtr *group G_GNUC_UNUSED)
2856 2857 2858 2859 2860 2861
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

J
Ján Tomko 已提交
2862

J
Ján Tomko 已提交
2863
int virCgroupTerminateMachine(const char *name G_GNUC_UNUSED)
J
Ján Tomko 已提交
2864 2865 2866 2867 2868 2869 2870
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2871
int
J
Ján Tomko 已提交
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
virCgroupNewMachine(const char *name G_GNUC_UNUSED,
                    const char *drivername G_GNUC_UNUSED,
                    const unsigned char *uuid G_GNUC_UNUSED,
                    const char *rootdir G_GNUC_UNUSED,
                    pid_t pidleader G_GNUC_UNUSED,
                    bool isContainer G_GNUC_UNUSED,
                    size_t nnicindexes G_GNUC_UNUSED,
                    int *nicindexes G_GNUC_UNUSED,
                    const char *partition G_GNUC_UNUSED,
                    int controllers G_GNUC_UNUSED,
                    unsigned int maxthreads G_GNUC_UNUSED,
                    virCgroupPtr *group G_GNUC_UNUSED)
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


bool
virCgroupNewIgnoreError(void)
{
    VIR_DEBUG("No cgroups present/configured/accessible, ignoring error");
    return true;
}

2898 2899

bool
J
Ján Tomko 已提交
2900 2901
virCgroupHasController(virCgroupPtr cgroup G_GNUC_UNUSED,
                       int controller G_GNUC_UNUSED)
2902 2903 2904 2905 2906
{
    return false;
}


2907
int
J
Ján Tomko 已提交
2908 2909 2910 2911
virCgroupPathOfController(virCgroupPtr group G_GNUC_UNUSED,
                          unsigned int controller G_GNUC_UNUSED,
                          const char *key G_GNUC_UNUSED,
                          char **path G_GNUC_UNUSED)
2912 2913 2914 2915 2916 2917 2918
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2919
int
J
Ján Tomko 已提交
2920 2921
virCgroupAddProcess(virCgroupPtr group G_GNUC_UNUSED,
                    pid_t pid G_GNUC_UNUSED)
2922 2923 2924 2925 2926 2927 2928
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2929
int
J
Ján Tomko 已提交
2930 2931
virCgroupAddMachineProcess(virCgroupPtr group G_GNUC_UNUSED,
                           pid_t pid G_GNUC_UNUSED)
2932 2933 2934 2935 2936 2937 2938
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2939
int
J
Ján Tomko 已提交
2940 2941
virCgroupAddThread(virCgroupPtr group G_GNUC_UNUSED,
                   pid_t pid G_GNUC_UNUSED)
2942 2943 2944 2945 2946 2947 2948
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2949
int
J
Ján Tomko 已提交
2950 2951 2952 2953 2954
virCgroupGetBlkioIoServiced(virCgroupPtr group G_GNUC_UNUSED,
                            long long *bytes_read G_GNUC_UNUSED,
                            long long *bytes_write G_GNUC_UNUSED,
                            long long *requests_read G_GNUC_UNUSED,
                            long long *requests_write G_GNUC_UNUSED)
2955 2956 2957 2958 2959 2960 2961 2962
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2963 2964 2965 2966 2967 2968
virCgroupGetBlkioIoDeviceServiced(virCgroupPtr group G_GNUC_UNUSED,
                                  const char *path G_GNUC_UNUSED,
                                  long long *bytes_read G_GNUC_UNUSED,
                                  long long *bytes_write G_GNUC_UNUSED,
                                  long long *requests_read G_GNUC_UNUSED,
                                  long long *requests_write G_GNUC_UNUSED)
2969 2970 2971 2972 2973 2974 2975
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2976
int
J
Ján Tomko 已提交
2977 2978
virCgroupSetBlkioWeight(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned int weight G_GNUC_UNUSED)
2979 2980 2981 2982 2983 2984 2985 2986
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
2987 2988
virCgroupGetBlkioWeight(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned int *weight G_GNUC_UNUSED)
2989 2990 2991 2992 2993 2994 2995
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


2996
static int
J
Ján Tomko 已提交
2997 2998 2999
virCgroupSetBlkioDeviceWeight(virCgroupPtr group G_GNUC_UNUSED,
                              const char *path G_GNUC_UNUSED,
                              unsigned int weight G_GNUC_UNUSED)
3000 3001 3002 3003 3004 3005
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3006
static int
J
Ján Tomko 已提交
3007 3008 3009
virCgroupSetBlkioDeviceReadIops(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned int riops G_GNUC_UNUSED)
3010 3011 3012 3013 3014 3015
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3016
static int
J
Ján Tomko 已提交
3017 3018 3019
virCgroupSetBlkioDeviceWriteIops(virCgroupPtr group G_GNUC_UNUSED,
                                 const char *path G_GNUC_UNUSED,
                                 unsigned int wiops G_GNUC_UNUSED)
3020 3021 3022 3023 3024 3025
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3026
static int
J
Ján Tomko 已提交
3027 3028 3029
virCgroupSetBlkioDeviceReadBps(virCgroupPtr group G_GNUC_UNUSED,
                               const char *path G_GNUC_UNUSED,
                               unsigned long long rbps G_GNUC_UNUSED)
3030 3031 3032 3033 3034 3035
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3036
static int
J
Ján Tomko 已提交
3037 3038 3039
virCgroupSetBlkioDeviceWriteBps(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned long long wbps G_GNUC_UNUSED)
3040 3041 3042 3043 3044 3045
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3046
static int
J
Ján Tomko 已提交
3047 3048 3049
virCgroupGetBlkioDeviceWeight(virCgroupPtr group G_GNUC_UNUSED,
                              const char *path G_GNUC_UNUSED,
                              unsigned int *weight G_GNUC_UNUSED)
3050 3051 3052 3053 3054 3055
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3056
static int
J
Ján Tomko 已提交
3057 3058 3059
virCgroupGetBlkioDeviceReadIops(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned int *riops G_GNUC_UNUSED)
3060 3061 3062 3063 3064 3065
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3066
static int
J
Ján Tomko 已提交
3067 3068 3069
virCgroupGetBlkioDeviceWriteIops(virCgroupPtr group G_GNUC_UNUSED,
                                 const char *path G_GNUC_UNUSED,
                                 unsigned int *wiops G_GNUC_UNUSED)
3070 3071 3072 3073 3074 3075
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3076
static int
J
Ján Tomko 已提交
3077 3078 3079
virCgroupGetBlkioDeviceReadBps(virCgroupPtr group G_GNUC_UNUSED,
                               const char *path G_GNUC_UNUSED,
                               unsigned long long *rbps G_GNUC_UNUSED)
3080 3081 3082 3083 3084 3085
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3086
static int
J
Ján Tomko 已提交
3087 3088 3089
virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group G_GNUC_UNUSED,
                                const char *path G_GNUC_UNUSED,
                                unsigned long long *wbps G_GNUC_UNUSED)
3090 3091 3092 3093 3094
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}
3095

3096
int
J
Ján Tomko 已提交
3097 3098
virCgroupSetMemory(virCgroupPtr group G_GNUC_UNUSED,
                   unsigned long long kb G_GNUC_UNUSED)
3099 3100 3101 3102 3103 3104 3105
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


P
Pavel Hrdina 已提交
3106
int
J
Ján Tomko 已提交
3107 3108 3109 3110 3111 3112 3113
virCgroupGetMemoryStat(virCgroupPtr group G_GNUC_UNUSED,
                       unsigned long long *cache G_GNUC_UNUSED,
                       unsigned long long *activeAnon G_GNUC_UNUSED,
                       unsigned long long *inactiveAnon G_GNUC_UNUSED,
                       unsigned long long *activeFile G_GNUC_UNUSED,
                       unsigned long long *inactiveFile G_GNUC_UNUSED,
                       unsigned long long *unevictable G_GNUC_UNUSED)
P
Pavel Hrdina 已提交
3114 3115 3116 3117 3118 3119 3120
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3121
int
J
Ján Tomko 已提交
3122 3123
virCgroupGetMemoryUsage(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned long *kb G_GNUC_UNUSED)
3124 3125 3126 3127 3128 3129 3130 3131
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3132 3133
virCgroupSetMemoryHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long kb G_GNUC_UNUSED)
3134 3135 3136 3137 3138 3139 3140 3141
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3142 3143
virCgroupGetMemoryHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long *kb G_GNUC_UNUSED)
3144 3145 3146 3147 3148 3149 3150 3151
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3152 3153
virCgroupSetMemorySoftLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long kb G_GNUC_UNUSED)
3154 3155 3156 3157 3158 3159 3160 3161
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3162 3163
virCgroupGetMemorySoftLimit(virCgroupPtr group G_GNUC_UNUSED,
                            unsigned long long *kb G_GNUC_UNUSED)
3164 3165 3166 3167 3168 3169 3170 3171
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3172 3173
virCgroupSetMemSwapHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                             unsigned long long kb G_GNUC_UNUSED)
3174 3175 3176 3177 3178 3179 3180 3181
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3182 3183
virCgroupGetMemSwapHardLimit(virCgroupPtr group G_GNUC_UNUSED,
                             unsigned long long *kb G_GNUC_UNUSED)
3184 3185 3186 3187 3188 3189 3190 3191
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3192 3193
virCgroupGetMemSwapUsage(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *kb G_GNUC_UNUSED)
3194 3195 3196 3197 3198 3199 3200 3201
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3202 3203
virCgroupSetCpusetMems(virCgroupPtr group G_GNUC_UNUSED,
                       const char *mems G_GNUC_UNUSED)
3204 3205 3206 3207 3208 3209 3210 3211
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3212 3213
virCgroupGetCpusetMems(virCgroupPtr group G_GNUC_UNUSED,
                       char **mems G_GNUC_UNUSED)
3214 3215 3216 3217 3218 3219
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3220 3221

int
J
Ján Tomko 已提交
3222 3223
virCgroupSetCpusetMemoryMigrate(virCgroupPtr group G_GNUC_UNUSED,
                                bool migrate G_GNUC_UNUSED)
3224 3225 3226 3227 3228 3229 3230 3231
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3232 3233
virCgroupGetCpusetMemoryMigrate(virCgroupPtr group G_GNUC_UNUSED,
                                bool *migrate G_GNUC_UNUSED)
3234 3235 3236 3237 3238 3239
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3240 3241

int
J
Ján Tomko 已提交
3242 3243
virCgroupSetCpusetCpus(virCgroupPtr group G_GNUC_UNUSED,
                       const char *cpus G_GNUC_UNUSED)
3244 3245 3246 3247 3248 3249 3250 3251
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3252 3253
virCgroupGetCpusetCpus(virCgroupPtr group G_GNUC_UNUSED,
                       char **cpus G_GNUC_UNUSED)
3254 3255 3256 3257 3258 3259
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3260
int
J
Ján Tomko 已提交
3261 3262
virCgroupAllowAllDevices(virCgroupPtr group G_GNUC_UNUSED,
                         int perms G_GNUC_UNUSED)
3263 3264 3265 3266 3267
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}
3268 3269

int
J
Ján Tomko 已提交
3270
virCgroupDenyAllDevices(virCgroupPtr group G_GNUC_UNUSED)
3271 3272 3273 3274 3275 3276 3277 3278
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3279 3280 3281 3282 3283
virCgroupAllowDevice(virCgroupPtr group G_GNUC_UNUSED,
                     char type G_GNUC_UNUSED,
                     int major G_GNUC_UNUSED,
                     int minor G_GNUC_UNUSED,
                     int perms G_GNUC_UNUSED)
3284 3285 3286 3287 3288 3289 3290
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3291
int
J
Ján Tomko 已提交
3292 3293 3294 3295
virCgroupAllowDevicePath(virCgroupPtr group G_GNUC_UNUSED,
                         const char *path G_GNUC_UNUSED,
                         int perms G_GNUC_UNUSED,
                         bool ignoreEaccess G_GNUC_UNUSED)
3296 3297 3298 3299 3300 3301 3302
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3303
int
J
Ján Tomko 已提交
3304 3305 3306 3307 3308
virCgroupDenyDevice(virCgroupPtr group G_GNUC_UNUSED,
                    char type G_GNUC_UNUSED,
                    int major G_GNUC_UNUSED,
                    int minor G_GNUC_UNUSED,
                    int perms G_GNUC_UNUSED)
3309 3310 3311 3312 3313 3314 3315
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3316
int
J
Ján Tomko 已提交
3317 3318 3319 3320
virCgroupDenyDevicePath(virCgroupPtr group G_GNUC_UNUSED,
                        const char *path G_GNUC_UNUSED,
                        int perms G_GNUC_UNUSED,
                        bool ignoreEacces G_GNUC_UNUSED)
3321 3322 3323 3324 3325 3326 3327
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3328
int
J
Ján Tomko 已提交
3329 3330
virCgroupSetCpuShares(virCgroupPtr group G_GNUC_UNUSED,
                      unsigned long long shares G_GNUC_UNUSED)
3331 3332 3333 3334 3335 3336 3337 3338
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3339 3340
virCgroupGetCpuShares(virCgroupPtr group G_GNUC_UNUSED,
                      unsigned long long *shares G_GNUC_UNUSED)
3341 3342 3343 3344 3345 3346 3347 3348
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3349 3350
virCgroupSetCpuCfsPeriod(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long cfs_period G_GNUC_UNUSED)
3351 3352 3353 3354 3355 3356 3357 3358
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3359 3360
virCgroupGetCpuCfsPeriod(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *cfs_period G_GNUC_UNUSED)
3361 3362 3363 3364 3365 3366 3367 3368
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3369 3370
virCgroupSetCpuCfsQuota(virCgroupPtr group G_GNUC_UNUSED,
                        long long cfs_quota G_GNUC_UNUSED)
3371 3372 3373 3374 3375 3376 3377 3378
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3379
virCgroupRemove(virCgroupPtr group G_GNUC_UNUSED)
3380 3381 3382 3383 3384 3385 3386
{
    virReportSystemError(ENXIO, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3387
int
J
Ján Tomko 已提交
3388 3389
virCgroupKillRecursive(virCgroupPtr group G_GNUC_UNUSED,
                       int signum G_GNUC_UNUSED)
3390 3391 3392 3393 3394 3395 3396 3397
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3398
virCgroupKillPainfully(virCgroupPtr group G_GNUC_UNUSED)
3399 3400 3401 3402 3403 3404 3405
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3406
int
J
Ján Tomko 已提交
3407 3408
virCgroupGetCpuCfsQuota(virCgroupPtr group G_GNUC_UNUSED,
                        long long *cfs_quota G_GNUC_UNUSED)
3409 3410 3411 3412 3413 3414 3415 3416
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3417 3418
virCgroupGetCpuacctUsage(virCgroupPtr group G_GNUC_UNUSED,
                         unsigned long long *usage G_GNUC_UNUSED)
3419 3420 3421 3422 3423 3424 3425 3426
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3427 3428
virCgroupGetCpuacctPercpuUsage(virCgroupPtr group G_GNUC_UNUSED,
                               char **usage G_GNUC_UNUSED)
3429 3430 3431 3432 3433 3434 3435 3436
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3437 3438 3439
virCgroupGetCpuacctStat(virCgroupPtr group G_GNUC_UNUSED,
                        unsigned long long *user G_GNUC_UNUSED,
                        unsigned long long *sys G_GNUC_UNUSED)
3440 3441 3442 3443 3444 3445 3446
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3447
int
J
Ján Tomko 已提交
3448 3449 3450
virCgroupGetDomainTotalCpuStats(virCgroupPtr group G_GNUC_UNUSED,
                                virTypedParameterPtr params G_GNUC_UNUSED,
                                int nparams G_GNUC_UNUSED)
3451 3452 3453 3454 3455 3456 3457
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


3458
int
J
Ján Tomko 已提交
3459 3460
virCgroupSetFreezerState(virCgroupPtr group G_GNUC_UNUSED,
                         const char *state G_GNUC_UNUSED)
3461 3462 3463 3464 3465 3466 3467 3468
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3469 3470
virCgroupGetFreezerState(virCgroupPtr group G_GNUC_UNUSED,
                         char **state G_GNUC_UNUSED)
3471 3472 3473 3474 3475 3476 3477
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


E
Eric Blake 已提交
3478
int
J
Ján Tomko 已提交
3479 3480 3481
virCgroupBindMount(virCgroupPtr group G_GNUC_UNUSED,
                   const char *oldroot G_GNUC_UNUSED,
                   const char *mountopts G_GNUC_UNUSED)
3482
{
3483 3484 3485
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
3486
}
3487

3488 3489

bool
J
Ján Tomko 已提交
3490
virCgroupSupportsCpuBW(virCgroupPtr cgroup G_GNUC_UNUSED)
3491 3492 3493 3494 3495
{
    VIR_DEBUG("Control groups not supported on this platform");
    return false;
}

E
Eric Blake 已提交
3496 3497

int
J
Ján Tomko 已提交
3498 3499 3500 3501 3502 3503
virCgroupGetPercpuStats(virCgroupPtr group G_GNUC_UNUSED,
                        virTypedParameterPtr params G_GNUC_UNUSED,
                        unsigned int nparams G_GNUC_UNUSED,
                        int start_cpu G_GNUC_UNUSED,
                        unsigned int ncpus G_GNUC_UNUSED,
                        virBitmapPtr guestvcpus G_GNUC_UNUSED)
E
Eric Blake 已提交
3504 3505 3506 3507 3508 3509 3510 3511
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}


int
J
Ján Tomko 已提交
3512 3513 3514 3515
virCgroupSetOwner(virCgroupPtr cgroup G_GNUC_UNUSED,
                  uid_t uid G_GNUC_UNUSED,
                  gid_t gid G_GNUC_UNUSED,
                  int controllers G_GNUC_UNUSED)
E
Eric Blake 已提交
3516 3517 3518 3519 3520 3521
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3522
int
J
Ján Tomko 已提交
3523 3524
virCgroupHasEmptyTasks(virCgroupPtr cgroup G_GNUC_UNUSED,
                       int controller G_GNUC_UNUSED)
3525 3526 3527 3528 3529 3530
{
    virReportSystemError(ENOSYS, "%s",
                         _("Control groups not supported on this platform"));
    return -1;
}

3531
bool
J
Ján Tomko 已提交
3532
virCgroupControllerAvailable(int controller G_GNUC_UNUSED)
3533 3534 3535
{
    return false;
}
3536
#endif /* !__linux__ */
3537 3538


3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565
/**
 * virCgroupFree:
 *
 * @group: The group structure to free
 */
void
virCgroupFree(virCgroupPtr *group)
{
    size_t i;

    if (*group == NULL)
        return;

    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
        VIR_FREE((*group)->legacy[i].mountPoint);
        VIR_FREE((*group)->legacy[i].linkPoint);
        VIR_FREE((*group)->legacy[i].placement);
    }

    VIR_FREE((*group)->unified.mountPoint);
    VIR_FREE((*group)->unified.placement);

    VIR_FREE((*group)->path);
    VIR_FREE(*group);
}


3566 3567 3568 3569 3570
int
virCgroupDelThread(virCgroupPtr cgroup,
                   virCgroupThreadName nameval,
                   int idx)
{
3571
    virCgroupPtr new_cgroup = NULL;
3572 3573 3574 3575 3576 3577 3578

    if (cgroup) {
        if (virCgroupNewThread(cgroup, nameval, idx, false, &new_cgroup) < 0)
            return -1;

        /* Remove the offlined cgroup */
        virCgroupRemove(new_cgroup);
3579
        virCgroupFree(&new_cgroup);
3580 3581 3582 3583
    }

    return 0;
}
3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668


/**
 * Calls virCgroupSetBlkioDeviceWeight() to set up blkio device weight,
 * then retrieves the actual value set by the kernel with
 * virCgroupGetBlkioDeviceWeight() in the same @weight pointer.
 */
int
virCgroupSetupBlkioDeviceWeight(virCgroupPtr cgroup, const char *path,
                                unsigned int *weight)
{
    if (virCgroupSetBlkioDeviceWeight(cgroup, path, *weight) < 0 ||
        virCgroupGetBlkioDeviceWeight(cgroup, path, weight) < 0)
        return -1;

    return 0;
}


/**
 * Calls virCgroupSetBlkioDeviceReadIops() to set up blkio device riops,
 * then retrieves the actual value set by the kernel with
 * virCgroupGetBlkioDeviceReadIops() in the same @riops pointer.
 */
int
virCgroupSetupBlkioDeviceReadIops(virCgroupPtr cgroup, const char *path,
                                  unsigned int *riops)
{
    if (virCgroupSetBlkioDeviceReadIops(cgroup, path, *riops) < 0 ||
        virCgroupGetBlkioDeviceReadIops(cgroup, path, riops) < 0)
        return -1;

    return 0;
}


/**
 * Calls virCgroupSetBlkioDeviceWriteIops() to set up blkio device wiops,
 * then retrieves the actual value set by the kernel with
 * virCgroupGetBlkioDeviceWriteIops() in the same @wiops pointer.
 */
int
virCgroupSetupBlkioDeviceWriteIops(virCgroupPtr cgroup, const char *path,
                                   unsigned int *wiops)
{
    if (virCgroupSetBlkioDeviceWriteIops(cgroup, path, *wiops) < 0 ||
        virCgroupGetBlkioDeviceWriteIops(cgroup, path, wiops) < 0)
        return -1;

    return 0;
}


/**
 * Calls virCgroupSetBlkioDeviceReadBps() to set up blkio device rbps,
 * then retrieves the actual value set by the kernel with
 * virCgroupGetBlkioDeviceReadBps() in the same @rbps pointer.
 */
int
virCgroupSetupBlkioDeviceReadBps(virCgroupPtr cgroup, const char *path,
                                 unsigned long long *rbps)
{
    if (virCgroupSetBlkioDeviceReadBps(cgroup, path, *rbps) < 0 ||
        virCgroupGetBlkioDeviceReadBps(cgroup, path, rbps) < 0)
        return -1;

    return 0;
}


/**
 * Calls virCgroupSetBlkioDeviceWriteBps() to set up blkio device wbps,
 * then retrieves the actual value set by the kernel with
 * virCgroupGetBlkioDeviceWriteBps() in the same @wbps pointer.
 */
int
virCgroupSetupBlkioDeviceWriteBps(virCgroupPtr cgroup, const char *path,
                                  unsigned long long *wbps)
{
    if (virCgroupSetBlkioDeviceWriteBps(cgroup, path, *wbps) < 0 ||
        virCgroupGetBlkioDeviceWriteBps(cgroup, path, wbps) < 0)
        return -1;

    return 0;
}
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683


int
virCgroupSetupCpusetCpus(virCgroupPtr cgroup, virBitmapPtr cpumask)
{
    g_autofree char *new_cpus = NULL;

    if (!(new_cpus = virBitmapFormat(cpumask)))
        return -1;

    if (virCgroupSetCpusetCpus(cgroup, new_cpus) < 0)
        return -1;

    return 0;
}