diff --git a/tests/test_other.py b/tests/test_other.py index 7f8ccd1f4623e..4351ed81b298d 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -46,7 +46,6 @@ emconfigure = shared.bat_suffix(path_from_root('emconfigure')) emconfig = shared.bat_suffix(path_from_root('em-config')) emsize = shared.bat_suffix(path_from_root('emsize')) -emprofile = shared.bat_suffix(path_from_root('tools', 'emprofile')) wasm_dis = os.path.join(building.get_binaryen_bin(), 'wasm-dis') wasm_opt = os.path.join(building.get_binaryen_bin(), 'wasm-opt') @@ -7375,20 +7374,10 @@ def test_closure_externs(self): self.run_process([EMCC, path_from_root('tests', 'hello_world.c'), '--closure', '1', '--pre-js', path_from_root('tests', 'test_closure_externs_pre_js.js'), '--closure-args', '--externs "' + path_from_root('tests', 'test_closure_externs.js') + '"']) def test_toolchain_profiler(self): - # Verify some basic functionality of EM_PROFILE_TOOLCHAIN environ = os.environ.copy() environ['EM_PROFILE_TOOLCHAIN'] = '1' - - self.run_process([emprofile, '--reset']) - err = self.expect_fail([emprofile, '--graph']) - self.assertContained('No profiler logs were found', err) - + # replaced subprocess functions should not cause errors self.run_process([EMCC, path_from_root('tests', 'hello_world.c')], env=environ) - self.assertEqual('hello, world!', self.run_js('a.out.js').strip()) - - self.run_process([emprofile, '--graph']) - self.assertTrue(glob.glob('toolchain_profiler.results*.html')) - self.assertTrue(glob.glob('toolchain_profiler.results*.json')) def test_noderawfs(self): fopen_write = open(path_from_root('tests', 'asmfs', 'fopen_write.cpp')).read() diff --git a/tools/create_entry_points.py b/tools/create_entry_points.py index 4dc75dc9b4010..ced34f23b5310 100755 --- a/tools/create_entry_points.py +++ b/tools/create_entry_points.py @@ -31,7 +31,6 @@ emscons emsize tools/emdump -tools/emprofile tools/file_packager '''.split() diff --git a/tools/emprofile b/tools/emprofile deleted file mode 100755 index b9fcf9532af82..0000000000000 --- a/tools/emprofile +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh -# Copyright 2020 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. -# -# Entry point for running python scripts on UNIX systems. -# -# To modify this file, edit `tools/run_python.sh` and then run -# `tools/create_entry_points.py` - -if [ -z "$PYTHON" ]; then - PYTHON=$EMSDK_PYTHON -fi - -if [ -z "$PYTHON" ]; then - PYTHON=$(which python3 2> /dev/null) -fi - -if [ -z "$PYTHON" ]; then - PYTHON=$(which python 2> /dev/null) -fi - -if [ -z "$PYTHON" ]; then - echo 'unable to find python in $PATH' - exit 1 -fi - -exec "$PYTHON" "$0.py" "$@" diff --git a/tools/emprofile.bat b/tools/emprofile.bat index 37eee09d6abea..f9d8033274278 100644 --- a/tools/emprofile.bat +++ b/tools/emprofile.bat @@ -1,11 +1,2 @@ -:: Entry point for running python scripts on windows systems. -:: To modify this file, edit `tools/run_python.bat` and then run -:: `tools/create_entry_points.py` - -@setlocal -@set EM_PY=%EMSDK_PYTHON% -@if "%EM_PY%"=="" ( - set EM_PY=python -) - -@"%EM_PY%" "%~dp0\%~n0.py" %* +@echo off +python "%~dp0\emprofile.py" %* \ No newline at end of file diff --git a/tools/emprofile.py b/tools/emprofile.py index 643ff0cdfd4c6..cf0cb80c1cd52 100755 --- a/tools/emprofile.py +++ b/tools/emprofile.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python # 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 @@ -16,24 +16,34 @@ # 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(): - if os.path.exists(profiler_logs_path): + try: shutil.rmtree(profiler_logs_path) + except IOError: + pass def list_files_in_directory(d): files = [] - if os.path.exists(d): - for i in os.listdir(d): + try: + items = os.listdir(d) + for i in items: f = os.path.join(d, i) if os.path.isfile(f): - files.append(f) - return files + files += [f] + return files + except IOError: + return [] -def create_profiling_graph(outfile): +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 = [] @@ -53,16 +63,16 @@ def create_profiling_graph(outfile): print('Failed to parse JSON file "' + f + '"!', file=sys.stderr) sys.exit(1) if len(all_results) == 0: - print(f'No profiler logs were found in path: ${profiler_logs_path}.\nTry setting the environment variable EM_PROFILE_TOOLCHAIN=1 and run some emcc commands, then re-run "emprofile.py --graph".', file=sys.stderr) - return 1 + 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' + json_file = OUTFILE + '.json' open(json_file, 'w').write(json.dumps(all_results, indent=2)) print('Wrote "' + json_file + '"') - html_file = outfile + '.html' + 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 + '"') @@ -70,13 +80,9 @@ def create_profiling_graph(outfile): if not DEBUG_EMPROFILE_PY: delete_profiler_logs() - return 0 - -def main(args): - if len(args) < 2: - print('''\ -Usage: +if len(sys.argv) < 2: + print('''Usage: emprofile.py --reset Deletes all previously recorded profiling log files. @@ -87,23 +93,14 @@ def main(args): --outfile=x.html Specifies the name of the results file to generate. - ''') - return 1 - - if '--reset' in args: - delete_profiler_logs() - elif '--graph' in args: - outfile = 'toolchain_profiler.results_' + time.strftime('%Y%m%d_%H%M') - for arg in args: - if arg.startswith('--outfile='): - outfile = arg.split('=', 1)[1].strip().replace('.html', '') - return create_profiling_graph(outfile) - else: - print('Unknown command "' + args[1] + '"!') - return 1 - - return 0 +''') + sys.exit(1) -if __name__ == '__main__': - sys.exit(main(sys.argv)) +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)