Skip to content

Commit babf185

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 2aab702 commit babf185

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
@@ -4539,20 +4539,30 @@ threadRun(void *arg)
45394539
* or it's time to print a progress report. Update input_mask to show
45404540
* which client(s) received data.
45414541
*/
4542-
if (min_usec > 0 && maxsock != -1)
4542+
if (min_usec > 0)
45434543
{
4544-
int nsocks; /* return from select(2) */
4544+
int nsocks = 0; /* return from select(2) if called */
45454545

45464546
if (min_usec != PG_INT64_MAX)
45474547
{
4548-
struct timeval timeout;
4548+
if (maxsock != -1)
4549+
{
4550+
struct timeval timeout;
45494551

4550-
timeout.tv_sec = min_usec / 1000000;
4551-
timeout.tv_usec = min_usec % 1000000;
4552-
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout);
4552+
timeout.tv_sec = min_usec / 1000000;
4553+
timeout.tv_usec = min_usec % 1000000;
4554+
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout);
4555+
}
4556+
else /* nothing active, simple sleep */
4557+
{
4558+
pg_usleep(min_usec);
4559+
}
45534560
}
4554-
else
4561+
else /* no explicit delay, select without timeout */
4562+
{
45554563
nsocks = select(maxsock + 1, &input_mask, NULL, NULL, NULL);
4564+
}
4565+
45564566
if (nsocks < 0)
45574567
{
45584568
if (errno == EINTR)
@@ -4565,7 +4575,7 @@ threadRun(void *arg)
45654575
goto done;
45664576
}
45674577
}
4568-
else
4578+
else /* min_usec == 0, i.e. something needs to be executed */
45694579
{
45704580
/* If we didn't call select(), don't try to read any data */
45714581
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