Skip to content

[feat]jar extractor 获取内存信息支持pod #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/database/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions cli/extractor/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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