ros2_tracing/tracetools_launch/trace.py

66 lines
2 KiB
Python
Raw Normal View History

2019-06-23 14:20:46 +02:00
"""Module for the Trace action."""
2019-06-17 14:33:04 +02:00
import os
from typing import List
from typing import Optional
from launch.action import Action
2019-06-17 14:33:04 +02:00
from launch.event import Event
from launch.event_handlers import OnShutdown
from launch.launch_context import LaunchContext
2019-06-17 14:55:06 +02:00
from tracetools_trace.tools import lttng
2019-06-17 14:33:04 +02:00
from tracetools_trace.tools import names
class Trace(Action):
"""
Tracing action for launch.
Sets up and enables tracing through a launch file description.
"""
2019-06-17 14:33:04 +02:00
def __init__(
2019-06-23 14:33:56 +02:00
self,
*,
session_name: str,
2019-06-17 14:33:04 +02:00
base_path: str = '/tmp',
events_ust: List[str] = names.DEFAULT_EVENTS_ROS,
events_kernel: List[str] = names.DEFAULT_EVENTS_KERNEL,
2019-06-23 14:33:56 +02:00
**kwargs,
2019-06-17 16:42:49 +02:00
) -> None:
"""Constructor."""
super().__init__(**kwargs)
2019-06-17 14:33:04 +02:00
self.__session_name = session_name
self.__path = os.path.join(base_path, session_name)
self.__events_ust = events_ust
self.__events_kernel = events_kernel
def execute(self, context: LaunchContext) -> Optional[List[Action]]:
2019-06-17 14:33:04 +02:00
# TODO make sure this is done as late as possible
2019-06-23 14:33:56 +02:00
context.register_event_handler(OnShutdown(on_shutdown=self._destroy))
2019-06-17 14:33:04 +02:00
# TODO make sure this is done as early as possible
self._setup()
2019-06-23 14:24:30 +02:00
def _setup(self) -> None:
2019-06-17 14:33:04 +02:00
print('setting up tracing!')
2019-06-17 14:55:06 +02:00
lttng.lttng_setup(
self.__session_name,
self.__path,
ros_events=self.__events_ust,
kernel_events=self.__events_kernel)
lttng.lttng_start(self.__session_name)
2019-06-17 14:33:04 +02:00
2019-06-23 14:24:30 +02:00
def _destroy(self, event: Event, context: LaunchContext) -> None:
2019-06-17 14:33:04 +02:00
print('destroying tracing session!')
2019-06-17 14:55:06 +02:00
lttng.lttng_stop(self.__session_name)
lttng.lttng_destroy(self.__session_name)
2019-06-23 14:21:01 +02:00
def __repr__(self):
2019-06-23 14:33:56 +02:00
return (
"Trace("
f"session_name='{self.__session_name}', "
f"path='{self.__path}', "
f"num_events_ust={len(self.__events_ust)}, "
2019-06-23 14:21:01 +02:00
f"num_events_kernel={len(self.__events_kernel)})"
2019-06-23 14:33:56 +02:00
)