55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
import ctypes
|
|
import os
|
|
|
|
# Correct constants for Linux Kernel 6.4+
|
|
PR_SET_MEMORY_MERGE = 67
|
|
PR_GET_MEMORY_MERGE = 68
|
|
|
|
class EnableProcessKSMNode:
|
|
def __init__(self):
|
|
pass
|
|
|
|
@classmethod
|
|
def INPUT_TYPES(cls):
|
|
return {
|
|
}
|
|
|
|
RETURN_TYPES = ("STRING",)
|
|
RETURN_NAMES = ("ksm_stats",)
|
|
FUNCTION = "apply_ksm"
|
|
CATEGORY = "utils/memory"
|
|
|
|
def apply_ksm(self):
|
|
libc = ctypes.CDLL(None)
|
|
|
|
status = libc.prctl(PR_GET_MEMORY_MERGE, 0, 0, 0, 0)
|
|
status_msg = "ACTIVE" if status == 1 else f"INACTIVE ({status})"
|
|
|
|
pid = os.getpid()
|
|
stat_path = f"/proc/{pid}/ksm_stat"
|
|
stats_output = f"PID: {pid}\nKSM Status: {status_msg}\n\n--- CURRENT STATS ---\n"
|
|
|
|
if os.path.exists(stat_path):
|
|
try:
|
|
with open(stat_path, 'r') as f:
|
|
stats_output += f.read()
|
|
except Exception as e:
|
|
stats_output += f"Could not read ksm_stat: {str(e)}\n"
|
|
else:
|
|
stats_output += "Warning: ksm_stat file not found.\n"
|
|
|
|
# System Optimization Help Section
|
|
stats_output += (
|
|
"\n--- SYSTEM OPTIMIZATION HELP ---\n"
|
|
"If pages aren't merging quickly enough, run these\n"
|
|
"commands in your Linux terminal to optimize KSM:\n\n"
|
|
"1. Ensure KSM daemon is active:\n"
|
|
" echo 1 | sudo tee /sys/kernel/mm/ksm/run\n\n"
|
|
"2. Speed up scanning interval (default is 20ms):\n"
|
|
" echo 10 | sudo tee /sys/kernel/mm/ksm/sleep_millisecs\n\n"
|
|
"3. Increase pages scanned per pass (default is 100):\n"
|
|
" echo 5000 | sudo tee /sys/kernel/mm/ksm/pages_to_scan\n"
|
|
)
|
|
|
|
return (stats_output,)
|