From f40d969a45759b400457db00baacf1e19f33eac5 Mon Sep 17 00:00:00 2001 From: Hannu Krosing Date: Sun, 4 Oct 2026 11:24:46 +0000 Subject: [PATCH v4 10/12] Refactor pgbench poller: encapsulate socket_set and add poller API aliases Improve the encapsulation of the socket polling module (poller.c/h): - Move the platform-specific #ifdefs (POLL_USING_PPOLL / POLL_USING_SELECT), system headers ( / ), and struct socket_set definitions out of poller.h and into poller.c, making socket_set an opaque type in the public header. - Add socket_wait_method_name() in poller.c and define SOCKET_WAIT_METHOD in terms of it so callers do not need platform-specific polling macros exposed in poller.h. - Provide PgBenchPoller typedef and poller_* macro aliases (poller_create, poller_destroy, poller_clear, poller_add, poller_wait, poller_has_input) alongside the existing *_socket_set APIs. --- src/bin/pgbench/poller.c | 40 +++++++++++++++++++++++++ src/bin/pgbench/poller.h | 63 +++++++++++++++------------------------- 2 files changed, 64 insertions(+), 39 deletions(-) diff --git a/src/bin/pgbench/poller.c b/src/bin/pgbench/poller.c index 2bfc519d886..246dade7026 100644 --- a/src/bin/pgbench/poller.c +++ b/src/bin/pgbench/poller.c @@ -19,6 +19,17 @@ #include #include +/* For testing, PGBENCH_USE_SELECT can be defined to force use of that code */ +#if defined(HAVE_PPOLL) && !defined(PGBENCH_USE_SELECT) +#define POLL_USING_PPOLL +#ifdef HAVE_POLL_H +#include +#endif +#else /* no ppoll(), so use select() */ +#define POLL_USING_SELECT +#include +#endif + #include "common/logging.h" #include "poller.h" @@ -59,6 +70,21 @@ #ifdef POLL_USING_PPOLL +#define SOCKET_WAIT_METHOD_NAME "ppoll" + +struct socket_set +{ + int maxfds; /* allocated length of pollfds[] array */ + int curfds; /* number currently in use */ + struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER]; +}; + +const char * +socket_wait_method_name(void) +{ + return SOCKET_WAIT_METHOD_NAME; +} + socket_set * alloc_socket_set(int count) { @@ -132,6 +158,20 @@ socket_has_input(socket_set *sa, int fd, int idx) #ifdef POLL_USING_SELECT +#define SOCKET_WAIT_METHOD_NAME "select" + +struct socket_set +{ + int maxfd; /* largest FD currently set in fds */ + fd_set fds; +}; + +const char * +socket_wait_method_name(void) +{ + return SOCKET_WAIT_METHOD_NAME; +} + socket_set * alloc_socket_set(int count) { diff --git a/src/bin/pgbench/poller.h b/src/bin/pgbench/poller.h index 275e77d2137..720691c1c62 100644 --- a/src/bin/pgbench/poller.h +++ b/src/bin/pgbench/poller.h @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * poller.h - * Socket set polling abstraction for pgbench + * Socket multiplexing and event poller abstraction for pgbench * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California @@ -10,52 +10,37 @@ * *------------------------------------------------------------------------- */ -#ifndef POLLER_H -#define POLLER_H -/* For testing, PGBENCH_USE_SELECT can be defined to force use of that code */ -#if defined(HAVE_PPOLL) && !defined(PGBENCH_USE_SELECT) -#define POLL_USING_PPOLL -#ifdef HAVE_POLL_H -#include -#endif -#else /* no ppoll(), so use select() */ -#define POLL_USING_SELECT -#include -#endif +#ifndef PGBENCH_POLLER_H +#define PGBENCH_POLLER_H /* - * Multi-platform socket set implementations + * Opaque socket set / poller handle */ +typedef struct socket_set socket_set; +typedef struct socket_set PgBenchPoller; -#ifdef POLL_USING_PPOLL -#define SOCKET_WAIT_METHOD "ppoll" - -typedef struct socket_set -{ - int maxfds; /* allocated length of pollfds[] array */ - int curfds; /* number currently in use */ - struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER]; -} socket_set; - -#endif /* POLL_USING_PPOLL */ - -#ifdef POLL_USING_SELECT -#define SOCKET_WAIT_METHOD "select" - -typedef struct socket_set -{ - int maxfd; /* largest FD currently set in fds */ - fd_set fds; -} socket_set; - -#endif /* POLL_USING_SELECT */ - +/* + * Lifecycle & polling operations + */ extern socket_set *alloc_socket_set(int count); extern void free_socket_set(socket_set *sa); extern void clear_socket_set(socket_set *sa); extern void add_socket_to_set(socket_set *sa, int fd, int idx); -extern int wait_on_socket_set(socket_set *sa, int64 usecs); +extern int wait_on_socket_set(socket_set *sa, int64 usecs); extern bool socket_has_input(socket_set *sa, int fd, int idx); +extern const char *socket_wait_method_name(void); + +#define SOCKET_WAIT_METHOD (socket_wait_method_name()) -#endif /* POLLER_H */ +/* + * Object-style aliases for PgBenchPoller + */ +#define poller_create(max_sockets) alloc_socket_set(max_sockets) +#define poller_destroy(p) free_socket_set(p) +#define poller_clear(p) clear_socket_set(p) +#define poller_add(p, fd, idx) add_socket_to_set(p, fd, idx) +#define poller_wait(p, usecs) wait_on_socket_set(p, usecs) +#define poller_has_input(p, fd, idx) socket_has_input(p, fd, idx) + +#endif /* PGBENCH_POLLER_H */ -- 2.56.0.385.gd3acb90ef8-goog