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

Commit 6b45e3b

Browse files
committed
Arrange to generate different random sequences in the different child
processes of a pgbench run, when we are using -j > 1 and are emulating threads via fork(). Otherwise the children all inherit the same random sequence state and produce the same random-number sequence. In the threaded case the different threads will share one RNG state, so they will produce different subsets of one sequence, which is maybe more correlated than a purist would like but will not be "the same". So we leave that case alone. First noticed by Takahiro Itagaki, and is also part of the explanation for the pgbench misbehavior recently reported by Jaime Casanova.
1 parent d8e511f commit 6b45e3b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

contrib/pgbench/pgbench.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* A simple benchmark program for PostgreSQL
55
* Originally written by Tatsuo Ishii and enhanced by many contributors.
66
*
7-
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.91 2009/09/10 13:59:57 ishii Exp $
7+
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.92 2009/12/11 21:50:06 tgl Exp $
88
* Copyright (c) 2000-2009, PostgreSQL Global Development Group
99
* ALL RIGHTS RESERVED;
1010
*
@@ -1916,7 +1916,7 @@ main(int argc, char **argv)
19161916
INSTR_TIME_SET_CURRENT(start_time);
19171917
srandom((unsigned int) INSTR_TIME_GET_MICROSEC(start_time));
19181918

1919-
/* process bultin SQL scripts */
1919+
/* process builtin SQL scripts */
19201920
switch (ttype)
19211921
{
19221922
case 0:
@@ -2201,6 +2201,7 @@ pthread_create(pthread_t *thread,
22012201
{
22022202
fork_pthread *th;
22032203
void *ret;
2204+
instr_time start_time;
22042205

22052206
th = (fork_pthread *) malloc(sizeof(fork_pthread));
22062207
pipe(th->pipes);
@@ -2211,20 +2212,31 @@ pthread_create(pthread_t *thread,
22112212
free(th);
22122213
return errno;
22132214
}
2214-
if (th->pid != 0) /* parent process */
2215+
if (th->pid != 0) /* in parent process */
22152216
{
22162217
close(th->pipes[1]);
22172218
*thread = th;
22182219
return 0;
22192220
}
22202221

2221-
/* child process */
2222+
/* in child process */
22222223
close(th->pipes[0]);
22232224

22242225
/* set alarm again because the child does not inherit timers */
22252226
if (duration > 0)
22262227
setalarm(duration);
22272228

2229+
/*
2230+
* Set a different random seed in each child process. Otherwise they
2231+
* all inherit the parent's state and generate the same "random"
2232+
* sequence. (In the threaded case, the different threads will obtain
2233+
* subsets of the output of a single random() sequence, which should be
2234+
* okay for our purposes.)
2235+
*/
2236+
INSTR_TIME_SET_CURRENT(start_time);
2237+
srandom(((unsigned int) INSTR_TIME_GET_MICROSEC(start_time)) +
2238+
((unsigned int) getpid()));
2239+
22282240
ret = start_routine(arg);
22292241
write(th->pipes[1], ret, sizeof(TResult));
22302242
close(th->pipes[1]);

0 commit comments

Comments
 (0)