|
17 | 17 | package mem
|
18 | 18 |
|
19 | 19 | import (
|
20 |
| - "context" |
21 |
| - "fmt" |
22 |
| - "github.com/chaosblade-io/chaosblade-exec-os/exec" |
23 |
| - "github.com/chaosblade-io/chaosblade-spec-go/channel" |
24 |
| - "github.com/chaosblade-io/chaosblade-spec-go/log" |
25 |
| - "github.com/containerd/cgroups" |
26 |
| - "github.com/shirou/gopsutil/mem" |
27 |
| - "strconv" |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "github.com/chaosblade-io/chaosblade-exec-os/exec" |
| 23 | + "github.com/chaosblade-io/chaosblade-spec-go/channel" |
| 24 | + "github.com/chaosblade-io/chaosblade-spec-go/log" |
| 25 | + "github.com/containerd/cgroups" |
| 26 | + "github.com/shirou/gopsutil/mem" |
| 27 | + "strconv" |
28 | 28 | )
|
29 | 29 |
|
30 | 30 | func getAvailableAndTotal(ctx context.Context, burnMemMode string, includeBufferCache bool) (int64, int64, error) {
|
31 | 31 |
|
32 |
| - pid := ctx.Value(channel.NSTargetFlagName) |
33 |
| - total := int64(0) |
34 |
| - available := int64(0) |
| 32 | + pid := ctx.Value(channel.NSTargetFlagName) |
| 33 | + total := int64(0) |
| 34 | + available := int64(0) |
35 | 35 |
|
36 |
| - if pid != nil { |
37 |
| - p, err := strconv.Atoi(pid.(string)) |
38 |
| - if err != nil { |
39 |
| - return 0, 0, fmt.Errorf("load cgroup error, %v", err) |
40 |
| - } |
| 36 | + if pid != nil { |
| 37 | + p, err := strconv.Atoi(pid.(string)) |
| 38 | + if err != nil { |
| 39 | + return 0, 0, fmt.Errorf("load cgroup error, %v", err) |
| 40 | + } |
41 | 41 |
|
42 |
| - cgroupRoot := ctx.Value("cgroup-root") |
43 |
| - if cgroupRoot == "" { |
44 |
| - cgroupRoot = "/sys/fs/cgroup/" |
45 |
| - } |
| 42 | + cgroupRoot := ctx.Value("cgroup-root") |
| 43 | + if cgroupRoot == "" { |
| 44 | + cgroupRoot = "/sys/fs/cgroup/" |
| 45 | + } |
46 | 46 |
|
47 |
| - log.Debugf(ctx, "get mem useage by cgroup, root path: %s", cgroupRoot) |
| 47 | + log.Debugf(ctx, "get mem useage by cgroup, root path: %s", cgroupRoot) |
48 | 48 |
|
49 |
| - cgroup, err := cgroups.Load(exec.Hierarchy(cgroupRoot.(string)), exec.PidPath(p)) |
50 |
| - if err != nil { |
51 |
| - return 0, 0, fmt.Errorf("load cgroup error, %v", err) |
52 |
| - } |
53 |
| - stats, err := cgroup.Stat(cgroups.IgnoreNotExist) |
54 |
| - if err != nil { |
55 |
| - return 0, 0, fmt.Errorf("load cgroup stat error, %v", err) |
56 |
| - } |
57 |
| - if stats != nil && stats.Memory.Usage.Limit < PageCounterMax { |
58 |
| - total = int64(stats.Memory.Usage.Limit) |
59 |
| - available = total - int64(stats.Memory.Usage.Usage) |
60 |
| - if burnMemMode == "ram" && !includeBufferCache { |
61 |
| - available = available + int64(stats.Memory.Cache) |
62 |
| - } |
63 |
| - } |
64 |
| - return total, available, nil |
65 |
| - } |
| 49 | + cgroup, err := cgroups.Load(exec.Hierarchy(cgroupRoot.(string)), exec.PidPath(p)) |
| 50 | + if err != nil { |
| 51 | + return 0, 0, fmt.Errorf("load cgroup error, %v", err) |
| 52 | + } |
| 53 | + stats, err := cgroup.Stat(cgroups.IgnoreNotExist) |
| 54 | + if err != nil { |
| 55 | + return 0, 0, fmt.Errorf("load cgroup stat error, %v", err) |
| 56 | + } |
| 57 | + if stats != nil && stats.Memory.Usage.Limit < PageCounterMax { |
| 58 | + total = int64(stats.Memory.Usage.Limit) |
| 59 | + available = total - int64(stats.Memory.Usage.Usage) |
| 60 | + if burnMemMode == "ram" && !includeBufferCache { |
| 61 | + available = available + int64(stats.Memory.Cache) |
| 62 | + } |
| 63 | + return total, available, nil |
| 64 | + } |
| 65 | + } |
66 | 66 |
|
67 |
| - virtualMemory, err := mem.VirtualMemory() |
68 |
| - if err != nil { |
69 |
| - return 0, 0, err |
70 |
| - } |
71 |
| - total = int64(virtualMemory.Total) |
72 |
| - available = int64(virtualMemory.Free) |
73 |
| - if burnMemMode == "ram" && !includeBufferCache { |
74 |
| - available = available + int64(virtualMemory.Buffers+virtualMemory.Cached) |
75 |
| - } |
76 |
| - return total, available, nil |
| 67 | + virtualMemory, err := mem.VirtualMemory() |
| 68 | + if err != nil { |
| 69 | + return 0, 0, err |
| 70 | + } |
| 71 | + total = int64(virtualMemory.Total) |
| 72 | + available = int64(virtualMemory.Free) |
| 73 | + if burnMemMode == "ram" && !includeBufferCache { |
| 74 | + available = available + int64(virtualMemory.Buffers+virtualMemory.Cached) |
| 75 | + } |
| 76 | + return total, available, nil |
77 | 77 | }
|
0 commit comments