On 02/15/2015 03:01 PM, Jincheng Miao wrote:
For system default pagesize, it's hard to calculate,
and it changes all the time, so just skip it. For others,
reading from sysfs to get free pages.
Signed-off-by: Jincheng Miao <jmiao(a)redhat.com>
---
repos/virconn/free_pages.py | 97 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 97 insertions(+), 0 deletions(-)
create mode 100644 repos/virconn/free_pages.py
diff --git a/repos/virconn/free_pages.py b/repos/virconn/free_pages.py
new file mode 100644
index 0000000..7172dfe
--- /dev/null
+++ b/repos/virconn/free_pages.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+# test libvirt free pages
+
+import os
+import resource
+
+import libvirt
+from libvirt import libvirtError
+
+from src import sharedmod
+
+required_params = ('cellid', 'pagesize',)
+optional_params = {}
+
+HUGEPAGE_PATH =
'/sys/devices/system/node/node%s/hugepages/hugepages-%skB/free_hugepages'
+
+def parse_unit(pagesz):
+ """ parse a integer value, its unit is KiB
+ """
+ val = int(pagesz[0:len(pagesz)-1])
+ unit = pagesz[-1]
+ if unit == 'K':
+ unit = 1
+ elif unit == 'M':
+ unit = 1024
+ elif unit == 'G':
+ unit = 1024*1024
+ else:
+ return None
+
+ return val * unit
+
+def parse_page_list(pagesize):
+ """ parse page size
+ """
+ if pagesize == None:
+ return None
+
+ l = list()
+ for ps in pagesize.split(','):
+ ps.strip().upper()
string variable is immutable type, should assign it to
a new.
ps = ps.strip().upper() or parse_unit(ps.strip().upper())
jiahu
+ val = parse_unit(ps)
+ if val == None:
+ return None
+ l.append(val)
+ return l
+
+def check_free_pages(page_list, cell_id, free_page, logger):
+ """ check page size
+ """
+ for ps in page_list:
+ # if pagesize is equal to system pagesize, since it is hard to
+ # calculate, so we just pass it
+ if resource.getpagesize()/1024 == ps:
+ logger.info("skip to check default %sKB-page" % ps)
+ continue
+
+ sysfs_path = HUGEPAGE_PATH % (cell_id, ps)
+ if not os.access(sysfs_path, os.R_OK):
+ logger.error("could not find %s" % sysfs_path)
+ return False
+ f= open(sysfs_path)
+ fp = int(f.read())
+ f.close()
+ if not fp == free_page[0][ps]:
+ logger.error("Free %sKB page checking failed" % ps)
+ return False
+ logger.info("Free %sKB page: %s" % (ps, fp))
+
+ return True
+
+def free_pages(params):
+ """ test libvirt free pages
+ """
+ logger = params['logger']
+ cell_id = int(params['cellid'])
+
+ conn = sharedmod.libvirtobj['conn']
+
+ page_list = parse_page_list(params['pagesize'])
+ if page_list == None:
+ logger.error("pagesize could not be recognized")
+ return 1
+
+ try:
+ free_page = conn.getFreePages(page_list, cell_id, 1)
+
+ if check_free_pages(page_list, cell_id, free_page, logger):
+ logger.info("Success to check free page")
+ else:
+ logger.error("Failed to check free page")
+ return 1
+ except libvirtError, e:
+ logger.error("API error message: %s, error code is %s" %
+ e.message)
+ return 1
+ return 0
\ No newline at end of file