Skip to content

Commit

Permalink
Fixed invocation path (plasma-umass#624)
Browse files Browse the repository at this point in the history
* Updated path

* Made all paths canonically absolute

* Added updated smoketests

* Hoiseted variable

* Added debugging info to smoketest

* Small smoketest update

* Added print

* Removed a lower statement

* Added debug printing

* More debug printing

* Added variable for profiler base

* Added definition

* Tried to make type checker happy

* Some cleanup

---------

Co-authored-by: Sam Stern <jstern@umass.edu>
Co-authored-by: Sam Stern <sam@samstern.me>
  • Loading branch information
3 people authored Jul 26, 2023
1 parent 21be704 commit 90d4437
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
3 changes: 1 addition & 2 deletions scalene/scalene_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,13 @@ def output_profiles(
)

# Print out the the profile for the source, line by line.
full_fname = os.path.normpath(os.path.join(program_path, fname))
full_fname = program
try:
with open(full_fname, "r", encoding="utf-8") as source_file:
code_lines = source_file.readlines()

except (FileNotFoundError, OSError):
continue

# Find all enclosing regions (loops or function defs) for each line of code.

code_str = ''.join(code_lines)
Expand Down
30 changes: 16 additions & 14 deletions scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class Scalene:
__json.gpu = __gpu.has_gpu()
__invalidate_queue: List[Tuple[Filename, LineNumber]] = []
__invalidate_mutex: threading.Lock

__profiler_base: str
@staticmethod
def get_original_lock() -> threading.Lock:
"""Return the true lock, which we shim in replacement_lock.py."""
Expand Down Expand Up @@ -704,7 +704,6 @@ def __init__(
arguments: argparse.Namespace,
program_being_profiled: Optional[Filename] = None,
) -> None:

import scalene.replacement_exit
import scalene.replacement_get_context

Expand Down Expand Up @@ -792,6 +791,7 @@ def __init__(
sys.executable,
cmdline,
)
Scalene.__profiler_base = str(os.path.dirname(__file__))
# Now create all the files.
for name in Scalene.__all_python_names:
fname = os.path.join(Scalene.__python_alias_dir, name)
Expand Down Expand Up @@ -1612,7 +1612,7 @@ def should_trace(filename: str, func : str) -> bool:
"""Return true if we should trace this filename and function."""
if not filename:
return False
if os.path.join("scalene", "scalene") in filename:
if Scalene.__profiler_base in filename:
# Don't profile the profiler.
return False
if Scalene.__functions_to_profile:
Expand All @@ -1621,18 +1621,18 @@ def should_trace(filename: str, func : str) -> bool:
return True
return False
# Don't profile the Python libraries, unless overridden by --profile-all
try:
resolved_filename = str(pathlib.Path(filename).resolve())
except OSError:
# Not a file
return False
if not Scalene.__args.profile_all:
try:
resolved_filename = str(pathlib.Path(filename).resolve()).lower()
except OSError:
# Not a file
return False
for n in sysconfig.get_scheme_names():
for p in sysconfig.get_path_names():
libdir = str(pathlib.Path(sysconfig.get_path(p, n)).resolve()).lower()
libdir = str(pathlib.Path(sysconfig.get_path(p, n)).resolve())
if libdir in resolved_filename:
return False

# Generic handling follows (when no @profile decorator has been used).
profile_exclude_list = Scalene.__args.profile_exclude.split(",")
if any(
Expand Down Expand Up @@ -1673,7 +1673,9 @@ def should_trace(filename: str, func : str) -> bool:
filename = os.path.normpath(
os.path.join(Scalene.__program_path, filename)
)
return Scalene.__program_path in filename

return Scalene.__program_path in resolved_filename


__done = False

Expand Down Expand Up @@ -2034,7 +2036,7 @@ def run_profiler(
try:
code = compile(
prog_being_profiled.read(),
progs[0],
os.path.abspath(progs[0]),
"exec",
)
except SyntaxError:
Expand All @@ -2052,7 +2054,7 @@ def run_profiler(
)
else:
# Otherwise, use the invoked directory.
Scalene.__program_path = os.getcwd()
Scalene.__program_path = program_path
# Grab local and global variables.
if Scalene.__args.memory:
from scalene import pywhere # type: ignore
Expand All @@ -2073,7 +2075,7 @@ def run_profiler(
# Do a GC before we start.
gc.collect()
# Start the profiler.
profiler = Scalene(args, Filename(progs[0]))
profiler = Scalene(args, Filename(os.path.abspath(progs[0])))
try:
# We exit with this status (returning error code as appropriate).
exit_status = profiler.profile_code(
Expand Down
22 changes: 16 additions & 6 deletions test/smoketest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@
import json

def smoketest(fname, rest):
proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", *rest, fname] ,capture_output=True)
cmd = [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", "/dev/stderr", *rest, fname]
print("COMMAND", ' '.join(cmd))
proc = subprocess.run(cmd ,capture_output=True)
stdout = proc.stdout.decode('utf-8')
stderr = proc.stderr.decode('utf-8')

if proc.returncode != 0:
print("Exited with a non-zero code:", proc.returncode)
print("Stdout:", proc.stdout.decode('utf-8'))
print("STDOUT", stdout)
print("STDERR", stderr)

exit(proc.returncode)
# stdout = proc.stdout.decode('utf-8')
stderr = proc.stderr.decode('utf-8')
# print("STDOUT", stdout)
# print("STDOUT", stdout)
# print("\nSTDERR", stderr)
try:
scalene_json = json.loads(stderr)
except json.JSONDecodeError:
print("Invalid JSON", stderr)
print("STDOUT", stdout)
print("STDERR", stderr)
exit(1)
if len(scalene_json) == 0:
print("No JSON output")
print("STDOUT", stdout)
print("STDERR", stderr)
exit(1)
files = scalene_json['files']
if not len(files) > 0:
print("No files found in output")
exit(1)
for _fname in files:
if not any( (line['n_cpu_percent_c'] > 0 or line['n_cpu_percent_python'] > 0) for line in files[fname]['lines']):

if not any( (line['n_cpu_percent_c'] > 0 or line['n_cpu_percent_python'] > 0) for line in files[_fname]['lines']):
print("No non-zero lines in", _fname)
exit(1)

Expand Down

0 comments on commit 90d4437

Please sign in to comment.