From: Michael Tremer Date: Thu, 4 Feb 2021 12:03:12 +0000 (+0000) Subject: util: Move some helper functions into an extra module X-Git-Tag: 0.1.0~10 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=24c2452d99c667d49af5325b5a67a75a732935ed;p=fireperf.git util: Move some helper functions into an extra module Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index bafa535..9503f4e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -67,7 +67,9 @@ fireperf_SOURCES = \ src/main.c \ src/main.h \ src/server.c \ - src/server.h + src/server.h \ + src/util.c \ + src/util.h # ------------------------------------------------------------------------------ diff --git a/src/server.c b/src/server.c index b119c80..adf5fe7 100644 --- a/src/server.c +++ b/src/server.c @@ -29,6 +29,7 @@ #include "logging.h" #include "main.h" #include "server.h" +#include "util.h" #define SOCKET_BACKLOG 1024 @@ -43,81 +44,6 @@ struct fireperf_server_stats { size_t total_bytes_received; }; -enum { - FIREPERF_FORMAT_BYTES, - FIREPERF_FORMAT_BITS, -}; - -const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; -const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL }; - -static char* format_size(ssize_t size, int unit) { - const char** suffixes; - unsigned int divisor; - - switch (unit) { - case FIREPERF_FORMAT_BYTES: - suffixes = suffixes_bytes; - divisor = 1024; - break; - - case FIREPERF_FORMAT_BITS: - suffixes = suffixes_bits; - divisor = 1000; - break; - - // Invalid input - default: - return NULL; - } - - const char** suffix; - char* retval = NULL; - - // Convert into double - double s = size; - - for (suffix = suffixes; *suffix; suffix++) { - if (abs(s) < divisor) - break; - - s /= divisor; - } - - if (!*suffix) - return NULL; - - // Format the output string - int r = asprintf(&retval, "%.02f %s", s, *suffix); - if (r < 0) - return NULL; - - return retval; -} - -static char __timespec[20]; - -static const char* format_timespec(const struct timespec* t) { - // Convert to local time - struct tm* tm = localtime(&t->tv_sec); - - size_t s = strftime(__timespec, sizeof(__timespec), "%F %T", tm); - - if (s) - return __timespec; - - return NULL; -} - -static unsigned long timespec_delta(struct timespec* t1, struct timespec* t2) { - // Compute delta in milliseconds - return ( - ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000)) - - - ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000)) - ) / 1000.0; -} - static int dump_stats(struct fireperf_config* conf, struct fireperf_server_stats* stats) { struct timespec now; diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..551574b --- /dev/null +++ b/src/util.c @@ -0,0 +1,96 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2021 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include +#include + +#include "util.h" + +static const char* suffixes_bytes[] = { "B", "KiB", "MiB", "GiB", "TiB", NULL }; +static const char* suffixes_bits[] = { "b", "Kb", "Mb", "Gb", "Tb", NULL }; + +char* format_size(ssize_t size, int unit) { + const char** suffixes; + unsigned int divisor; + + switch (unit) { + case FIREPERF_FORMAT_BYTES: + suffixes = suffixes_bytes; + divisor = 1024; + break; + + case FIREPERF_FORMAT_BITS: + suffixes = suffixes_bits; + divisor = 1000; + break; + + // Invalid input + default: + return NULL; + } + + const char** suffix; + char* retval = NULL; + + // Convert into double + double s = size; + + for (suffix = suffixes; *suffix; suffix++) { + if (abs(s) < divisor) + break; + + s /= divisor; + } + + if (!*suffix) + return NULL; + + // Format the output string + int r = asprintf(&retval, "%.02f %s", s, *suffix); + if (r < 0) + return NULL; + + return retval; +} + +static char __timespec[20]; + +const char* format_timespec(const struct timespec* t) { + // Convert to local time + struct tm* tm = localtime(&t->tv_sec); + + size_t s = strftime(__timespec, sizeof(__timespec), "%F %T", tm); + + if (s) + return __timespec; + + return NULL; +} + +unsigned long timespec_delta(struct timespec* t1, struct timespec* t2) { + // Compute delta in milliseconds + return ( + ((t1->tv_sec * 1000) + (t1->tv_nsec / 1000000)) + - + ((t2->tv_sec * 1000) + (t2->tv_nsec / 1000000)) + ) / 1000.0; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..077134f --- /dev/null +++ b/src/util.h @@ -0,0 +1,31 @@ +/*############################################################################# +# # +# fireperf - A network benchmarking tool # +# Copyright (C) 2021 IPFire Development Team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include + +enum { + FIREPERF_FORMAT_BYTES, + FIREPERF_FORMAT_BITS, +}; + +char* format_size(ssize_t size, int unit); +const char* format_timespec(const struct timespec* t); +unsigned long timespec_delta(struct timespec* t1, struct timespec* t2);