func GetUsage(id string) (int64, error) { id, err := docker.ExpandId(id) if err != nil { log.Println("Error when expanding id:", err) return 0, err } path := config.CgroupPath("memory", id) + "/" + LXC_MEM_USAGE_FILE f, err := os.Open(path) if err != nil { log.Println("Error while opening:", err) return 0, err } defer f.Close() buffer := make([]byte, 16) n, err := f.Read(buffer) if err != nil { log.Println("Error while reading ", path, ":", err) return 0, err } buffer = buffer[:n-1] val, err := strconv.ParseInt(string(buffer), 10, 64) if err != nil { log.Println("Error while parsing ", string(buffer), " : ", err) return 0, err } return val, nil }
func ExpandId(id string) (string, error) { fullId, err := expandIdFromCache(id) if err == nil { return fullId, nil } if err != IdNotInCache { return "", errgo.Mask(err) } dir := filepath.Dir(config.CgroupPath("memory", id)) switch config.ENV["CGROUP_SOURCE"] { case "systemd": id, err = expandSystemdId(dir, id) case "docker": id, err = expandDockerId(dir, id) default: panic("not a known CGROUP SOURCE") } if err != nil { return "", errgo.Mask(err) } containerIdsCache = append(containerIdsCache, id) return id, nil }
func Pid(id string) (string, error) { time.Sleep(time.Second) path := config.CgroupPath("memory", id) content, err := ioutil.ReadFile(path + "/tasks") if err != nil { return "", err } return strings.Split(string(content), "\n")[0], nil }
func cpuacctUsage(container string) (int64, error) { file := config.CgroupPath("cpuacct", container) + "/" + LXC_CPUACCT_USAGE_FILE f, err := os.Open(file) if err != nil { log.Println(err) return 0, err } buffer := make([]byte, 16) n, err := f.Read(buffer) buffer = buffer[:n] bufferStr := string(buffer) bufferStr = bufferStr[:len(bufferStr)-1] res, err := strconv.ParseInt(bufferStr, 10, 64) if err != nil { log.Println("Failed to parse : ", err) return 0, err } return res, nil }