summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2016-10-21 21:22:34 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-11-06 23:35:36 +0000
commit4d2658eeeeca5f4b9af74ff761580befec91d373 (patch)
tree5eed93bea700299ebfcf9a439b3416df58c45662
parente35775c598b7db9f3d93e0004b0f7a0dc9028284 (diff)
downloadpoky-4d2658eeeeca5f4b9af74ff761580befec91d373.tar.gz
task-time: Add simple buildstats analysis script
The 'task-time' Python script is used for simple manual analysis of buildstats. It displays task timing information in the same format (and using the same calculation) as the Bash 'time' builtin, and can optionally sort tasks by real (wall-clock), user (user space CPU), or sys (kernel CPU) time used. The timing information comes from the getrusage(2) fields added by commit adfdca4df18f ("buildstats: Improve to add getrusage data and corrected IO stats"). That commit is required for the script to work. Example 1: Running 'task-time' on a specific task buildstat: $ task-time ./20161005235448/gettext-0.16.1-r6/do_compile ./20161005235448/gettext-0.16.1-r6/do_compile: real 0m54.560s user 0m46.028s sys 0m2.772s Example 2: Running 'task-time' on a directory, sorting on wall-clock time: $ task-time tmp/buildstats/20161018083535 --sort real tmp/buildstats/20161018083535/bash-4.3.30-r0/do_fetch: real 10m59.140s user 0m1.152s sys 0m0.320s tmp/buildstats/20161018083535/readline-native-6.3-r0/do_fetch: real 8m57.310s user 0m0.860s sys 0m0.288s tmp/buildstats/20161018083535/perl-5.22.1-r0/do_compile: real 4m28.840s user 4m1.348s sys 0m15.816s ... Example 3: Running 'task-time' on all do_compile buildstats for a particular build by using shell globbing, sorting on user space CPU time: $ task-time tmp/buildstats/20161018083535/*/do_compile --sort user tmp/buildstats/20161018083535/qemu-native-2.7.0-r1/do_compile: real 0m49.570s user 21m45.236s sys 1m44.380s tmp/buildstats/20161018083535/linux-yocto-4.8+gitAUTOINC+03bf3dd731_67813e7efa-r0/do_compile: real 0m49.530s user 21m39.588s sys 1m59.576s tmp/buildstats/20161018083535/gcc-cross-i586-6.2.0-r0/do_compile: real 1m8.130s user 15m54.256s sys 1m28.776s ... Example 4: Comparing a task between two builds: $ task-time 201610052{25856,35448}/gettext-0*/do_compile --sort real 20161005235448/gettext-0.16.1-r6/do_compile: real 0m54.560s user 0m46.028s sys 0m2.772s 20161005225856/gettext-0.19.8.1-r0/do_compile: real 0m41.520s user 2m17.312s sys 0m7.536s (From OE-Core rev: 76dfad5b598e2937554bddeecf47482b14a854cd) Signed-off-by: Ulf Magnusson <ulfalizer@gmail.com> Signed-off-by: Ross Burton <ross.burton@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rwxr-xr-xscripts/task-time132
1 files changed, 132 insertions, 0 deletions
diff --git a/scripts/task-time b/scripts/task-time
new file mode 100755
index 0000000000..e58040a9b9
--- /dev/null
+++ b/scripts/task-time
@@ -0,0 +1,132 @@
1#!/usr/bin/env python3
2
3import argparse
4import os
5import re
6import sys
7
8arg_parser = argparse.ArgumentParser(
9 description="""
10Reports time consumed for one or more task in a format similar to the standard
11Bash 'time' builtin. Optionally sorts tasks by real (wall-clock), user (user
12space CPU), or sys (kernel CPU) time.
13""")
14
15arg_parser.add_argument(
16 "paths",
17 metavar="path",
18 nargs="+",
19 help="""
20A path containing task buildstats. If the path is a directory, e.g.
21build/tmp/buildstats, then all task found (recursively) in it will be
22processed. If the path is a single task buildstat, e.g.
23build/tmp/buildstats/20161018083535/foo-1.0-r0/do_compile, then just that
24buildstat will be processed. Multiple paths can be specified to process all of
25them. Files whose names do not start with "do_" are ignored.
26""")
27
28arg_parser.add_argument(
29 "--sort",
30 choices=("none", "real", "user", "sys"),
31 default="none",
32 help="""
33The measurement to sort the output by. Defaults to 'none', which means to sort
34by the order paths were given on the command line. For other options, tasks are
35sorted in descending order from the highest value.
36""")
37
38args = arg_parser.parse_args()
39
40# Field names and regexes for parsing out their values from buildstat files
41field_regexes = (("elapsed", ".*Elapsed time: ([0-9.]+)"),
42 ("user", "rusage ru_utime: ([0-9.]+)"),
43 ("sys", "rusage ru_stime: ([0-9.]+)"),
44 ("child user", "Child rusage ru_utime: ([0-9.]+)"),
45 ("child sys", "Child rusage ru_stime: ([0-9.]+)"))
46
47# A list of (<path>, <dict>) tuples, where <path> is the path of a do_* task
48# buildstat file and <dict> maps fields from the file to their values
49task_infos = []
50
51def save_times_for_task(path):
52 """Saves information for the buildstat file 'path' in 'task_infos'."""
53
54 if not os.path.basename(path).startswith("do_"):
55 return
56
57 with open(path) as f:
58 fields = {}
59
60 for line in f:
61 for name, regex in field_regexes:
62 match = re.match(regex, line)
63 if match:
64 fields[name] = float(match.group(1))
65 break
66
67 # Check that all expected fields were present
68 for name, regex in field_regexes:
69 if name not in fields:
70 print("Warning: Skipping '{}' because no field matching '{}' could be found"
71 .format(path, regex),
72 file=sys.stderr)
73 return
74
75 task_infos.append((path, fields))
76
77def save_times_for_dir(path):
78 """Runs save_times_for_task() for each file in path and its subdirs, recursively."""
79
80 # Raise an exception for os.walk() errors instead of ignoring them
81 def walk_onerror(e):
82 raise e
83
84 for root, _, files in os.walk(path, onerror=walk_onerror):
85 for fname in files:
86 save_times_for_task(os.path.join(root, fname))
87
88for path in args.paths:
89 if os.path.isfile(path):
90 save_times_for_task(path)
91 else:
92 save_times_for_dir(path)
93
94def elapsed_time(task_info):
95 return task_info[1]["elapsed"]
96
97def tot_user_time(task_info):
98 return task_info[1]["user"] + task_info[1]["child user"]
99
100def tot_sys_time(task_info):
101 return task_info[1]["sys"] + task_info[1]["child sys"]
102
103if args.sort != "none":
104 sort_fn = {"real": elapsed_time, "user": tot_user_time, "sys": tot_sys_time}
105 task_infos.sort(key=sort_fn[args.sort], reverse=True)
106
107first_entry = True
108
109# Catching BrokenPipeError avoids annoying errors when the output is piped into
110# e.g. 'less' or 'head' and not completely read
111try:
112 for task_info in task_infos:
113 real = elapsed_time(task_info)
114 user = tot_user_time(task_info)
115 sys = tot_sys_time(task_info)
116
117 if not first_entry:
118 print()
119 first_entry = False
120
121 # Mimic Bash's 'time' builtin
122 print("{}:\n"
123 "real\t{}m{:.3f}s\n"
124 "user\t{}m{:.3f}s\n"
125 "sys\t{}m{:.3f}s"
126 .format(task_info[0],
127 int(real//60), real%60,
128 int(user//60), user%60,
129 int(sys//60), sys%60))
130
131except BrokenPipeError:
132 pass