Skip to content

Commit 396ef15

Browse files
committed
Fix busy-wait in pgbench, with --rate.
If --rate was used to throttle pgbench, it failed to sleep when it had nothing to do, leading to a busy-wait with 100% CPU usage. This bug was introduced in the refactoring in v10. Before that, sleep() was called with a timeout, even when there were no file descriptors to wait for. Reported by Jeff Janes, patch by Fabien COELHO. Backpatch to v10. Discussion: https://www.postgresql.org/message-id/CAMkU%3D1x5hoX0pLLKPRnXCy0T8uHoDvXdq%2B7kAM9eoC9_z72ucw%40mail.gmail.com
1 parent 2632bcc commit 396ef15

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4578,20 +4578,30 @@ threadRun(void *arg)
45784578
* or it's time to print a progress report. Update input_mask to show
45794579
* which client(s) received data.
45804580
*/
4581-
if (min_usec > 0 && maxsock != -1)
4581+
if (min_usec > 0)
45824582
{
4583-
int nsocks; /* return from select(2) */
4583+
int nsocks = 0; /* return from select(2) if called */
45844584

45854585
if (min_usec != PG_INT64_MAX)
45864586
{
4587-
struct timeval timeout;
4587+
if (maxsock != -1)
4588+
{
4589+
struct timeval timeout;
45884590

4589-
timeout.tv_sec = min_usec / 1000000;
4590-
timeout.tv_usec = min_usec % 1000000;
4591-
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout);
4591+
timeout.tv_sec = min_usec / 1000000;
4592+
timeout.tv_usec = min_usec % 1000000;
4593+
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout);
4594+
}
4595+
else /* nothing active, simple sleep */
4596+
{
4597+
pg_usleep(min_usec);
4598+
}
45924599
}
4593-
else
4600+
else /* no explicit delay, select without timeout */
4601+
{
45944602
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, NULL);
4603+
}
4604+
45954605
if (nsocks < 0)
45964606
{
45974607
if (errno == EINTR)
@@ -4604,7 +4614,7 @@ threadRun(void *arg)
46044614
goto done;
46054615
}
46064616
}
4607-
else
4617+
else /* min_usec == 0, i.e. something needs to be executed */
46084618
{
46094619
/* If we didn't call select(), don't try to read any data */
46104620
FD_ZERO(&input_mask);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy