From 29d16e44b39f509c1d08e1fd941fef5af537e4f4 Mon Sep 17 00:00:00 2001 From: mahuaizhi Date: Fri, 9 Aug 2024 11:31:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[feat]jar=20extractor=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=86=85=E5=AD=98=E4=BF=A1=E6=81=AF=E6=94=AF=E6=8C=81pod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/database/create.py | 4 ++++ cli/extractor/extractor.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) 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..7d4083a8 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,15 @@ 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 + return memory_limit + From 63586b8ade86e02039dffffc61c1e93d5471d737 Mon Sep 17 00:00:00 2001 From: mahuaizhi Date: Fri, 9 Aug 2024 14:45:38 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[feat]jar=20extractor=20=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=86=85=E5=AD=98=E4=BF=A1=E6=81=AF=E6=94=AF=E6=8C=81pod=20?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli/extractor/extractor.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/extractor/extractor.py b/cli/extractor/extractor.py index 7d4083a8..9be1ff4e 100644 --- a/cli/extractor/extractor.py +++ b/cli/extractor/extractor.py @@ -203,5 +203,11 @@ def get_pod_memory_limit(): 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