]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: debug: add a new DEBUG_FD build option
authorWilly Tarreau <w@1wt.eu>
Tue, 23 Jun 2020 08:04:54 +0000 (10:04 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 23 Jun 2020 08:04:54 +0000 (10:04 +0200)
When DEBUG_FD is set at build time, we'll keep a counter of per-FD events
in the fdtab. This counter is reported in "show fd" even for closed FDs if
not zero. The purpose is to help spot situations where an apparently closed
FD continues to be reported in loops, or where some events are dismissed.

Makefile
include/haproxy/fd-t.h
include/haproxy/fd.h
src/cli.c
src/ev_epoll.c
src/ev_evports.c
src/ev_kqueue.c
src/ev_poll.c
src/ev_select.c
src/fd.c

index 9d8d350de43dc1ef33111a48c4118a09958af6dd..d77900931b79769e3a3e3f928a0d7699a32454c7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -213,7 +213,8 @@ SMALL_OPTS =
 # You can enable debugging on specific code parts by setting DEBUG=-DDEBUG_xxx.
 # Currently defined DEBUG macros include DEBUG_FULL, DEBUG_MEMORY, DEBUG_FSM,
 # DEBUG_HASH, DEBUG_AUTH, DEBUG_SPOE, DEBUG_UAF and DEBUG_THREAD, DEBUG_STRICT,
-# DEBUG_DEV. Please check sources for exact meaning or do not use at all.
+# DEBUG_DEV, DEBUG_FD. Please check sources for exact meaning or do not use at
+# all.
 DEBUG =
 
 #### Trace options
index 97b383cebff6c9b4ce53ea988b3e4cdc4586ffaf..6ff8ef135dfc749737729eb6734d8b683b211ce9 100644 (file)
@@ -134,6 +134,9 @@ struct fdtab {
        unsigned char cloned:1;              /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */
        unsigned char initialized:1;         /* 1 if init phase was done on this fd (e.g. set non-blocking) */
        unsigned char et_possible:1;         /* 1 if edge-triggered is possible on this FD */
+#ifdef DEBUG_FD
+       unsigned int event_count;            /* number of events reported */
+#endif
 } THREAD_ALIGNED(64);
 
 /* polled mask, one bit per thread and per direction for each FD */
index f7af4e162dc22c8933aea2dc6993b3d010faeec8..7b77567fa1c5b3aa95d9adf2d4b76d00f24c1a97 100644 (file)
@@ -446,6 +446,9 @@ static inline void fd_insert(int fd, void *owner, void (*iocb)(int fd), unsigned
        fdtab[fd].linger_risk = 0;
        fdtab[fd].cloned = 0;
        fdtab[fd].et_possible = 0;
+#ifdef DEBUG_FD
+       fdtab[fd].event_count = 0;
+#endif
 
        /* conn_fd_handler should support edge-triggered FDs */
        if ((global.tune.options & GTUNE_FD_ET) && fdtab[fd].iocb == conn_fd_handler)
index b8fac13a88fbb60d686323899eecd9252b83063a..b9afc1b20481e172ad44d6b0f248c7bc36b42596 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -1002,7 +1002,14 @@ static int cli_io_handler_show_fd(struct appctx *appctx)
 
                fdt = fdtab[fd];
 
-               if (!fdt.owner)
+               /* When DEBUG_FD is set, we also report closed FDs that have a
+                * non-null event count to detect stuck ones.
+                */
+               if (!fdt.owner
+#ifdef DEBUG_FD
+                   && !fdt.event_count
+#endif
+                   )
                        goto skip; // closed
 
                if (fdt.iocb == conn_fd_handler) {
@@ -1038,7 +1045,10 @@ static int cli_io_handler_show_fd(struct appctx *appctx)
                             fdt.iocb);
                resolve_sym_name(&trash, NULL, fdt.iocb);
 
-               if (fdt.iocb == conn_fd_handler) {
+               if (!fdt.owner) {
+                       chunk_appendf(&trash, ")");
+               }
+               else if (fdt.iocb == conn_fd_handler) {
                        chunk_appendf(&trash, ") back=%d cflg=0x%08x", is_back, conn_flags);
                        if (px)
                                chunk_appendf(&trash, " px=%s", px->id);
@@ -1061,6 +1071,9 @@ static int cli_io_handler_show_fd(struct appctx *appctx)
                                      li->bind_conf->frontend->id);
                }
 
+#ifdef DEBUG_FD
+               chunk_appendf(&trash, " evcnt=%u", fdtab[fd].event_count);
+#endif
                chunk_appendf(&trash, ")\n");
 
                if (ci_putchk(si_ic(si), &trash) == -1) {
index 92c000f85937413e42869d0ce5e4c511565804a4..ba7f2e030f81c23befde4a1c7917970f413dd037 100644 (file)
@@ -222,6 +222,9 @@ static void _do_poll(struct poller *p, int exp, int wake)
                e = epoll_events[count].events;
                fd = epoll_events[count].data.fd;
 
+#ifdef DEBUG_FD
+               _HA_ATOMIC_ADD(&fdtab[fd].event_count, 1);
+#endif
                if (!fdtab[fd].owner) {
                        activity[tid].poll_dead_fd++;
                        continue;
index a00813584f29c1629a253aad93f52ac8ad988ae2..2ad546ff87aa44861cd7b3156ba8451ff910d81d 100644 (file)
@@ -216,6 +216,9 @@ static void _do_poll(struct poller *p, int exp, int wake)
                fd = evports_evlist[i].portev_object;
                events = evports_evlist[i].portev_events;
 
+#ifdef DEBUG_FD
+               _HA_ATOMIC_ADD(&fdtab[fd].event_count, 1);
+#endif
                if (fdtab[fd].owner == NULL) {
                        activity[tid].poll_dead_fd++;
                        continue;
index 539394e7fe769125a1b54111b0b00e9837b3a168..a67d05b267b929ab875075a87c4c7e4f8906a4bf 100644 (file)
@@ -183,6 +183,9 @@ static void _do_poll(struct poller *p, int exp, int wake)
                unsigned int n = 0;
                fd = kev[count].ident;
 
+#ifdef DEBUG_FD
+               _HA_ATOMIC_ADD(&fdtab[fd].event_count, 1);
+#endif
                if (!fdtab[fd].owner) {
                        activity[tid].poll_dead_fd++;
                        continue;
index e63334e14ad881ef16e91d0f7829889f47f5503b..b0b0e2bc4daaa424cc085303e8c513d6978ce399 100644 (file)
@@ -219,6 +219,9 @@ static void _do_poll(struct poller *p, int exp, int wake)
                int e = poll_events[count].revents;
                fd = poll_events[count].fd;
 
+#ifdef DEBUG_FD
+               _HA_ATOMIC_ADD(&fdtab[fd].event_count, 1);
+#endif
                if (!(e & ( POLLOUT | POLLIN | POLLERR | POLLHUP | POLLRDHUP )))
                        continue;
 
index 544b7f21175e9c34d45b84a8765bf28c0988629e..aca1ea8ec15baa60e4fe4492469ad700a7df2a3d 100644 (file)
@@ -197,6 +197,9 @@ static void _do_poll(struct poller *p, int exp, int wake)
                for (count = BITS_PER_INT, fd = fds * BITS_PER_INT; count && fd < maxfd; count--, fd++) {
                        unsigned int n = 0;
 
+#ifdef DEBUG_FD
+                       _HA_ATOMIC_ADD(&fdtab[fd].event_count, 1);
+#endif
                        if (!fdtab[fd].owner) {
                                activity[tid].poll_dead_fd++;
                                continue;
index 60ad69901ea684ab148a2ec7091782a592e53e9e..bf2383e4d7e630c0f5f9d9c70ff2be370f1f385c 100644 (file)
--- a/src/fd.c
+++ b/src/fd.c
@@ -322,6 +322,9 @@ static void fd_dodelete(int fd, int do_close)
 
        fdtab[fd].state = 0;
 
+#ifdef DEBUG_FD
+       fdtab[fd].event_count = 0;
+#endif
        port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port);
        fdinfo[fd].port_range = NULL;
        fdtab[fd].owner = NULL;