예제 #1
0
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
}
예제 #2
0
func GetUsage(id string) (Usage, error) {
	usage := Usage{}
	id, err := docker.ExpandId(id)
	if err != nil {
		log.Println("Error when expanding id:", err)
		return usage, err
	}

	errors := make(chan error)
	wg := &sync.WaitGroup{}
	wg.Add(6)
	go readMemoryCgroupInt64Async(id, LXC_MAX_MEM_FILE, &usage.MaxMemoryUsage, errors, wg)
	go readMemoryCgroupInt64Async(id, LXC_MAX_SWAP_MEM_FILE, &usage.MaxSwapMemoryUsage, errors, wg)
	go readMemoryCgroupInt64Async(id, LXC_MEM_LIMIT_FILE, &usage.MemoryLimit, errors, wg)
	go readMemoryCgroupInt64Async(id, LXC_MEM_USAGE_FILE, &usage.MemoryUsage.MemoryUsage, errors, wg)
	go readMemoryCgroupInt64Async(id, LXC_SWAP_MEM_LIMIT_FILE, &usage.SwapMemoryLimit, errors, wg)
	go readMemoryCgroupInt64Async(id, LXC_SWAP_MEM_USAGE_FILE, &usage.SwapMemoryUsage, errors, wg)

	go func() {
		wg.Wait()
		close(errors)
	}()

	for err := range errors {
		if err != nil {
			return usage, err
		}
	}

	usage.SwapUsage = usage.SwapMemoryUsage - usage.MemoryUsage.MemoryUsage
	usage.SwapLimit = usage.SwapMemoryLimit - usage.MemoryLimit
	usage.MaxSwapUsage = usage.MaxSwapMemoryUsage - usage.MaxMemoryUsage
	return usage, nil
}
예제 #3
0
func GetUsage(id string) (Usage, error) {
	id, err := docker.ExpandId(id)
	if err != nil {
		return Usage{}, err
	}
	usage := Usage{}
	usage.NetworkStat = netUsages[id]
	usage.RxBps = int64(float64(netUsages[id].Received.Bytes-previousNetUsages[id].Received.Bytes) / float64(config.RefreshTime))
	usage.TxBps = int64(float64(netUsages[id].Transmit.Bytes-previousNetUsages[id].Transmit.Bytes) / float64(config.RefreshTime))
	return usage, nil
}
예제 #4
0
func GetUsage(id string) (int64, error) {
	id, err := docker.ExpandId(id)
	if err != nil {
		log.Println("Error when expanding id:", err)
		return -1, err
	}
	if _, ok := previousCpuUsages[id]; !ok {
		return -1, nil
	}
	deltaCpuUsage := float64(cpuUsages[id] - previousCpuUsages[id])
	deltaSystemCpuUsage := float64(currentSystemUsage[id] - previousSystemUsage[id])

	return int64(deltaCpuUsage / deltaSystemCpuUsage * 100 * float64(runtime.NumCPU())), nil
}
예제 #5
0
func GetUsage(id string) (Usage, error) {
	id, err := docker.ExpandId(id)
	if err != nil {
		log.Println("Error when expanding id:", err)
		return Usage{}, err
	}
	if _, ok := previousCpuUsages[id]; !ok {
		return Usage{}, nil
	}
	deltaCpuUsage := float64(cpuUsages[id] - previousCpuUsages[id])
	deltaSystemCpuUsage := float64(currentSystemUsage[id] - previousSystemUsage[id])

	percents := int(deltaCpuUsage / deltaSystemCpuUsage * 100 * float64(runtime.NumCPU()))
	return Usage{
		UsageInPercents: percents,
	}, nil
}