[systemd-devel] [PATCH 2/2] core: add startup resource control option
WaLyong Cho
walyong.cho at samsung.com
Wed Mar 12 03:13:57 PDT 2014
Similar to CPUShares= and BlockIOWeight= respectively. However only
assign the specified weight during startup. Each control group
attribute is re-assigned as weight by CPUShares=weight and
BlockIOWeight=weight after startup. If not CPUShares= or
BlockIOWeight= be specified, then the attribute is re-assigned to each
default attribute value. (default cpu.shares=1024, blkio.weight=1000)
If only CPUShares=weight or BlockIOWeight=weight be specified, then
that implies StartupCPUShares=weight and StartupBlockIOWeight=weight.
---
src/core/cgroup.c | 25 ++++++++--
src/core/cgroup.h | 6 ++-
src/core/dbus-cgroup.c | 40 ++++++++++++++++
src/core/load-fragment-gperf.gperf.m4 | 2 +
src/core/load-fragment.c | 83 +++++++++++++++++++++++++++++++++
src/core/load-fragment.h | 2 +
6 files changed, 152 insertions(+), 6 deletions(-)
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 18de888..2f49197 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -34,8 +34,12 @@ void cgroup_context_init(CGroupContext *c) {
* structure is preinitialized to 0 */
c->cpu_shares = 1024;
+ c->startup_cpu_shares = 1024;
+ c->use_startup_cpu_shares = false;
c->memory_limit = (uint64_t) -1;
c->blockio_weight = 1000;
+ c->startup_blockio_weight = 1000;
+ c->use_startup_blockio_weight = false;
}
void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a) {
@@ -93,14 +97,18 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
"%sBlockIOAccounting=%s\n"
"%sMemoryAccounting=%s\n"
"%sCPUShares=%lu\n"
+ "%sStartupCPUShares=%lu\n"
"%sBlockIOWeight=%lu\n"
+ "%sStartupBlockIOWeight=%lu\n"
"%sMemoryLimit=%" PRIu64 "\n"
"%sDevicePolicy=%s\n",
prefix, yes_no(c->cpu_accounting),
prefix, yes_no(c->blockio_accounting),
prefix, yes_no(c->memory_accounting),
prefix, c->cpu_shares,
+ prefix, c->startup_cpu_shares,
prefix, c->blockio_weight,
+ prefix, c->startup_blockio_weight,
prefix, c->memory_limit,
prefix, cgroup_device_policy_to_string(c->device_policy));
@@ -269,7 +277,7 @@ fail:
return -errno;
}
-void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const char *path) {
+void cgroup_context_apply(Manager *m, CGroupContext *c, CGroupControllerMask mask, const char *path) {
bool is_root;
int r;
@@ -286,7 +294,9 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
if ((mask & CGROUP_CPU) && !is_root) {
char buf[DECIMAL_STR_MAX(unsigned long) + 1];
- sprintf(buf, "%lu\n", c->cpu_shares);
+ sprintf(buf, "%lu\n", m->system_state == STATE_STARTUP
+ ? c->startup_cpu_shares
+ : c->cpu_shares);
r = cg_set_attribute("cpu", path, "cpu.shares", buf);
if (r < 0)
log_warning("Failed to set cpu.shares on %s: %s", path, strerror(-r));
@@ -300,7 +310,9 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
CGroupBlockIODeviceBandwidth *b;
if (!is_root) {
- sprintf(buf, "%lu\n", c->blockio_weight);
+ sprintf(buf, "%lu\n", m->system_state == STATE_STARTUP
+ ? c->startup_blockio_weight
+ : c->blockio_weight);
r = cg_set_attribute("blkio", path, "blkio.weight", buf);
if (r < 0)
log_warning("Failed to set blkio.weight on %s: %s", path, strerror(-r));
@@ -409,11 +421,14 @@ CGroupControllerMask cgroup_context_get_mask(CGroupContext *c) {
/* Figure out which controllers we need */
- if (c->cpu_accounting || c->cpu_shares != 1024)
+ if (c->cpu_accounting ||
+ c->cpu_shares != 1024 ||
+ c->startup_cpu_shares != 1024)
mask |= CGROUP_CPUACCT | CGROUP_CPU;
if (c->blockio_accounting ||
c->blockio_weight != 1000 ||
+ c->startup_blockio_weight != 1000 ||
c->blockio_device_weights ||
c->blockio_device_bandwidths)
mask |= CGROUP_BLKIO;
@@ -639,7 +654,7 @@ static int unit_realize_cgroup_now(Unit *u) {
return r;
/* Finally, apply the necessary attributes. */
- cgroup_context_apply(unit_get_cgroup_context(u), mask, u->cgroup_path);
+ cgroup_context_apply(u->manager, unit_get_cgroup_context(u), mask, u->cgroup_path);
return 0;
}
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
index be717ad..3022b63 100644
--- a/src/core/cgroup.h
+++ b/src/core/cgroup.h
@@ -71,8 +71,12 @@ struct CGroupContext {
bool memory_accounting;
unsigned long cpu_shares;
+ unsigned long startup_cpu_shares;
+ bool use_startup_cpu_shares;
unsigned long blockio_weight;
+ unsigned long startup_blockio_weight;
+ bool use_startup_blockio_weight;
LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
@@ -89,7 +93,7 @@ struct CGroupContext {
void cgroup_context_init(CGroupContext *c);
void cgroup_context_done(CGroupContext *c);
void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
-void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const char *path);
+void cgroup_context_apply(Manager *m, CGroupContext *c, CGroupControllerMask mask, const char *path);
CGroupControllerMask cgroup_context_get_mask(CGroupContext *c);
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index 775825b..fdd7cb3 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -137,8 +137,10 @@ const sd_bus_vtable bus_cgroup_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("CPUAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, cpu_accounting), 0),
SD_BUS_PROPERTY("CPUShares", "t", bus_property_get_ulong, offsetof(CGroupContext, cpu_shares), 0),
+ SD_BUS_PROPERTY("StartupCPUShares", "t", bus_property_get_ulong, offsetof(CGroupContext, startup_cpu_shares), 0),
SD_BUS_PROPERTY("BlockIOAccounting", "b", bus_property_get_bool, offsetof(CGroupContext, blockio_accounting), 0),
SD_BUS_PROPERTY("BlockIOWeight", "t", bus_property_get_ulong, offsetof(CGroupContext, blockio_weight), 0),
+ SD_BUS_PROPERTY("StartupBlockIOWeight", "t", bus_property_get_ulong, offsetof(CGroupContext, startup_blockio_weight), 0),
SD_BUS_PROPERTY("BlockIODeviceWeight", "a(st)", property_get_blockio_device_weight, 0, 0),
SD_BUS_PROPERTY("BlockIOReadBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
SD_BUS_PROPERTY("BlockIOWriteBandwidth", "a(st)", property_get_blockio_device_bandwidths, 0, 0),
@@ -197,6 +199,25 @@ int bus_cgroup_set_property(
return 1;
+ } else if (streq(name, "StartupCPUShares")) {
+ uint64_t u64;
+ unsigned long ul;
+
+ r = sd_bus_message_read(message, "t", &u64);
+ if (r < 0)
+ return r;
+
+ ul = (unsigned long) u64;
+ if (ul <= 0 || (uint64_t) ul != u64)
+ return sd_bus_error_set_errnof(error, EINVAL, "StartupCPUShares value out of range");
+
+ if (mode != UNIT_CHECK) {
+ c->startup_cpu_shares = ul;
+ unit_write_drop_in_private_format(u, mode, name, "StartupCPUShares=%lu", ul);
+ }
+
+ return 1;
+
} else if (streq(name, "BlockIOAccounting")) {
int b;
@@ -230,6 +251,25 @@ int bus_cgroup_set_property(
return 1;
+ } else if (streq(name, "StartupBlockIOWeight")) {
+ uint64_t u64;
+ unsigned long ul;
+
+ r = sd_bus_message_read(message, "t", &u64);
+ if (r < 0)
+ return r;
+
+ ul = (unsigned long) u64;
+ if (ul < 10 || ul > 1000)
+ return sd_bus_error_set_errnof(error, EINVAL, "StartupBlockIOWeight value out of range");
+
+ if (mode != UNIT_CHECK) {
+ c->startup_blockio_weight = ul;
+ unit_write_drop_in_private_format(u, mode, name, "StartupBlockIOWeight=%lu", ul);
+ }
+
+ return 1;
+
} else if (streq(name, "BlockIOReadBandwidth") || streq(name, "BlockIOWriteBandwidth")) {
const char *path;
bool read = true;
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index f85d868..e2e2601 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -109,12 +109,14 @@ m4_define(`CGROUP_CONTEXT_CONFIG_ITEMS',
`$1.Slice, config_parse_unit_slice, 0, 0
$1.CPUAccounting, config_parse_bool, 0, offsetof($1, cgroup_context.cpu_accounting)
$1.CPUShares, config_parse_cpu_shares, 0, offsetof($1, cgroup_context)
+$1.StartupCPUShares, config_parse_startup_cpu_shares, 0, offsetof($1, cgroup_context)
$1.MemoryAccounting, config_parse_bool, 0, offsetof($1, cgroup_context.memory_accounting)
$1.MemoryLimit, config_parse_memory_limit, 0, offsetof($1, cgroup_context)
$1.DeviceAllow, config_parse_device_allow, 0, offsetof($1, cgroup_context)
$1.DevicePolicy, config_parse_device_policy, 0, offsetof($1, cgroup_context.device_policy)
$1.BlockIOAccounting, config_parse_bool, 0, offsetof($1, cgroup_context.blockio_accounting)
$1.BlockIOWeight, config_parse_blockio_weight, 0, offsetof($1, cgroup_context)
+$1.StartupBlockIOWeight, config_parse_startup_blockio_weight, 0, offsetof($1, cgroup_context)
$1.BlockIODeviceWeight, config_parse_blockio_device_weight, 0, offsetof($1, cgroup_context)
$1.BlockIOReadBandwidth, config_parse_blockio_bandwidth, 0, offsetof($1, cgroup_context)
$1.BlockIOWriteBandwidth, config_parse_blockio_bandwidth, 0, offsetof($1, cgroup_context)'
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 06e3031..f75ec33 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -2460,6 +2460,47 @@ int config_parse_cpu_shares(
}
c->cpu_shares = lu;
+ if (!c->use_startup_cpu_shares)
+ c->startup_cpu_shares = lu;
+
+ return 0;
+}
+
+int config_parse_startup_cpu_shares(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ CGroupContext *c = data;
+ unsigned long lu;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+
+ if (isempty(rvalue)) {
+ c->startup_cpu_shares = 1024;
+ return 0;
+ }
+
+ r = safe_atolu(rvalue, &lu);
+ if (r < 0 || lu <= 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Startup CPU shares '%s' invalid. Ignoring.", rvalue);
+ return 0;
+ }
+
+ c->startup_cpu_shares = lu;
+ c->use_startup_cpu_shares = true;
+
return 0;
}
@@ -2589,6 +2630,46 @@ int config_parse_blockio_weight(
}
c->blockio_weight = lu;
+ if (!c->use_startup_blockio_weight)
+ c->startup_blockio_weight = lu;
+
+ return 0;
+}
+
+int config_parse_startup_blockio_weight(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ unsigned section_line,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ CGroupContext *c = data;
+ unsigned long lu;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+
+ if (isempty(rvalue)) {
+ c->startup_blockio_weight = 1000;
+ return 0;
+ }
+
+ r = safe_atolu(rvalue, &lu);
+ if (r < 0 || lu < 10 || lu > 1000) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Startup Block IO weight '%s' invalid. Ignoring.", rvalue);
+ return 0;
+ }
+
+ c->startup_blockio_weight = lu;
+ c->use_startup_blockio_weight = true;
return 0;
}
@@ -3406,11 +3487,13 @@ void unit_dump_config_items(FILE *f) {
{ config_parse_address_families, "FAMILIES" },
#endif
{ config_parse_cpu_shares, "SHARES" },
+ { config_parse_startup_cpu_shares, "STARTUPSHARES" },
{ config_parse_memory_limit, "LIMIT" },
{ config_parse_device_allow, "DEVICE" },
{ config_parse_device_policy, "POLICY" },
{ config_parse_blockio_bandwidth, "BANDWIDTH" },
{ config_parse_blockio_weight, "WEIGHT" },
+ { config_parse_startup_blockio_weight, "STARTUPWEIGHT" },
{ config_parse_blockio_device_weight, "DEVICEWEIGHT" },
{ config_parse_long, "LONG" },
{ config_parse_socket_service, "SERVICE" },
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
index 3513851..d8d4b2d 100644
--- a/src/core/load-fragment.h
+++ b/src/core/load-fragment.h
@@ -80,10 +80,12 @@ int config_parse_syscall_errno(const char *unit, const char *filename, unsigned
int config_parse_environ(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_unit_slice(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_cpu_shares(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_startup_cpu_shares(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_device_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_device_allow(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_blockio_weight(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_startup_blockio_weight(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_blockio_device_weight(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_blockio_bandwidth(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_job_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
--
1.7.9.5
More information about the systemd-devel
mailing list