Skip to content

Commit

Permalink
Error out if trace already exists unless 'append trace' option is used
Browse files Browse the repository at this point in the history
Signed-off-by: Christophe Bedard <christophe.bedard@apex.ai>
  • Loading branch information
christophebedard committed Apr 17, 2023
1 parent 45305b2 commit cc77af0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
14 changes: 14 additions & 0 deletions tracetools_launch/tracetools_launch/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(
session_name: SomeSubstitutionsType,
append_timestamp: bool = False,
base_path: Optional[SomeSubstitutionsType] = None,
append_trace: bool = False,
events_ust: Iterable[SomeSubstitutionsType] = names.DEFAULT_EVENTS_ROS,
events_kernel: Iterable[SomeSubstitutionsType] = [],
context_fields:
Expand All @@ -135,6 +136,8 @@ def __init__(
:param append_timestamp: whether to append timestamp to the session name
:param base_path: the path to the base directory in which to create the session directory,
or `None` for default
:param append_trace: whether to append to the trace directory if it already exists,
otherwise an error is reported
:param events_ust: the list of ROS UST events to enable
:param events_kernel: the list of kernel events to enable
:param context_fields: the names of context fields to enable
Expand All @@ -152,6 +155,7 @@ def __init__(
self.__session_name = normalize_to_list_of_substitutions(session_name)
self.__base_path = base_path \
if base_path is None else normalize_to_list_of_substitutions(base_path)
self.__append_trace = append_trace
self.__trace_directory = None
self.__events_ust = [normalize_to_list_of_substitutions(x) for x in events_ust]
self.__events_kernel = [normalize_to_list_of_substitutions(x) for x in events_kernel]
Expand All @@ -174,6 +178,10 @@ def session_name(self):
def base_path(self):
return self.__base_path

@property
def append_trace(self):
return self.__append_trace

@property
def trace_directory(self):
return self.__trace_directory
Expand Down Expand Up @@ -272,6 +280,10 @@ def parse(cls, entity: Entity, parser: Parser):
base_path = entity.get_attr('base-path', optional=True)
if base_path:
kwargs['base_path'] = parser.parse_substitution(base_path)
append_trace = entity.get_attr(
'append-trace', data_type=bool, optional=True, can_be_str=False)
if append_trace is not None:
kwargs['append_trace'] = append_trace
# Make sure to handle empty strings and replace with empty lists,
# otherwise an empty string enables all events
events_ust = entity.get_attr('events-ust', optional=True)
Expand Down Expand Up @@ -402,6 +414,7 @@ def _setup(self) -> bool:
self.__trace_directory = lttng.lttng_init(
session_name=self.__session_name,
base_path=self.__base_path,
append_trace=self.__append_trace,
ros_events=self.__events_ust,
kernel_events=self.__events_kernel,
context_fields=self.__context_fields,
Expand Down Expand Up @@ -436,6 +449,7 @@ def __repr__(self):
'Trace('
f'session_name={self.__session_name}, '
f'base_path={self.__base_path}, '
f'append_trace={self.__append_trace}, '
f'trace_directory={self.__trace_directory}, '
f'events_ust={self.__events_ust}, '
f'events_kernel={self.__events_kernel}, '
Expand Down
3 changes: 3 additions & 0 deletions tracetools_trace/tracetools_trace/tools/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
'-l', '--list', dest='list', action='store_true',
help='display lists of enabled events and context names (default: %(default)s)')
parser.add_argument(
'-a', '--append-trace', dest='append_trace', action='store_true',
help='append to trace if it already exists, otherwise error out (default: %(default)s)')
11 changes: 9 additions & 2 deletions tracetools_trace/tracetools_trace/tools/lttng_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def setup(
*,
session_name: str,
base_path: str,
append_trace: bool = False,
ros_events: Union[List[str], Set[str]] = DEFAULT_EVENTS_ROS,
kernel_events: Union[List[str], Set[str]] = [],
context_fields: Union[List[str], Set[str], Dict[str, List[str]]] = DEFAULT_CONTEXT,
Expand All @@ -89,6 +90,8 @@ def setup(
:param session_name: the name of the session
:param base_path: the path to the directory in which to create the tracing session directory,
which will be created if needed
:param append_trace: whether to append to the trace directory if it already exists, otherwise
an error is reported
:param ros_events: list of ROS events to enable
:param kernel_events: list of kernel events to enable
:param context_fields: the names of context fields to enable
Expand All @@ -106,6 +109,11 @@ def setup(
# Validate parameters
if not session_name:
raise RuntimeError('empty session name')
# Resolve full tracing directory path
full_path = os.path.join(base_path, session_name)
if os.path.isdir(full_path) and not append_trace:
raise RuntimeError(
f'trace directory already exists, use the append option to append to it: {full_path}')

# Check if there is a session daemon running
if lttng.session_daemon_alive() == 0:
Expand Down Expand Up @@ -179,9 +187,8 @@ def setup(
channel_kernel.attr.output = lttng.EVENT_MMAP
events_list_kernel = _create_events(kernel_events)

# Resolve full tracing directory path and create session
# Create session
# LTTng will create the parent directories if needed
full_path = os.path.join(base_path, session_name)
_create_session(session_name, full_path)

# Handles, channels, events
Expand Down
5 changes: 5 additions & 0 deletions tracetools_trace/tracetools_trace/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def init(
*,
session_name: str,
base_path: Optional[str],
append_trace: bool,
ros_events: List[str],
kernel_events: List[str],
context_fields: List[str],
Expand All @@ -46,6 +47,8 @@ def init(
:param session_name: the name of the session
:param base_path: the path to the directory in which to create the tracing session directory,
or `None` for default
:param append_trace: whether to append to the trace directory if it already exists, otherwise
an error is reported
:param ros_events: list of ROS events to enable
:param kernel_events: list of kernel events to enable
:param context_fields: list of context fields to enable
Expand Down Expand Up @@ -84,6 +87,7 @@ def init(
trace_directory = lttng.lttng_init(
session_name=session_name,
base_path=base_path,
append_trace=append_trace,
ros_events=ros_events,
kernel_events=kernel_events,
context_fields=context_fields,
Expand Down Expand Up @@ -137,6 +141,7 @@ def trace(args: argparse.Namespace) -> int:
if not init(
session_name=args.session_name,
base_path=args.path,
append_trace=args.append_trace,
ros_events=args.events_ust,
kernel_events=args.events_kernel,
context_fields=args.context_fields,
Expand Down

0 comments on commit cc77af0

Please sign in to comment.