From 25c2756a82ad480edc3b37cdd6ae0154518af0a4 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Mon, 10 Sep 2018 12:13:18 -0600 Subject: [PATCH] apps/examples/tcpblaster: Add an option to use poll() to pace input or output. --- examples/tcpblaster/Kconfig | 20 ++++++++++++++++++++ examples/tcpblaster/tcpblaster_client.c | 25 +++++++++++++++++++++++++ examples/tcpblaster/tcpblaster_server.c | 25 +++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/examples/tcpblaster/Kconfig b/examples/tcpblaster/Kconfig index ee6612fa3..87479eda9 100644 --- a/examples/tcpblaster/Kconfig +++ b/examples/tcpblaster/Kconfig @@ -35,6 +35,26 @@ config EXAMPLES_TCPBLASTER_PRIORITY1 int "Target1 priority" default 100 +config EXAMPLES_TCPBLASTER_POLLOUT + bool "Use poll() to pace output" + default n + depends on !DISABLE_POLL + ---help--- + Client will use poll() to verify that send() will not block. This + does not improve performance (in fact, it will degrade perform + slightly). But it is useful for verifying that poll() can be used + to pace output. + +config EXAMPLES_TCPBLASTER_POLLIN + bool "Use poll() to pace input" + default n + depends on !DISABLE_POLL + ---help--- + Client will use poll() to verify that recv() will not block. This + does not improve performance (in fact, it will degrade perform + slightly). But it is useful for verifying that poll() can be used + to pace input. + config EXAMPLES_TCPBLASTER_LOOPBACK bool "Loopback test" default n diff --git a/examples/tcpblaster/tcpblaster_client.c b/examples/tcpblaster/tcpblaster_client.c index 8407335c3..0de331bfe 100644 --- a/examples/tcpblaster/tcpblaster_client.c +++ b/examples/tcpblaster/tcpblaster_client.c @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -147,6 +148,30 @@ void tcpblaster_client(void) for (; ; ) { +#ifdef CONFIG_EXAMPLES_TCPBLASTER_POLLOUT + struct pollfd fds[1]; + int ret; + + memset(fds, 0, 1 * sizeof(struct pollfd)); + fds[0].fd = sockfd; + fds[0].events = POLLOUT | POLLHUP; + + /* Wait until we can send data or until the connection is lost */ + + ret = poll(fds, 1, -1); + if (ret < 0) + { + printf("client: ERROR poll failed: %d\n", errno); + goto errout_with_socket; + } + + if ((fds[0].revents & POLLHUP) != 0) + { + printf("client: WARNING poll returned POLLHUP\n"); + goto errout_with_socket; + } +#endif + nbytessent = send(sockfd, outbuf, SENDSIZE, 0); if (nbytessent < 0) { diff --git a/examples/tcpblaster/tcpblaster_server.c b/examples/tcpblaster/tcpblaster_server.c index b974918c6..2de06722d 100644 --- a/examples/tcpblaster/tcpblaster_server.c +++ b/examples/tcpblaster/tcpblaster_server.c @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -187,6 +188,30 @@ void tcpblaster_server(void) for (; ; ) { +#ifdef CONFIG_EXAMPLES_TCPBLASTER_POLLIN + struct pollfd fds[1]; + int ret; + + memset(fds, 0, 1 * sizeof(struct pollfd)); + fds[0].fd = acceptsd; + fds[0].events = POLLIN | POLLHUP; + + /* Wait until we can receive data or until the connection is lost */ + + ret = poll(fds, 1, -1); + if (ret < 0) + { + printf("server: ERROR poll failed: %d\n", errno); + goto errout_with_acceptsd; + } + + if ((fds[0].revents & POLLHUP) != 0) + { + printf("server: WARNING poll returned POLLHUP\n"); + goto errout_with_acceptsd; + } +#endif + nbytesread = recv(acceptsd, buffer, SENDSIZE, 0); if (nbytesread < 0) {