From 3bfc7f1b2052a794ffceb6a33f5b21f613a479dc Mon Sep 17 00:00:00 2001 From: Kyle Blatter Date: Wed, 5 Nov 2014 10:08:45 -0800 Subject: [PATCH] Refactor arc_summary to simplify -p processing The -p option is used to specify a specific page of output to be displayed. If omitted, all output pages would be displayed. arc_summary, as it stood, had really kludgy processing code for processing the -p option. It relied on a try-except block which was treated as an if statement and in normal operation would fail any time a user didn't specify the -p option on the command line. When the exception was thrown, the script would then display all output pages. This happened whether the -p option was omitted or malformed. Thus, in the principle use case, an exception would be raised in order to run the script as desired. The same except code would be called regardless of the exception, however, and malformed -p arguments would also cause the script to execute. Additionally, this required the function which handles the case where all output pages were to be displayed, _call_all, to be potentially called from several locations within main. This commit refactors the option processing code to simplify it and make it easier to catch runtime errors in the script. This is done by specializing the try-except block to only have an exception when the -p argument is malformed. When the -p option is correctly selected by the user, it calls a function in the unSub array directly, which will only display one page of output. Finally in the context of this refactoring the page breaks have been removed. Pages seem to have been added into the output in the FreeNAS version of the script. This patch removes pages from the output to more closely resemble the freebsd version of the script. Signed-off-by: Brian Behlendorf Signed-off-by: Kyle Blatter Signed-off-by: Ned Bass --- cmd/arc_summary/arc_summary.py | 42 +++++++++++++++------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/cmd/arc_summary/arc_summary.py b/cmd/arc_summary/arc_summary.py index 25db1a941..1456e58f3 100755 --- a/cmd/arc_summary/arc_summary.py +++ b/cmd/arc_summary/arc_summary.py @@ -97,7 +97,6 @@ def div1(): def div2(): - div1() sys.stdout.write("\n") @@ -1049,7 +1048,6 @@ def _sysctl_summary(Kstat): sys.stdout.write("\t\# %s\n" % sysctl_descriptions[name]) sys.stdout.write(format % (name, value)) - unSub = [ _arc_summary, _arc_efficiency, @@ -1058,17 +1056,6 @@ unSub = [ _vdev_summary, ] - -def _call_all(): - page = 1 - Kstat = get_Kstat() - for unsub in unSub: - unsub(Kstat) - sys.stdout.write("\t\t\t\t\t\t\t\tPage: %2d" % page) - div2() - page += 1 - - def zfs_header(): daydate = time.strftime("%a %b %d %H:%M:%S %Y") @@ -1094,20 +1081,27 @@ def main(): if opt == '-p': args['p'] = arg - if args: - alternate_sysctl_layout = True if 'a' in args else False - show_sysctl_descriptions = True if 'd' in args else False + Kstat = get_Kstat() + + alternate_sysctl_layout = 'a' in args + show_sysctl_descriptions = 'd' in args + + pages = [] + + if 'p' in args: try: - zfs_header() - unSub[int(args['p']) - 1]() - div2() - - except: - _call_all() - + pages.append(unSub[int(args['p']) - 1]) + except IndexError , e: + sys.stderr.write('the argument to -p must be between 1 and ' + + str(len(unSub)) + '\n') + sys.exit() else: - _call_all() + pages = unSub + zfs_header() + for page in pages: + page(Kstat) + div2() if __name__ == '__main__': main()