Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ba3deee

Browse files
committed
Lift the limitation that # of clients must be a multiple of # of threads
Fabien Coelho
1 parent 8650d16 commit ba3deee

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

doc/src/sgml/ref/pgbench.sgml

+1-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,7 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
326326
<para>
327327
Number of worker threads within <application>pgbench</application>.
328328
Using more than one thread can be helpful on multi-CPU machines.
329-
The number of clients must be a multiple of the number of threads,
330-
since each thread is given the same number of client sessions to manage.
329+
Clients are distributed as evenly as possible among available threads.
331330
Default is 1.
332331
</para>
333332
</listitem>

src/bin/pgbench/pgbench.c

+19-8
Original file line numberDiff line numberDiff line change
@@ -2819,6 +2819,7 @@ main(int argc, char **argv)
28192819
int64 latency_late = 0;
28202820

28212821
int i;
2822+
int nclients_dealt;
28222823

28232824
#ifdef HAVE_GETRLIMIT
28242825
struct rlimit rlim;
@@ -3114,6 +3115,14 @@ main(int argc, char **argv)
31143115
}
31153116
}
31163117

3118+
/*
3119+
* Don't need more threads than there are clients. (This is not merely an
3120+
* optimization; throttle_delay is calculated incorrectly below if some
3121+
* threads have no clients assigned to them.)
3122+
*/
3123+
if (nthreads > nclients)
3124+
nthreads = nclients;
3125+
31173126
/* compute a per thread delay */
31183127
throttle_delay *= nthreads;
31193128

@@ -3153,12 +3162,6 @@ main(int argc, char **argv)
31533162
if (nxacts <= 0 && duration <= 0)
31543163
nxacts = DEFAULT_NXACTS;
31553164

3156-
if (nclients % nthreads != 0)
3157-
{
3158-
fprintf(stderr, "number of clients (%d) must be a multiple of number of threads (%d)\n", nclients, nthreads);
3159-
exit(1);
3160-
}
3161-
31623165
/* --sampling-rate may be used only with -l */
31633166
if (sample_rate > 0.0 && !use_log)
31643167
{
@@ -3359,19 +3362,24 @@ main(int argc, char **argv)
33593362

33603363
/* set up thread data structures */
33613364
threads = (TState *) pg_malloc(sizeof(TState) * nthreads);
3365+
nclients_dealt = 0;
3366+
33623367
for (i = 0; i < nthreads; i++)
33633368
{
33643369
TState *thread = &threads[i];
33653370

33663371
thread->tid = i;
3367-
thread->state = &state[nclients / nthreads * i];
3368-
thread->nstate = nclients / nthreads;
3372+
thread->state = &state[nclients_dealt];
3373+
thread->nstate =
3374+
(nclients - nclients_dealt + nthreads - i - 1) / (nthreads - i);
33693375
thread->random_state[0] = random();
33703376
thread->random_state[1] = random();
33713377
thread->random_state[2] = random();
33723378
thread->throttle_latency_skipped = 0;
33733379
thread->latency_late = 0;
33743380

3381+
nclients_dealt += thread->nstate;
3382+
33753383
if (is_latencies)
33763384
{
33773385
/* Reserve memory for the thread to store per-command latencies */
@@ -3395,6 +3403,9 @@ main(int argc, char **argv)
33953403
}
33963404
}
33973405

3406+
/* all clients must be assigned to a thread */
3407+
Assert(nclients_dealt == nclients);
3408+
33983409
/* get start up time */
33993410
INSTR_TIME_SET_CURRENT(start_time);
34003411

0 commit comments

Comments
 (0)