|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Author: Adrian Böckenkamp |
| 5 | +# License: BSD (https://opensource.org/licenses/BSD-3-Clause) |
| 6 | +# Date: 12/11/2019 |
| 7 | + |
| 8 | +import logger |
| 9 | +import node |
| 10 | + |
| 11 | + |
| 12 | +class Helpers: |
| 13 | + @staticmethod |
| 14 | + def __warn_if_not_empty(n): |
| 15 | + """ |
| 16 | + Warns if the provided prefix is not empty. |
| 17 | +
|
| 18 | + :param n: The roslaunch2.node.Node instance to be used |
| 19 | + """ |
| 20 | + assert isinstance(n, node.Node) |
| 21 | + if n.prefix is not None: |
| 22 | + logger.warning("Node.prefix is not empty, overwriting existing prefix '{}' (only one can be active at a" |
| 23 | + " time).".format(n.prefix)) |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def enable_callgrind(node): |
| 27 | + """ |
| 28 | + Enable CPU profiling using valgrind's "callgrind" buildin tool. Must be installed using |
| 29 | + :code:`sudo apt install valgrind`. Callgrind is a profiling tool that records the call history among function in |
| 30 | + UNIX process. The profile data is written out to a file named :code:`callgrind.[node_name].[pid]` at program |
| 31 | + termination. The data files generated by Callgrind can be loaded into Kcachegrind (GUI) for browsing the performance results |
| 32 | + using :code:`kcachegrind ~/.ros/callgrind.[node_name].[pid]`. Kcachegrind can be installed using |
| 33 | + :code:`sudo apt install kcachegrind`. |
| 34 | +
|
| 35 | + :param node: The roslaunch2.node.Node instance to be used |
| 36 | + """ |
| 37 | + Helpers.__warn_if_not_empty(node) |
| 38 | + node.prefix = "valgrind --tool=callgrind --callgrind-out-file='callgrind.{}.%p'".format(node.name) |
| 39 | + |
| 40 | + @staticmethod |
| 41 | + def enable_nice(node): |
| 42 | + """ |
| 43 | + Nice your process to lower its CPU usage. |
| 44 | +
|
| 45 | + :param node: The roslaunch2.node.Node instance to be used |
| 46 | + """ |
| 47 | + Helpers.__warn_if_not_empty(node) |
| 48 | + node.prefix = "nice" |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def enable_gdb(node, separate_window=True): |
| 52 | + """ |
| 53 | + Debug your ROS node using gdb (for roscpp, not rospy). You should compile your code in DEBUG mode (e. g., add |
| 54 | + :code:`-g` flag in gcc). Requires :code:`sudo apt install xterm` if :code:`separate_window=True`. |
| 55 | +
|
| 56 | + :param node: The roslaunch2.node.Node instance to be debugged |
| 57 | + :param separate_window: :code:`True` to open the GDB session in a new window (manually type :code:`run` to start |
| 58 | + it), :code:`False` otherwise (will start right away, without having to type anything to start it) |
| 59 | + """ |
| 60 | + Helpers.__warn_if_not_empty(node) |
| 61 | + if separate_window: |
| 62 | + node.prefix = "xterm -e gdb --args" |
| 63 | + else: |
| 64 | + node.prefix = "gdb -ex run --args" |
| 65 | + |
| 66 | + @staticmethod |
| 67 | + def enable_valgrind(node): |
| 68 | + """ |
| 69 | + Check for memory leaks using valgrind. Must be installed using :code:`sudo apt install valgrind`. |
| 70 | +
|
| 71 | + :param node: The roslaunch2.node.Node instance to be analyzed |
| 72 | + """ |
| 73 | + Helpers.__warn_if_not_empty(node) |
| 74 | + node.prefix = "valgrind" |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def enable_separate_window(node): |
| 78 | + """ |
| 79 | + Run the node in a separate window using xterm. All output is just printed in this separate window, making it |
| 80 | + easier to read. Requires :code:`sudo apt install xterm`. |
| 81 | +
|
| 82 | + :param node: The roslaunch2.node.Node instance to be executed |
| 83 | + """ |
| 84 | + Helpers.__warn_if_not_empty(node) |
| 85 | + node.prefix = "xterm -e" |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def enable_pdb(node): |
| 89 | + """ |
| 90 | + Debug your ROS node using pdb (for rospy, not roscpp). You have to manually type :code:`run` to start it. |
| 91 | + Requires :code:`sudo apt install xterm`. |
| 92 | +
|
| 93 | + :param node: The roslaunch2.node.Node instance to be debugged |
| 94 | + """ |
| 95 | + Helpers.__warn_if_not_empty(node) |
| 96 | + node.prefix = "xterm -e python -m pdb" |
0 commit comments