|
14 | 14 |
|
15 | 15 | #include "ray/common/memory_monitor_test_fixture.h" |
16 | 16 |
|
| 17 | +#include "ray/common/memory_monitor_utils.h" |
17 | 18 | #include "ray/util/logging.h" |
18 | 19 |
|
19 | 20 | namespace ray { |
@@ -41,31 +42,82 @@ std::string MemoryMonitorTestFixture::MockProcMemoryUsage(pid_t pid, |
41 | 42 | return proc_dir; |
42 | 43 | } |
43 | 44 |
|
44 | | -std::string MemoryMonitorTestFixture::MockCgroupMemoryUsage(int64_t total_bytes, |
45 | | - int64_t current_bytes, |
46 | | - int64_t inactive_file_bytes, |
47 | | - int64_t active_file_bytes) { |
| 45 | +std::string MemoryMonitorTestFixture::MockCgroupv2MemoryUsage( |
| 46 | + int64_t total_bytes, |
| 47 | + int64_t current_bytes, |
| 48 | + std::optional<int64_t> anon_memory_bytes, |
| 49 | + std::optional<int64_t> shmem_memory_bytes, |
| 50 | + int64_t inactive_file_bytes, |
| 51 | + int64_t active_file_bytes) { |
48 | 52 | auto temp_dir_or = TempDirectory::Create(); |
49 | 53 | RAY_CHECK(temp_dir_or.ok()) << "Failed to create temp directory: " |
50 | 54 | << temp_dir_or.status().message(); |
51 | 55 | mock_cgroup_dirs_.push_back(std::move(temp_dir_or.value())); |
52 | 56 |
|
53 | 57 | const std::string &cgroup_path = mock_cgroup_dirs_.back()->GetPath(); |
54 | 58 |
|
55 | | - mock_cgroup_files_.push_back(std::make_unique<TempFile>(cgroup_path + "/memory.max")); |
| 59 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 60 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV2MemoryMaxPath)); |
56 | 61 | mock_cgroup_files_.back()->AppendLine(std::to_string(total_bytes) + "\n"); |
57 | 62 |
|
58 | | - mock_cgroup_files_.push_back( |
59 | | - std::make_unique<TempFile>(cgroup_path + "/memory.current")); |
| 63 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 64 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV2MemoryUsagePath)); |
60 | 65 | mock_cgroup_files_.back()->AppendLine(std::to_string(current_bytes) + "\n"); |
61 | 66 |
|
62 | | - mock_cgroup_files_.push_back(std::make_unique<TempFile>(cgroup_path + "/memory.stat")); |
63 | | - mock_cgroup_files_.back()->AppendLine("anon 123456\n"); |
64 | | - mock_cgroup_files_.back()->AppendLine("inactive_file " + |
65 | | - std::to_string(inactive_file_bytes) + "\n"); |
66 | | - mock_cgroup_files_.back()->AppendLine("active_file " + |
67 | | - std::to_string(active_file_bytes) + "\n"); |
68 | | - mock_cgroup_files_.back()->AppendLine("some_other_key 789\n"); |
| 67 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 68 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV2MemoryStatPath)); |
| 69 | + if (anon_memory_bytes.has_value()) { |
| 70 | + mock_cgroup_files_.back()->AppendLine( |
| 71 | + std::string(MemoryMonitorUtils::kCgroupsV2MemoryAnonKey) + " " + |
| 72 | + std::to_string(*anon_memory_bytes) + "\n"); |
| 73 | + } |
| 74 | + if (shmem_memory_bytes.has_value()) { |
| 75 | + mock_cgroup_files_.back()->AppendLine( |
| 76 | + std::string(MemoryMonitorUtils::kCgroupsV2MemoryShmemKey) + " " + |
| 77 | + std::to_string(*shmem_memory_bytes) + "\n"); |
| 78 | + } |
| 79 | + mock_cgroup_files_.back()->AppendLine( |
| 80 | + std::string(MemoryMonitorUtils::kCgroupsV2MemoryStatInactiveFileKey) + " " + |
| 81 | + std::to_string(inactive_file_bytes) + "\n"); |
| 82 | + mock_cgroup_files_.back()->AppendLine( |
| 83 | + std::string(MemoryMonitorUtils::kCgroupsV2MemoryStatActiveFileKey) + " " + |
| 84 | + std::to_string(active_file_bytes) + "\n"); |
| 85 | + |
| 86 | + return cgroup_path; |
| 87 | +} |
| 88 | + |
| 89 | +std::string MemoryMonitorTestFixture::MockCgroupv1MemoryUsage(int64_t total_bytes, |
| 90 | + int64_t current_bytes, |
| 91 | + int64_t inactive_file_bytes, |
| 92 | + int64_t active_file_bytes) { |
| 93 | + auto temp_dir_or = TempDirectory::Create(); |
| 94 | + RAY_CHECK(temp_dir_or.ok()) << "Failed to create temp directory: " |
| 95 | + << temp_dir_or.status().message(); |
| 96 | + mock_cgroup_dirs_.push_back(std::move(temp_dir_or.value())); |
| 97 | + |
| 98 | + const std::string &cgroup_path = mock_cgroup_dirs_.back()->GetPath(); |
| 99 | + |
| 100 | + auto memory_dir_or = TempDirectory::Create(cgroup_path + "/memory"); |
| 101 | + RAY_CHECK(memory_dir_or.ok()) |
| 102 | + << "Failed to create temp directory: " << memory_dir_or.status().message(); |
| 103 | + mock_cgroup_dirs_.push_back(std::move(memory_dir_or.value())); |
| 104 | + |
| 105 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 106 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV1MemoryMaxPath)); |
| 107 | + mock_cgroup_files_.back()->AppendLine(std::to_string(total_bytes) + "\n"); |
| 108 | + |
| 109 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 110 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV1MemoryUsagePath)); |
| 111 | + mock_cgroup_files_.back()->AppendLine(std::to_string(current_bytes) + "\n"); |
| 112 | + |
| 113 | + mock_cgroup_files_.push_back(std::make_unique<TempFile>( |
| 114 | + cgroup_path + "/" + MemoryMonitorUtils::kCgroupsV1MemoryStatPath)); |
| 115 | + mock_cgroup_files_.back()->AppendLine( |
| 116 | + std::string(MemoryMonitorUtils::kCgroupsV1MemoryStatInactiveFileKey) + " " + |
| 117 | + std::to_string(inactive_file_bytes) + "\n"); |
| 118 | + mock_cgroup_files_.back()->AppendLine( |
| 119 | + std::string(MemoryMonitorUtils::kCgroupsV1MemoryStatActiveFileKey) + " " + |
| 120 | + std::to_string(active_file_bytes) + "\n"); |
69 | 121 |
|
70 | 122 | return cgroup_path; |
71 | 123 | } |
|
0 commit comments