Skip to content

Commit

Permalink
Implements --label option for python profiler (#34)
Browse files Browse the repository at this point in the history
* Implements --label option for python profiler

- removes '-a', '-f', and '-l' options in favor of '--label args file line' for consistency with omnitrace exe

* cmake formatting

* Bump version to 1.1.0
  • Loading branch information
jrmadsen authored Jun 10, 2022
1 parent 90ab7a8 commit 017e794
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 60 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.1.0
16 changes: 6 additions & 10 deletions source/docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,21 @@ python3.8 -m omnitrace --help
Use `omnitrace-python --help` to view the available options:

```console
usage: omnitrace [-h] [-b] [-c FILE] [-s FILE] [--trace-c [BOOL]] [-a [BOOL]] [-l [BOOL]] [-f [BOOL]] [-F [BOOL]] [-I FUNC [FUNC ...]] [-E FUNC [FUNC ...]] [-R FUNC [FUNC ...]] [-MI FILE [FILE ...]] [-ME FILE [FILE ...]] [-MR FILE [FILE ...]] [-v VERBOSITY]
usage: omnitrace [-h] [-v VERBOSITY] [-b] [-c FILE] [-s FILE] [-F [BOOL]] [--label [{args,file,line} [{args,file,line} ...]]] [-I FUNC [FUNC ...]] [-E FUNC [FUNC ...]] [-R FUNC [FUNC ...]] [-MI FILE [FILE ...]] [-ME FILE [FILE ...]] [-MR FILE [FILE ...]] [--trace-c [BOOL]]

optional arguments:
-h, --help show this help message and exit
-v VERBOSITY, --verbosity VERBOSITY
Logging verbosity
-b, --builtin Put 'profile' in the builtins. Use '@profile' to decorate a single function, or 'with profile:' to profile a single section of code.
-c FILE, --config FILE
Omnitrace configuration file
-s FILE, --setup FILE
Code to execute before the code to profile
--trace-c [BOOL] Enable profiling C functions
-a [BOOL], --include-args [BOOL]
Encode the argument values
-l [BOOL], --include-line [BOOL]
Encode the function line number
-f [BOOL], --include-file [BOOL]
Encode the function filename
-F [BOOL], --full-filepath [BOOL]
Encode the full function filename (instead of basename)
--label [{args,file,line} [{args,file,line} ...]]
Encode the function arguments, filename, and/or line number into the profiling function label
-I FUNC [FUNC ...], --function-include FUNC [FUNC ...]
Include any entries with these function names
-E FUNC [FUNC ...], --function-exclude FUNC [FUNC ...]
Expand All @@ -79,8 +76,7 @@ optional arguments:
Filter out any entries from these files
-MR FILE [FILE ...], --module-restrict FILE [FILE ...]
Select only entries from these files
-v VERBOSITY, --verbosity VERBOSITY
Logging verbosity
--trace-c [BOOL] Enable profiling C functions

usage: python3 -m omnitrace <OMNITRACE_ARGS> -- <SCRIPT> <SCRIPT_ARGS>
```
Expand Down
80 changes: 33 additions & 47 deletions source/python/omnitrace/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,28 @@ def str2bool(v):
else:
raise argparse.ArgumentTypeError("Boolean value expected.")

_default_label = []
if _profiler_config.include_args:
_default_label.append("args")
if _profiler_config.include_filename:
_default_label.append("file")
if _profiler_config.include_line:
_default_label.append("line")

parser = argparse.ArgumentParser(
"omnitrace",
add_help=True,
epilog="usage: {} -m omnitrace <OMNITRACE_ARGS> -- <SCRIPT> <SCRIPT_ARGS>".format(
os.path.basename(sys.executable)
),
)
parser.add_argument(
"-v",
"--verbosity",
type=int,
default=_profiler_config.verbosity,
help="Logging verbosity",
)
parser.add_argument(
"-b",
"--builtin",
Expand All @@ -124,45 +139,6 @@ def str2bool(v):
metavar="FILE",
help="Code to execute before the code to profile",
)
parser.add_argument(
"--trace-c",
type=str2bool,
nargs="?",
metavar="BOOL",
const=True,
default=_profiler_config.trace_c,
help="Enable profiling C functions",
)
parser.add_argument(
"-a",
"--include-args",
type=str2bool,
nargs="?",
metavar="BOOL",
const=True,
default=_profiler_config.include_args,
help="Encode the argument values",
)
parser.add_argument(
"-l",
"--include-line",
type=str2bool,
nargs="?",
metavar="BOOL",
const=True,
default=_profiler_config.include_line,
help="Encode the function line number",
)
parser.add_argument(
"-f",
"--include-file",
type=str2bool,
nargs="?",
metavar="BOOL",
const=True,
default=_profiler_config.include_filename,
help="Encode the function filename",
)
parser.add_argument(
"-F",
"--full-filepath",
Expand All @@ -173,6 +149,14 @@ def str2bool(v):
default=_profiler_config.full_filepath,
help="Encode the full function filename (instead of basename)",
)
parser.add_argument(
"--label",
type=str,
choices=("args", "file", "line"),
nargs="*",
default=_default_label,
help="Encode the function arguments, filename, and/or line number into the profiling function label",
)
parser.add_argument(
"-I",
"--function-include",
Expand Down Expand Up @@ -228,11 +212,13 @@ def str2bool(v):
help="Select only entries from these files",
)
parser.add_argument(
"-v",
"--verbosity",
type=int,
default=_profiler_config.verbosity,
help="Logging verbosity",
"--trace-c",
type=str2bool,
nargs="?",
metavar="BOOL",
const=True,
default=_profiler_config.trace_c,
help="Enable profiling C functions",
)

return parser.parse_args(args)
Expand Down Expand Up @@ -326,9 +312,9 @@ def main():
from .libpyomnitrace.profiler import config as _profiler_config

_profiler_config.trace_c = opts.trace_c
_profiler_config.include_args = opts.include_args
_profiler_config.include_line = opts.include_line
_profiler_config.include_filename = opts.include_file
_profiler_config.include_args = "args" in opts.label
_profiler_config.include_line = "line" in opts.label
_profiler_config.include_filename = "file" in opts.label
_profiler_config.full_filepath = opts.full_filepath
_profiler_config.include_functions = opts.function_include
_profiler_config.include_modules = opts.module_include
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ foreach(_VERSION ${OMNITRACE_PYTHON_VERSIONS})
PYTHON_EXECUTABLE ${_PYTHON_EXECUTABLE}
PYTHON_VERSION ${_VERSION}
FILE ${CMAKE_SOURCE_DIR}/examples/python/external.py
PROFILE_ARGS -f
PROFILE_ARGS "--label" "file"
RUN_ARGS -v 10 -n 5
ENVIRONMENT "${_python_environment}")

Expand All @@ -826,7 +826,7 @@ foreach(_VERSION ${OMNITRACE_PYTHON_VERSIONS})
PYTHON_EXECUTABLE ${_PYTHON_EXECUTABLE}
PYTHON_VERSION ${_VERSION}
FILE ${CMAKE_SOURCE_DIR}/examples/python/builtin.py
PROFILE_ARGS -b -l -f
PROFILE_ARGS "-b" "--label" "file" "line"
RUN_ARGS -v 10 -n 5
ENVIRONMENT "${_python_environment}")

Expand Down

0 comments on commit 017e794

Please sign in to comment.