Skip to content

Instantly share code, notes, and snippets.

@hed0rah
Created April 8, 2026 00:17
Show Gist options
  • Select an option

  • Save hed0rah/629079ca544a52bbdccd559d0b5ff871 to your computer and use it in GitHub Desktop.

Select an option

Save hed0rah/629079ca544a52bbdccd559d0b5ff871 to your computer and use it in GitHub Desktop.
KSM stats from /sys/kernel/mm/ksm/pages_shared
#!/usr/bin/env python3
# i found a rinky dink python2 version of this in my old files. bringing it to 2026 for the hell of it.
import os
ksm_path = '/sys/kernel/mm/ksm/'
def get_val(name):
try:
with open(os.path.join(ksm_path, name), 'r') as f:
return f.read().strip()
except FileNotFoundError:
return "N/A"
# Stats
stats = ['pages_shared', 'pages_sharing', 'pages_unshared', 'pages_volatile']
# Tunables
tunables = ['run', 'pages_to_scan', 'sleep_millisecs', 'merge_across_nodes']
page_size = os.sysconf('SC_PAGE_SIZE')
savings = (int(get_val('pages_sharing')) - int(get_val('pages_shared'))) * page_size / 1024 / 1024
print(f"\n--- KSM Statistics ---")
print(f"Page Size: {page_size} bytes")
for s in stats:
print(f"{s:15}: {get_val(s)}")
print(f"\n--- KSM Tunables ---")
for t in tunables:
print(f"{t:15}: {get_val(t)}")
print(f"\nEstimated Savings: {savings:.2f} MB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment