podman

Форк
0
/
stats_freebsd.go 
132 строки · 4.3 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"fmt"
7
	"math"
8
	"time"
9

10
	"github.com/containers/podman/v5/libpod/define"
11
	"github.com/containers/podman/v5/pkg/rctl"
12
	"github.com/containers/storage/pkg/system"
13
	"github.com/sirupsen/logrus"
14
)
15

16
// getPlatformContainerStats gets the platform-specific running stats
17
// for a given container.  The previousStats is used to correctly
18
// calculate cpu percentages. You should pass nil if there is no
19
// previous stat for this container.
20
func (c *Container) getPlatformContainerStats(stats *define.ContainerStats, previousStats *define.ContainerStats) error {
21
	now := uint64(time.Now().UnixNano())
22

23
	jailName, err := c.jailName()
24
	if err != nil {
25
		return fmt.Errorf("getting jail name: %w", err)
26
	}
27

28
	entries, err := rctl.GetRacct("jail:" + jailName)
29
	if err != nil {
30
		return fmt.Errorf("unable to read accounting for %s: %w", jailName, err)
31
	}
32

33
	// If the current total usage is less than what was previously
34
	// recorded then it means the container was restarted and runs
35
	// in a new jail
36
	if dur, ok := entries["wallclock"]; ok {
37
		if previousStats.Duration > dur*1000000000 {
38
			previousStats = &define.ContainerStats{}
39
		}
40
	}
41

42
	for key, val := range entries {
43
		switch key {
44
		case "cputime": // CPU time, in seconds
45
			stats.CPUNano = val * 1000000000
46
			stats.AvgCPU = calculateCPUPercent(stats.CPUNano, 0, now, uint64(c.state.StartedTime.UnixNano()))
47
		case "datasize": // data size, in bytes
48
		case "stacksize": // stack size, in bytes
49
		case "coredumpsize": // core dump size, in bytes
50
		case "memoryuse": // resident set size, in bytes
51
			stats.MemUsage = val
52
		case "memorylocked": // locked memory, in bytes
53
		case "maxproc": // number of processes
54
			stats.PIDs = val
55
		case "openfiles": // file descriptor table size
56
		case "vmemoryuse": // address space limit, in bytes
57
		case "pseudoterminals": // number of PTYs
58
		case "swapuse": // swap space that may be reserved or used, in bytes
59
		case "nthr": // number of threads
60
		case "msgqqueued": // number of queued SysV messages
61
		case "msgqsize": // SysV message queue size, in bytes
62
		case "nmsgq": // number of SysV message queues
63
		case "nsem": // number of SysV semaphores
64
		case "nsemop": // number of SysV semaphores modified in a single semop(2) call
65
		case "nshm": // number of SysV shared memory segments
66
		case "shmsize": // SysV shared memory size, in bytes
67
		case "wallclock": // wallclock time, in seconds
68
			stats.Duration = val * 1000000000
69
			stats.UpTime = time.Duration(stats.Duration)
70
		case "pcpu": // %CPU, in percents of a single CPU core
71
			stats.CPU = float64(val)
72
		case "readbps": // filesystem reads, in bytes per second
73
			stats.BlockInput = val
74
		case "writebps": // filesystem writes, in bytes per second
75
			stats.BlockOutput = val
76
		case "readiops": // filesystem reads, in operations per second
77
		case "writeiops": // filesystem writes, in operations per second
78
		}
79
	}
80
	stats.MemLimit = c.getMemLimit()
81
	stats.SystemNano = now
82

83
	return nil
84
}
85

86
// getMemLimit returns the memory limit for a container
87
func (c *Container) getMemLimit() uint64 {
88
	memLimit := uint64(math.MaxUint64)
89

90
	resources := c.LinuxResources()
91
	if resources != nil && resources.Memory != nil && resources.Memory.Limit != nil {
92
		memLimit = uint64(*resources.Memory.Limit)
93
	}
94

95
	mi, err := system.ReadMemInfo()
96
	if err != nil {
97
		logrus.Errorf("ReadMemInfo error: %v", err)
98
		return 0
99
	}
100

101
	//nolint:unconvert
102
	physicalLimit := uint64(mi.MemTotal)
103

104
	if memLimit <= 0 || memLimit > physicalLimit {
105
		return physicalLimit
106
	}
107

108
	return memLimit
109
}
110

111
// calculateCPUPercent calculates the cpu usage using the latest measurement in stats.
112
// previousCPU is the last value of stats.CPU.Usage.Total measured at the time previousSystem.
113
//
114
//	(now - previousSystem) is the time delta in nanoseconds, between the measurement in previousCPU
115
//
116
// and the updated value in stats.
117
func calculateCPUPercent(currentCPU, previousCPU, now, previousSystem uint64) float64 {
118
	var (
119
		cpuPercent  = 0.0
120
		cpuDelta    = float64(currentCPU - previousCPU)
121
		systemDelta = float64(now - previousSystem)
122
	)
123
	if systemDelta > 0.0 && cpuDelta > 0.0 {
124
		// gets a ratio of container cpu usage total, and multiplies that by 100 to get a percentage
125
		cpuPercent = (cpuDelta / systemDelta) * 100
126
	}
127
	return cpuPercent
128
}
129

130
func getOnlineCPUs(container *Container) (int, error) {
131
	return 0, nil
132
}
133

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

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

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

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