提交 f35ddaa7 编写于 作者: S Santhosh G

Added methods in memory library that gets and sets a transparent hugepage values.

GenIOException Base Exception class has been created for all IO related exceptions.
Also adds write_file_or_fail method in genio library which writes
to a file and raises exception when write fails.
Signed-off-by: NSanthosh G <santhog4@linux.vnet.ibm.com>
上级 7d682b9d
......@@ -29,6 +29,13 @@ _open_log_files = {}
_log_file_dir = os.environ.get('TMPDIR', '/tmp')
class GenIOError(Exception):
"""
Base Exception Class for all IO exceptions
"""
pass
def log_line(filename, line):
"""
Write a line to a file.
......@@ -174,3 +181,21 @@ def write_one_line(filename, line):
:type line: str
"""
write_file(filename, line.rstrip('\n') + '\n')
def write_file_or_fail(filename, data):
"""
Write to a file and raise exception on write failure
:param filename: Path to file
:type filename: str
:param data: Data to be written to file
:type data: str
:raises GenIOError: On write Failure
"""
fd = os.open(filename, os.O_WRONLY)
try:
ret = os.write(fd, data)
except OSError as details:
raise GenIOError("The write to %s failed: %s" % (
filename, details))
......@@ -17,12 +17,14 @@
# Authors: Yiqiao Pu <ypu@redhat.com>
import os
import re
import glob
import math
import logging
from . import process
from . import genio
# Returns total memory in kb
......@@ -278,3 +280,33 @@ def get_buddy_info(chunk_sizes, nodes="all", zones="all"):
buddyinfo_dict[chunk_size] += int(chunk_info)
return buddyinfo_dict
def set_thp_value(feature, value):
"""
Sets THP feature to a given value
:param feature: Thp feature to set
:type feature: str
:param value: Value to be set to feature
:type value: str
"""
thp_path = '/sys/kernel/mm/transparent_hugepage/'
thp_feature_to_set = os.path.join(thp_path, feature)
genio.write_file_or_fail(thp_feature_to_set, value)
def get_thp_value(feature):
"""
Gets the value of the thp feature arg passed
:Param feature: Thp feature to get value
:type feature: str
"""
thp_path = '/sys/kernel/mm/transparent_hugepage/'
thp_feature_to_get = os.path.join(thp_path, feature)
value = genio.read_file(thp_feature_to_get)
if feature in ("enabled", "defrag", "shmem_enabled"):
return (re.search(r"\[(\w+)\]", value)).group(1)
else:
return value
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册