mirror of
https://github.com/kata-containers/cgroups-rs.git
synced 2026-08-05 02:13:23 +00:00
Support setting max memory limit in cgroup v2
When setting memory.max/memory.swap.max in cgroup v2, Linux uses "max" instead of "-1" to indicate no restriction on memory usage. The set_limit and set_memswap_limit functions of the memory controller accept i64 as a parameter. In cgroupv2, if the parameter is -1, "max" should be written into the cgroup file. Fixes #128 Signed-off-by: mengze <mengze@linux.alibaba.com>
This commit is contained in:
@@ -844,13 +844,16 @@ impl MemController {
|
|||||||
/// Set the memory usage limit of the control group, in bytes.
|
/// Set the memory usage limit of the control group, in bytes.
|
||||||
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.limit_in_bytes";
|
let mut file_name = "memory.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.max";
|
file_name = "memory.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -881,13 +884,16 @@ impl MemController {
|
|||||||
/// Set the memory+swap limit of the control group, in bytes.
|
/// Set the memory+swap limit of the control group, in bytes.
|
||||||
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
pub fn set_memswap_limit(&self, limit: i64) -> Result<()> {
|
||||||
let mut file_name = "memory.memsw.limit_in_bytes";
|
let mut file_name = "memory.memsw.limit_in_bytes";
|
||||||
|
let mut limit_str = limit.to_string();
|
||||||
if self.v2 {
|
if self.v2 {
|
||||||
file_name = "memory.swap.max";
|
file_name = "memory.swap.max";
|
||||||
|
if limit == -1 {
|
||||||
|
limit_str = "max".to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
self.open_path(file_name, true).and_then(|mut file| {
|
self.open_path(file_name, true).and_then(|mut file| {
|
||||||
file.write_all(limit.to_string().as_ref()).map_err(|e| {
|
file.write_all(limit_str.as_ref())
|
||||||
Error::with_cause(WriteFailed(file_name.to_string(), limit.to_string()), e)
|
.map_err(|e| Error::with_cause(WriteFailed(file_name.to_string(), limit_str), e))
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user