Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-09-22 Thread Tom Lane
I wrote:
> I'm strongly tempted to just remove the POLL_UNWANTED business
> altogether, as it seems both pointless and unportable on its face.
> Almost by definition, we can't know what "other" bits a given
> implementation might set.
> I'm not entirely following the point of including POLLRDHUP in
> POLL_EVENTS, either.  What's wrong with the traditional solution
> of detecting EOF?

So after studying that a bit longer, I think it's just wrong.
It's not the business of this code to be checking for connection
errors at all; that is libpq's province.  The libpq API specifies
that callers should wait for read-ready on the socket, and nothing
else.  So the only bit we need concern ourselves with is POLLIN.

I also seriously disliked both the details of the abstraction API
and its lack of documentation.  (Other people complained about that
upthread, too.)  So attached is a rewrite attempt.  There's still a
couple of grotty things about it; in particular the ppoll variant of
socket_has_input() knows more than one could wish about how it's being
used.  But I couldn't see a way to make it cleaner without significant
changes to the logic in threadRun, and that didn't seem better.

I think that Andres' concern upthread about iterating over a whole
lot of sockets is somewhat misplaced.  We aren't going to be iterating
over the entire set of client connections, only those being run by a
particular pgbench thread.  So assuming you're using a reasonable ratio
of threads to clients, there won't be very many to look at in any one
thread.  In any case, I'm dubious that we could get much of a win from
some other abstraction for waiting: both of these code paths do work
pretty much proportional to the number of connections the current
thread is responsible for, and it's hard to see how to avoid that.

I've tested this on both Linux and FreeBSD, and it seems to work fine.

I'm reasonably happy with this version of the patch, and would be
ready to commit it, but I thought I'd throw it out for another round
of review if anyone wants to.

regards, tom lane

diff --git a/configure b/configure
index 9b30402..21ecd29 100755
--- a/configure
+++ b/configure
@@ -15093,7 +15093,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 2e60a89..8fe6894 100644
--- a/configure.in
+++ b/configure.in
@@ -1562,7 +1562,7 @@ PGAC_FUNC_WCSTOMBS_L
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
+AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
 
 AC_REPLACE_FUNCS(fseeko)
 case $host_os in
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c..ae81aba 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -28,8 +28,8 @@
  */
 
 #ifdef WIN32
-#define FD_SETSIZE 1024			/* set before winsock2.h is included */
-#endif			/* ! WIN32 */
+#define FD_SETSIZE 1024			/* must set before winsock2.h is included */
+#endif
 
 #include "postgres_fe.h"
 #include "fe_utils/conditional.h"
@@ -45,12 +45,21 @@
 #include 
 #include 
 #include 
+#ifdef HAVE_SYS_RESOURCE_H
+#include 		/* for getrlimit */
+#endif
+
+/* 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
 #ifdef HAVE_SYS_SELECT_H
 #include 
 #endif
-
-#ifdef HAVE_SYS_RESOURCE_H
-#include 		/* for getrlimit */
 #endif
 
 #ifndef M_PI
@@ -71,6 +80,33 @@
 #define MM2_ROT47
 
 /*
+ * Multi-platform socket set implementations
+ */
+
+#ifdef POLL_USING_PPOLL
+#define SOCKET_WAIT_METHOD "ppoll"
+
+typedef struct socket_set
+{
+	int			maxfds;			/* allocated length of pollfds[] array */
+	int			

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-09-22 Thread Tom Lane
Fabien COELHO  writes:
> The patch was not applying cleanly anymore for me, so here is a rebase of 
> your latest version.

The cfbot doesn't like that patch, probably because of the Windows newlines.
Here's a version with regular newlines, and some cosmetic cleanup in the
configure infrastructure.

I haven't looked at the pgbench changes proper yet, but I did quickly
test building on FreeBSD 11, which has ppoll, and it falls over:

pgbench.c:6080:69: error: use of undeclared identifier 'POLLRDHUP'
  ...== -1 || (PQsocket(con) >= 0 && !(sa[idx].revents & POLL_UNWANTED)))
 ^
pgbench.c:6059:24: note: expanded from macro 'POLL_UNWANTED'
#define POLL_UNWANTED (POLLRDHUP|POLLERR|POLLHUP|POLLNVAL)
   ^
pgbench.c:6085:42: error: use of undeclared identifier 'POLLRDHUP'
errno, sa[idx].fd, (sa[idx].revents & POLL_UNWANTED));
  ^
pgbench.c:6059:24: note: expanded from macro 'POLL_UNWANTED'
#define POLL_UNWANTED (POLLRDHUP|POLLERR|POLLHUP|POLLNVAL)
   ^
pgbench.c:6107:19: error: use of undeclared identifier 'POLLRDHUP'
sa[idx].events = POLL_EVENTS;
 ^
pgbench.c:6057:22: note: expanded from macro 'POLL_EVENTS'
#define POLL_EVENTS (POLLRDHUP|POLLIN|POLLPRI)
 ^
3 errors generated.
make[3]: *** [: pgbench.o] Error 1


I'm strongly tempted to just remove the POLL_UNWANTED business
altogether, as it seems both pointless and unportable on its face.
Almost by definition, we can't know what "other" bits a given
implementation might set.

I'm not entirely following the point of including POLLRDHUP in
POLL_EVENTS, either.  What's wrong with the traditional solution
of detecting EOF?

regards, tom lane

diff --git a/configure b/configure
index 9b30402..21ecd29 100755
*** a/configure
--- b/configure
*** fi
*** 15093,15099 
  LIBS_including_readline="$LIBS"
  LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
  
! for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
  do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
  ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
