Beispiel #1
0
// requires a path to a platform configuration file in this format:
// <launchablename>:
//   cgroup:
//     cpus: 4
//     memory: 123456
// and the <launchablename> and <cgroupname>
// a cgroup with the name <cgroupname> will be created, using the parameters for
// <launchablename> found in the platform configuration
// then, the current PID will be added to that cgroup
func cgEnter(platconf, launchableName, cgroupName string) error {
	platconfBuf, err := ioutil.ReadFile(platconf)
	if err != nil {
		return err
	}
	cgMap := make(map[string]map[string]cgroups.Config)
	err = yaml.Unmarshal(platconfBuf, cgMap)
	if err != nil {
		return err
	}

	if _, ok := cgMap[launchableName]; !ok {
		return util.Errorf("Unknown launchable %q in PLATFORM_CONFIG_PATH", launchableName)
	}
	if _, ok := cgMap[launchableName]["cgroup"]; !ok {
		return util.Errorf("Launchable %q has malformed PLATFORM_CONFIG_PATH", launchableName)
	}
	cgConfig := cgMap[launchableName]["cgroup"]
	cgConfig.Name = cgroupName

	cg, err := cgroups.Find()
	if err != nil {
		return util.Errorf("Could not find cgroupfs mount point: %s", err)
	}
	err = cg.Write(cgConfig)
	if _, ok := err.(cgroups.UnsupportedError); ok {
		// if a subsystem is not supported, just log
		// and carry on
		log.Printf("Unsupported subsystem (%s), continuing\n", err)
		return nil
	} else if err != nil {
		return util.Errorf("Could not set cgroup parameters: %s", err)
	}
	return cg.AddPID(cgConfig.Name, os.Getpid())
}
Beispiel #2
0
func main() {
	app.Version(version.VERSION)
	kingpin.MustParse(app.Parse(os.Args[1:]))
	logger := log.New(os.Stderr, "", 0)

	// P2 only uses the "memory" and "cpu" resource controllers, and it creates an
	// identical hierarchy in either. Just scan "cpu".
	sys, err := cgroups.Find()
	if err != nil {
		logger.Fatalf("error finding cgroups: %v", err)
	}
	output, err := scanCgroup(sys.CPU)
	if err != nil {
		logger.Fatalf("error scanning cpu controller: %v", err)
	}
	data, err := json.Marshal(&output)
	if err != nil {
		logger.Fatalf("error formatting output: %v", err)
	}
	_, err = os.Stdout.Write(data)
	if err != nil {
		logger.Fatalf("error writing output: %v", err)
	}
	_, _ = os.Stdout.Write([]byte("\n"))
}