task-time 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright OpenEmbedded Contributors
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import argparse
  8. import os
  9. import re
  10. import sys
  11. arg_parser = argparse.ArgumentParser(
  12. description="""
  13. Reports time consumed for one or more task in a format similar to the standard
  14. Bash 'time' builtin. Optionally sorts tasks by real (wall-clock), user (user
  15. space CPU), or sys (kernel CPU) time.
  16. """)
  17. arg_parser.add_argument(
  18. "paths",
  19. metavar="path",
  20. nargs="+",
  21. help="""
  22. A path containing task buildstats. If the path is a directory, e.g.
  23. build/tmp/buildstats, then all task found (recursively) in it will be
  24. processed. If the path is a single task buildstat, e.g.
  25. build/tmp/buildstats/20161018083535/foo-1.0-r0/do_compile, then just that
  26. buildstat will be processed. Multiple paths can be specified to process all of
  27. them. Files whose names do not start with "do_" are ignored.
  28. """)
  29. arg_parser.add_argument(
  30. "--sort",
  31. choices=("none", "real", "user", "sys"),
  32. default="none",
  33. help="""
  34. The measurement to sort the output by. Defaults to 'none', which means to sort
  35. by the order paths were given on the command line. For other options, tasks are
  36. sorted in descending order from the highest value.
  37. """)
  38. args = arg_parser.parse_args()
  39. # Field names and regexes for parsing out their values from buildstat files
  40. field_regexes = (("elapsed", ".*Elapsed time: ([0-9.]+)"),
  41. ("user", "rusage ru_utime: ([0-9.]+)"),
  42. ("sys", "rusage ru_stime: ([0-9.]+)"),
  43. ("child user", "Child rusage ru_utime: ([0-9.]+)"),
  44. ("child sys", "Child rusage ru_stime: ([0-9.]+)"))
  45. # A list of (<path>, <dict>) tuples, where <path> is the path of a do_* task
  46. # buildstat file and <dict> maps fields from the file to their values
  47. task_infos = []
  48. def save_times_for_task(path):
  49. """Saves information for the buildstat file 'path' in 'task_infos'."""
  50. if not os.path.basename(path).startswith("do_"):
  51. return
  52. with open(path) as f:
  53. fields = {}
  54. for line in f:
  55. for name, regex in field_regexes:
  56. match = re.match(regex, line)
  57. if match:
  58. fields[name] = float(match.group(1))
  59. break
  60. # Check that all expected fields were present
  61. for name, regex in field_regexes:
  62. if name not in fields:
  63. print("Warning: Skipping '{}' because no field matching '{}' could be found"
  64. .format(path, regex),
  65. file=sys.stderr)
  66. return
  67. task_infos.append((path, fields))
  68. def save_times_for_dir(path):
  69. """Runs save_times_for_task() for each file in path and its subdirs, recursively."""
  70. # Raise an exception for os.walk() errors instead of ignoring them
  71. def walk_onerror(e):
  72. raise e
  73. for root, _, files in os.walk(path, onerror=walk_onerror):
  74. for fname in files:
  75. save_times_for_task(os.path.join(root, fname))
  76. for path in args.paths:
  77. if os.path.isfile(path):
  78. save_times_for_task(path)
  79. else:
  80. save_times_for_dir(path)
  81. def elapsed_time(task_info):
  82. return task_info[1]["elapsed"]
  83. def tot_user_time(task_info):
  84. return task_info[1]["user"] + task_info[1]["child user"]
  85. def tot_sys_time(task_info):
  86. return task_info[1]["sys"] + task_info[1]["child sys"]
  87. if args.sort != "none":
  88. sort_fn = {"real": elapsed_time, "user": tot_user_time, "sys": tot_sys_time}
  89. task_infos.sort(key=sort_fn[args.sort], reverse=True)
  90. first_entry = True
  91. # Catching BrokenPipeError avoids annoying errors when the output is piped into
  92. # e.g. 'less' or 'head' and not completely read
  93. try:
  94. for task_info in task_infos:
  95. real = elapsed_time(task_info)
  96. user = tot_user_time(task_info)
  97. sys = tot_sys_time(task_info)
  98. if not first_entry:
  99. print()
  100. first_entry = False
  101. # Mimic Bash's 'time' builtin
  102. print("{}:\n"
  103. "real\t{}m{:.3f}s\n"
  104. "user\t{}m{:.3f}s\n"
  105. "sys\t{}m{:.3f}s"
  106. .format(task_info[0],
  107. int(real//60), real%60,
  108. int(user//60), user%60,
  109. int(sys//60), sys%60))
  110. except BrokenPipeError:
  111. pass