From 576a63be9ce196ff82590676a95e68535471b1c6 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Nov 2022 10:13:05 -0500 Subject: [PATCH] introduce new macro for format buffer length --- include/villas/node/config.hpp.in | 7 ++++--- lib/format.cpp | 3 ++- lib/hooks/print.cpp | 8 ++++++-- lib/nodes/go.cpp | 12 ++++++------ lib/nodes/kafka.cpp | 2 +- lib/nodes/zeromq.cpp | 2 +- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/villas/node/config.hpp.in b/include/villas/node/config.hpp.in index eb6a60d79..db4e1ef5e 100644 --- a/include/villas/node/config.hpp.in +++ b/include/villas/node/config.hpp.in @@ -14,9 +14,10 @@ #include /** Default number of values in a sample */ -#define DEFAULT_SAMPLE_LENGTH 64u -#define DEFAULT_QUEUE_LENGTH 1024u -#define MAX_SAMPLE_LENGTH 256u +#define DEFAULT_SAMPLE_LENGTH 64u +#define DEFAULT_QUEUE_LENGTH 1024u +#define MAX_SAMPLE_LENGTH 512u +#define DEFAULT_FORMAT_BUFFER_LENGTH 4096u /** Number of hugepages which are requested from the the kernel. * @see https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt */ diff --git a/lib/format.cpp b/lib/format.cpp index 4dc0cc6bd..b65740373 100644 --- a/lib/format.cpp +++ b/lib/format.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -62,7 +63,7 @@ Format::Format(int fl) : signals(nullptr) { in.buflen = - out.buflen = 4096; + out.buflen = DEFAULT_FORMAT_BUFFER_LENGTH; in.buffer = new char[in.buflen]; out.buffer = new char[out.buflen]; diff --git a/lib/hooks/print.cpp b/lib/hooks/print.cpp index d5dc06ba2..109916f4e 100644 --- a/lib/hooks/print.cpp +++ b/lib/hooks/print.cpp @@ -26,6 +26,7 @@ protected: std::string output_path; FILE *output; + std::vector output_buffer; public: PrintHook(Path *p, Node *n, int fl, int prio, bool en = true) : @@ -44,6 +45,8 @@ public: if (!output) throw SystemError("Failed to open file"); } + else + output_buffer = std::vector(DEFAULT_FORMAT_BUFFER_LENGTH); formatter->start(signals); @@ -107,10 +110,11 @@ public: assert(state == State::STARTED); if (!output) { - char buf[1024]; + char *buf = output_buffer.data(); + size_t buflen = output_buffer.size(); size_t wbytes; - formatter->sprint(buf, sizeof(buf), &wbytes, smp); + formatter->sprint(buf, buflen, &wbytes, smp); if (wbytes > 0 && buf[wbytes-1] == '\n') buf[wbytes-1] = 0; diff --git a/lib/nodes/go.cpp b/lib/nodes/go.cpp index d7d39b0f0..c2ba472e6 100644 --- a/lib/nodes/go.cpp +++ b/lib/nodes/go.cpp @@ -200,14 +200,14 @@ std::vector GoNode::getNetemFDs() int GoNode::_read(struct Sample * smps[], unsigned cnt) { int ret; - char buf[4096]; + char data[DEFAULT_FORMAT_BUFFER_LENGTH]; size_t rbytes; - auto d = GoNodeRead(node, buf, 4096); + auto d = GoNodeRead(node, data, sizeof(data)); if (d.r1) return d.r1; - ret = formatter->sscan(buf, d.r0, &rbytes, smps, cnt); + ret = formatter->sscan(data, d.r0, &rbytes, smps, cnt); if (ret < 0 || (size_t) d.r0 != rbytes) { logger->warn("Received invalid packet: ret={}, bytes={}, rbytes={}", ret, d.r0, rbytes); return ret; @@ -219,17 +219,17 @@ int GoNode::_read(struct Sample * smps[], unsigned cnt) int GoNode::_write(struct Sample * smps[], unsigned cnt) { int ret; - char buf[4096]; + char buf[DEFAULT_FORMAT_BUFFER_LENGTH]; size_t wbytes; - ret = formatter->sprint(buf, 4096, &wbytes, smps, cnt); + ret = formatter->sprint(buf, DEFAULT_FORMAT_BUFFER_LENGTH, &wbytes, smps, cnt); if (ret < 0) return ret; GoSlice slice = { data: buf, len: GoInt(wbytes), - cap: 4096 + cap: DEFAULT_FORMAT_BUFFER_LENGTH }; ret = GoNodeWrite(node, slice); diff --git a/lib/nodes/kafka.cpp b/lib/nodes/kafka.cpp index b223b4dce..c8902b5a8 100644 --- a/lib/nodes/kafka.cpp +++ b/lib/nodes/kafka.cpp @@ -541,7 +541,7 @@ int villas::node::kafka_write(NodeCompat *n, struct Sample * const smps[], unsig size_t wbytes; - char data[4096]; + char data[DEFAULT_FORMAT_BUFFER_LENGTH]; ret = k->formatter->sprint(data, sizeof(data), &wbytes, smps, cnt); if (ret < 0) diff --git a/lib/nodes/zeromq.cpp b/lib/nodes/zeromq.cpp index f13588cd6..74c3a9ebd 100644 --- a/lib/nodes/zeromq.cpp +++ b/lib/nodes/zeromq.cpp @@ -551,7 +551,7 @@ int villas::node::zeromq_write(NodeCompat *n, struct Sample * const smps[], unsi size_t wbytes; zmq_msg_t m; - char data[4096]; + char data[DEFAULT_FORMAT_BUFFER_LENGTH]; ret = z->formatter->sprint(data, sizeof(data), &wbytes, smps, cnt); if (ret <= 0)