diff --git a/README.md b/README.md index 5592672..18a8eed 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ The first option is to use the `ros2 trace` command. $ ros2 trace ``` -By default, it will enable all ROS tracepoints and a few kernel tracepoints. The trace will be written to `/tmp/session-YYYYMMDDHHMMSS`. Run the command with `-h` for more information. +By default, it will enable all ROS tracepoints and a few kernel tracepoints. The trace will be written to `~/.ros/tracing/session-YYYYMMDDHHMMSS`. Run the command with `-h` for more information. ### Launch file trace action diff --git a/ros2trace/ros2trace/api/__init__.py b/ros2trace/ros2trace/api/__init__.py index d39da83..4f139c1 100644 --- a/ros2trace/ros2trace/api/__init__.py +++ b/ros2trace/ros2trace/api/__init__.py @@ -14,10 +14,9 @@ """API functions for the ROS 2 trace command.""" -import os - from tracetools_trace.tools import args from tracetools_trace.tools import lttng +from tracetools_trace.tools import path def add_trace_arguments(parser): @@ -32,7 +31,6 @@ def init(args): """ session_name = args.session_name base_path = args.path - full_path = os.path.join(base_path, session_name) ros_events = args.events_ust kernel_events = args.events_kernel @@ -51,9 +49,14 @@ def init(args): else: print('kernel tracing disabled') - print(f'writting tracing session to: {full_path}') + full_session_path = path.get_full_session_path(session_name, base_path) + print(f'writing tracing session to: {full_session_path}') input('press enter to start...') - lttng.lttng_init(session_name, full_path, ros_events=ros_events, kernel_events=kernel_events) + lttng.lttng_init( + session_name, + base_path=base_path, + ros_events=ros_events, + kernel_events=kernel_events) def fini(args): diff --git a/tracetools_launch/launch/example.launch.py b/tracetools_launch/launch/example.launch.py index caecc05..45ff2a7 100644 --- a/tracetools_launch/launch/example.launch.py +++ b/tracetools_launch/launch/example.launch.py @@ -22,8 +22,7 @@ from tracetools_launch.action import Trace def generate_launch_description(): return LaunchDescription([ Trace( - session_name='my-tracing-session', - base_path='/tmp'), + session_name='my-tracing-session'), Node( package='tracetools_test', node_executable='test_ping', diff --git a/tracetools_launch/tracetools_launch/action.py b/tracetools_launch/tracetools_launch/action.py index e46ea28..00cfa67 100644 --- a/tracetools_launch/tracetools_launch/action.py +++ b/tracetools_launch/tracetools_launch/action.py @@ -24,6 +24,7 @@ from launch.event_handlers import OnShutdown from launch.launch_context import LaunchContext from tracetools_trace.tools import lttng from tracetools_trace.tools import names +from tracetools_trace.tools import path class Trace(Action): @@ -37,7 +38,7 @@ class Trace(Action): self, *, session_name: str, - base_path: str = '/tmp', + base_path: str = path.DEFAULT_BASE_PATH, events_ust: List[str] = names.DEFAULT_EVENTS_ROS, events_kernel: List[str] = names.DEFAULT_EVENTS_KERNEL, **kwargs, @@ -46,13 +47,13 @@ class Trace(Action): Constructor. :param session_name: the name of the tracing session - :param base_path: the base directory in which to create the trace directory + :param base_path: the path to the base directory in which to create the tracing session directory :param events_ust: the list of ROS UST events to enable :param events_kernel: the list of kernel events to enable """ super().__init__(**kwargs) self.__session_name = session_name - self.__path = os.path.join(base_path, session_name) + self.__base_path = base_path self.__events_ust = events_ust self.__events_kernel = events_kernel @@ -65,7 +66,7 @@ class Trace(Action): def _setup(self) -> None: lttng.lttng_init( self.__session_name, - self.__path, + self.__base_path, ros_events=self.__events_ust, kernel_events=self.__events_kernel) @@ -76,7 +77,7 @@ class Trace(Action): return ( "Trace(" f"session_name='{self.__session_name}', " - f"path='{self.__path}', " + f"base_path='{self.__base_path}', " f"num_events_ust={len(self.__events_ust)}, " f"num_events_kernel={len(self.__events_kernel)})" ) diff --git a/tracetools_test/tracetools_test/case.py b/tracetools_test/tracetools_test/case.py index 79f4f81..559a7e9 100644 --- a/tracetools_test/tracetools_test/case.py +++ b/tracetools_test/tracetools_test/case.py @@ -52,7 +52,6 @@ class TraceTestCase(unittest.TestCase): package: str = 'tracetools_test', ) -> None: """Constructor.""" - print(f'methodName={args[0]}') super().__init__(methodName=args[0]) self._base_path = base_path self._session_name_prefix = session_name_prefix diff --git a/tracetools_trace/tracetools_trace/tools/args.py b/tracetools_trace/tracetools_trace/tools/args.py index b56b328..c2a96ca 100644 --- a/tracetools_trace/tracetools_trace/tools/args.py +++ b/tracetools_trace/tracetools_trace/tools/args.py @@ -18,6 +18,7 @@ import argparse import time from . import names +from . import path class DefaultArgValueCompleter: @@ -45,7 +46,7 @@ def add_arguments(parser): help='the name of the tracing session (default: session-YYYYMMDDHHMMSS)') parser.add_argument( '--path', '-p', dest='path', - default='/tmp', + default=path.DEFAULT_BASE_PATH, help='path of the base directory for trace data (default: %(default)s)') arg = parser.add_argument( '--ust', '-u', nargs='*', dest='events_ust', default=names.DEFAULT_EVENTS_ROS, diff --git a/tracetools_trace/tracetools_trace/tools/lttng.py b/tracetools_trace/tracetools_trace/tools/lttng.py index d715d61..52e4a76 100644 --- a/tracetools_trace/tracetools_trace/tools/lttng.py +++ b/tracetools_trace/tracetools_trace/tools/lttng.py @@ -21,11 +21,13 @@ import lttng from .names import DEFAULT_CONTEXT from .names import DEFAULT_EVENTS_KERNEL from .names import DEFAULT_EVENTS_ROS +from .path import DEFAULT_BASE_PATH +from .path import get_full_session_path def lttng_init( session_name: str, - full_path: str, + base_path: str = DEFAULT_BASE_PATH, ros_events: List[str] = DEFAULT_EVENTS_ROS, kernel_events: List[str] = DEFAULT_EVENTS_KERNEL, context_names: List[str] = DEFAULT_CONTEXT @@ -34,12 +36,12 @@ def lttng_init( Set up and start LTTng session. :param session_name: the name of the session - :param full_path: the full path to the main directory to write trace data to + :param base_path: the path to the directory in which to create the tracing session directory :param ros_events: list of ROS events to enable :param kernel_events: list of kernel events to enable :param context_names: list of context elements to enable """ - _lttng_setup(session_name, full_path, ros_events, kernel_events, context_names) + _lttng_setup(session_name, base_path, ros_events, kernel_events, context_names) _lttng_start(session_name) @@ -55,7 +57,7 @@ def lttng_fini(session_name: str) -> None: def _lttng_setup( session_name: str, - full_path: str, + base_path: str = DEFAULT_BASE_PATH, ros_events: List[str] = DEFAULT_EVENTS_ROS, kernel_events: List[str] = DEFAULT_EVENTS_KERNEL, context_names: List[str] = DEFAULT_CONTEXT, @@ -68,13 +70,16 @@ def _lttng_setup( See: https://lttng.org/docs/#doc-core-concepts :param session_name: the name of the session - :param full_path: the full path to the main directory to write trace data to + :param base_path: the path to the directory in which to create the tracing session directory :param ros_events: list of ROS events to enable :param kernel_events: list of kernel events to enable :param context_names: list of context elements to enable :param channel_name_ust: the UST channel name :param channel_name_kernel: the kernel channel name """ + # Resolve full tracing directory path + full_path = get_full_session_path(session_name, base_path=base_path) + ust_enabled = ros_events is not None and len(ros_events) > 0 kernel_enabled = kernel_events is not None and len(kernel_events) > 0 diff --git a/tracetools_trace/tracetools_trace/tools/path.py b/tracetools_trace/tracetools_trace/tools/path.py new file mode 100644 index 0000000..5c830f0 --- /dev/null +++ b/tracetools_trace/tracetools_trace/tools/path.py @@ -0,0 +1,34 @@ +# Copyright 2019 Robert Bosch GmbH +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + + +DEFAULT_BASE_PATH = '~/.ros/tracing/' + + +def get_full_session_path( + session_name: str, + base_path: str = DEFAULT_BASE_PATH +) -> str: + """ + Get the full path to the trace directory of a given session. + + :param session_name: the name of the tracing session + :param base_path: the path to the directory containing the trace directory + :return: the full path to the tracing session directory + """ + if base_path is None: + base_path = DEFAULT_BASE_PATH + return os.path.expanduser(os.path.join(base_path, session_name)) diff --git a/tracetools_trace/tracetools_trace/trace.py b/tracetools_trace/tracetools_trace/trace.py index 679acff..f127038 100644 --- a/tracetools_trace/tracetools_trace/trace.py +++ b/tracetools_trace/tracetools_trace/trace.py @@ -15,10 +15,9 @@ """Entrypoint/script to setup and start an LTTng tracing session.""" -import os - from tracetools_trace.tools import args from tracetools_trace.tools import lttng +from tracetools_trace.tools import path def main(): @@ -26,7 +25,6 @@ def main(): session_name = params.session_name base_path = params.path - full_path = os.path.join(base_path, session_name) ros_events = params.events_ust kernel_events = params.events_kernel @@ -45,9 +43,14 @@ def main(): else: print('kernel tracing disabled') - print(f'writting tracing session to: {full_path}') + full_session_path = path.get_full_session_path(session_name, base_path) + print(f'writing tracing session to: {full_session_path}') input('press enter to start...') - lttng.lttng_init(session_name, full_path, ros_events=ros_events, kernel_events=kernel_events) + lttng.lttng_init( + session_name, + base_path=base_path, + ros_events=ros_events, + kernel_events=kernel_events) input('press enter to stop...') print('stopping & destroying tracing session')