File: emprofile.py

package info (click to toggle)
emscripten 2.0.12~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 108,440 kB
  • sloc: ansic: 510,324; cpp: 384,763; javascript: 84,341; python: 51,362; sh: 50,019; pascal: 4,159; makefile: 3,409; asm: 2,150; lisp: 1,869; ruby: 488; cs: 142
file content (106 lines) | stat: -rwxr-xr-x 3,332 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# Copyright 2016 The Emscripten Authors.  All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License.  Both these licenses can be
# found in the LICENSE file.

import json
import os
import shutil
import sys
import tempfile
import time

profiler_logs_path = os.path.join(tempfile.gettempdir(), 'emscripten_toolchain_profiler_logs')

# If set to 1, always generates the output file under the same filename and doesn't delete the temp data.
DEBUG_EMPROFILE_PY = 0

OUTFILE = 'toolchain_profiler.results_' + time.strftime('%Y%m%d_%H%M')
for arg in sys.argv:
  if arg.startswith('--outfile='):
    OUTFILE = arg.split('=', 1)[1].strip().replace('.html', '')


# Deletes all previously captured log files to make room for a new clean run.
def delete_profiler_logs():
  try:
    shutil.rmtree(profiler_logs_path)
  except IOError:
    pass


def list_files_in_directory(d):
  files = []
  try:
    items = os.listdir(d)
    for i in items:
      f = os.path.join(d, i)
      if os.path.isfile(f):
        files += [f]
    return files
  except IOError:
    return []


def create_profiling_graph():
  log_files = [f for f in list_files_in_directory(profiler_logs_path) if 'toolchain_profiler.pid_' in f]

  all_results = []
  if len(log_files):
    print('Processing ' + str(len(log_files)) + ' profile log files in "' + profiler_logs_path + '"...')
  for f in log_files:
    try:
      json_data = open(f, 'r').read()
      lines = json_data.split('\n')
      lines = [x for x in lines if x != '[' and x != ']' and x != ',' and len(x.strip())]
      lines = [(x + ',') if not x.endswith(',') else x for x in lines]
      lines[-1] = lines[-1][:-1]
      json_data = '[' + '\n'.join(lines) + ']'
      all_results += json.loads(json_data)
    except Exception as e:
      print(str(e), file=sys.stderr)
      print('Failed to parse JSON file "' + f + '"!', file=sys.stderr)
      sys.exit(1)
  if len(all_results) == 0:
    print('No profiler logs were found in path "' + profiler_logs_path + '". Try setting the environment variable EM_PROFILE_TOOLCHAIN=1 and run some emcc commands, and then rerun "python emprofile.py --graph" again.')
    return

  all_results.sort(key=lambda x: x['time'])

  json_file = OUTFILE + '.json'
  open(json_file, 'w').write(json.dumps(all_results, indent=2))
  print('Wrote "' + json_file + '"')

  html_file = OUTFILE + '.html'
  html_contents = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'toolchain_profiler.results_template.html'), 'r').read().replace('{{{results_log_file}}}', '"' + json_file + '"')
  open(html_file, 'w').write(html_contents)
  print('Wrote "' + html_file + '"')

  if not DEBUG_EMPROFILE_PY:
    delete_profiler_logs()


if len(sys.argv) < 2:
  print('''Usage:
       emprofile.py --reset
         Deletes all previously recorded profiling log files.

       emprofile.py --graph
         Draws a graph from all recorded profiling log files.

Optional parameters:

        --outfile=x.html
          Specifies the name of the results file to generate.
''')
  sys.exit(1)


if '--reset' in sys.argv:
  delete_profiler_logs()
elif '--graph' in sys.argv:
  create_profiling_graph()
else:
  print('Unknown command "' + sys.argv[1] + '"!')
  sys.exit(1)