blob: 33f4a3fa715e15cd284983910127e4caa86328c1 [file] [log] [blame]
drh27338e62013-04-06 00:19:37 +00001/*
2** 2013-04-05
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This is a program used for testing SQLite, and specifically for testing
14** the ability of independent processes to access the same SQLite database
15** concurrently.
16**
17** Compile this program as follows:
18**
19** gcc -g -c -Wall sqlite3.c $(OPTS)
20** gcc -g -o mptest mptest.c sqlite3.o $(LIBS)
21**
22** Recommended options:
23**
24** -DHAVE_USLEEP
25** -DSQLITE_NO_SYNC
26** -DSQLITE_THREADSAFE=0
27** -DSQLITE_OMIT_LOAD_EXTENSION
28**
29** Run like this:
30**
31** ./mptest $database $script
32**
33** where $database is the database to use for testing and $script is a
34** test script.
35*/
36#include "sqlite3.h"
37#include <stdio.h>
drhbc94dbb2013-04-08 14:28:33 +000038#if defined(_WIN32)
mistachkin08d41892013-04-11 00:09:44 +000039# define WIN32_LEAN_AND_MEAN
40# include <windows.h>
drhbc94dbb2013-04-08 14:28:33 +000041#else
42# include <unistd.h>
43#endif
drh27338e62013-04-06 00:19:37 +000044#include <stdlib.h>
45#include <string.h>
46#include <assert.h>
47#include <ctype.h>
48
mistachkin08d41892013-04-11 00:09:44 +000049/* The suffix to append to the child command lines, if any */
50#if defined(_WIN32)
51# define CMDLINE_SUFFIX ""
mistachkinfdd72c92013-04-11 21:13:10 +000052# define GETPID (int)GetCurrentProcessId
mistachkin08d41892013-04-11 00:09:44 +000053#else
54# define CMDLINE_SUFFIX "&"
mistachkinfdd72c92013-04-11 21:13:10 +000055# define GETPID getpid
mistachkin08d41892013-04-11 00:09:44 +000056#endif
57
drh841810c2013-04-08 13:59:11 +000058/* Mark a parameter as unused to suppress compiler warnings */
59#define UNUSED_PARAMETER(x) (void)x
60
drh27338e62013-04-06 00:19:37 +000061/* Global data
62*/
63static struct Global {
64 char *argv0; /* Name of the executable */
65 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */
66 char *zDbFile; /* Name of the database */
67 sqlite3 *db; /* Open connection to database */
68 char *zErrLog; /* Filename for error log */
69 FILE *pErrLog; /* Where to write errors */
70 char *zLog; /* Name of output log file */
71 FILE *pLog; /* Where to write log messages */
drhe3be8c82013-04-11 11:53:45 +000072 char zName[32]; /* Symbolic name of this process */
drh27338e62013-04-06 00:19:37 +000073 int taskId; /* Task ID. 0 means supervisor. */
74 int iTrace; /* Tracing level */
75 int bSqlTrace; /* True to trace SQL commands */
76 int nError; /* Number of errors */
drh3f5bc382013-04-06 13:09:11 +000077 int nTest; /* Number of --match operators */
78 int iTimeout; /* Milliseconds until a busy timeout */
drhbc94dbb2013-04-08 14:28:33 +000079 int bSync; /* Call fsync() */
drh27338e62013-04-06 00:19:37 +000080} g;
81
drh3f5bc382013-04-06 13:09:11 +000082/* Default timeout */
83#define DEFAULT_TIMEOUT 10000
84
drh27338e62013-04-06 00:19:37 +000085/*
86** Print a message adding zPrefix[] to the beginning of every line.
87*/
88static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){
89 while( zMsg && zMsg[0] ){
90 int i;
91 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){}
92 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg);
93 zMsg += i;
94 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++;
95 }
96}
97
98/*
99** Compare two pointers to strings, where the pointers might be NULL.
100*/
101static int safe_strcmp(const char *a, const char *b){
102 if( a==b ) return 0;
103 if( a==0 ) return -1;
104 if( b==0 ) return 1;
105 return strcmp(a,b);
106}
107
108/*
109** Return TRUE if string z[] matches glob pattern zGlob[].
110** Return FALSE if the pattern does not match.
111**
112** Globbing rules:
113**
114** '*' Matches any sequence of zero or more characters.
115**
116** '?' Matches exactly one character.
117**
118** [...] Matches one character from the enclosed list of
119** characters.
120**
121** [^...] Matches one character not in the enclosed list.
122**
123** '#' Matches any sequence of one or more digits with an
124** optional + or - sign in front
125*/
126int strglob(const char *zGlob, const char *z){
127 int c, c2;
128 int invert;
129 int seen;
130
131 while( (c = (*(zGlob++)))!=0 ){
132 if( c=='*' ){
133 while( (c=(*(zGlob++))) == '*' || c=='?' ){
134 if( c=='?' && (*(z++))==0 ) return 0;
135 }
136 if( c==0 ){
137 return 1;
138 }else if( c=='[' ){
139 while( *z && strglob(zGlob-1,z) ){
140 z++;
141 }
142 return (*z)!=0;
143 }
144 while( (c2 = (*(z++)))!=0 ){
145 while( c2!=c ){
146 c2 = *(z++);
147 if( c2==0 ) return 0;
148 }
149 if( strglob(zGlob,z) ) return 1;
150 }
151 return 0;
152 }else if( c=='?' ){
153 if( (*(z++))==0 ) return 0;
154 }else if( c=='[' ){
155 int prior_c = 0;
156 seen = 0;
157 invert = 0;
158 c = *(z++);
159 if( c==0 ) return 0;
160 c2 = *(zGlob++);
161 if( c2=='^' ){
162 invert = 1;
163 c2 = *(zGlob++);
164 }
165 if( c2==']' ){
166 if( c==']' ) seen = 1;
167 c2 = *(zGlob++);
168 }
169 while( c2 && c2!=']' ){
170 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
171 c2 = *(zGlob++);
172 if( c>=prior_c && c<=c2 ) seen = 1;
173 prior_c = 0;
174 }else{
175 if( c==c2 ){
176 seen = 1;
177 }
178 prior_c = c2;
179 }
180 c2 = *(zGlob++);
181 }
182 if( c2==0 || (seen ^ invert)==0 ) return 0;
183 }else if( c=='#' ){
184 if( (z[0]=='-' || z[0]=='+') && isdigit(z[1]) ) z++;
185 if( !isdigit(z[0]) ) return 0;
186 z++;
187 while( isdigit(z[0]) ){ z++; }
188 }else{
189 if( c!=(*(z++)) ) return 0;
190 }
191 }
192 return *z==0;
193}
194
195/*
196** Close output stream pOut if it is not stdout or stderr
197*/
198static void maybeClose(FILE *pOut){
199 if( pOut!=stdout && pOut!=stderr ) fclose(pOut);
200}
201
202/*
203** Print an error message
204*/
205static void errorMessage(const char *zFormat, ...){
206 va_list ap;
207 char *zMsg;
208 char zPrefix[30];
209 va_start(ap, zFormat);
210 zMsg = sqlite3_vmprintf(zFormat, ap);
211 va_end(ap);
212 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName);
213 if( g.pLog ){
214 printWithPrefix(g.pLog, zPrefix, zMsg);
215 fflush(g.pLog);
216 }
217 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
218 printWithPrefix(g.pErrLog, zPrefix, zMsg);
219 fflush(g.pErrLog);
220 }
221 sqlite3_free(zMsg);
222 g.nError++;
223}
224
225/* Forward declaration */
226static int trySql(const char*, ...);
227
228/*
229** Print an error message and then quit.
230*/
231static void fatalError(const char *zFormat, ...){
232 va_list ap;
233 char *zMsg;
234 char zPrefix[30];
235 va_start(ap, zFormat);
236 zMsg = sqlite3_vmprintf(zFormat, ap);
237 va_end(ap);
238 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName);
239 if( g.pLog ){
240 printWithPrefix(g.pLog, zPrefix, zMsg);
241 fflush(g.pLog);
242 maybeClose(g.pLog);
243 }
244 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
245 printWithPrefix(g.pErrLog, zPrefix, zMsg);
246 fflush(g.pErrLog);
247 maybeClose(g.pErrLog);
248 }
249 sqlite3_free(zMsg);
250 if( g.db ){
251 int nTry = 0;
drh3f5bc382013-04-06 13:09:11 +0000252 g.iTimeout = 0;
253 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
254 && (nTry++)<100 ){
drh27338e62013-04-06 00:19:37 +0000255 sqlite3_sleep(10);
256 }
257 }
258 sqlite3_close(g.db);
259 exit(1);
260}
261
262
263/*
264** Print a log message
265*/
266static void logMessage(const char *zFormat, ...){
267 va_list ap;
268 char *zMsg;
269 char zPrefix[30];
270 va_start(ap, zFormat);
271 zMsg = sqlite3_vmprintf(zFormat, ap);
272 va_end(ap);
273 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName);
274 if( g.pLog ){
275 printWithPrefix(g.pLog, zPrefix, zMsg);
276 fflush(g.pLog);
277 }
278 sqlite3_free(zMsg);
279}
280
281/*
282** Return the length of a string omitting trailing whitespace
283*/
284static int clipLength(const char *z){
285 int n = (int)strlen(z);
286 while( n>0 && isspace(z[n-1]) ){ n--; }
287 return n;
288}
289
290/*
drh1bf44c72013-04-08 13:48:29 +0000291** Auxiliary SQL function to return the name of the VFS
292*/
293static void vfsNameFunc(
294 sqlite3_context *context,
295 int argc,
296 sqlite3_value **argv
297){
298 sqlite3 *db = sqlite3_context_db_handle(context);
299 char *zVfs = 0;
drh841810c2013-04-08 13:59:11 +0000300 UNUSED_PARAMETER(argc);
301 UNUSED_PARAMETER(argv);
drh1bf44c72013-04-08 13:48:29 +0000302 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs);
303 if( zVfs ){
304 sqlite3_result_text(context, zVfs, -1, sqlite3_free);
305 }
306}
307
308/*
drh3f5bc382013-04-06 13:09:11 +0000309** Busy handler with a g.iTimeout-millisecond timeout
310*/
311static int busyHandler(void *pCD, int count){
drh841810c2013-04-08 13:59:11 +0000312 UNUSED_PARAMETER(pCD);
drh3f5bc382013-04-06 13:09:11 +0000313 if( count*10>g.iTimeout ){
314 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout);
315 return 0;
316 }
317 sqlite3_sleep(10);
318 return 1;
319}
320
321/*
drh27338e62013-04-06 00:19:37 +0000322** SQL Trace callback
323*/
324static void sqlTraceCallback(void *NotUsed1, const char *zSql){
drh841810c2013-04-08 13:59:11 +0000325 UNUSED_PARAMETER(NotUsed1);
drh27338e62013-04-06 00:19:37 +0000326 logMessage("[%.*s]", clipLength(zSql), zSql);
327}
328
329/*
drh1790bb32013-04-06 14:30:29 +0000330** SQL error log callback
331*/
332static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){
drh841810c2013-04-08 13:59:11 +0000333 UNUSED_PARAMETER(pArg);
drh1790bb32013-04-06 14:30:29 +0000334 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return;
drhe5ebd222013-04-08 15:36:51 +0000335 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return;
drhe3be8c82013-04-11 11:53:45 +0000336 if( (iErrCode&0xff)==SQLITE_NOTICE ){
drhab755ac2013-04-09 18:36:36 +0000337 logMessage("(info) %s", zMsg);
338 }else{
339 errorMessage("(errcode=%d) %s", iErrCode, zMsg);
340 }
drh1790bb32013-04-06 14:30:29 +0000341}
342
343/*
drh27338e62013-04-06 00:19:37 +0000344** Prepare an SQL statement. Issue a fatal error if unable.
345*/
346static sqlite3_stmt *prepareSql(const char *zFormat, ...){
347 va_list ap;
348 char *zSql;
349 int rc;
350 sqlite3_stmt *pStmt = 0;
351 va_start(ap, zFormat);
352 zSql = sqlite3_vmprintf(zFormat, ap);
353 va_end(ap);
354 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
355 if( rc!=SQLITE_OK ){
356 sqlite3_finalize(pStmt);
357 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
358 }
359 sqlite3_free(zSql);
360 return pStmt;
361}
362
363/*
364** Run arbitrary SQL. Issue a fatal error on failure.
365*/
366static void runSql(const char *zFormat, ...){
367 va_list ap;
368 char *zSql;
369 int rc;
370 va_start(ap, zFormat);
371 zSql = sqlite3_vmprintf(zFormat, ap);
372 va_end(ap);
373 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
374 if( rc!=SQLITE_OK ){
375 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
376 }
377 sqlite3_free(zSql);
378}
379
380/*
381** Try to run arbitrary SQL. Return success code.
382*/
383static int trySql(const char *zFormat, ...){
384 va_list ap;
385 char *zSql;
386 int rc;
387 va_start(ap, zFormat);
388 zSql = sqlite3_vmprintf(zFormat, ap);
389 va_end(ap);
390 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
391 sqlite3_free(zSql);
392 return rc;
393}
394
395/* Structure for holding an arbitrary length string
396*/
397typedef struct String String;
398struct String {
399 char *z; /* the string */
400 int n; /* Slots of z[] used */
401 int nAlloc; /* Slots of z[] allocated */
402};
403
404/* Free a string */
405static void stringFree(String *p){
406 if( p->z ) sqlite3_free(p->z);
407 memset(p, 0, sizeof(*p));
408}
409
410/* Append n bytes of text to a string. If n<0 append the entire string. */
411static void stringAppend(String *p, const char *z, int n){
412 if( n<0 ) n = (int)strlen(z);
413 if( p->n+n>=p->nAlloc ){
414 int nAlloc = p->nAlloc*2 + n + 100;
415 char *z = sqlite3_realloc(p->z, nAlloc);
416 if( z==0 ) fatalError("out of memory");
417 p->z = z;
418 p->nAlloc = nAlloc;
419 }
420 memcpy(p->z+p->n, z, n);
421 p->n += n;
422 p->z[p->n] = 0;
423}
424
425/* Reset a string to an empty string */
426static void stringReset(String *p){
427 if( p->z==0 ) stringAppend(p, " ", 1);
428 p->n = 0;
429 p->z[0] = 0;
430}
431
432/* Append a new token onto the end of the string */
433static void stringAppendTerm(String *p, const char *z){
434 int i;
435 if( p->n ) stringAppend(p, " ", 1);
436 if( z==0 ){
437 stringAppend(p, "nil", 3);
438 return;
439 }
440 for(i=0; z[i] && !isspace(z[i]); i++){}
441 if( i>0 && z[i]==0 ){
442 stringAppend(p, z, i);
443 return;
444 }
445 stringAppend(p, "'", 1);
446 while( z[0] ){
447 for(i=0; z[i] && z[i]!='\''; i++){}
448 if( z[i] ){
449 stringAppend(p, z, i+1);
450 stringAppend(p, "'", 1);
451 z += i+1;
452 }else{
453 stringAppend(p, z, i);
454 break;
455 }
456 }
457 stringAppend(p, "'", 1);
458}
459
460/*
461** Callback function for evalSql()
462*/
463static int evalCallback(void *pCData, int argc, char **argv, char **azCol){
464 String *p = (String*)pCData;
465 int i;
drh841810c2013-04-08 13:59:11 +0000466 UNUSED_PARAMETER(azCol);
drh27338e62013-04-06 00:19:37 +0000467 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]);
468 return 0;
469}
470
471/*
472** Run arbitrary SQL and record the results in an output string
473** given by the first parameter.
474*/
475static int evalSql(String *p, const char *zFormat, ...){
476 va_list ap;
477 char *zSql;
478 int rc;
479 char *zErrMsg = 0;
480 va_start(ap, zFormat);
481 zSql = sqlite3_vmprintf(zFormat, ap);
482 va_end(ap);
drh3f5bc382013-04-06 13:09:11 +0000483 assert( g.iTimeout>0 );
drh27338e62013-04-06 00:19:37 +0000484 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg);
485 sqlite3_free(zSql);
486 if( rc ){
487 char zErr[30];
488 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc);
489 stringAppendTerm(p, zErr);
490 if( zErrMsg ){
491 stringAppendTerm(p, zErrMsg);
492 sqlite3_free(zErrMsg);
493 }
494 }
495 return rc;
496}
497
498/*
drh1bf44c72013-04-08 13:48:29 +0000499** Auxiliary SQL function to recursively evaluate SQL.
500*/
501static void evalFunc(
502 sqlite3_context *context,
503 int argc,
504 sqlite3_value **argv
505){
506 sqlite3 *db = sqlite3_context_db_handle(context);
507 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
508 String res;
509 char *zErrMsg = 0;
510 int rc;
drh841810c2013-04-08 13:59:11 +0000511 UNUSED_PARAMETER(argc);
drh1bf44c72013-04-08 13:48:29 +0000512 memset(&res, 0, sizeof(res));
513 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg);
514 if( zErrMsg ){
515 sqlite3_result_error(context, zErrMsg, -1);
516 sqlite3_free(zErrMsg);
517 }else if( rc ){
518 sqlite3_result_error_code(context, rc);
519 }else{
520 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT);
521 }
522 stringFree(&res);
523}
524
525/*
drh27338e62013-04-06 00:19:37 +0000526** Look up the next task for client iClient in the database.
527** Return the task script and the task number and mark that
528** task as being under way.
529*/
530static int startScript(
531 int iClient, /* The client number */
532 char **pzScript, /* Write task script here */
drh4c5298f2013-04-10 12:01:21 +0000533 int *pTaskId, /* Write task number here */
534 char **pzTaskName /* Name of the task */
drh27338e62013-04-06 00:19:37 +0000535){
536 sqlite3_stmt *pStmt = 0;
537 int taskId;
538 int rc;
drh3f5bc382013-04-06 13:09:11 +0000539 int totalTime = 0;
drh27338e62013-04-06 00:19:37 +0000540
541 *pzScript = 0;
drh3f5bc382013-04-06 13:09:11 +0000542 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000543 while(1){
drhf90e50f2013-04-08 19:13:48 +0000544 rc = trySql("BEGIN IMMEDIATE");
drh27338e62013-04-06 00:19:37 +0000545 if( rc==SQLITE_BUSY ){
546 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +0000547 totalTime += 10;
drh27338e62013-04-06 00:19:37 +0000548 continue;
549 }
550 if( rc!=SQLITE_OK ){
drh6adab7a2013-04-08 18:58:00 +0000551 fatalError("in startScript: %s", sqlite3_errmsg(g.db));
drh27338e62013-04-06 00:19:37 +0000552 }
drh3f5bc382013-04-06 13:09:11 +0000553 if( g.nError || g.nTest ){
554 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d",
555 g.nError, g.nTest);
drh27338e62013-04-06 00:19:37 +0000556 g.nError = 0;
drh3f5bc382013-04-06 13:09:11 +0000557 g.nTest = 0;
558 }
559 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient);
560 rc = sqlite3_step(pStmt);
561 sqlite3_finalize(pStmt);
562 if( rc==SQLITE_ROW ){
563 runSql("DELETE FROM client WHERE id=%d", iClient);
drh3f5bc382013-04-06 13:09:11 +0000564 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000565 runSql("COMMIT TRANSACTION;");
drh3f5bc382013-04-06 13:09:11 +0000566 return SQLITE_DONE;
drh27338e62013-04-06 00:19:37 +0000567 }
568 pStmt = prepareSql(
drh4c5298f2013-04-10 12:01:21 +0000569 "SELECT script, id, name FROM task"
drh27338e62013-04-06 00:19:37 +0000570 " WHERE client=%d AND starttime IS NULL"
571 " ORDER BY id LIMIT 1", iClient);
572 rc = sqlite3_step(pStmt);
573 if( rc==SQLITE_ROW ){
574 int n = sqlite3_column_bytes(pStmt, 0);
drhf90e50f2013-04-08 19:13:48 +0000575 *pzScript = sqlite3_malloc(n+1);
drh27338e62013-04-06 00:19:37 +0000576 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0));
577 *pTaskId = taskId = sqlite3_column_int(pStmt, 1);
drh4c5298f2013-04-10 12:01:21 +0000578 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2));
drh27338e62013-04-06 00:19:37 +0000579 sqlite3_finalize(pStmt);
580 runSql("UPDATE task"
581 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
582 " WHERE id=%d;", taskId);
drh3f5bc382013-04-06 13:09:11 +0000583 g.iTimeout = DEFAULT_TIMEOUT;
drhf90e50f2013-04-08 19:13:48 +0000584 runSql("COMMIT TRANSACTION;");
drh27338e62013-04-06 00:19:37 +0000585 return SQLITE_OK;
586 }
587 sqlite3_finalize(pStmt);
588 if( rc==SQLITE_DONE ){
drh3f5bc382013-04-06 13:09:11 +0000589 if( totalTime>30000 ){
590 errorMessage("Waited over 30 seconds with no work. Giving up.");
591 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient);
592 sqlite3_close(g.db);
593 exit(1);
594 }
drhf90e50f2013-04-08 19:13:48 +0000595 while( trySql("COMMIT")==SQLITE_BUSY ){
596 sqlite3_sleep(10);
597 totalTime += 10;
598 }
drh27338e62013-04-06 00:19:37 +0000599 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +0000600 totalTime += 100;
drh27338e62013-04-06 00:19:37 +0000601 continue;
602 }
603 fatalError("%s", sqlite3_errmsg(g.db));
604 }
drh3f5bc382013-04-06 13:09:11 +0000605 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000606}
607
608/*
drh3f5bc382013-04-06 13:09:11 +0000609** Mark a script as having finished. Remove the CLIENT table entry
610** if bShutdown is true.
drh27338e62013-04-06 00:19:37 +0000611*/
drh3f5bc382013-04-06 13:09:11 +0000612static int finishScript(int iClient, int taskId, int bShutdown){
613 runSql("UPDATE task"
614 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
615 " WHERE id=%d;", taskId);
616 if( bShutdown ){
617 runSql("DELETE FROM client WHERE id=%d", iClient);
drh27338e62013-04-06 00:19:37 +0000618 }
drh3f5bc382013-04-06 13:09:11 +0000619 return SQLITE_OK;
620}
621
622/*
623** Start up a client process for iClient, if it is not already
624** running. If the client is already running, then this routine
625** is a no-op.
626*/
627static void startClient(int iClient){
628 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient);
629 if( sqlite3_changes(g.db) ){
630 char *zSys;
drhbc94dbb2013-04-08 14:28:33 +0000631 int rc;
drh3f5bc382013-04-06 13:09:11 +0000632 zSys = sqlite3_mprintf(
mistachkin08d41892013-04-11 00:09:44 +0000633 "%s \"%s\" --client %d --trace %d %s%s%s",
drh3f5bc382013-04-06 13:09:11 +0000634 g.argv0, g.zDbFile, iClient, g.iTrace,
drhbc94dbb2013-04-08 14:28:33 +0000635 g.bSqlTrace ? "--sqltrace " : "",
mistachkin08d41892013-04-11 00:09:44 +0000636 g.bSync ? "--sync " : "",
637 CMDLINE_SUFFIX
drhbc94dbb2013-04-08 14:28:33 +0000638 );
mistachkin08d41892013-04-11 00:09:44 +0000639#if !defined(_WIN32)
drhbc94dbb2013-04-08 14:28:33 +0000640 rc = system(zSys);
641 if( rc ) errorMessage("system() fails with error code %d", rc);
mistachkin08d41892013-04-11 00:09:44 +0000642#else
643 {
644 STARTUPINFOA startupInfo;
645 PROCESS_INFORMATION processInfo;
646 memset(&startupInfo, 0, sizeof(startupInfo));
647 startupInfo.cb = sizeof(startupInfo);
648 memset(&processInfo, 0, sizeof(processInfo));
649 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL,
650 &startupInfo, &processInfo);
651 if( rc ){
652 CloseHandle(processInfo.hThread);
653 CloseHandle(processInfo.hProcess);
654 }else{
655 errorMessage("CreateProcessA() fails with error code %lu",
656 GetLastError());
657 }
drhf012ae02013-04-06 14:04:22 +0000658 }
drhf012ae02013-04-06 14:04:22 +0000659#endif
mistachkin08d41892013-04-11 00:09:44 +0000660 sqlite3_free(zSys);
drh3f5bc382013-04-06 13:09:11 +0000661 }
drh27338e62013-04-06 00:19:37 +0000662}
663
664/*
665** Read the entire content of a file into memory
666*/
667static char *readFile(const char *zFilename){
668 FILE *in = fopen(zFilename, "rb");
669 long sz;
670 char *z;
671 if( in==0 ){
672 fatalError("cannot open \"%s\" for reading", zFilename);
673 }
674 fseek(in, 0, SEEK_END);
675 sz = ftell(in);
676 rewind(in);
677 z = sqlite3_malloc( sz+1 );
678 sz = (long)fread(z, 1, sz, in);
679 z[sz] = 0;
680 fclose(in);
681 return z;
682}
683
684/*
685** Return the length of the next token.
686*/
687static int tokenLength(const char *z, int *pnLine){
688 int n = 0;
689 if( isspace(z[0]) || (z[0]=='/' && z[1]=='*') ){
690 int inC = 0;
691 int c;
692 if( z[0]=='/' ){
693 inC = 1;
694 n = 2;
695 }
696 while( (c = z[n++])!=0 ){
697 if( c=='\n' ) (*pnLine)++;
698 if( isspace(c) ) continue;
699 if( inC && c=='*' && z[n]=='/' ){
700 n++;
701 inC = 0;
702 }else if( !inC && c=='/' && z[n]=='*' ){
703 n++;
704 inC = 1;
705 }else if( !inC ){
706 break;
707 }
708 }
709 n--;
710 }else if( z[0]=='-' && z[1]=='-' ){
711 for(n=2; z[n] && z[n]!='\n'; n++){}
712 if( z[n] ){ (*pnLine)++; n++; }
713 }else if( z[0]=='"' || z[0]=='\'' ){
714 int delim = z[0];
715 for(n=1; z[n]; n++){
716 if( z[n]=='\n' ) (*pnLine)++;
717 if( z[n]==delim ){
718 n++;
719 if( z[n+1]!=delim ) break;
720 }
721 }
722 }else{
723 int c;
724 for(n=1; (c = z[n])!=0 && !isspace(c) && c!='"' && c!='\'' && c!=';'; n++){}
725 }
726 return n;
727}
728
729/*
730** Copy a single token into a string buffer.
731*/
732static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
733 int i;
734 if( nIn<=0 ){
735 zOut[0] = 0;
736 return 0;
737 }
738 for(i=0; i<nIn && i<nOut-1 && !isspace(zIn[i]); i++){ zOut[i] = zIn[i]; }
739 zOut[i] = 0;
740 return i;
741}
742
743/*
drh7dfe8e22013-04-08 13:13:43 +0000744** Find the number of characters up to the start of the next "--end" token.
drh27338e62013-04-06 00:19:37 +0000745*/
746static int findEnd(const char *z, int *pnLine){
747 int n = 0;
748 while( z[n] && (strncmp(z+n,"--end",5) || !isspace(z[n+5])) ){
749 n += tokenLength(z+n, pnLine);
750 }
751 return n;
752}
753
754/*
drh7dfe8e22013-04-08 13:13:43 +0000755** Find the number of characters up to the first character past the
756** of the next "--endif" or "--else" token. Nested --if commands are
757** also skipped.
758*/
759static int findEndif(const char *z, int stopAtElse, int *pnLine){
760 int n = 0;
761 while( z[n] ){
762 int len = tokenLength(z+n, pnLine);
763 if( (strncmp(z+n,"--endif",7)==0 && isspace(z[n+7]))
764 || (stopAtElse && strncmp(z+n,"--else",6)==0 && isspace(z[n+6]))
765 ){
766 return n+len;
767 }
768 if( strncmp(z+n,"--if",4)==0 && isspace(z[n+4]) ){
769 int skip = findEndif(z+n+len, 0, pnLine);
770 n += skip + len;
771 }else{
772 n += len;
773 }
774 }
775 return n;
776}
777
778/*
drh27338e62013-04-06 00:19:37 +0000779** Wait for a client process to complete all its tasks
780*/
781static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){
782 sqlite3_stmt *pStmt;
783 int rc;
784 if( iClient>0 ){
785 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000786 "SELECT 1 FROM task"
787 " WHERE client=%d"
788 " AND client IN (SELECT id FROM client)"
789 " AND endtime IS NULL",
drh27338e62013-04-06 00:19:37 +0000790 iClient);
791 }else{
792 pStmt = prepareSql(
drh3f5bc382013-04-06 13:09:11 +0000793 "SELECT 1 FROM task"
794 " WHERE client IN (SELECT id FROM client)"
795 " AND endtime IS NULL");
drh27338e62013-04-06 00:19:37 +0000796 }
drh3f5bc382013-04-06 13:09:11 +0000797 g.iTimeout = 0;
drh27338e62013-04-06 00:19:37 +0000798 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW)
799 && iTimeout>0
800 ){
801 sqlite3_reset(pStmt);
802 sqlite3_sleep(50);
803 iTimeout -= 50;
804 }
805 sqlite3_finalize(pStmt);
drh3f5bc382013-04-06 13:09:11 +0000806 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +0000807 if( rc!=SQLITE_DONE ){
808 if( zErrPrefix==0 ) zErrPrefix = "";
809 if( iClient>0 ){
810 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient);
811 }else{
812 errorMessage("%stimeout waiting for all clients", zErrPrefix);
813 }
814 }
815}
816
drh4c5298f2013-04-10 12:01:21 +0000817/* Return a pointer to the tail of a filename
818*/
819static char *filenameTail(char *z){
820 int i, j;
821 for(i=j=0; z[i]; i++) if( z[i]=='/' ) j = i+1;
822 return z+j;
823}
824
drh27338e62013-04-06 00:19:37 +0000825/* Maximum number of arguments to a --command */
drh7dfe8e22013-04-08 13:13:43 +0000826#define MX_ARG 2
drh27338e62013-04-06 00:19:37 +0000827
828/*
829** Run a script.
830*/
831static void runScript(
832 int iClient, /* The client number, or 0 for the master */
833 int taskId, /* The task ID for clients. 0 for master */
834 char *zScript, /* Text of the script */
835 char *zFilename /* File from which script was read. */
836){
837 int lineno = 1;
838 int prevLine = 1;
839 int ii = 0;
840 int iBegin = 0;
841 int n, c, j;
drh27338e62013-04-06 00:19:37 +0000842 int len;
843 int nArg;
844 String sResult;
845 char zCmd[30];
846 char zError[1000];
847 char azArg[MX_ARG][100];
drh27338e62013-04-06 00:19:37 +0000848
drh27338e62013-04-06 00:19:37 +0000849 memset(&sResult, 0, sizeof(sResult));
850 stringReset(&sResult);
851 while( (c = zScript[ii])!=0 ){
852 prevLine = lineno;
853 len = tokenLength(zScript+ii, &lineno);
854 if( isspace(c) || (c=='/' && zScript[ii+1]=='*') ){
855 ii += len;
856 continue;
857 }
858 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){
859 ii += len;
860 continue;
861 }
862
863 /* Run any prior SQL before processing the new --command */
864 if( ii>iBegin ){
865 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
866 evalSql(&sResult, zSql);
867 sqlite3_free(zSql);
868 iBegin = ii + len;
869 }
870
871 /* Parse the --command */
872 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
873 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
874 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
875 while( n<len-2 && isspace(zScript[ii+2+n]) ){ n++; }
876 if( n>=len-2 ) break;
877 n += extractToken(zScript+ii+2+n, len-2-n,
878 azArg[nArg], sizeof(azArg[nArg]));
879 }
880 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0;
881
882 /*
883 ** --sleep N
884 **
885 ** Pause for N milliseconds
886 */
887 if( strcmp(zCmd, "sleep")==0 ){
888 sqlite3_sleep(atoi(azArg[0]));
889 }else
890
891 /*
892 ** --exit N
893 **
894 ** Exit this process. If N>0 then exit without shutting down
895 ** SQLite. (In other words, simulate a crash.)
896 */
drh6adab7a2013-04-08 18:58:00 +0000897 if( strcmp(zCmd, "exit")==0 && iClient>0 ){
drh27338e62013-04-06 00:19:37 +0000898 int rc = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +0000899 finishScript(iClient, taskId, 1);
drh27338e62013-04-06 00:19:37 +0000900 if( rc==0 ) sqlite3_close(g.db);
901 exit(rc);
902 }else
903
904 /*
drh6adab7a2013-04-08 18:58:00 +0000905 ** --finish
906 **
907 ** Mark the current task as having finished, even if it is not.
908 ** This can be used in conjunction with --exit to simulate a crash.
909 */
910 if( strcmp(zCmd, "finish")==0 && iClient>0 ){
911 finishScript(iClient, taskId, 1);
912 }else
913
914 /*
drh7dfe8e22013-04-08 13:13:43 +0000915 ** --reset
drh27338e62013-04-06 00:19:37 +0000916 **
917 ** Reset accumulated results back to an empty string
918 */
919 if( strcmp(zCmd, "reset")==0 ){
920 stringReset(&sResult);
921 }else
922
923 /*
924 ** --match ANSWER...
925 **
926 ** Check to see if output matches ANSWER. Report an error if not.
927 */
928 if( strcmp(zCmd, "match")==0 ){
929 int jj;
930 char *zAns = zScript+ii;
931 for(jj=7; jj<len-1 && isspace(zAns[jj]); jj++){}
932 zAns += jj;
933 if( strncmp(sResult.z, zAns, len-jj-1) ){
934 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
935 prevLine, zFilename, len-jj-1, zAns, sResult.z);
936 }
drh3f5bc382013-04-06 13:09:11 +0000937 g.nTest++;
drh27338e62013-04-06 00:19:37 +0000938 stringReset(&sResult);
939 }else
940
941 /*
drh1bf44c72013-04-08 13:48:29 +0000942 ** --output
943 **
944 ** Output the result of the previous SQL.
945 */
946 if( strcmp(zCmd, "output")==0 ){
947 logMessage("%s", sResult.z);
948 }else
949
950 /*
drh27338e62013-04-06 00:19:37 +0000951 ** --source FILENAME
952 **
953 ** Run a subscript from a separate file.
954 */
955 if( strcmp(zCmd, "source")==0 ){
drhe348fc72013-04-06 18:35:07 +0000956 char *zNewFile, *zNewScript;
957 char *zToDel = 0;
958 zNewFile = azArg[0];
959 if( zNewFile[0]!='/' ){
960 int k;
961 for(k=(int)strlen(zFilename)-1; k>=0 && zFilename[k]!='/'; k--){}
962 if( k>0 ){
963 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile);
964 }
965 }
966 zNewScript = readFile(zNewFile);
drh27338e62013-04-06 00:19:37 +0000967 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile);
968 runScript(0, 0, zNewScript, zNewFile);
969 sqlite3_free(zNewScript);
970 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile);
drhbc94dbb2013-04-08 14:28:33 +0000971 sqlite3_free(zToDel);
drh27338e62013-04-06 00:19:37 +0000972 }else
973
974 /*
975 ** --print MESSAGE....
976 **
977 ** Output the remainder of the line to the log file
978 */
979 if( strcmp(zCmd, "print")==0 ){
980 int jj;
981 for(jj=7; jj<len && isspace(zScript[ii+jj]); jj++){}
982 logMessage("%.*s", len-jj, zScript+ii+jj);
983 }else
984
985 /*
drh7dfe8e22013-04-08 13:13:43 +0000986 ** --if EXPR
987 **
988 ** Skip forward to the next matching --endif or --else if EXPR is false.
989 */
990 if( strcmp(zCmd, "if")==0 ){
991 int jj, rc;
992 sqlite3_stmt *pStmt;
993 for(jj=4; jj<len && isspace(zScript[ii+jj]); jj++){}
994 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
995 rc = sqlite3_step(pStmt);
996 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
997 ii += findEndif(zScript+ii+len, 1, &lineno);
998 }
999 sqlite3_finalize(pStmt);
1000 }else
1001
1002 /*
1003 ** --else
1004 **
1005 ** This command can only be encountered if currently inside an --if that
1006 ** is true. Skip forward to the next matching --endif.
1007 */
1008 if( strcmp(zCmd, "else")==0 ){
1009 ii += findEndif(zScript+ii+len, 0, &lineno);
1010 }else
1011
1012 /*
1013 ** --endif
1014 **
1015 ** This command can only be encountered if currently inside an --if that
1016 ** is true or an --else of a false if. This is a no-op.
1017 */
1018 if( strcmp(zCmd, "endif")==0 ){
1019 /* no-op */
1020 }else
1021
1022 /*
drh27338e62013-04-06 00:19:37 +00001023 ** --start CLIENT
1024 **
1025 ** Start up the given client.
1026 */
drh7dfe8e22013-04-08 13:13:43 +00001027 if( strcmp(zCmd, "start")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001028 int iNewClient = atoi(azArg[0]);
drh3f5bc382013-04-06 13:09:11 +00001029 if( iNewClient>0 ){
1030 startClient(iNewClient);
drh27338e62013-04-06 00:19:37 +00001031 }
drh27338e62013-04-06 00:19:37 +00001032 }else
1033
1034 /*
1035 ** --wait CLIENT TIMEOUT
1036 **
1037 ** Wait until all tasks complete for the given client. If CLIENT is
1038 ** "all" then wait for all clients to complete. Wait no longer than
1039 ** TIMEOUT milliseconds (default 10,000)
1040 */
drh7dfe8e22013-04-08 13:13:43 +00001041 if( strcmp(zCmd, "wait")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001042 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000;
1043 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n",
1044 prevLine, zFilename);
1045 waitForClient(atoi(azArg[0]), iTimeout, zError);
1046 }else
1047
1048 /*
1049 ** --task CLIENT
1050 ** <task-content-here>
1051 ** --end
1052 **
drh3f5bc382013-04-06 13:09:11 +00001053 ** Assign work to a client. Start the client if it is not running
1054 ** already.
drh27338e62013-04-06 00:19:37 +00001055 */
drh7dfe8e22013-04-08 13:13:43 +00001056 if( strcmp(zCmd, "task")==0 && iClient==0 ){
drh27338e62013-04-06 00:19:37 +00001057 int iTarget = atoi(azArg[0]);
1058 int iEnd;
1059 char *zTask;
drh4c5298f2013-04-10 12:01:21 +00001060 char *zTName;
drh27338e62013-04-06 00:19:37 +00001061 iEnd = findEnd(zScript+ii+len, &lineno);
drh3f5bc382013-04-06 13:09:11 +00001062 if( iTarget<0 ){
1063 errorMessage("line %d of %s: bad client number: %d",
drh27338e62013-04-06 00:19:37 +00001064 prevLine, zFilename, iTarget);
drh3f5bc382013-04-06 13:09:11 +00001065 }else{
1066 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len);
drh4c5298f2013-04-10 12:01:21 +00001067 if( nArg>1 ){
1068 zTName = sqlite3_mprintf("%s", azArg[1]);
1069 }else{
1070 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine);
1071 }
drh3f5bc382013-04-06 13:09:11 +00001072 startClient(iTarget);
drh4c5298f2013-04-10 12:01:21 +00001073 runSql("INSERT INTO task(client,script,name)"
1074 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName);
drh3f5bc382013-04-06 13:09:11 +00001075 sqlite3_free(zTask);
drh4c5298f2013-04-10 12:01:21 +00001076 sqlite3_free(zTName);
drh27338e62013-04-06 00:19:37 +00001077 }
drh27338e62013-04-06 00:19:37 +00001078 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno);
1079 len += iEnd;
1080 iBegin = ii+len;
1081 }else
1082
1083 /* error */{
1084 errorMessage("line %d of %s: unknown command --%s",
1085 prevLine, zFilename, zCmd);
1086 }
1087 ii += len;
1088 }
1089 if( iBegin<ii ){
1090 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
1091 runSql(zSql);
1092 sqlite3_free(zSql);
1093 }
1094 stringFree(&sResult);
1095}
1096
1097/*
1098** Look for a command-line option. If present, return a pointer.
1099** Return NULL if missing.
1100**
1101** hasArg==0 means the option is a flag. It is either present or not.
1102** hasArg==1 means the option has an argument. Return a pointer to the
1103** argument.
1104*/
1105static char *findOption(
1106 char **azArg,
1107 int *pnArg,
1108 const char *zOption,
1109 int hasArg
1110){
1111 int i, j;
1112 char *zReturn = 0;
1113 int nArg = *pnArg;
1114
1115 assert( hasArg==0 || hasArg==1 );
1116 for(i=0; i<nArg; i++){
1117 const char *z;
1118 if( i+hasArg >= nArg ) break;
1119 z = azArg[i];
1120 if( z[0]!='-' ) continue;
1121 z++;
1122 if( z[0]=='-' ){
1123 if( z[1]==0 ) break;
1124 z++;
1125 }
1126 if( strcmp(z,zOption)==0 ){
1127 if( hasArg && i==nArg-1 ){
1128 fatalError("command-line option \"--%s\" requires an argument", z);
1129 }
1130 if( hasArg ){
1131 zReturn = azArg[i+1];
1132 }else{
1133 zReturn = azArg[i];
1134 }
1135 j = i+1+(hasArg!=0);
1136 while( j<nArg ) azArg[i++] = azArg[j++];
1137 *pnArg = i;
1138 return zReturn;
1139 }
1140 }
1141 return zReturn;
1142}
1143
1144/* Print a usage message for the program and exit */
1145static void usage(const char *argv0){
1146 int i;
1147 const char *zTail = argv0;
1148 for(i=0; argv0[i]; i++){
1149 if( argv0[i]=='/' ) zTail = argv0+i+1;
1150 }
1151 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail);
1152 exit(1);
1153}
1154
1155/* Report on unrecognized arguments */
1156static void unrecognizedArguments(
1157 const char *argv0,
1158 int nArg,
1159 char **azArg
1160){
1161 int i;
1162 fprintf(stderr,"%s: unrecognized arguments:", argv0);
1163 for(i=0; i<nArg; i++){
1164 fprintf(stderr," %s", azArg[i]);
1165 }
1166 fprintf(stderr,"\n");
1167 exit(1);
1168}
1169
1170int main(int argc, char **argv){
1171 const char *zClient;
1172 int iClient;
drhe348fc72013-04-06 18:35:07 +00001173 int n, i;
drh27338e62013-04-06 00:19:37 +00001174 int openFlags = SQLITE_OPEN_READWRITE;
1175 int rc;
1176 char *zScript;
1177 int taskId;
1178 const char *zTrace;
drhe348fc72013-04-06 18:35:07 +00001179 const char *zCOption;
drh27338e62013-04-06 00:19:37 +00001180
1181 g.argv0 = argv[0];
1182 g.iTrace = 1;
1183 if( argc<2 ) usage(argv[0]);
1184 g.zDbFile = argv[1];
drhe348fc72013-04-06 18:35:07 +00001185 if( strglob("*.test", g.zDbFile) ) usage(argv[0]);
1186 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){
1187 fprintf(stderr, "SQLite library and header mismatch\n"
1188 "Library: %s\n"
1189 "Header: %s\n",
1190 sqlite3_sourceid(), SQLITE_SOURCE_ID);
1191 exit(1);
1192 }
drh27338e62013-04-06 00:19:37 +00001193 n = argc-2;
drhe3be8c82013-04-11 11:53:45 +00001194 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID());
drh27338e62013-04-06 00:19:37 +00001195 g.zVfs = findOption(argv+2, &n, "vfs", 1);
1196 zClient = findOption(argv+2, &n, "client", 1);
1197 g.zErrLog = findOption(argv+2, &n, "errlog", 1);
1198 g.zLog = findOption(argv+2, &n, "log", 1);
1199 zTrace = findOption(argv+2, &n, "trace", 1);
1200 if( zTrace ) g.iTrace = atoi(zTrace);
1201 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0;
1202 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0;
drhbc94dbb2013-04-08 14:28:33 +00001203 g.bSync = findOption(argv+2, &n, "sync", 0)!=0;
drh27338e62013-04-06 00:19:37 +00001204 if( g.zErrLog ){
1205 g.pErrLog = fopen(g.zErrLog, "a");
1206 }else{
1207 g.pErrLog = stderr;
1208 }
1209 if( g.zLog ){
1210 g.pLog = fopen(g.zLog, "a");
1211 }else{
1212 g.pLog = stdout;
1213 }
drhe3be8c82013-04-11 11:53:45 +00001214
drh1790bb32013-04-06 14:30:29 +00001215 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0);
drh27338e62013-04-06 00:19:37 +00001216 if( zClient ){
1217 iClient = atoi(zClient);
1218 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
drhe3be8c82013-04-11 11:53:45 +00001219 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
1220 GETPID(), iClient);
drh27338e62013-04-06 00:19:37 +00001221 }else{
drhe348fc72013-04-06 18:35:07 +00001222 if( g.iTrace>0 ){
1223 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
1224 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
1225 printf("-DSQLITE_%s\n", zCOption);
1226 }
1227 fflush(stdout);
1228 }
drh27338e62013-04-06 00:19:37 +00001229 iClient = 0;
1230 unlink(g.zDbFile);
1231 openFlags |= SQLITE_OPEN_CREATE;
1232 }
1233 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
1234 if( rc ) fatalError("cannot open [%s]", g.zDbFile);
drh3f5bc382013-04-06 13:09:11 +00001235 sqlite3_busy_handler(g.db, busyHandler, 0);
drh1bf44c72013-04-08 13:48:29 +00001236 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
1237 vfsNameFunc, 0, 0);
1238 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
1239 evalFunc, 0, 0);
drh3f5bc382013-04-06 13:09:11 +00001240 g.iTimeout = DEFAULT_TIMEOUT;
drh27338e62013-04-06 00:19:37 +00001241 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0);
drhbc94dbb2013-04-08 14:28:33 +00001242 if( !g.bSync ) trySql("PRAGMA synchronous=OFF");
drh27338e62013-04-06 00:19:37 +00001243 if( iClient>0 ){
1244 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2);
1245 if( g.iTrace ) logMessage("start-client");
1246 while(1){
drh4c5298f2013-04-10 12:01:21 +00001247 char *zTaskName = 0;
1248 rc = startScript(iClient, &zScript, &taskId, &zTaskName);
drh27338e62013-04-06 00:19:37 +00001249 if( rc==SQLITE_DONE ) break;
drh4c5298f2013-04-10 12:01:21 +00001250 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId);
drh27338e62013-04-06 00:19:37 +00001251 runScript(iClient, taskId, zScript, zTaskName);
drh4c5298f2013-04-10 12:01:21 +00001252 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId);
drh3f5bc382013-04-06 13:09:11 +00001253 finishScript(iClient, taskId, 0);
drh4c5298f2013-04-10 12:01:21 +00001254 sqlite3_free(zTaskName);
drh27338e62013-04-06 00:19:37 +00001255 sqlite3_sleep(10);
1256 }
1257 if( g.iTrace ) logMessage("end-client");
1258 }else{
1259 sqlite3_stmt *pStmt;
drh3f5bc382013-04-06 13:09:11 +00001260 int iTimeout;
drh27338e62013-04-06 00:19:37 +00001261 if( n==0 ){
1262 fatalError("missing script filename");
1263 }
1264 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
1265 runSql(
1266 "CREATE TABLE task(\n"
1267 " id INTEGER PRIMARY KEY,\n"
drh4c5298f2013-04-10 12:01:21 +00001268 " name TEXT,\n"
drh27338e62013-04-06 00:19:37 +00001269 " client INTEGER,\n"
1270 " starttime DATE,\n"
1271 " endtime DATE,\n"
1272 " script TEXT\n"
1273 ");"
drh3f5bc382013-04-06 13:09:11 +00001274 "CREATE INDEX task_i1 ON task(client, starttime);\n"
1275 "CREATE INDEX task_i2 ON task(client, endtime);\n"
1276 "CREATE TABLE counters(nError,nTest);\n"
1277 "INSERT INTO counters VALUES(0,0);\n"
1278 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n"
drh27338e62013-04-06 00:19:37 +00001279 );
1280 zScript = readFile(argv[2]);
1281 if( g.iTrace ) logMessage("begin script [%s]\n", argv[2]);
1282 runScript(0, 0, zScript, argv[2]);
1283 sqlite3_free(zScript);
1284 if( g.iTrace ) logMessage("end script [%s]\n", argv[2]);
1285 waitForClient(0, 2000, "during shutdown...\n");
drh3f5bc382013-04-06 13:09:11 +00001286 trySql("UPDATE client SET wantHalt=1");
1287 sqlite3_sleep(10);
1288 g.iTimeout = 0;
1289 iTimeout = 1000;
1290 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1291 || rc==SQLITE_ROW) && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001292 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001293 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001294 }
1295 sqlite3_sleep(100);
drh3f5bc382013-04-06 13:09:11 +00001296 pStmt = prepareSql("SELECT nError, nTest FROM counters");
1297 iTimeout = 1000;
1298 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){
drh27338e62013-04-06 00:19:37 +00001299 sqlite3_sleep(10);
drh3f5bc382013-04-06 13:09:11 +00001300 iTimeout -= 10;
drh27338e62013-04-06 00:19:37 +00001301 }
1302 if( rc==SQLITE_ROW ){
1303 g.nError += sqlite3_column_int(pStmt, 0);
drh3f5bc382013-04-06 13:09:11 +00001304 g.nTest += sqlite3_column_int(pStmt, 1);
drh27338e62013-04-06 00:19:37 +00001305 }
1306 sqlite3_finalize(pStmt);
1307 }
1308 sqlite3_close(g.db);
1309 maybeClose(g.pLog);
1310 maybeClose(g.pErrLog);
1311 if( iClient==0 ){
drh3f5bc382013-04-06 13:09:11 +00001312 printf("Summary: %d errors in %d tests\n", g.nError, g.nTest);
drh27338e62013-04-06 00:19:37 +00001313 }
1314 return g.nError>0;
1315}