From aac7f1d17ec00be4ea7bb833e57c1869627a380f Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 8 Oct 2024 15:07:34 +0200 Subject: [PATCH] BMP: TX stats in CLI --- lib/string.h | 43 +++++++++++++++++++++++++++++++++++++++++++ proto/bmp/bmp.c | 10 +++++++++- proto/bmp/bmp.h | 2 ++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/string.h b/lib/string.h index 0c21f513..f1fb1382 100644 --- a/lib/string.h +++ b/lib/string.h @@ -24,6 +24,8 @@ char *mb_sprintf(pool *p, const char *fmt, ...); char *mb_vsprintf(pool *p, const char *fmt, va_list args); char *lp_sprintf(linpool *p, const char *fmt, ...); char *lp_vsprintf(linpool *p, const char *fmt, va_list args); +#define tmp_sprintf(...) lp_sprintf(tmp_linpool, __VA_ARGS__) +#define tmp_vsprintf(...) lp_vsprintf(tmp_linpool, __VA_ARGS__) int buffer_vprint(buffer *buf, const char *fmt, va_list args); int buffer_print(buffer *buf, const char *fmt, ...); @@ -36,6 +38,47 @@ byte bstrtobyte16(const char *str); int bstrhextobin(const char *s, byte *b); int bstrbintohex(const byte *b, size_t len, char *buf, size_t size, char delim) ACCESS_READ(1, 2) ACCESS_WRITE(3, 4); +static inline const char *fmt_order(u64 value, int decimals, u64 kb_threshold) +{ + bool too_big = (value + 512 < 512ULL); + + u64 mv = value; + int magnitude = 0; + while (mv > kb_threshold) + { + magnitude++; + mv = (mv + (too_big ? 0 : 512)) / 1024; + } + + ASSERT_DIE(magnitude < 7); + char suffix = " kMGTEP"[magnitude]; + while ((magnitude - 1) * 3 > decimals) + { + magnitude--; + value = (value + (too_big ? 0 : 512)) / 1024; + too_big = false; + } + + if ((!decimals) || (suffix == ' ')) + return tmp_sprintf("%lu %c", value, suffix); + + u64 divisor = 1; + for (int i=0; ibuf, btb->end); - if (sk_send(p->sk, btb->end - btb->buf) <= 0) + u64 sz = btb->end - btb->buf; + p->tx_sent += sz; + p->tx_sent_total += sz; + if (sk_send(p->sk, sz) <= 0) return; // log(L_INFO "btb free buf %p", btb->buf); @@ -1174,6 +1177,7 @@ bmp_down(struct bmp_proto *p) { ASSERT(p->started); p->started = false; + p->tx_sent = 0; TRACE(D_EVENTS, "BMP session closed"); @@ -1477,6 +1481,10 @@ bmp_show_proto_info(struct proto *P) if (p->sock_err) cli_msg(-1006, " %-19s %M", "Last error:", p->sock_err); + + cli_msg(-1006, " %-19s % 7u (limit %u)", "Pending buffers:", p->tx_pending_count, p->tx_pending_limit); + cli_msg(-1006, " %-19s % 9sB", "Session TX:", fmt_order(p->tx_sent, 1, 10000)); + cli_msg(-1006, " %-19s % 9sB", "Total TX:", fmt_order(p->tx_sent_total, 1, 10000)); } } diff --git a/proto/bmp/bmp.h b/proto/bmp/bmp.h index 939a1531..28f4f478 100644 --- a/proto/bmp/bmp.h +++ b/proto/bmp/bmp.h @@ -73,6 +73,8 @@ struct bmp_proto { struct bmp_tx_buffer_class *tx_pcls; // Class used for tx_pending allocations uint tx_pending_count; // How many buffers waiting for flush uint tx_pending_limit; // Maximum on buffer count + u64 tx_sent; // Amount of data sent + u64 tx_sent_total; // Amount of data sent accumulated over reconnections event *tx_overflow_event; // Too many buffers waiting for flush timer *connect_retry_timer; // Timer for retrying connection to the BMP collector bool started; // Flag that stores running status of BMP instance