@@ -256,6 +256,7 @@ typedef struct
256
256
int nstate ; /* length of state[] */
257
257
unsigned short random_state [3 ]; /* separate randomness for each thread */
258
258
int64 throttle_trigger ; /* previous/next throttling (us) */
259
+ FILE * logfile ; /* where to log, or NULL */
259
260
260
261
/* per thread collected stats */
261
262
instr_time start_time ; /* thread start time */
@@ -366,8 +367,8 @@ static void setalarm(int seconds);
366
367
static void * threadRun (void * arg );
367
368
368
369
static void processXactStats (TState * thread , CState * st , instr_time * now ,
369
- bool skipped , FILE * logfile , StatsData * agg );
370
- static void doLog (TState * thread , CState * st , FILE * logfile , instr_time * now ,
370
+ bool skipped , StatsData * agg );
371
+ static void doLog (TState * thread , CState * st , instr_time * now ,
371
372
StatsData * agg , bool skipped , double latency , double lag );
372
373
373
374
@@ -1246,7 +1247,7 @@ chooseScript(TState *thread)
1246
1247
1247
1248
/* return false iff client should be disconnected */
1248
1249
static bool
1249
- doCustom (TState * thread , CState * st , FILE * logfile , StatsData * agg )
1250
+ doCustom (TState * thread , CState * st , StatsData * agg )
1250
1251
{
1251
1252
PGresult * res ;
1252
1253
Command * * commands ;
@@ -1300,7 +1301,7 @@ doCustom(TState *thread, CState *st, FILE *logfile, StatsData *agg)
1300
1301
now_us = INSTR_TIME_GET_MICROSEC (now );
1301
1302
while (thread -> throttle_trigger < now_us - latency_limit )
1302
1303
{
1303
- processXactStats (thread , st , & now , true, logfile , agg );
1304
+ processXactStats (thread , st , & now , true, agg );
1304
1305
/* next rendez-vous */
1305
1306
wait = getPoissonRand (thread , throttle_delay );
1306
1307
thread -> throttle_trigger += wait ;
@@ -1361,8 +1362,8 @@ doCustom(TState *thread, CState *st, FILE *logfile, StatsData *agg)
1361
1362
if (commands [st -> state + 1 ] == NULL )
1362
1363
{
1363
1364
if (progress || throttle_delay || latency_limit ||
1364
- per_script_stats || logfile )
1365
- processXactStats (thread , st , & now , false, logfile , agg );
1365
+ per_script_stats || use_log )
1366
+ processXactStats (thread , st , & now , false, agg );
1366
1367
else
1367
1368
thread -> stats .cnt ++ ;
1368
1369
}
@@ -1454,7 +1455,8 @@ doCustom(TState *thread, CState *st, FILE *logfile, StatsData *agg)
1454
1455
}
1455
1456
1456
1457
/* Record transaction start time under logging, progress or throttling */
1457
- if ((logfile || progress || throttle_delay || latency_limit || per_script_stats ) && st -> state == 0 )
1458
+ if ((use_log || progress || throttle_delay || latency_limit ||
1459
+ per_script_stats ) && st -> state == 0 )
1458
1460
{
1459
1461
INSTR_TIME_SET_CURRENT (st -> txn_begin );
1460
1462
@@ -1794,9 +1796,13 @@ doCustom(TState *thread, CState *st, FILE *logfile, StatsData *agg)
1794
1796
* print log entry after completing one transaction.
1795
1797
*/
1796
1798
static void
1797
- doLog (TState * thread , CState * st , FILE * logfile , instr_time * now ,
1799
+ doLog (TState * thread , CState * st , instr_time * now ,
1798
1800
StatsData * agg , bool skipped , double latency , double lag )
1799
1801
{
1802
+ FILE * logfile = thread -> logfile ;
1803
+
1804
+ Assert (use_log );
1805
+
1800
1806
/*
1801
1807
* Skip the log entry if sampling is enabled and this row doesn't belong
1802
1808
* to the random sample.
@@ -1879,7 +1885,7 @@ doLog(TState *thread, CState *st, FILE *logfile, instr_time *now,
1879
1885
*/
1880
1886
static void
1881
1887
processXactStats (TState * thread , CState * st , instr_time * now ,
1882
- bool skipped , FILE * logfile , StatsData * agg )
1888
+ bool skipped , StatsData * agg )
1883
1889
{
1884
1890
double latency = 0.0 ,
1885
1891
lag = 0.0 ;
@@ -1906,7 +1912,7 @@ processXactStats(TState *thread, CState *st, instr_time *now,
1906
1912
thread -> stats .cnt ++ ;
1907
1913
1908
1914
if (use_log )
1909
- doLog (thread , st , logfile , now , agg , skipped , latency , lag );
1915
+ doLog (thread , st , now , agg , skipped , latency , lag );
1910
1916
1911
1917
/* XXX could use a mutex here, but we choose not to */
1912
1918
if (per_script_stats )
@@ -3289,7 +3295,7 @@ main(int argc, char **argv)
3289
3295
exit (1 );
3290
3296
}
3291
3297
3292
- /* --sampling-rate may must not be used with --aggregate-interval */
3298
+ /* --sampling-rate may not be used with --aggregate-interval */
3293
3299
if (sample_rate > 0.0 && agg_interval > 0 )
3294
3300
{
3295
3301
fprintf (stderr , "log sampling (--sampling-rate) and aggregation (--aggregate-interval) cannot be used at the same time\n" );
@@ -3460,6 +3466,7 @@ main(int argc, char **argv)
3460
3466
thread -> random_state [0 ] = random ();
3461
3467
thread -> random_state [1 ] = random ();
3462
3468
thread -> random_state [2 ] = random ();
3469
+ thread -> logfile = NULL ; /* filled in later */
3463
3470
thread -> latency_late = 0 ;
3464
3471
initStats (& thread -> stats , 0.0 );
3465
3472
@@ -3555,7 +3562,6 @@ threadRun(void *arg)
3555
3562
{
3556
3563
TState * thread = (TState * ) arg ;
3557
3564
CState * state = thread -> state ;
3558
- FILE * logfile = NULL ; /* per-thread log file */
3559
3565
instr_time start ,
3560
3566
end ;
3561
3567
int nstate = thread -> nstate ;
@@ -3589,9 +3595,9 @@ threadRun(void *arg)
3589
3595
snprintf (logpath , sizeof (logpath ), "pgbench_log.%d" , main_pid );
3590
3596
else
3591
3597
snprintf (logpath , sizeof (logpath ), "pgbench_log.%d.%d" , main_pid , thread -> tid );
3592
- logfile = fopen (logpath , "w" );
3598
+ thread -> logfile = fopen (logpath , "w" );
3593
3599
3594
- if (logfile == NULL )
3600
+ if (thread -> logfile == NULL )
3595
3601
{
3596
3602
fprintf (stderr , "could not open logfile \"%s\": %s\n" ,
3597
3603
logpath , strerror (errno ));
@@ -3628,7 +3634,7 @@ threadRun(void *arg)
3628
3634
if (debug )
3629
3635
fprintf (stderr , "client %d executing script \"%s\"\n" , st -> id ,
3630
3636
sql_script [st -> use_file ].name );
3631
- if (!doCustom (thread , st , logfile , & aggs ))
3637
+ if (!doCustom (thread , st , & aggs ))
3632
3638
remains -- ; /* I've aborted */
3633
3639
3634
3640
if (st -> ecnt > prev_ecnt && commands [st -> state ]-> type == META_COMMAND )
@@ -3767,7 +3773,7 @@ threadRun(void *arg)
3767
3773
if (st -> con && (FD_ISSET (PQsocket (st -> con ), & input_mask )
3768
3774
|| commands [st -> state ]-> type == META_COMMAND ))
3769
3775
{
3770
- if (!doCustom (thread , st , logfile , & aggs ))
3776
+ if (!doCustom (thread , st , & aggs ))
3771
3777
remains -- ; /* I've aborted */
3772
3778
}
3773
3779
@@ -3871,14 +3877,14 @@ threadRun(void *arg)
3871
3877
disconnect_all (state , nstate );
3872
3878
INSTR_TIME_SET_CURRENT (end );
3873
3879
INSTR_TIME_ACCUM_DIFF (thread -> conn_time , end , start );
3874
- if (logfile )
3880
+ if (thread -> logfile )
3875
3881
{
3876
3882
if (agg_interval )
3877
3883
{
3878
3884
/* log aggregated but not yet reported transactions */
3879
- doLog (thread , state , logfile , & end , & aggs , false, 0 , 0 );
3885
+ doLog (thread , state , & end , & aggs , false, 0 , 0 );
3880
3886
}
3881
- fclose (logfile );
3887
+ fclose (thread -> logfile );
3882
3888
}
3883
3889
return NULL ;
3884
3890
}
0 commit comments