
from __future__ import absolute_import
from __future__ import division

import six

from . import expose


@expose
def ksmTune(tuningParams):
    KSM_PARAMS = {'run': 3, 'merge_across_nodes': 3,
                  'sleep_millisecs': 0x100000000,
                  'pages_to_scan': 0x100000000}
    for (k, v) in six.iteritems(tuningParams):
        if k not in six.iterkeys(KSM_PARAMS):
            raise Exception('Invalid key in KSM parameter: %s=%s' % (k, v))
        if int(v) < 0 or int(v) >= KSM_PARAMS[k]:
            raise Exception('Invalid value in KSM parameter: %s=%s' %
                            (k, v))
        with open('/sys/kernel/mm/ksm/%s' % k, 'w') as f:
            f.write(str(v))
