From f449107563a7ab6444d9002c273c66766eb304d9 Mon Sep 17 00:00:00 2001 From: Julien BERAUD Date: Wed, 13 Jan 2016 18:40:35 +0100 Subject: [PATCH] AP_HAL_Linux: Add Perf Lttng Support for perf api using lttng. Some additional build tricks needed for bebop because lttng uses dl_open which is not compatible with a static link on a different libc as used on the bebop --- .../AP_HAL_Linux/AP_HAL_Linux_Namespace.h | 1 + libraries/AP_HAL_Linux/Perf.cpp | 2 +- libraries/AP_HAL_Linux/Perf_Lttng.cpp | 87 +++++++++++++++++++ libraries/AP_HAL_Linux/Perf_Lttng.h | 32 +++++++ .../AP_HAL_Linux/Perf_Lttng_TracePoints.h | 65 ++++++++++++++ mk/board_native.mk | 8 ++ 6 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 libraries/AP_HAL_Linux/Perf_Lttng.cpp create mode 100644 libraries/AP_HAL_Linux/Perf_Lttng.h create mode 100644 libraries/AP_HAL_Linux/Perf_Lttng_TracePoints.h diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h index aa34099a0a..623e734e52 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Namespace.h @@ -51,6 +51,7 @@ namespace Linux { class VideoIn; class OpticalFlow_Onboard; class Flow_PX4; + class Perf_Lttng; } #endif // __AP_HAL_LINUX_NAMESPACE_H__ diff --git a/libraries/AP_HAL_Linux/Perf.cpp b/libraries/AP_HAL_Linux/Perf.cpp index 0239655a88..6732843ac9 100644 --- a/libraries/AP_HAL_Linux/Perf.cpp +++ b/libraries/AP_HAL_Linux/Perf.cpp @@ -18,7 +18,7 @@ #include -#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX +#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX && !defined(PERF_LTTNG) #include #include diff --git a/libraries/AP_HAL_Linux/Perf_Lttng.cpp b/libraries/AP_HAL_Linux/Perf_Lttng.cpp new file mode 100644 index 0000000000..b537a82191 --- /dev/null +++ b/libraries/AP_HAL_Linux/Perf_Lttng.cpp @@ -0,0 +1,87 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include + +#pragma GCC diagnostic ignored "-Wcast-align" +#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX && defined(PERF_LTTNG) +#define TRACEPOINT_CREATE_PROBES +#define TRACEPOINT_DEFINE +#include "Perf_Lttng_TracePoints.h" + +#include +#include "Perf_Lttng.h" +#include "AP_HAL_Linux.h" +#include "Util.h" + +using namespace Linux; + +Perf_Lttng::Perf_Lttng(AP_HAL::Util::perf_counter_type type, const char *name) + : _type(type) +{ + strncpy(_name, name, MAX_TRACEPOINT_NAME_LEN); +} + +void Perf_Lttng::begin() +{ + if (_type != AP_HAL::Util::PC_ELAPSED) { + return; + } + tracepoint(ardupilot, begin, _name); +} + +void Perf_Lttng::end() +{ + if (_type != AP_HAL::Util::PC_ELAPSED) { + return; + } + tracepoint(ardupilot, end, _name); +} + +void Perf_Lttng::count() +{ + if (_type != AP_HAL::Util::PC_COUNT) { + return; + } + tracepoint(ardupilot, count, _name, ++_count); +} + +Util::perf_counter_t Util::perf_alloc(perf_counter_type type, const char *name) +{ + return new Linux::Perf_Lttng(type, name); +} + +void Util::perf_begin(perf_counter_t perf) +{ + Linux::Perf_Lttng *perf_lttng = (Linux::Perf_Lttng *)perf; + + perf_lttng->begin(); +} + +void Util::perf_end(perf_counter_t perf) +{ + Linux::Perf_Lttng *perf_lttng = (Linux::Perf_Lttng *)perf; + + perf_lttng->end(); +} + +void Util::perf_count(perf_counter_t perf) +{ + Linux::Perf_Lttng *perf_lttng = (Linux::Perf_Lttng *)perf; + + perf_lttng->count(); +} + +#endif diff --git a/libraries/AP_HAL_Linux/Perf_Lttng.h b/libraries/AP_HAL_Linux/Perf_Lttng.h new file mode 100644 index 0000000000..a043f1c8ca --- /dev/null +++ b/libraries/AP_HAL_Linux/Perf_Lttng.h @@ -0,0 +1,32 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#pragma once +#include "AP_HAL_Linux.h" +#include + +#define MAX_TRACEPOINT_NAME_LEN 128 + +class Linux::Perf_Lttng { +public: + Perf_Lttng(enum AP_HAL::Util::perf_counter_type type, const char *name); + void begin(); + void end(); + void count(); +private: + char _name[MAX_TRACEPOINT_NAME_LEN]; + uint64_t _count; + enum AP_HAL::Util::perf_counter_type _type; +}; diff --git a/libraries/AP_HAL_Linux/Perf_Lttng_TracePoints.h b/libraries/AP_HAL_Linux/Perf_Lttng_TracePoints.h new file mode 100644 index 0000000000..e8ad271221 --- /dev/null +++ b/libraries/AP_HAL_Linux/Perf_Lttng_TracePoints.h @@ -0,0 +1,65 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER ardupilot + +#undef TRACEPOINT_INCLUDE +#define TRACEPOINT_INCLUDE + +#if !defined(_PERF_LTTNG_TRACEPOINT_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _PERF_LTTNG_TRACEPOINT_H + +#include + +TRACEPOINT_EVENT( + ardupilot, + begin, + TP_ARGS( + char*, name_arg + ), + TP_FIELDS( + ctf_string(name_field, name_arg) + ) +) + +TRACEPOINT_EVENT( + ardupilot, + end, + TP_ARGS( + char*, name_arg + ), + TP_FIELDS( + ctf_string(name_field, name_arg) + ) +) + +TRACEPOINT_EVENT( + ardupilot, + count, + TP_ARGS( + char*, name_arg, + int, count_arg + ), + TP_FIELDS( + ctf_string(name_field, name_arg) + ctf_integer(int, count_field, count_arg) + ) +) + +#endif /* _HELLO_TP_H */ + +#include + diff --git a/mk/board_native.mk b/mk/board_native.mk index cc9a783f0a..a686ebff1b 100644 --- a/mk/board_native.mk +++ b/mk/board_native.mk @@ -26,6 +26,14 @@ COPTS = -ffunction-sections -fdata-sections -fsigned-char ASOPTS = -x assembler-with-cpp +# features: TODO detect dependecy and make them optional +HAVE_LTTNG= + +ifeq ($(HAVE_LTTNG),1) +DEFINES += -DPERF_LTTNG=1 +LIBS += -llttng-ust -ldl +endif + # disable as this breaks distcc #ifneq ($(SYSTYPE),Darwin) #LISTOPTS = -adhlns=$(@:.o=.lst)