From 55caa6051d675148aba009c85618c6d3adf85091 Mon Sep 17 00:00:00 2001 From: tjp_zju Date: Wed, 7 Jan 2026 14:42:20 +0800 Subject: [PATCH] refactor: find_loaded_library (#31866) Signed-off-by: tjp_zju Co-authored-by: Cyrus Leung --- vllm/device_allocator/cumem.py | 28 +------------------ .../device_communicators/cuda_wrapper.py | 28 +------------------ vllm/utils/system_utils.py | 27 ++++++++++++++++++ 3 files changed, 29 insertions(+), 54 deletions(-) diff --git a/vllm/device_allocator/cumem.py b/vllm/device_allocator/cumem.py index e9695698b..2f97288b6 100644 --- a/vllm/device_allocator/cumem.py +++ b/vllm/device_allocator/cumem.py @@ -19,37 +19,11 @@ import torch from vllm.logger import init_logger from vllm.utils.platform_utils import is_pin_memory_available +from vllm.utils.system_utils import find_loaded_library logger = init_logger(__name__) -def find_loaded_library(lib_name) -> str | None: - """ - According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html, - the file `/proc/self/maps` contains the memory maps of the process, which includes the - shared libraries loaded by the process. We can use this file to find the path of the - a loaded library. - """ # noqa - found_line = None - with open("/proc/self/maps") as f: - for line in f: - if lib_name in line: - found_line = line - break - if found_line is None: - # the library is not loaded in the current process - return None - # if lib_name is libcudart, we need to match a line with: - # address /path/to/libcudart-hash.so.11.0 - start = found_line.index("/") - path = found_line[start:].strip() - filename = path.split("/")[-1] - assert filename.rpartition(".so")[0].startswith(lib_name), ( - f"Unexpected filename: {filename} for library {lib_name}" - ) - return path - - cumem_available = False try: from vllm.cumem_allocator import ( diff --git a/vllm/distributed/device_communicators/cuda_wrapper.py b/vllm/distributed/device_communicators/cuda_wrapper.py index 6aadab33e..422991ca9 100644 --- a/vllm/distributed/device_communicators/cuda_wrapper.py +++ b/vllm/distributed/device_communicators/cuda_wrapper.py @@ -15,6 +15,7 @@ import torch # noqa import vllm.envs as envs from vllm.logger import init_logger from vllm.platforms import current_platform +from vllm.utils.system_utils import find_loaded_library logger = init_logger(__name__) @@ -37,33 +38,6 @@ class Function: argtypes: list[Any] -def find_loaded_library(lib_name) -> str | None: - """ - According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html, - the file `/proc/self/maps` contains the memory maps of the process, which includes the - shared libraries loaded by the process. We can use this file to find the path of the - a loaded library. - """ # noqa - found = False - with open("/proc/self/maps") as f: - for line in f: - if lib_name in line: - found = True - break - if not found: - # the library is not loaded in the current process - return None - # if lib_name is libcudart, we need to match a line with: - # address /path/to/libcudart-hash.so.11.0 - start = line.index("/") - path = line[start:].strip() - filename = path.split("/")[-1] - assert filename.rpartition(".so")[0].startswith(lib_name), ( - f"Unexpected filename: {filename} for library {lib_name}" - ) - return path - - class CudaRTLibrary: exported_functions = [ # ​cudaError_t cudaSetDevice ( int device ) diff --git a/vllm/utils/system_utils.py b/vllm/utils/system_utils.py index 76cac59c1..aa14b3951 100644 --- a/vllm/utils/system_utils.py +++ b/vllm/utils/system_utils.py @@ -267,3 +267,30 @@ def set_ulimit(target_soft_limit: int = 65535): current_soft, e, ) + + +def find_loaded_library(lib_name: str) -> str | None: + """ + According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html, + the file `/proc/self/maps` contains the memory maps of the process, which includes the + shared libraries loaded by the process. We can use this file to find the path of the + a loaded library. + """ # noqa + found_line = None + with open("/proc/self/maps") as f: + for line in f: + if lib_name in line: + found_line = line + break + if found_line is None: + # the library is not loaded in the current process + return None + # if lib_name is libcudart, we need to match a line with: + # address /path/to/libcudart-hash.so.11.0 + start = found_line.index("/") + path = found_line[start:].strip() + filename = path.split("/")[-1] + assert filename.rpartition(".so")[0].startswith(lib_name), ( + f"Unexpected filename: {filename} for library {lib_name}" + ) + return path