From 2dfa77805662706e3c1d92ad0f5412dec2f43ed1 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 30 Jan 2025 16:25:40 +0100 Subject: [PATCH] MINOR: fd: add a generation number to file descriptors This patch adds a counter of close() on file descriptors in the fdtab. The goal is to better detect if reported events concern the current or a previous file descriptor. For now the counter is only added, and is showed in "show fd" as "gen". We're reusing unused space at the end of the struct. If it's needed for something more important later, this patch can be reverted. (cherry picked from commit d155924efe453faa0ea5b5e62e6a3e2bf7ee7358) Signed-off-by: Willy Tarreau (cherry picked from commit 3f646022e0d7421a7f105e9aab1546fd7263ce7d) Signed-off-by: Willy Tarreau --- include/haproxy/fd-t.h | 1 + src/cli.c | 3 ++- src/fd.c | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/haproxy/fd-t.h b/include/haproxy/fd-t.h index c2b2aee..5fa3689 100644 --- a/include/haproxy/fd-t.h +++ b/include/haproxy/fd-t.h @@ -198,6 +198,7 @@ struct fdtab { * if the room is needed for more important stuff. */ unsigned int nb_takeover; /* number of times this FD was taken over since inserted (used for debugging) */ + unsigned int generation; /* number of times this FD was closed before (used for epoll strengthening) */ #ifdef DEBUG_FD unsigned int event_count; /* number of events reported */ #endif diff --git a/src/cli.c b/src/cli.c index a20b86d..ff123b7 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1411,7 +1411,7 @@ static int cli_io_handler_show_fd(struct appctx *appctx) suspicious = 1; chunk_printf(&trash, - " %5d : st=0x%06x(%c%c %c%c%c%c%c W:%c%c%c R:%c%c%c) ref=%#x gid=%d tmask=0x%lx umask=0x%lx prmsk=0x%lx pwmsk=0x%lx owner=%p tkov=%u iocb=%p(", + " %5d : st=0x%06x(%c%c %c%c%c%c%c W:%c%c%c R:%c%c%c) ref=%#x gid=%d tmask=0x%lx umask=0x%lx prmsk=0x%lx pwmsk=0x%lx owner=%p gen=%u tkov=%u iocb=%p(", fd, fdt.state, (fdt.state & FD_CLONED) ? 'C' : 'c', @@ -1433,6 +1433,7 @@ static int cli_io_handler_show_fd(struct appctx *appctx) polled_mask[fd].poll_recv, polled_mask[fd].poll_send, fdt.owner, + fdt.generation, fdt.nb_takeover, fdt.iocb); resolve_sym_name(&trash, NULL, fdt.iocb); diff --git a/src/fd.c b/src/fd.c index ca8dffc..33f7444 100644 --- a/src/fd.c +++ b/src/fd.c @@ -354,8 +354,10 @@ void _fd_delete_orphan(int fd) /* perform the close() call last as it's what unlocks the instant reuse * of this FD by any other thread. */ - if (!fd_disown) + if (!fd_disown) { + fdtab[fd].generation++; close(fd); + } _HA_ATOMIC_DEC(&ha_used_fds); } -- 1.7.10.4