diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-27 17:40:43 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-02-27 17:40:43 +0100 |
commit | 84f0d95bf6672d2426caeb1ac66d6dae119a2f5f (patch) | |
tree | 8ed3d5b9446cb0ea6ea24956dd7edc765e71e835 | |
parent | bcff9ae55cb324ffe499347e1925c29f1d6800c9 (diff) | |
download | psutil-84f0d95bf6672d2426caeb1ac66d6dae119a2f5f.tar.gz |
better print formatting for print scripts
-rw-r--r-- | scripts/internal/print_access_denied.py | 89 | ||||
-rw-r--r-- | scripts/internal/print_api_speed.py | 44 |
2 files changed, 75 insertions, 58 deletions
diff --git a/scripts/internal/print_access_denied.py b/scripts/internal/print_access_denied.py index d903f8b9..a098a72b 100644 --- a/scripts/internal/print_access_denied.py +++ b/scripts/internal/print_access_denied.py @@ -10,38 +10,39 @@ It prints how many AccessDenied exceptions are raised in total and for what Process method. $ make print-access-denied -username 0 0.0% SUCCESS -cpu_num 0 0.0% SUCCESS -num_ctx_switches 0 0.0% SUCCESS -pid 0 0.0% SUCCESS -cmdline 0 0.0% SUCCESS -create_time 0 0.0% SUCCESS -ionice 0 0.0% SUCCESS -cpu_percent 0 0.0% SUCCESS -terminal 0 0.0% SUCCESS -ppid 0 0.0% SUCCESS -nice 0 0.0% SUCCESS -status 0 0.0% SUCCESS -cpu_times 0 0.0% SUCCESS -memory_info 0 0.0% SUCCESS -threads 0 0.0% SUCCESS -uids 0 0.0% SUCCESS -num_threads 0 0.0% SUCCESS -name 0 0.0% SUCCESS -gids 0 0.0% SUCCESS -cpu_affinity 0 0.0% SUCCESS -memory_percent 0 0.0% SUCCESS -memory_full_info 70 20.8% ACCESS DENIED -memory_maps 70 20.8% ACCESS DENIED -exe 174 51.8% ACCESS DENIED -connections 237 70.5% ACCESS DENIED -num_fds 237 70.5% ACCESS DENIED -cwd 237 70.5% ACCESS DENIED -io_counters 237 70.5% ACCESS DENIED -open_files 237 70.5% ACCESS DENIED -environ 237 70.5% ACCESS DENIED ------------------------------------------------ -total: 1736 (336 total processes) +API AD Percent Outcome +memory_info 0 0.0% SUCCESS +uids 0 0.0% SUCCESS +cmdline 0 0.0% SUCCESS +create_time 0 0.0% SUCCESS +status 0 0.0% SUCCESS +num_ctx_switches 0 0.0% SUCCESS +username 0 0.0% SUCCESS +ionice 0 0.0% SUCCESS +memory_percent 0 0.0% SUCCESS +gids 0 0.0% SUCCESS +cpu_times 0 0.0% SUCCESS +nice 0 0.0% SUCCESS +pid 0 0.0% SUCCESS +cpu_percent 0 0.0% SUCCESS +num_threads 0 0.0% SUCCESS +cpu_num 0 0.0% SUCCESS +ppid 0 0.0% SUCCESS +terminal 0 0.0% SUCCESS +name 0 0.0% SUCCESS +threads 0 0.0% SUCCESS +cpu_affinity 0 0.0% SUCCESS +memory_maps 71 21.3% ACCESS DENIED +memory_full_info 71 21.3% ACCESS DENIED +exe 174 52.1% ACCESS DENIED +environ 238 71.3% ACCESS DENIED +num_fds 238 71.3% ACCESS DENIED +io_counters 238 71.3% ACCESS DENIED +cwd 238 71.3% ACCESS DENIED +connections 238 71.3% ACCESS DENIED +open_files 238 71.3% ACCESS DENIED +-------------------------------------------------- +Totals: access-denied=1744, calls=10020, processes=334 """ from __future__ import print_function, division @@ -52,31 +53,37 @@ from scriptutils import hilite def main(): + # collect tot_procs = 0 tot_ads = 0 + tot_calls = 0 signaler = object() d = defaultdict(int) for p in psutil.process_iter(attrs=[], ad_value=signaler): tot_procs += 1 for methname, value in p.info.items(): + tot_calls += 1 if value is signaler: tot_ads += 1 d[methname] += 1 else: d[methname] += 0 + # print + templ = "%-20s %-5s %-9s %s" + s = templ % ("API", "AD", "Percent", "Outcome") + print(hilite(s, ok=None, bold=True)) for methname, ads in sorted(d.items(), key=lambda x: x[1]): perc = (ads / tot_procs) * 100 - s = "%-20s %-3s %5.1f%% " % (methname, ads, perc) - if not ads: - s += "SUCCESS" - s = hilite(s, ok=True) - else: - s += "ACCESS DENIED" - s = hilite(s, ok=False) + outcome = "SUCCESS" if not ads else "ACCESS DENIED" + s = templ % (methname, ads, "%6.1f%%" % perc, outcome) + s = hilite(s, ok=not ads) print(s) - print("-----------------------------------------------") - print("total: %19s (%s total processes)" % (tot_ads, tot_procs)) + print("-" * 50) + # print("total: %s AccessDenied errors (%s total processes)" % ( + # tot_ads, tot_procs)) + print("Totals: access-denied=%s, calls=%s, processes=%s" % ( + tot_ads, tot_calls, tot_procs)) if __name__ == '__main__': diff --git a/scripts/internal/print_api_speed.py b/scripts/internal/print_api_speed.py index 188ae737..12040dcf 100644 --- a/scripts/internal/print_api_speed.py +++ b/scripts/internal/print_api_speed.py @@ -7,20 +7,26 @@ """Benchmark all API calls. $ python scripts/internal/print_api_speed.py -SYSTEM APIS - boot_time 0.000127 secs - cpu_count 0.000034 secs - cpu_count (cores) 0.000279 secs - cpu_freq 0.000438 secs - ... - -PROCESS APIS - cmdline 0.000027 secs - connections 0.000056 secs - cpu_affinity 0.000014 secs - cpu_num 0.000054 secs - cpu_percent 0.000077 secs - ... +SYSTEM APIS SECONDS +---------------------------------- +boot_time 0.000140 +cpu_count 0.000016 +cpu_count (cores) 0.000312 +cpu_freq 0.000811 +cpu_percent 0.000138 +cpu_stats 0.000165 +cpu_times 0.000140 +... + +PROCESS APIS SECONDS +---------------------------------- +children 0.007246 +cmdline 0.000069 +connections 0.000072 +cpu_affinity 0.000012 +cpu_num 0.000035 +cpu_percent 0.000042 +cpu_times 0.000031 """ from __future__ import print_function, division @@ -36,6 +42,7 @@ from scriptutils import hilite SORT_BY_TIME = False if psutil.POSIX else True TOP_SLOWEST = 7 timings = [] +templ = "%-25s %s" def print_timings(): @@ -45,7 +52,7 @@ def print_timings(): slower.append(x[0]) while timings[:]: title, elapsed = timings.pop(0) - s = " %-30s %f secs" % (title, elapsed) + s = templ % (title, "%f" % elapsed) if title in slower: s = hilite(s, ok=False) print(s) @@ -76,7 +83,8 @@ def run(): if name not in ignore: public_apis.append(name) - print(titlestr("SYSTEM APIS")) + print(titlestr(templ % ("SYSTEM APIS", "SECONDS"))) + print("-" * 34) for name in public_apis: fun = getattr(psutil, name) args = () @@ -90,7 +98,9 @@ def run(): print_timings() # --- process - print(titlestr("\nPROCESS APIS")) + print("") + print(titlestr(templ % ("PROCESS APIS", "SECONDS"))) + print("-" * 34) ignore = ['send_signal', 'suspend', 'resume', 'terminate', 'kill', 'wait', 'as_dict', 'parent', 'parents', 'memory_info_ex', 'oneshot', 'pid', 'rlimit'] |