본문으로 건너뛰기

[Ray] 메모리 모니터 리팩터링: cgroup 경로 주입으로 테스트 가능성 확보

PR 링크: ray-project/ray#60752 상태: Merged | 변경: +500 / -305

들어가며

메모리 모니터는 시스템 메모리 사용량을 감시하고 임계치를 초과하면 워커를 종료합니다. 기존 구현은 /sys/fs/cgroup//proc/meminfo를 하드코딩으로 읽어 실제 시스템 상태에 의존했기 때문에 테스트가 불안정했습니다. 이 PR은 경로를 주입할 수 있게 리팩터링하여 가짜 cgroup으로 메모리 사용량을 시뮬레이션할 수 있게 합니다.

핵심 코드 분석

생성자에 cgroup 경로 주입

Before:

MemoryMonitor::MemoryMonitor(
    instrumented_io_context &io_service,
    float usage_threshold,
    int64_t min_memory_free_bytes,
    uint64_t monitor_interval_ms,
    MemoryUsageRefreshCallback monitor_callback)

After:

MemoryMonitor::MemoryMonitor(
    instrumented_io_context &io_service,
    float usage_threshold,
    int64_t min_memory_free_bytes,
    uint64_t monitor_interval_ms,
    MemoryUsageRefreshCallback monitor_callback,
    const std::string root_cgroup_path)  // 새 매개변수

메모리 스냅샷을 단일 함수로 통합

Before:

// 부분 구성이 가능한 위험한 패턴
auto [used_memory_bytes, total_memory_bytes] = GetMemoryBytes();
MemorySnapshot system_memory;
system_memory.used_bytes = used_mem_bytes;
system_memory.total_bytes = total_mem_bytes;

After:

// 단일 호출로 완전한 스냅샷 반환
SystemMemorySnapshot cur_memory_snapshot =
    TakeSystemMemorySnapshot(root_cgroup_path_);

cgroup 경로를 동적으로 조합

Before:

if (std::filesystem::exists(kCgroupsV2MemoryMaxPath)) {
    std::ifstream mem_file(kCgroupsV2MemoryMaxPath, ...);

After:

std::string cgroupV2MemoryMaxPath = root_cgroup_path + "/" + kCgroupsV2MemoryMaxPath;
if (std::filesystem::exists(cgroupV2MemoryMaxPath)) {
    std::ifstream mem_file(cgroupV2MemoryMaxPath, ...);

왜 이게 좋은가

  • 테스트에서 가짜 cgroup 디렉토리를 생성하고 경로를 주입하여 임의의 메모리 사용량을 시뮬레이션할 수 있습니다
  • MemorySnapshot을 부분적으로 구성하던 패턴이 제거되어 불완전한 스냅샷으로 인한 버그 가능성이 사라졌습니다
  • 콜백 시그니처가 move 시맨틱을 사용하여 불필요한 복사를 방지합니다
  • GetMemoryBytesstatic 함수 TakeSystemMemorySnapshot으로 변경되어 인스턴스 상태에 의존하지 않습니다
  • Ray의 리소스 격리 시리즈(2/n)의 일부로, 향후 더 정교한 메모리 관리 테스트의 기반이 됩니다

참고 자료

댓글

관련 포스트

PR Analysis 의 다른글