ksm-service.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from comar.service import *
  2. import os
  3. serviceType = "local"
  4. serviceDefault = "conditional"
  5. serviceDesc = _({"en": "Kernel Samepage Merging",
  6. "tr": "Kernel Aynısayfa Birleştirici"})
  7. serviceConf = "ksm"
  8. SYSFS_KSM_RUN = "/sys/kernel/mm/ksm/run"
  9. SYSFS_KSM_MAX_KERNEL_PAGES = "/sys/kernel/mm/ksm/max_kernel_pages"
  10. # unless KSM_MAX_KERNEL_PAGES is set, let ksm munch up to half of total memory.
  11. def default_max_kernel_pages():
  12. max_kernel_pages = config.get("KSM_MAX_KERNEL_PAGES", "")
  13. if max_kernel_pages != "":
  14. return max_kernel_pages
  15. total = 0
  16. with open("/proc/meminfo", "r") as _meminfo:
  17. for line in _meminfo.readlines():
  18. if "MemTotal" in line:
  19. total = int(line.split()[-2])
  20. break
  21. pagesize = int(os.sysconf("SC_PAGESIZE"))
  22. return (total * 1024 / pagesize / 2)
  23. @synchronized
  24. def start():
  25. max_kernel_pages = str(default_max_kernel_pages())
  26. if os.path.exists(SYSFS_KSM_MAX_KERNEL_PAGES):
  27. open(SYSFS_KSM_MAX_KERNEL_PAGES, "w").write(max_kernel_pages)
  28. open(SYSFS_KSM_RUN, "w").write("1")
  29. @synchronized
  30. def stop():
  31. if os.path.exists(SYSFS_KSM_RUN):
  32. open(SYSFS_KSM_RUN, "w").write("0")
  33. def ready():
  34. status = is_on()
  35. if status == "on" or (status == "conditional" and os.path.exists(SYSFS_KSM_RUN)):
  36. start()
  37. def status():
  38. ret = False
  39. try:
  40. ret = (open(SYSFS_KSM_RUN, "r").read().strip() == "1")
  41. except IOError:
  42. pass
  43. return ret