Unify behavior for memory cgroup

We have a rule that for optional cgroups, don't fail if some
of them are not mounted, but we want it fail hard when a
user specifies an option and we are unable to fulfill the
request.

Memory cgroup should also follow this rule.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
This commit is contained in:
Qiang Huang 2015-10-20 14:01:48 +08:00
parent 3be7f87b1b
commit 194e0e4db6
1 changed files with 22 additions and 11 deletions

View File

@ -19,15 +19,20 @@ type MemoryGroup struct {
func (s *MemoryGroup) Apply(d *data) (err error) {
path, err := d.path("memory")
if err != nil {
if cgroups.IsNotFound(err) {
return nil
}
if err != nil && !cgroups.IsNotFound(err) {
return err
}
if memoryAssigned(d.c) {
if path != "" {
if err := os.MkdirAll(path, 0755); err != nil {
return err
}
}
if err := s.Set(path, d.c); err != nil {
return err
}
}
defer func() {
if err != nil {
@ -35,13 +40,10 @@ func (s *MemoryGroup) Apply(d *data) (err error) {
}
}()
if err := s.Set(path, d.c); err != nil {
return err
}
// We need to join memory cgroup after set memory limits, because
// kmem.limit_in_bytes can only be set when the cgroup is empty.
if _, err = d.join("memory"); err != nil {
_, err = d.join("memory")
if err != nil && !cgroups.IsNotFound(err) {
return err
}
@ -132,6 +134,15 @@ func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil
}
func memoryAssigned(cgroup *configs.Cgroup) bool {
return cgroup.Memory != 0 ||
cgroup.MemoryReservation != 0 ||
cgroup.MemorySwap > 0 ||
cgroup.KernelMemory > 0 ||
cgroup.OomKillDisable ||
cgroup.MemorySwappiness != -1
}
func getMemoryData(path, name string) (cgroups.MemoryData, error) {
memoryData := cgroups.MemoryData{}