Skip to content

Commit 711d834

Browse files
authored
Merge pull request #65 from Wadud-Ma/feature/jar_extractor
[feat]jar extractor get memory information support pod
2 parents a37402a + 0aba41f commit 711d834

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

Diff for: cli/database/create.py

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def memory_statistics():
3434

3535
# 获取总内存大小(以字节为单位)
3636
total_memory = memory.total
37+
pod_memory_limit = get_pod_memory_limit()
38+
if pod_memory_limit != 0:
39+
total_memory = pod_memory_limit
3740

3841
# 格式化内存大小
3942
size_units = ["B", "KB", "MB", "GB", "TB"]

Diff for: cli/extractor/extractor.py

+23
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ def jar_extractor_cmd(extractor_path, source_root, database):
164164
# 获取内存信息
165165
mem = psutil.virtual_memory()
166166
total_memory = mem.total
167+
pod_memory_limit = get_pod_memory_limit()
168+
if pod_memory_limit != 0:
169+
total_memory = pod_memory_limit
167170
total_memory_gb = round(total_memory / (1024 ** 3))
168171
logging.info("current memory is : %s GB", total_memory_gb)
169172
xmx = max(total_memory_gb - 1, 6)
@@ -190,3 +193,23 @@ def extractor_run(language, source_root, database, timeout, options):
190193
logging.error("Not supported language: %s", language)
191194
return -1
192195

196+
197+
def get_pod_memory_limit():
198+
# cgroup 文件系统路径
199+
memory_limit_path = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
200+
memory_limit = 0
201+
try:
202+
with open(memory_limit_path, 'r') as f:
203+
memory_limit = int(f.read().strip())
204+
except FileNotFoundError:
205+
pass
206+
except PermissionError:
207+
logging.error("Permission denied when accessing cgroup files.")
208+
except IOError as e:
209+
logging.error(f"IO error occurred when accessing cgroup files: {e}")
210+
except Exception as e:
211+
logging.error(f"An unexpected error occurred: {e}")
212+
return memory_limit
213+
214+
215+

0 commit comments

Comments
 (0)