Implement clock override plugin support
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 1 Oct 2014 21:59:01 +0000 (17:59 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 10 Apr 2015 20:44:17 +0000 (16:44 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
doc/examples/Makefile.am
doc/examples/clock-override/Makefile [new file with mode: 0644]
doc/examples/clock-override/README [new file with mode: 0644]
doc/examples/clock-override/lttng-ust-clock-override-example.c [new file with mode: 0644]
doc/examples/clock-override/run-clock-override [new file with mode: 0755]
include/Makefile.am
liblttng-ust-ctl/ustctl.c
liblttng-ust/Makefile.am
liblttng-ust/clock.h
liblttng-ust/lttng-ust-comm.c
liblttng-ust/lttng-ust-uuid.h

index 6f0a31aa37d777498aa516d17ba836d35b4950b6..328c6b0f0432c6c4139b1fc951169e198887298b 100644 (file)
@@ -4,6 +4,7 @@ doc_examples_gen_tpdir = ${docdir}/examples/gen-tp
 doc_examples_demodir = ${docdir}/examples/demo
 doc_examples_hello_static_libdir = ${docdir}/examples/hello-static-lib
 doc_examples_demo_tracefdir = ${docdir}/examples/demo-tracef
+doc_examples_clock_overridedir = ${docdir}/examples/clock-override
 
 if BUILD_JAVA_AGENT
 doc_examples_java_juldir = ${docdir}/examples/java-jul
@@ -43,12 +44,17 @@ dist_doc_examples_demo_tracef_DATA = demo-tracef/Makefile \
        demo-tracef/demo-tracef.c \
        demo-tracef/README
 
+dist_doc_examples_clock_override_DATA = clock-override/Makefile \
+       clock-override/lttng-ust-clock-override-example.c \
+       clock-override/run-clock-override \
+       clock-override/README
+
 if NO_SHARED
 # Don't build examples if shared libraries support was explicitly
 # disabled.
 else
 # Copies are for VPATH build support
-SUBDIRS_PROXY = easy-ust demo hello-static-lib demo-tracef
+SUBDIRS_PROXY = easy-ust demo hello-static-lib demo-tracef clock-override
 
 if BUILD_GEN_TP_EXAMPLES
 SUBDIRS_PROXY += gen-tp
diff --git a/doc/examples/clock-override/Makefile b/doc/examples/clock-override/Makefile
new file mode 100644 (file)
index 0000000..8f38c4f
--- /dev/null
@@ -0,0 +1,38 @@
+# Copyright (C) 2013  Jérémie Galarneau <jeremie.galarneau@efficios.com>
+# Copyright (C) 2014  Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+#
+# 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.
+
+ifdef AM_CC
+       CC = $(AM_CC)
+endif
+
+LIBS = -ldl    # On Linux
+#LIBS = -lc    # On BSD
+LOCAL_CPPFLAGS += -I.
+
+all: lttng-ust-clock-override-example.so
+
+lttng-ust-clock-override-example.o: lttng-ust-clock-override-example.c
+       $(CC) $(CPPFLAGS) $(LOCAL_CPPFLAGS) $(CFLAGS) $(AM_CPPFLAGS) \
+               $(AM_CFLAGS) -fpic -c -o $@ $<
+
+lttng-ust-clock-override-example.so: lttng-ust-clock-override-example.o
+       $(CC) -shared -Wl,--no-as-needed -o $@ $(LDFLAGS) $(CFLAGS) \
+               $(AM_LDFLAGS) $(AM_CFLAGS) lttng-ust-clock-override-example.o
+
+.PHONY: clean
+clean:
+       rm -f *.o *.so
diff --git a/doc/examples/clock-override/README b/doc/examples/clock-override/README
new file mode 100644 (file)
index 0000000..e3ddf22
--- /dev/null
@@ -0,0 +1,4 @@
+This clock override example shows how to implement and load a clock
+override plugin for LTTng-UST. This can be useful in cases where direct
+hardware access is available for architecture-specific clocks, and where
+it should be used rather than the Linux kernel Monotonic clock.
diff --git a/doc/examples/clock-override/lttng-ust-clock-override-example.c b/doc/examples/clock-override/lttng-ust-clock-override-example.c
new file mode 100644 (file)
index 0000000..766e66c
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * lttng-clock-override-example.c
+ *
+ * Copyright (c) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ *
+ * 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 <stdlib.h>
+#include <time.h>
+#include <string.h>
+#include <stdio.h>
+#include <lttng/ust-clock.h>
+
+static
+uint64_t plugin_read64(void)
+{
+       struct timespec ts;
+
+       clock_gettime(CLOCK_MONOTONIC, &ts);
+       /*
+        * This is a rather dumb example, but it illustrates the plugin
+        * mechanism: we take the monotonic clock, and transform it into
+        * a very coarse clock, which increment only at 1KHz frequency.
+        */
+       return ((uint64_t) ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL);
+}
+
+static
+uint64_t plugin_freq(void)
+{
+       return 1000;    /* 1KHz clock (very coarse!) */
+}
+
+static
+int plugin_uuid(char *uuid)
+{
+       const char myuuid[] = "123456789012345678901234567890123456";
+
+       /*
+        * Should read some unique identifier for this clock shared
+        * across all components of the system using this clock for
+        * tracing.
+        */
+       memcpy(uuid, myuuid, LTTNG_UST_UUID_STR_LEN);
+       return 0;
+}
+
+static
+const char *plugin_name(void)
+{
+       return "my_example_clock";
+}
+
+static
+const char *plugin_description(void)
+{
+       return "Coarse monotonic clock at 1KHz";
+}
+
+void lttng_ust_clock_plugin_init(void)
+{
+       int ret;
+
+       ret = lttng_ust_trace_clock_set_read64_cb(plugin_read64);
+       if (ret) {
+               fprintf(stderr, "Error setting clock override read64 callback: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+       ret = lttng_ust_trace_clock_set_freq_cb(plugin_freq);
+       if (ret) {
+               fprintf(stderr, "Error setting clock override freq callback: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+       ret = lttng_ust_trace_clock_set_uuid_cb(plugin_uuid);
+       if (ret) {
+               fprintf(stderr, "Error setting clock override uuid callback: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+
+       ret = lttng_ust_trace_clock_set_name_cb(plugin_name);
+       if (ret) {
+               fprintf(stderr, "Error setting clock override name callback: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+
+       ret = lttng_ust_trace_clock_set_description_cb(plugin_description);
+       if (ret) {
+               fprintf(stderr, "Error setting clock override description callback: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+
+       ret = lttng_ust_enable_trace_clock_override();
+       if (ret) {
+               fprintf(stderr, "Error enabling clock override: %s\n",
+                       strerror(-ret));
+               goto error;
+       }
+
+       return;
+
+error:
+       exit(EXIT_FAILURE);
+}
diff --git a/doc/examples/clock-override/run-clock-override b/doc/examples/clock-override/run-clock-override
new file mode 100755 (executable)
index 0000000..295b483
--- /dev/null
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# launch with: run-clock-override progname args
+
+DIR=$(dirname $0)
+DIR=$(readlink -f $DIR)
+
+LTTNG_UST_CLOCK_PLUGIN="$DIR/lttng-ust-clock-override-example.so" ${*}
index 836cc9f3e4cf8de4c5d97317e97eb5c13c7385f7..b3598ffc859b24fc1c377baed07e7e9d142eb9d9 100644 (file)
@@ -20,7 +20,8 @@ nobase_include_HEADERS = \
        lttng/bug.h \
        lttng/ust-error.h \
        lttng/tracef.h \
-       lttng/lttng-ust-tracef.h
+       lttng/lttng-ust-tracef.h \
+       lttng/ust-clock.h
 
 # note: usterr-signal-safe.h, core.h and share.h need namespace cleanup.
 
index ca17255d914e9b65346685e4e1988382d5e18d0b..5801bc6b4b78e6b8c6b837d44755c79f1264f348 100644 (file)
@@ -32,6 +32,7 @@
 #include "../libringbuffer/frontend.h"
 #include "../liblttng-ust/wait.h"
 #include "../liblttng-ust/lttng-rb-clients.h"
+#include "../liblttng-ust/clock.h"
 
 /*
  * Number of milliseconds to retry before failing metadata writes on
@@ -2003,6 +2004,7 @@ static __attribute__((constructor))
 void ustctl_init(void)
 {
        init_usterr();
+       lttng_ust_clock_init();
        lttng_ring_buffer_metadata_client_init();
        lttng_ring_buffer_client_overwrite_init();
        lttng_ring_buffer_client_overwrite_rt_init();
index e2e1baaab9b8a8e7e5c049948aaf1b8a7eaa0e46..796ce5b3837fe939cb3599d50275d8eadb195a7e 100644 (file)
@@ -64,7 +64,8 @@ liblttng_ust_support_la_SOURCES = \
        lttng-ring-buffer-client-overwrite.c \
        lttng-ring-buffer-client-overwrite-rt.c \
        lttng-ring-buffer-metadata-client.h \
-       lttng-ring-buffer-metadata-client.c
+       lttng-ring-buffer-metadata-client.c \
+       lttng-clock.c
 
 liblttng_ust_la_SOURCES =
 
index 88eca432a5733f7e4bc35f1878b911617a5119d5..388ed658cbb9761a3c206c890ed620a396d3f92b 100644 (file)
 #include <stdint.h>
 #include <stddef.h>
 #include <stdio.h>
+#include <urcu/system.h>
+#include <urcu/arch.h>
+#include <lttng/ust-clock.h>
+
 #include "lttng-ust-uuid.h"
 
-/* TRACE CLOCK */
+struct lttng_trace_clock {
+       uint64_t (*read64)(void);
+       uint64_t (*freq)(void);
+       int (*uuid)(char *uuid);
+       const char *(*name)(void);
+       const char *(*description)(void);
+};
 
-/*
- * Currently using the kernel MONOTONIC clock, waiting for kernel-side
- * LTTng to implement mmap'd trace clock.
- */
+extern struct lttng_trace_clock *lttng_trace_clock;
 
-/* Choosing correct trace clock */
+void lttng_ust_clock_init(void);
+
+/* Use the kernel MONOTONIC clock. */
 
 static __inline__
-uint64_t trace_clock_read64(void)
+uint64_t trace_clock_read64_monotonic(void)
 {
        struct timespec ts;
 
@@ -46,13 +55,13 @@ uint64_t trace_clock_read64(void)
 }
 
 static __inline__
-uint64_t trace_clock_freq(void)
+uint64_t trace_clock_freq_monotonic(void)
 {
        return 1000000000ULL;
 }
 
 static __inline__
-int trace_clock_uuid(char *uuid)
+int trace_clock_uuid_monotonic(char *uuid)
 {
        int ret = 0;
        size_t len;
@@ -78,4 +87,82 @@ end:
        return ret;
 }
 
+static __inline__
+const char *trace_clock_name_monotonic(void)
+{
+       return "monotonic";
+}
+
+static __inline__
+const char *trace_clock_description_monotonic(void)
+{
+       return "Monotonic Clock";
+}
+
+static __inline__
+uint64_t trace_clock_read64(void)
+{
+       struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
+
+       if (caa_likely(!ltc)) {
+               return trace_clock_read64_monotonic();
+       } else {
+               cmm_read_barrier_depends();     /* load ltc before content */
+               return ltc->read64();
+       }
+}
+
+static __inline__
+uint64_t trace_clock_freq(void)
+{
+       struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
+
+       if (!ltc) {
+               return trace_clock_freq_monotonic();
+       } else {
+               cmm_read_barrier_depends();     /* load ltc before content */
+               return ltc->freq();
+       }
+}
+
+static __inline__
+int trace_clock_uuid(char *uuid)
+{
+       struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
+
+       cmm_read_barrier_depends();     /* load ltc before content */
+       /* Use default UUID cb when NULL */
+       if (!ltc || !ltc->uuid) {
+               return trace_clock_uuid_monotonic(uuid);
+       } else {
+               return ltc->uuid(uuid);
+       }
+}
+
+static __inline__
+const char *trace_clock_name(void)
+{
+       struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
+
+       if (!ltc) {
+               return trace_clock_name_monotonic();
+       } else {
+               cmm_read_barrier_depends();     /* load ltc before content */
+               return ltc->name();
+       }
+}
+
+static __inline__
+const char *trace_clock_description(void)
+{
+       struct lttng_trace_clock *ltc = CMM_LOAD_SHARED(lttng_trace_clock);
+
+       if (!ltc) {
+               return trace_clock_description_monotonic();
+       } else {
+               cmm_read_barrier_depends();     /* load ltc before content */
+               return ltc->description();
+       }
+}
+
 #endif /* _UST_CLOCK_H */
index 378ca21cfc71f354a282fe4a9a521cf77319ef02..97ecdf1e99001bac3ddda7b7476d2e5a08b6c566 100644 (file)
@@ -52,6 +52,7 @@
 #include "compat.h"
 #include "../libringbuffer/tlsfixup.h"
 #include "lttng-ust-baddr.h"
+#include "clock.h"
 
 /*
  * Has lttng ust comm constructor been called ?
@@ -1444,6 +1445,7 @@ void __attribute__((constructor)) lttng_ust_init(void)
         */
        init_usterr();
        init_tracepoint();
+       lttng_ust_clock_init();
        lttng_ust_baddr_statedump_init();
        lttng_ring_buffer_metadata_client_init();
        lttng_ring_buffer_client_overwrite_init();
index add11e1f842fca5f84e6ee33bb510144c3bccfa0..3b05d178e02a7f2bd1b0fff9754cf06c92932261 100644 (file)
  */
 
 #include <lttng/ust-events.h> /* For LTTNG_UST_UUID_LEN */
-
-/*
- * Includes final \0.
- */
-#define LTTNG_UST_UUID_STR_LEN         37
+#include <lttng/ust-clock.h>
 
 #endif /* _LTTNG_UST_UUID_H */
This page took 0.031658 seconds and 4 git commands to generate.