Skip to content

Commit 074d8e4

Browse files
committed
perf report task_analyzer: fix pylint warnings
pylint points out 156 warnings (errors+warnings+refactor+checks messages), 48 of them have been fixed in this patch. Many warnings by pylint, given for this file, are false positives, such as "unused-argument" in case of 'trace_unhandled', and 'sched__sched_switch' functions, the arguments are required by design, hence pylint check has been disabled for those lines. Other warnings such as 'consider-using-f-string', 'consider-using-enumerate' have not been fixed, considering the code complexity in mind Most changes don't affect the behaviour in any way, other than 'inconsistent-return-statements' [1] fixes in following functions, where it used to return None, and now returns False: 1. '_limit_filtered' 2. '_is_within_timelimit' On reading the code, default return should be logically False, so both issues have been fixed by returning False at the end. [1]: https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/inconsistent-return-statements.html) Signed-off-by: Aditya Gupta <adityag@linux.ibm.com>
1 parent 06c2afb commit 074d8e4

1 file changed

Lines changed: 59 additions & 62 deletions

File tree

tools/perf/scripts/python/task-analyzer.py

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import argparse
1717
import decimal
1818

19+
from perf_trace_context import *
20+
from Core import *
1921

2022
sys.path.append(
2123
os.environ["PERF_EXEC_PATH"] + "/scripts/python/Perf-Trace-Util/lib/Perf/Trace"
2224
)
23-
from perf_trace_context import *
24-
from Core import *
2525

