|
| 1 | +package fs2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/opencontainers/runc/libcontainer/cgroups" |
| 10 | +) |
| 11 | + |
| 12 | +const exampleMemoryStatData = `anon 790425600 |
| 13 | +file 6502666240 |
| 14 | +kernel_stack 7012352 |
| 15 | +pagetables 8867840 |
| 16 | +percpu 2445520 |
| 17 | +sock 40960 |
| 18 | +shmem 6721536 |
| 19 | +file_mapped 656187392 |
| 20 | +file_dirty 1122304 |
| 21 | +file_writeback 0 |
| 22 | +swapcached 10 |
| 23 | +anon_thp 438304768 |
| 24 | +file_thp 0 |
| 25 | +shmem_thp 0 |
| 26 | +inactive_anon 892223488 |
| 27 | +active_anon 2973696 |
| 28 | +inactive_file 5307346944 |
| 29 | +active_file 1179316224 |
| 30 | +unevictable 31477760 |
| 31 | +slab_reclaimable 348866240 |
| 32 | +slab_unreclaimable 10099808 |
| 33 | +slab 358966048 |
| 34 | +workingset_refault_anon 0 |
| 35 | +workingset_refault_file 0 |
| 36 | +workingset_activate_anon 0 |
| 37 | +workingset_activate_file 0 |
| 38 | +workingset_restore_anon 0 |
| 39 | +workingset_restore_file 0 |
| 40 | +workingset_nodereclaim 0 |
| 41 | +pgfault 103216687 |
| 42 | +pgmajfault 6879 |
| 43 | +pgrefill 0 |
| 44 | +pgscan 0 |
| 45 | +pgsteal 0 |
| 46 | +pgactivate 1110217 |
| 47 | +pgdeactivate 292 |
| 48 | +pglazyfree 267 |
| 49 | +pglazyfreed 0 |
| 50 | +thp_fault_alloc 57411 |
| 51 | +thp_collapse_alloc 443` |
| 52 | + |
| 53 | +func TestStatMemoryPodCgroupNotFound(t *testing.T) { |
| 54 | + // We're using a fake cgroupfs. |
| 55 | + cgroups.TestMode = true |
| 56 | + fakeCgroupDir := t.TempDir() |
| 57 | + |
| 58 | + // only write memory.stat to ensure pod cgroup usage |
| 59 | + // still reads memory.current. |
| 60 | + statPath := filepath.Join(fakeCgroupDir, "memory.stat") |
| 61 | + if err := os.WriteFile(statPath, []byte(exampleMemoryStatData), 0o644); err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + gotStats := cgroups.NewStats() |
| 66 | + |
| 67 | + // use a fake root path to mismatch the file we wrote. |
| 68 | + // this triggers the non-root path which should fail to find memory.current. |
| 69 | + err := statMemory(fakeCgroupDir, gotStats) |
| 70 | + if err == nil { |
| 71 | + t.Errorf("expected error when statting memory for cgroupv2 root, but was nil") |
| 72 | + } |
| 73 | + |
| 74 | + if !strings.Contains(err.Error(), "memory.current: no such file or directory") { |
| 75 | + t.Errorf("expected error to contain 'memory.current: no such file or directory', but was %s", err.Error()) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestStatMemoryPodCgroup(t *testing.T) { |
| 80 | + // We're using a fake cgroupfs. |
| 81 | + cgroups.TestMode = true |
| 82 | + fakeCgroupDir := t.TempDir() |
| 83 | + |
| 84 | + statPath := filepath.Join(fakeCgroupDir, "memory.stat") |
| 85 | + if err := os.WriteFile(statPath, []byte(exampleMemoryStatData), 0o644); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + |
| 89 | + if err := os.WriteFile(filepath.Join(fakeCgroupDir, "memory.current"), []byte("123456789"), 0o644); err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + if err := os.WriteFile(filepath.Join(fakeCgroupDir, "memory.max"), []byte("999999999"), 0o644); err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + gotStats := cgroups.NewStats() |
| 98 | + |
| 99 | + // use a fake root path to trigger the pod cgroup lookup. |
| 100 | + err := statMemory(fakeCgroupDir, gotStats) |
| 101 | + if err != nil { |
| 102 | + t.Errorf("expected no error when statting memory for cgroupv2 root, but got %#+v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // result should be "memory.current" |
| 106 | + var expectedUsageBytes uint64 = 123456789 |
| 107 | + if gotStats.MemoryStats.Usage.Usage != expectedUsageBytes { |
| 108 | + t.Errorf("parsed cgroupv2 memory.stat doesn't match expected result: \ngot %#v\nexpected %#v\n", gotStats.MemoryStats.Usage.Usage, expectedUsageBytes) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestRootStatsFromMeminfo(t *testing.T) { |
| 113 | + stats := &cgroups.Stats{ |
| 114 | + MemoryStats: cgroups.MemoryStats{ |
| 115 | + Stats: map[string]uint64{ |
| 116 | + "anon": 790425600, |
| 117 | + "file": 6502666240, |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + if err := rootStatsFromMeminfo(stats); err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + // result is anon + file |
| 127 | + var expectedUsageBytes uint64 = 7293091840 |
| 128 | + if stats.MemoryStats.Usage.Usage != expectedUsageBytes { |
| 129 | + t.Errorf("parsed cgroupv2 memory.stat doesn't match expected result: \ngot %d\nexpected %d\n", stats.MemoryStats.Usage.Usage, expectedUsageBytes) |
| 130 | + } |
| 131 | + |
| 132 | + // swap is adjusted to mem+swap |
| 133 | + if stats.MemoryStats.SwapUsage.Usage < stats.MemoryStats.Usage.Usage { |
| 134 | + t.Errorf("swap usage %d should be at least mem usage %d", stats.MemoryStats.SwapUsage.Usage, stats.MemoryStats.Usage.Usage) |
| 135 | + } |
| 136 | + if stats.MemoryStats.SwapUsage.Limit < stats.MemoryStats.Usage.Limit { |
| 137 | + t.Errorf("swap limit %d should be at least mem limit %d", stats.MemoryStats.SwapUsage.Limit, stats.MemoryStats.Usage.Limit) |
| 138 | + } |
| 139 | +} |
0 commit comments