PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/pgbench/pgbench.c

https://github.com/bbt123/postgres
C | 3518 lines | 2642 code | 459 blank | 417 comment | 589 complexity | f7a6b15b57324ba77ce0261182bf6f1b MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * pgbench.c
  3. *
  4. * A simple benchmark program for PostgreSQL
  5. * Originally written by Tatsuo Ishii and enhanced by many contributors.
  6. *
  7. * contrib/pgbench/pgbench.c
  8. * Copyright (c) 2000-2014, PostgreSQL Global Development Group
  9. * ALL RIGHTS RESERVED;
  10. *
  11. * Permission to use, copy, modify, and distribute this software and its
  12. * documentation for any purpose, without fee, and without a written agreement
  13. * is hereby granted, provided that the above copyright notice and this
  14. * paragraph and the following two paragraphs appear in all copies.
  15. *
  16. * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
  17. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
  18. * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
  19. * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
  20. * POSSIBILITY OF SUCH DAMAGE.
  21. *
  22. * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  25. * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
  26. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27. *
  28. */
  29. #ifdef WIN32
  30. #define FD_SETSIZE 1024 /* set before winsock2.h is included */
  31. #endif /* ! WIN32 */
  32. #include "postgres_fe.h"
  33. #include "getopt_long.h"
  34. #include "libpq-fe.h"
  35. #include "portability/instr_time.h"
  36. #include <ctype.h>
  37. #include <math.h>
  38. #include <signal.h>
  39. #include <sys/time.h>
  40. #ifdef HAVE_SYS_SELECT_H
  41. #include <sys/select.h>
  42. #endif
  43. #ifdef HAVE_SYS_RESOURCE_H
  44. #include <sys/resource.h> /* for getrlimit */
  45. #endif
  46. #ifndef INT64_MAX
  47. #define INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
  48. #endif
  49. /*
  50. * Multi-platform pthread implementations
  51. */
  52. #ifdef WIN32
  53. /* Use native win32 threads on Windows */
  54. typedef struct win32_pthread *pthread_t;
  55. typedef int pthread_attr_t;
  56. static int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
  57. static int pthread_join(pthread_t th, void **thread_return);
  58. #elif defined(ENABLE_THREAD_SAFETY)
  59. /* Use platform-dependent pthread capability */
  60. #include <pthread.h>
  61. #else
  62. /* Use emulation with fork. Rename pthread identifiers to avoid conflicts */
  63. #define PTHREAD_FORK_EMULATION
  64. #include <sys/wait.h>
  65. #define pthread_t pg_pthread_t
  66. #define pthread_attr_t pg_pthread_attr_t
  67. #define pthread_create pg_pthread_create
  68. #define pthread_join pg_pthread_join
  69. typedef struct fork_pthread *pthread_t;
  70. typedef int pthread_attr_t;
  71. static int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
  72. static int pthread_join(pthread_t th, void **thread_return);
  73. #endif
  74. /********************************************************************
  75. * some configurable parameters */
  76. /* max number of clients allowed */
  77. #ifdef FD_SETSIZE
  78. #define MAXCLIENTS (FD_SETSIZE - 10)
  79. #else
  80. #define MAXCLIENTS 1024
  81. #endif
  82. #define LOG_STEP_SECONDS 5 /* seconds between log messages */
  83. #define DEFAULT_NXACTS 10 /* default nxacts */
  84. int nxacts = 0; /* number of transactions per client */
  85. int duration = 0; /* duration in seconds */
  86. /*
  87. * scaling factor. for example, scale = 10 will make 1000000 tuples in
  88. * pgbench_accounts table.
  89. */
  90. int scale = 1;
  91. /*
  92. * fillfactor. for example, fillfactor = 90 will use only 90 percent
  93. * space during inserts and leave 10 percent free.
  94. */
  95. int fillfactor = 100;
  96. /*
  97. * create foreign key constraints on the tables?
  98. */
  99. int foreign_keys = 0;
  100. /*
  101. * use unlogged tables?
  102. */
  103. int unlogged_tables = 0;
  104. /*
  105. * log sampling rate (1.0 = log everything, 0.0 = option not given)
  106. */
  107. double sample_rate = 0.0;
  108. /*
  109. * When threads are throttled to a given rate limit, this is the target delay
  110. * to reach that rate in usec. 0 is the default and means no throttling.
  111. */
  112. int64 throttle_delay = 0;
  113. /*
  114. * tablespace selection
  115. */
  116. char *tablespace = NULL;
  117. char *index_tablespace = NULL;
  118. /*
  119. * end of configurable parameters
  120. *********************************************************************/
  121. #define nbranches 1 /* Makes little sense to change this. Change
  122. * -s instead */
  123. #define ntellers 10
  124. #define naccounts 100000
  125. /*
  126. * The scale factor at/beyond which 32bit integers are incapable of storing
  127. * 64bit values.
  128. *
  129. * Although the actual threshold is 21474, we use 20000 because it is easier to
  130. * document and remember, and isn't that far away from the real threshold.
  131. */
  132. #define SCALE_32BIT_THRESHOLD 20000
  133. bool use_log; /* log transaction latencies to a file */
  134. bool use_quiet; /* quiet logging onto stderr */
  135. int agg_interval; /* log aggregates instead of individual
  136. * transactions */
  137. int progress = 0; /* thread progress report every this seconds */
  138. int progress_nclients = 0; /* number of clients for progress
  139. * report */
  140. int progress_nthreads = 0; /* number of threads for progress
  141. * report */
  142. bool is_connect; /* establish connection for each transaction */
  143. bool is_latencies; /* report per-command latencies */
  144. int main_pid; /* main process id used in log filename */
  145. char *pghost = "";
  146. char *pgport = "";
  147. char *login = NULL;
  148. char *dbName;
  149. const char *progname;
  150. volatile bool timer_exceeded = false; /* flag from signal handler */
  151. /* variable definitions */
  152. typedef struct
  153. {
  154. char *name; /* variable name */
  155. char *value; /* its value */
  156. } Variable;
  157. #define MAX_FILES 128 /* max number of SQL script files allowed */
  158. #define SHELL_COMMAND_SIZE 256 /* maximum size allowed for shell command */
  159. /*
  160. * structures used in custom query mode
  161. */
  162. typedef struct
  163. {
  164. PGconn *con; /* connection handle to DB */
  165. int id; /* client No. */
  166. int state; /* state No. */
  167. int cnt; /* xacts count */
  168. int ecnt; /* error count */
  169. int listen; /* 0 indicates that an async query has been
  170. * sent */
  171. int sleeping; /* 1 indicates that the client is napping */
  172. bool throttling; /* whether nap is for throttling */
  173. int64 until; /* napping until (usec) */
  174. Variable *variables; /* array of variable definitions */
  175. int nvariables;
  176. instr_time txn_begin; /* used for measuring transaction latencies */
  177. instr_time stmt_begin; /* used for measuring statement latencies */
  178. int64 txn_latencies; /* cumulated latencies */
  179. int64 txn_sqlats; /* cumulated square latencies */
  180. bool is_throttled; /* whether transaction throttling is done */
  181. int use_file; /* index in sql_files for this client */
  182. bool prepared[MAX_FILES];
  183. } CState;
  184. /*
  185. * Thread state and result
  186. */
  187. typedef struct
  188. {
  189. int tid; /* thread id */
  190. pthread_t thread; /* thread handle */
  191. CState *state; /* array of CState */
  192. int nstate; /* length of state[] */
  193. instr_time start_time; /* thread start time */
  194. instr_time *exec_elapsed; /* time spent executing cmds (per Command) */
  195. int *exec_count; /* number of cmd executions (per Command) */
  196. unsigned short random_state[3]; /* separate randomness for each thread */
  197. int64 throttle_trigger; /* previous/next throttling (us) */
  198. int64 throttle_lag; /* total transaction lag behind throttling */
  199. int64 throttle_lag_max; /* max transaction lag */
  200. } TState;
  201. #define INVALID_THREAD ((pthread_t) 0)
  202. typedef struct
  203. {
  204. instr_time conn_time;
  205. int64 xacts;
  206. int64 latencies;
  207. int64 sqlats;
  208. int64 throttle_lag;
  209. int64 throttle_lag_max;
  210. } TResult;
  211. /*
  212. * queries read from files
  213. */
  214. #define SQL_COMMAND 1
  215. #define META_COMMAND 2
  216. #define MAX_ARGS 10
  217. typedef enum QueryMode
  218. {
  219. QUERY_SIMPLE, /* simple query */
  220. QUERY_EXTENDED, /* extended query */
  221. QUERY_PREPARED, /* extended query with prepared statements */
  222. NUM_QUERYMODE
  223. } QueryMode;
  224. static QueryMode querymode = QUERY_SIMPLE;
  225. static const char *QUERYMODE[] = {"simple", "extended", "prepared"};
  226. typedef struct
  227. {
  228. char *line; /* full text of command line */
  229. int command_num; /* unique index of this Command struct */
  230. int type; /* command type (SQL_COMMAND or META_COMMAND) */
  231. int argc; /* number of command words */
  232. char *argv[MAX_ARGS]; /* command word list */
  233. } Command;
  234. typedef struct
  235. {
  236. long start_time; /* when does the interval start */
  237. int cnt; /* number of transactions */
  238. double min_duration; /* min/max durations */
  239. double max_duration;
  240. double sum; /* sum(duration), sum(duration^2) - for
  241. * estimates */
  242. double sum2;
  243. } AggVals;
  244. static Command **sql_files[MAX_FILES]; /* SQL script files */
  245. static int num_files; /* number of script files */
  246. static int num_commands = 0; /* total number of Command structs */
  247. static int debug = 0; /* debug flag */
  248. /* default scenario */
  249. static char *tpc_b = {
  250. "\\set nbranches " CppAsString2(nbranches) " * :scale\n"
  251. "\\set ntellers " CppAsString2(ntellers) " * :scale\n"
  252. "\\set naccounts " CppAsString2(naccounts) " * :scale\n"
  253. "\\setrandom aid 1 :naccounts\n"
  254. "\\setrandom bid 1 :nbranches\n"
  255. "\\setrandom tid 1 :ntellers\n"
  256. "\\setrandom delta -5000 5000\n"
  257. "BEGIN;\n"
  258. "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
  259. "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
  260. "UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;\n"
  261. "UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;\n"
  262. "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
  263. "END;\n"
  264. };
  265. /* -N case */
  266. static char *simple_update = {
  267. "\\set nbranches " CppAsString2(nbranches) " * :scale\n"
  268. "\\set ntellers " CppAsString2(ntellers) " * :scale\n"
  269. "\\set naccounts " CppAsString2(naccounts) " * :scale\n"
  270. "\\setrandom aid 1 :naccounts\n"
  271. "\\setrandom bid 1 :nbranches\n"
  272. "\\setrandom tid 1 :ntellers\n"
  273. "\\setrandom delta -5000 5000\n"
  274. "BEGIN;\n"
  275. "UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;\n"
  276. "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
  277. "INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);\n"
  278. "END;\n"
  279. };
  280. /* -S case */
  281. static char *select_only = {
  282. "\\set naccounts " CppAsString2(naccounts) " * :scale\n"
  283. "\\setrandom aid 1 :naccounts\n"
  284. "SELECT abalance FROM pgbench_accounts WHERE aid = :aid;\n"
  285. };
  286. /* Function prototypes */
  287. static void setalarm(int seconds);
  288. static void *threadRun(void *arg);
  289. static void
  290. usage(void)
  291. {
  292. printf("%s is a benchmarking tool for PostgreSQL.\n\n"
  293. "Usage:\n"
  294. " %s [OPTION]... [DBNAME]\n"
  295. "\nInitialization options:\n"
  296. " -i, --initialize invokes initialization mode\n"
  297. " -F, --fillfactor=NUM set fill factor\n"
  298. " -n, --no-vacuum do not run VACUUM after initialization\n"
  299. " -q, --quiet quiet logging (one message each 5 seconds)\n"
  300. " -s, --scale=NUM scaling factor\n"
  301. " --foreign-keys create foreign key constraints between tables\n"
  302. " --index-tablespace=TABLESPACE\n"
  303. " create indexes in the specified tablespace\n"
  304. " --tablespace=TABLESPACE create tables in the specified tablespace\n"
  305. " --unlogged-tables create tables as unlogged tables\n"
  306. "\nBenchmarking options:\n"
  307. " -c, --client=NUM number of concurrent database clients (default: 1)\n"
  308. " -C, --connect establish new connection for each transaction\n"
  309. " -D, --define=VARNAME=VALUE\n"
  310. " define variable for use by custom script\n"
  311. " -f, --file=FILENAME read transaction script from FILENAME\n"
  312. " -j, --jobs=NUM number of threads (default: 1)\n"
  313. " -l, --log write transaction times to log file\n"
  314. " -M, --protocol=simple|extended|prepared\n"
  315. " protocol for submitting queries (default: simple)\n"
  316. " -n, --no-vacuum do not run VACUUM before tests\n"
  317. " -N, --skip-some-updates skip updates of pgbench_tellers and pgbench_branches\n"
  318. " -P, --progress=NUM show thread progress report every NUM seconds\n"
  319. " -r, --report-latencies report average latency per command\n"
  320. " -R, --rate=NUM target rate in transactions per second\n"
  321. " -s, --scale=NUM report this scale factor in output\n"
  322. " -S, --select-only perform SELECT-only transactions\n"
  323. " -t, --transactions=NUM number of transactions each client runs (default: 10)\n"
  324. " -T, --time=NUM duration of benchmark test in seconds\n"
  325. " -v, --vacuum-all vacuum all four standard tables before tests\n"
  326. " --aggregate-interval=NUM aggregate data over NUM seconds\n"
  327. " --sampling-rate=NUM fraction of transactions to log (e.g. 0.01 for 1%%)\n"
  328. "\nCommon options:\n"
  329. " -d, --debug print debugging output\n"
  330. " -h, --host=HOSTNAME database server host or socket directory\n"
  331. " -p, --port=PORT database server port number\n"
  332. " -U, --username=USERNAME connect as specified database user\n"
  333. " -V, --version output version information, then exit\n"
  334. " -?, --help show this help, then exit\n"
  335. "\n"
  336. "Report bugs to <pgsql-bugs@postgresql.org>.\n",
  337. progname, progname);
  338. }
  339. /*
  340. * strtoint64 -- convert a string to 64-bit integer
  341. *
  342. * This function is a modified version of scanint8() from
  343. * src/backend/utils/adt/int8.c.
  344. */
  345. static int64
  346. strtoint64(const char *str)
  347. {
  348. const char *ptr = str;
  349. int64 result = 0;
  350. int sign = 1;
  351. /*
  352. * Do our own scan, rather than relying on sscanf which might be broken
  353. * for long long.
  354. */
  355. /* skip leading spaces */
  356. while (*ptr && isspace((unsigned char) *ptr))
  357. ptr++;
  358. /* handle sign */
  359. if (*ptr == '-')
  360. {
  361. ptr++;
  362. /*
  363. * Do an explicit check for INT64_MIN. Ugly though this is, it's
  364. * cleaner than trying to get the loop below to handle it portably.
  365. */
  366. if (strncmp(ptr, "9223372036854775808", 19) == 0)
  367. {
  368. result = -INT64CONST(0x7fffffffffffffff) - 1;
  369. ptr += 19;
  370. goto gotdigits;
  371. }
  372. sign = -1;
  373. }
  374. else if (*ptr == '+')
  375. ptr++;
  376. /* require at least one digit */
  377. if (!isdigit((unsigned char) *ptr))
  378. fprintf(stderr, "invalid input syntax for integer: \"%s\"\n", str);
  379. /* process digits */
  380. while (*ptr && isdigit((unsigned char) *ptr))
  381. {
  382. int64 tmp = result * 10 + (*ptr++ - '0');
  383. if ((tmp / 10) != result) /* overflow? */
  384. fprintf(stderr, "value \"%s\" is out of range for type bigint\n", str);
  385. result = tmp;
  386. }
  387. gotdigits:
  388. /* allow trailing whitespace, but not other trailing chars */
  389. while (*ptr != '\0' && isspace((unsigned char) *ptr))
  390. ptr++;
  391. if (*ptr != '\0')
  392. fprintf(stderr, "invalid input syntax for integer: \"%s\"\n", str);
  393. return ((sign < 0) ? -result : result);
  394. }
  395. /* random number generator: uniform distribution from min to max inclusive */
  396. static int64
  397. getrand(TState *thread, int64 min, int64 max)
  398. {
  399. /*
  400. * Odd coding is so that min and max have approximately the same chance of
  401. * being selected as do numbers between them.
  402. *
  403. * pg_erand48() is thread-safe and concurrent, which is why we use it
  404. * rather than random(), which in glibc is non-reentrant, and therefore
  405. * protected by a mutex, and therefore a bottleneck on machines with many
  406. * CPUs.
  407. */
  408. return min + (int64) ((max - min + 1) * pg_erand48(thread->random_state));
  409. }
  410. /* call PQexec() and exit() on failure */
  411. static void
  412. executeStatement(PGconn *con, const char *sql)
  413. {
  414. PGresult *res;
  415. res = PQexec(con, sql);
  416. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  417. {
  418. fprintf(stderr, "%s", PQerrorMessage(con));
  419. exit(1);
  420. }
  421. PQclear(res);
  422. }
  423. /* set up a connection to the backend */
  424. static PGconn *
  425. doConnect(void)
  426. {
  427. PGconn *conn;
  428. static char *password = NULL;
  429. bool new_pass;
  430. /*
  431. * Start the connection. Loop until we have a password if requested by
  432. * backend.
  433. */
  434. do
  435. {
  436. #define PARAMS_ARRAY_SIZE 7
  437. const char *keywords[PARAMS_ARRAY_SIZE];
  438. const char *values[PARAMS_ARRAY_SIZE];
  439. keywords[0] = "host";
  440. values[0] = pghost;
  441. keywords[1] = "port";
  442. values[1] = pgport;
  443. keywords[2] = "user";
  444. values[2] = login;
  445. keywords[3] = "password";
  446. values[3] = password;
  447. keywords[4] = "dbname";
  448. values[4] = dbName;
  449. keywords[5] = "fallback_application_name";
  450. values[5] = progname;
  451. keywords[6] = NULL;
  452. values[6] = NULL;
  453. new_pass = false;
  454. conn = PQconnectdbParams(keywords, values, true);
  455. if (!conn)
  456. {
  457. fprintf(stderr, "Connection to database \"%s\" failed\n",
  458. dbName);
  459. return NULL;
  460. }
  461. if (PQstatus(conn) == CONNECTION_BAD &&
  462. PQconnectionNeedsPassword(conn) &&
  463. password == NULL)
  464. {
  465. PQfinish(conn);
  466. password = simple_prompt("Password: ", 100, false);
  467. new_pass = true;
  468. }
  469. } while (new_pass);
  470. /* check to see that the backend connection was successfully made */
  471. if (PQstatus(conn) == CONNECTION_BAD)
  472. {
  473. fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
  474. dbName, PQerrorMessage(conn));
  475. PQfinish(conn);
  476. return NULL;
  477. }
  478. return conn;
  479. }
  480. /* throw away response from backend */
  481. static void
  482. discard_response(CState *state)
  483. {
  484. PGresult *res;
  485. do
  486. {
  487. res = PQgetResult(state->con);
  488. if (res)
  489. PQclear(res);
  490. } while (res);
  491. }
  492. static int
  493. compareVariables(const void *v1, const void *v2)
  494. {
  495. return strcmp(((const Variable *) v1)->name,
  496. ((const Variable *) v2)->name);
  497. }
  498. static char *
  499. getVariable(CState *st, char *name)
  500. {
  501. Variable key,
  502. *var;
  503. /* On some versions of Solaris, bsearch of zero items dumps core */
  504. if (st->nvariables <= 0)
  505. return NULL;
  506. key.name = name;
  507. var = (Variable *) bsearch((void *) &key,
  508. (void *) st->variables,
  509. st->nvariables,
  510. sizeof(Variable),
  511. compareVariables);
  512. if (var != NULL)
  513. return var->value;
  514. else
  515. return NULL;
  516. }
  517. /* check whether the name consists of alphabets, numerals and underscores. */
  518. static bool
  519. isLegalVariableName(const char *name)
  520. {
  521. int i;
  522. for (i = 0; name[i] != '\0'; i++)
  523. {
  524. if (!isalnum((unsigned char) name[i]) && name[i] != '_')
  525. return false;
  526. }
  527. return true;
  528. }
  529. static int
  530. putVariable(CState *st, const char *context, char *name, char *value)
  531. {
  532. Variable key,
  533. *var;
  534. key.name = name;
  535. /* On some versions of Solaris, bsearch of zero items dumps core */
  536. if (st->nvariables > 0)
  537. var = (Variable *) bsearch((void *) &key,
  538. (void *) st->variables,
  539. st->nvariables,
  540. sizeof(Variable),
  541. compareVariables);
  542. else
  543. var = NULL;
  544. if (var == NULL)
  545. {
  546. Variable *newvars;
  547. /*
  548. * Check for the name only when declaring a new variable to avoid
  549. * overhead.
  550. */
  551. if (!isLegalVariableName(name))
  552. {
  553. fprintf(stderr, "%s: invalid variable name '%s'\n", context, name);
  554. return false;
  555. }
  556. if (st->variables)
  557. newvars = (Variable *) pg_realloc(st->variables,
  558. (st->nvariables + 1) * sizeof(Variable));
  559. else
  560. newvars = (Variable *) pg_malloc(sizeof(Variable));
  561. st->variables = newvars;
  562. var = &newvars[st->nvariables];
  563. var->name = pg_strdup(name);
  564. var->value = pg_strdup(value);
  565. st->nvariables++;
  566. qsort((void *) st->variables, st->nvariables, sizeof(Variable),
  567. compareVariables);
  568. }
  569. else
  570. {
  571. char *val;
  572. /* dup then free, in case value is pointing at this variable */
  573. val = pg_strdup(value);
  574. free(var->value);
  575. var->value = val;
  576. }
  577. return true;
  578. }
  579. static char *
  580. parseVariable(const char *sql, int *eaten)
  581. {
  582. int i = 0;
  583. char *name;
  584. do
  585. {
  586. i++;
  587. } while (isalnum((unsigned char) sql[i]) || sql[i] == '_');
  588. if (i == 1)
  589. return NULL;
  590. name = pg_malloc(i);
  591. memcpy(name, &sql[1], i - 1);
  592. name[i - 1] = '\0';
  593. *eaten = i;
  594. return name;
  595. }
  596. static char *
  597. replaceVariable(char **sql, char *param, int len, char *value)
  598. {
  599. int valueln = strlen(value);
  600. if (valueln > len)
  601. {
  602. size_t offset = param - *sql;
  603. *sql = pg_realloc(*sql, strlen(*sql) - len + valueln + 1);
  604. param = *sql + offset;
  605. }
  606. if (valueln != len)
  607. memmove(param + valueln, param + len, strlen(param + len) + 1);
  608. strncpy(param, value, valueln);
  609. return param + valueln;
  610. }
  611. static char *
  612. assignVariables(CState *st, char *sql)
  613. {
  614. char *p,
  615. *name,
  616. *val;
  617. p = sql;
  618. while ((p = strchr(p, ':')) != NULL)
  619. {
  620. int eaten;
  621. name = parseVariable(p, &eaten);
  622. if (name == NULL)
  623. {
  624. while (*p == ':')
  625. {
  626. p++;
  627. }
  628. continue;
  629. }
  630. val = getVariable(st, name);
  631. free(name);
  632. if (val == NULL)
  633. {
  634. p++;
  635. continue;
  636. }
  637. p = replaceVariable(&sql, p, eaten, val);
  638. }
  639. return sql;
  640. }
  641. static void
  642. getQueryParams(CState *st, const Command *command, const char **params)
  643. {
  644. int i;
  645. for (i = 0; i < command->argc - 1; i++)
  646. params[i] = getVariable(st, command->argv[i + 1]);
  647. }
  648. /*
  649. * Run a shell command. The result is assigned to the variable if not NULL.
  650. * Return true if succeeded, or false on error.
  651. */
  652. static bool
  653. runShellCommand(CState *st, char *variable, char **argv, int argc)
  654. {
  655. char command[SHELL_COMMAND_SIZE];
  656. int i,
  657. len = 0;
  658. FILE *fp;
  659. char res[64];
  660. char *endptr;
  661. int retval;
  662. /*----------
  663. * Join arguments with whitespace separators. Arguments starting with
  664. * exactly one colon are treated as variables:
  665. * name - append a string "name"
  666. * :var - append a variable named 'var'
  667. * ::name - append a string ":name"
  668. *----------
  669. */
  670. for (i = 0; i < argc; i++)
  671. {
  672. char *arg;
  673. int arglen;
  674. if (argv[i][0] != ':')
  675. {
  676. arg = argv[i]; /* a string literal */
  677. }
  678. else if (argv[i][1] == ':')
  679. {
  680. arg = argv[i] + 1; /* a string literal starting with colons */
  681. }
  682. else if ((arg = getVariable(st, argv[i] + 1)) == NULL)
  683. {
  684. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[i]);
  685. return false;
  686. }
  687. arglen = strlen(arg);
  688. if (len + arglen + (i > 0 ? 1 : 0) >= SHELL_COMMAND_SIZE - 1)
  689. {
  690. fprintf(stderr, "%s: too long shell command\n", argv[0]);
  691. return false;
  692. }
  693. if (i > 0)
  694. command[len++] = ' ';
  695. memcpy(command + len, arg, arglen);
  696. len += arglen;
  697. }
  698. command[len] = '\0';
  699. /* Fast path for non-assignment case */
  700. if (variable == NULL)
  701. {
  702. if (system(command))
  703. {
  704. if (!timer_exceeded)
  705. fprintf(stderr, "%s: cannot launch shell command\n", argv[0]);
  706. return false;
  707. }
  708. return true;
  709. }
  710. /* Execute the command with pipe and read the standard output. */
  711. if ((fp = popen(command, "r")) == NULL)
  712. {
  713. fprintf(stderr, "%s: cannot launch shell command\n", argv[0]);
  714. return false;
  715. }
  716. if (fgets(res, sizeof(res), fp) == NULL)
  717. {
  718. if (!timer_exceeded)
  719. fprintf(stderr, "%s: cannot read the result\n", argv[0]);
  720. return false;
  721. }
  722. if (pclose(fp) < 0)
  723. {
  724. fprintf(stderr, "%s: cannot close shell command\n", argv[0]);
  725. return false;
  726. }
  727. /* Check whether the result is an integer and assign it to the variable */
  728. retval = (int) strtol(res, &endptr, 10);
  729. while (*endptr != '\0' && isspace((unsigned char) *endptr))
  730. endptr++;
  731. if (*res == '\0' || *endptr != '\0')
  732. {
  733. fprintf(stderr, "%s: must return an integer ('%s' returned)\n", argv[0], res);
  734. return false;
  735. }
  736. snprintf(res, sizeof(res), "%d", retval);
  737. if (!putVariable(st, "setshell", variable, res))
  738. return false;
  739. #ifdef DEBUG
  740. printf("shell parameter name: %s, value: %s\n", argv[1], res);
  741. #endif
  742. return true;
  743. }
  744. #define MAX_PREPARE_NAME 32
  745. static void
  746. preparedStatementName(char *buffer, int file, int state)
  747. {
  748. sprintf(buffer, "P%d_%d", file, state);
  749. }
  750. static bool
  751. clientDone(CState *st, bool ok)
  752. {
  753. (void) ok; /* unused */
  754. if (st->con != NULL)
  755. {
  756. PQfinish(st->con);
  757. st->con = NULL;
  758. }
  759. return false; /* always false */
  760. }
  761. static
  762. void
  763. agg_vals_init(AggVals *aggs, instr_time start)
  764. {
  765. /* basic counters */
  766. aggs->cnt = 0; /* number of transactions */
  767. aggs->sum = 0; /* SUM(duration) */
  768. aggs->sum2 = 0; /* SUM(duration*duration) */
  769. /* min and max transaction duration */
  770. aggs->min_duration = 0;
  771. aggs->max_duration = 0;
  772. /* start of the current interval */
  773. aggs->start_time = INSTR_TIME_GET_DOUBLE(start);
  774. }
  775. /* return false iff client should be disconnected */
  776. static bool
  777. doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile, AggVals *agg)
  778. {
  779. PGresult *res;
  780. Command **commands;
  781. bool trans_needs_throttle = false;
  782. top:
  783. commands = sql_files[st->use_file];
  784. /*
  785. * Handle throttling once per transaction by sleeping. It is simpler to
  786. * do this here rather than at the end, because so much complicated logic
  787. * happens below when statements finish.
  788. */
  789. if (throttle_delay && !st->is_throttled)
  790. {
  791. /*
  792. * Use inverse transform sampling to randomly generate a delay, such
  793. * that the series of delays will approximate a Poisson distribution
  794. * centered on the throttle_delay time.
  795. *
  796. * 10000 implies a 9.2 (-log(1/10000)) to 0.0 (log 1) delay
  797. * multiplier, and results in a 0.055 % target underestimation bias:
  798. *
  799. * SELECT 1.0/AVG(-LN(i/10000.0)) FROM generate_series(1,10000) AS i;
  800. * = 1.000552717032611116335474
  801. *
  802. * If transactions are too slow or a given wait is shorter than a
  803. * transaction, the next transaction will start right away.
  804. */
  805. int64 wait = (int64) (throttle_delay *
  806. 1.00055271703 * -log(getrand(thread, 1, 10000) / 10000.0));
  807. thread->throttle_trigger += wait;
  808. st->until = thread->throttle_trigger;
  809. st->sleeping = 1;
  810. st->throttling = true;
  811. st->is_throttled = true;
  812. if (debug)
  813. fprintf(stderr, "client %d throttling " INT64_FORMAT " us\n",
  814. st->id, wait);
  815. }
  816. if (st->sleeping)
  817. { /* are we sleeping? */
  818. instr_time now;
  819. int64 now_us;
  820. INSTR_TIME_SET_CURRENT(now);
  821. now_us = INSTR_TIME_GET_MICROSEC(now);
  822. if (st->until <= now_us)
  823. {
  824. st->sleeping = 0; /* Done sleeping, go ahead with next command */
  825. if (st->throttling)
  826. {
  827. /* Measure lag of throttled transaction relative to target */
  828. int64 lag = now_us - st->until;
  829. thread->throttle_lag += lag;
  830. if (lag > thread->throttle_lag_max)
  831. thread->throttle_lag_max = lag;
  832. st->throttling = false;
  833. }
  834. }
  835. else
  836. return true; /* Still sleeping, nothing to do here */
  837. }
  838. if (st->listen)
  839. { /* are we receiver? */
  840. if (commands[st->state]->type == SQL_COMMAND)
  841. {
  842. if (debug)
  843. fprintf(stderr, "client %d receiving\n", st->id);
  844. if (!PQconsumeInput(st->con))
  845. { /* there's something wrong */
  846. fprintf(stderr, "Client %d aborted in state %d. Probably the backend died while processing.\n", st->id, st->state);
  847. return clientDone(st, false);
  848. }
  849. if (PQisBusy(st->con))
  850. return true; /* don't have the whole result yet */
  851. }
  852. /*
  853. * command finished: accumulate per-command execution times in
  854. * thread-local data structure, if per-command latencies are requested
  855. */
  856. if (is_latencies)
  857. {
  858. instr_time now;
  859. int cnum = commands[st->state]->command_num;
  860. INSTR_TIME_SET_CURRENT(now);
  861. INSTR_TIME_ACCUM_DIFF(thread->exec_elapsed[cnum],
  862. now, st->stmt_begin);
  863. thread->exec_count[cnum]++;
  864. }
  865. /* transaction finished: record latency under progress or throttling */
  866. if ((progress || throttle_delay) && commands[st->state + 1] == NULL)
  867. {
  868. instr_time diff;
  869. int64 latency;
  870. INSTR_TIME_SET_CURRENT(diff);
  871. INSTR_TIME_SUBTRACT(diff, st->txn_begin);
  872. latency = INSTR_TIME_GET_MICROSEC(diff);
  873. st->txn_latencies += latency;
  874. /*
  875. * XXX In a long benchmark run of high-latency transactions, this
  876. * int64 addition eventually overflows. For example, 100 threads
  877. * running 10s transactions will overflow it in 2.56 hours. With
  878. * a more-typical OLTP workload of .1s transactions, overflow
  879. * would take 256 hours.
  880. */
  881. st->txn_sqlats += latency * latency;
  882. }
  883. /*
  884. * if transaction finished, record the time it took in the log
  885. */
  886. if (logfile && commands[st->state + 1] == NULL)
  887. {
  888. instr_time now;
  889. instr_time diff;
  890. double usec;
  891. /*
  892. * write the log entry if this row belongs to the random sample,
  893. * or no sampling rate was given which means log everything.
  894. */
  895. if (sample_rate == 0.0 ||
  896. pg_erand48(thread->random_state) <= sample_rate)
  897. {
  898. INSTR_TIME_SET_CURRENT(now);
  899. diff = now;
  900. INSTR_TIME_SUBTRACT(diff, st->txn_begin);
  901. usec = (double) INSTR_TIME_GET_MICROSEC(diff);
  902. /* should we aggregate the results or not? */
  903. if (agg_interval > 0)
  904. {
  905. /*
  906. * are we still in the same interval? if yes, accumulate
  907. * the values (print them otherwise)
  908. */
  909. if (agg->start_time + agg_interval >= INSTR_TIME_GET_DOUBLE(now))
  910. {
  911. agg->cnt += 1;
  912. agg->sum += usec;
  913. agg->sum2 += usec * usec;
  914. /* first in this aggregation interval */
  915. if ((agg->cnt == 1) || (usec < agg->min_duration))
  916. agg->min_duration = usec;
  917. if ((agg->cnt == 1) || (usec > agg->max_duration))
  918. agg->max_duration = usec;
  919. }
  920. else
  921. {
  922. /*
  923. * Loop until we reach the interval of the current
  924. * transaction (and print all the empty intervals in
  925. * between).
  926. */
  927. while (agg->start_time + agg_interval < INSTR_TIME_GET_DOUBLE(now))
  928. {
  929. /*
  930. * This is a non-Windows branch (thanks to the
  931. * ifdef in usage), so we don't need to handle
  932. * this in a special way (see below).
  933. */
  934. fprintf(logfile, "%ld %d %.0f %.0f %.0f %.0f\n",
  935. agg->start_time,
  936. agg->cnt,
  937. agg->sum,
  938. agg->sum2,
  939. agg->min_duration,
  940. agg->max_duration);
  941. /* move to the next inteval */
  942. agg->start_time = agg->start_time + agg_interval;
  943. /* reset for "no transaction" intervals */
  944. agg->cnt = 0;
  945. agg->min_duration = 0;
  946. agg->max_duration = 0;
  947. agg->sum = 0;
  948. agg->sum2 = 0;
  949. }
  950. /*
  951. * and now update the reset values (include the
  952. * current)
  953. */
  954. agg->cnt = 1;
  955. agg->min_duration = usec;
  956. agg->max_duration = usec;
  957. agg->sum = usec;
  958. agg->sum2 = usec * usec;
  959. }
  960. }
  961. else
  962. {
  963. /* no, print raw transactions */
  964. #ifndef WIN32
  965. /*
  966. * This is more than we really ought to know about
  967. * instr_time
  968. */
  969. fprintf(logfile, "%d %d %.0f %d %ld %ld\n",
  970. st->id, st->cnt, usec, st->use_file,
  971. (long) now.tv_sec, (long) now.tv_usec);
  972. #else
  973. /*
  974. * On Windows, instr_time doesn't provide a timestamp
  975. * anyway
  976. */
  977. fprintf(logfile, "%d %d %.0f %d 0 0\n",
  978. st->id, st->cnt, usec, st->use_file);
  979. #endif
  980. }
  981. }
  982. }
  983. if (commands[st->state]->type == SQL_COMMAND)
  984. {
  985. /*
  986. * Read and discard the query result; note this is not included in
  987. * the statement latency numbers.
  988. */
  989. res = PQgetResult(st->con);
  990. switch (PQresultStatus(res))
  991. {
  992. case PGRES_COMMAND_OK:
  993. case PGRES_TUPLES_OK:
  994. break; /* OK */
  995. default:
  996. fprintf(stderr, "Client %d aborted in state %d: %s",
  997. st->id, st->state, PQerrorMessage(st->con));
  998. PQclear(res);
  999. return clientDone(st, false);
  1000. }
  1001. PQclear(res);
  1002. discard_response(st);
  1003. }
  1004. if (commands[st->state + 1] == NULL)
  1005. {
  1006. if (is_connect)
  1007. {
  1008. PQfinish(st->con);
  1009. st->con = NULL;
  1010. }
  1011. ++st->cnt;
  1012. if ((st->cnt >= nxacts && duration <= 0) || timer_exceeded)
  1013. return clientDone(st, true); /* exit success */
  1014. }
  1015. /* increment state counter */
  1016. st->state++;
  1017. if (commands[st->state] == NULL)
  1018. {
  1019. st->state = 0;
  1020. st->use_file = (int) getrand(thread, 0, num_files - 1);
  1021. commands = sql_files[st->use_file];
  1022. st->is_throttled = false;
  1023. /*
  1024. * No transaction is underway anymore, which means there is
  1025. * nothing to listen to right now. When throttling rate limits
  1026. * are active, a sleep will happen next, as the next transaction
  1027. * starts. And then in any case the next SQL command will set
  1028. * listen back to 1.
  1029. */
  1030. st->listen = 0;
  1031. trans_needs_throttle = (throttle_delay > 0);
  1032. }
  1033. }
  1034. if (st->con == NULL)
  1035. {
  1036. instr_time start,
  1037. end;
  1038. INSTR_TIME_SET_CURRENT(start);
  1039. if ((st->con = doConnect()) == NULL)
  1040. {
  1041. fprintf(stderr, "Client %d aborted in establishing connection.\n", st->id);
  1042. return clientDone(st, false);
  1043. }
  1044. INSTR_TIME_SET_CURRENT(end);
  1045. INSTR_TIME_ACCUM_DIFF(*conn_time, end, start);
  1046. }
  1047. /*
  1048. * This ensures that a throttling delay is inserted before proceeding with
  1049. * sql commands, after the first transaction. The first transaction
  1050. * throttling is performed when first entering doCustom.
  1051. */
  1052. if (trans_needs_throttle)
  1053. {
  1054. trans_needs_throttle = false;
  1055. goto top;
  1056. }
  1057. /* Record transaction start time under logging, progress or throttling */
  1058. if ((logfile || progress || throttle_delay) && st->state == 0)
  1059. INSTR_TIME_SET_CURRENT(st->txn_begin);
  1060. /* Record statement start time if per-command latencies are requested */
  1061. if (is_latencies)
  1062. INSTR_TIME_SET_CURRENT(st->stmt_begin);
  1063. if (commands[st->state]->type == SQL_COMMAND)
  1064. {
  1065. const Command *command = commands[st->state];
  1066. int r;
  1067. if (querymode == QUERY_SIMPLE)
  1068. {
  1069. char *sql;
  1070. sql = pg_strdup(command->argv[0]);
  1071. sql = assignVariables(st, sql);
  1072. if (debug)
  1073. fprintf(stderr, "client %d sending %s\n", st->id, sql);
  1074. r = PQsendQuery(st->con, sql);
  1075. free(sql);
  1076. }
  1077. else if (querymode == QUERY_EXTENDED)
  1078. {
  1079. const char *sql = command->argv[0];
  1080. const char *params[MAX_ARGS];
  1081. getQueryParams(st, command, params);
  1082. if (debug)
  1083. fprintf(stderr, "client %d sending %s\n", st->id, sql);
  1084. r = PQsendQueryParams(st->con, sql, command->argc - 1,
  1085. NULL, params, NULL, NULL, 0);
  1086. }
  1087. else if (querymode == QUERY_PREPARED)
  1088. {
  1089. char name[MAX_PREPARE_NAME];
  1090. const char *params[MAX_ARGS];
  1091. if (!st->prepared[st->use_file])
  1092. {
  1093. int j;
  1094. for (j = 0; commands[j] != NULL; j++)
  1095. {
  1096. PGresult *res;
  1097. char name[MAX_PREPARE_NAME];
  1098. if (commands[j]->type != SQL_COMMAND)
  1099. continue;
  1100. preparedStatementName(name, st->use_file, j);
  1101. res = PQprepare(st->con, name,
  1102. commands[j]->argv[0], commands[j]->argc - 1, NULL);
  1103. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  1104. fprintf(stderr, "%s", PQerrorMessage(st->con));
  1105. PQclear(res);
  1106. }
  1107. st->prepared[st->use_file] = true;
  1108. }
  1109. getQueryParams(st, command, params);
  1110. preparedStatementName(name, st->use_file, st->state);
  1111. if (debug)
  1112. fprintf(stderr, "client %d sending %s\n", st->id, name);
  1113. r = PQsendQueryPrepared(st->con, name, command->argc - 1,
  1114. params, NULL, NULL, 0);
  1115. }
  1116. else /* unknown sql mode */
  1117. r = 0;
  1118. if (r == 0)
  1119. {
  1120. if (debug)
  1121. fprintf(stderr, "client %d cannot send %s\n", st->id, command->argv[0]);
  1122. st->ecnt++;
  1123. }
  1124. else
  1125. st->listen = 1; /* flags that should be listened */
  1126. }
  1127. else if (commands[st->state]->type == META_COMMAND)
  1128. {
  1129. int argc = commands[st->state]->argc,
  1130. i;
  1131. char **argv = commands[st->state]->argv;
  1132. if (debug)
  1133. {
  1134. fprintf(stderr, "client %d executing \\%s", st->id, argv[0]);
  1135. for (i = 1; i < argc; i++)
  1136. fprintf(stderr, " %s", argv[i]);
  1137. fprintf(stderr, "\n");
  1138. }
  1139. if (pg_strcasecmp(argv[0], "setrandom") == 0)
  1140. {
  1141. char *var;
  1142. int64 min,
  1143. max;
  1144. char res[64];
  1145. if (*argv[2] == ':')
  1146. {
  1147. if ((var = getVariable(st, argv[2] + 1)) == NULL)
  1148. {
  1149. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[2]);
  1150. st->ecnt++;
  1151. return true;
  1152. }
  1153. min = strtoint64(var);
  1154. }
  1155. else
  1156. min = strtoint64(argv[2]);
  1157. #ifdef NOT_USED
  1158. if (min < 0)
  1159. {
  1160. fprintf(stderr, "%s: invalid minimum number %d\n", argv[0], min);
  1161. st->ecnt++;
  1162. return;
  1163. }
  1164. #endif
  1165. if (*argv[3] == ':')
  1166. {
  1167. if ((var = getVariable(st, argv[3] + 1)) == NULL)
  1168. {
  1169. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[3]);
  1170. st->ecnt++;
  1171. return true;
  1172. }
  1173. max = strtoint64(var);
  1174. }
  1175. else
  1176. max = strtoint64(argv[3]);
  1177. if (max < min)
  1178. {
  1179. fprintf(stderr, "%s: maximum is less than minimum\n", argv[0]);
  1180. st->ecnt++;
  1181. return true;
  1182. }
  1183. /*
  1184. * getrand() needs to be able to subtract max from min and add one
  1185. * to the result without overflowing. Since we know max > min, we
  1186. * can detect overflow just by checking for a negative result. But
  1187. * we must check both that the subtraction doesn't overflow, and
  1188. * that adding one to the result doesn't overflow either.
  1189. */
  1190. if (max - min < 0 || (max - min) + 1 < 0)
  1191. {
  1192. fprintf(stderr, "%s: range too large\n", argv[0]);
  1193. st->ecnt++;
  1194. return true;
  1195. }
  1196. #ifdef DEBUG
  1197. printf("min: " INT64_FORMAT " max: " INT64_FORMAT " random: " INT64_FORMAT "\n", min, max, getrand(thread, min, max));
  1198. #endif
  1199. snprintf(res, sizeof(res), INT64_FORMAT, getrand(thread, min, max));
  1200. if (!putVariable(st, argv[0], argv[1], res))
  1201. {
  1202. st->ecnt++;
  1203. return true;
  1204. }
  1205. st->listen = 1;
  1206. }
  1207. else if (pg_strcasecmp(argv[0], "set") == 0)
  1208. {
  1209. char *var;
  1210. int64 ope1,
  1211. ope2;
  1212. char res[64];
  1213. if (*argv[2] == ':')
  1214. {
  1215. if ((var = getVariable(st, argv[2] + 1)) == NULL)
  1216. {
  1217. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[2]);
  1218. st->ecnt++;
  1219. return true;
  1220. }
  1221. ope1 = strtoint64(var);
  1222. }
  1223. else
  1224. ope1 = strtoint64(argv[2]);
  1225. if (argc < 5)
  1226. snprintf(res, sizeof(res), INT64_FORMAT, ope1);
  1227. else
  1228. {
  1229. if (*argv[4] == ':')
  1230. {
  1231. if ((var = getVariable(st, argv[4] + 1)) == NULL)
  1232. {
  1233. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[4]);
  1234. st->ecnt++;
  1235. return true;
  1236. }
  1237. ope2 = strtoint64(var);
  1238. }
  1239. else
  1240. ope2 = strtoint64(argv[4]);
  1241. if (strcmp(argv[3], "+") == 0)
  1242. snprintf(res, sizeof(res), INT64_FORMAT, ope1 + ope2);
  1243. else if (strcmp(argv[3], "-") == 0)
  1244. snprintf(res, sizeof(res), INT64_FORMAT, ope1 - ope2);
  1245. else if (strcmp(argv[3], "*") == 0)
  1246. snprintf(res, sizeof(res), INT64_FORMAT, ope1 * ope2);
  1247. else if (strcmp(argv[3], "/") == 0)
  1248. {
  1249. if (ope2 == 0)
  1250. {
  1251. fprintf(stderr, "%s: division by zero\n", argv[0]);
  1252. st->ecnt++;
  1253. return true;
  1254. }
  1255. snprintf(res, sizeof(res), INT64_FORMAT, ope1 / ope2);
  1256. }
  1257. else
  1258. {
  1259. fprintf(stderr, "%s: unsupported operator %s\n", argv[0], argv[3]);
  1260. st->ecnt++;
  1261. return true;
  1262. }
  1263. }
  1264. if (!putVariable(st, argv[0], argv[1], res))
  1265. {
  1266. st->ecnt++;
  1267. return true;
  1268. }
  1269. st->listen = 1;
  1270. }
  1271. else if (pg_strcasecmp(argv[0], "sleep") == 0)
  1272. {
  1273. char *var;
  1274. int usec;
  1275. instr_time now;
  1276. if (*argv[1] == ':')
  1277. {
  1278. if ((var = getVariable(st, argv[1] + 1)) == NULL)
  1279. {
  1280. fprintf(stderr, "%s: undefined variable %s\n", argv[0], argv[1]);
  1281. st->ecnt++;
  1282. return true;
  1283. }
  1284. usec = atoi(var);
  1285. }
  1286. else
  1287. usec = atoi(argv[1]);
  1288. if (argc > 2)
  1289. {
  1290. if (pg_strcasecmp(argv[2], "ms") == 0)
  1291. usec *= 1000;
  1292. else if (pg_strcasecmp(argv[2], "s") == 0)
  1293. usec *= 1000000;
  1294. }
  1295. else
  1296. usec *= 1000000;
  1297. INSTR_TIME_SET_CURRENT(now);
  1298. st->until = INSTR_TIME_GET_MICROSEC(now) + usec;
  1299. st->sleeping = 1;
  1300. st->listen = 1;
  1301. }
  1302. else if (pg_strcasecmp(argv[0], "setshell") == 0)
  1303. {
  1304. bool ret = runShellCommand(st, argv[1], argv + 2, argc - 2);
  1305. if (timer_exceeded) /* timeout */
  1306. return clientDone(st, true);
  1307. else if (!ret) /* on error */
  1308. {
  1309. st->ecnt++;
  1310. return true;
  1311. }
  1312. else /* succeeded */
  1313. st->listen = 1;
  1314. }
  1315. else if (pg_strcasecmp(argv[0], "shell") == 0)
  1316. {
  1317. bool ret = runShellCommand(st, NULL, argv + 1, argc - 1);
  1318. if (timer_exceeded) /* timeout */
  1319. return clientDone(st, true);
  1320. else if (!ret) /* on error */
  1321. {
  1322. st->ecnt++;
  1323. return true;
  1324. }
  1325. else /* succeeded */
  1326. st->listen = 1;
  1327. }
  1328. goto top;
  1329. }
  1330. return true;
  1331. }
  1332. /* discard connections */
  1333. static void
  1334. disconnect_all(CState *state, int length)
  1335. {
  1336. int i;
  1337. for (i = 0; i < length; i++)
  1338. {
  1339. if (state[i].con)
  1340. {
  1341. PQfinish(state[i].con);
  1342. state[i].con = NULL;
  1343. }
  1344. }
  1345. }
  1346. /* create tables and setup data */
  1347. static void
  1348. init(bool is_no_vacuum)
  1349. {
  1350. /*
  1351. * The scale factor at/beyond which 32-bit integers are insufficient for
  1352. * storing TPC-B account IDs.
  1353. *
  1354. * Although the actual threshold is 21474, we use 20000 because it is easier to
  1355. * document and remember, and isn't that far away from the real threshold.
  1356. */
  1357. #define SCALE_32BIT_THRESHOLD 20000
  1358. /*
  1359. * Note: TPC-B requires at least 100 bytes per row, and the "filler"
  1360. * fields in these table declarations were intended to comply with that.
  1361. * The pgbench_accounts table complies with that because the "filler"
  1362. * column is set to blank-padded empty string. But for all other tables
  1363. * the columns default to NULL and so don't actually take any space. We
  1364. * could fix that by giving them non-null default values. However, that
  1365. * would completely break comparability of pgbench results with prior
  1366. * versions. Since pgbench has never pretended to be fully TPC-B compliant
  1367. * anyway, we stick with the historical behavior.
  1368. */
  1369. struct ddlinfo
  1370. {
  1371. const char *table; /* table name */
  1372. const char *smcols; /* column decls if accountIDs are 32 bits */
  1373. const char *bigcols; /* column decls if accountIDs are 64 bits */
  1374. int declare_fillfactor;
  1375. };
  1376. static const struct ddlinfo DDLs[] = {
  1377. {
  1378. "pgbench_history",
  1379. "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
  1380. "tid int,bid int,aid bigint,delta int,mtime timestamp,filler char(22)",
  1381. 0
  1382. },
  1383. {
  1384. "pgbench_tellers",
  1385. "tid int not null,bid int,tbalance int,filler char(84)",
  1386. "tid int not null,bid int,tbalance int,filler char(84)",
  1387. 1
  1388. },
  1389. {
  1390. "pgbench_accounts",
  1391. "aid int not null,bid int,abalance int,filler char(84)",
  1392. "aid bigint not null,bid int,abalance int,filler char(84)",
  1393. 1
  1394. },
  1395. {
  1396. "pgbench_branches",
  1397. "bid int not null,bbalance int,filler char(88)",
  1398. "bid int not null,bbalance int,filler char(88)",
  1399. 1
  1400. }
  1401. };
  1402. static const char *const DDLINDEXes[] = {
  1403. "alter table pgbench_branches add primary key (bid)",
  1404. "alter table pgbench_tellers add primary key (tid)",
  1405. "alter table pgbench_accounts add primary key (aid)"
  1406. };
  1407. static const char *const DDLKEYs[] = {
  1408. "alter table pgbench_tellers add foreign key (bid) references pgbench_branches",
  1409. "alter table pgbench_accounts add foreign key (bid) references pgbench_branches",
  1410. "alter table pgbench_history add foreign key (bid) references pgbench_branches",
  1411. "alter table pgbench_history add foreign key (tid) references pgbench_tellers",
  1412. "alter table pgbench_history add foreign key (aid) references pgbench_accounts"
  1413. };
  1414. PGconn *con;
  1415. PGresult *res;
  1416. char sql[256];
  1417. int i;
  1418. int64 k;
  1419. /* used to track elapsed time and estimate of the remaining time */
  1420. instr_time start,
  1421. diff;
  1422. double elapsed_sec,
  1423. remaining_sec;
  1424. int log_interval = 1;
  1425. if ((con = doConnect()) == NULL)
  1426. exit(1);
  1427. for (i = 0; i < lengthof(DDLs); i++)
  1428. {
  1429. char opts[256];
  1430. char buffer[256];
  1431. const struct ddlinfo *ddl = &DDLs[i];
  1432. const char *cols;
  1433. /* Remove old table, if it exists. */
  1434. snprintf(buffer, sizeof(buffer), "drop table if exists %s", ddl->table);
  1435. executeStatement(con, buffer);
  1436. /* Construct new create table statement. */
  1437. opts[0] = '\0';
  1438. if (ddl->declare_fillfactor)
  1439. snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
  1440. " with (fillfactor=%d)", fillfactor);
  1441. if (tablespace != NULL)
  1442. {
  1443. char *escape_tablespace;
  1444. escape_tablespace = PQescapeIdentifier(con, tablespace,
  1445. strlen(tablespace));
  1446. snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
  1447. " tablespace %s", escape_tablespace);
  1448. PQfreemem(escape_tablespace);
  1449. }
  1450. cols = (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols;
  1451. snprintf(buffer, sizeof(buffer), "create%s table %s(%s)%s",
  1452. unlogged_tables ? " unlogged" : "",
  1453. ddl->table, cols, opts);
  1454. executeStatement(con, buffer);
  1455. }
  1456. executeStatement(con, "begin");
  1457. for (i = 0; i < nbranches * scale; i++)
  1458. {
  1459. /* "filler" column defaults to NULL */
  1460. snprintf(sql, sizeof(sql),
  1461. "insert into pgbench_branches(bid,bbalance) values(%d,0)",
  1462. i + 1);
  1463. executeStatement(con, sql);
  1464. }
  1465. for (i = 0; i < ntellers * scale; i++)
  1466. {
  1467. /* "filler" column defaults to NULL */
  1468. snprintf(sql, sizeof(sql),
  1469. "insert into pgbench_tellers(tid,bid,tbalance) values (%d,%d,0)",
  1470. i + 1, i / ntellers + 1);
  1471. executeStatement(con, sql);
  1472. }
  1473. executeStatement(con, "commit");
  1474. /*
  1475. * fill the pgbench_accounts table with some data
  1476. */
  1477. fprintf(stderr, "creating tables...\n");
  1478. executeStatement(con, "begin");
  1479. executeStatement(con, "truncate pgbench_accounts");
  1480. res = PQexec(con, "copy pgbench_accounts from stdin");
  1481. if (PQresultStatus(res) != PGRES_COPY_IN)
  1482. {
  1483. fprintf(stderr, "%s", PQerrorMessage(con));
  1484. exit(1);
  1485. }
  1486. PQclear(res);
  1487. INSTR_TIME_SET_CURRENT(start);
  1488. for (k = 0; k < (int64) naccounts * scale; k++)
  1489. {
  1490. int64 j = k + 1;
  1491. /* "filler" column defaults to blank padded empty string */
  1492. snprintf(sql, sizeof(sql),
  1493. INT64_FORMAT "\t" INT64_FORMAT "\t%d\t\n",
  1494. j, k / naccounts + 1, 0);
  1495. if (PQputline(con, sql))
  1496. {
  1497. fprintf(stderr, "PQputline failed\n");
  1498. exit(1);
  1499. }
  1500. /*
  1501. * If we want to stick with the original logging, print a message each
  1502. * 100k inserted rows.
  1503. */
  1504. if ((!use_quiet) && (j % 100000 == 0))
  1505. {
  1506. INSTR_TIME_SET_CURRENT(diff);
  1507. INSTR_TIME_SUBTRACT(diff, start);
  1508. elapsed_sec = INSTR_TIME_GET_DOUBLE(diff);
  1509. remaining_sec = ((double) scale * naccounts - j) * elapsed_sec / j;
  1510. fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s).\n",
  1511. j, (int64) naccounts * scale,
  1512. (int) (((int64) j * 100) / (naccounts * (int64) scale)),
  1513. elapsed_sec, remaining_sec);
  1514. }
  1515. /* let's not call the timing for each row, but only each 100 rows */
  1516. else if (use_quiet && (j % 100 == 0))
  1517. {
  1518. INSTR_TIME_SET_CURRENT(diff);
  1519. INSTR_TIME_SUBTRACT(diff, start);
  1520. elapsed_sec = INSTR_TIME_GET_DOUBLE(diff);
  1521. remaining_sec = ((double) scale * naccounts - j) * elapsed_sec / j;
  1522. /* have we reached the next interval (or end)? */
  1523. if ((j == scale * naccounts) || (elapsed_sec >= log_interval * LOG_STEP_SECONDS))
  1524. {
  1525. fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s).\n",
  1526. j, (int64) naccounts * scale,
  1527. (int) (((int64) j * 100) / (naccounts * (int64) scale)), elapsed_sec, remaining_sec);
  1528. /* skip to the next interval */
  1529. log_interval = (int) ceil(elapsed_sec / LOG_STEP_SECONDS);
  1530. }
  1531. }
  1532. }
  1533. if (PQputline(con, "\\.\n"))
  1534. {
  1535. fprintf(stderr, "very last PQputline failed\n");
  1536. exit(1);
  1537. }
  1538. if (PQendcopy(con))
  1539. {
  1540. fprintf(stderr, "PQendcopy failed\n");
  1541. exit(1);
  1542. }
  1543. executeStatement(con, "commit");
  1544. /* vacuum */
  1545. if (!is_no_vacuum)
  1546. {
  1547. fprintf(stderr, "vacuum...\n");
  1548. executeStatement(con, "vacuum analyze pgbench_branches");
  1549. executeStatement(con, "vacuum analyze pgbench_tellers");
  1550. executeStatement(con, "vacuum analyze pgbench_accounts");
  1551. executeStatement(con, "vacuum analyze pgbench_history");
  1552. }
  1553. /*
  1554. * create indexes
  1555. */
  1556. fprintf(stderr, "set primary keys...\n");
  1557. for (i = 0; i < lengthof(DDLINDEXes); i++)
  1558. {
  1559. char buffer[256];
  1560. strlcpy(buffer, DDLINDEXes[i], sizeof(buffer));
  1561. if (index_tablespace != NULL)
  1562. {
  1563. char *escape_tablespace;
  1564. escape_tablespace = PQescapeIdentifier(con, index_tablespace,
  1565. strlen(index_tablespace));
  1566. snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
  1567. " using index tablespace %s", escape_tablespace);
  1568. PQfreemem(escape_tablespace);
  1569. }
  1570. executeStatement(con, buffer);
  1571. }
  1572. /*
  1573. * create foreign keys
  1574. */
  1575. if (foreign_keys)
  1576. {
  1577. fprintf(stderr, "set foreign keys...\n");
  1578. for (i = 0; i < lengthof(DDLKEYs); i++)
  1579. {
  1580. executeStatement(con, DDLKEYs[i]);
  1581. }
  1582. }
  1583. fprintf(stderr, "done.\n");
  1584. PQfinish(con);
  1585. }
  1586. /*
  1587. * Parse the raw sql and replace :param to $n.
  1588. */
  1589. static bool
  1590. parseQuery(Command *cmd, const char *raw_sql)
  1591. {
  1592. char *sql,
  1593. *p;
  1594. sql = pg_strdup(raw_sql);
  1595. cmd->argc = 1;
  1596. p = sql;
  1597. while ((p = strchr(p, ':')) != NULL)
  1598. {
  1599. char var[12];
  1600. char *name;
  1601. int eaten;
  1602. name = parseVariable(p, &eaten);
  1603. if (name == NULL)
  1604. {
  1605. while (*p == ':')
  1606. {
  1607. p++;
  1608. }
  1609. continue;
  1610. }
  1611. if (cmd->argc >= MAX_ARGS)
  1612. {
  1613. fprintf(stderr, "statement has too many arguments (maximum is %d): %s\n", MAX_ARGS - 1, raw_sql);
  1614. return false;
  1615. }
  1616. sprintf(var, "$%d", cmd->argc);
  1617. p = replaceVariable(&sql, p, eaten, var);
  1618. cmd->argv[cmd->argc] = name;
  1619. cmd->argc++;
  1620. }
  1621. cmd->argv[0] = sql;
  1622. return true;
  1623. }
  1624. /* Parse a command; return a Command struct, or NULL if it's a comment */
  1625. static Command *
  1626. process_commands(char *buf)
  1627. {
  1628. const char delim[] = " \f\n\r\t\v";
  1629. Command *my_commands;
  1630. int j;
  1631. char *p,
  1632. *tok;
  1633. /* Make the string buf end at the next newline */
  1634. if ((p = strchr(buf, '\n')) != NULL)
  1635. *p = '\0';
  1636. /* Skip leading whitespace */
  1637. p = buf;
  1638. while (isspace((unsigned char) *p))
  1639. p++;
  1640. /* If the line is empty or actually a comment, we're done */
  1641. if (*p == '\0' || strncmp(p, "--", 2) == 0)
  1642. return NULL;
  1643. /* Allocate and initialize Command structure */
  1644. my_commands = (Command *) pg_malloc(sizeof(Command));
  1645. my_commands->line = pg_strdup(buf);
  1646. my_commands->command_num = num_commands++;
  1647. my_commands->type = 0; /* until set */
  1648. my_commands->argc = 0;
  1649. if (*p == '\\')
  1650. {
  1651. my_commands->type = META_COMMAND;
  1652. j = 0;
  1653. tok = strtok(++p, delim);
  1654. while (tok != NULL)
  1655. {
  1656. my_commands->argv[j++] = pg_strdup(tok);
  1657. my_commands->argc++;
  1658. tok = strtok(NULL, delim);
  1659. }
  1660. if (pg_strcasecmp(my_commands->argv[0], "setrandom") == 0)
  1661. {
  1662. if (my_commands->argc < 4)
  1663. {
  1664. fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
  1665. exit(1);
  1666. }
  1667. for (j = 4; j < my_commands->argc; j++)
  1668. fprintf(stderr, "%s: extra argument \"%s\" ignored\n",
  1669. my_commands->argv[0], my_commands->argv[j]);
  1670. }
  1671. else if (pg_strcasecmp(my_commands->argv[0], "set") == 0)
  1672. {
  1673. if (my_commands->argc < 3)
  1674. {
  1675. fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
  1676. exit(1);
  1677. }
  1678. for (j = my_commands->argc < 5 ? 3 : 5; j < my_commands->argc; j++)
  1679. fprintf(stderr, "%s: extra argument \"%s\" ignored\n",
  1680. my_commands->argv[0], my_commands->argv[j]);
  1681. }
  1682. else if (pg_strcasecmp(my_commands->argv[0], "sleep") == 0)
  1683. {
  1684. if (my_commands->argc < 2)
  1685. {
  1686. fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
  1687. exit(1);
  1688. }
  1689. /*
  1690. * Split argument into number and unit to allow "sleep 1ms" etc.
  1691. * We don't have to terminate the number argument with null
  1692. * because it will be parsed with atoi, which ignores trailing
  1693. * non-digit characters.
  1694. */
  1695. if (my_commands->argv[1][0] != ':')
  1696. {
  1697. char *c = my_commands->argv[1];
  1698. while (isdigit((unsigned char) *c))
  1699. c++;
  1700. if (*c)
  1701. {
  1702. my_commands->argv[2] = c;
  1703. if (my_commands->argc < 3)
  1704. my_commands->argc = 3;
  1705. }
  1706. }
  1707. if (my_commands->argc >= 3)
  1708. {
  1709. if (pg_strcasecmp(my_commands->argv[2], "us") != 0 &&
  1710. pg_strcasecmp(my_commands->argv[2], "ms") != 0 &&
  1711. pg_strcasecmp(my_commands->argv[2], "s") != 0)
  1712. {
  1713. fprintf(stderr, "%s: unknown time unit '%s' - must be us, ms or s\n",
  1714. my_commands->argv[0], my_commands->argv[2]);
  1715. exit(1);
  1716. }
  1717. }
  1718. for (j = 3; j < my_commands->argc; j++)
  1719. fprintf(stderr, "%s: extra argument \"%s\" ignored\n",
  1720. my_commands->argv[0], my_commands->argv[j]);
  1721. }
  1722. else if (pg_strcasecmp(my_commands->argv[0], "setshell") == 0)
  1723. {
  1724. if (my_commands->argc < 3)
  1725. {
  1726. fprintf(stderr, "%s: missing argument\n", my_commands->argv[0]);
  1727. exit(1);
  1728. }
  1729. }
  1730. else if (pg_strcasecmp(my_commands->argv[0], "shell") == 0)
  1731. {
  1732. if (my_commands->argc < 1)
  1733. {
  1734. fprintf(stderr, "%s: missing command\n", my_commands->argv[0]);
  1735. exit(1);
  1736. }
  1737. }
  1738. else
  1739. {
  1740. fprintf(stderr, "Invalid command %s\n", my_commands->argv[0]);
  1741. exit(1);
  1742. }
  1743. }
  1744. else
  1745. {
  1746. my_commands->type = SQL_COMMAND;
  1747. switch (querymode)
  1748. {
  1749. case QUERY_SIMPLE:
  1750. my_commands->argv[0] = pg_strdup(p);
  1751. my_commands->argc++;
  1752. break;
  1753. case QUERY_EXTENDED:
  1754. case QUERY_PREPARED:
  1755. if (!parseQuery(my_commands, p))
  1756. exit(1);
  1757. break;
  1758. default:
  1759. exit(1);
  1760. }
  1761. }
  1762. return my_commands;
  1763. }
  1764. /*
  1765. * Read a line from fd, and return it in a malloc'd buffer.
  1766. * Return NULL at EOF.
  1767. *
  1768. * The buffer will typically be larger than necessary, but we don't care
  1769. * in this program, because we'll free it as soon as we've parsed the line.
  1770. */
  1771. static char *
  1772. read_line_from_file(FILE *fd)
  1773. {
  1774. char tmpbuf[BUFSIZ];
  1775. char *buf;
  1776. size_t buflen = BUFSIZ;
  1777. size_t used = 0;
  1778. buf = (char *) palloc(buflen);
  1779. buf[0] = '\0';
  1780. while (fgets(tmpbuf, BUFSIZ, fd) != NULL)
  1781. {
  1782. size_t thislen = strlen(tmpbuf);
  1783. /* Append tmpbuf to whatever we had already */
  1784. memcpy(buf + used, tmpbuf, thislen + 1);
  1785. used += thislen;
  1786. /* Done if we collected a newline */
  1787. if (thislen > 0 && tmpbuf[thislen - 1] == '\n')
  1788. break;
  1789. /* Else, enlarge buf to ensure we can append next bufferload */
  1790. buflen += BUFSIZ;
  1791. buf = (char *) pg_realloc(buf, buflen);
  1792. }
  1793. if (used > 0)
  1794. return buf;
  1795. /* Reached EOF */
  1796. free(buf);
  1797. return NULL;
  1798. }
  1799. static int
  1800. process_file(char *filename)
  1801. {
  1802. #define COMMANDS_ALLOC_NUM 128
  1803. Command **my_commands;
  1804. FILE *fd;
  1805. int lineno;
  1806. char *buf;
  1807. int alloc_num;
  1808. if (num_files >= MAX_FILES)
  1809. {
  1810. fprintf(stderr, "Up to only %d SQL files are allowed\n", MAX_FILES);
  1811. exit(1);
  1812. }
  1813. alloc_num = COMMANDS_ALLOC_NUM;
  1814. my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
  1815. if (strcmp(filename, "-") == 0)
  1816. fd = stdin;
  1817. else if ((fd = fopen(filename, "r")) == NULL)
  1818. {
  1819. fprintf(stderr, "%s: %s\n", filename, strerror(errno));
  1820. return false;
  1821. }
  1822. lineno = 0;
  1823. while ((buf = read_line_from_file(fd)) != NULL)
  1824. {
  1825. Command *command;
  1826. command = process_commands(buf);
  1827. free(buf);
  1828. if (command == NULL)
  1829. continue;
  1830. my_commands[lineno] = command;
  1831. lineno++;
  1832. if (lineno >= alloc_num)
  1833. {
  1834. alloc_num += COMMANDS_ALLOC_NUM;
  1835. my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num);
  1836. }
  1837. }
  1838. fclose(fd);
  1839. my_commands[lineno] = NULL;
  1840. sql_files[num_files++] = my_commands;
  1841. return true;
  1842. }
  1843. static Command **
  1844. process_builtin(char *tb)
  1845. {
  1846. #define COMMANDS_ALLOC_NUM 128
  1847. Command **my_commands;
  1848. int lineno;
  1849. char buf[BUFSIZ];
  1850. int alloc_num;
  1851. alloc_num = COMMANDS_ALLOC_NUM;
  1852. my_commands = (Command **) pg_malloc(sizeof(Command *) * alloc_num);
  1853. lineno = 0;
  1854. for (;;)
  1855. {
  1856. char *p;
  1857. Command *command;
  1858. p = buf;
  1859. while (*tb && *tb != '\n')
  1860. *p++ = *tb++;
  1861. if (*tb == '\0')
  1862. break;
  1863. if (*tb == '\n')
  1864. tb++;
  1865. *p = '\0';
  1866. command = process_commands(buf);
  1867. if (command == NULL)
  1868. continue;
  1869. my_commands[lineno] = command;
  1870. lineno++;
  1871. if (lineno >= alloc_num)
  1872. {
  1873. alloc_num += COMMANDS_ALLOC_NUM;
  1874. my_commands = pg_realloc(my_commands, sizeof(Command *) * alloc_num);
  1875. }
  1876. }
  1877. my_commands[lineno] = NULL;
  1878. return my_commands;
  1879. }
  1880. /* print out results */
  1881. static void
  1882. printResults(int ttype, int64 normal_xacts, int nclients,
  1883. TState *threads, int nthreads,
  1884. instr_time total_time, instr_time conn_total_time,
  1885. int64 total_latencies, int64 total_sqlats,
  1886. int64 throttle_lag, int64 throttle_lag_max)
  1887. {
  1888. double time_include,
  1889. tps_include,
  1890. tps_exclude;
  1891. char *s;
  1892. time_include = INSTR_TIME_GET_DOUBLE(total_time);
  1893. tps_include = normal_xacts / time_include;
  1894. tps_exclude = normal_xacts / (time_include -
  1895. (INSTR_TIME_GET_DOUBLE(conn_total_time) / nthreads));
  1896. if (ttype == 0)
  1897. s = "TPC-B (sort of)";
  1898. else if (ttype == 2)
  1899. s = "Update only pgbench_accounts";
  1900. else if (ttype == 1)
  1901. s = "SELECT only";
  1902. else
  1903. s = "Custom query";
  1904. printf("transaction type: %s\n", s);
  1905. printf("scaling factor: %d\n", scale);
  1906. printf("query mode: %s\n", QUERYMODE[querymode]);
  1907. printf("number of clients: %d\n", nclients);
  1908. printf("number of threads: %d\n", nthreads);
  1909. if (duration <= 0)
  1910. {
  1911. printf("number of transactions per client: %d\n", nxacts);
  1912. printf("number of transactions actually processed: " INT64_FORMAT "/" INT64_FORMAT "\n",
  1913. normal_xacts, (int64) nxacts * nclients);
  1914. }
  1915. else
  1916. {
  1917. printf("duration: %d s\n", duration);
  1918. printf("number of transactions actually processed: " INT64_FORMAT "\n",
  1919. normal_xacts);
  1920. }
  1921. if (throttle_delay || progress)
  1922. {
  1923. /* compute and show latency average and standard deviation */
  1924. double latency = 0.001 * total_latencies / normal_xacts;
  1925. double sqlat = (double) total_sqlats / normal_xacts;
  1926. printf("latency average: %.3f ms\n"
  1927. "latency stddev: %.3f ms\n",
  1928. latency, 0.001 * sqrt(sqlat - 1000000.0 * latency * latency));
  1929. }
  1930. else
  1931. {
  1932. /* only an average latency computed from the duration is available */
  1933. printf("latency average: %.3f ms\n",
  1934. 1000.0 * duration * nclients / normal_xacts);
  1935. }
  1936. if (throttle_delay)
  1937. {
  1938. /*
  1939. * Report average transaction lag under rate limit throttling. This
  1940. * is the delay between scheduled and actual start times for the
  1941. * transaction. The measured lag may be caused by thread/client load,
  1942. * the database load, or the Poisson throttling process.
  1943. */
  1944. printf("rate limit schedule lag: avg %.3f (max %.3f) ms\n",
  1945. 0.001 * throttle_lag / normal_xacts, 0.001 * throttle_lag_max);
  1946. }
  1947. printf("tps = %f (including connections establishing)\n", tps_include);
  1948. printf("tps = %f (excluding connections establishing)\n", tps_exclude);
  1949. /* Report per-command latencies */
  1950. if (is_latencies)
  1951. {
  1952. int i;
  1953. for (i = 0; i < num_files; i++)
  1954. {
  1955. Command **commands;
  1956. if (num_files > 1)
  1957. printf("statement latencies in milliseconds, file %d:\n", i + 1);
  1958. else
  1959. printf("statement latencies in milliseconds:\n");
  1960. for (commands = sql_files[i]; *commands != NULL; commands++)
  1961. {
  1962. Command *command = *commands;
  1963. int cnum = command->command_num;
  1964. double total_time;
  1965. instr_time total_exec_elapsed;
  1966. int total_exec_count;
  1967. int t;
  1968. /* Accumulate per-thread data for command */
  1969. INSTR_TIME_SET_ZERO(total_exec_elapsed);
  1970. total_exec_count = 0;
  1971. for (t = 0; t < nthreads; t++)
  1972. {
  1973. TState *thread = &threads[t];
  1974. INSTR_TIME_ADD(total_exec_elapsed,
  1975. thread->exec_elapsed[cnum]);
  1976. total_exec_count += thread->exec_count[cnum];
  1977. }
  1978. if (total_exec_count > 0)
  1979. total_time = INSTR_TIME_GET_MILLISEC(total_exec_elapsed) / (double) total_exec_count;
  1980. else
  1981. total_time = 0.0;
  1982. printf("\t%f\t%s\n", total_time, command->line);
  1983. }
  1984. }
  1985. }
  1986. }
  1987. int
  1988. main(int argc, char **argv)
  1989. {
  1990. static struct option long_options[] = {
  1991. /* systematic long/short named options */
  1992. {"client", required_argument, NULL, 'c'},
  1993. {"connect", no_argument, NULL, 'C'},
  1994. {"debug", no_argument, NULL, 'd'},
  1995. {"define", required_argument, NULL, 'D'},
  1996. {"file", required_argument, NULL, 'f'},
  1997. {"fillfactor", required_argument, NULL, 'F'},
  1998. {"host", required_argument, NULL, 'h'},
  1999. {"initialize", no_argument, NULL, 'i'},
  2000. {"jobs", required_argument, NULL, 'j'},
  2001. {"log", no_argument, NULL, 'l'},
  2002. {"no-vacuum", no_argument, NULL, 'n'},
  2003. {"port", required_argument, NULL, 'p'},
  2004. {"progress", required_argument, NULL, 'P'},
  2005. {"protocol", required_argument, NULL, 'M'},
  2006. {"quiet", no_argument, NULL, 'q'},
  2007. {"report-latencies", no_argument, NULL, 'r'},
  2008. {"scale", required_argument, NULL, 's'},
  2009. {"select-only", no_argument, NULL, 'S'},
  2010. {"skip-some-updates", no_argument, NULL, 'N'},
  2011. {"time", required_argument, NULL, 'T'},
  2012. {"transactions", required_argument, NULL, 't'},
  2013. {"username", required_argument, NULL, 'U'},
  2014. {"vacuum-all", no_argument, NULL, 'v'},
  2015. /* long-named only options */
  2016. {"foreign-keys", no_argument, &foreign_keys, 1},
  2017. {"index-tablespace", required_argument, NULL, 3},
  2018. {"tablespace", required_argument, NULL, 2},
  2019. {"unlogged-tables", no_argument, &unlogged_tables, 1},
  2020. {"sampling-rate", required_argument, NULL, 4},
  2021. {"aggregate-interval", required_argument, NULL, 5},
  2022. {"rate", required_argument, NULL, 'R'},
  2023. {NULL, 0, NULL, 0}
  2024. };
  2025. int c;
  2026. int nclients = 1; /* default number of simulated clients */
  2027. int nthreads = 1; /* default number of threads */
  2028. int is_init_mode = 0; /* initialize mode? */
  2029. int is_no_vacuum = 0; /* no vacuum at all before testing? */
  2030. int do_vacuum_accounts = 0; /* do vacuum accounts before testing? */
  2031. int ttype = 0; /* transaction type. 0: TPC-B, 1: SELECT only,
  2032. * 2: skip update of branches and tellers */
  2033. int optindex;
  2034. char *filename = NULL;
  2035. bool scale_given = false;
  2036. CState *state; /* status of clients */
  2037. TState *threads; /* array of thread */
  2038. instr_time start_time; /* start up time */
  2039. instr_time total_time;
  2040. instr_time conn_total_time;
  2041. int64 total_xacts = 0;
  2042. int64 total_latencies = 0;
  2043. int64 total_sqlats = 0;
  2044. int64 throttle_lag = 0;
  2045. int64 throttle_lag_max = 0;
  2046. int i;
  2047. #ifdef HAVE_GETRLIMIT
  2048. struct rlimit rlim;
  2049. #endif
  2050. PGconn *con;
  2051. PGresult *res;
  2052. char *env;
  2053. char val[64];
  2054. progname = get_progname(argv[0]);
  2055. if (argc > 1)
  2056. {
  2057. if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
  2058. {
  2059. usage();
  2060. exit(0);
  2061. }
  2062. if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
  2063. {
  2064. puts("pgbench (PostgreSQL) " PG_VERSION);
  2065. exit(0);
  2066. }
  2067. }
  2068. #ifdef WIN32
  2069. /* stderr is buffered on Win32. */
  2070. setvbuf(stderr, NULL, _IONBF, 0);
  2071. #endif
  2072. if ((env = getenv("PGHOST")) != NULL && *env != '\0')
  2073. pghost = env;
  2074. if ((env = getenv("PGPORT")) != NULL && *env != '\0')
  2075. pgport = env;
  2076. else if ((env = getenv("PGUSER")) != NULL && *env != '\0')
  2077. login = env;
  2078. state = (CState *) pg_malloc(sizeof(CState));
  2079. memset(state, 0, sizeof(CState));
  2080. while ((c = getopt_long(argc, argv, "ih:nvp:dqSNc:j:Crs:t:T:U:lf:D:F:M:P:R:", long_options, &optindex)) != -1)
  2081. {
  2082. switch (c)
  2083. {
  2084. case 'i':
  2085. is_init_mode++;
  2086. break;
  2087. case 'h':
  2088. pghost = pg_strdup(optarg);
  2089. break;
  2090. case 'n':
  2091. is_no_vacuum++;
  2092. break;
  2093. case 'v':
  2094. do_vacuum_accounts++;
  2095. break;
  2096. case 'p':
  2097. pgport = pg_strdup(optarg);
  2098. break;
  2099. case 'd':
  2100. debug++;
  2101. break;
  2102. case 'S':
  2103. ttype = 1;
  2104. break;
  2105. case 'N':
  2106. ttype = 2;
  2107. break;
  2108. case 'c':
  2109. nclients = atoi(optarg);
  2110. if (nclients <= 0 || nclients > MAXCLIENTS)
  2111. {
  2112. fprintf(stderr, "invalid number of clients: %d\n", nclients);
  2113. exit(1);
  2114. }
  2115. #ifdef HAVE_GETRLIMIT
  2116. #ifdef RLIMIT_NOFILE /* most platforms use RLIMIT_NOFILE */
  2117. if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
  2118. #else /* but BSD doesn't ... */
  2119. if (getrlimit(RLIMIT_OFILE, &rlim) == -1)
  2120. #endif /* RLIMIT_NOFILE */
  2121. {
  2122. fprintf(stderr, "getrlimit failed: %s\n", strerror(errno));
  2123. exit(1);
  2124. }
  2125. if (rlim.rlim_cur <= (nclients + 2))
  2126. {
  2127. fprintf(stderr, "You need at least %d open files but you are only allowed to use %ld.\n", nclients + 2, (long) rlim.rlim_cur);
  2128. fprintf(stderr, "Use limit/ulimit to increase the limit before using pgbench.\n");
  2129. exit(1);
  2130. }
  2131. #endif /* HAVE_GETRLIMIT */
  2132. break;
  2133. case 'j': /* jobs */
  2134. nthreads = atoi(optarg);
  2135. if (nthreads <= 0)
  2136. {
  2137. fprintf(stderr, "invalid number of threads: %d\n", nthreads);
  2138. exit(1);
  2139. }
  2140. break;
  2141. case 'C':
  2142. is_connect = true;
  2143. break;
  2144. case 'r':
  2145. is_latencies = true;
  2146. break;
  2147. case 's':
  2148. scale_given = true;
  2149. scale = atoi(optarg);
  2150. if (scale <= 0)
  2151. {
  2152. fprintf(stderr, "invalid scaling factor: %d\n", scale);
  2153. exit(1);
  2154. }
  2155. break;
  2156. case 't':
  2157. if (duration > 0)
  2158. {
  2159. fprintf(stderr, "specify either a number of transactions (-t) or a duration (-T), not both.\n");
  2160. exit(1);
  2161. }
  2162. nxacts = atoi(optarg);
  2163. if (nxacts <= 0)
  2164. {
  2165. fprintf(stderr, "invalid number of transactions: %d\n", nxacts);
  2166. exit(1);
  2167. }
  2168. break;
  2169. case 'T':
  2170. if (nxacts > 0)
  2171. {
  2172. fprintf(stderr, "specify either a number of transactions (-t) or a duration (-T), not both.\n");
  2173. exit(1);
  2174. }
  2175. duration = atoi(optarg);
  2176. if (duration <= 0)
  2177. {
  2178. fprintf(stderr, "invalid duration: %d\n", duration);
  2179. exit(1);
  2180. }
  2181. break;
  2182. case 'U':
  2183. login = pg_strdup(optarg);
  2184. break;
  2185. case 'l':
  2186. use_log = true;
  2187. break;
  2188. case 'q':
  2189. use_quiet = true;
  2190. break;
  2191. case 'f':
  2192. ttype = 3;
  2193. filename = pg_strdup(optarg);
  2194. if (process_file(filename) == false || *sql_files[num_files - 1] == NULL)
  2195. exit(1);
  2196. break;
  2197. case 'D':
  2198. {
  2199. char *p;
  2200. if ((p = strchr(optarg, '=')) == NULL || p == optarg || *(p + 1) == '\0')
  2201. {
  2202. fprintf(stderr, "invalid variable definition: %s\n", optarg);
  2203. exit(1);
  2204. }
  2205. *p++ = '\0';
  2206. if (!putVariable(&state[0], "option", optarg, p))
  2207. exit(1);
  2208. }
  2209. break;
  2210. case 'F':
  2211. fillfactor = atoi(optarg);
  2212. if ((fillfactor < 10) || (fillfactor > 100))
  2213. {
  2214. fprintf(stderr, "invalid fillfactor: %d\n", fillfactor);
  2215. exit(1);
  2216. }
  2217. break;
  2218. case 'M':
  2219. if (num_files > 0)
  2220. {
  2221. fprintf(stderr, "query mode (-M) should be specifiled before transaction scripts (-f)\n");
  2222. exit(1);
  2223. }
  2224. for (querymode = 0; querymode < NUM_QUERYMODE; querymode++)
  2225. if (strcmp(optarg, QUERYMODE[querymode]) == 0)
  2226. break;
  2227. if (querymode >= NUM_QUERYMODE)
  2228. {
  2229. fprintf(stderr, "invalid query mode (-M): %s\n", optarg);
  2230. exit(1);
  2231. }
  2232. break;
  2233. case 'P':
  2234. progress = atoi(optarg);
  2235. if (progress <= 0)
  2236. {
  2237. fprintf(stderr,
  2238. "thread progress delay (-P) must be positive (%s)\n",
  2239. optarg);
  2240. exit(1);
  2241. }
  2242. break;
  2243. case 'R':
  2244. {
  2245. /* get a double from the beginning of option value */
  2246. double throttle_value = atof(optarg);
  2247. if (throttle_value <= 0.0)
  2248. {
  2249. fprintf(stderr, "invalid rate limit: %s\n", optarg);
  2250. exit(1);
  2251. }
  2252. /* Invert rate limit into a time offset */
  2253. throttle_delay = (int64) (1000000.0 / throttle_value);
  2254. }
  2255. break;
  2256. case 0:
  2257. /* This covers long options which take no argument. */
  2258. break;
  2259. case 2: /* tablespace */
  2260. tablespace = pg_strdup(optarg);
  2261. break;
  2262. case 3: /* index-tablespace */
  2263. index_tablespace = pg_strdup(optarg);
  2264. break;
  2265. case 4:
  2266. sample_rate = atof(optarg);
  2267. if (sample_rate <= 0.0 || sample_rate > 1.0)
  2268. {
  2269. fprintf(stderr, "invalid sampling rate: %f\n", sample_rate);
  2270. exit(1);
  2271. }
  2272. break;
  2273. case 5:
  2274. #ifdef WIN32
  2275. fprintf(stderr, "--aggregate-interval is not currently supported on Windows");
  2276. exit(1);
  2277. #else
  2278. agg_interval = atoi(optarg);
  2279. if (agg_interval <= 0)
  2280. {
  2281. fprintf(stderr, "invalid number of seconds for aggregation: %d\n", agg_interval);
  2282. exit(1);
  2283. }
  2284. #endif
  2285. break;
  2286. default:
  2287. fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
  2288. exit(1);
  2289. break;
  2290. }
  2291. }
  2292. /* compute a per thread delay */
  2293. throttle_delay *= nthreads;
  2294. if (argc > optind)
  2295. dbName = argv[optind];
  2296. else
  2297. {
  2298. if ((env = getenv("PGDATABASE")) != NULL && *env != '\0')
  2299. dbName = env;
  2300. else if (login != NULL && *login != '\0')
  2301. dbName = login;
  2302. else
  2303. dbName = "";
  2304. }
  2305. if (is_init_mode)
  2306. {
  2307. init(is_no_vacuum);
  2308. exit(0);
  2309. }
  2310. /* Use DEFAULT_NXACTS if neither nxacts nor duration is specified. */
  2311. if (nxacts <= 0 && duration <= 0)
  2312. nxacts = DEFAULT_NXACTS;
  2313. if (nclients % nthreads != 0)
  2314. {
  2315. fprintf(stderr, "number of clients (%d) must be a multiple of number of threads (%d)\n", nclients, nthreads);
  2316. exit(1);
  2317. }
  2318. /* --sampling-rate may be used only with -l */
  2319. if (sample_rate > 0.0 && !use_log)
  2320. {
  2321. fprintf(stderr, "log sampling rate is allowed only when logging transactions (-l) \n");
  2322. exit(1);
  2323. }
  2324. /* -q may be used only with -i */
  2325. if (use_quiet && !is_init_mode)
  2326. {
  2327. fprintf(stderr, "quiet-logging is allowed only in initialization mode (-i)\n");
  2328. exit(1);
  2329. }
  2330. /* --sampling-rate may must not be used with --aggregate-interval */
  2331. if (sample_rate > 0.0 && agg_interval > 0)
  2332. {
  2333. fprintf(stderr, "log sampling (--sampling-rate) and aggregation (--aggregate-interval) can't be used at the same time\n");
  2334. exit(1);
  2335. }
  2336. if (agg_interval > 0 && (!use_log))
  2337. {
  2338. fprintf(stderr, "log aggregation is allowed only when actually logging transactions\n");
  2339. exit(1);
  2340. }
  2341. if ((duration > 0) && (agg_interval > duration))
  2342. {
  2343. fprintf(stderr, "number of seconds for aggregation (%d) must not be higher that test duration (%d)\n", agg_interval, duration);
  2344. exit(1);
  2345. }
  2346. if ((duration > 0) && (agg_interval > 0) && (duration % agg_interval != 0))
  2347. {
  2348. fprintf(stderr, "duration (%d) must be a multiple of aggregation interval (%d)\n", duration, agg_interval);
  2349. exit(1);
  2350. }
  2351. /*
  2352. * is_latencies only works with multiple threads in thread-based
  2353. * implementations, not fork-based ones, because it supposes that the
  2354. * parent can see changes made to the per-thread execution stats by child
  2355. * threads. It seems useful enough to accept despite this limitation, but
  2356. * perhaps we should FIXME someday (by passing the stats data back up
  2357. * through the parent-to-child pipes).
  2358. */
  2359. #ifndef ENABLE_THREAD_SAFETY
  2360. if (is_latencies && nthreads > 1)
  2361. {
  2362. fprintf(stderr, "-r does not work with -j larger than 1 on this platform.\n");
  2363. exit(1);
  2364. }
  2365. #endif
  2366. /*
  2367. * save main process id in the global variable because process id will be
  2368. * changed after fork.
  2369. */
  2370. main_pid = (int) getpid();
  2371. progress_nclients = nclients;
  2372. progress_nthreads = nthreads;
  2373. if (nclients > 1)
  2374. {
  2375. state = (CState *) pg_realloc(state, sizeof(CState) * nclients);
  2376. memset(state + 1, 0, sizeof(CState) * (nclients - 1));
  2377. /* copy any -D switch values to all clients */
  2378. for (i = 1; i < nclients; i++)
  2379. {
  2380. int j;
  2381. state[i].id = i;
  2382. for (j = 0; j < state[0].nvariables; j++)
  2383. {
  2384. if (!putVariable(&state[i], "startup", state[0].variables[j].name, state[0].variables[j].value))
  2385. exit(1);
  2386. }
  2387. }
  2388. }
  2389. if (debug)
  2390. {
  2391. if (duration <= 0)
  2392. printf("pghost: %s pgport: %s nclients: %d nxacts: %d dbName: %s\n",
  2393. pghost, pgport, nclients, nxacts, dbName);
  2394. else
  2395. printf("pghost: %s pgport: %s nclients: %d duration: %d dbName: %s\n",
  2396. pghost, pgport, nclients, duration, dbName);
  2397. }
  2398. /* opening connection... */
  2399. con = doConnect();
  2400. if (con == NULL)
  2401. exit(1);
  2402. if (PQstatus(con) == CONNECTION_BAD)
  2403. {
  2404. fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
  2405. fprintf(stderr, "%s", PQerrorMessage(con));
  2406. exit(1);
  2407. }
  2408. if (ttype != 3)
  2409. {
  2410. /*
  2411. * get the scaling factor that should be same as count(*) from
  2412. * pgbench_branches if this is not a custom query
  2413. */
  2414. res = PQexec(con, "select count(*) from pgbench_branches");
  2415. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  2416. {
  2417. fprintf(stderr, "%s", PQerrorMessage(con));
  2418. exit(1);
  2419. }
  2420. scale = atoi(PQgetvalue(res, 0, 0));
  2421. if (scale < 0)
  2422. {
  2423. fprintf(stderr, "count(*) from pgbench_branches invalid (%d)\n", scale);
  2424. exit(1);
  2425. }
  2426. PQclear(res);
  2427. /* warn if we override user-given -s switch */
  2428. if (scale_given)
  2429. fprintf(stderr,
  2430. "Scale option ignored, using pgbench_branches table count = %d\n",
  2431. scale);
  2432. }
  2433. /*
  2434. * :scale variables normally get -s or database scale, but don't override
  2435. * an explicit -D switch
  2436. */
  2437. if (getVariable(&state[0], "scale") == NULL)
  2438. {
  2439. snprintf(val, sizeof(val), "%d", scale);
  2440. for (i = 0; i < nclients; i++)
  2441. {
  2442. if (!putVariable(&state[i], "startup", "scale", val))
  2443. exit(1);
  2444. }
  2445. }
  2446. /*
  2447. * Define a :client_id variable that is unique per connection. But don't
  2448. * override an explicit -D switch.
  2449. */
  2450. if (getVariable(&state[0], "client_id") == NULL)
  2451. {
  2452. for (i = 0; i < nclients; i++)
  2453. {
  2454. snprintf(val, sizeof(val), "%d", i);
  2455. if (!putVariable(&state[i], "startup", "client_id", val))
  2456. exit(1);
  2457. }
  2458. }
  2459. if (!is_no_vacuum)
  2460. {
  2461. fprintf(stderr, "starting vacuum...");
  2462. executeStatement(con, "vacuum pgbench_branches");
  2463. executeStatement(con, "vacuum pgbench_tellers");
  2464. executeStatement(con, "truncate pgbench_history");
  2465. fprintf(stderr, "end.\n");
  2466. if (do_vacuum_accounts)
  2467. {
  2468. fprintf(stderr, "starting vacuum pgbench_accounts...");
  2469. executeStatement(con, "vacuum analyze pgbench_accounts");
  2470. fprintf(stderr, "end.\n");
  2471. }
  2472. }
  2473. PQfinish(con);
  2474. /* set random seed */
  2475. INSTR_TIME_SET_CURRENT(start_time);
  2476. srandom((unsigned int) INSTR_TIME_GET_MICROSEC(start_time));
  2477. /* process builtin SQL scripts */
  2478. switch (ttype)
  2479. {
  2480. case 0:
  2481. sql_files[0] = process_builtin(tpc_b);
  2482. num_files = 1;
  2483. break;
  2484. case 1:
  2485. sql_files[0] = process_builtin(select_only);
  2486. num_files = 1;
  2487. break;
  2488. case 2:
  2489. sql_files[0] = process_builtin(simple_update);
  2490. num_files = 1;
  2491. break;
  2492. default:
  2493. break;
  2494. }
  2495. /* set up thread data structures */
  2496. threads = (TState *) pg_malloc(sizeof(TState) * nthreads);
  2497. for (i = 0; i < nthreads; i++)
  2498. {
  2499. TState *thread = &threads[i];
  2500. thread->tid = i;
  2501. thread->state = &state[nclients / nthreads * i];
  2502. thread->nstate = nclients / nthreads;
  2503. thread->random_state[0] = random();
  2504. thread->random_state[1] = random();
  2505. thread->random_state[2] = random();
  2506. if (is_latencies)
  2507. {
  2508. /* Reserve memory for the thread to store per-command latencies */
  2509. int t;
  2510. thread->exec_elapsed = (instr_time *)
  2511. pg_malloc(sizeof(instr_time) * num_commands);
  2512. thread->exec_count = (int *)
  2513. pg_malloc(sizeof(int) * num_commands);
  2514. for (t = 0; t < num_commands; t++)
  2515. {
  2516. INSTR_TIME_SET_ZERO(thread->exec_elapsed[t]);
  2517. thread->exec_count[t] = 0;
  2518. }
  2519. }
  2520. else
  2521. {
  2522. thread->exec_elapsed = NULL;
  2523. thread->exec_count = NULL;
  2524. }
  2525. }
  2526. /* get start up time */
  2527. INSTR_TIME_SET_CURRENT(start_time);
  2528. /* set alarm if duration is specified. */
  2529. if (duration > 0)
  2530. setalarm(duration);
  2531. /* start threads */
  2532. for (i = 0; i < nthreads; i++)
  2533. {
  2534. TState *thread = &threads[i];
  2535. INSTR_TIME_SET_CURRENT(thread->start_time);
  2536. /* the first thread (i = 0) is executed by main thread */
  2537. if (i > 0)
  2538. {
  2539. int err = pthread_create(&thread->thread, NULL, threadRun, thread);
  2540. if (err != 0 || thread->thread == INVALID_THREAD)
  2541. {
  2542. fprintf(stderr, "cannot create thread: %s\n", strerror(err));
  2543. exit(1);
  2544. }
  2545. }
  2546. else
  2547. {
  2548. thread->thread = INVALID_THREAD;
  2549. }
  2550. }
  2551. /* wait for threads and accumulate results */
  2552. INSTR_TIME_SET_ZERO(conn_total_time);
  2553. for (i = 0; i < nthreads; i++)
  2554. {
  2555. void *ret = NULL;
  2556. if (threads[i].thread == INVALID_THREAD)
  2557. ret = threadRun(&threads[i]);
  2558. else
  2559. pthread_join(threads[i].thread, &ret);
  2560. if (ret != NULL)
  2561. {
  2562. TResult *r = (TResult *) ret;
  2563. total_xacts += r->xacts;
  2564. total_latencies += r->latencies;
  2565. total_sqlats += r->sqlats;
  2566. throttle_lag += r->throttle_lag;
  2567. if (r->throttle_lag_max > throttle_lag_max)
  2568. throttle_lag_max = r->throttle_lag_max;
  2569. INSTR_TIME_ADD(conn_total_time, r->conn_time);
  2570. free(ret);
  2571. }
  2572. }
  2573. disconnect_all(state, nclients);
  2574. /*
  2575. * XXX We compute results as though every client of every thread started
  2576. * and finished at the same time. That model can diverge noticeably from
  2577. * reality for a short benchmark run involving relatively many threads.
  2578. * The first thread may process notably many transactions before the last
  2579. * thread begins. Improving the model alone would bring limited benefit,
  2580. * because performance during those periods of partial thread count can
  2581. * easily exceed steady state performance. This is one of the many ways
  2582. * short runs convey deceptive performance figures.
  2583. */
  2584. INSTR_TIME_SET_CURRENT(total_time);
  2585. INSTR_TIME_SUBTRACT(total_time, start_time);
  2586. printResults(ttype, total_xacts, nclients, threads, nthreads,
  2587. total_time, conn_total_time, total_latencies, total_sqlats,
  2588. throttle_lag, throttle_lag_max);
  2589. return 0;
  2590. }
  2591. static void *
  2592. threadRun(void *arg)
  2593. {
  2594. TState *thread = (TState *) arg;
  2595. CState *state = thread->state;
  2596. TResult *result;
  2597. FILE *logfile = NULL; /* per-thread log file */
  2598. instr_time start,
  2599. end;
  2600. int nstate = thread->nstate;
  2601. int remains = nstate; /* number of remaining clients */
  2602. int i;
  2603. /* for reporting progress: */
  2604. int64 thread_start = INSTR_TIME_GET_MICROSEC(thread->start_time);
  2605. int64 last_report = thread_start;
  2606. int64 next_report = last_report + (int64) progress * 1000000;
  2607. int64 last_count = 0,
  2608. last_lats = 0,
  2609. last_sqlats = 0,
  2610. last_lags = 0;
  2611. AggVals aggs;
  2612. /*
  2613. * Initialize throttling rate target for all of the thread's clients. It
  2614. * might be a little more accurate to reset thread->start_time here too.
  2615. * The possible drift seems too small relative to typical throttle delay
  2616. * times to worry about it.
  2617. */
  2618. INSTR_TIME_SET_CURRENT(start);
  2619. thread->throttle_trigger = INSTR_TIME_GET_MICROSEC(start);
  2620. thread->throttle_lag = 0;
  2621. thread->throttle_lag_max = 0;
  2622. result = pg_malloc(sizeof(TResult));
  2623. INSTR_TIME_SET_ZERO(result->conn_time);
  2624. /* open log file if requested */
  2625. if (use_log)
  2626. {
  2627. char logpath[64];
  2628. if (thread->tid == 0)
  2629. snprintf(logpath, sizeof(logpath), "pgbench_log.%d", main_pid);
  2630. else
  2631. snprintf(logpath, sizeof(logpath), "pgbench_log.%d.%d", main_pid, thread->tid);
  2632. logfile = fopen(logpath, "w");
  2633. if (logfile == NULL)
  2634. {
  2635. fprintf(stderr, "Couldn't open logfile \"%s\": %s", logpath, strerror(errno));
  2636. goto done;
  2637. }
  2638. }
  2639. if (!is_connect)
  2640. {
  2641. /* make connections to the database */
  2642. for (i = 0; i < nstate; i++)
  2643. {
  2644. if ((state[i].con = doConnect()) == NULL)
  2645. goto done;
  2646. }
  2647. }
  2648. /* time after thread and connections set up */
  2649. INSTR_TIME_SET_CURRENT(result->conn_time);
  2650. INSTR_TIME_SUBTRACT(result->conn_time, thread->start_time);
  2651. agg_vals_init(&aggs, thread->start_time);
  2652. /* send start up queries in async manner */
  2653. for (i = 0; i < nstate; i++)
  2654. {
  2655. CState *st = &state[i];
  2656. Command **commands = sql_files[st->use_file];
  2657. int prev_ecnt = st->ecnt;
  2658. st->use_file = getrand(thread, 0, num_files - 1);
  2659. if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
  2660. remains--; /* I've aborted */
  2661. if (st->ecnt > prev_ecnt && commands[st->state]->type == META_COMMAND)
  2662. {
  2663. fprintf(stderr, "Client %d aborted in state %d. Execution meta-command failed.\n", i, st->state);
  2664. remains--; /* I've aborted */
  2665. PQfinish(st->con);
  2666. st->con = NULL;
  2667. }
  2668. }
  2669. while (remains > 0)
  2670. {
  2671. fd_set input_mask;
  2672. int maxsock; /* max socket number to be waited */
  2673. int64 now_usec = 0;
  2674. int64 min_usec;
  2675. FD_ZERO(&input_mask);
  2676. maxsock = -1;
  2677. min_usec = INT64_MAX;
  2678. for (i = 0; i < nstate; i++)
  2679. {
  2680. CState *st = &state[i];
  2681. Command **commands = sql_files[st->use_file];
  2682. int sock;
  2683. if (st->con == NULL)
  2684. {
  2685. continue;
  2686. }
  2687. else if (st->sleeping)
  2688. {
  2689. if (st->throttling && timer_exceeded)
  2690. {
  2691. /* interrupt client which has not started a transaction */
  2692. remains--;
  2693. st->sleeping = 0;
  2694. st->throttling = false;
  2695. PQfinish(st->con);
  2696. st->con = NULL;
  2697. continue;
  2698. }
  2699. else /* just a nap from the script */
  2700. {
  2701. int this_usec;
  2702. if (min_usec == INT64_MAX)
  2703. {
  2704. instr_time now;
  2705. INSTR_TIME_SET_CURRENT(now);
  2706. now_usec = INSTR_TIME_GET_MICROSEC(now);
  2707. }
  2708. this_usec = st->until - now_usec;
  2709. if (min_usec > this_usec)
  2710. min_usec = this_usec;
  2711. }
  2712. }
  2713. else if (commands[st->state]->type == META_COMMAND)
  2714. {
  2715. min_usec = 0; /* the connection is ready to run */
  2716. break;
  2717. }
  2718. sock = PQsocket(st->con);
  2719. if (sock < 0)
  2720. {
  2721. fprintf(stderr, "bad socket: %s\n", strerror(errno));
  2722. goto done;
  2723. }
  2724. FD_SET(sock, &input_mask);
  2725. if (maxsock < sock)
  2726. maxsock = sock;
  2727. }
  2728. if (min_usec > 0 && maxsock != -1)
  2729. {
  2730. int nsocks; /* return from select(2) */
  2731. if (min_usec != INT64_MAX)
  2732. {
  2733. struct timeval timeout;
  2734. timeout.tv_sec = min_usec / 1000000;
  2735. timeout.tv_usec = min_usec % 1000000;
  2736. nsocks = select(maxsock + 1, &input_mask, NULL, NULL, &timeout);
  2737. }
  2738. else
  2739. nsocks = select(maxsock + 1, &input_mask, NULL, NULL, NULL);
  2740. if (nsocks < 0)
  2741. {
  2742. if (errno == EINTR)
  2743. continue;
  2744. /* must be something wrong */
  2745. fprintf(stderr, "select failed: %s\n", strerror(errno));
  2746. goto done;
  2747. }
  2748. }
  2749. /* ok, backend returns reply */
  2750. for (i = 0; i < nstate; i++)
  2751. {
  2752. CState *st = &state[i];
  2753. Command **commands = sql_files[st->use_file];
  2754. int prev_ecnt = st->ecnt;
  2755. if (st->con && (FD_ISSET(PQsocket(st->con), &input_mask)
  2756. || commands[st->state]->type == META_COMMAND))
  2757. {
  2758. if (!doCustom(thread, st, &result->conn_time, logfile, &aggs))
  2759. remains--; /* I've aborted */
  2760. }
  2761. if (st->ecnt > prev_ecnt && commands[st->state]->type == META_COMMAND)
  2762. {
  2763. fprintf(stderr, "Client %d aborted in state %d. Execution of meta-command failed.\n", i, st->state);
  2764. remains--; /* I've aborted */
  2765. PQfinish(st->con);
  2766. st->con = NULL;
  2767. }
  2768. }
  2769. #ifdef PTHREAD_FORK_EMULATION
  2770. /* each process reports its own progression */
  2771. if (progress)
  2772. {
  2773. instr_time now_time;
  2774. int64 now;
  2775. INSTR_TIME_SET_CURRENT(now_time);
  2776. now = INSTR_TIME_GET_MICROSEC(now_time);
  2777. if (now >= next_report)
  2778. {
  2779. /* generate and show report */
  2780. int64 count = 0,
  2781. lats = 0,
  2782. sqlats = 0;
  2783. int64 lags = thread->throttle_lag;
  2784. int64 run = now - last_report;
  2785. double tps,
  2786. total_run,
  2787. latency,
  2788. sqlat,
  2789. stdev,
  2790. lag;
  2791. for (i = 0; i < nstate; i++)
  2792. {
  2793. count += state[i].cnt;
  2794. lats += state[i].txn_latencies;
  2795. sqlats += state[i].txn_sqlats;
  2796. }
  2797. total_run = (now - thread_start) / 1000000.0;
  2798. tps = 1000000.0 * (count - last_count) / run;
  2799. latency = 0.001 * (lats - last_lats) / (count - last_count);
  2800. sqlat = 1.0 * (sqlats - last_sqlats) / (count - last_count);
  2801. stdev = 0.001 * sqrt(sqlat - 1000000.0 * latency * latency);
  2802. lag = 0.001 * (lags - last_lags) / (count - last_count);
  2803. if (throttle_delay)
  2804. fprintf(stderr,
  2805. "progress %d: %.1f s, %.1f tps, "
  2806. "lat %.3f ms stddev %.3f, lag %.3f ms\n",
  2807. thread->tid, total_run, tps, latency, stdev, lag);
  2808. else
  2809. fprintf(stderr,
  2810. "progress %d: %.1f s, %.1f tps, "
  2811. "lat %.3f ms stddev %.3f\n",
  2812. thread->tid, total_run, tps, latency, stdev);
  2813. last_count = count;
  2814. last_lats = lats;
  2815. last_sqlats = sqlats;
  2816. last_lags = lags;
  2817. last_report = now;
  2818. next_report += (int64) progress *1000000;
  2819. }
  2820. }
  2821. #else
  2822. /* progress report by thread 0 for all threads */
  2823. if (progress && thread->tid == 0)
  2824. {
  2825. instr_time now_time;
  2826. int64 now;
  2827. INSTR_TIME_SET_CURRENT(now_time);
  2828. now = INSTR_TIME_GET_MICROSEC(now_time);
  2829. if (now >= next_report)
  2830. {
  2831. /* generate and show report */
  2832. int64 count = 0,
  2833. lats = 0,
  2834. sqlats = 0,
  2835. lags = 0;
  2836. int64 run = now - last_report;
  2837. double tps,
  2838. total_run,
  2839. latency,
  2840. sqlat,
  2841. lag,
  2842. stdev;
  2843. for (i = 0; i < progress_nclients; i++)
  2844. {
  2845. count += state[i].cnt;
  2846. lats += state[i].txn_latencies;
  2847. sqlats += state[i].txn_sqlats;
  2848. }
  2849. for (i = 0; i < progress_nthreads; i++)
  2850. lags += thread[i].throttle_lag;
  2851. total_run = (now - thread_start) / 1000000.0;
  2852. tps = 1000000.0 * (count - last_count) / run;
  2853. latency = 0.001 * (lats - last_lats) / (count - last_count);
  2854. sqlat = 1.0 * (sqlats - last_sqlats) / (count - last_count);
  2855. stdev = 0.001 * sqrt(sqlat - 1000000.0 * latency * latency);
  2856. lag = 0.001 * (lags - last_lags) / (count - last_count);
  2857. if (throttle_delay)
  2858. fprintf(stderr,
  2859. "progress: %.1f s, %.1f tps, "
  2860. "lat %.3f ms stddev %.3f, lag %.3f ms\n",
  2861. total_run, tps, latency, stdev, lag);
  2862. else
  2863. fprintf(stderr,
  2864. "progress: %.1f s, %.1f tps, "
  2865. "lat %.3f ms stddev %.3f\n",
  2866. total_run, tps, latency, stdev);
  2867. last_count = count;
  2868. last_lats = lats;
  2869. last_sqlats = sqlats;
  2870. last_lags = lags;
  2871. last_report = now;
  2872. next_report += (int64) progress *1000000;
  2873. }
  2874. }
  2875. #endif /* PTHREAD_FORK_EMULATION */
  2876. }
  2877. done:
  2878. INSTR_TIME_SET_CURRENT(start);
  2879. disconnect_all(state, nstate);
  2880. result->xacts = 0;
  2881. result->latencies = 0;
  2882. result->sqlats = 0;
  2883. for (i = 0; i < nstate; i++)
  2884. {
  2885. result->xacts += state[i].cnt;
  2886. result->latencies += state[i].txn_latencies;
  2887. result->sqlats += state[i].txn_sqlats;
  2888. }
  2889. result->throttle_lag = thread->throttle_lag;
  2890. result->throttle_lag_max = thread->throttle_lag_max;
  2891. INSTR_TIME_SET_CURRENT(end);
  2892. INSTR_TIME_ACCUM_DIFF(result->conn_time, end, start);
  2893. if (logfile)
  2894. fclose(logfile);
  2895. return result;
  2896. }
  2897. /*
  2898. * Support for duration option: set timer_exceeded after so many seconds.
  2899. */
  2900. #ifndef WIN32
  2901. static void
  2902. handle_sig_alarm(SIGNAL_ARGS)
  2903. {
  2904. timer_exceeded = true;
  2905. }
  2906. static void
  2907. setalarm(int seconds)
  2908. {
  2909. pqsignal(SIGALRM, handle_sig_alarm);
  2910. alarm(seconds);
  2911. }
  2912. #ifndef ENABLE_THREAD_SAFETY
  2913. /*
  2914. * implements pthread using fork.
  2915. */
  2916. typedef struct fork_pthread
  2917. {
  2918. pid_t pid;
  2919. int pipes[2];
  2920. } fork_pthread;
  2921. static int
  2922. pthread_create(pthread_t *thread,
  2923. pthread_attr_t *attr,
  2924. void *(*start_routine) (void *),
  2925. void *arg)
  2926. {
  2927. fork_pthread *th;
  2928. void *ret;
  2929. int rc;
  2930. th = (fork_pthread *) pg_malloc(sizeof(fork_pthread));
  2931. if (pipe(th->pipes) < 0)
  2932. {
  2933. free(th);
  2934. return errno;
  2935. }
  2936. th->pid = fork();
  2937. if (th->pid == -1) /* error */
  2938. {
  2939. free(th);
  2940. return errno;
  2941. }
  2942. if (th->pid != 0) /* in parent process */
  2943. {
  2944. close(th->pipes[1]);
  2945. *thread = th;
  2946. return 0;
  2947. }
  2948. /* in child process */
  2949. close(th->pipes[0]);
  2950. /* set alarm again because the child does not inherit timers */
  2951. if (duration > 0)
  2952. setalarm(duration);
  2953. ret = start_routine(arg);
  2954. rc = write(th->pipes[1], ret, sizeof(TResult));
  2955. (void) rc;
  2956. close(th->pipes[1]);
  2957. free(th);
  2958. exit(0);
  2959. }
  2960. static int
  2961. pthread_join(pthread_t th, void **thread_return)
  2962. {
  2963. int status;
  2964. while (waitpid(th->pid, &status, 0) != th->pid)
  2965. {
  2966. if (errno != EINTR)
  2967. return errno;
  2968. }
  2969. if (thread_return != NULL)
  2970. {
  2971. /* assume result is TResult */
  2972. *thread_return = pg_malloc(sizeof(TResult));
  2973. if (read(th->pipes[0], *thread_return, sizeof(TResult)) != sizeof(TResult))
  2974. {
  2975. free(*thread_return);
  2976. *thread_return = NULL;
  2977. }
  2978. }
  2979. close(th->pipes[0]);
  2980. free(th);
  2981. return 0;
  2982. }
  2983. #endif
  2984. #else /* WIN32 */
  2985. static VOID CALLBACK
  2986. win32_timer_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
  2987. {
  2988. timer_exceeded = true;
  2989. }
  2990. static void
  2991. setalarm(int seconds)
  2992. {
  2993. HANDLE queue;
  2994. HANDLE timer;
  2995. /* This function will be called at most once, so we can cheat a bit. */
  2996. queue = CreateTimerQueue();
  2997. if (seconds > ((DWORD) -1) / 1000 ||
  2998. !CreateTimerQueueTimer(&timer, queue,
  2999. win32_timer_callback, NULL, seconds * 1000, 0,
  3000. WT_EXECUTEINTIMERTHREAD | WT_EXECUTEONLYONCE))
  3001. {
  3002. fprintf(stderr, "Failed to set timer\n");
  3003. exit(1);
  3004. }
  3005. }
  3006. /* partial pthread implementation for Windows */
  3007. typedef struct win32_pthread
  3008. {
  3009. HANDLE handle;
  3010. void *(*routine) (void *);
  3011. void *arg;
  3012. void *result;
  3013. } win32_pthread;
  3014. static unsigned __stdcall
  3015. win32_pthread_run(void *arg)
  3016. {
  3017. win32_pthread *th = (win32_pthread *) arg;
  3018. th->result = th->routine(th->arg);
  3019. return 0;
  3020. }
  3021. static int
  3022. pthread_create(pthread_t *thread,
  3023. pthread_attr_t *attr,
  3024. void *(*start_routine) (void *),
  3025. void *arg)
  3026. {
  3027. int save_errno;
  3028. win32_pthread *th;
  3029. th = (win32_pthread *) pg_malloc(sizeof(win32_pthread));
  3030. th->routine = start_routine;
  3031. th->arg = arg;
  3032. th->result = NULL;
  3033. th->handle = (HANDLE) _beginthreadex(NULL, 0, win32_pthread_run, th, 0, NULL);
  3034. if (th->handle == NULL)
  3035. {
  3036. save_errno = errno;
  3037. free(th);
  3038. return save_errno;
  3039. }
  3040. *thread = th;
  3041. return 0;
  3042. }
  3043. static int
  3044. pthread_join(pthread_t th, void **thread_return)
  3045. {
  3046. if (th == NULL || th->handle == NULL)
  3047. return errno = EINVAL;
  3048. if (WaitForSingleObject(th->handle, INFINITE) != WAIT_OBJECT_0)
  3049. {
  3050. _dosmaperr(GetLastError());
  3051. return errno;
  3052. }
  3053. if (thread_return)
  3054. *thread_return = th->result;
  3055. CloseHandle(th->handle);
  3056. free(th);
  3057. return 0;
  3058. }
  3059. #endif /* WIN32 */