Merge pull request #343 from hqhq/hq_unify_behavior_for_memory

Unify behavior for memory cgroup
This commit is contained in:
Mrunal Patel 2015-11-02 14:58:31 -08:00
commit cf73b32eeb
1 changed files with 22 additions and 11 deletions

View File

@ -23,14 +23,19 @@ func (s *MemoryGroup) Name() string {
func (s *MemoryGroup) Apply(d *data) (err error) { func (s *MemoryGroup) Apply(d *data) (err error) {
path, err := d.path("memory") path, err := d.path("memory")
if err != nil { if err != nil && !cgroups.IsNotFound(err) {
if cgroups.IsNotFound(err) {
return nil
}
return err return err
} }
if err := os.MkdirAll(path, 0755); err != nil { if memoryAssigned(d.c) {
return err 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() { defer func() {
@ -39,13 +44,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 // We need to join memory cgroup after set memory limits, because
// kmem.limit_in_bytes can only be set when the cgroup is empty. // 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 return err
} }
@ -136,6 +138,15 @@ func (s *MemoryGroup) GetStats(path string, stats *cgroups.Stats) error {
return nil 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) { func getMemoryData(path, name string) (cgroups.MemoryData, error) {
memoryData := cgroups.MemoryData{} memoryData := cgroups.MemoryData{}