11cb0ef41Sopenharmony_ci#!/usr/bin/env python3
21cb0ef41Sopenharmony_ci# Copyright 2021 the V8 project authors. All rights reserved.
31cb0ef41Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
41cb0ef41Sopenharmony_ci# found in the LICENSE file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci# Processes {stdout} output generated by --trace-wasm-compilation-times
71cb0ef41Sopenharmony_ci# for easier consumption by human readers.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciimport sys
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cidef SizeInternal(number, suffix):
121cb0ef41Sopenharmony_ci  if suffix == "": return "%d" % number
131cb0ef41Sopenharmony_ci  if number < 10: return "%.1f%s" % (number, suffix)
141cb0ef41Sopenharmony_ci  return "%d%s" % (number, suffix)
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cidef Size(number):
171cb0ef41Sopenharmony_ci  if (number < 1024): return SizeInternal(number, "")
181cb0ef41Sopenharmony_ci  number /= 1024
191cb0ef41Sopenharmony_ci  if (number < 1024): return SizeInternal(number, "K")
201cb0ef41Sopenharmony_ci  number /= 1024
211cb0ef41Sopenharmony_ci  if (number < 1024): return SizeInternal(number, "M")
221cb0ef41Sopenharmony_ci  number /= 1024
231cb0ef41Sopenharmony_ci  if (number < 1024): return SizeInternal(number, "G")
241cb0ef41Sopenharmony_ci  return SizeInternal(number / 1024, "T")
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cimodules = {}
271cb0ef41Sopenharmony_cimax_module = 0
281cb0ef41Sopenharmony_citotal_tf_time = 0
291cb0ef41Sopenharmony_citotal_tf_size = 0
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cidef RegisterName(raw):
321cb0ef41Sopenharmony_ci  global max_module
331cb0ef41Sopenharmony_ci  parts = raw.split("#")
341cb0ef41Sopenharmony_ci  m = parts[0]
351cb0ef41Sopenharmony_ci  if m not in modules:
361cb0ef41Sopenharmony_ci    modules[m] = max_module
371cb0ef41Sopenharmony_ci    max_module += 1
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cidef Name(raw):
401cb0ef41Sopenharmony_ci  parts = raw.split("#")
411cb0ef41Sopenharmony_ci  if len(modules) == 1: return "#%s" % parts[1]
421cb0ef41Sopenharmony_ci  return "m%d#%s" % (modules[parts[0]], parts[1])
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciclass Function:
451cb0ef41Sopenharmony_ci  def __init__(self, index):
461cb0ef41Sopenharmony_ci    self.index = index
471cb0ef41Sopenharmony_ci    self.has_lo = False
481cb0ef41Sopenharmony_ci    self.has_tf = False
491cb0ef41Sopenharmony_ci    self.time_lo = -1
501cb0ef41Sopenharmony_ci    self.time_tf = -1
511cb0ef41Sopenharmony_ci    self.mem_lo = -1
521cb0ef41Sopenharmony_ci    self.mem_tf_max = -1
531cb0ef41Sopenharmony_ci    self.mem_tf_total = -1
541cb0ef41Sopenharmony_ci    self.name = ""
551cb0ef41Sopenharmony_ci    self.size_wasm = -1
561cb0ef41Sopenharmony_ci    self.size_lo = -1
571cb0ef41Sopenharmony_ci    self.size_tf = -1
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  def AddLine(self, words):
601cb0ef41Sopenharmony_ci    assert self.index == words[2], "wrong function"
611cb0ef41Sopenharmony_ci    if words[4] == "TurboFan,":
621cb0ef41Sopenharmony_ci      self.AddTFLine(words)
631cb0ef41Sopenharmony_ci    elif words[4] == "Liftoff,":
641cb0ef41Sopenharmony_ci      self.AddLiftoffLine(words)
651cb0ef41Sopenharmony_ci    else:
661cb0ef41Sopenharmony_ci      raise Exception("unknown compiler: %s" % words[4])
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  def AddTFLine(self, words):
691cb0ef41Sopenharmony_ci    assert not self.has_tf, "duplicate TF line for %s" % self.index
701cb0ef41Sopenharmony_ci    self.has_tf = True
711cb0ef41Sopenharmony_ci    # 0        1        2  3     4         5    6 7  8   9     10 11
721cb0ef41Sopenharmony_ci    # Compiled function #6 using TurboFan, took 0 ms and 14440 / 44656
731cb0ef41Sopenharmony_ci    # 12        13     14       15 16       17 18   19
741cb0ef41Sopenharmony_ci    # max/total bytes; bodysize 12 codesize 24 name wasm-function#6
751cb0ef41Sopenharmony_ci    self.time_tf = int(words[6])
761cb0ef41Sopenharmony_ci    self.mem_tf_max = int(words[9])
771cb0ef41Sopenharmony_ci    self.mem_tf_total = int(words[11])
781cb0ef41Sopenharmony_ci    self.size_tf = int(words[17])
791cb0ef41Sopenharmony_ci    self.name = words[19]
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  def AddLiftoffLine(self, words):
821cb0ef41Sopenharmony_ci    assert self.index == words[2], "wrong function"
831cb0ef41Sopenharmony_ci    assert not self.has_lo, "duplicate Liftoff line for %s" % self.index
841cb0ef41Sopenharmony_ci    self.has_lo = True
851cb0ef41Sopenharmony_ci    # 0        1        2  3     4        5    6 7  8   9   10     11       12
861cb0ef41Sopenharmony_ci    # Compiled function #6 using Liftoff, took 0 ms and 968 bytes; bodysize 4
871cb0ef41Sopenharmony_ci    # 13       14
881cb0ef41Sopenharmony_ci    # codesize 68
891cb0ef41Sopenharmony_ci    self.time_lo = int(words[6])
901cb0ef41Sopenharmony_ci    self.mem_lo = int(words[9])
911cb0ef41Sopenharmony_ci    self.size_lo = int(words[14])
921cb0ef41Sopenharmony_ci    self.size_wasm = int(words[12])
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci  def __str__(self):
951cb0ef41Sopenharmony_ci    return "%s: time %d %d mem %s %s %s size %s %s %s name %s" % (
961cb0ef41Sopenharmony_ci      Name(self.index), self.time_lo, self.time_tf,
971cb0ef41Sopenharmony_ci      Size(self.mem_lo), Size(self.mem_tf_max), Size(self.mem_tf_total),
981cb0ef41Sopenharmony_ci      Size(self.size_wasm), Size(self.size_lo), Size(self.size_tf), self.name
991cb0ef41Sopenharmony_ci    )
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_cifuncs_dict = {}
1021cb0ef41Sopenharmony_cifuncs_list = []
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciif len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help", "help"):
1051cb0ef41Sopenharmony_ci  print("Pass output file (generated with --trace-wasm-compilation-times) as "
1061cb0ef41Sopenharmony_ci        "argument")
1071cb0ef41Sopenharmony_ci  sys.exit(1)
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciwith open(sys.argv[1], "r") as f:
1101cb0ef41Sopenharmony_ci  for line in f.readlines():
1111cb0ef41Sopenharmony_ci    words = line.strip().split(" ")
1121cb0ef41Sopenharmony_ci    if words[0] != "Compiled" or words[1] != "function":
1131cb0ef41Sopenharmony_ci      continue
1141cb0ef41Sopenharmony_ci    name = words[2]
1151cb0ef41Sopenharmony_ci    RegisterName(name)
1161cb0ef41Sopenharmony_ci    if name in funcs_dict:
1171cb0ef41Sopenharmony_ci      func = funcs_dict[name]
1181cb0ef41Sopenharmony_ci    else:
1191cb0ef41Sopenharmony_ci      func = Function(name)
1201cb0ef41Sopenharmony_ci      funcs_dict[name] = func
1211cb0ef41Sopenharmony_ci      funcs_list.append(func)
1221cb0ef41Sopenharmony_ci    func.AddLine(words)
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_cifuncs_list.sort(key=lambda fun: fun.time_tf)
1251cb0ef41Sopenharmony_cifor f in funcs_list:
1261cb0ef41Sopenharmony_ci  print(f)
1271cb0ef41Sopenharmony_ci  if f.time_tf > 0: total_tf_time += f.time_tf
1281cb0ef41Sopenharmony_ci  if f.size_tf > 0: total_tf_size += f.size_tf
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ciprint("Total TF time: %d" % total_tf_time)
1311cb0ef41Sopenharmony_ciprint("Total TF size: %d" % total_tf_size)
132