podman

Форк
0
/
stats_common.go 
59 строк · 1.7 Кб
1
//go:build !remote && (linux || freebsd)
2

3
package libpod
4

5
import (
6
	"fmt"
7

8
	"github.com/containers/podman/v5/libpod/define"
9
)
10

11
// GetContainerStats gets the running stats for a given container.
12
// The previousStats is used to correctly calculate cpu percentages. You
13
// should pass nil if there is no previous stat for this container.
14
func (c *Container) GetContainerStats(previousStats *define.ContainerStats) (*define.ContainerStats, error) {
15
	stats := new(define.ContainerStats)
16
	stats.ContainerID = c.ID()
17
	stats.Name = c.Name()
18

19
	if c.config.NoCgroups {
20
		return nil, fmt.Errorf("cannot run top on container %s as it did not create a cgroup: %w", c.ID(), define.ErrNoCgroups)
21
	}
22

23
	if !c.batched {
24
		c.lock.Lock()
25
		defer c.lock.Unlock()
26
		if err := c.syncContainer(); err != nil {
27
			return stats, err
28
		}
29
	}
30

31
	// returns stats with the fields' default values respective of their type
32
	if c.state.State != define.ContainerStateRunning && c.state.State != define.ContainerStatePaused {
33
		return stats, nil
34
	}
35

36
	if previousStats == nil {
37
		previousStats = &define.ContainerStats{
38
			// if we have no prev stats use the container start time as prev time
39
			// otherwise we cannot correctly calculate the CPU percentage
40
			SystemNano: uint64(c.state.StartedTime.UnixNano()),
41
		}
42
	}
43

44
	netStats, err := getContainerNetIO(c)
45
	if err != nil {
46
		return nil, err
47
	}
48
	stats.Network = netStats
49

50
	if err := c.getPlatformContainerStats(stats, previousStats); err != nil {
51
		return nil, err
52
	}
53
	return stats, nil
54
}
55

56
// GetOnlineCPUs returns the number of online CPUs as set in the container cpu-set using sched_getaffinity
57
func GetOnlineCPUs(container *Container) (int, error) {
58
	return getOnlineCPUs(container)
59
}
60

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.