Skip to content

Commit c2fe5cf

Browse files
committed
add useful prefix helper functions (e.g., for node debugging)
1 parent 0410c2d commit c2fe5cf

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
# General information about the project.
6565
project = u'roslaunch2'
6666
# noinspection PyShadowingBuiltins
67-
copyright = u'2017, Adrian Böckenkamp'
67+
copyright = u'2019, Adrian Böckenkamp'
6868
author = u'Adrian Böckenkamp'
6969

7070
# The version info for the project you're documenting, acts as replacement for

doc/roslaunch2.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ roslaunch2.remote module
108108
:undoc-members:
109109
:show-inheritance:
110110

111+
roslaunch2.helpers module
112+
-------------------------
113+
114+
.. automodule:: roslaunch2.helpers
115+
:members:
116+
:undoc-members:
117+
:show-inheritance:
118+
111119

112120
Module contents
113121
---------------

src/roslaunch2/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Author: Adrian Böckenkamp
55
# License: BSD (https://opensource.org/licenses/BSD-3-Clause)
6-
# Date: 13/03/2018
6+
# Date: 12/11/2019
77

88
# Import all submodules typically used in launch modules:
99
from group import *
@@ -17,6 +17,7 @@
1717
from node import *
1818
from environment import *
1919
from test import *
20+
from helpers import *
2021

2122
import argparse
2223

src/roslaunch2/helpers.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)