--- 15093,15099 
  LIBS_including_readline="$LIBS"
  LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
  
! for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
  do :
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
  ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 2e60a89..8fe6894 100644
*** a/configure.in
--- b/configure.in
*** PGAC_FUNC_WCSTOMBS_L
*** 1562,1568 
  LIBS_including_readline="$LIBS"
  LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
  
! AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
  
  AC_REPLACE_FUNCS(fseeko)
  case $host_os in
--- 1562,1568 
  LIBS_including_readline="$LIBS"
  LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
  
! AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
  
  AC_REPLACE_FUNCS(fseeko)
  case $host_os in
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c..3d378db 100644
*** a/src/bin/pgbench/pgbench.c
--- b/src/bin/pgbench/pgbench.c
***
*** 45,53 
--- 45,62 
  #include 
  #include 
  #include 
+ #ifndef PGBENCH_USE_SELECT			/* force use of select(2)? */
+ #ifdef HAVE_PPOLL
+ #define POLL_USING_PPOLL
+ #include 
+ #endif
+ #endif
+ #ifndef POLL_USING_PPOLL
+ #define POLL_USING_SELECT
  #ifdef HAVE_SYS_SELECT_H
  #include 
  #endif
+ #endif
  
  #ifdef HAVE_SYS_RESOURCE_H
  #include 		/* for getrlimit */
