| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | from dataclasses import dataclass |
| 4 | from typing import Any |
| 5 | |
| 6 | from vllm.utils.gc_utils import ( |
| 7 | GCDebugConfig, |
| 8 | _compute_detailed_type, |
| 9 | _compute_top_gc_collected_objects, |
| 10 | ) |
| 11 | |
| 12 | |
| 13 | @dataclass |
| 14 | class Normal: |
| 15 | v: int |
| 16 | |
| 17 | |
| 18 | @dataclass |
| 19 | class ListWrapper: |
| 20 | vs: list[int] |
| 21 | |
| 22 | def __len__(self) -> int: |
| 23 | return len(self.vs) |
| 24 | |
| 25 | |
| 26 | def test_compute_detailed_type(): |
| 27 | assert ( |
| 28 | _compute_detailed_type(Normal(v=8)) |
| 29 | == "<class 'tests.utils_.test_gc_utils.Normal'>" |
| 30 | ) |
| 31 | |
| 32 | assert _compute_detailed_type([1, 2, 3]) == "<class 'list'>(size:3)" |
| 33 | assert _compute_detailed_type({4, 5}) == "<class 'set'>(size:2)" |
| 34 | assert _compute_detailed_type({6: 7}) == "<class 'dict'>(size:1)" |
| 35 | assert ( |
| 36 | _compute_detailed_type(ListWrapper(vs=[])) |
| 37 | == "<class 'tests.utils_.test_gc_utils.ListWrapper'>(size:0)" |
| 38 | ) |
| 39 | |
| 40 | |
| 41 | def test_compute_top_gc_collected_objects(): |
| 42 | objects: list[Any] = [ |
| 43 | [1, 2, 3], |
| 44 | [4, 5, 6], |
| 45 | [7, 8, 9], |
| 46 | [10, 11, 12], |
| 47 | {13, 14}, |
| 48 | {15: 16, 17: 18}, |
| 49 | Normal(v=19), |
| 50 | Normal(v=20), |
| 51 | Normal(v=21), |
| 52 | ] |
| 53 | assert _compute_top_gc_collected_objects(objects, top=-1) == "" |
| 54 | assert _compute_top_gc_collected_objects(objects, top=0) == "" |
| 55 | assert ( |
| 56 | _compute_top_gc_collected_objects(objects, top=1) |
| 57 | == " 4:<class 'list'>(size:3)" |
| 58 | ) |
| 59 | assert _compute_top_gc_collected_objects(objects, top=2) == "\n".join( |
| 60 | [ |
| 61 | " 4:<class 'list'>(size:3)", |
| 62 | " 3:<class 'tests.utils_.test_gc_utils.Normal'>", |
| 63 | ] |
| 64 | ) |
| 65 | assert _compute_top_gc_collected_objects(objects, top=3) == "\n".join( |
| 66 | [ |
| 67 | " 4:<class 'list'>(size:3)", |
| 68 | " 3:<class 'tests.utils_.test_gc_utils.Normal'>", |
| 69 | " 1:<class 'set'>(size:2)", |
| 70 | ] |
| 71 | ) |
| 72 | |
| 73 | |
| 74 | def test_gc_debug_config(): |
| 75 | assert not GCDebugConfig(None).enabled |
| 76 | assert not GCDebugConfig("").enabled |
| 77 | assert not GCDebugConfig("0").enabled |
| 78 | |
| 79 | config = GCDebugConfig("1") |
| 80 | assert config.enabled |
| 81 | assert config.top_objects == -1 |
| 82 | |
| 83 | config = GCDebugConfig('{"top_objects":5}') |
| 84 | assert config.enabled |
| 85 | assert config.top_objects == 5 |