From a7453e30ca4c9f42d27d42f96bc223533c330900 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Fri, 1 Feb 2019 17:02:02 +0106 Subject: ipc_tcp: add new ipc example using loopback sockets This demo should (hopefully serve) as a basis for overlaying pcap and ftrace data within trace compass. Signed-off-by: John Ogness --- schulung_tools/ipc_tcp/Makefile | 25 +++++++++++++++ schulung_tools/ipc_tcp/README | 25 +++++++++++++++ schulung_tools/ipc_tcp/recv.c | 68 +++++++++++++++++++++++++++++++++++++++++ schulung_tools/ipc_tcp/send.c | 52 +++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 schulung_tools/ipc_tcp/Makefile create mode 100644 schulung_tools/ipc_tcp/README create mode 100644 schulung_tools/ipc_tcp/recv.c create mode 100644 schulung_tools/ipc_tcp/send.c diff --git a/schulung_tools/ipc_tcp/Makefile b/schulung_tools/ipc_tcp/Makefile new file mode 100644 index 0000000..be9a747 --- /dev/null +++ b/schulung_tools/ipc_tcp/Makefile @@ -0,0 +1,25 @@ +CC = $(CROSS_COMPILE)gcc +CFLAGS = -Wall -Werror -g -O0 -std=c99 -D_GNU_SOURCE +LDFLAGS = +CFLAGS += \ + -Wchar-subscripts \ + -Wmissing-declarations \ + -Wmissing-prototypes \ + -Wnested-externs \ + -Wpointer-arith \ + -Wcast-align \ + -Wfloat-equal \ + -Wsign-compare + +all: recv send + +recv: recv.c + $(CC) $(CFLAGS) $(LDFLAGS) -o$@ $< + +send: send.c + $(CC) $(CFLAGS) $(LDFLAGS) -o$@ $< + +clean: + rm -f send recv + +.PHONY: clean all diff --git a/schulung_tools/ipc_tcp/README b/schulung_tools/ipc_tcp/README new file mode 100644 index 0000000..71222dd --- /dev/null +++ b/schulung_tools/ipc_tcp/README @@ -0,0 +1,25 @@ +#!/bin/sh + +# build programs send/recv +make + +# create uprobe events +sudo perf probe -x ./send sending=send.c:37 +sudo perf probe -x ./recv received=recv.c:62 + +# run receiver +sudo taskset 1 chrt -f 80 ./recv & + +# run sender (wrapping with trace-cmd) +sudo trace-cmd record \ + -e sched:sched_switch \ + -e sched:sched_wakeup \ + -e sched:sched_pi_setprio \ + -e probe_send:sending \ + -e probe_recv:received \ + -e syscalls \ + taskset 1 chrt -f 70 ./send +# type message and hit return + +# view results +kernelshark diff --git a/schulung_tools/ipc_tcp/recv.c b/schulung_tools/ipc_tcp/recv.c new file mode 100644 index 0000000..bd76f20 --- /dev/null +++ b/schulung_tools/ipc_tcp/recv.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static char buffer[1024 * 1024]; + +int main(void) +{ + struct sockaddr_in sa; + size_t count = 0; + int listen_s; + size_t size; + int rv; + int s; + + listen_s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listen_s < 0) + return 1; + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr("127.0.0.1"); + sa.sin_port = htons(8806); + + rv = 1; + if (setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, + (void *)&rv, sizeof(rv)) < 0) { + return 1; + } + + if (bind(listen_s, (struct sockaddr *)&sa, sizeof(sa)) < 0) { + fprintf(stderr, "bind() failed: %s\n", strerror(errno)); + return 1; + } + + if (listen(listen_s, 1) != 0) + return 1; + + s = accept(listen_s, NULL, NULL); + if (s < 0) + return 1; + + if (read(s, &size, sizeof(size)) != sizeof(size)) + return 1; + + if (size >= sizeof(buffer)) + size = sizeof(buffer) - 1; + + while (count < size) { + rv = read(s, buffer + count, size - count); + if (rv <= 0) + return 1; + count += rv; + } + + /* set "received" uprobe here */ + + printf("recv (%d): received message: %s\n", getpid(), buffer); + + close(s); + close(listen_s); + + return 0; +} diff --git a/schulung_tools/ipc_tcp/send.c b/schulung_tools/ipc_tcp/send.c new file mode 100644 index 0000000..60d2f1f --- /dev/null +++ b/schulung_tools/ipc_tcp/send.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static char buffer[1024 * 1024]; + +int main(void) +{ + struct sockaddr_in sa; + size_t count = 0; + size_t size; + int rv; + int s; + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s < 0) + return 1; + memset(&sa, 0, sizeof(sa)); + sa.sin_family = AF_INET; + sa.sin_addr.s_addr = inet_addr("127.0.0.1"); + sa.sin_port = htons(8806); + + if (connect(s, (struct sockaddr *)&sa, sizeof(struct sockaddr)) != 0) + return 1; + + printf("send (%d): type message and hit RETURN to send signal\n", + getpid()); + fgets(buffer, sizeof(buffer), stdin); + + /* set "sending" uprobe here */ + + size = strlen(buffer); + + if (write(s, &size, sizeof(size)) != sizeof(size)) + return 1; + + while (count < size) { + rv = write(s, buffer + count, size - count); + if (rv <= 0) + return 1; + count += rv; + } + + sleep(1); + + return 0; +} -- cgit v1.2.3