2626
# Definition of possible ASCII color codes
2727
_COLORS = {
@@ -72,7 +72,7 @@ def iteritems(d):
7272

7373
def _check_color():
7474
global _COLORS
75-
"""user enforced no-color or if stdout is no tty we disable colors"""
75+
# user enforced no-color or if stdout is no tty we disable colors
7676
if sys.stdout.isatty() and args.stdio_color != "never":
7777
return
7878
_COLORS = {
@@ -92,8 +92,7 @@ def _parse_args():
9292
parser.add_argument(
9393
"--time-limit",
9494
default=[],
95-
help=
96-
"print tasks only in time[s] window e.g"
95+
help="print tasks only in time[s] window e.g"
9796
" --time-limit 123.111:789.222(print all between 123.111 and 789.222)"
9897
" --time-limit 123: (print all from 123)"
9998
" --time-limit :456 (print all until incl. 456)",
@@ -146,8 +145,8 @@ def _parse_args():
146145
"--rename-comms-by-tids",
147146
default="",
148147
help="rename task names by using tid (<tid>:<newname>,<tid>:<newname>)"
149-
" This option is handy for inexpressive processnames like python interpreted"
150-
" process. E.g --rename 1337:my-python-app",
148+
" This option is handy for inexpressive processnames like python"
149+
" interpreted process. E.g --rename 1337:my-python-app",
151150
)
152151
parser.add_argument(
153152
"--stdio-color",
@@ -169,7 +168,7 @@ def _parse_args():
169168
" --summary-extended are used.",
170169
)
171170
args = parser.parse_args()
172-
args.tid_renames = dict()
171+
args.tid_renames = {}
173172

174173
_argument_filter_sanity_check()
175174
_argument_prepare_check()
@@ -187,14 +186,14 @@ def time_uniter(unit):
187186

188187
def _init_db():
189188
global db
190-
db = dict()
191-
db["running"] = dict()
192-
db["cpu"] = dict()
193-
db["tid"] = dict()
189+
db = {}
190+
db["running"] = {}
191+
db["cpu"] = {}
192+
db["tid"] = {}
194193
db["global"] = []
195194
if args.summary or args.summary_extended or args.summary_only:
196-
db["task_info"] = dict()
197-
db["runtime_info"] = dict()
195+
db["task_info"] = {}
196+
db["runtime_info"] = {}
198197
# min values for summary depending on the header
199198
db["task_info"]["pid"] = len("PID")
200199
db["task_info"]["tid"] = len("TID")
@@ -207,7 +206,7 @@ def _init_db():
207206
db["runtime_info"]["mean"] = len("Mean")
208207
db["runtime_info"]["median"] = len("Median")
209208
if args.summary_extended:
210-
db["inter_times"] = dict()
209+
db["inter_times"] = {}
211210
db["inter_times"]["out_in"] = len("Out-In")
212211
db["inter_times"]["inter_at"] = len("At")
213212
db["inter_times"]["out_out"] = len("Out-Out")
@@ -221,14 +220,14 @@ def _median(numbers):
221220
index = n // 2
222221
if n % 2:
223222
return sorted(numbers)[index]
224-
return sum(sorted(numbers)[index - 1 : index + 1]) / 2
223+
return sum(sorted(numbers)[index - 1: index + 1]) / 2
225224

226225

227226
def _mean(numbers):
228227
return sum(numbers) / len(numbers)
229228

230229

231-
class Timespans(object):
230+
class Timespans:
232231
"""
233232
The elapsed time between two occurrences of the same task is being tracked with the
234233
help of this class. There are 4 of those Timespans Out-Out, In-Out, Out-In and
@@ -285,9 +284,7 @@ def _update_max_entries(self):
285284
self.max_out_in = self.out_in
286285
self.max_at = self._time_in
287286

288-
289-
290-
class Summary(object):
287+
class Summary:
291288
"""
292289
Primary instance for calculating the summary output. Processes the whole trace to
293290
find and memorize relevant data such as mean, max et cetera. This instance handles
@@ -302,16 +299,16 @@ class AlignmentHelper:
302299
Used to calculated the alignment for the output of the summary.
303300
"""
304301
def __init__(self, pid, tid, comm, runs, acc, mean,
305-
median, min, max, max_at):
302+
median, min_, max_, max_at):
306303
self.pid = pid
307304
self.tid = tid
308305
self.comm = comm
309306
self.runs = runs
310307
self.acc = acc
311308
self.mean = mean
312309
self.median = median
313-
self.min = min
314-
self.max = max
310+
self.min = min_
311+
self.max = max_
315312
self.max_at = max_at
316313
if args.summary_extended:
317314
self.out_in = None
@@ -338,7 +335,7 @@ def _print_header(self):
338335
sum(db["inter_times"].values()) - 4 * decimal_precision
339336
)
340337
_header += ("Max Inter Task Times",)
341-
fd_sum.write(fmt.format(*_header) + "\n")
338+
fd_sum.write(fmt.format(*_header) + "\n")
342339

343340
def _column_titles(self):
344341
"""
@@ -399,7 +396,6 @@ def _column_titles(self):
399396

400397
fd_sum.write(fmt.format(*column_titles) + "\n")
401398

402-
403399
def _task_stats(self):
404400
"""calculates the stats of every task and constructs the printable summary"""
405401
for tid in sorted(db["tid"]):
@@ -498,7 +494,6 @@ def _format_stats(self):
498494
fmt += "{}{{:{}.{}f}}".format(separator, len_in_out, time_precision)
499495
return fmt
500496

501-
502497
def _calc_alignments_summary(self, align_helper):
503498
# Length is being cut in 3 groups so that further addition is easier to handle.
504499
# The length of every argument from the alignment helper is being checked if it
@@ -514,7 +509,6 @@ def _calc_alignments_summary(self, align_helper):
514509
if len(str(getattr(align_helper, key))) > db["inter_times"][key]:
515510
db["inter_times"][key] = len(str(getattr(align_helper, key)))
516511

517-
518512
def print(self):
519513
self._task_stats()
520514
fmt = self._format_stats()
@@ -526,13 +520,11 @@ def print(self):
526520
for i in range(len(self._body)):
527521
fd_sum.write(fmt.format(*tuple(self._body[i])) + "\n")
528522

529-
530-
531-
class Task(object):
523+
class Task:
532524
""" The class is used to handle the information of a given task."""
533525

534-
def __init__(self, id, tid, cpu, comm):
535-
self.id = id
526+
def __init__(self, id_, tid, cpu, comm):
527+
self.id = id_
536528
self.tid = tid
537529
self.cpu = cpu
538530
self.comm = comm
@@ -632,8 +624,6 @@ def _print_header():
632624
header += ("Time Out-Out", "Time In-In", "Time In-Out")
633625
fd_task.write(fmt.format(*header) + "\n")
634626

635-
636-
637627
def _print_task_finish(task):
638628
"""calculating every entry of a row and printing it immediately"""
639629
c_row_set = ""
@@ -672,7 +662,6 @@ def _print_task_finish(task):
672662
in_in = timespan_gap_tid.in_in
673663
in_out = timespan_gap_tid.in_out
674664

675-
676665
if args.extended_times:
677666
line_out = fmt.format(c_row_set, task.time_in(), task.time_out(), task.cpu,
678667
task.pid, c_tid_set, task.tid, c_tid_reset, c_row_set, task.comm,
@@ -684,7 +673,7 @@ def _print_task_finish(task):
684673
task.runtime(time_unit), out_in, c_row_reset) + "\n"
685674
try:
686675
fd_task.write(line_out)
687-
except(IOError):
676+
except IOError:
688677
# don't mangle the output if user SIGINT this script
689678
sys.exit()
690679

@@ -694,7 +683,7 @@ def _record_cleanup(_list):
694683
is not enabled
695684
"""
696685
if not args.summary and len(_list) > 1:
697-
_list = _list[len(_list) - 1 :]
686+
_list = _list[len(_list) - 1:]
698687

699688

700689
def _record_by_tid(task):
@@ -742,7 +731,7 @@ def _handle_task_finish(tid, cpu, time, perf_sample_dict):
742731

743732
# print only tasks which are not being filtered and no print of trace
744733
# for summary only, but record every task.
745-
if not _limit_filtered(tid, pid, task.comm) and not args.summary_only:
734+
if not _limit_filtered(tid, task.comm) and not args.summary_only:
746735
_print_task_finish(task)
747736
_record_by_tid(task)
748737
_record_by_cpu(task)
@@ -774,18 +763,16 @@ def _time_to_internal(time_ns):
774763
return decimal.Decimal(time_ns) / decimal.Decimal(1e9)
775764

776765

777-
def _limit_filtered(tid, pid, comm):
766+
def _limit_filtered(tid, comm):
778767
if args.filter_tasks:
779-
if str(tid) in args.filter_tasks or comm in args.filter_tasks:
780-
return True
781-
else:
782-
return False
768+
return str(tid) in args.filter_tasks or comm in args.filter_tasks
783769
if args.limit_to_tasks:
784-
if str(tid) in args.limit_to_tasks or comm in args.limit_to_tasks:
785-
return False
786-
else:
787-
return True
770+
return not (
771+
str(tid) in args.limit_to_tasks or comm in args.limit_to_tasks
772+
)
788773

774+
# by default, not filtering out the tid/comm
775+
return False
789776

790777
def _argument_filter_sanity_check():
791778
if args.limit_to_tasks and args.filter_tasks:
@@ -822,7 +809,7 @@ def _argument_prepare_check():
822809
if len(tid_name) != 2:
823810
continue
824811
args.tid_renames[int(tid_name[0])] = tid_name[1]
825-
args.highlight_tasks_map = dict()
812+
args.highlight_tasks_map = {}
826813
for highlight_tasks_tuple in args.highlight_tasks.split(","):
827814
tasks_color_map = highlight_tasks_tuple.split(":")
828815
# default highlight color to red if no color set by user
@@ -842,18 +829,19 @@ def _argument_prepare_check():
842829
elif args.ms:
843830
time_unit = "ms"
844831

845-
846832
fd_task = sys.stdout
847833
if args.csv:
848834
args.stdio_color = "never"
849-
fd_task = open(args.csv, "w")
850-
print("generating csv at",args.csv,)
835+
# pylint: disable=consider-using-with
836+
fd_task = open(args.csv, "w", encoding="utf-8")
837+
print("generating csv at", args.csv)
851838

852839
fd_sum = sys.stdout
853840
if args.csv_summary:
854841
args.stdio_color = "never"
855-
fd_sum = open(args.csv_summary, "w")
856-
print("generating csv summary at",args.csv_summary)
842+
# pylint: disable=consider-using-with
843+
fd_sum = open(args.csv_summary, "w", encoding="utf-8")
844+
print("generating csv summary at", args.csv_summary)
857845
if not args.csv:
858846
args.summary_only = True
859847

@@ -867,31 +855,38 @@ def _is_within_timelimit(time):
867855
return True
868856
lower_time_limit = args.time_limit[0]
869857
upper_time_limit = args.time_limit[1]
858+
if lower_time_limit:
859+
lower_time_limit = decimal.Decimal(lower_time_limit)
860+
if upper_time_limit:
861+
upper_time_limit = decimal.Decimal(upper_time_limit)
862+
870863
# check for upper limit
871864
if upper_time_limit == "":
872-
if time >= decimal.Decimal(lower_time_limit):
865+
if time >= lower_time_limit:
873866
return True
874867
# check for lower limit
875868
if lower_time_limit == "":
876-
if time <= decimal.Decimal(upper_time_limit):
869+
if time <= upper_time_limit:
877870
return True
878871
# quit if time exceeds upper limit. Good for big datasets
879872
else:
880-
quit()
873+
sys.exit()
881874
if lower_time_limit != "" and upper_time_limit != "":
882-
if (time >= decimal.Decimal(lower_time_limit) and
883-
time <= decimal.Decimal(upper_time_limit)):
875+
if lower_time_limit <= time <= upper_time_limit:
884876
return True
885877
# quit if time exceeds upper limit. Good for big datasets
886-
elif time > decimal.Decimal(upper_time_limit):
887-
quit()
878+
elif time > upper_time_limit:
879+
sys.exit()
880+
881+
# time < lower_time_limit
882+
return False
888883

889884
def _prepare_fmt_precision():
890885
decimal_precision = 6
891886
time_precision = 3
892887
if args.ns:
893-
decimal_precision = 9
894-
time_precision = 0
888+
decimal_precision = 9
889+
time_precision = 0
895890
return decimal_precision, time_precision
896891

897892
def _prepare_fmt_sep():
@@ -902,6 +897,7 @@ def _prepare_fmt_sep():
902897
fix_csv_align = 0
903898
return separator, fix_csv_align
904899

900+
# pylint: disable=unused-argument
905901
def trace_unhandled(event_name, context, event_fields_dict, perf_sample_dict):
906902
pass
907903

@@ -917,6 +913,7 @@ def trace_end():
917913
if args.summary or args.summary_extended or args.summary_only:
918914
Summary().print()
919915

916+
# pylint: disable=unused-argument
920917
def sched__sched_switch(event_name, context, common_cpu, common_secs, common_nsecs,
921918
common_pid, common_comm, common_callchain, prev_comm,
922919
prev_pid, prev_prio, prev_state, next_comm, next_pid,

0 commit comments

Comments
 (0)