[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH 1/2] bloat-o-meter: switch arguments parsing to argparse
From:       Jones Syue 薛懷宗 <jonessyue () qnap ! com>
Date:       2024-03-19 9:28:23
Message-ID: SI2PR04MB5097BD53D4E6DD1E92C20DC5DC2C2 () SI2PR04MB5097 ! apcprd04 ! prod ! outlook ! com
[Download RAW message or body]

This patch switches arguments parsing to argparse module, which makes it
easy to extend further arguments into this script. Note that one thing is
changed: how additional parameters is pass-through to readelf, now it needs
an assignment char '=' followed by a pair of single/double quotes to wrap
additional parameters, like:
./scripts/bloat-o-meter --="-a -n" busybox_unstripped_{old,new}
or
./scripts/bloat-o-meter busybox_unstripped_{old,new} --="-a -n"

It is a bit different in old days, always pass-through in the tail:
./scripts/bloat-o-meter busybox_unstripped_{old,new} -- -a -n

This is a help usage show how it looks like:
./scripts/bloat-o-meter -h
usage: bloat-o-meter [-h] [-t] [-- OPT] file1 file2

Simple script used to compare symbol size and section size of 2 object files,
and report size difference. Object files like unstripped binaries with extra
information could generate a detailed report, which is strongly recommended.

positional arguments:
  file1       Old file to compare, e.g. busybox_unstripped_old
  file2       New file to compare, e.g. busybox_unstripped_new

optional arguments:
  -h, --help  show this help message and exit
  -t          Show time spent on parsing/processing
  -- OPT      Pass additional parameters to readelf, e.g. --="-a"

Signed-off-by: Jones Syue <jonessyue@qnap.com>
---
 scripts/bloat-o-meter | 64 ++++++++++++++++++++-------------------------------
 1 file changed, 25 insertions(+), 39 deletions(-)

diff --git a/scripts/bloat-o-meter b/scripts/bloat-o-meter
index b4a1d28113b0..72b0693764bf 100755
--- a/scripts/bloat-o-meter
+++ b/scripts/bloat-o-meter
@@ -7,41 +7,27 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import sys, os
+import sys, os, argparse
 
-def usage():
-    sys.stderr.write("usage: %s [-t] file1 file2 [-- <readelf options>]\n"
-                        % sys.argv[0])
-    sys.stderr.write("\t-t\tShow time spent on parsing/processing\n")
-    sys.stderr.write("\t--\tPass additional parameters to readelf\n")
-    sys.exit(1)
+usage='''
+Simple script used to compare symbol size and section size of 2 object files,
+and report size difference. Object files like unstripped binaries with extra
+information could generate a detailed report, which is strongly recommended.
+'''
+parser = argparse.ArgumentParser(description=usage)
+parser.add_argument('-t', action='store_true',
+                    help='Show time spent on parsing/processing')
+parser.add_argument('--', dest='opt', default='',
+                    help='Pass additional parameters to readelf, e.g. --="-a"')
+parser.add_argument('file1', help='Old file to compare, '
+                    'e.g. busybox_unstripped_old')
+parser.add_argument('file2', help='New file to compare, '
+                    'e.g. busybox_unstripped_new')
+args = parser.parse_args()
 
-f1, f2 = (None, None)
-flag_timing, dashes = (False, False)
-
-for f in sys.argv[1:]:
-    if f.startswith("-"):
-        if f == "--": # sym_args
-            dashes = True
-            break
-        if f == "-t": # timings
-            flag_timing = True
-    else:
-        if not os.path.exists(f):
-            sys.stderr.write("Error: file '%s' does not exist\n" % f)
-            usage()
-        if f1 is None:
-            f1 = f
-        elif f2 is None:
-            f2 = f
-        else:
-            usage()
-if flag_timing:
+if args.t:
     import time
-if f1 is None or f2 is None:
-    usage()
 
-sym_args = " ".join(sys.argv[3 + flag_timing + dashes:])
 def getsizes(file):
     sym, alias, lut, section = {}, {}, {}, {}
     for l in os.popen("readelf -W -S " + file).readlines():
@@ -52,7 +38,7 @@ def getsizes(file):
         if x[1] not in [".rodata"]: continue
         sym[x[1]] = {"addr" : int(x[3], 16), "size" : int(x[5], 16)}
         section[x[0][1:-1]] = {"name" : x[1]}
-    for l in os.popen("readelf -W -s %s %s" % (sym_args, file)).readlines():
+    for l in os.popen("readelf -W -s %s %s" % (args.opt, file)).readlines():
         l = l.strip()
         if not (len(l) and l[0].isdigit() and len(l.split()) == 8):
             continue
@@ -80,14 +66,14 @@ def getsizes(file):
             sym[alias[(addr, sz)]["name"]] = {"addr" : addr, "size": sz}
     return sym
 
-if flag_timing:
+if args.t:
     start_t1 = int(time.time() * 1e9)
-old = getsizes(f1)
-if flag_timing:
+old = getsizes(args.file1)
+if args.t:
     end_t1 = int(time.time() * 1e9)
     start_t2 = int(time.time() * 1e9)
-new = getsizes(f2)
-if flag_timing:
+new = getsizes(args.file2)
+if args.t:
     end_t2 = int(time.time() * 1e9)
     start_t3 = int(time.time() * 1e9)
 grow, shrink, add, remove, up, down = 0, 0, 0, 0, 0, 0
@@ -121,7 +107,7 @@ for name in common:
 
 delta.sort()
 delta.reverse()
-if flag_timing:
+if args.t:
     end_t3 = int(time.time() * 1e9)
 
 print("%-48s %7s %7s %+7s" % ("function", "old", "new", "delta"))
@@ -134,7 +120,7 @@ print("-"*78)
 total="(add/remove: %s/%s grow/shrink: %s/%s up/down: %s/%s)%%sTotal: %s bytes"\
     % (add, remove, grow, shrink, up, -down, up-down)
 print(total % (" "*(80-len(total))))
-if flag_timing:
+if args.t:
     print("\n%d/%d; %d Parse origin/new; processing nsecs" %
         (end_t1-start_t1, end_t2-start_t2, end_t3-start_t3))
     print("total nsecs: %d" % (end_t3-start_t1))
-- 
2.1.4

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic