Just to speed things up, please have a look at the proposed UST patch. (it's not in mainline yet)
This is specifically for a demo program example so far. Comments are welcome, Thanks, Mathieu ----- Original Message ----- > From: "Máté Ferenczy" <[email protected]> > To: "Mathieu Desnoyers" <[email protected]> > Cc: [email protected] > Sent: Wednesday, December 11, 2013 8:52:30 PM > Subject: RE: efficient use of LTTng in legacy codebase > It sounds great, I like the idea. Just to make sure I understand your > suggestion, are we talking about converting the variable argument list to > string in include/lttng/ust-tracepoint-event-write.h? > For example: > #define ctf_format_string(_item, …) \ > do { \ > char _buf[BUF_MAXLEN]; \ > snprintf(buf, sizeof(_buf), __VA_ARGS__); \ > _ctf_string(_item, _buf, 0); \ > } while(0) > Or do you have some better place to put this logic? > Thanks, > Mate > From: Mathieu Desnoyers [mailto:[email protected]] > Sent: Wednesday, December 11, 2013 4:48 PM > To: Máté Ferenczy > Cc: [email protected] > Subject: Re: efficient use of LTTng in legacy codebase > ----- Original Message ----- > > From: "Máté Ferenczy" < [email protected] > > > > To: "mathieu desnoyers" < [email protected] > > > > Cc: [email protected] > > > Sent: Wednesday, December 11, 2013 6:52:22 PM > > > Subject: RE: efficient use of LTTng in legacy codebase > > > I found this email thread: > > https://www.mail-archive.com/[email protected]/msg02278.html > > > According to this I could just check > > caa_unlikely(__tracepoint_##provider##___##name.state). However, Dave > > claims > > that this state will stay true even if the tracepoint is disabled. Is there > > anything else I should check for? > > I would recommend doing this in the UST probe provider instead. Within the > TRACEPOINT_EVENT "TP_FIELD" section, when you declare your string field, you > could parse the format string there in the last argument of a ctf_string() > (a statement expression). The format string would be received as parameter > by the tracepoint, along with a variable arg list (...). In the probe, it > would be turned into a va_list (see stdarg(3)), and the parameters could be > decoded into a string there. This has the benefit of moving the > serialization call out-of-line into the probe provider rather than > clobbering the tracepoint call site. > Thoughts ? > Thanks, > Mathieu > > Thanks, > > > Mate > > > From: Máté Ferenczy > > > Sent: Wednesday, December 11, 2013 12:17 PM > > > To: '[email protected]' > > > Cc: '[email protected]' > > > Subject: efficient use of LTTng in legacy codebase > > > Hello Mathieu, > > > Our team was asked to support LTTng UST solution in a legacy codebase. The > > existing codebase is fairly large, and the requirement is that we provide > > all the existing logs and debugs in the system as LTTng traces. > > > The planned solution so far was just to pass everything we have as text > > (strings) to LTTng. For that we obviously have to convert the printf-like > > format string and parameter list to a string by calling snprintf before > > giving the string to the LTTng API. That would however mean, that these > > snprintfs are getting called every time even if there is no listener/active > > trace session for the given trace. In this case, all those unnecessary > > snprintf calls may end up significantly impacting the performance of our > > applications. > > > In order to work around this, we were thinking that if there was a way to > > query the LTTng library from our application code whether there is an > > active > > tracing session for the given trace level, we could avoid calling those > > snprintfs in case they are not needed. Do you think it would be feasible to > > easily get this data from LTTng lib? If yes, can you provide us some > > pointers where to look at in order to do those changes ourselves? > > > Any suggestions are welcome. > > > Thank you, > > > Mate Ferenczy > > -- > Mathieu Desnoyers > EfficiOS Inc. > http://www.efficios.com -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
commit 2e589b6235396d29832ad9ff52ecc35a98df349f Author: Mathieu Desnoyers <[email protected]> Date: Thu Dec 12 07:29:26 2013 -0500 Add trace format demo Signed-off-by: Mathieu Desnoyers <[email protected]> diff --git a/.gitignore b/.gitignore index ce7c572..ece9f5b 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ doc/examples/easy-ust/sample doc/examples/hello-static-lib/hello doc/examples/gen-tp/sample doc/examples/gen-tp/sample_tracepoint.h +doc/examples/demo-fmt-string/demo-fmt-string tests/hello/hello tests/hello.cxx/hello diff --git a/doc/examples/Makefile.am b/doc/examples/Makefile.am index 725806d..917976d 100644 --- a/doc/examples/Makefile.am +++ b/doc/examples/Makefile.am @@ -44,7 +44,7 @@ if NO_SHARED # disabled. else # Copies are for VPATH build support -SUBDIRS_PROXY = easy-ust demo gen-tp hello-static-lib +SUBDIRS_PROXY = easy-ust demo gen-tp hello-static-lib demo-fmt-string all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ diff --git a/doc/examples/demo-fmt-string/Makefile b/doc/examples/demo-fmt-string/Makefile new file mode 100644 index 0000000..a3060d3 --- /dev/null +++ b/doc/examples/demo-fmt-string/Makefile @@ -0,0 +1,48 @@ +# Copyright (C) 2013 Jérémie Galarneau <[email protected]> +# +# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED +# OR IMPLIED. ANY USE IS AT YOUR OWN RISK. +# +# Permission is hereby granted to use or copy this program for any +# purpose, provided the above notices are retained on all copies. +# Permission to modify the code and to distribute modified code is +# granted, provided the above notices are retained, and a notice that +# the code was modified is included with the above copyright notice. +# +# This Makefile is not using automake so that users may see how to build +# a program with tracepoint provider probes as stand-alone shared objects. +# +# This makefile is purposefully kept simple to support GNU and BSD make. + +CC = gcc +LIBS = -ldl -llttng-ust # On Linux +#LIBS = -lc # On BSD +LOCAL_CPPFLAGS += -I. + +all: demo-fmt-string + +tp.o: tp.c ust_tests_demo_fmt_string.h trace-fmt.h + $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(CFLAGS) $(AM_CPPFLAGS) \ + $(AM_CFLAGS) -fpic -c -o $@ $< + +trace-fmt.o: trace-fmt.c ust_tests_demo_fmt_string.h trace-fmt.h + $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(CFLAGS) $(AM_CPPFLAGS) \ + $(AM_CFLAGS) -fpic -c -o $@ $< + + +lttng-ust-provider-ust-tests-demo-fmt-string.a: tp.o trace-fmt.o + ar -rc $@ $+ + +demo-fmt-string.o: demo-fmt-string.c trace-fmt.h + $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(CFLAGS) $(AM_CPPFLAGS) \ + $(AM_CFLAGS) -c -o $@ $< + +demo-fmt-string: demo-fmt-string.o \ + lttng-ust-provider-ust-tests-demo-fmt-string.a + $(CC) $(LDFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(AM_CFLAGS) \ + -o $@ demo-fmt-string.o \ + lttng-ust-provider-ust-tests-demo-fmt-string.a $(LIBS) + +.PHONY: clean +clean: + rm -f *.o *.a demo-fmt-string diff --git a/doc/examples/demo-fmt-string/README b/doc/examples/demo-fmt-string/README new file mode 100644 index 0000000..e069e73 --- /dev/null +++ b/doc/examples/demo-fmt-string/README @@ -0,0 +1,20 @@ +This is a demo application showing how to trace formatted strings into +LTTng-UST. + +The simplest command to trace the demo program are: + +lttng create +lttng enable-event -u "ust_tests_demo_fmt_string:event" +lttng start +./demo-fmt-string +lttng stop +lttng view +lttng destroy + +The resulting lttng view output should look like this: + +[07:32:02.021045683] (+?.?????????) thinkos ust_tests_demo_fmt_string:event: { cpu_id = 2 }, { _msg_length = 46, msg = "This is a "mystring test" formatted 0 event 42" } +[07:32:02.021062328] (+0.000016645) thinkos ust_tests_demo_fmt_string:event: { cpu_id = 2 }, { _msg_length = 46, msg = "This is a "mystring test" formatted 1 event 42" } +[07:32:02.021066300] (+0.000003972) thinkos ust_tests_demo_fmt_string:event: { cpu_id = 2 }, { _msg_length = 46, msg = "This is a "mystring test" formatted 2 event 42" } +[07:32:02.021069507] (+0.000003207) thinkos ust_tests_demo_fmt_string:event: { cpu_id = 2 }, { _msg_length = 46, msg = "This is a "mystring test" formatted 3 event 42" } +[07:32:02.021072541] (+0.000003034) thinkos ust_tests_demo_fmt_string:event: { cpu_id = 2 }, { _msg_length = 46, msg = "This is a "mystring test" formatted 4 event 42" } diff --git a/doc/examples/demo-fmt-string/demo-fmt-string.c b/doc/examples/demo-fmt-string/demo-fmt-string.c new file mode 100644 index 0000000..7be4ec4 --- /dev/null +++ b/doc/examples/demo-fmt-string/demo-fmt-string.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2009 Pierre-Marc Fournier + * Copyright (C) 2011 Mathieu Desnoyers <[email protected]> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 of + * the License. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <stdio.h> +#include <unistd.h> +#include <sys/mman.h> +#include <stdarg.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <signal.h> +#include <string.h> +#include <arpa/inet.h> +#include <stdlib.h> + +#include "trace-fmt.h" + +int main(int argc, char **argv) +{ + int i; + int delay = 0; + const char *str = "mystring test"; + long l = 0x42; + + if (argc == 2) + delay = atoi(argv[1]); + + fprintf(stderr, "Demo program starting.\n"); + + sleep(delay); + + fprintf(stderr, "Tracing... "); + for (i = 0; i < 5; i++) { + ust_tests_demo_trace_fmt( + "This is a \"%s\" formatted %d event %lx", + str, i, l); + } + fprintf(stderr, " done.\n"); + return 0; +} diff --git a/doc/examples/demo-fmt-string/tp.c b/doc/examples/demo-fmt-string/tp.c new file mode 100644 index 0000000..47283ad --- /dev/null +++ b/doc/examples/demo-fmt-string/tp.c @@ -0,0 +1,26 @@ +/* + * tp.c + * + * Copyright (c) 2011 Mathieu Desnoyers <[email protected]> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define TRACEPOINT_CREATE_PROBES +#include "ust_tests_demo_fmt_string.h" diff --git a/doc/examples/demo-fmt-string/trace-fmt.c b/doc/examples/demo-fmt-string/trace-fmt.c new file mode 100644 index 0000000..1f59be4 --- /dev/null +++ b/doc/examples/demo-fmt-string/trace-fmt.c @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2013 Mathieu Desnoyers <[email protected]> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + + +#define _GNU_SOURCE +#include <stdio.h> + +#define TRACEPOINT_DEFINE +#include "trace-fmt.h" + +void _ust_tests_demo_trace_fmt(const char *fmt, ...) +{ + va_list ap; + char *msg; + int len; + + va_start(ap, fmt); + len = vasprintf(&msg, fmt, ap); + /* len does not include the final \0 */ + if (len < 0) + goto end; + __tracepoint_cb_ust_tests_demo_fmt_string___event(msg, len); + free(msg); +end: + va_end(ap); +} + + diff --git a/doc/examples/demo-fmt-string/trace-fmt.h b/doc/examples/demo-fmt-string/trace-fmt.h new file mode 100644 index 0000000..73ef58d --- /dev/null +++ b/doc/examples/demo-fmt-string/trace-fmt.h @@ -0,0 +1,37 @@ +#ifndef _LTTNG_UST_DEMO_TRACE_FMT_H +#define _LTTNG_UST_DEMO_TRACE_FMT_H + +/* + * Copyright (C) 2013 Mathieu Desnoyers <[email protected]> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ust_tests_demo_fmt_string.h" + +void _ust_tests_demo_trace_fmt(const char *fmt, ...); + +#define ust_tests_demo_trace_fmt(fmt, ...) \ + do { \ + STAP_PROBEV(tracepoint_ust_tests_demo_fmt_string, event, ## __VA_ARGS__); \ + if (caa_unlikely(__tracepoint_ust_tests_demo_fmt_string___event.state)) \ + _ust_tests_demo_trace_fmt(fmt, ## __VA_ARGS__); \ + } while (0) + +#endif /* _LTTNG_UST_DEMO_TRACE_FMT_H */ diff --git a/doc/examples/demo-fmt-string/ust_tests_demo_fmt_string.h b/doc/examples/demo-fmt-string/ust_tests_demo_fmt_string.h new file mode 100644 index 0000000..c1cff6b --- /dev/null +++ b/doc/examples/demo-fmt-string/ust_tests_demo_fmt_string.h @@ -0,0 +1,46 @@ +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER ust_tests_demo_fmt_string + +#if !defined(_TRACEPOINT_UST_TESTS_DEMO_FMT_STRING_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _TRACEPOINT_UST_TESTS_DEMO_FMT_STRING_H + +/* + * Copyright (C) 2011 Mathieu Desnoyers <[email protected]> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include <lttng/tracepoint.h> +#include <stdarg.h> + +TRACEPOINT_EVENT(ust_tests_demo_fmt_string, event, + TP_ARGS(const char *, msg, unsigned int, len), + TP_FIELDS( + ctf_sequence_text(char, msg, msg, unsigned int, len) + ) +) +TRACEPOINT_LOGLEVEL(ust_tests_demo_fmt_string, event, TRACE_DEBUG) + +#endif /* _TRACEPOINT_UST_TESTS_DEMO_FMT_STRING_H */ + +#undef TRACEPOINT_INCLUDE +#define TRACEPOINT_INCLUDE "./ust_tests_demo_fmt_string.h" + +/* This part must be outside ifdef protection */ +#include <lttng/tracepoint-event.h>
_______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
