diff --git a/cli/database/create.py b/cli/database/create.py index 30589ec0..ce75e5ee 100644 --- a/cli/database/create.py +++ b/cli/database/create.py @@ -35,6 +35,10 @@ def memory_statistics(): # 获取总内存大小(以字节为单位) total_memory = memory.total + pod_memory_limit = get_pod_memory_limit() + if pod_memory_limit != 0: + total_memory = pod_memory_limit + # 格式化内存大小 size_units = ["B", "KB", "MB", "GB", "TB"] unit_index = 0 diff --git a/cli/extractor/extractor.py b/cli/extractor/extractor.py index 205efe6e..9be1ff4e 100644 --- a/cli/extractor/extractor.py +++ b/cli/extractor/extractor.py @@ -164,6 +164,9 @@ def jar_extractor_cmd(extractor_path, source_root, database): # 获取内存信息 mem = psutil.virtual_memory() total_memory = mem.total + pod_memory_limit = get_pod_memory_limit() + if pod_memory_limit != 0: + total_memory = pod_memory_limit total_memory_gb = round(total_memory / (1024 ** 3)) logging.info("current memory is : %s GB", total_memory_gb) xmx = max(total_memory_gb - 1, 6) @@ -190,3 +193,21 @@ def extractor_run(language, source_root, database, timeout, options): logging.error("Not supported language: %s", language) return -1 + +def get_pod_memory_limit(): + # cgroup 文件系统路径 + memory_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes" + memory_limit = 0 + try: + with open(memory_limit_path, 'r') as f: + memory_limit = int(f.read().strip()) + except FileNotFoundError: + pass + except PermissionError: + logging.error("Permission denied when accessing cgroup files.") + except IOError as e: + logging.error(f"IO error occurred when accessing cgroup files: {e}") + except Exception as e: + logging.error(f"An unexpected error occurred: {e}") + return memory_limit +