2018-09-18 15:48:33 +00:00
|
|
|
/*
|
|
|
|
* vircgroupv2.c: methods for cgroups v2 backend
|
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-08-17 14:28:11 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
# include <mntent.h>
|
2018-09-28 18:40:11 +00:00
|
|
|
# include <sys/mount.h>
|
2018-08-17 14:28:11 +00:00
|
|
|
#endif /* __linux__ */
|
|
|
|
|
2018-09-18 15:48:33 +00:00
|
|
|
#include "internal.h"
|
|
|
|
|
2018-12-13 14:53:50 +00:00
|
|
|
#define LIBVIRT_VIRCGROUPPRIV_H_ALLOW
|
2018-09-18 15:48:33 +00:00
|
|
|
#include "vircgrouppriv.h"
|
|
|
|
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2018-09-18 15:48:33 +00:00
|
|
|
#include "vircgroup.h"
|
|
|
|
#include "vircgroupbackend.h"
|
|
|
|
#include "vircgroupv2.h"
|
2018-09-25 12:37:21 +00:00
|
|
|
#include "virerror.h"
|
2018-08-17 14:28:11 +00:00
|
|
|
#include "virfile.h"
|
2018-09-18 15:48:33 +00:00
|
|
|
#include "virlog.h"
|
2018-08-17 14:28:11 +00:00
|
|
|
#include "virstring.h"
|
2018-09-14 16:19:53 +00:00
|
|
|
#include "virsystemd.h"
|
2018-09-18 15:48:33 +00:00
|
|
|
|
|
|
|
VIR_LOG_INIT("util.cgroup");
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_CGROUP
|
|
|
|
|
|
|
|
VIR_ENUM_DECL(virCgroupV2Controller);
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virCgroupV2Controller,
|
|
|
|
VIR_CGROUP_CONTROLLER_LAST,
|
2018-09-18 15:48:33 +00:00
|
|
|
"cpu", "cpuacct", "cpuset", "memory", "devices",
|
2019-01-20 16:30:15 +00:00
|
|
|
"freezer", "io", "net_cls", "perf_event", "name=systemd",
|
|
|
|
);
|
2018-09-18 15:48:33 +00:00
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
2018-08-17 14:28:11 +00:00
|
|
|
/* We're looking for one 'cgroup2' fs mount which has some
|
|
|
|
* controllers enabled. */
|
|
|
|
static bool
|
|
|
|
virCgroupV2Available(void)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
FILE *mounts = NULL;
|
|
|
|
struct mntent entry;
|
|
|
|
char buf[CGROUP_MAX_VAL];
|
|
|
|
|
|
|
|
if (!(mounts = fopen("/proc/mounts", "r")))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (getmntent_r(mounts, &entry, buf, sizeof(buf)) != NULL) {
|
|
|
|
VIR_AUTOFREE(char *) contFile = NULL;
|
|
|
|
VIR_AUTOFREE(char *) contStr = NULL;
|
|
|
|
|
|
|
|
if (STRNEQ(entry.mnt_type, "cgroup2"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Systemd uses cgroup v2 for process tracking but no controller is
|
|
|
|
* available. We should consider this configuration as cgroup v2 is
|
|
|
|
* not available. */
|
|
|
|
if (virAsprintf(&contFile, "%s/cgroup.controllers", entry.mnt_dir) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virFileReadAll(contFile, 1024 * 1024, &contStr) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (STREQ(contStr, ""))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_FCLOSE(mounts);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-14 16:19:53 +00:00
|
|
|
static bool
|
|
|
|
virCgroupV2ValidateMachineGroup(virCgroupPtr group,
|
|
|
|
const char *name ATTRIBUTE_UNUSED,
|
|
|
|
const char *drivername,
|
|
|
|
const char *machinename)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) partmachinename = NULL;
|
|
|
|
VIR_AUTOFREE(char *) scopename = NULL;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (virAsprintf(&partmachinename, "%s.libvirt-%s", machinename,
|
|
|
|
drivername) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCgroupPartitionEscape(&partmachinename) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(scopename = virSystemdMakeScopeName(machinename, drivername,
|
|
|
|
false))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCgroupPartitionEscape(&scopename) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(tmp = strrchr(group->unified.placement, '/')))
|
|
|
|
return false;
|
2018-12-11 09:35:20 +00:00
|
|
|
|
|
|
|
if (STREQ(tmp, "/emulator")) {
|
|
|
|
*tmp = '\0';
|
|
|
|
|
|
|
|
if (!(tmp = strrchr(group->unified.placement, '/')))
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-14 16:19:53 +00:00
|
|
|
tmp++;
|
|
|
|
|
|
|
|
if (STRNEQ(tmp, partmachinename) &&
|
|
|
|
STRNEQ(tmp, scopename)) {
|
|
|
|
VIR_DEBUG("Name '%s' for unified does not match '%s' or '%s'",
|
|
|
|
tmp, partmachinename, scopename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:42:17 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2CopyMounts(virCgroupPtr group,
|
|
|
|
virCgroupPtr parent)
|
|
|
|
{
|
|
|
|
return VIR_STRDUP(group->unified.mountPoint, parent->unified.mountPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-25 12:34:01 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2CopyPlacement(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
virCgroupPtr parent)
|
|
|
|
{
|
2019-09-26 19:25:52 +00:00
|
|
|
VIR_DEBUG("group=%p path=%s parent=%p", group, path, parent);
|
|
|
|
|
2018-09-25 12:34:01 +00:00
|
|
|
if (path[0] == '/') {
|
|
|
|
if (VIR_STRDUP(group->unified.placement, path) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* parent == "/" + path="" => "/"
|
|
|
|
* parent == "/libvirt.service" + path == "" => "/libvirt.service"
|
|
|
|
* parent == "/libvirt.service" + path == "foo" => "/libvirt.service/foo"
|
|
|
|
*/
|
|
|
|
if (virAsprintf(&group->unified.placement, "%s%s%s",
|
|
|
|
parent->unified.placement,
|
|
|
|
(STREQ(parent->unified.placement, "/") ||
|
|
|
|
STREQ(path, "") ? "" : "/"),
|
|
|
|
path) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-25 12:34:47 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2DetectMounts(virCgroupPtr group,
|
|
|
|
const char *mntType,
|
|
|
|
const char *mntOpts ATTRIBUTE_UNUSED,
|
|
|
|
const char *mntDir)
|
|
|
|
{
|
|
|
|
if (STRNEQ(mntType, "cgroup2"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
VIR_FREE(group->unified.mountPoint);
|
|
|
|
|
|
|
|
return VIR_STRDUP(group->unified.mountPoint, mntDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-14 21:46:42 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2DetectPlacement(virCgroupPtr group,
|
|
|
|
const char *path,
|
2019-09-26 19:00:55 +00:00
|
|
|
const char *controllers,
|
2018-09-14 21:46:42 +00:00
|
|
|
const char *selfpath)
|
|
|
|
{
|
|
|
|
if (group->unified.placement)
|
|
|
|
return 0;
|
|
|
|
|
2019-09-26 19:25:52 +00:00
|
|
|
VIR_DEBUG("group=%p path=%s controllers=%s selfpath=%s",
|
|
|
|
group, path, controllers, selfpath);
|
|
|
|
|
2019-09-26 19:00:55 +00:00
|
|
|
/* controllers == "" indicates the cgroupv2 controller path */
|
|
|
|
if (STRNEQ(controllers, ""))
|
|
|
|
return 0;
|
|
|
|
|
2018-09-14 21:46:42 +00:00
|
|
|
/*
|
|
|
|
* selfpath == "/" + path="" -> "/"
|
|
|
|
* selfpath == "/libvirt.service" + path == "" -> "/libvirt.service"
|
|
|
|
* selfpath == "/libvirt.service" + path == "foo" -> "/libvirt.service/foo"
|
|
|
|
*/
|
|
|
|
if (virAsprintf(&group->unified.placement,
|
|
|
|
"%s%s%s", selfpath,
|
|
|
|
(STREQ(selfpath, "/") ||
|
|
|
|
STREQ(path, "") ? "" : "/"),
|
|
|
|
path) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-18 07:17:24 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2ValidatePlacement(virCgroupPtr group,
|
|
|
|
pid_t pid ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
if (!group->unified.placement) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Could not find placement for v2 controller"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-18 07:21:53 +00:00
|
|
|
static char *
|
|
|
|
virCgroupV2StealPlacement(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
VIR_STEAL_PTR(ret, group->unified.placement);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-25 12:37:21 +00:00
|
|
|
static int
|
2019-07-22 12:37:36 +00:00
|
|
|
virCgroupV2ParseControllersFile(virCgroupPtr group,
|
|
|
|
virCgroupPtr parent)
|
2018-09-25 12:37:21 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
VIR_AUTOFREE(char *) contStr = NULL;
|
|
|
|
VIR_AUTOFREE(char *) contFile = NULL;
|
|
|
|
char **contList = NULL;
|
|
|
|
char **tmp;
|
|
|
|
|
2019-07-22 12:37:36 +00:00
|
|
|
if (parent) {
|
|
|
|
if (virAsprintf(&contFile, "%s%s/cgroup.subtree_control",
|
|
|
|
parent->unified.mountPoint,
|
|
|
|
NULLSTR_EMPTY(parent->unified.placement)) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&contFile, "%s%s/cgroup.controllers",
|
|
|
|
group->unified.mountPoint,
|
|
|
|
NULLSTR_EMPTY(group->unified.placement)) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-25 12:37:21 +00:00
|
|
|
|
|
|
|
rc = virFileReadAll(contFile, 1024 * 1024, &contStr);
|
|
|
|
if (rc < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to read from '%s'"), contFile);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virTrimSpaces(contStr, NULL);
|
|
|
|
|
|
|
|
contList = virStringSplit(contStr, " ", 20);
|
|
|
|
if (!contList)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tmp = contList;
|
|
|
|
|
|
|
|
while (*tmp) {
|
|
|
|
int type = virCgroupV2ControllerTypeFromString(*tmp);
|
|
|
|
|
|
|
|
if (type >= 0)
|
|
|
|
group->unified.controllers |= 1 << type;
|
|
|
|
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
virStringListFree(contList);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2DetectControllers(virCgroupPtr group,
|
2019-07-22 12:37:36 +00:00
|
|
|
int controllers,
|
|
|
|
virCgroupPtr parent)
|
2018-09-25 12:37:21 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2019-07-22 12:37:36 +00:00
|
|
|
if (virCgroupV2ParseControllersFile(group, parent) < 0)
|
2019-06-27 13:50:22 +00:00
|
|
|
return -1;
|
2018-09-25 12:37:21 +00:00
|
|
|
|
2019-06-27 13:50:22 +00:00
|
|
|
/* In cgroup v2 there is no cpuacct controller, the cpu.stat file always
|
|
|
|
* exists with usage stats. */
|
|
|
|
group->unified.controllers |= 1 << VIR_CGROUP_CONTROLLER_CPUACCT;
|
2018-09-25 12:37:21 +00:00
|
|
|
|
2019-06-22 11:46:22 +00:00
|
|
|
if (controllers >= 0)
|
|
|
|
group->unified.controllers &= controllers;
|
|
|
|
|
2018-09-25 12:37:21 +00:00
|
|
|
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++)
|
|
|
|
VIR_DEBUG("Controller '%s' present=%s",
|
|
|
|
virCgroupV2ControllerTypeToString(i),
|
|
|
|
(group->unified.controllers & 1 << i) ? "yes" : "no");
|
|
|
|
|
2019-06-22 11:46:22 +00:00
|
|
|
return group->unified.controllers;
|
2018-09-25 12:37:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 17:40:15 +00:00
|
|
|
static bool
|
|
|
|
virCgroupV2HasController(virCgroupPtr group,
|
|
|
|
int controller)
|
|
|
|
{
|
|
|
|
return group->unified.controllers & (1 << controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 17:43:04 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetAnyController(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
/* The least significant bit is position 1. */
|
2019-10-03 14:51:30 +00:00
|
|
|
return __builtin_ffs(group->unified.controllers) - 1;
|
2018-08-19 17:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-19 17:45:49 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2PathOfController(virCgroupPtr group,
|
|
|
|
int controller,
|
|
|
|
const char *key,
|
|
|
|
char **path)
|
|
|
|
{
|
|
|
|
if (!virCgroupV2HasController(group, controller)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("v2 controller '%s' is not available"),
|
|
|
|
virCgroupV2ControllerTypeToString(controller));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virAsprintf(path, "%s%s/%s",
|
|
|
|
group->unified.mountPoint,
|
|
|
|
group->unified.placement,
|
2019-02-12 16:25:06 +00:00
|
|
|
NULLSTR_EMPTY(key)) < 0)
|
2018-08-19 17:45:49 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-21 12:47:47 +00:00
|
|
|
/**
|
|
|
|
* virCgroupV2EnableController:
|
|
|
|
*
|
|
|
|
* Returns: -1 on fatal error
|
|
|
|
* -2 if we failed to write into cgroup.subtree_control
|
|
|
|
* 0 on success
|
|
|
|
*/
|
2018-09-18 15:49:19 +00:00
|
|
|
static int
|
2019-07-22 12:44:05 +00:00
|
|
|
virCgroupV2EnableController(virCgroupPtr group,
|
|
|
|
virCgroupPtr parent,
|
2019-06-21 12:47:47 +00:00
|
|
|
int controller,
|
|
|
|
bool report)
|
2018-09-18 15:49:19 +00:00
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) val = NULL;
|
2019-06-21 12:47:47 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-09-18 15:49:19 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&val, "+%s",
|
|
|
|
virCgroupV2ControllerTypeToString(controller)) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:47:47 +00:00
|
|
|
if (virCgroupPathOfController(parent, controller,
|
|
|
|
"cgroup.subtree_control", &path) < 0) {
|
2018-09-18 15:49:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-21 12:47:47 +00:00
|
|
|
if (virFileWriteStr(path, val, 0) < 0) {
|
|
|
|
if (report) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to enable controller '%s' for '%s'"),
|
|
|
|
val, path);
|
|
|
|
}
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
2019-07-22 12:44:05 +00:00
|
|
|
group->unified.controllers |= 1 << controller;
|
|
|
|
|
2018-09-18 15:49:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2019-07-22 12:46:34 +00:00
|
|
|
virCgroupV2MakeGroup(virCgroupPtr parent,
|
2018-09-18 15:49:19 +00:00
|
|
|
virCgroupPtr group,
|
|
|
|
bool create,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
|
|
|
int controller;
|
|
|
|
|
2019-06-27 12:54:54 +00:00
|
|
|
if (flags & VIR_CGROUP_SYSTEMD) {
|
|
|
|
VIR_DEBUG("Running with systemd so we should not create cgroups ourselves.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:49:19 +00:00
|
|
|
VIR_DEBUG("Make group %s", group->path);
|
|
|
|
|
|
|
|
controller = virCgroupV2GetAnyController(group);
|
|
|
|
if (virCgroupV2PathOfController(group, controller, "", &path) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
VIR_DEBUG("Make controller %s", path);
|
|
|
|
|
|
|
|
if (!virFileExists(path) &&
|
|
|
|
(!create || (mkdir(path, 0755) < 0 && errno != EEXIST))) {
|
|
|
|
virReportSystemError(errno, _("Failed to create v2 cgroup '%s'"),
|
|
|
|
group->path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create) {
|
|
|
|
if (flags & VIR_CGROUP_THREAD) {
|
2019-06-20 11:07:07 +00:00
|
|
|
if (virCgroupSetValueStr(group, controller,
|
2018-09-18 15:49:19 +00:00
|
|
|
"cgroup.type", "threaded") < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:09:08 +00:00
|
|
|
if (virCgroupV2HasController(parent, VIR_CGROUP_CONTROLLER_CPU) &&
|
2019-07-22 12:44:05 +00:00
|
|
|
virCgroupV2EnableController(group, parent,
|
2019-06-21 12:47:47 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
true) < 0) {
|
2018-09-18 15:49:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-02-20 12:50:23 +00:00
|
|
|
|
|
|
|
if (virCgroupV2HasController(parent, VIR_CGROUP_CONTROLLER_CPUSET) &&
|
2019-07-22 12:44:05 +00:00
|
|
|
virCgroupV2EnableController(group, parent,
|
2019-06-21 12:47:47 +00:00
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
true) < 0) {
|
2019-02-20 12:50:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-09-18 15:49:19 +00:00
|
|
|
} else {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
|
2019-06-20 11:10:20 +00:00
|
|
|
int rc;
|
|
|
|
|
2018-09-18 15:49:19 +00:00
|
|
|
if (!virCgroupV2HasController(parent, i))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Controllers that are implicitly enabled if available. */
|
|
|
|
if (i == VIR_CGROUP_CONTROLLER_CPUACCT)
|
|
|
|
continue;
|
|
|
|
|
2019-07-22 12:44:05 +00:00
|
|
|
rc = virCgroupV2EnableController(group, parent, i, false);
|
2019-06-20 11:10:20 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
if (rc == -2) {
|
|
|
|
virResetLastError();
|
|
|
|
VIR_DEBUG("failed to enable '%s' controller, skipping",
|
|
|
|
virCgroupV2ControllerTypeToString(i));
|
|
|
|
group->unified.controllers &= ~(1 << i);
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-18 15:49:19 +00:00
|
|
|
return -1;
|
2019-06-20 11:10:20 +00:00
|
|
|
}
|
2018-09-18 15:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:49:08 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2Remove(virCgroupPtr group)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) grppath = NULL;
|
|
|
|
int controller;
|
|
|
|
|
|
|
|
/* Don't delete the root group, if we accidentally
|
|
|
|
ended up in it for some reason */
|
|
|
|
if (STREQ(group->unified.placement, "/"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
controller = virCgroupV2GetAnyController(group);
|
|
|
|
if (virCgroupV2PathOfController(group, controller, "", &grppath) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return virCgroupRemoveRecursively(grppath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:49:33 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2AddTask(virCgroupPtr group,
|
|
|
|
pid_t pid,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
int controller = virCgroupV2GetAnyController(group);
|
|
|
|
|
|
|
|
if (flags & VIR_CGROUP_TASK_THREAD)
|
|
|
|
return virCgroupSetValueI64(group, controller, "cgroup.threads", pid);
|
|
|
|
else
|
|
|
|
return virCgroupSetValueI64(group, controller, "cgroup.procs", pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:49:57 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2HasEmptyTasks(virCgroupPtr cgroup,
|
|
|
|
int controller)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
VIR_AUTOFREE(char *) content = NULL;
|
|
|
|
|
|
|
|
ret = virCgroupGetValueStr(cgroup, controller, "cgroup.procs", &content);
|
|
|
|
|
|
|
|
if (ret == 0 && content[0] == '\0')
|
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-10 08:51:14 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2KillRecursive(virCgroupPtr group,
|
|
|
|
int signum,
|
|
|
|
virHashTablePtr pids)
|
|
|
|
{
|
|
|
|
int controller = virCgroupV2GetAnyController(group);
|
|
|
|
|
|
|
|
if (controller < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return virCgroupKillRecursiveInternal(group, signum, pids, controller,
|
|
|
|
"cgroup.threads", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-28 18:40:11 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2BindMount(virCgroupPtr group,
|
|
|
|
const char *oldroot,
|
|
|
|
const char *mountopts)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) opts = NULL;
|
|
|
|
VIR_AUTOFREE(char *) src = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("Mounting cgroups at '%s'", group->unified.mountPoint);
|
|
|
|
|
|
|
|
if (virFileMakePath(group->unified.mountPoint) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to create directory %s"),
|
|
|
|
group->unified.mountPoint);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virAsprintf(&opts, "mode=755,size=65536%s", mountopts) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virAsprintf(&src, "%s%s", oldroot, group->unified.mountPoint) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (mount(src, group->unified.mountPoint, "none", MS_BIND, NULL) < 0) {
|
|
|
|
virReportSystemError(errno, _("Failed to bind cgroup '%s' on '%s'"),
|
|
|
|
src, group->unified.mountPoint);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:50:57 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetOwner(virCgroupPtr cgroup,
|
|
|
|
uid_t uid,
|
|
|
|
gid_t gid,
|
|
|
|
int controllers ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) base = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&base, "%s%s", cgroup->unified.mountPoint,
|
|
|
|
cgroup->unified.placement) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileChownFiles(base, uid, gid) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (chown(base, uid, gid) < 0) {
|
|
|
|
virReportSystemError(errno, _("cannot chown '%s' to (%u, %u)"),
|
|
|
|
base, uid, gid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:51:28 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioWeight(virCgroupPtr group,
|
|
|
|
unsigned int weight)
|
|
|
|
{
|
2019-06-18 13:19:43 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-08-17 14:51:28 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2019-06-18 13:19:43 +00:00
|
|
|
const char *format = "%u";
|
2018-08-17 14:51:28 +00:00
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.bfq.weight", &path) < 0) {
|
2018-08-17 14:51:28 +00:00
|
|
|
return -1;
|
2019-06-18 13:19:43 +00:00
|
|
|
}
|
2018-08-17 14:51:28 +00:00
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (!virFileExists(path)) {
|
|
|
|
VIR_FREE(path);
|
|
|
|
format = "default %u";
|
|
|
|
|
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.weight", &path) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileExists(path)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("blkio weight is valid only for bfq or cfq scheduler"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virAsprintf(&value, format, weight) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return virCgroupSetValueRaw(path, value);
|
2018-08-17 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioWeight(virCgroupPtr group,
|
|
|
|
unsigned int *weight)
|
|
|
|
{
|
2019-06-18 13:19:43 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-08-17 14:51:28 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
|
|
|
char *tmp;
|
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.bfq.weight", &path) < 0) {
|
2018-08-17 14:51:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (!virFileExists(path)) {
|
|
|
|
VIR_FREE(path);
|
|
|
|
|
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.weight", &path) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileExists(path)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("blkio weight is valid only for bfq or cfq scheduler"));
|
2018-08-17 14:51:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-06-18 13:19:43 +00:00
|
|
|
|
|
|
|
if (virCgroupGetValueRaw(path, &value) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((tmp = strstr(value, "default "))) {
|
|
|
|
tmp += strlen("default ");
|
|
|
|
} else {
|
|
|
|
tmp = value;
|
|
|
|
}
|
2018-08-17 14:51:28 +00:00
|
|
|
|
2019-08-19 14:01:50 +00:00
|
|
|
if (virStrToLong_ui(tmp, &tmp, 10, weight) < 0) {
|
2018-08-17 14:51:28 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:51:54 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioIoServiced(virCgroupPtr group,
|
|
|
|
long long *bytes_read,
|
|
|
|
long long *bytes_write,
|
|
|
|
long long *requests_read,
|
|
|
|
long long *requests_write)
|
|
|
|
{
|
|
|
|
long long stats_val;
|
|
|
|
VIR_AUTOFREE(char *) str1 = NULL;
|
|
|
|
char *p1;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const char *value_names[] = {
|
|
|
|
"rbytes=",
|
|
|
|
"wbytes=",
|
|
|
|
"rios=",
|
|
|
|
"wios=",
|
|
|
|
};
|
|
|
|
long long *value_ptrs[] = {
|
|
|
|
bytes_read,
|
|
|
|
bytes_write,
|
|
|
|
requests_read,
|
|
|
|
requests_write
|
|
|
|
};
|
|
|
|
|
|
|
|
*bytes_read = 0;
|
|
|
|
*bytes_write = 0;
|
|
|
|
*requests_read = 0;
|
|
|
|
*requests_write = 0;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.stat", &str1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sum up all entries of the same kind, from all devices */
|
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(value_names); i++) {
|
|
|
|
p1 = str1;
|
|
|
|
|
|
|
|
while ((p1 = strstr(p1, value_names[i]))) {
|
|
|
|
p1 += strlen(value_names[i]);
|
|
|
|
if (virStrToLong_ll(p1, &p1, 10, &stats_val) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse byte '%s' stat '%s'"),
|
|
|
|
value_names[i], p1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats_val < 0 ||
|
|
|
|
(stats_val > 0 && *value_ptrs[i] > (LLONG_MAX - stats_val))) {
|
|
|
|
virReportError(VIR_ERR_OVERFLOW,
|
|
|
|
_("Sum of byte '%s' stat overflows"),
|
|
|
|
value_names[i]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*value_ptrs[i] += stats_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:52:24 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioIoDeviceServiced(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
long long *bytes_read,
|
|
|
|
long long *bytes_write,
|
|
|
|
long long *requests_read,
|
|
|
|
long long *requests_write)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str1 = NULL;
|
|
|
|
VIR_AUTOFREE(char *) str2 = NULL;
|
|
|
|
char *p1;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const char *value_names[] = {
|
|
|
|
"rbytes=",
|
|
|
|
"wbytes=",
|
|
|
|
"rios=",
|
|
|
|
"wios=",
|
|
|
|
};
|
|
|
|
long long *value_ptrs[] = {
|
|
|
|
bytes_read,
|
|
|
|
bytes_write,
|
|
|
|
requests_read,
|
|
|
|
requests_write
|
|
|
|
};
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.stat", &str1) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(str2 = virCgroupGetBlockDevString(path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!(p1 = strstr(str1, str2))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find byte stats for block device '%s'"),
|
|
|
|
str2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_CARDINALITY(value_names); i++) {
|
|
|
|
if (!(p1 = strstr(p1, value_names[i]))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot find byte '%s' stats for block device '%s'"),
|
|
|
|
value_names[i], str2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p1 += strlen(value_names[i]);
|
|
|
|
if (virStrToLong_ll(p1, &p1, 10, value_ptrs[i]) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Cannot parse '%s' stat '%s'"),
|
|
|
|
value_names[i], p1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:52:50 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioDeviceWeight(virCgroupPtr group,
|
2019-06-18 13:19:43 +00:00
|
|
|
const char *devPath,
|
2018-08-17 14:52:50 +00:00
|
|
|
unsigned int weight)
|
|
|
|
{
|
2019-06-18 13:19:43 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-08-17 14:52:50 +00:00
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
VIR_AUTOFREE(char *) blkstr = NULL;
|
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (!(blkstr = virCgroupGetBlockDevString(devPath)))
|
2018-08-17 14:52:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virAsprintf(&str, "%s%d", blkstr, weight) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.weight", &path) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!virFileExists(path)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("blkio device weight is valid only for cfq scheduler"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueRaw(path, str);
|
2018-08-17 14:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioDeviceWeight(virCgroupPtr group,
|
2019-06-18 13:19:43 +00:00
|
|
|
const char *devPath,
|
2018-08-17 14:52:50 +00:00
|
|
|
unsigned int *weight)
|
|
|
|
{
|
2019-06-18 13:19:43 +00:00
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
2018-08-17 14:52:50 +00:00
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-06-18 13:24:41 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2019-08-30 14:15:15 +00:00
|
|
|
char *tmp;
|
2018-08-17 14:52:50 +00:00
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.weight", &path) < 0) {
|
2018-08-17 14:52:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:19:43 +00:00
|
|
|
if (!virFileExists(path)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("blkio device weight is valid only for cfq scheduler"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCgroupGetValueRaw(path, &value) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virCgroupGetValueForBlkDev(value, devPath, &str) < 0)
|
2019-06-18 13:24:41 +00:00
|
|
|
return -1;
|
|
|
|
|
2018-08-17 14:52:50 +00:00
|
|
|
if (!str) {
|
|
|
|
*weight = 0;
|
2019-08-30 14:15:15 +00:00
|
|
|
} else if (virStrToLong_ui(str, &tmp, 10, weight) < 0) {
|
2018-08-17 14:52:50 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:53:17 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioDeviceReadIops(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned int riops)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
VIR_AUTOFREE(char *) blkstr = NULL;
|
|
|
|
|
|
|
|
if (!(blkstr = virCgroupGetBlockDevString(path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (riops == 0) {
|
|
|
|
if (virAsprintf(&str, "%sriops=max", blkstr) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&str, "%sriops=%u", blkstr, riops) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioDeviceReadIops(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned int *riops)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-06-18 13:24:41 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2018-08-17 14:53:17 +00:00
|
|
|
const char *name = "riops=";
|
|
|
|
char *tmp;
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
&value) < 0) {
|
2018-08-17 14:53:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueForBlkDev(value, path, &str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-17 14:53:17 +00:00
|
|
|
if (!str) {
|
|
|
|
*riops = 0;
|
|
|
|
} else {
|
|
|
|
if (!(tmp = strstr(str, name))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find '%s' limit for block device '%s'"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen(name);
|
|
|
|
|
|
|
|
if (STREQLEN(tmp, "max", 3)) {
|
|
|
|
*riops = 0;
|
2019-08-19 14:01:50 +00:00
|
|
|
} else if (virStrToLong_ui(tmp, &tmp, 10, riops) < 0) {
|
2018-08-17 14:53:17 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:53:45 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioDeviceWriteIops(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned int wiops)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
VIR_AUTOFREE(char *) blkstr = NULL;
|
|
|
|
|
|
|
|
if (!(blkstr = virCgroupGetBlockDevString(path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (wiops == 0) {
|
|
|
|
if (virAsprintf(&str, "%swiops=max", blkstr) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&str, "%swiops=%u", blkstr, wiops) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioDeviceWriteIops(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned int *wiops)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-06-18 13:24:41 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2018-08-17 14:53:45 +00:00
|
|
|
const char *name = "wiops=";
|
|
|
|
char *tmp;
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
&value) < 0) {
|
2018-08-17 14:53:45 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueForBlkDev(value, path, &str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-17 14:53:45 +00:00
|
|
|
if (!str) {
|
|
|
|
*wiops = 0;
|
|
|
|
} else {
|
|
|
|
if (!(tmp = strstr(str, name))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find '%s' limit for block device '%s'"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen(name);
|
|
|
|
|
|
|
|
if (STREQLEN(tmp, "max", 3)) {
|
|
|
|
*wiops = 0;
|
2019-08-19 14:01:50 +00:00
|
|
|
} else if (virStrToLong_ui(tmp, &tmp, 10, wiops) < 0) {
|
2018-08-17 14:53:45 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:54:11 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioDeviceReadBps(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long rbps)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
VIR_AUTOFREE(char *) blkstr = NULL;
|
|
|
|
|
|
|
|
if (!(blkstr = virCgroupGetBlockDevString(path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rbps == 0) {
|
|
|
|
if (virAsprintf(&str, "%srbps=max", blkstr) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&str, "%srbps=%llu", blkstr, rbps) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioDeviceReadBps(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long *rbps)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-06-18 13:24:41 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2018-08-17 14:54:11 +00:00
|
|
|
const char *name = "rbps=";
|
|
|
|
char *tmp;
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
&value) < 0) {
|
2018-08-17 14:54:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueForBlkDev(value, path, &str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-17 14:54:11 +00:00
|
|
|
if (!str) {
|
|
|
|
*rbps = 0;
|
|
|
|
} else {
|
|
|
|
if (!(tmp = strstr(str, name))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find '%s' limit for block device '%s'"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen(name);
|
|
|
|
|
|
|
|
if (STREQLEN(tmp, "max", 3)) {
|
|
|
|
*rbps = 0;
|
2019-08-19 14:01:50 +00:00
|
|
|
} else if (virStrToLong_ull(tmp, &tmp, 10, rbps) < 0) {
|
2018-08-17 14:54:11 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:54:36 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetBlkioDeviceWriteBps(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long wbps)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
VIR_AUTOFREE(char *) blkstr = NULL;
|
|
|
|
|
|
|
|
if (!(blkstr = virCgroupGetBlockDevString(path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (wbps == 0) {
|
|
|
|
if (virAsprintf(&str, "%swbps=max", blkstr) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
if (virAsprintf(&str, "%swbps=%llu", blkstr, wbps) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetBlkioDeviceWriteBps(virCgroupPtr group,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long *wbps)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-06-18 13:24:41 +00:00
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
2018-08-17 14:54:36 +00:00
|
|
|
const char *name = "wbps=";
|
|
|
|
char *tmp;
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_BLKIO,
|
|
|
|
"io.max",
|
|
|
|
&value) < 0) {
|
2018-08-17 14:54:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-18 13:24:41 +00:00
|
|
|
if (virCgroupGetValueForBlkDev(value, path, &str) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-08-17 14:54:36 +00:00
|
|
|
if (!str) {
|
|
|
|
*wbps = 0;
|
|
|
|
} else {
|
|
|
|
if (!(tmp = strstr(str, name))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to find '%s' limit for block device '%s'"),
|
|
|
|
name, path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen(name);
|
|
|
|
|
|
|
|
if (STREQLEN(tmp, "max", 3)) {
|
|
|
|
*wbps = 0;
|
2019-08-19 14:01:50 +00:00
|
|
|
} else if (virStrToLong_ull(tmp, &tmp, 10, wbps) < 0) {
|
2018-08-17 14:54:36 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:54:59 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetMemory(virCgroupPtr group,
|
|
|
|
unsigned long long kb)
|
|
|
|
{
|
|
|
|
unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
if (kb > maxkb) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("Memory '%llu' must be less than %llu"),
|
|
|
|
kb, maxkb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kb == maxkb) {
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.max",
|
|
|
|
"max");
|
|
|
|
} else {
|
|
|
|
return virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.max",
|
|
|
|
kb << 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:55:34 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetMemoryStat(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)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) stat = NULL;
|
|
|
|
char *line = NULL;
|
|
|
|
unsigned long long cacheVal = 0;
|
|
|
|
unsigned long long activeAnonVal = 0;
|
|
|
|
unsigned long long inactiveAnonVal = 0;
|
|
|
|
unsigned long long activeFileVal = 0;
|
|
|
|
unsigned long long inactiveFileVal = 0;
|
|
|
|
unsigned long long unevictableVal = 0;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.stat",
|
|
|
|
&stat) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = stat;
|
|
|
|
|
2018-11-07 23:40:24 +00:00
|
|
|
while (*line) {
|
2018-08-17 14:55:34 +00:00
|
|
|
char *newLine = strchr(line, '\n');
|
|
|
|
char *valueStr = strchr(line, ' ');
|
|
|
|
unsigned long long value;
|
|
|
|
|
|
|
|
if (newLine)
|
|
|
|
*newLine = '\0';
|
|
|
|
|
|
|
|
if (!valueStr) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Cannot parse 'memory.stat' cgroup file."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*valueStr = '\0';
|
|
|
|
|
|
|
|
if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to parse '%s' as an integer"),
|
|
|
|
valueStr + 1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ(line, "file"))
|
|
|
|
cacheVal = value >> 10;
|
|
|
|
else if (STREQ(line, "active_anon"))
|
|
|
|
activeAnonVal = value >> 10;
|
|
|
|
else if (STREQ(line, "inactive_anon"))
|
|
|
|
inactiveAnonVal = value >> 10;
|
|
|
|
else if (STREQ(line, "active_file"))
|
|
|
|
activeFileVal = value >> 10;
|
|
|
|
else if (STREQ(line, "inactive_file"))
|
|
|
|
inactiveFileVal = value >> 10;
|
|
|
|
else if (STREQ(line, "unevictable"))
|
|
|
|
unevictableVal = value >> 10;
|
2018-11-07 23:40:24 +00:00
|
|
|
|
|
|
|
if (newLine)
|
|
|
|
line = newLine + 1;
|
|
|
|
else
|
|
|
|
break;
|
2018-08-17 14:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*cache = cacheVal;
|
|
|
|
*activeAnon = activeAnonVal;
|
|
|
|
*inactiveAnon = inactiveAnonVal;
|
|
|
|
*activeFile = activeFileVal;
|
|
|
|
*inactiveFile = inactiveFileVal;
|
|
|
|
*unevictable = unevictableVal;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:56:00 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetMemoryUsage(virCgroupPtr group,
|
|
|
|
unsigned long *kb)
|
|
|
|
{
|
|
|
|
unsigned long long usage_in_bytes;
|
|
|
|
int ret = virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.current", &usage_in_bytes);
|
|
|
|
if (ret == 0)
|
|
|
|
*kb = (unsigned long) usage_in_bytes >> 10;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:56:31 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetMemoryHardLimit(virCgroupPtr group,
|
|
|
|
unsigned long long kb)
|
|
|
|
{
|
|
|
|
return virCgroupV2SetMemory(group, kb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetMemoryHardLimit(virCgroupPtr group,
|
|
|
|
unsigned long long *kb)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
|
|
|
unsigned long long max;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.max", &value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ(value, "max")) {
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(value, NULL, 10, &max) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."),
|
|
|
|
value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*kb = max >> 10;
|
|
|
|
if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:57:00 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetMemorySoftLimit(virCgroupPtr group,
|
|
|
|
unsigned long long kb)
|
|
|
|
{
|
|
|
|
unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
if (kb > maxkb) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("Memory '%llu' must be less than %llu"),
|
|
|
|
kb, maxkb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kb == maxkb) {
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.high",
|
|
|
|
"max");
|
|
|
|
} else {
|
|
|
|
return virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.high",
|
|
|
|
kb << 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetMemorySoftLimit(virCgroupPtr group,
|
|
|
|
unsigned long long *kb)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
|
|
|
unsigned long long high;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.high", &value) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (STREQ(value, "max")) {
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(value, NULL, 10, &high) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."),
|
|
|
|
value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*kb = high >> 10;
|
|
|
|
if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:57:30 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetMemSwapHardLimit(virCgroupPtr group,
|
|
|
|
unsigned long long kb)
|
|
|
|
{
|
|
|
|
unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
if (kb > maxkb) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("Memory '%llu' must be less than %llu"),
|
|
|
|
kb, maxkb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kb == maxkb) {
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.swap.max",
|
|
|
|
"max");
|
|
|
|
} else {
|
|
|
|
return virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.swap.max",
|
|
|
|
kb << 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetMemSwapHardLimit(virCgroupPtr group,
|
|
|
|
unsigned long long *kb)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
|
|
|
unsigned long long max;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.swap.max", &value) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STREQ(value, "max")) {
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virStrToLong_ull(value, NULL, 10, &max) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."),
|
|
|
|
value);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*kb = max >> 10;
|
|
|
|
if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
|
|
|
|
*kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:57:52 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetMemSwapUsage(virCgroupPtr group,
|
|
|
|
unsigned long long *kb)
|
|
|
|
{
|
|
|
|
unsigned long long usage_in_bytes;
|
|
|
|
int ret;
|
|
|
|
ret = virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_MEMORY,
|
|
|
|
"memory.swap.current", &usage_in_bytes);
|
|
|
|
if (ret == 0)
|
|
|
|
*kb = (unsigned long) usage_in_bytes >> 10;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:58:15 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpuShares(virCgroupPtr group,
|
|
|
|
unsigned long long shares)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.weight", shares);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpuShares(virCgroupPtr group,
|
|
|
|
unsigned long long *shares)
|
|
|
|
{
|
|
|
|
return virCgroupGetValueU64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.weight", shares);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:58:40 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpuCfsPeriod(virCgroupPtr group,
|
|
|
|
unsigned long long cfs_period)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) value = NULL;
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
/* The cfs_period should be greater or equal than 1ms, and less or equal
|
|
|
|
* than 1s.
|
|
|
|
*/
|
|
|
|
if (cfs_period < 1000 || cfs_period > 1000000) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("cfs_period '%llu' must be in range (1000, 1000000)"),
|
|
|
|
cfs_period);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", &str) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp = strchr(str, ' '))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid 'cpu.max' data."));
|
|
|
|
return -1;
|
|
|
|
}
|
2019-09-05 09:22:11 +00:00
|
|
|
*tmp = '\0';
|
2018-08-17 14:58:40 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&value, "%s %llu", str, cfs_period) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return virCgroupSetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpuCfsPeriod(virCgroupPtr group,
|
|
|
|
unsigned long long *cfs_period)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", &str) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp = strchr(str, ' '))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Invalid 'cpu.max' data."));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:01:50 +00:00
|
|
|
if (virStrToLong_ull(tmp, &tmp, 10, cfs_period) < 0) {
|
2018-08-17 14:58:40 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' from cpu.max."), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:59:02 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpuCfsQuota(virCgroupPtr group,
|
|
|
|
long long cfs_quota)
|
|
|
|
{
|
|
|
|
/* The cfs_quota should be greater or equal than 1ms */
|
|
|
|
if (cfs_quota >= 0 &&
|
|
|
|
(cfs_quota < 1000 ||
|
|
|
|
cfs_quota > ULLONG_MAX / 1000)) {
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("cfs_quota '%lld' must be in range (1000, %llu)"),
|
|
|
|
cfs_quota, ULLONG_MAX / 1000);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfs_quota == ULLONG_MAX / 1000) {
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", "max");
|
|
|
|
}
|
|
|
|
|
|
|
|
return virCgroupSetValueI64(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", cfs_quota);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpuCfsQuota(virCgroupPtr group,
|
|
|
|
long long *cfs_quota)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
2019-08-30 14:15:15 +00:00
|
|
|
char *tmp;
|
2018-08-17 14:59:02 +00:00
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", &str) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:59:54 +00:00
|
|
|
if (STREQLEN(str, "max", 3)) {
|
2018-08-17 14:59:02 +00:00
|
|
|
*cfs_quota = ULLONG_MAX / 1000;
|
2019-08-20 11:59:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-08-17 14:59:02 +00:00
|
|
|
|
2019-08-30 14:15:15 +00:00
|
|
|
if (virStrToLong_ll(str, &tmp, 10, cfs_quota) < 0) {
|
2018-08-17 14:59:02 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' from cpu.max."), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:59:27 +00:00
|
|
|
static bool
|
|
|
|
virCgroupV2SupportsCpuBW(virCgroupPtr cgroup)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) path = NULL;
|
|
|
|
|
|
|
|
if (virCgroupV2PathOfController(cgroup, VIR_CGROUP_CONTROLLER_CPU,
|
|
|
|
"cpu.max", &path) < 0) {
|
|
|
|
virResetLastError();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virFileExists(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 14:59:49 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetCpuacctUsage(virCgroupPtr group,
|
|
|
|
unsigned long long *usage)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
|
|
|
|
"cpu.stat", &str) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp = strstr(str, "usage_usec "))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot parse cpu usage stat '%s'"), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen("usage_usec ");
|
|
|
|
|
|
|
|
if (virStrToLong_ull(tmp, &tmp, 10, usage) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."), tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*usage *= 1000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-17 15:00:00 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2GetCpuacctStat(virCgroupPtr group,
|
|
|
|
unsigned long long *user,
|
|
|
|
unsigned long long *sys)
|
|
|
|
{
|
|
|
|
VIR_AUTOFREE(char *) str = NULL;
|
|
|
|
char *tmp;
|
|
|
|
unsigned long long userVal = 0;
|
|
|
|
unsigned long long sysVal = 0;
|
|
|
|
|
|
|
|
if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
|
|
|
|
"cpu.stat", &str) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp = strstr(str, "user_usec "))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot parse cpu user stat '%s'"), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen("user_usec ");
|
|
|
|
|
|
|
|
if (virStrToLong_ull(tmp, &tmp, 10, &userVal) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."), tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tmp = strstr(str, "system_usec "))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("cannot parse cpu sys stat '%s'"), str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tmp += strlen("system_usec ");
|
|
|
|
|
|
|
|
if (virStrToLong_ull(tmp, &tmp, 10, &sysVal) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to parse value '%s' as number."), tmp);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*user = userVal * 1000;
|
|
|
|
*sys = sysVal * 1000;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 14:42:51 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpusetMems(virCgroupPtr group,
|
|
|
|
const char *mems)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
"cpuset.mems",
|
|
|
|
mems);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpusetMems(virCgroupPtr group,
|
|
|
|
char **mems)
|
|
|
|
{
|
|
|
|
return virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
"cpuset.mems",
|
|
|
|
mems);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 14:53:34 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpusetMemoryMigrate(virCgroupPtr group ATTRIBUTE_UNUSED,
|
|
|
|
bool migrate ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpusetMemoryMigrate(virCgroupPtr group ATTRIBUTE_UNUSED,
|
|
|
|
bool *migrate)
|
|
|
|
{
|
|
|
|
*migrate = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-19 14:55:38 +00:00
|
|
|
static int
|
|
|
|
virCgroupV2SetCpusetCpus(virCgroupPtr group,
|
|
|
|
const char *cpus)
|
|
|
|
{
|
|
|
|
return virCgroupSetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
"cpuset.cpus",
|
|
|
|
cpus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virCgroupV2GetCpusetCpus(virCgroupPtr group,
|
|
|
|
char **cpus)
|
|
|
|
{
|
|
|
|
return virCgroupGetValueStr(group,
|
|
|
|
VIR_CGROUP_CONTROLLER_CPUSET,
|
|
|
|
"cpuset.cpus",
|
|
|
|
cpus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-18 15:48:33 +00:00
|
|
|
virCgroupBackend virCgroupV2Backend = {
|
|
|
|
.type = VIR_CGROUP_BACKEND_TYPE_V2,
|
2018-08-17 14:28:11 +00:00
|
|
|
|
|
|
|
.available = virCgroupV2Available,
|
2018-09-14 16:19:53 +00:00
|
|
|
.validateMachineGroup = virCgroupV2ValidateMachineGroup,
|
2018-08-17 14:42:17 +00:00
|
|
|
.copyMounts = virCgroupV2CopyMounts,
|
2018-09-25 12:34:01 +00:00
|
|
|
.copyPlacement = virCgroupV2CopyPlacement,
|
2018-09-25 12:34:47 +00:00
|
|
|
.detectMounts = virCgroupV2DetectMounts,
|
2018-09-14 21:46:42 +00:00
|
|
|
.detectPlacement = virCgroupV2DetectPlacement,
|
2018-09-18 07:17:24 +00:00
|
|
|
.validatePlacement = virCgroupV2ValidatePlacement,
|
2018-09-18 07:21:53 +00:00
|
|
|
.stealPlacement = virCgroupV2StealPlacement,
|
2018-09-25 12:37:21 +00:00
|
|
|
.detectControllers = virCgroupV2DetectControllers,
|
2018-08-19 17:40:15 +00:00
|
|
|
.hasController = virCgroupV2HasController,
|
2018-08-19 17:43:04 +00:00
|
|
|
.getAnyController = virCgroupV2GetAnyController,
|
2018-08-19 17:45:49 +00:00
|
|
|
.pathOfController = virCgroupV2PathOfController,
|
2018-09-18 15:49:19 +00:00
|
|
|
.makeGroup = virCgroupV2MakeGroup,
|
2018-08-17 14:49:08 +00:00
|
|
|
.remove = virCgroupV2Remove,
|
2018-08-17 14:49:33 +00:00
|
|
|
.addTask = virCgroupV2AddTask,
|
2018-08-17 14:49:57 +00:00
|
|
|
.hasEmptyTasks = virCgroupV2HasEmptyTasks,
|
2018-12-10 08:51:14 +00:00
|
|
|
.killRecursive = virCgroupV2KillRecursive,
|
2018-09-28 18:40:11 +00:00
|
|
|
.bindMount = virCgroupV2BindMount,
|
2018-08-17 14:50:57 +00:00
|
|
|
.setOwner = virCgroupV2SetOwner,
|
2018-08-17 14:51:28 +00:00
|
|
|
|
|
|
|
.setBlkioWeight = virCgroupV2SetBlkioWeight,
|
|
|
|
.getBlkioWeight = virCgroupV2GetBlkioWeight,
|
2018-08-17 14:51:54 +00:00
|
|
|
.getBlkioIoServiced = virCgroupV2GetBlkioIoServiced,
|
2018-08-17 14:52:24 +00:00
|
|
|
.getBlkioIoDeviceServiced = virCgroupV2GetBlkioIoDeviceServiced,
|
2018-08-17 14:52:50 +00:00
|
|
|
.setBlkioDeviceWeight = virCgroupV2SetBlkioDeviceWeight,
|
|
|
|
.getBlkioDeviceWeight = virCgroupV2GetBlkioDeviceWeight,
|
2018-08-17 14:53:17 +00:00
|
|
|
.setBlkioDeviceReadIops = virCgroupV2SetBlkioDeviceReadIops,
|
|
|
|
.getBlkioDeviceReadIops = virCgroupV2GetBlkioDeviceReadIops,
|
2018-08-17 14:53:45 +00:00
|
|
|
.setBlkioDeviceWriteIops = virCgroupV2SetBlkioDeviceWriteIops,
|
|
|
|
.getBlkioDeviceWriteIops = virCgroupV2GetBlkioDeviceWriteIops,
|
2018-08-17 14:54:11 +00:00
|
|
|
.setBlkioDeviceReadBps = virCgroupV2SetBlkioDeviceReadBps,
|
|
|
|
.getBlkioDeviceReadBps = virCgroupV2GetBlkioDeviceReadBps,
|
2018-08-17 14:54:36 +00:00
|
|
|
.setBlkioDeviceWriteBps = virCgroupV2SetBlkioDeviceWriteBps,
|
|
|
|
.getBlkioDeviceWriteBps = virCgroupV2GetBlkioDeviceWriteBps,
|
2018-08-17 14:54:59 +00:00
|
|
|
|
|
|
|
.setMemory = virCgroupV2SetMemory,
|
2018-08-17 14:55:34 +00:00
|
|
|
.getMemoryStat = virCgroupV2GetMemoryStat,
|
2018-08-17 14:56:00 +00:00
|
|
|
.getMemoryUsage = virCgroupV2GetMemoryUsage,
|
2018-08-17 14:56:31 +00:00
|
|
|
.setMemoryHardLimit = virCgroupV2SetMemoryHardLimit,
|
|
|
|
.getMemoryHardLimit = virCgroupV2GetMemoryHardLimit,
|
2018-08-17 14:57:00 +00:00
|
|
|
.setMemorySoftLimit = virCgroupV2SetMemorySoftLimit,
|
|
|
|
.getMemorySoftLimit = virCgroupV2GetMemorySoftLimit,
|
2018-08-17 14:57:30 +00:00
|
|
|
.setMemSwapHardLimit = virCgroupV2SetMemSwapHardLimit,
|
|
|
|
.getMemSwapHardLimit = virCgroupV2GetMemSwapHardLimit,
|
2018-08-17 14:57:52 +00:00
|
|
|
.getMemSwapUsage = virCgroupV2GetMemSwapUsage,
|
2018-08-17 14:58:15 +00:00
|
|
|
|
|
|
|
.setCpuShares = virCgroupV2SetCpuShares,
|
|
|
|
.getCpuShares = virCgroupV2GetCpuShares,
|
2018-08-17 14:58:40 +00:00
|
|
|
.setCpuCfsPeriod = virCgroupV2SetCpuCfsPeriod,
|
|
|
|
.getCpuCfsPeriod = virCgroupV2GetCpuCfsPeriod,
|
2018-08-17 14:59:02 +00:00
|
|
|
.setCpuCfsQuota = virCgroupV2SetCpuCfsQuota,
|
|
|
|
.getCpuCfsQuota = virCgroupV2GetCpuCfsQuota,
|
2018-08-17 14:59:27 +00:00
|
|
|
.supportsCpuBW = virCgroupV2SupportsCpuBW,
|
2018-08-17 14:59:49 +00:00
|
|
|
|
|
|
|
.getCpuacctUsage = virCgroupV2GetCpuacctUsage,
|
2018-08-17 15:00:00 +00:00
|
|
|
.getCpuacctStat = virCgroupV2GetCpuacctStat,
|
2019-02-19 14:42:51 +00:00
|
|
|
|
|
|
|
.setCpusetMems = virCgroupV2SetCpusetMems,
|
|
|
|
.getCpusetMems = virCgroupV2GetCpusetMems,
|
2019-02-19 14:53:34 +00:00
|
|
|
.setCpusetMemoryMigrate = virCgroupV2SetCpusetMemoryMigrate,
|
|
|
|
.getCpusetMemoryMigrate = virCgroupV2GetCpusetMemoryMigrate,
|
2019-02-19 14:55:38 +00:00
|
|
|
.setCpusetCpus = virCgroupV2SetCpusetCpus,
|
|
|
|
.getCpusetCpus = virCgroupV2GetCpusetCpus,
|
2018-09-18 15:48:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
virCgroupV2Register(void)
|
|
|
|
{
|
|
|
|
virCgroupBackendRegister(&virCgroupV2Backend);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !__linux__ */
|
|
|
|
|
|
|
|
void
|
|
|
|
virCgroupV2Register(void)
|
|
|
|
{
|
|
|
|
VIR_INFO("Control groups not supported on this platform");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !__linux__ */
|