summaryrefslogtreecommitdiff
path: root/lib/vty.c
diff options
context:
space:
mode:
authorSrujana <skanchisamud@nvidia.com>2024-07-30 20:39:33 +0000
committerRajasekar Raja <rajasekarr@nvidia.com>2024-08-27 12:47:00 -0700
commit9112fb367b1ae0168b4e7a81f41c2ca621979199 (patch)
tree134a10f33a7069ca9025cf2f46726ff3bd227559 /lib/vty.c
parentfa7c77f2939b4be9649ee95a78ed3a307aeac342 (diff)
lib: Memory spike reduction for sh cmds at scale
The output buffer vty->obuf is a linked list where each element is of 4KB. Currently, when a huge sh command like <show ip route json> is executed on a large scale, all the vty_outs are processed and the entire data is accumulated. After the entire vty execution, vtysh_flush proceeses and puts this data in the socket (131KB at a time). Problem here is the memory spike for such heavy duty show commands. The fix here is to chunkify the output on VTY shell by flushing it intermediately for every 128 KB of output accumulated and free the memory allocated for the buffer data. This way, we achieve ~25-30% reduction in the memory spike. Fixes: #16498 Note: This is a continuation of MR #16498 Signed-off-by: Srujana <skanchisamud@nvidia.com> Signed-off-by: Rajasekar Raja <rajasekarr@nvidia.com>
Diffstat (limited to 'lib/vty.c')
-rw-r--r--lib/vty.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/vty.c b/lib/vty.c
index 0a4a3d2b86..256a3bb3f5 100644
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -345,8 +345,17 @@ int vty_out(struct vty *vty, const char *format, ...)
case VTY_SHELL_SERV:
case VTY_FILE:
default:
+ vty->vty_buf_size_accumulated += strlen(filtered);
/* print without crlf replacement */
buffer_put(vty->obuf, (uint8_t *)filtered, strlen(filtered));
+ /* For every chunk of memory, we invoke vtysh_flush where we
+ * put the data of collective vty->obuf Linked List items on the
+ * socket and free the vty->obuf data.
+ */
+ if (vty->vty_buf_size_accumulated >= VTY_MAX_INTERMEDIATE_FLUSH) {
+ vty->vty_buf_size_accumulated = 0;
+ vtysh_flush(vty);
+ }
break;
}
@@ -2118,6 +2127,8 @@ static void vtysh_accept(struct event *thread)
int client_len;
struct sockaddr_un client;
struct vty *vty;
+ int ret = 0;
+ uint32_t sndbufsize = VTY_SEND_BUF_MAX;
vty_event_serv(VTYSH_SERV, vtyserv);
@@ -2141,6 +2152,20 @@ static void vtysh_accept(struct event *thread)
close(sock);
return;
}
+
+ /*
+ * Increasing the SEND socket buffer size so that the socket can hold
+ * before sending it to VTY shell.
+ */
+ ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&sndbufsize,
+ sizeof(sndbufsize));
+ if (ret < 0) {
+ flog_err(EC_LIB_SOCKET,
+ "Cannot set socket %d send buffer size, %s", sock,
+ safe_strerror(errno));
+ close(sock);
+ return;
+ }
set_cloexec(sock);
#ifdef VTYSH_DEBUG
@@ -2227,6 +2252,7 @@ static int vtysh_flush(struct vty *vty)
vty_close(vty);
return -1;
case BUFFER_EMPTY:
+ vty->vty_buf_size_accumulated = 0;
break;
}
return 0;