|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +import gc |
| 4 | +import json |
| 5 | +import time |
| 6 | +from collections import Counter |
| 7 | +from contextlib import suppress |
| 8 | +from typing import Any, Optional |
| 9 | + |
| 10 | +from vllm.envs import VLLM_GC_DEBUG |
| 11 | +from vllm.logger import init_logger |
| 12 | + |
| 13 | +logger = init_logger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class GCDebugConfig: |
| 17 | + """ |
| 18 | + Config for GC Debugger. |
| 19 | + - 0: disable GC debugger |
| 20 | + - 1: enable GC debugger with gc.collect elpased times |
| 21 | + - '{"top_objects":5}': enable GC debugger with top 5 collected objects |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, gc_debug_conf: Optional[str] = None) -> None: |
| 25 | + self.enabled: bool = False |
| 26 | + self.top_objects: int = -1 |
| 27 | + |
| 28 | + if not gc_debug_conf or gc_debug_conf == "0": |
| 29 | + pass |
| 30 | + elif gc_debug_conf == "1": |
| 31 | + self.enabled = True |
| 32 | + else: |
| 33 | + try: |
| 34 | + json_conf = json.loads(gc_debug_conf) |
| 35 | + self.enabled = True |
| 36 | + self.top_objects = json_conf.get("top_objects", -1) |
| 37 | + except Exception: |
| 38 | + self.enabled = False |
| 39 | + logger.error("Failed to parse VLLM_GC_DEBUG(%s)", |
| 40 | + VLLM_GC_DEBUG) |
| 41 | + logger.info("GC Debug Config. %s", str(self)) |
| 42 | + |
| 43 | + def __repr__(self) -> str: |
| 44 | + return f"enabled:{self.enabled},top_objects:{self.top_objects}" |
| 45 | + |
| 46 | + |
| 47 | +class GCDebugger: |
| 48 | + """ |
| 49 | + Debugger for GC which logs helpful information for GC understanding. |
| 50 | + To enable, you should call maybe_attach_gc_debug_callback in the process. |
| 51 | + """ |
| 52 | + |
| 53 | + def __init__(self, config: GCDebugConfig) -> None: |
| 54 | + self.config = config |
| 55 | + # Start time in micro second of this GC cycle |
| 56 | + self.start_time_ns: int = time.monotonic_ns() |
| 57 | + # If config.top_objects is positive, |
| 58 | + # compute top collected objects by object types |
| 59 | + self.gc_top_collected_objects: str = "" |
| 60 | + |
| 61 | + def handle(self, phase: str, info: dict[str, int]) -> None: |
| 62 | + """ |
| 63 | + Handles a GC event (e.g. GC start or GC finish) |
| 64 | + """ |
| 65 | + generation = info.get("generation") |
| 66 | + if generation is None: |
| 67 | + return |
| 68 | + if phase == "start": |
| 69 | + # Before GC started, record GC start time |
| 70 | + # and top collected objects |
| 71 | + self.start_time_ns = time.monotonic_ns() |
| 72 | + self.gc_top_collected_objects = _compute_top_gc_collected_objects( |
| 73 | + gc.get_objects(generation), self.config.top_objects) |
| 74 | + elif phase == "stop": |
| 75 | + # After GC finished, Record GC elapsed time and |
| 76 | + # optionally top collected objects |
| 77 | + elpased_ms = (time.monotonic_ns() - self.start_time_ns) / 1e6 |
| 78 | + logger.info( |
| 79 | + "GC took %.3fms to complete. " |
| 80 | + "Collected %s objects in GC generation %d.%s", |
| 81 | + elpased_ms, |
| 82 | + str(info.get("collected", "?")), |
| 83 | + generation, |
| 84 | + (f" Top collected objects: \n{self.gc_top_collected_objects}" |
| 85 | + if self.gc_top_collected_objects else ""), |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +def maybe_attach_gc_debug_callback() -> None: |
| 90 | + """ |
| 91 | + Attached a callback for GC debug when VLLM_GC_DEBUG is enabled. |
| 92 | + """ |
| 93 | + config = GCDebugConfig(VLLM_GC_DEBUG) |
| 94 | + if config.enabled: |
| 95 | + debugger: GCDebugger = GCDebugger(config) |
| 96 | + |
| 97 | + def gc_callback(phase: str, info: dict[str, int]) -> None: |
| 98 | + debugger.handle(phase, info) |
| 99 | + |
| 100 | + gc.callbacks.append(gc_callback) |
| 101 | + |
| 102 | + |
| 103 | +def _compute_detailed_type(o: Any) -> str: |
| 104 | + """ |
| 105 | + Detailed object type. |
| 106 | +
|
| 107 | + TODO(Jialin): Further enhance the detailed type with element types for |
| 108 | + easier debugging. We tried but occasionally it would run into signals |
| 109 | + which kills the engine. |
| 110 | + """ |
| 111 | + size_str: str = "" |
| 112 | + # Object doesn't support len() - this can happen with type objects |
| 113 | + # or other objects that don't implement __len__ properly |
| 114 | + with suppress(Exception): |
| 115 | + size_str = f"(size:{len(o)})" |
| 116 | + return f"{str(type(o))}{size_str}" |
| 117 | + |
| 118 | + |
| 119 | +def _compute_top_gc_collected_objects(objects: list[Any], top: int) -> str: |
| 120 | + """ |
| 121 | + Group collected objects by types. |
| 122 | + """ |
| 123 | + if top <= 0: |
| 124 | + return "" |
| 125 | + object_types = [_compute_detailed_type(o) for o in objects] |
| 126 | + return "\n".join( |
| 127 | + f"{count:>5}:{object_type}" |
| 128 | + for object_type, count in Counter(object_types).most_common(top)) |
0 commit comments