@@ -2819,6 +2819,7 @@ main(int argc, char **argv)
28192819int64 latency_late = 0 ;
28202820
28212821int i ;
2822+ int nclients_dealt ;
28222823
28232824#ifdef HAVE_GETRLIMIT
28242825struct 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 */
31183127throttle_delay *=nthreads ;
31193128
@@ -3153,12 +3162,6 @@ main(int argc, char **argv)
31533162if (nxacts <=0 && duration <=0 )
31543163nxacts = 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 */
31633166if (sample_rate > 0.0 && !use_log )
31643167{
@@ -3359,19 +3362,24 @@ main(int argc, char **argv)
33593362
33603363/* set up thread data structures */
33613364threads = (TState * )pg_malloc (sizeof (TState )* nthreads );
3365+ nclients_dealt = 0 ;
3366+
33623367for (i = 0 ;i < nthreads ;i ++ )
33633368{
33643369TState * thread = & threads [i ];
33653370
33663371thread -> 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 );
33693375thread -> random_state [0 ]= random ();
33703376thread -> random_state [1 ]= random ();
33713377thread -> random_state [2 ]= random ();
33723378thread -> throttle_latency_skipped = 0 ;
33733379thread -> latency_late = 0 ;
33743380
3381+ nclients_dealt += thread -> nstate ;
3382+
33753383if (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 */
33993410INSTR_TIME_SET_CURRENT (start_time );
34003411