diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/blkin/blkin-lib | |
parent | Initial commit. (diff) | |
download | ceph-upstream/16.2.11+ds.tar.xz ceph-upstream/16.2.11+ds.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/blkin/blkin-lib/CMakeLists.txt | 18 | ||||
-rw-r--r-- | src/blkin/blkin-lib/Makefile | 92 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tests/CMakeLists.txt | 15 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tests/Makefile | 55 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tests/test.c | 200 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tests/test.cc | 204 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tests/test_p.cc | 210 | ||||
-rw-r--r-- | src/blkin/blkin-lib/tp.c | 35 | ||||
-rw-r--r-- | src/blkin/blkin-lib/zipkin_c.c | 356 | ||||
-rw-r--r-- | src/blkin/blkin-lib/zipkin_c.h | 334 | ||||
-rw-r--r-- | src/blkin/blkin-lib/zipkin_trace.h | 130 | ||||
-rw-r--r-- | src/blkin/blkin-lib/ztracer.hpp | 248 |
12 files changed, 1897 insertions, 0 deletions
diff --git a/src/blkin/blkin-lib/CMakeLists.txt b/src/blkin/blkin-lib/CMakeLists.txt new file mode 100644 index 000000000..d2e1d0f73 --- /dev/null +++ b/src/blkin/blkin-lib/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories(.) + +#blkin +set(blkin_srcs + zipkin_c.c + tp.c +) +add_library(blkin ${blkin_srcs}) +set_target_properties(blkin PROPERTIES POSITION_INDEPENDENT_CODE ON) +target_link_libraries(blkin dl ${LTTNG_LIBRARIES}) + +set(blkin_headers + zipkin_c.h + zipkin_trace.h + ztracer.hpp +) + +add_subdirectory(tests) diff --git a/src/blkin/blkin-lib/Makefile b/src/blkin/blkin-lib/Makefile new file mode 100644 index 000000000..52852f449 --- /dev/null +++ b/src/blkin/blkin-lib/Makefile @@ -0,0 +1,92 @@ +# Copyright 2014 Marios Kogias <marioskogias@gmail.com> +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +.PHONY: default clean distclean run run_c run_pp + +MAJOR=0 +MINOR=1 +LIBS= -ldl -llttng-ust +DLIB=libblkin + +LIB_DIR=$(shell pwd) +TEST_DIR=$(shell pwd)/tests +prefix= /usr/local +libdir= $(prefix)/lib +incdir= $(prefix)/include + +H_FILES= zipkin_c.h zipkin_trace.h ztracer.hpp + +default: $(DLIB).so test testpp testppp + +$(DLIB).so: $(DLIB).$(MAJOR).so + ln -sf $< $@ + +$(DLIB).$(MAJOR).so: $(DLIB).$(MAJOR).$(MINOR).so + ln -sf $< $@ + +$(DLIB).$(MAJOR).$(MINOR).so: zipkin_c.o tp.o + g++ -shared -o $@ $^ $(LIBS) + +zipkin_c.o: zipkin_c.c zipkin_c.h zipkin_trace.h + gcc -I. -Wall -fpic -g -c -o $@ $< + +tp.o: tp.c zipkin_trace.h + gcc -I. -fpic -g -c -o $@ $< + +test: $(TEST_DIR)/test.c $(DLIB).so + make -C tests test + +testpp: $(TEST_DIR)/test.cc $(DLIB).so + make -C tests testpp + +testppp: $(TEST_DIR)/test_p.cc $(DLIB).so + make -C tests testppp + +run_c: + make -C tests run_c + +run_pp: + make -C tests run_pp + +run_ppp: + make -C tests run_ppp + +run: run_c run_pp + +install: + install -m 644 $(DLIB).$(MAJOR).$(MINOR).so $(DESTDIR)/$(libdir) + cp -P $(DLIB).$(MAJOR).so $(DESTDIR)/$(libdir) + cp -P $(DLIB).so $(DESTDIR)/$(libdir) + install -m 644 $(H_FILES) $(DESTDIR)/$(incdir) + +clean: + rm -f *.o *.so + make -C tests clean + +distclean: clean + rm -f socket diff --git a/src/blkin/blkin-lib/tests/CMakeLists.txt b/src/blkin/blkin-lib/tests/CMakeLists.txt new file mode 100644 index 000000000..d69cec3da --- /dev/null +++ b/src/blkin/blkin-lib/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +#test +add_executable(testc test.c) +target_link_libraries(testc blkin lttng-ust) +add_test(NAME testc COMMAND $<TARGET_FILE:testc>) + +#testpp +add_executable(testpp test.cc) +set_target_properties(testpp PROPERTIES COMPILE_FLAGS "-std=c++11") +target_link_libraries(testpp blkin lttng-ust pthread) +add_test(NAME testpp COMMAND $<TARGET_FILE:testpp>) + +#testppp +add_executable(testppp test_p.cc) +target_link_libraries(testppp blkin lttng-ust) +add_test(NAME testppp COMMAND $<TARGET_FILE:testppp>) diff --git a/src/blkin/blkin-lib/tests/Makefile b/src/blkin/blkin-lib/tests/Makefile new file mode 100644 index 000000000..bed3fc869 --- /dev/null +++ b/src/blkin/blkin-lib/tests/Makefile @@ -0,0 +1,55 @@ +# Copyright 2014 Marios Kogias <marioskogias@gmail.com> +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or +# without modification, are permitted provided that the following +# conditions are met: +# +# 1. Redistributions of source code must retain the above +# copyright notice, this list of conditions and the following +# disclaimer. +# 2. Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials +# provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS +# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +.PHONY: default clean distclean run run_c run_pp + +LIB_DIR=$(shell pwd)/.. +DLIB=$(LIB_DIR)/libblkin + +test: test.c $(DLIB).so + gcc test.c -o test -g -I$(LIB_DIR) -L$(LIB_DIR) -lblkin + +testpp: test.cc $(DLIB).so + LD_LIBRARY_PATH=$(LIB_DIR) g++ $< -o testpp -std=c++11 -g -I$(LIB_DIR) -L$(LIB_DIR) -lblkin -lpthread + +testppp: test_p.cc $(DLIB).so + LD_LIBRARY_PATH=$(LIB_DIR) g++ $< -o testppp -g -I$(LIB_DIR) -L$(LIB_DIR) -lblkin + +run_c: + LD_LIBRARY_PATH=$(LIB_DIR) ./test + +run_pp: + LD_LIBRARY_PATH=$(LIB_DIR) ./testpp + +run_ppp: + LD_LIBRARY_PATH=$(LIB_DIR) ./testppp + +run: run_c run_pp + +clean: + rm -f *.o *.so test testpp testppp socket diff --git a/src/blkin/blkin-lib/tests/test.c b/src/blkin/blkin-lib/tests/test.c new file mode 100644 index 000000000..2cabd99bf --- /dev/null +++ b/src/blkin/blkin-lib/tests/test.c @@ -0,0 +1,200 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +/* + * In this example we have 2 processes communicating over a unix socket. + * We are going to trace the communication with our library + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include <zipkin_c.h> + +#define SOCK_PATH "/tmp/socket" + +struct message { + char actual_message[20]; + struct blkin_trace_info trace_info; +}; + +void process_a() +{ + int i, r; + printf("I am process A: %d\n", getpid()); + + r = blkin_init(); + if (r < 0) { + fprintf(stderr, "Could not initialize blkin\n"); + exit(1); + } + + /*initialize endpoint*/ + struct blkin_endpoint endp; + blkin_init_endpoint(&endp, "10.0.0.1", 5000, "service a"); + + struct blkin_trace trace; + struct blkin_annotation ant; + struct message msg = {.actual_message = "message"}; + char ack; + + /*create and bind socket*/ + int s, s2, len; + socklen_t t; + struct sockaddr_un local, remote; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + exit(1); + } + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, SOCK_PATH); + unlink(local.sun_path); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(s, (struct sockaddr *)&local, len) == -1) { + perror("bind"); + exit(1); + } + + if (listen(s, 5) == -1) { + perror("listen"); + exit(1); + } + + printf("Waiting for a connection...\n"); + t = sizeof(remote); + if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1) { + perror("accept"); + exit(1); + } + + printf("Connected.\n"); + + for (i=0;i<10;i++) { + + /*create trace*/ + blkin_init_new_trace(&trace, "process a", &endp); + + blkin_init_timestamp_annotation(&ant, "start", &endp); + blkin_record(&trace, &ant); + + /*set trace fields to message*/ + blkin_get_trace_info(&trace, &msg.trace_info); + + /*send*/ + send(s2, &msg, sizeof(struct message), 0); + + /*wait for ack*/ + recv(s2, &ack, 1, 0); + + /*create annotation and log*/ + blkin_init_timestamp_annotation(&ant, "end", &endp); + blkin_record(&trace, &ant); + } + close(s2); +} + +void process_b() +{ + int i, r; + printf("I am process B: %d\n", getpid()); + + r = blkin_init(); + if (r < 0) { + fprintf(stderr, "Could not initialize blkin\n"); + exit(1); + } + /*initialize endpoint*/ + struct blkin_endpoint endp; + blkin_init_endpoint(&endp, "10.0.0.2", 5001, "service b"); + + struct blkin_trace trace; + struct blkin_annotation ant; + struct message msg; + int s, len; + struct sockaddr_un remote; + + /*Connect*/ + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + perror("socket"); + exit(1); + } + + printf("Trying to connect...\n"); + + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, SOCK_PATH); + len = strlen(remote.sun_path) + sizeof(remote.sun_family); + if (connect(s, (struct sockaddr *)&remote, len) == -1) { + perror("connect"); + exit(1); + } + + printf("Connected.\n"); + + for (i=0;i<10;i++) { + recv(s, &msg, sizeof(struct message), 0); + + /*create child trace*/ + blkin_init_child_info(&trace, &msg.trace_info, &endp, "process b"); + + /*create annotation and log*/ + blkin_init_timestamp_annotation(&ant, "start", &endp); + blkin_record(&trace, &ant); + + /*Process...*/ + usleep(10); + printf("Message received %s\n", msg.actual_message); + + /*create annotation and log*/ + blkin_init_timestamp_annotation(&ant, "end", &endp); + blkin_record(&trace, &ant); + + /*send ack*/ + send(s, "*", 1, 0); + } +} + + +int main() +{ + if (fork()){ + process_a(); + exit(0); + } + else{ + process_b(); + exit(0); + } +} diff --git a/src/blkin/blkin-lib/tests/test.cc b/src/blkin/blkin-lib/tests/test.cc new file mode 100644 index 000000000..f93597dd3 --- /dev/null +++ b/src/blkin/blkin-lib/tests/test.cc @@ -0,0 +1,204 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <thread> +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <ztracer.hpp> +#include <iostream> + +#define SOCK_PATH "socket" + +struct message { + int actual_message; + struct blkin_trace_info trace_info; + message(int s) : actual_message(s) {}; + message() {} +}; + +class Parent { + private: + int s, s2; + ZTracer::Endpoint e; + public: + Parent() : e("0.0.0.0", 1, "parent") + { + connect(); + } + void operator()() + { + struct sockaddr_un remote; + int t; + std::cout << "I am parent process : " << getpid() << std::endl; + + /* Wait for connection */ + t = sizeof(remote); + if ((s2 = accept(s, (struct sockaddr *)&remote, (socklen_t *)&t)) == -1) { + std::cerr << "accept" << std::endl; + exit(1); + } + + std::cerr << "Connected" << std::endl; + + for (int i=0;i<10;i++) { + /*Init trace*/ + ZTracer::Trace tr("parent process", &e); + + process(tr); + + wait_response(); + + /*Log received*/ + tr.event("parent end"); + } + } + + void process(ZTracer::Trace &tr) + { + struct message msg(rand()); + /*Annotate*/ + tr.event("parent start"); + /*Set trace info to the message*/ + msg.trace_info = *tr.get_info(); + + /*send*/ + send(s2, &msg, sizeof(struct message), 0); + } + + void wait_response() + { + char ack; + recv(s2, &ack, 1, 0); + } + + void connect() + { + /*create and bind socket*/ + int len; + struct sockaddr_un local; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + std::cerr << "socket" << std::endl; + exit(1); + } + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, SOCK_PATH); + unlink(local.sun_path); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(s, (struct sockaddr *)&local, len) == -1) { + std::cerr << "bind" << std::endl; + exit(1); + } + + if (listen(s, 5) == -1) { + std::cerr << "listen" << std::endl; + exit(1); + } + + std::cout << "Waiting for a connection..." << std::endl; + } + +}; + +class Child { + private: + int s; + ZTracer::Endpoint e; + public: + Child() : e("0.0.0.1", 2, "child") + { + } + void operator()() + { + /*Connect to the socket*/ + soc_connect(); + + for (int i=0;i<10;i++) + process(); + } + + void process() + { + struct message msg; + recv(s, &msg, sizeof(struct message), 0); + + ZTracer::Trace tr("Child process", &e, &msg.trace_info, true); + tr.event("child start"); + + usleep(10); + std::cout << "Message received : " << msg.actual_message << ::std::endl; + tr.event("child end"); + + send(s, "*", 1, 0); + } + + + void soc_connect() + { + int len; + struct sockaddr_un remote; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + std::cerr << "socket" << std::endl; + exit(1); + } + + std::cout << "Trying to connect...\n" << std::endl; + + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, SOCK_PATH); + len = strlen(remote.sun_path) + sizeof(remote.sun_family); + if (connect(s, (struct sockaddr *)&remote, len) == -1) { + std::cerr << "connect" << std::endl;; + exit(1); + } + + std::cout << "Connected" << std::endl; + } + +}; +int main(int argc, const char *argv[]) +{ + int r = ZTracer::ztrace_init(); + if (r < 0) { + std::cout << "Error initializing blkin" << std::endl; + return -1; + } + Parent p; + Child c; + std::thread workerThread1(p); + std::thread workerThread2(c); + workerThread1.join(); + workerThread2.join(); + + return 0; +} diff --git a/src/blkin/blkin-lib/tests/test_p.cc b/src/blkin/blkin-lib/tests/test_p.cc new file mode 100644 index 000000000..ec59da3b6 --- /dev/null +++ b/src/blkin/blkin-lib/tests/test_p.cc @@ -0,0 +1,210 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <ztracer.hpp> +#include <iostream> +#include <cstdlib> + +#define SOCK_PATH "socket" + +struct message { + int actual_message; + struct blkin_trace_info trace_info; + message(int s) : actual_message(s) {}; + message() {} +}; + +class Parent { + private: + int s, s2; + ZTracer::Endpoint e; + public: + Parent() : e("0.0.0.0", 1, "parent") + { + connect(); + } + void operator()() + { + struct sockaddr_un remote; + int t; + std::cout << "I am parent process : " << getpid() << std::endl; + + /* Wait for connection */ + t = sizeof(remote); + if ((s2 = accept(s, (struct sockaddr *)&remote, (socklen_t *)&t)) == -1) { + std::cerr << "accept" << std::endl; + exit(1); + } + + std::cerr << "Connected" << std::endl; + + for (int i=0;i<10;i++) { + /*Init trace*/ + ZTracer::Trace tr("parent process", &e); + process(tr); + + wait_response(); + + /*Log received*/ + tr.event("parent end"); + } + } + + void process(ZTracer::Trace &tr) + { + struct message msg(rand()); + /*Annotate*/ + tr.event("parent start"); + /*Set trace info to the message*/ + msg.trace_info = *tr.get_info(); + + /*send*/ + send(s2, &msg, sizeof(struct message), 0); + } + + void wait_response() + { + char ack; + recv(s2, &ack, 1, 0); + } + + void connect() + { + /*create and bind socket*/ + int len; + struct sockaddr_un local; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + std::cerr << "socket" << std::endl; + exit(1); + } + + local.sun_family = AF_UNIX; + strcpy(local.sun_path, SOCK_PATH); + unlink(local.sun_path); + len = strlen(local.sun_path) + sizeof(local.sun_family); + if (bind(s, (struct sockaddr *)&local, len) == -1) { + std::cerr << "bind" << std::endl; + exit(1); + } + + if (listen(s, 5) == -1) { + std::cerr << "listen" << std::endl; + exit(1); + } + + std::cout << "Waiting for a connection..." << std::endl; + } + +}; + +class Child { + private: + int s; + ZTracer::Endpoint e; + public: + Child() : e("0.0.0.1", 2, "child") + { + } + void operator()() + { + /*Connect to the socket*/ + soc_connect(); + + for (int i=0;i<10;i++) + process(); + } + + void process() + { + struct message msg; + recv(s, &msg, sizeof(struct message), 0); + + ZTracer::Trace tr("Child process", &e, &msg.trace_info, true); + tr.event("child start"); + + usleep(10); + std::cout << "Message received : " << msg.actual_message << ::std::endl; + tr.event("child end"); + + send(s, "*", 1, 0); + } + + + void soc_connect() + { + int len; + struct sockaddr_un remote; + + if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { + std::cerr << "socket" << std::endl; + exit(1); + } + + std::cout << "Trying to connect...\n" << std::endl; + + remote.sun_family = AF_UNIX; + strcpy(remote.sun_path, SOCK_PATH); + len = strlen(remote.sun_path) + sizeof(remote.sun_family); + if (connect(s, (struct sockaddr *)&remote, len) == -1) { + std::cerr << "connect" << std::endl;; + exit(1); + } + + std::cout << "Connected" << std::endl; + } + +}; +int main(int argc, const char *argv[]) +{ + if (fork()) { + int r = ZTracer::ztrace_init(); + if (r < 0) { + std::cout << "Error initializing blkin" << std::endl; + exit(1); + } + Parent p; + p(); + exit(0); + } else { + int r = ZTracer::ztrace_init(); + if (r < 0) { + std::cout << "Error initializing blkin" << std::endl; + exit(1); + } + Child c; + c(); + exit(0); + } + return 0; +} diff --git a/src/blkin/blkin-lib/tp.c b/src/blkin/blkin-lib/tp.c new file mode 100644 index 000000000..e72177e57 --- /dev/null +++ b/src/blkin/blkin-lib/tp.c @@ -0,0 +1,35 @@ +/* + * tp.c + * + * Copyright (c) 2011 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. + */ + +/* + * Defining macro creates the code objects of the traceprobes, only do + * it once per file + */ +#define TRACEPOINT_CREATE_PROBES +/* + * The header containing our TRACEPOINT_EVENTs. + */ +#include <zipkin_trace.h> diff --git a/src/blkin/blkin-lib/zipkin_c.c b/src/blkin/blkin-lib/zipkin_c.c new file mode 100644 index 000000000..a68182b55 --- /dev/null +++ b/src/blkin/blkin-lib/zipkin_c.c @@ -0,0 +1,356 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include "zipkin_c.h" +#include <pthread.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> + +#define TRACEPOINT_DEFINE +#include <zipkin_trace.h> + +const char *default_ip = "NaN"; +const char *default_name = "NoName"; + +const char* const CLIENT_SEND = "cs"; +const char* const CLIENT_RECV = "cr"; +const char* const SERVER_SEND = "ss"; +const char* const SERVER_RECV = "sr"; +const char* const WIRE_SEND = "ws"; +const char* const WIRE_RECV = "wr"; +const char* const CLIENT_SEND_FRAGMENT = "csf"; +const char* const CLIENT_RECV_FRAGMENT = "crf"; +const char* const SERVER_SEND_FRAGMENT = "ssf"; +const char* const SERVER_RECV_FRAGMENT = "srf"; + +static int64_t random_big() +{ + int64_t a; + a = rand(); + a = a << 32; + int b = rand(); + a = a + b; + if (a<0) + a = !a; + return a; +} + +int blkin_init() +{ + static pthread_mutex_t blkin_init_mutex = PTHREAD_MUTEX_INITIALIZER; + static int initialized = 0; + + /* + * Initialize srand with sth appropriete + * time is not good for archipelago: several deamons -> same timstamp + */ + pthread_mutex_lock(&blkin_init_mutex); + if (!initialized) { + int inf, seed; + inf = open("/dev/urandom", O_RDONLY); //file descriptor 1 + read(inf, &seed, sizeof(int)); + close(inf); + srand(seed); + initialized = 1; + } + pthread_mutex_unlock(&blkin_init_mutex); + return 0; +} + +int blkin_init_new_trace(struct blkin_trace *new_trace, const char *service, + const struct blkin_endpoint *endpoint) +{ + int res; + if (!new_trace) { + res = -EINVAL; + goto OUT; + } + new_trace->name = service; + blkin_init_trace_info(&(new_trace->info)); + new_trace->endpoint = endpoint; + res = 0; + +OUT: + return res; +} + +void blkin_init_trace_info(struct blkin_trace_info *trace_info) +{ + trace_info->span_id = trace_info->trace_id = random_big(); + trace_info->parent_span_id = 0; +} + +int blkin_init_child_info(struct blkin_trace *child, + const struct blkin_trace_info *parent_info, + const struct blkin_endpoint *endpoint, + const char *child_name) +{ + int res; + if ((!child) || (!parent_info) || (!endpoint)){ + res = -EINVAL; + goto OUT; + } + child->info.trace_id = parent_info->trace_id; + child->info.span_id = random_big(); + child->info.parent_span_id = parent_info->span_id; + child->name = child_name; + child->endpoint = endpoint; + res = 0; + +OUT: + return res; +} + +int blkin_init_child(struct blkin_trace *child, + const struct blkin_trace *parent, + const struct blkin_endpoint *endpoint, + const char *child_name) +{ + int res; + if (!parent) { + res = -EINVAL; + goto OUT; + } + if (!endpoint) + endpoint = parent->endpoint; + if (blkin_init_child_info(child, &parent->info, endpoint, child_name) != 0){ + res = -EINVAL; + goto OUT; + } + res = 0; + +OUT: + return res; +} + +int blkin_init_endpoint(struct blkin_endpoint *endp, const char *ip, + int16_t port, const char *name) +{ + int res; + if (!endp){ + res = -EINVAL; + goto OUT; + } + if (!ip) + ip = default_ip; + + endp->ip = ip; + endp->port = port; + endp->name = name; + res = 0; + +OUT: + return res; +} + +int blkin_init_string_annotation(struct blkin_annotation *annotation, + const char *key, const char *val, const struct blkin_endpoint *endpoint) +{ + int res; + if ((!annotation) || (!key) || (!val)){ + res = -EINVAL; + goto OUT; + } + annotation->type = ANNOT_STRING; + annotation->key = key; + annotation->val_str = val; + annotation->endpoint = endpoint; + res = 0; + +OUT: + return res; +} + +int blkin_init_integer_annotation(struct blkin_annotation *annotation, + const char *key, int64_t val, const struct blkin_endpoint *endpoint) +{ + int res; + if ((!annotation) || (!key)) { + res = -EINVAL; + goto OUT; + } + annotation->type = ANNOT_INTEGER; + annotation->key = key; + annotation->val_int = val; + annotation->endpoint = endpoint; + res = 0; + +OUT: + return res; +} + +int blkin_init_timestamp_annotation(struct blkin_annotation *annotation, + const char *event, const struct blkin_endpoint *endpoint) +{ + int res; + if ((!annotation) || (!event)){ + res = -EINVAL; + goto OUT; + } + annotation->type = ANNOT_TIMESTAMP; + annotation->val_str = event; + annotation->endpoint = endpoint; + res = 0; + +OUT: + return res; +} + +int blkin_record(const struct blkin_trace *trace, + const struct blkin_annotation *annotation) +{ + int res; + if (!annotation || !trace || !trace->name) { + res = -EINVAL; + goto OUT; + } + + const struct blkin_endpoint *endpoint = + annotation->endpoint ? : trace->endpoint; + if (!endpoint || !endpoint->ip || !endpoint->name) { + res = -EINVAL; + goto OUT; + } + + if (annotation->type == ANNOT_STRING) { + if ((!annotation->key) || (!annotation->val_str)) { + res = -EINVAL; + goto OUT; + } + tracepoint(zipkin, keyval_string, trace->name, + endpoint->name, endpoint->port, endpoint->ip, + trace->info.trace_id, trace->info.span_id, + trace->info.parent_span_id, + annotation->key, annotation->val_str); + } + else if (annotation->type == ANNOT_INTEGER) { + if (!annotation->key) { + res = -EINVAL; + goto OUT; + } + tracepoint(zipkin, keyval_integer, trace->name, + endpoint->name, endpoint->port, endpoint->ip, + trace->info.trace_id, trace->info.span_id, + trace->info.parent_span_id, + annotation->key, annotation->val_int); + } + else { + if (!annotation->val_str) { + res = -EINVAL; + goto OUT; + } + tracepoint(zipkin, timestamp , trace->name, + endpoint->name, endpoint->port, endpoint->ip, + trace->info.trace_id, trace->info.span_id, + trace->info.parent_span_id, + annotation->val_str); + } + res = 0; +OUT: + return res; +} + +int blkin_get_trace_info(const struct blkin_trace *trace, + struct blkin_trace_info *info) +{ + int res; + if ((!trace) || (!info)){ + res = -EINVAL; + goto OUT; + } + + res = 0; + *info = trace->info; +OUT: + return res; +} + +int blkin_set_trace_info(struct blkin_trace *trace, + const struct blkin_trace_info *info) +{ + int res; + if ((!trace) || (!info)){ + res = -EINVAL; + goto OUT; + } + + res = 0; + trace->info = *info; +OUT: + return res; +} + +int blkin_set_trace_properties(struct blkin_trace *trace, + const struct blkin_trace_info *info, + const char *name, + const struct blkin_endpoint *endpoint) +{ + int res; + if ((!trace) || (!info) || (!endpoint) || (!name)){ + res = -EINVAL; + goto OUT; + } + + res = 0; + trace->info = *info; + trace->name = name; + trace->endpoint = endpoint; + +OUT: + return res; +} + +int blkin_pack_trace_info(const struct blkin_trace_info *info, + struct blkin_trace_info_packed *pinfo) +{ + if (!info || !pinfo) { + return -EINVAL; + } + + pinfo->trace_id = __cpu_to_be64(info->trace_id); + pinfo->span_id = __cpu_to_be64(info->span_id); + pinfo->parent_span_id = __cpu_to_be64(info->parent_span_id); + + return 0; +} + +int blkin_unpack_trace_info(const struct blkin_trace_info_packed *pinfo, + struct blkin_trace_info *info) +{ + if (!info || !pinfo) { + return -EINVAL; + } + + info->trace_id = __be64_to_cpu(pinfo->trace_id); + info->span_id = __be64_to_cpu(pinfo->span_id); + info->parent_span_id = __be64_to_cpu(pinfo->parent_span_id); + + return 0; +} diff --git a/src/blkin/blkin-lib/zipkin_c.h b/src/blkin/blkin-lib/zipkin_c.h new file mode 100644 index 000000000..77b5bc6d7 --- /dev/null +++ b/src/blkin/blkin-lib/zipkin_c.h @@ -0,0 +1,334 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef ZIPKIN_C_H_ +#define ZIPKIN_C_H_ + +#include <stdint.h> +#include <asm/byteorder.h> + +#define BLKIN_TIMESTAMP(trace, endp, event) \ + do { \ + struct blkin_annotation __annot; \ + blkin_init_timestamp_annotation(&__annot, event, endp); \ + blkin_record(trace, &__annot); \ + } while (0); + +#define BLKIN_KEYVAL_STRING(trace, endp, key, val) \ + do { \ + struct blkin_annotation __annot; \ + blkin_init_string_annotation(&__annot, key, val, endp); \ + blkin_record(trace, &__annot); \ + } while (0); + +#define BLKIN_KEYVAL_INTEGER(trace, endp, key, val) \ + do { \ + struct blkin_annotation __annot; \ + blkin_init_integer_annotation(&__annot, key, val, endp);\ + blkin_record(trace, &__annot); \ + } while (0); + +/** + * Core annotations used by Zipkin used to denote the beginning and end of + * client and server spans. + * For more information refer to + * https://github.com/openzipkin/zipkin/blob/master/zipkin-thrift/src/main/thrift/com/twitter/zipkin/zipkinCore.thrift + */ +extern const char* const CLIENT_SEND; +extern const char* const CLIENT_RECV; +extern const char* const SERVER_SEND; +extern const char* const SERVER_RECV; +extern const char* const WIRE_SEND; +extern const char* const WIRE_RECV; +extern const char* const CLIENT_SEND_FRAGMENT; +extern const char* const CLIENT_RECV_FRAGMENT; +extern const char* const SERVER_SEND_FRAGMENT; +extern const char* const SERVER_RECV_FRAGMENT; + +/** + * @struct blkin_endpoint + * Information about an endpoint of our instrumented application where + * annotations take place + */ +struct blkin_endpoint { + const char *ip; + int16_t port; + const char *name; +}; + +/** + * @struct blkin_trace_info + * The information exchanged between different layers offering the needed + * trace semantics + */ +struct blkin_trace_info { + int64_t trace_id; + int64_t span_id; + int64_t parent_span_id; +}; + +/** + * @struct blkin_trace_info_packed + * + * Packed version of the struct blkin_trace_info. Usefull when sending over + * network. + * + */ +struct blkin_trace_info_packed { + __be64 trace_id; + __be64 span_id; + __be64 parent_span_id; +} __attribute__((packed)); + + +/** + * @struct blkin_trace + * Struct used to define the context in which an annotation happens + */ +struct blkin_trace { + const char *name; + struct blkin_trace_info info; + const struct blkin_endpoint *endpoint; +}; + +/** + * @typedef blkin_annotation_type + * There are 2 kinds of annotation key-val and timestamp + */ +typedef enum { + ANNOT_STRING = 0, + ANNOT_INTEGER, + ANNOT_TIMESTAMP +} blkin_annotation_type; + +/** + * @struct blkin_annotation + * Struct carrying information about an annotation. This information can either + * be key-val or that a specific event happened + */ +struct blkin_annotation { + blkin_annotation_type type; + const char *key; + union { + const char *val_str; + int64_t val_int; + }; + const struct blkin_endpoint *endpoint; +}; + +/** + * Initialize the zipkin library. + * + * @return 0 on success + */ +int blkin_init(); + +/** + * Initialize a new blkin_trace with the information given. The new trace will + * have no parent so the parent id will be zero. + * + * @param new_trace the blkin_trace to be initialized + * @param name the trace's name + * @param endpoint a pointer to a blkin_endpoint struct that contains + * info about where the specific trace takes place + * + * @returns 0 on success or negative error code + */ +int blkin_init_new_trace(struct blkin_trace *new_trace, const char *name, + const struct blkin_endpoint *endpoint); + +/** + * Initialize blkin_trace_info for a root span. The new trace will + * have no parent so the parent id will be zero, and the id and trace id will + * be the same. + * + * @param trace_info the blkin_trace_info to be initialized + */ +void blkin_init_trace_info(struct blkin_trace_info *trace_info); + +/** + * Initialize a blkin_trace as a child of the given parent + * bkin_trace. The child trace will have the same trace_id, new + * span_id and parent_span_id its parent's span_id. + * + * @param child the blkin_trace to be initialized + * @param parent the parent blkin_trace + * @param child_name the blkin_trace name of the child + * + * @returns 0 on success or negative error code + */ +int blkin_init_child(struct blkin_trace *child, + const struct blkin_trace *parent, + const struct blkin_endpoint *endpoint, + const char *child_name); + +/** + * Initialize a blkin_trace struct and set the blkin_trace_info field to be + * child of the given blkin_trace_info. This means + * Same trace_id + * Different span_id + * Child's parent_span_id == parent's span_id + * + * @param child the new child blkin_trace_info + * @param info the parent's blkin_trace_info struct + * @param child_name the blkin_trace struct name field + * + * @returns 0 on success or negative error code + */ +int blkin_init_child_info(struct blkin_trace *child, + const struct blkin_trace_info *info, + const struct blkin_endpoint *endpoint, + const char *child_name); + +/** + * Initialize a blkin_endpoint struct with the information given + * + * @param endp the endpoint to be initialized + * @param ip the ip address of the specific endpoint + * @param port the TCP/UDP port of the specific endpoint + * @param name the name of the service running on the specific endpoint + * + * @returns 0 on success or negative error code + */ +int blkin_init_endpoint(struct blkin_endpoint *endpoint, + const char *ip, int16_t port, const char *name); + +/** + * Initialize a key-value blkin_annotation + * + * @param annotation the annotation to be initialized + * @param key the annotation's key + * @param val the annotation's string value + * @param endpoint where did this annotation occured + * + * @returns 0 on success or negative error code + */ +int blkin_init_string_annotation(struct blkin_annotation *annotation, + const char *key, const char *val, + const struct blkin_endpoint *endpoint); +/** + * Initialize a key-value blkin_annotation + * + * @param annotation the annotation to be initialized + * @param key the annotation's key + * @param val the annotation's int value + * @param endpoint where did this annotation occured + * + * @returns 0 on success or negative error code + */ + +int blkin_init_integer_annotation(struct blkin_annotation *annotation, + const char *key, int64_t val, + const struct blkin_endpoint *endpoint); + +/** + * Initialize a timestamp blkin_annotation + * + * @param annotation the annotation to be initialized + * @param event the event happened to be annotated + * @param endpoint where did this annotation occured + * + * @returns 0 on success or negative error code + */ + +int blkin_init_timestamp_annotation(struct blkin_annotation *annot, + const char *event, + const struct blkin_endpoint *endpoint); + +/** + * Log an annotation in terms of a specific trace + * + * @param trace the trace to which the annotation belongs + * @param annotation the annotation to be logged + * + * @returns 0 on success or negative error code + */ +int blkin_record(const struct blkin_trace *trace, + const struct blkin_annotation *annotation); + +/** + * Copy a blkin_trace_info struct into a the field info of a blkin_trace struct + * + * @param trace the destination + * @param info where to copy from + * + * @returns 0 on success or negative error code + */ +int blkin_get_trace_info(const struct blkin_trace *trace, + struct blkin_trace_info *info); + +/** + * Copy the blkin_trace_info from a blkin_trace to another blkin_trace_info + * + * @param trace the trace with the essential info + * @param info the destination + * + * @returns 0 on success or negative error code + */ +int blkin_set_trace_info(struct blkin_trace *trace, + const struct blkin_trace_info *info); + +/** + * Set the trace information, name and endpoint of a trace. + * + * @param trace the trace to which the properties will be assigned + * @param info blkin_trace_information with the trace identifiers + * @param name span name + * @param endpoint associated host + * + * @returns 0 on success or negative error code + */ +int blkin_set_trace_properties(struct blkin_trace *trace, + const struct blkin_trace_info *info, + const char *name, + const struct blkin_endpoint *endpoint); + +/** + * Convert a blkin_trace_info to the packed version. + * + * @param info The unpacked trace info. + * @param pinfo The provided packed version to be initialized. + * + * @returns 0 on success or negative error code + */ +int blkin_pack_trace_info(const struct blkin_trace_info *info, + struct blkin_trace_info_packed *pinfo); + +/** + * Convert a packed blkin_trace_info to the unpacked version. + * + * @param pinfo The provided packed version to be unpacked. + * @param info The unpacked trace info. + * + * @returns 0 on success or negative error code + */ +int blkin_unpack_trace_info(const struct blkin_trace_info_packed *pinfo, + struct blkin_trace_info *info); + +#endif /* ZIPKIN_C_H_ */ diff --git a/src/blkin/blkin-lib/zipkin_trace.h b/src/blkin/blkin-lib/zipkin_trace.h new file mode 100644 index 000000000..2a72d47bd --- /dev/null +++ b/src/blkin/blkin-lib/zipkin_trace.h @@ -0,0 +1,130 @@ +/* + * Zipkin lttng-ust tracepoint provider. + */ + +#undef TRACEPOINT_PROVIDER +#define TRACEPOINT_PROVIDER zipkin + +#undef TRACEPOINT_INCLUDE +#define TRACEPOINT_INCLUDE "./zipkin_trace.h" + +#if !defined(_ZIPKIN_H) || defined(TRACEPOINT_HEADER_MULTI_READ) +#define _ZIPKIN_H + +#include <lttng/tracepoint.h> + +TRACEPOINT_EVENT( + zipkin, + keyval_string, + TP_ARGS(const char *, trace_name, const char *, service, + int, port, const char *, ip, long, trace, + long, span, long, parent_span, + const char *, key, const char *, val ), + + TP_FIELDS( + /* + * Each span has a name mentioned on it in the UI + * This is the trace name + */ + ctf_string(trace_name, trace_name) + /* + * Each trace takes place in a specific machine-endpoint + * This is identified by a name, a port number and an ip + */ + ctf_string(service_name, service) + ctf_integer(int, port_no, port) + ctf_string(ip, ip) + /* + * According to the tracing semantics each trace should have + * a trace id, a span id and a parent span id + */ + ctf_integer(long, trace_id, trace) + ctf_integer(long, span_id, span) + ctf_integer(long, parent_span_id, parent_span) + /* + * The following is the real annotated information + */ + ctf_string(key, key) + ctf_string(val, val) + ) + ) +TRACEPOINT_LOGLEVEL( + zipkin, + keyval_string, + TRACE_WARNING) + +/* + * This tracepoint allows for integers to come out keyval + */ + +TRACEPOINT_EVENT( + zipkin, + keyval_integer, + TP_ARGS(const char *, trace_name, const char *, service, + int, port, const char *, ip, long, trace, + long, span, long, parent_span, + const char *, key, int64_t, val ), + + TP_FIELDS( + /* + * Each span has a name mentioned on it in the UI + * This is the trace name + */ + ctf_string(trace_name, trace_name) + /* + * Each trace takes place in a specific machine-endpoint + * This is identified by a name, a port number and an ip + */ + ctf_string(service_name, service) + ctf_integer(int, port_no, port) + ctf_string(ip, ip) + /* + * According to the tracing semantics each trace should have + * a trace id, a span id and a parent span id + */ + ctf_integer(long, trace_id, trace) + ctf_integer(long, span_id, span) + ctf_integer(long, parent_span_id, parent_span) + /* + * The following is the real annotated information + */ + ctf_string(key, key) + ctf_integer(int64_t, val, val) + ) + ) +TRACEPOINT_LOGLEVEL( + zipkin, + keyval_integer, + TRACE_WARNING) +/* + * In this event we follow the same semantics but we trace timestamp + * annotations + */ + +TRACEPOINT_EVENT( + zipkin, + timestamp, + TP_ARGS(const char *, trace_name, const char *, service, + int, port, const char *, ip, long, trace, + long, span, long, parent_span, + const char *, event), + + TP_FIELDS( + ctf_string(trace_name, trace_name) + ctf_string(service_name, service) + ctf_integer(int, port_no, port) + ctf_string(ip, ip) + ctf_integer(long, trace_id, trace) + ctf_integer(long, span_id, span) + ctf_integer(long, parent_span_id, parent_span) + ctf_string(event, event) + ) + ) +TRACEPOINT_LOGLEVEL( + zipkin, + timestamp, + TRACE_WARNING) +#endif /* _ZIPKIN_H */ + +#include <lttng/tracepoint-event.h> + diff --git a/src/blkin/blkin-lib/ztracer.hpp b/src/blkin/blkin-lib/ztracer.hpp new file mode 100644 index 000000000..3c4707ea4 --- /dev/null +++ b/src/blkin/blkin-lib/ztracer.hpp @@ -0,0 +1,248 @@ +/* + * Copyright 2014 Marios Kogias <marioskogias@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * 1. Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef ZTRACER_H + +#define ZTRACER_H + +#include <string> +extern "C" { +#include <zipkin_c.h> +} + +namespace ZTracer { + using std::string; + + const char* const CLIENT_SEND = "cs"; + const char* const CLIENT_RECV = "cr"; + const char* const SERVER_SEND = "ss"; + const char* const SERVER_RECV = "sr"; + const char* const WIRE_SEND = "ws"; + const char* const WIRE_RECV = "wr"; + const char* const CLIENT_SEND_FRAGMENT = "csf"; + const char* const CLIENT_RECV_FRAGMENT = "crf"; + const char* const SERVER_SEND_FRAGMENT = "ssf"; + const char* const SERVER_RECV_FRAGMENT = "srf"; + + static inline int ztrace_init() { return blkin_init(); } + + class Endpoint : private blkin_endpoint { + private: + string _ip; // storage for blkin_endpoint.ip, see copy_ip() + string _name; // storage for blkin_endpoint.name, see copy_name() + + friend class Trace; + public: + Endpoint(const char *name) + { + blkin_init_endpoint(this, "0.0.0.0", 0, name); + } + Endpoint(const char *ip, int port, const char *name) + { + blkin_init_endpoint(this, ip, port, name); + } + + // copy constructor and operator need to account for ip/name storage + Endpoint(const Endpoint &rhs) : _ip(rhs._ip), _name(rhs._name) + { + blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip, + rhs.port, + _name.size() ? _name.c_str(): rhs.name); + } + const Endpoint& operator=(const Endpoint &rhs) + { + _ip.assign(rhs._ip); + _name.assign(rhs._name); + blkin_init_endpoint(this, _ip.size() ? _ip.c_str() : rhs.ip, + rhs.port, + _name.size() ? _name.c_str() : rhs.name); + return *this; + } + + // Endpoint assumes that ip and name will be string literals, and + // avoids making a copy and freeing on destruction. if you need to + // initialize Endpoint with a temporary string, copy_ip/copy_name() + // will store it in a std::string and assign from that + void copy_ip(const string &newip) + { + _ip.assign(newip); + ip = _ip.c_str(); + } + void copy_name(const string &newname) + { + _name.assign(newname); + name = _name.c_str(); + } + + void copy_address_from(const Endpoint *endpoint) + { + _ip.assign(endpoint->ip); + ip = _ip.c_str(); + port = endpoint->port; + } + void share_address_from(const Endpoint *endpoint) + { + ip = endpoint->ip; + port = endpoint->port; + } + void set_port(int p) { port = p; } + }; + + class Trace : private blkin_trace { + private: + string _name; // storage for blkin_trace.name, see copy_name() + + public: + // default constructor zero-initializes blkin_trace valid() + // will return false on a default-constructed Trace until + // init() + Trace() + { + // zero-initialize so valid() returns false + name = NULL; + info.trace_id = 0; + info.span_id = 0; + info.parent_span_id = 0; + endpoint = NULL; + } + + // construct a Trace with an optional parent + Trace(const char *name, const Endpoint *ep, const Trace *parent = NULL) + { + if (parent && parent->valid()) { + blkin_init_child(this, parent, ep ? : parent->endpoint, + name); + } else { + blkin_init_new_trace(this, name, ep); + } + } + + // construct a Trace from blkin_trace_info + Trace(const char *name, const Endpoint *ep, + const blkin_trace_info *i, bool child=false) + { + if (child) + blkin_init_child_info(this, i, ep, name); + else { + blkin_init_new_trace(this, name, ep); + set_info(i); + } + } + + // copy constructor and operator need to account for name storage + Trace(const Trace &rhs) : _name(rhs._name) + { + name = _name.size() ? _name.c_str() : rhs.name; + info = rhs.info; + endpoint = rhs.endpoint; + } + const Trace& operator=(const Trace &rhs) + { + _name.assign(rhs._name); + name = _name.size() ? _name.c_str() : rhs.name; + info = rhs.info; + endpoint = rhs.endpoint; + return *this; + } + + // return true if the Trace has been initialized + bool valid() const { return info.trace_id != 0; } + operator bool() const { return valid(); } + + // (re)initialize a Trace with an optional parent + int init(const char *name, const Endpoint *ep, + const Trace *parent = NULL) + { + if (parent && parent->valid()) + return blkin_init_child(this, parent, + ep ? : parent->endpoint, name); + + return blkin_init_new_trace(this, name, ep); + } + + // (re)initialize a Trace from blkin_trace_info + int init(const char *name, const Endpoint *ep, + const blkin_trace_info *i, bool child=false) + { + if (child) + return blkin_init_child_info(this, i, ep, _name.c_str()); + + return blkin_set_trace_properties(this, i, _name.c_str(), ep); + } + + // Trace assumes that name will be a string literal, and avoids + // making a copy and freeing on destruction. if you need to + // initialize Trace with a temporary string, copy_name() will store + // it in a std::string and assign from that + void copy_name(const string &newname) + { + _name.assign(newname); + name = _name.c_str(); + } + + const blkin_trace_info* get_info() const { return &info; } + void set_info(const blkin_trace_info *i) { info = *i; } + + // record key-value annotations + void keyval(const char *key, const char *val) const + { + if (valid()) + BLKIN_KEYVAL_STRING(this, endpoint, key, val); + } + void keyval(const char *key, int64_t val) const + { + if (valid()) + BLKIN_KEYVAL_INTEGER(this, endpoint, key, val); + } + void keyval(const char *key, const char *val, const Endpoint *ep) const + { + if (valid()) + BLKIN_KEYVAL_STRING(this, ep, key, val); + } + void keyval(const char *key, int64_t val, const Endpoint *ep) const + { + if (valid()) + BLKIN_KEYVAL_INTEGER(this, ep, key, val); + } + + // record timestamp annotations + void event(const char *event) const + { + if (valid()) + BLKIN_TIMESTAMP(this, endpoint, event); + } + void event(const char *event, const Endpoint *ep) const + { + if (valid()) + BLKIN_TIMESTAMP(this, ep, event); + } + }; + +} +#endif /* end of include guard: ZTRACER_H */ |