Beispiel #1
0
func (m *Manager) Set(container *configs.Config) error {
	for name, path := range m.Paths {
		sys, err := subsystems.Get(name)
		if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
			continue
		}
		if err := sys.Set(path, container.Cgroups); err != nil {
			return err
		}
	}
	return nil
}
func (m *Manager) Set(container *configs.Config) error {
	for name, path := range m.Paths {
		sys, ok := subsystems[name]
		if !ok || !cgroups.PathExists(path) {
			continue
		}
		if err := sys.Set(path, container.Cgroups); err != nil {
			return err
		}
	}

	return nil
}
// Exists checks if all subsystem cgroups already exist
func (m *cgroupManagerImpl) Exists(name CgroupName) bool {
	// Get map of all cgroup paths on the system for the particular cgroup
	cgroupPaths := m.buildCgroupPaths(name)

	// If even one cgroup path doesn't exist, then the cgroup doesn't exist.
	for _, path := range cgroupPaths {
		if !libcontainercgroups.PathExists(path) {
			return false
		}
	}

	return true
}
Beispiel #4
0
func (m *Manager) GetStats() (*cgroups.Stats, error) {
	m.mu.Lock()
	defer m.mu.Unlock()
	stats := cgroups.NewStats()
	for name, path := range m.Paths {
		sys, err := subsystems.Get(name)
		if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
			continue
		}
		if err := sys.GetStats(path, stats); err != nil {
			return nil, err
		}
	}
	return stats, nil
}
// Exists checks if all subsystem cgroups already exist
func (m *cgroupManagerImpl) Exists(name string) bool {
	// Get map of all cgroup paths on the system for the particular cgroup
	cgroupPaths := make(map[string]string, len(m.subsystems.MountPoints))
	for key, val := range m.subsystems.MountPoints {
		cgroupPaths[key] = path.Join(val, name)
	}

	// If even one cgroup doesn't exist we go on to create it
	for _, path := range cgroupPaths {
		if !libcontainercgroups.PathExists(path) {
			return false
		}
	}
	return true
}
// Exists checks if all subsystem cgroups already exist
func (m *cgroupManagerImpl) Exists(name string) bool {
	// Get map of all cgroup paths on the system for the particular cgroup
	cgroupPaths := make(map[string]string, len(m.subsystems.mountPoints))
	for key, val := range m.subsystems.mountPoints {
		cgroupPaths[key] = path.Join(val, name)
	}

	// If even one cgroup doesn't exist we go on to create it
	// @TODO(dubstack) We skip check for systemd until we update
	// libcontainer in vendor
	for key, path := range cgroupPaths {
		if key != "systemd" && !libcontainercgroups.PathExists(path) {
			return false
		}
	}
	return true
}
Beispiel #7
0
func setKernelMemory(path string, kernelMemoryLimit int64) error {
	if path == "" {
		return fmt.Errorf("no such directory for %s", cgroupKernelMemoryLimit)
	}
	if !cgroups.PathExists(filepath.Join(path, cgroupKernelMemoryLimit)) {
		// kernel memory is not enabled on the system so we should do nothing
		return nil
	}
	if err := ioutil.WriteFile(filepath.Join(path, cgroupKernelMemoryLimit), []byte(strconv.FormatInt(kernelMemoryLimit, 10)), 0700); err != nil {
		// Check if the error number returned by the syscall is "EBUSY"
		// The EBUSY signal is returned on attempts to write to the
		// memory.kmem.limit_in_bytes file if the cgroup has children or
		// once tasks have been attached to the cgroup
		if pathErr, ok := err.(*os.PathError); ok {
			if errNo, ok := pathErr.Err.(syscall.Errno); ok {
				if errNo == syscall.EBUSY {
					return fmt.Errorf("failed to set %s, because either tasks have already joined this cgroup or it has children", cgroupKernelMemoryLimit)
				}
			}
		}
		return fmt.Errorf("failed to write %v to %v: %v", kernelMemoryLimit, cgroupKernelMemoryLimit, err)
	}
	return nil
}
Beispiel #8
0
func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
	path, err := d.path("memory")
	if err != nil && !cgroups.IsNotFound(err) {
		return err
	}
	// reset error.
	err = nil
	if path == "" {
		// Invalid input.
		return fmt.Errorf("invalid path for memory cgroups: %+v", d)
	}
	defer func() {
		if err != nil {
			os.RemoveAll(path)
		}
	}()
	if !cgroups.PathExists(path) {
		if err = os.MkdirAll(path, 0755); err != nil {
			return err
		}
	}
	if memoryAssigned(d.config) {
		// We have to set kernel memory here, as we can't change it once
		// processes have been attached to the cgroup.
		if err = s.SetKernelMemory(path, d.config); 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 _, jerr := d.join("memory"); jerr != nil && !cgroups.IsNotFound(jerr) {
		err = jerr
		return err
	}
	return nil
}