From 1e6510aea7c503b94ab5beda987d630ed47808bf Mon Sep 17 00:00:00 2001 From: Florian Fischer <florian.fl.fischer@fau.de> Date: Sun, 7 Mar 2021 13:18:02 +0100 Subject: [PATCH] [EchoServer] add configurable computation part in us spent busy looping --- apps/EchoServer.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/apps/EchoServer.cpp b/apps/EchoServer.cpp index e0c96ef1..683b9265 100644 --- a/apps/EchoServer.cpp +++ b/apps/EchoServer.cpp @@ -4,6 +4,7 @@ #include <sys/types.h> #include <cerrno> +#include <chrono> #include <cstdlib> #include <cstring> #include <iostream> @@ -12,17 +13,24 @@ #include "Common.hpp" #include "Debug.hpp" #include "Runtime.hpp" +#include "emper-config.h" #include "io.hpp" +#ifdef EMPER_HAS_COMPARE_H +#include <compare> +#endif + const std::string HOST = "::"; const std::string PORT = "12345"; +unsigned int computations_us = 0; + auto main(int argc, char* argv[]) -> int { std::string host = HOST; std::string port = PORT; - if (argc > 2) { - std::cerr << "Usage: " << argv[0] << " [port]" << std::endl; + if (argc > 3) { + std::cerr << "Usage: " << argv[0] << " [port] [computation_us]" << std::endl; exit(EXIT_FAILURE); } @@ -30,6 +38,10 @@ auto main(int argc, char* argv[]) -> int { port = std::string(argv[1]); } + if (argc > 2) { + computations_us = std::stoi(argv[2]); + } + std::cout << "Echoserver listening on " << host << ":" << port << std::endl; Runtime runtime; @@ -53,6 +65,14 @@ auto main(int argc, char* argv[]) -> int { exit(EXIT_SUCCESS); } + const auto start = std::chrono::steady_clock::now(); + const auto deadline = start + std::chrono::microseconds(computations_us); + // TODO: The suppressed linter error below may be a false positive + // reported by clang-tidy. + // NOLINTNEXTLINE(modernize-use-nullptr) + while (std::chrono::steady_clock::now() < deadline) { + } + ssize_t bytes_send = emper::io::sendAndWait(socket, buf, bytes_recv, MSG_NOSIGNAL, true); if (unlikely(bytes_recv != bytes_send)) { LOGE("server send failed: " << strerror(errno)); -- GitLab