*** static int	pthread_join(pthread_t th, vo
*** 92,104 
  
  /
   * some configurable parameters */
! 
! /* max number of clients allowed */
  #ifdef FD_SETSIZE
! #define MAXCLIENTS	(FD_SETSIZE - 10)
  #else
! #define MAXCLIENTS	1024
  #endif
  
  #define DEFAULT_INIT_STEPS "dtgvp"	/* default -I setting */
  
--- 101,119 
  
  

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-09-21 Thread Tom Lane
I wrote:
> So ... why exactly is this patch insisting on ppoll() rather than just
> plain poll()?  AFAICS, all you're doing with that is being able to
> specify the timeout in microsec not millisec, which does not really
> justify taking much of a hit in portability, to my mind.

To check into my assumptions here, I did a bit of testing to find out
what ppoll can really do in terms of timeout accuracy.  As best I can
tell, its resolution is on the order of 100 usec on both fairly new
Linux kernels (4.17.19 on Fedora 28) and fairly old ones (2.6.32 on
RHEL6).  So there's not really that much daylight between what you can
do with ppoll() and with poll()'s millisecond resolution.  select(2)
seems to have about the same effective timeout resolution.

Interestingly, select(2)'s timeout resolution is noticeably better than
that, around 10 usec, on recent macOS.  Didn't try other platforms.

Also, I'd misunderstood the portability angle.  Unlike pselect(),
ppoll() is *not* in POSIX.  It started as a Linux-ism, although
I see it's appeared recently in FreeBSD.  That somewhat assuages
my fear of broken implementations on specific platforms, but it
also means that it's going to be far from universally available.

So the question here really boils down to whether you think that a
large set of pgbench connections is interesting on non-Linux platforms.
There's a case to be made that it isn't, perhaps, but I'm not exactly
sold.

On the other hand, while we could certainly make a poll-based code path
within this patch, I'm not quite sure what we'd do with it.  My results
for macOS indicate that using poll rather than select would create a
tradeoff: in return for possibly allowing more clients, there would be
a definite loss in timeout precision.  I don't think that limiting
\sleep commands to ms precision would be so awful, but it's easier
to believe that loss of precision in the transaction dispatch rate for
"--rate" tests could be a problem for some people.  So we might have
to expose the choice of which call to use to users, which seems like
a mess.

So maybe what we've got here --- make it better on Linux, with no
change elsewhere --- is about as good as we can hope for.

Also, I notice that the kevent syscall available on macOS and some
BSDen uses a struct-timespec timeout parameter, ie, theoretical nsec
resolution same as ppoll.  So there's a case to be made that where
we should go for those platforms is to build a kevent-based code
path not a poll-based one.  It'd be unreasonable to insist on this
patch providing that, though.

Anyway, bottom line: my objection on Wednesday was mainly based on
the assumption that we might have to contend with broken ppoll on
some platforms, which now appears to be fallacious.  So, objection
withdrawn.

regards, tom lane



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-09-19 Thread Tom Lane
"Rady, Doug"  writes:
> This patch enables building pgbench to use ppoll() instead of select()
> to allow for more than (FD_SETSIZE - 10) connections.  As implemented,
> when using ppoll(), the only connection limitation is system resources.

So ... why exactly is this patch insisting on ppoll() rather than just
plain poll()?  AFAICS, all you're doing with that is being able to
specify the timeout in microsec not millisec, which does not really
justify taking much of a hit in portability, to my mind.

> “… ppoll() is to poll() as pselect() is to select().  Since the
> existing code uses select(), why not convert to poll() rather than
> ppoll()?”

A moment's glance at our git history will remind you that we attempted
to start using pselect() last year, and the attempt crashed and burned:


Author: Tom Lane 
Branch: master Release: REL_10_BR [64925603c] 2017-04-24 18:29:03 -0400

Revert "Use pselect(2) not select(2), if available, to wait in postmaster's 
loop."

This reverts commit 81069a9efc5a374dd39874a161f456f0fb3afba4.

Buildfarm results suggest that some platforms have versions of pselect(2)
that are not merely non-atomic, but flat out non-functional.  Revert the
use-pselect patch to confirm this diagnosis (and exclude the no-SA_RESTART
patch as the source of trouble).  If it's so, we should probably look into
blacklisting specific platforms that have broken pselect.

Discussion: https://postgr.es/m/9696.1493072...@sss.pgh.pa.us


Now, it might be that ppoll doesn't suffer from as many portability
problems as pselect, but I don't see a good reason to assume that.
So I'd rather see if we can't convert this to use poll(), and thereby
ensure that it works basically everywhere.

regards, tom lane



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-09-15 Thread Fabien COELHO


The author hasn't replied, but the attached seems to have cured the 
bitrot so that it at least applies. Let's see what the cfbot makes of 
it and then possibly fix any Windows issues.


The patch was not applying cleanly anymore for me, so here is a rebase of 
your latest version.


Morever, ISTM that Tom's "why?" question has been answered: there are very 
large systems out there with many processors, which are tested against 
many connections, exceeding select limit.


I have turned back this patch to ready.

--
Fabien.diff --git a/configure b/configure
index c6a44a9078..0456064456 100755
--- a/configure
+++ b/configure
@@ -15060,7 +15060,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 3ada48b5f9..fceba79023 100644
--- a/configure.in
+++ b/configure.in
@@ -1544,7 +1544,7 @@ PGAC_FUNC_WCSTOMBS_L
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
+AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
 
 AC_REPLACE_FUNCS(fseeko)
 case $host_os in
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c089..3d378db714 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -45,9 +45,18 @@
 #include 
 #include 
 #include 
+#ifndef PGBENCH_USE_SELECT			/* force use of select(2)? */
+#ifdef HAVE_PPOLL
+#define POLL_USING_PPOLL
+#include 
+#endif
+#endif
+#ifndef POLL_USING_PPOLL
+#define POLL_USING_SELECT
 #ifdef HAVE_SYS_SELECT_H
 #include 
 #endif
+#endif
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include 		/* for getrlimit */
@@ -92,13 +101,19 @@ static int	pthread_join(pthread_t th, void **thread_return);
 
 /
  * some configurable parameters */
-
-/* max number of clients allowed */
+#ifdef POLL_USING_SELECT	/* using select(2) */
+#define SOCKET_WAIT_METHOD "select"
+typedef fd_set socket_set;
 #ifdef FD_SETSIZE
-#define MAXCLIENTS	(FD_SETSIZE - 10)
+#define MAXCLIENTS	(FD_SETSIZE - 10) /* system limited max number of clients allowed */
 #else
-#define MAXCLIENTS	1024
+#define MAXCLIENTS	1024		/* max number of clients allowed */
 #endif
+#else	/* using ppoll(2) */
+#define SOCKET_WAIT_METHOD "ppoll"
+typedef struct pollfd socket_set;
+#define MAXCLIENTS	-1		/* unlimited number of clients */
+#endif /* POLL_USING_SELECT */
 
 #define DEFAULT_INIT_STEPS "dtgvp"	/* default -I setting */
 
@@ -525,6 +540,13 @@ static void addScript(ParsedScript script);
 static void *threadRun(void *arg);
 static void setalarm(int seconds);
 static void finishCon(CState *st);
+static socket_set *alloc_socket_set(int count);
+static bool error_on_socket(socket_set *sa, int idx, PGconn *con);
+static void free_socket_set(socket_set *sa);
+static bool ignore_socket(socket_set *sa, int idx, PGconn *con);
+static void clear_socket_set(socket_set *sa, int count);
+static void set_socket(socket_set *sa, int fd, int idx);
+static int wait_on_socket_set(socket_set *sa, int nstate, int maxsock, int64 usec);
 
 
 /* callback functions for our flex lexer */
@@ -1143,6 +1165,7 @@ doConnect(void)
 			!have_password)
 		{
 			PQfinish(conn);
+			conn = NULL;
 			simple_prompt("Password: ", password, sizeof(password), false);
 			have_password = true;
 			new_pass = true;
@@ -4903,7 +4926,7 @@ main(int argc, char **argv)
 			case 'c':
 benchmarking_option_set = true;
 nclients = atoi(optarg);
-if (nclients <= 0 || nclients > MAXCLIENTS)
+if (nclients <= 0 || (MAXCLIENTS != -1 && nclients > MAXCLIENTS))
 {
 	fprintf(stderr, "invalid number of clients: \"%s\"\n",
 			optarg);
@@ -5614,6 +5637,7 @@ threadRun(void *arg)
 	int64		next_report = last_report + (int64) progress * 100;
 	StatsData	last,
 aggs;
+	socket_set	*sockets = alloc_socket_set(nstate);
 
 	/*
 	 * Initialize throttling 

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-08-09 Thread Andrew Dunstan




On 08/09/2018 05:46 PM, Andrew Dunstan wrote:



On 08/09/2018 12:45 PM, Andrew Dunstan wrote:



On 07/03/2018 07:52 PM, Andrew Dunstan wrote:



On 05/17/2018 01:23 AM, Thomas Munro wrote:
On Tue, Mar 27, 2018 at 9:23 AM, Rady, Doug  
wrote:

pgbench11-ppoll-v12.patch

Hi Doug,

FYI this patch is trying and failing to use ppoll() on Windows:

https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.30 






It's still failing -  see 



I'm setting this back to "Waiting on Author" until that's fixed.




The author hasn't replied, but the attached seems to have cured the 
bitrot so that it at least applies. Let's see what the cfbot makes of 
it and then possibly fix any Windows issues.







And there's still a Windows problem which I think is cured in the 
attached patch





and the CFBot agrees.

cheers

andrew

--
Andrew Dunstanhttps://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-08-09 Thread Andrew Dunstan



On 08/09/2018 12:45 PM, Andrew Dunstan wrote:



On 07/03/2018 07:52 PM, Andrew Dunstan wrote:



On 05/17/2018 01:23 AM, Thomas Munro wrote:
On Tue, Mar 27, 2018 at 9:23 AM, Rady, Doug  
wrote:

pgbench11-ppoll-v12.patch

Hi Doug,

FYI this patch is trying and failing to use ppoll() on Windows:

https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.30 






It's still failing -  see 



I'm setting this back to "Waiting on Author" until that's fixed.




The author hasn't replied, but the attached seems to have cured the 
bitrot so that it at least applies. Let's see what the cfbot makes of 
it and then possibly fix any Windows issues.







And there's still a Windows problem which I think is cured in the 
attached patch


cheers

andrew


--
Andrew Dunstanhttps://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

diff --git a/configure b/configure
index 2665213..4579003 100755
--- a/configure
+++ b/configure
@@ -14916,7 +14916,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat ppoll pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 397f6bc..bd08068 100644
--- a/configure.in
+++ b/configure.in
@@ -1540,7 +1540,7 @@ PGAC_FUNC_WCSTOMBS_L
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
+AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat ppoll pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
 
 AC_REPLACE_FUNCS(fseeko)
 case $host_os in
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c..3d378db 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -45,9 +45,18 @@
 #include 
 #include 
 #include 
+#ifndef PGBENCH_USE_SELECT			/* force use of select(2)? */
+#ifdef HAVE_PPOLL
+#define POLL_USING_PPOLL
+#include 
+#endif
+#endif
+#ifndef POLL_USING_PPOLL
+#define POLL_USING_SELECT
 #ifdef HAVE_SYS_SELECT_H
 #include 
 #endif
+#endif
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include 		/* for getrlimit */
@@ -92,13 +101,19 @@ static int	pthread_join(pthread_t th, void **thread_return);
 
 /
  * some configurable parameters */
-
-/* max number of clients allowed */
+#ifdef POLL_USING_SELECT	/* using select(2) */
+#define SOCKET_WAIT_METHOD "select"
+typedef fd_set socket_set;
 #ifdef FD_SETSIZE
-#define MAXCLIENTS	(FD_SETSIZE - 10)
+#define MAXCLIENTS	(FD_SETSIZE - 10) /* system limited max number of clients allowed */
 #else
-#define MAXCLIENTS	1024
+#define MAXCLIENTS	1024		/* max number of clients allowed */
 #endif
+#else	/* using ppoll(2) */
+#define SOCKET_WAIT_METHOD "ppoll"
+typedef struct pollfd socket_set;
+#define MAXCLIENTS	-1		/* unlimited number of clients */
+#endif /* POLL_USING_SELECT */
 
 #define DEFAULT_INIT_STEPS "dtgvp"	/* default -I setting */
 
@@ -525,6 +540,13 @@ static void addScript(ParsedScript script);
 static void *threadRun(void *arg);
 static void setalarm(int seconds);
 static void finishCon(CState *st);
+static socket_set *alloc_socket_set(int count);
+static bool error_on_socket(socket_set *sa, int idx, PGconn *con);
+static void free_socket_set(socket_set *sa);
+static bool ignore_socket(socket_set *sa, int idx, PGconn *con);
+static void clear_socket_set(socket_set *sa, int count);
+static void set_socket(socket_set *sa, int fd, int idx);
+static int wait_on_socket_set(socket_set *sa, int nstate, int maxsock, int64 usec);
 
 
 /* callback functions for our flex lexer */
@@ -1143,6 +1165,7 @@ doConnect(void)
 			!have_password)
 		{
 			PQfinish(conn);
+			conn = NULL;
 			simple_prompt("Password: ", password, sizeof(password), false);
 			have_password = true;
 			new_pass = true;
@@ -4903,7 +4926,7 @@ main(int argc, char **argv)
 			case 'c':
 benchmarking_option_set = 

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-08-09 Thread Andrew Dunstan



On 07/03/2018 07:52 PM, Andrew Dunstan wrote:



On 05/17/2018 01:23 AM, Thomas Munro wrote:

On Tue, Mar 27, 2018 at 9:23 AM, Rady, Doug  wrote:

pgbench11-ppoll-v12.patch

Hi Doug,

FYI this patch is trying and failing to use ppoll() on Windows:

https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.30




It's still failing -  see 



I'm setting this back to "Waiting on Author" until that's fixed.




The author hasn't replied, but the attached seems to have cured the 
bitrot so that it at least applies. Let's see what the cfbot makes of it 
and then possibly fix any Windows issues.



cheers

andrew

--
Andrew Dunstanhttps://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

diff --git a/configure b/configure
index 2665213..4579003 100755
--- a/configure
+++ b/configure
@@ -14916,7 +14916,7 @@ fi
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
+for ac_func in cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat ppoll pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/configure.in b/configure.in
index 397f6bc..bd08068 100644
--- a/configure.in
+++ b/configure.in
@@ -1540,7 +1540,7 @@ PGAC_FUNC_WCSTOMBS_L
 LIBS_including_readline="$LIBS"
 LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
 
-AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
+AC_CHECK_FUNCS([cbrt clock_gettime dlopen fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate pstat ppoll pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
 
 AC_REPLACE_FUNCS(fseeko)
 case $host_os in
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 41b756c..e23ba1a 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -45,9 +45,18 @@
 #include 
 #include 
 #include 
+#ifndef PGBENCH_USE_SELECT			/* force use of select(2)? */
+#ifdef HAVE_PPOLL
+#define POLL_USING_PPOLL
+#include 
+#endif
+#endif
+#ifndef POLL_USING_PPOLL
 #ifdef HAVE_SYS_SELECT_H
+#define POLL_USING_SELECT
 #include 
 #endif
+#endif
 
 #ifdef HAVE_SYS_RESOURCE_H
 #include 		/* for getrlimit */
@@ -92,13 +101,19 @@ static int	pthread_join(pthread_t th, void **thread_return);
 
 /
  * some configurable parameters */
-
-/* max number of clients allowed */
+#ifdef POLL_USING_SELECT	/* using select(2) */
+#define SOCKET_WAIT_METHOD "select"
+typedef fd_set socket_set;
 #ifdef FD_SETSIZE
-#define MAXCLIENTS	(FD_SETSIZE - 10)
+#define MAXCLIENTS	(FD_SETSIZE - 10) /* system limited max number of clients allowed */
 #else
-#define MAXCLIENTS	1024
+#define MAXCLIENTS	1024		/* max number of clients allowed */
 #endif
+#else	/* using ppoll(2) */
+#define SOCKET_WAIT_METHOD "ppoll"
+typedef struct pollfd socket_set;
+#define MAXCLIENTS	-1		/* unlimited number of clients */
+#endif /* POLL_USING_SELECT */
 
 #define DEFAULT_INIT_STEPS "dtgvp"	/* default -I setting */
 
@@ -525,6 +540,13 @@ static void addScript(ParsedScript script);
 static void *threadRun(void *arg);
 static void setalarm(int seconds);
 static void finishCon(CState *st);
+static socket_set *alloc_socket_set(int count);
+static bool error_on_socket(socket_set *sa, int idx, PGconn *con);
+static void free_socket_set(socket_set *sa);
+static bool ignore_socket(socket_set *sa, int idx, PGconn *con);
+static void clear_socket_set(socket_set *sa, int count);
+static void set_socket(socket_set *sa, int fd, int idx);
+static int wait_on_socket_set(socket_set *sa, int nstate, int maxsock, int64 usec);
 
 
 /* callback functions for our flex lexer */
@@ -1143,6 +1165,7 @@ doConnect(void)
 			!have_password)
 		{
 			PQfinish(conn);
+			conn = NULL;
 			simple_prompt("Password: ", password, sizeof(password), false);
 			have_password = true;
 			new_pass = true;
@@ -4903,7 +4926,7 @@ main(int argc, char **argv)
 			case 'c':
 benchmarking_option_set = true;
 nclients = atoi(optarg);
-if (nclients <= 0 || nclients > MAXCLIENTS)
+if (nclients <= 0 || (MAXCLIENTS != -1 && nclients > 

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-07-03 Thread Andrew Dunstan




On 05/17/2018 01:23 AM, Thomas Munro wrote:

On Tue, Mar 27, 2018 at 9:23 AM, Rady, Doug  wrote:

pgbench11-ppoll-v12.patch

Hi Doug,

FYI this patch is trying and failing to use ppoll() on Windows:

https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.30




It's still failing -  see 



I'm setting this back to "Waiting on Author" until that's fixed.

cheers

andrew

--
Andrew Dunstanhttps://www.2ndQuadrant.com
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services




Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-05-16 Thread Thomas Munro
On Tue, Mar 27, 2018 at 9:23 AM, Rady, Doug  wrote:
> pgbench11-ppoll-v12.patch

Hi Doug,

FYI this patch is trying and failing to use ppoll() on Windows:

https://ci.appveyor.com/project/postgresql-cfbot/postgresql/build/1.0.30

-- 
Thomas Munro
http://www.enterprisedb.com



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-04-06 Thread konstantin knizhnik

On Apr 7, 2018, at 12:49 AM, Tom Lane wrote:

> Andres Freund  writes:
>> I'm still not particularly happy with this.
> 
> I'm a bit confused as to what the point is.  It seems unlikely that one
> pgbench process can effectively drive enough backends for select's
> limitations to really be an issue.

pgbench is multithreaded application, so in theory it can drive almost 
arbitrary number of connections.
It is limited only by network throughput, but if pgbench is launched at the 
same host and connected  to the server through loopback or unix sockets,
then network is also not a limit.
We quite often have to spawn more than 1k connections and SMP systems with 
hundreds of CPU.
So there are two choices: either use patched version of pgbench which is using 
poll, either spawn several instances of pgbench (which is not always 
convenient).

> 
>   regards, tom lane
> 




Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-04-06 Thread Tom Lane
Andres Freund  writes:
> I'm still not particularly happy with this.

I'm a bit confused as to what the point is.  It seems unlikely that one
pgbench process can effectively drive enough backends for select's
limitations to really be an issue.

regards, tom lane



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-04-06 Thread Andres Freund
Hi,

I'm still not particularly happy with this.  Checking whether I can
polish it up.

a) the new function names are partially non-descriptive and their
   meaning is undocumented.  As an extreme example:

-   if (!FD_ISSET(sock, _mask))
+   if (ignore_socket(sockets, i, st->con))
continue;

   reading the new code it's entirely unclear what that could mean. Are
   you marking the socket as ignored? What does ignored even mean?

   There's not a single comment on what the new functions mean. It's not
   that bad if there's no docs on what FD_ISSET implies, because that's a
   well known and documented interface. But introducing an abstraction
   without any comments on it?

b) Does this actually improve the situation all that much? We still loop
   over every socket:

/* ok, advance the state machine of each connection */
for (i = 0; i < nstate; i++)
{
CState *st = [i];

if (st->state == CSTATE_WAIT_RESULT)
{
/* don't call doCustom unless data is available 
*/

if (error_on_socket(sockets, i, st->con))
goto done;

if (ignore_socket(sockets, i, st->con))
continue;
}
else if (st->state == CSTATE_FINISHED ||
 st->state == CSTATE_ABORTED)
{
/* this client is done, no need to consider it 
anymore */
continue;
}

doCustom(thread, st, );

/* If doCustom changed client to finished state, reduce 
remains */
if (st->state == CSTATE_FINISHED || st->state == 
CSTATE_ABORTED)
remains--;
}

   if the goal is to make this more scalable, wouldn't this require
   using a proper polling mechanism that supports signalling the
   sockets with relevant changes, rather than busy-looping through every
   socket if there's just a single change?

   I kinda wonder if the proper fix wouldn't be to have one patch making
   WaitEventSets usable from frontend code, and then make this code use
   them.  Not a small project though.

Greetings,

Andres Freund



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-03-27 Thread Fabien COELHO


Hello Doug,

I've compiled and run with both ppoll & select options without issues. 
Two quite minor style comment (at least 2 instances):


Fixed. Fixed. Also fixed issue with 'timeout' not being passed as NULL 
when no timeout time.


v12 compiled and tested with both ppoll & select (by forcing). All seems 
ok to me.


Switched back to "ready".

--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-03-25 Thread Fabien COELHO


Hello Doug,


Updated the patch to not do the #undef
pgbench11-ppoll-v11.patch attached.


Patch applies. Do not forget to regenerate configure to test...

I've compiled and run with both ppoll & select options without issues.

Two quite minor style comment (at least 2 instances):

  if (cond) return false; else return true;

ISTM that the simpler the better:

  return !cond;

Also ISTM that the following does not comply with pg C style expectations 
(idem, 2 instances):


  } else {


--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-03-03 Thread Andres Freund
On 2018-03-01 11:30:39 +0100, Fabien COELHO wrote:
> 
> > > -#ifdef HAVE_SYS_SELECT_H
> > > +#ifdef PGBENCH_USE_SELECT/* force use of 
> > > select(2)? */
> > > +#undef HAVE_PPOLL
> > > +#endif
> > > +#ifdef HAVE_PPOLL
> > > +#include 
> > > +#elif defined(HAVE_SYS_SELECT_H)
> > > +#define POLL_USING_SELECT
> > 
> > (random thing noticed while going through patches)
> > 
> > It strikes me as a bad idea to undefine configure selected
> > symbols. Postgres header might rely on them. It also strikes me as
> > entirely unnecessary here.
> 
> Yes, I though about this one but let it pass. Indeed, it would be sufficient
> to not load "poll.h" when select is forced, without undefining the configure
> setting.

I've marked the CF entry waiting on author.

Greetings,

Andres Freund



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-03-01 Thread Fabien COELHO



-#ifdef HAVE_SYS_SELECT_H
+#ifdef PGBENCH_USE_SELECT  /* force use of select(2)? */
+#undef HAVE_PPOLL
+#endif
+#ifdef HAVE_PPOLL
+#include 
+#elif defined(HAVE_SYS_SELECT_H)
+#define POLL_USING_SELECT


(random thing noticed while going through patches)

It strikes me as a bad idea to undefine configure selected
symbols. Postgres header might rely on them. It also strikes me as
entirely unnecessary here.


Yes, I though about this one but let it pass. Indeed, it would be 
sufficient to not load "poll.h" when select is forced, without undefining 
the configure setting.


--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-03-01 Thread Andres Freund
On 2018-01-28 23:02:57 +, Rady, Doug wrote:
> diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
> index 31ea6ca06e..689f15a772 100644
> --- a/src/bin/pgbench/pgbench.c
> +++ b/src/bin/pgbench/pgbench.c
> @@ -44,7 +44,13 @@
>  #include 
>  #include 
>  #include 
> -#ifdef HAVE_SYS_SELECT_H
> +#ifdef PGBENCH_USE_SELECT/* force use of select(2)? */
> +#undef HAVE_PPOLL
> +#endif
> +#ifdef HAVE_PPOLL
> +#include 
> +#elif defined(HAVE_SYS_SELECT_H)
> +#define POLL_USING_SELECT

(random thing noticed while going through patches)

It strikes me as a bad idea to undefine configure selected
symbols. Postgres header might rely on them. It also strikes me as
entirely unnecessary here.

- Andres



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-29 Thread Fabien COELHO


Hello Doug,


   I'm not sure why you do the following trick, could you explain?
  +#undef USE_SELECT
  +#define USE_SELECT

This was due to compiler complaint about USE_SELECT being redefined.
Have replaced that "trick" with a new #define POLL_USING_SELECT which is used 
elsewhere in pgbench instead.


Ok, why not.

Another option to avoid the warning and a new name could have been to 
"#ifndef X #define X #endif /* !X */"


Patch applies cleanly, compiles cleanly for both options, local & global 
"make check" ok.


Switched to "Ready".

--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-26 Thread Fabien COELHO


Hello Doug,

Patch applies, compiles, tests ok.


   > [...] Replaced USE_PPOLL with HAVE_PPOLL as having both seems redundant.

   I'm okay with that. I'm wondering whether there should be a way to force
   using one or the other when both are available. Not sure.

Added option to force use of select(2) via:  -DUSE_SELECT


USE_SELECT could mean something somewhere. Maybe use something more 
specific like PGBENCH_USE_SELECT? Having this macro available simplifies 
testing.


I'm not sure why you do the following trick, could you explain?
  +#undef USE_SELECT
  +#define USE_SELECT

In the select implementation you do:

 return (socket_set *) pg_malloc0(sizeof(socket_set) * MAXCLIENTS);

but ISTM that socket_set is already an fd_set which represents a set of 
clients, so allocating a number of it is needed. The initial 
implementation just does "fs_set input_mask", whetever the number of 
clients, and it works fine.


--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-25 Thread Fabien COELHO


Hello Doug,


This time with the revised patch file:  pgbench11-ppoll-v8.patch


Patch applies cleanly. Compiles cleanly and runs fine in both ppoll & 
select cases.


I'm okay with having a preferred ppoll implementation because of its improved
capability.

A few minor additional comments/suggestions:

Cpp has an #elif that could be used to manage the ppoll/select alternative.
It is already used elsewhere in the file. Or not.

I must admit that I'm not fond of the alloc_socket_set trick with MAXCLIENTS,
especially without any comment. I'd suggest to just have two distinct functions
in their corresponding sections.

I would add a comment that free_socket_set code is common to both 
versions, and maybe consider moving it afterwards. Or maybe just duplicate 
if in each section for homogeneity.


It looks like error_on_socket and ignore_socket should return a boolean instead
of an int. Also, maybe simplify the implementation of the later by avoiding
the ?: expression.

ISTM that the error_on_socket function in the ppoll case deserves some 
comments, especially on the condition.




[...] Replaced USE_PPOLL with HAVE_PPOLL as having both seems redundant.


I'm okay with that. I'm wondering whether there should be a way to force 
using one or the other when both are available. Not sure.


--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-25 Thread Fabien COELHO


ISTM that the v7 patch version you sent is identical to v6.

--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-24 Thread Fabien COELHO


Hello Doug,

This version of the patch attempts to address the feedback for the 
previous submission on 28-Nov-2017


Please avoid recreating a thread, but rather respond to the previous one, 
I missed this post.


The overall function-based implementation with limited ifdefs seems 
readable and maintainable. I think that it rather improves the code in 
places by hiding details.


Patch applies with one warning:

  pgbench11-ppoll-v6.patch:141: trailing whitespace.
 set_socket(sockets, PQsocket(state[i].con), i);
  warning: 1 line adds whitespace errors.

The patch implies to run "autoconf" to regenerate the configure script.

Compilation with "ppoll" ok, globale & local make check ok. I do not have 
hardware which allows to run with over 1024 clients, so I cannot say that 
I tested the case.


Compilation without "ppoll" gets a warning:

  pgbench.c:5645:1: warning: ‘clear_socket’ defined but not used...
clear_socket(socket_array *sa, int fd, int idx)

The "clear_socket" function is not used in this case. I'd suggest to 
remove it from the prototypes, remove or comment the unused implementation 
in the select section, and keep the one in the ppoll section. Or use it if 
it is needed. Or inline it in the "init_socket_array" function where it is 
used just once.


I'm not sure of the name "socket_array", because it is an array only in 
the ppoll case. Maybe "sockets_t" or "socket_set"? Or something else?


Maybe "init_socket_array" should be named "clear_...".

I would suggest not to include sys/select.h when using ppoll, as it is a useless
include this case. I.e. move includes in the ifdef USE_PPOLL section?

Please do not remove comments, eg:

  -  /* identify which client sockets should be checked for input */

On #endif or #else, especially large scope ones, I would have a comment to 
say about what it is, eg at the minimum:

  #else /* select(2) implementation */
  #endif /* USE_PPOLL */

On:
 +#if defined(USE_PPOLL)
 +#ifdef POLLRDHUP

Use just one "ifdef" style?

There should be a comment about what this sections are providing, eg:
  /* ppoll(2) implementation for "socket_array" functions */

There should be an empty line and possibly a comment explaining why
POLLRDHUP may not be there and/or why this define is necessary.

With select you always initialize the timeout, but not with ppoll.
Use a common approach in the implementations?

The "finishCon" function addition seems totally unrelated to this patch. 
Although I agree that this function improves the code, it is refactoring 
and does not really belong here.


--
Fabien.

Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2018-01-06 Thread Stephen Frost
Doug,

* Rady, Doug (radyd...@amazon.com) wrote:
> This patch enables building pgbench to use ppoll() instead of select()
> to allow for more than (FD_SETSIZE - 10) connections.  As implemented,
> when using ppoll(), the only connection limitation is system resources.

Looks like this patch had some good feedback back when it was posted,
but we haven't seen any update to it based on that feedback and we're
now nearly a week into the January commitfest.

In order to keep the commitfest moving and to give you the best
opportunity at having the patch be reviewed and committed during this
cycle, I'd suggest trying to find some time soon to incorporate the
recommendations provided and post an updated patch for review.

Thanks!

Stephen


signature.asc
Description: PGP signature


Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2017-11-29 Thread Fabien COELHO


[...] Yeah, that sort of style would be OK with me.  But I wouldn't 
like:


struct blah {
#ifdef FOO
   int doohicky;
#else
   char *doohicky;
};


Indeed. Me neither.

--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2017-11-29 Thread Robert Haas
On Wed, Nov 29, 2017 at 3:40 AM, Fabien COELHO  wrote:
> ever is best. Not sure that "pfds" is the right name. If the two variables
> means the same thing, they should have the same name, although possibly
> different types.

Although I agree with a good bit of what you say here, I don't agree
with that.  If the member used by ppoll() (or just poll()) has a
different name than the one used for select(), it's much easier to,
say, grep for everyplace where the field you care about is used.  If
you use the same name for different things, that doesn't work.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2017-11-29 Thread Fabien COELHO


Hello,


This patch enables building pgbench to use ppoll() instead of select()
to allow for more than (FD_SETSIZE - 10) connections.  As implemented,
when using ppoll(), the only connection limitation is system resources.


I'm fine with allowing more clients through ppoll, as large 
multi/many/whatever-core hardware becomes more common and is expected to 
grow.


However, I'm at odds with the how and the ppoll/select compatibility layer 
offered by the patch, and the implied maintenance cost just to understand 
what is happening while reading the code cluttered with possibly empty 
macros and ifdef stuff. There are many ifdef, many strange/astute macros, 
significant dissymetry in the code, which reduce maintainability 
noticeably.


I think that it should abstract cleanly the two implementations into a set 
of functions with the same signatures, even if some arguments are unsused, 
and use these functions without ifdef stuff later. Maybe some macros too, 
ok, but not too much.


"SCKTWTMTHD"... WTH? I do not want to know what it means. I'm sure a 
better name (if something without vowels can be a name of anything in 
English...) can be found. Names must be chosen with great care.


MAXCLIENTS: could be set to -1 when there is no limit, and tested to avoid 
one ifdef.


I must admit that I do not see the benefit of "{ do { ... } while(0); }"
compared to "{ ... }". Maybe it has to do with compiler warnings.


The patch has been implemented to introduce a minimal of #ifdef/#ifndef
clutter in the code.


I think that it could be much more minimal than that. I would aim at 
having just one.


 #ifdef USE_PPOLL
 specific functions definitions
 some macros
 #else /* SELECT */
 idem for select
 #endif

Ok, it may not be the best solution everywhere, but it should be 
significantly reduce compared to the current state.


ISTM that ifdef which breaks the code structure should be avoided, eg

 #ifdef ...
 if (...)
 #else
 if (...)
 #endif
 {
// condition was true...

It is unclear to me why the input_mask is declared in the threadRun 
function (so is thread specific) but pfds is in each thread struct (so is 
also thread specific, but differently). The same logic should be used for 
both, which ever is best. Not sure that "pfds" is the right name. If the 
two variables means the same thing, they should have the same name, 
although possibly different types.



$ pgbench -j 3000 -c 1500


Probably you intended 1500 threads for 3000 clients.

--
Fabien.



Re: PATCH: pgbench - option to build using ppoll() for larger connection counts

2017-11-28 Thread Robert Haas
On Tue, Nov 28, 2017 at 10:38 AM, Rady, Doug  wrote:
> This patch enables building pgbench to use ppoll() instead of select()
> to allow for more than (FD_SETSIZE - 10) connections.  As implemented,
> when using ppoll(), the only connection limitation is system resources.

IIUC, ppoll() is to poll() as pselect() is to select().  Since the
existing code uses select(), why not convert to poll() rather than
ppoll()?

+#ifdef USE_PPOLL
+#define SCKTWTMTHD "ppoll"
+#undef MAXCLIENTS
+#define POLL_EVENTS (POLLIN|POLLPRI|POLLRDHUP)
+#define POLL_FAIL (POLLERR|POLLHUP|POLLNVAL|POLLRDHUP)
+#define PFD_RESETEV(s) { do { if ((s)->pfdp) { (s)->pfdp->events =
POLL_EVENTS; (s)->pfdp->revents = 0; } } while(0); }
+#define PFD_ZERO(s) { do { if ((s)->pfdp) { (s)->pfdp->fd = -1;
(s)->pfdp->events = POLL_EVENTS; (s)->pfdp->revents = 0; } } while(0);
}
+#define PFD_SETFD(s) { do { (s)->pfdp->fd = PQsocket((s)->con); } while(0); }
+#define PFD_STRUCT_POLLFD(p)struct pollfd   (p);
+#define PFD_THREAD_FREE(t) { do { if ((t)->pfds) {
pg_free((t)->pfds); (t)->pfds = NULL; } } while (0); }
+#define PFD_THREAD_INIT(t,s,n) { do { int _i; (t)->pfds = (struct
pollfd *) pg_malloc0(sizeof(struct pollfd) * (n)); for (_i = 0; _i <
(n); _i++) { (s)[_i].pfdp = &(t)->pfds[_i]; (s)[_i].pfdp->fd = -1; } }
while (0); }
+#else
+#define SCKTWTMTHD "select"
+#define PFD_RESETEV(s)
+#define PFD_ZERO(s)
+#define PFD_SETFD(s)
+#define PFD_STRUCT_POLLFD(p)
+#define PFD_THREAD_FREE(t)
+#define PFD_THREAD_INIT(t,s,n)
+#endif

I find this an impenetrable mess.  I am not going to commit a macro
with a ten-letter name that contains neither punctuation marks nor
vowels, nor one that is a single 200+ character that embeds a malloc
and a loop call but no comments.  While I agree with the general goal
of avoiding introducing lots of #ifdefs, and while I think that the
introduction of finishCon() seems like a possibly-useful step in that
direction, I don't think that having a bunch of long, complex,
comment-free macros like this actually improves code clarity.  Better
to have some embedded #ifdefs than this.

+if (debug)
+fprintf(stderr, "client %d connecting ...\n",
+st->id);
+

This is unrelated to the purpose of the patch.  Please don't mix in
unrelated changes.

+if (debug)
+fprintf(stderr, "client %d connecting\n",
+state[i].id);

Ditto.

IMHO, this looks like a broadly reasonable thing to do.  I had no idea
that FD_SETSIZE was ever set to a value much less than the number of
FDs the system could handle -- but if it is, then this seems like a
reasonable fix, once we agree on exactly how the details should look.

I don't remember off-hand whether you've done testing to see how
poll()/ppoll() performs compares to select(), but that might also be
something to check.

-- 
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company