OLD | NEW |
| (Empty) |
1 /* | |
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> | |
38 #if defined(_WIN32) | |
39 # define WIN32_LEAN_AND_MEAN | |
40 # include <windows.h> | |
41 #else | |
42 # include <unistd.h> | |
43 #endif | |
44 #include <stdlib.h> | |
45 #include <string.h> | |
46 #include <assert.h> | |
47 #include <ctype.h> | |
48 | |
49 #define ISSPACE(X) isspace((unsigned char)(X)) | |
50 #define ISDIGIT(X) isdigit((unsigned char)(X)) | |
51 | |
52 /* The suffix to append to the child command lines, if any */ | |
53 #if defined(_WIN32) | |
54 # define GETPID (int)GetCurrentProcessId | |
55 #else | |
56 # define GETPID getpid | |
57 #endif | |
58 | |
59 /* The directory separator character(s) */ | |
60 #if defined(_WIN32) | |
61 # define isDirSep(c) (((c) == '/') || ((c) == '\\')) | |
62 #else | |
63 # define isDirSep(c) ((c) == '/') | |
64 #endif | |
65 | |
66 /* Mark a parameter as unused to suppress compiler warnings */ | |
67 #define UNUSED_PARAMETER(x) (void)x | |
68 | |
69 /* Global data | |
70 */ | |
71 static struct Global { | |
72 char *argv0; /* Name of the executable */ | |
73 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */ | |
74 char *zDbFile; /* Name of the database */ | |
75 sqlite3 *db; /* Open connection to database */ | |
76 char *zErrLog; /* Filename for error log */ | |
77 FILE *pErrLog; /* Where to write errors */ | |
78 char *zLog; /* Name of output log file */ | |
79 FILE *pLog; /* Where to write log messages */ | |
80 char zName[32]; /* Symbolic name of this process */ | |
81 int taskId; /* Task ID. 0 means supervisor. */ | |
82 int iTrace; /* Tracing level */ | |
83 int bSqlTrace; /* True to trace SQL commands */ | |
84 int bIgnoreSqlErrors; /* Ignore errors in SQL statements */ | |
85 int nError; /* Number of errors */ | |
86 int nTest; /* Number of --match operators */ | |
87 int iTimeout; /* Milliseconds until a busy timeout */ | |
88 int bSync; /* Call fsync() */ | |
89 } g; | |
90 | |
91 /* Default timeout */ | |
92 #define DEFAULT_TIMEOUT 10000 | |
93 | |
94 /* | |
95 ** Print a message adding zPrefix[] to the beginning of every line. | |
96 */ | |
97 static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){ | |
98 while( zMsg && zMsg[0] ){ | |
99 int i; | |
100 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){} | |
101 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg); | |
102 zMsg += i; | |
103 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++; | |
104 } | |
105 } | |
106 | |
107 /* | |
108 ** Compare two pointers to strings, where the pointers might be NULL. | |
109 */ | |
110 static int safe_strcmp(const char *a, const char *b){ | |
111 if( a==b ) return 0; | |
112 if( a==0 ) return -1; | |
113 if( b==0 ) return 1; | |
114 return strcmp(a,b); | |
115 } | |
116 | |
117 /* | |
118 ** Return TRUE if string z[] matches glob pattern zGlob[]. | |
119 ** Return FALSE if the pattern does not match. | |
120 ** | |
121 ** Globbing rules: | |
122 ** | |
123 ** '*' Matches any sequence of zero or more characters. | |
124 ** | |
125 ** '?' Matches exactly one character. | |
126 ** | |
127 ** [...] Matches one character from the enclosed list of | |
128 ** characters. | |
129 ** | |
130 ** [^...] Matches one character not in the enclosed list. | |
131 ** | |
132 ** '#' Matches any sequence of one or more digits with an | |
133 ** optional + or - sign in front | |
134 */ | |
135 int strglob(const char *zGlob, const char *z){ | |
136 int c, c2; | |
137 int invert; | |
138 int seen; | |
139 | |
140 while( (c = (*(zGlob++)))!=0 ){ | |
141 if( c=='*' ){ | |
142 while( (c=(*(zGlob++))) == '*' || c=='?' ){ | |
143 if( c=='?' && (*(z++))==0 ) return 0; | |
144 } | |
145 if( c==0 ){ | |
146 return 1; | |
147 }else if( c=='[' ){ | |
148 while( *z && strglob(zGlob-1,z) ){ | |
149 z++; | |
150 } | |
151 return (*z)!=0; | |
152 } | |
153 while( (c2 = (*(z++)))!=0 ){ | |
154 while( c2!=c ){ | |
155 c2 = *(z++); | |
156 if( c2==0 ) return 0; | |
157 } | |
158 if( strglob(zGlob,z) ) return 1; | |
159 } | |
160 return 0; | |
161 }else if( c=='?' ){ | |
162 if( (*(z++))==0 ) return 0; | |
163 }else if( c=='[' ){ | |
164 int prior_c = 0; | |
165 seen = 0; | |
166 invert = 0; | |
167 c = *(z++); | |
168 if( c==0 ) return 0; | |
169 c2 = *(zGlob++); | |
170 if( c2=='^' ){ | |
171 invert = 1; | |
172 c2 = *(zGlob++); | |
173 } | |
174 if( c2==']' ){ | |
175 if( c==']' ) seen = 1; | |
176 c2 = *(zGlob++); | |
177 } | |
178 while( c2 && c2!=']' ){ | |
179 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ | |
180 c2 = *(zGlob++); | |
181 if( c>=prior_c && c<=c2 ) seen = 1; | |
182 prior_c = 0; | |
183 }else{ | |
184 if( c==c2 ){ | |
185 seen = 1; | |
186 } | |
187 prior_c = c2; | |
188 } | |
189 c2 = *(zGlob++); | |
190 } | |
191 if( c2==0 || (seen ^ invert)==0 ) return 0; | |
192 }else if( c=='#' ){ | |
193 if( (z[0]=='-' || z[0]=='+') && ISDIGIT(z[1]) ) z++; | |
194 if( !ISDIGIT(z[0]) ) return 0; | |
195 z++; | |
196 while( ISDIGIT(z[0]) ){ z++; } | |
197 }else{ | |
198 if( c!=(*(z++)) ) return 0; | |
199 } | |
200 } | |
201 return *z==0; | |
202 } | |
203 | |
204 /* | |
205 ** Close output stream pOut if it is not stdout or stderr | |
206 */ | |
207 static void maybeClose(FILE *pOut){ | |
208 if( pOut!=stdout && pOut!=stderr ) fclose(pOut); | |
209 } | |
210 | |
211 /* | |
212 ** Print an error message | |
213 */ | |
214 static void errorMessage(const char *zFormat, ...){ | |
215 va_list ap; | |
216 char *zMsg; | |
217 char zPrefix[30]; | |
218 va_start(ap, zFormat); | |
219 zMsg = sqlite3_vmprintf(zFormat, ap); | |
220 va_end(ap); | |
221 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName); | |
222 if( g.pLog ){ | |
223 printWithPrefix(g.pLog, zPrefix, zMsg); | |
224 fflush(g.pLog); | |
225 } | |
226 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){ | |
227 printWithPrefix(g.pErrLog, zPrefix, zMsg); | |
228 fflush(g.pErrLog); | |
229 } | |
230 sqlite3_free(zMsg); | |
231 g.nError++; | |
232 } | |
233 | |
234 /* Forward declaration */ | |
235 static int trySql(const char*, ...); | |
236 | |
237 /* | |
238 ** Print an error message and then quit. | |
239 */ | |
240 static void fatalError(const char *zFormat, ...){ | |
241 va_list ap; | |
242 char *zMsg; | |
243 char zPrefix[30]; | |
244 va_start(ap, zFormat); | |
245 zMsg = sqlite3_vmprintf(zFormat, ap); | |
246 va_end(ap); | |
247 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName); | |
248 if( g.pLog ){ | |
249 printWithPrefix(g.pLog, zPrefix, zMsg); | |
250 fflush(g.pLog); | |
251 maybeClose(g.pLog); | |
252 } | |
253 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){ | |
254 printWithPrefix(g.pErrLog, zPrefix, zMsg); | |
255 fflush(g.pErrLog); | |
256 maybeClose(g.pErrLog); | |
257 } | |
258 sqlite3_free(zMsg); | |
259 if( g.db ){ | |
260 int nTry = 0; | |
261 g.iTimeout = 0; | |
262 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY | |
263 && (nTry++)<100 ){ | |
264 sqlite3_sleep(10); | |
265 } | |
266 } | |
267 sqlite3_close(g.db); | |
268 exit(1); | |
269 } | |
270 | |
271 | |
272 /* | |
273 ** Print a log message | |
274 */ | |
275 static void logMessage(const char *zFormat, ...){ | |
276 va_list ap; | |
277 char *zMsg; | |
278 char zPrefix[30]; | |
279 va_start(ap, zFormat); | |
280 zMsg = sqlite3_vmprintf(zFormat, ap); | |
281 va_end(ap); | |
282 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName); | |
283 if( g.pLog ){ | |
284 printWithPrefix(g.pLog, zPrefix, zMsg); | |
285 fflush(g.pLog); | |
286 } | |
287 sqlite3_free(zMsg); | |
288 } | |
289 | |
290 /* | |
291 ** Return the length of a string omitting trailing whitespace | |
292 */ | |
293 static int clipLength(const char *z){ | |
294 int n = (int)strlen(z); | |
295 while( n>0 && ISSPACE(z[n-1]) ){ n--; } | |
296 return n; | |
297 } | |
298 | |
299 /* | |
300 ** Auxiliary SQL function to return the name of the VFS | |
301 */ | |
302 static void vfsNameFunc( | |
303 sqlite3_context *context, | |
304 int argc, | |
305 sqlite3_value **argv | |
306 ){ | |
307 sqlite3 *db = sqlite3_context_db_handle(context); | |
308 char *zVfs = 0; | |
309 UNUSED_PARAMETER(argc); | |
310 UNUSED_PARAMETER(argv); | |
311 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs); | |
312 if( zVfs ){ | |
313 sqlite3_result_text(context, zVfs, -1, sqlite3_free); | |
314 } | |
315 } | |
316 | |
317 /* | |
318 ** Busy handler with a g.iTimeout-millisecond timeout | |
319 */ | |
320 static int busyHandler(void *pCD, int count){ | |
321 UNUSED_PARAMETER(pCD); | |
322 if( count*10>g.iTimeout ){ | |
323 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout); | |
324 return 0; | |
325 } | |
326 sqlite3_sleep(10); | |
327 return 1; | |
328 } | |
329 | |
330 /* | |
331 ** SQL Trace callback | |
332 */ | |
333 static void sqlTraceCallback(void *NotUsed1, const char *zSql){ | |
334 UNUSED_PARAMETER(NotUsed1); | |
335 logMessage("[%.*s]", clipLength(zSql), zSql); | |
336 } | |
337 | |
338 /* | |
339 ** SQL error log callback | |
340 */ | |
341 static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){ | |
342 UNUSED_PARAMETER(pArg); | |
343 if( iErrCode==SQLITE_ERROR && g.bIgnoreSqlErrors ) return; | |
344 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return; | |
345 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return; | |
346 if( (iErrCode&0xff)==SQLITE_NOTICE ){ | |
347 logMessage("(info) %s", zMsg); | |
348 }else{ | |
349 errorMessage("(errcode=%d) %s", iErrCode, zMsg); | |
350 } | |
351 } | |
352 | |
353 /* | |
354 ** Prepare an SQL statement. Issue a fatal error if unable. | |
355 */ | |
356 static sqlite3_stmt *prepareSql(const char *zFormat, ...){ | |
357 va_list ap; | |
358 char *zSql; | |
359 int rc; | |
360 sqlite3_stmt *pStmt = 0; | |
361 va_start(ap, zFormat); | |
362 zSql = sqlite3_vmprintf(zFormat, ap); | |
363 va_end(ap); | |
364 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); | |
365 if( rc!=SQLITE_OK ){ | |
366 sqlite3_finalize(pStmt); | |
367 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql); | |
368 } | |
369 sqlite3_free(zSql); | |
370 return pStmt; | |
371 } | |
372 | |
373 /* | |
374 ** Run arbitrary SQL. Issue a fatal error on failure. | |
375 */ | |
376 static void runSql(const char *zFormat, ...){ | |
377 va_list ap; | |
378 char *zSql; | |
379 int rc; | |
380 va_start(ap, zFormat); | |
381 zSql = sqlite3_vmprintf(zFormat, ap); | |
382 va_end(ap); | |
383 rc = sqlite3_exec(g.db, zSql, 0, 0, 0); | |
384 if( rc!=SQLITE_OK ){ | |
385 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql); | |
386 } | |
387 sqlite3_free(zSql); | |
388 } | |
389 | |
390 /* | |
391 ** Try to run arbitrary SQL. Return success code. | |
392 */ | |
393 static int trySql(const char *zFormat, ...){ | |
394 va_list ap; | |
395 char *zSql; | |
396 int rc; | |
397 va_start(ap, zFormat); | |
398 zSql = sqlite3_vmprintf(zFormat, ap); | |
399 va_end(ap); | |
400 rc = sqlite3_exec(g.db, zSql, 0, 0, 0); | |
401 sqlite3_free(zSql); | |
402 return rc; | |
403 } | |
404 | |
405 /* Structure for holding an arbitrary length string | |
406 */ | |
407 typedef struct String String; | |
408 struct String { | |
409 char *z; /* the string */ | |
410 int n; /* Slots of z[] used */ | |
411 int nAlloc; /* Slots of z[] allocated */ | |
412 }; | |
413 | |
414 /* Free a string */ | |
415 static void stringFree(String *p){ | |
416 if( p->z ) sqlite3_free(p->z); | |
417 memset(p, 0, sizeof(*p)); | |
418 } | |
419 | |
420 /* Append n bytes of text to a string. If n<0 append the entire string. */ | |
421 static void stringAppend(String *p, const char *z, int n){ | |
422 if( n<0 ) n = (int)strlen(z); | |
423 if( p->n+n>=p->nAlloc ){ | |
424 int nAlloc = p->nAlloc*2 + n + 100; | |
425 char *zNew = sqlite3_realloc(p->z, nAlloc); | |
426 if( zNew==0 ) fatalError("out of memory"); | |
427 p->z = zNew; | |
428 p->nAlloc = nAlloc; | |
429 } | |
430 memcpy(p->z+p->n, z, n); | |
431 p->n += n; | |
432 p->z[p->n] = 0; | |
433 } | |
434 | |
435 /* Reset a string to an empty string */ | |
436 static void stringReset(String *p){ | |
437 if( p->z==0 ) stringAppend(p, " ", 1); | |
438 p->n = 0; | |
439 p->z[0] = 0; | |
440 } | |
441 | |
442 /* Append a new token onto the end of the string */ | |
443 static void stringAppendTerm(String *p, const char *z){ | |
444 int i; | |
445 if( p->n ) stringAppend(p, " ", 1); | |
446 if( z==0 ){ | |
447 stringAppend(p, "nil", 3); | |
448 return; | |
449 } | |
450 for(i=0; z[i] && !ISSPACE(z[i]); i++){} | |
451 if( i>0 && z[i]==0 ){ | |
452 stringAppend(p, z, i); | |
453 return; | |
454 } | |
455 stringAppend(p, "'", 1); | |
456 while( z[0] ){ | |
457 for(i=0; z[i] && z[i]!='\''; i++){} | |
458 if( z[i] ){ | |
459 stringAppend(p, z, i+1); | |
460 stringAppend(p, "'", 1); | |
461 z += i+1; | |
462 }else{ | |
463 stringAppend(p, z, i); | |
464 break; | |
465 } | |
466 } | |
467 stringAppend(p, "'", 1); | |
468 } | |
469 | |
470 /* | |
471 ** Callback function for evalSql() | |
472 */ | |
473 static int evalCallback(void *pCData, int argc, char **argv, char **azCol){ | |
474 String *p = (String*)pCData; | |
475 int i; | |
476 UNUSED_PARAMETER(azCol); | |
477 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]); | |
478 return 0; | |
479 } | |
480 | |
481 /* | |
482 ** Run arbitrary SQL and record the results in an output string | |
483 ** given by the first parameter. | |
484 */ | |
485 static int evalSql(String *p, const char *zFormat, ...){ | |
486 va_list ap; | |
487 char *zSql; | |
488 int rc; | |
489 char *zErrMsg = 0; | |
490 va_start(ap, zFormat); | |
491 zSql = sqlite3_vmprintf(zFormat, ap); | |
492 va_end(ap); | |
493 assert( g.iTimeout>0 ); | |
494 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg); | |
495 sqlite3_free(zSql); | |
496 if( rc ){ | |
497 char zErr[30]; | |
498 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc); | |
499 stringAppendTerm(p, zErr); | |
500 if( zErrMsg ){ | |
501 stringAppendTerm(p, zErrMsg); | |
502 sqlite3_free(zErrMsg); | |
503 } | |
504 } | |
505 return rc; | |
506 } | |
507 | |
508 /* | |
509 ** Auxiliary SQL function to recursively evaluate SQL. | |
510 */ | |
511 static void evalFunc( | |
512 sqlite3_context *context, | |
513 int argc, | |
514 sqlite3_value **argv | |
515 ){ | |
516 sqlite3 *db = sqlite3_context_db_handle(context); | |
517 const char *zSql = (const char*)sqlite3_value_text(argv[0]); | |
518 String res; | |
519 char *zErrMsg = 0; | |
520 int rc; | |
521 UNUSED_PARAMETER(argc); | |
522 memset(&res, 0, sizeof(res)); | |
523 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg); | |
524 if( zErrMsg ){ | |
525 sqlite3_result_error(context, zErrMsg, -1); | |
526 sqlite3_free(zErrMsg); | |
527 }else if( rc ){ | |
528 sqlite3_result_error_code(context, rc); | |
529 }else{ | |
530 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT); | |
531 } | |
532 stringFree(&res); | |
533 } | |
534 | |
535 /* | |
536 ** Look up the next task for client iClient in the database. | |
537 ** Return the task script and the task number and mark that | |
538 ** task as being under way. | |
539 */ | |
540 static int startScript( | |
541 int iClient, /* The client number */ | |
542 char **pzScript, /* Write task script here */ | |
543 int *pTaskId, /* Write task number here */ | |
544 char **pzTaskName /* Name of the task */ | |
545 ){ | |
546 sqlite3_stmt *pStmt = 0; | |
547 int taskId; | |
548 int rc; | |
549 int totalTime = 0; | |
550 | |
551 *pzScript = 0; | |
552 g.iTimeout = 0; | |
553 while(1){ | |
554 rc = trySql("BEGIN IMMEDIATE"); | |
555 if( rc==SQLITE_BUSY ){ | |
556 sqlite3_sleep(10); | |
557 totalTime += 10; | |
558 continue; | |
559 } | |
560 if( rc!=SQLITE_OK ){ | |
561 fatalError("in startScript: %s", sqlite3_errmsg(g.db)); | |
562 } | |
563 if( g.nError || g.nTest ){ | |
564 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d", | |
565 g.nError, g.nTest); | |
566 g.nError = 0; | |
567 g.nTest = 0; | |
568 } | |
569 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient); | |
570 rc = sqlite3_step(pStmt); | |
571 sqlite3_finalize(pStmt); | |
572 if( rc==SQLITE_ROW ){ | |
573 runSql("DELETE FROM client WHERE id=%d", iClient); | |
574 g.iTimeout = DEFAULT_TIMEOUT; | |
575 runSql("COMMIT TRANSACTION;"); | |
576 return SQLITE_DONE; | |
577 } | |
578 pStmt = prepareSql( | |
579 "SELECT script, id, name FROM task" | |
580 " WHERE client=%d AND starttime IS NULL" | |
581 " ORDER BY id LIMIT 1", iClient); | |
582 rc = sqlite3_step(pStmt); | |
583 if( rc==SQLITE_ROW ){ | |
584 int n = sqlite3_column_bytes(pStmt, 0); | |
585 *pzScript = sqlite3_malloc(n+1); | |
586 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0)); | |
587 *pTaskId = taskId = sqlite3_column_int(pStmt, 1); | |
588 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2)); | |
589 sqlite3_finalize(pStmt); | |
590 runSql("UPDATE task" | |
591 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')" | |
592 " WHERE id=%d;", taskId); | |
593 g.iTimeout = DEFAULT_TIMEOUT; | |
594 runSql("COMMIT TRANSACTION;"); | |
595 return SQLITE_OK; | |
596 } | |
597 sqlite3_finalize(pStmt); | |
598 if( rc==SQLITE_DONE ){ | |
599 if( totalTime>30000 ){ | |
600 errorMessage("Waited over 30 seconds with no work. Giving up."); | |
601 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient); | |
602 sqlite3_close(g.db); | |
603 exit(1); | |
604 } | |
605 while( trySql("COMMIT")==SQLITE_BUSY ){ | |
606 sqlite3_sleep(10); | |
607 totalTime += 10; | |
608 } | |
609 sqlite3_sleep(100); | |
610 totalTime += 100; | |
611 continue; | |
612 } | |
613 fatalError("%s", sqlite3_errmsg(g.db)); | |
614 } | |
615 g.iTimeout = DEFAULT_TIMEOUT; | |
616 } | |
617 | |
618 /* | |
619 ** Mark a script as having finished. Remove the CLIENT table entry | |
620 ** if bShutdown is true. | |
621 */ | |
622 static int finishScript(int iClient, int taskId, int bShutdown){ | |
623 runSql("UPDATE task" | |
624 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')" | |
625 " WHERE id=%d;", taskId); | |
626 if( bShutdown ){ | |
627 runSql("DELETE FROM client WHERE id=%d", iClient); | |
628 } | |
629 return SQLITE_OK; | |
630 } | |
631 | |
632 /* | |
633 ** Start up a client process for iClient, if it is not already | |
634 ** running. If the client is already running, then this routine | |
635 ** is a no-op. | |
636 */ | |
637 static void startClient(int iClient){ | |
638 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient); | |
639 if( sqlite3_changes(g.db) ){ | |
640 char *zSys; | |
641 int rc; | |
642 zSys = sqlite3_mprintf("%s \"%s\" --client %d --trace %d", | |
643 g.argv0, g.zDbFile, iClient, g.iTrace); | |
644 if( g.bSqlTrace ){ | |
645 zSys = sqlite3_mprintf("%z --sqltrace", zSys); | |
646 } | |
647 if( g.bSync ){ | |
648 zSys = sqlite3_mprintf("%z --sync", zSys); | |
649 } | |
650 if( g.zVfs ){ | |
651 zSys = sqlite3_mprintf("%z --vfs \"%s\"", zSys, g.zVfs); | |
652 } | |
653 if( g.iTrace>=2 ) logMessage("system('%q')", zSys); | |
654 #if !defined(_WIN32) | |
655 zSys = sqlite3_mprintf("%z &", zSys); | |
656 rc = system(zSys); | |
657 if( rc ) errorMessage("system() fails with error code %d", rc); | |
658 #else | |
659 { | |
660 STARTUPINFOA startupInfo; | |
661 PROCESS_INFORMATION processInfo; | |
662 memset(&startupInfo, 0, sizeof(startupInfo)); | |
663 startupInfo.cb = sizeof(startupInfo); | |
664 memset(&processInfo, 0, sizeof(processInfo)); | |
665 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL, | |
666 &startupInfo, &processInfo); | |
667 if( rc ){ | |
668 CloseHandle(processInfo.hThread); | |
669 CloseHandle(processInfo.hProcess); | |
670 }else{ | |
671 errorMessage("CreateProcessA() fails with error code %lu", | |
672 GetLastError()); | |
673 } | |
674 } | |
675 #endif | |
676 sqlite3_free(zSys); | |
677 } | |
678 } | |
679 | |
680 /* | |
681 ** Read the entire content of a file into memory | |
682 */ | |
683 static char *readFile(const char *zFilename){ | |
684 FILE *in = fopen(zFilename, "rb"); | |
685 long sz; | |
686 char *z; | |
687 if( in==0 ){ | |
688 fatalError("cannot open \"%s\" for reading", zFilename); | |
689 } | |
690 fseek(in, 0, SEEK_END); | |
691 sz = ftell(in); | |
692 rewind(in); | |
693 z = sqlite3_malloc( sz+1 ); | |
694 sz = (long)fread(z, 1, sz, in); | |
695 z[sz] = 0; | |
696 fclose(in); | |
697 return z; | |
698 } | |
699 | |
700 /* | |
701 ** Return the length of the next token. | |
702 */ | |
703 static int tokenLength(const char *z, int *pnLine){ | |
704 int n = 0; | |
705 if( ISSPACE(z[0]) || (z[0]=='/' && z[1]=='*') ){ | |
706 int inC = 0; | |
707 int c; | |
708 if( z[0]=='/' ){ | |
709 inC = 1; | |
710 n = 2; | |
711 } | |
712 while( (c = z[n++])!=0 ){ | |
713 if( c=='\n' ) (*pnLine)++; | |
714 if( ISSPACE(c) ) continue; | |
715 if( inC && c=='*' && z[n]=='/' ){ | |
716 n++; | |
717 inC = 0; | |
718 }else if( !inC && c=='/' && z[n]=='*' ){ | |
719 n++; | |
720 inC = 1; | |
721 }else if( !inC ){ | |
722 break; | |
723 } | |
724 } | |
725 n--; | |
726 }else if( z[0]=='-' && z[1]=='-' ){ | |
727 for(n=2; z[n] && z[n]!='\n'; n++){} | |
728 if( z[n] ){ (*pnLine)++; n++; } | |
729 }else if( z[0]=='"' || z[0]=='\'' ){ | |
730 int delim = z[0]; | |
731 for(n=1; z[n]; n++){ | |
732 if( z[n]=='\n' ) (*pnLine)++; | |
733 if( z[n]==delim ){ | |
734 n++; | |
735 if( z[n+1]!=delim ) break; | |
736 } | |
737 } | |
738 }else{ | |
739 int c; | |
740 for(n=1; (c = z[n])!=0 && !ISSPACE(c) && c!='"' && c!='\'' && c!=';'; n++){} | |
741 } | |
742 return n; | |
743 } | |
744 | |
745 /* | |
746 ** Copy a single token into a string buffer. | |
747 */ | |
748 static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){ | |
749 int i; | |
750 if( nIn<=0 ){ | |
751 zOut[0] = 0; | |
752 return 0; | |
753 } | |
754 for(i=0; i<nIn && i<nOut-1 && !ISSPACE(zIn[i]); i++){ zOut[i] = zIn[i]; } | |
755 zOut[i] = 0; | |
756 return i; | |
757 } | |
758 | |
759 /* | |
760 ** Find the number of characters up to the start of the next "--end" token. | |
761 */ | |
762 static int findEnd(const char *z, int *pnLine){ | |
763 int n = 0; | |
764 while( z[n] && (strncmp(z+n,"--end",5) || !ISSPACE(z[n+5])) ){ | |
765 n += tokenLength(z+n, pnLine); | |
766 } | |
767 return n; | |
768 } | |
769 | |
770 /* | |
771 ** Find the number of characters up to the first character past the | |
772 ** of the next "--endif" or "--else" token. Nested --if commands are | |
773 ** also skipped. | |
774 */ | |
775 static int findEndif(const char *z, int stopAtElse, int *pnLine){ | |
776 int n = 0; | |
777 while( z[n] ){ | |
778 int len = tokenLength(z+n, pnLine); | |
779 if( (strncmp(z+n,"--endif",7)==0 && ISSPACE(z[n+7])) | |
780 || (stopAtElse && strncmp(z+n,"--else",6)==0 && ISSPACE(z[n+6])) | |
781 ){ | |
782 return n+len; | |
783 } | |
784 if( strncmp(z+n,"--if",4)==0 && ISSPACE(z[n+4]) ){ | |
785 int skip = findEndif(z+n+len, 0, pnLine); | |
786 n += skip + len; | |
787 }else{ | |
788 n += len; | |
789 } | |
790 } | |
791 return n; | |
792 } | |
793 | |
794 /* | |
795 ** Wait for a client process to complete all its tasks | |
796 */ | |
797 static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){ | |
798 sqlite3_stmt *pStmt; | |
799 int rc; | |
800 if( iClient>0 ){ | |
801 pStmt = prepareSql( | |
802 "SELECT 1 FROM task" | |
803 " WHERE client=%d" | |
804 " AND client IN (SELECT id FROM client)" | |
805 " AND endtime IS NULL", | |
806 iClient); | |
807 }else{ | |
808 pStmt = prepareSql( | |
809 "SELECT 1 FROM task" | |
810 " WHERE client IN (SELECT id FROM client)" | |
811 " AND endtime IS NULL"); | |
812 } | |
813 g.iTimeout = 0; | |
814 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW) | |
815 && iTimeout>0 | |
816 ){ | |
817 sqlite3_reset(pStmt); | |
818 sqlite3_sleep(50); | |
819 iTimeout -= 50; | |
820 } | |
821 sqlite3_finalize(pStmt); | |
822 g.iTimeout = DEFAULT_TIMEOUT; | |
823 if( rc!=SQLITE_DONE ){ | |
824 if( zErrPrefix==0 ) zErrPrefix = ""; | |
825 if( iClient>0 ){ | |
826 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient); | |
827 }else{ | |
828 errorMessage("%stimeout waiting for all clients", zErrPrefix); | |
829 } | |
830 } | |
831 } | |
832 | |
833 /* Return a pointer to the tail of a filename | |
834 */ | |
835 static char *filenameTail(char *z){ | |
836 int i, j; | |
837 for(i=j=0; z[i]; i++) if( isDirSep(z[i]) ) j = i+1; | |
838 return z+j; | |
839 } | |
840 | |
841 /* | |
842 ** Interpret zArg as a boolean value. Return either 0 or 1. | |
843 */ | |
844 static int booleanValue(char *zArg){ | |
845 int i; | |
846 if( zArg==0 ) return 0; | |
847 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){} | |
848 if( i>0 && zArg[i]==0 ) return atoi(zArg); | |
849 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){ | |
850 return 1; | |
851 } | |
852 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){ | |
853 return 0; | |
854 } | |
855 errorMessage("unknown boolean: [%s]", zArg); | |
856 return 0; | |
857 } | |
858 | |
859 | |
860 /* This routine exists as a convenient place to set a debugger | |
861 ** breakpoint. | |
862 */ | |
863 static void test_breakpoint(void){ static volatile int cnt = 0; cnt++; } | |
864 | |
865 /* Maximum number of arguments to a --command */ | |
866 #define MX_ARG 2 | |
867 | |
868 /* | |
869 ** Run a script. | |
870 */ | |
871 static void runScript( | |
872 int iClient, /* The client number, or 0 for the master */ | |
873 int taskId, /* The task ID for clients. 0 for master */ | |
874 char *zScript, /* Text of the script */ | |
875 char *zFilename /* File from which script was read. */ | |
876 ){ | |
877 int lineno = 1; | |
878 int prevLine = 1; | |
879 int ii = 0; | |
880 int iBegin = 0; | |
881 int n, c, j; | |
882 int len; | |
883 int nArg; | |
884 String sResult; | |
885 char zCmd[30]; | |
886 char zError[1000]; | |
887 char azArg[MX_ARG][100]; | |
888 | |
889 memset(&sResult, 0, sizeof(sResult)); | |
890 stringReset(&sResult); | |
891 while( (c = zScript[ii])!=0 ){ | |
892 prevLine = lineno; | |
893 len = tokenLength(zScript+ii, &lineno); | |
894 if( ISSPACE(c) || (c=='/' && zScript[ii+1]=='*') ){ | |
895 ii += len; | |
896 continue; | |
897 } | |
898 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){ | |
899 ii += len; | |
900 continue; | |
901 } | |
902 | |
903 /* Run any prior SQL before processing the new --command */ | |
904 if( ii>iBegin ){ | |
905 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); | |
906 evalSql(&sResult, zSql); | |
907 sqlite3_free(zSql); | |
908 iBegin = ii + len; | |
909 } | |
910 | |
911 /* Parse the --command */ | |
912 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii); | |
913 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd)); | |
914 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){ | |
915 while( n<len-2 && ISSPACE(zScript[ii+2+n]) ){ n++; } | |
916 if( n>=len-2 ) break; | |
917 n += extractToken(zScript+ii+2+n, len-2-n, | |
918 azArg[nArg], sizeof(azArg[nArg])); | |
919 } | |
920 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0; | |
921 | |
922 /* | |
923 ** --sleep N | |
924 ** | |
925 ** Pause for N milliseconds | |
926 */ | |
927 if( strcmp(zCmd, "sleep")==0 ){ | |
928 sqlite3_sleep(atoi(azArg[0])); | |
929 }else | |
930 | |
931 /* | |
932 ** --exit N | |
933 ** | |
934 ** Exit this process. If N>0 then exit without shutting down | |
935 ** SQLite. (In other words, simulate a crash.) | |
936 */ | |
937 if( strcmp(zCmd, "exit")==0 ){ | |
938 int rc = atoi(azArg[0]); | |
939 finishScript(iClient, taskId, 1); | |
940 if( rc==0 ) sqlite3_close(g.db); | |
941 exit(rc); | |
942 }else | |
943 | |
944 /* | |
945 ** --testcase NAME | |
946 ** | |
947 ** Begin a new test case. Announce in the log that the test case | |
948 ** has begun. | |
949 */ | |
950 if( strcmp(zCmd, "testcase")==0 ){ | |
951 if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii); | |
952 stringReset(&sResult); | |
953 }else | |
954 | |
955 /* | |
956 ** --finish | |
957 ** | |
958 ** Mark the current task as having finished, even if it is not. | |
959 ** This can be used in conjunction with --exit to simulate a crash. | |
960 */ | |
961 if( strcmp(zCmd, "finish")==0 && iClient>0 ){ | |
962 finishScript(iClient, taskId, 1); | |
963 }else | |
964 | |
965 /* | |
966 ** --reset | |
967 ** | |
968 ** Reset accumulated results back to an empty string | |
969 */ | |
970 if( strcmp(zCmd, "reset")==0 ){ | |
971 stringReset(&sResult); | |
972 }else | |
973 | |
974 /* | |
975 ** --match ANSWER... | |
976 ** | |
977 ** Check to see if output matches ANSWER. Report an error if not. | |
978 */ | |
979 if( strcmp(zCmd, "match")==0 ){ | |
980 int jj; | |
981 char *zAns = zScript+ii; | |
982 for(jj=7; jj<len-1 && ISSPACE(zAns[jj]); jj++){} | |
983 zAns += jj; | |
984 if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){ | |
985 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]", | |
986 prevLine, zFilename, len-jj-1, zAns, sResult.z); | |
987 } | |
988 g.nTest++; | |
989 stringReset(&sResult); | |
990 }else | |
991 | |
992 /* | |
993 ** --glob ANSWER... | |
994 ** --notglob ANSWER.... | |
995 ** | |
996 ** Check to see if output does or does not match the glob pattern | |
997 ** ANSWER. | |
998 */ | |
999 if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){ | |
1000 int jj; | |
1001 char *zAns = zScript+ii; | |
1002 char *zCopy; | |
1003 int isGlob = (zCmd[0]=='g'); | |
1004 for(jj=9-3*isGlob; jj<len-1 && ISSPACE(zAns[jj]); jj++){} | |
1005 zAns += jj; | |
1006 zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns); | |
1007 if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){ | |
1008 errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]", | |
1009 prevLine, zFilename, zCopy, sResult.z); | |
1010 } | |
1011 sqlite3_free(zCopy); | |
1012 g.nTest++; | |
1013 stringReset(&sResult); | |
1014 }else | |
1015 | |
1016 /* | |
1017 ** --output | |
1018 ** | |
1019 ** Output the result of the previous SQL. | |
1020 */ | |
1021 if( strcmp(zCmd, "output")==0 ){ | |
1022 logMessage("%s", sResult.z); | |
1023 }else | |
1024 | |
1025 /* | |
1026 ** --source FILENAME | |
1027 ** | |
1028 ** Run a subscript from a separate file. | |
1029 */ | |
1030 if( strcmp(zCmd, "source")==0 ){ | |
1031 char *zNewFile, *zNewScript; | |
1032 char *zToDel = 0; | |
1033 zNewFile = azArg[0]; | |
1034 if( !isDirSep(zNewFile[0]) ){ | |
1035 int k; | |
1036 for(k=(int)strlen(zFilename)-1; k>=0 && !isDirSep(zFilename[k]); k--){} | |
1037 if( k>0 ){ | |
1038 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile); | |
1039 } | |
1040 } | |
1041 zNewScript = readFile(zNewFile); | |
1042 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile); | |
1043 runScript(0, 0, zNewScript, zNewFile); | |
1044 sqlite3_free(zNewScript); | |
1045 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile); | |
1046 sqlite3_free(zToDel); | |
1047 }else | |
1048 | |
1049 /* | |
1050 ** --print MESSAGE.... | |
1051 ** | |
1052 ** Output the remainder of the line to the log file | |
1053 */ | |
1054 if( strcmp(zCmd, "print")==0 ){ | |
1055 int jj; | |
1056 for(jj=7; jj<len && ISSPACE(zScript[ii+jj]); jj++){} | |
1057 logMessage("%.*s", len-jj, zScript+ii+jj); | |
1058 }else | |
1059 | |
1060 /* | |
1061 ** --if EXPR | |
1062 ** | |
1063 ** Skip forward to the next matching --endif or --else if EXPR is false. | |
1064 */ | |
1065 if( strcmp(zCmd, "if")==0 ){ | |
1066 int jj, rc; | |
1067 sqlite3_stmt *pStmt; | |
1068 for(jj=4; jj<len && ISSPACE(zScript[ii+jj]); jj++){} | |
1069 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj); | |
1070 rc = sqlite3_step(pStmt); | |
1071 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){ | |
1072 ii += findEndif(zScript+ii+len, 1, &lineno); | |
1073 } | |
1074 sqlite3_finalize(pStmt); | |
1075 }else | |
1076 | |
1077 /* | |
1078 ** --else | |
1079 ** | |
1080 ** This command can only be encountered if currently inside an --if that | |
1081 ** is true. Skip forward to the next matching --endif. | |
1082 */ | |
1083 if( strcmp(zCmd, "else")==0 ){ | |
1084 ii += findEndif(zScript+ii+len, 0, &lineno); | |
1085 }else | |
1086 | |
1087 /* | |
1088 ** --endif | |
1089 ** | |
1090 ** This command can only be encountered if currently inside an --if that | |
1091 ** is true or an --else of a false if. This is a no-op. | |
1092 */ | |
1093 if( strcmp(zCmd, "endif")==0 ){ | |
1094 /* no-op */ | |
1095 }else | |
1096 | |
1097 /* | |
1098 ** --start CLIENT | |
1099 ** | |
1100 ** Start up the given client. | |
1101 */ | |
1102 if( strcmp(zCmd, "start")==0 && iClient==0 ){ | |
1103 int iNewClient = atoi(azArg[0]); | |
1104 if( iNewClient>0 ){ | |
1105 startClient(iNewClient); | |
1106 } | |
1107 }else | |
1108 | |
1109 /* | |
1110 ** --wait CLIENT TIMEOUT | |
1111 ** | |
1112 ** Wait until all tasks complete for the given client. If CLIENT is | |
1113 ** "all" then wait for all clients to complete. Wait no longer than | |
1114 ** TIMEOUT milliseconds (default 10,000) | |
1115 */ | |
1116 if( strcmp(zCmd, "wait")==0 && iClient==0 ){ | |
1117 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000; | |
1118 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n", | |
1119 prevLine, zFilename); | |
1120 waitForClient(atoi(azArg[0]), iTimeout, zError); | |
1121 }else | |
1122 | |
1123 /* | |
1124 ** --task CLIENT | |
1125 ** <task-content-here> | |
1126 ** --end | |
1127 ** | |
1128 ** Assign work to a client. Start the client if it is not running | |
1129 ** already. | |
1130 */ | |
1131 if( strcmp(zCmd, "task")==0 && iClient==0 ){ | |
1132 int iTarget = atoi(azArg[0]); | |
1133 int iEnd; | |
1134 char *zTask; | |
1135 char *zTName; | |
1136 iEnd = findEnd(zScript+ii+len, &lineno); | |
1137 if( iTarget<0 ){ | |
1138 errorMessage("line %d of %s: bad client number: %d", | |
1139 prevLine, zFilename, iTarget); | |
1140 }else{ | |
1141 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len); | |
1142 if( nArg>1 ){ | |
1143 zTName = sqlite3_mprintf("%s", azArg[1]); | |
1144 }else{ | |
1145 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine); | |
1146 } | |
1147 startClient(iTarget); | |
1148 runSql("INSERT INTO task(client,script,name)" | |
1149 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName); | |
1150 sqlite3_free(zTask); | |
1151 sqlite3_free(zTName); | |
1152 } | |
1153 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno); | |
1154 len += iEnd; | |
1155 iBegin = ii+len; | |
1156 }else | |
1157 | |
1158 /* | |
1159 ** --breakpoint | |
1160 ** | |
1161 ** This command calls "test_breakpoint()" which is a routine provided | |
1162 ** as a convenient place to set a debugger breakpoint. | |
1163 */ | |
1164 if( strcmp(zCmd, "breakpoint")==0 ){ | |
1165 test_breakpoint(); | |
1166 }else | |
1167 | |
1168 /* | |
1169 ** --show-sql-errors BOOLEAN | |
1170 ** | |
1171 ** Turn display of SQL errors on and off. | |
1172 */ | |
1173 if( strcmp(zCmd, "show-sql-errors")==0 ){ | |
1174 g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1; | |
1175 }else | |
1176 | |
1177 | |
1178 /* error */{ | |
1179 errorMessage("line %d of %s: unknown command --%s", | |
1180 prevLine, zFilename, zCmd); | |
1181 } | |
1182 ii += len; | |
1183 } | |
1184 if( iBegin<ii ){ | |
1185 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin); | |
1186 runSql(zSql); | |
1187 sqlite3_free(zSql); | |
1188 } | |
1189 stringFree(&sResult); | |
1190 } | |
1191 | |
1192 /* | |
1193 ** Look for a command-line option. If present, return a pointer. | |
1194 ** Return NULL if missing. | |
1195 ** | |
1196 ** hasArg==0 means the option is a flag. It is either present or not. | |
1197 ** hasArg==1 means the option has an argument. Return a pointer to the | |
1198 ** argument. | |
1199 */ | |
1200 static char *findOption( | |
1201 char **azArg, | |
1202 int *pnArg, | |
1203 const char *zOption, | |
1204 int hasArg | |
1205 ){ | |
1206 int i, j; | |
1207 char *zReturn = 0; | |
1208 int nArg = *pnArg; | |
1209 | |
1210 assert( hasArg==0 || hasArg==1 ); | |
1211 for(i=0; i<nArg; i++){ | |
1212 const char *z; | |
1213 if( i+hasArg >= nArg ) break; | |
1214 z = azArg[i]; | |
1215 if( z[0]!='-' ) continue; | |
1216 z++; | |
1217 if( z[0]=='-' ){ | |
1218 if( z[1]==0 ) break; | |
1219 z++; | |
1220 } | |
1221 if( strcmp(z,zOption)==0 ){ | |
1222 if( hasArg && i==nArg-1 ){ | |
1223 fatalError("command-line option \"--%s\" requires an argument", z); | |
1224 } | |
1225 if( hasArg ){ | |
1226 zReturn = azArg[i+1]; | |
1227 }else{ | |
1228 zReturn = azArg[i]; | |
1229 } | |
1230 j = i+1+(hasArg!=0); | |
1231 while( j<nArg ) azArg[i++] = azArg[j++]; | |
1232 *pnArg = i; | |
1233 return zReturn; | |
1234 } | |
1235 } | |
1236 return zReturn; | |
1237 } | |
1238 | |
1239 /* Print a usage message for the program and exit */ | |
1240 static void usage(const char *argv0){ | |
1241 int i; | |
1242 const char *zTail = argv0; | |
1243 for(i=0; argv0[i]; i++){ | |
1244 if( isDirSep(argv0[i]) ) zTail = argv0+i+1; | |
1245 } | |
1246 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail); | |
1247 exit(1); | |
1248 } | |
1249 | |
1250 /* Report on unrecognized arguments */ | |
1251 static void unrecognizedArguments( | |
1252 const char *argv0, | |
1253 int nArg, | |
1254 char **azArg | |
1255 ){ | |
1256 int i; | |
1257 fprintf(stderr,"%s: unrecognized arguments:", argv0); | |
1258 for(i=0; i<nArg; i++){ | |
1259 fprintf(stderr," %s", azArg[i]); | |
1260 } | |
1261 fprintf(stderr,"\n"); | |
1262 exit(1); | |
1263 } | |
1264 | |
1265 int SQLITE_CDECL main(int argc, char **argv){ | |
1266 const char *zClient; | |
1267 int iClient; | |
1268 int n, i; | |
1269 int openFlags = SQLITE_OPEN_READWRITE; | |
1270 int rc; | |
1271 char *zScript; | |
1272 int taskId; | |
1273 const char *zTrace; | |
1274 const char *zCOption; | |
1275 const char *zJMode; | |
1276 const char *zNRep; | |
1277 int nRep = 1, iRep; | |
1278 | |
1279 g.argv0 = argv[0]; | |
1280 g.iTrace = 1; | |
1281 if( argc<2 ) usage(argv[0]); | |
1282 g.zDbFile = argv[1]; | |
1283 if( strglob("*.test", g.zDbFile) ) usage(argv[0]); | |
1284 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){ | |
1285 fprintf(stderr, "SQLite library and header mismatch\n" | |
1286 "Library: %s\n" | |
1287 "Header: %s\n", | |
1288 sqlite3_sourceid(), SQLITE_SOURCE_ID); | |
1289 exit(1); | |
1290 } | |
1291 n = argc-2; | |
1292 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID()); | |
1293 zJMode = findOption(argv+2, &n, "journalmode", 1); | |
1294 zNRep = findOption(argv+2, &n, "repeat", 1); | |
1295 if( zNRep ) nRep = atoi(zNRep); | |
1296 if( nRep<1 ) nRep = 1; | |
1297 g.zVfs = findOption(argv+2, &n, "vfs", 1); | |
1298 zClient = findOption(argv+2, &n, "client", 1); | |
1299 g.zErrLog = findOption(argv+2, &n, "errlog", 1); | |
1300 g.zLog = findOption(argv+2, &n, "log", 1); | |
1301 zTrace = findOption(argv+2, &n, "trace", 1); | |
1302 if( zTrace ) g.iTrace = atoi(zTrace); | |
1303 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0; | |
1304 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0; | |
1305 g.bSync = findOption(argv+2, &n, "sync", 0)!=0; | |
1306 if( g.zErrLog ){ | |
1307 g.pErrLog = fopen(g.zErrLog, "a"); | |
1308 }else{ | |
1309 g.pErrLog = stderr; | |
1310 } | |
1311 if( g.zLog ){ | |
1312 g.pLog = fopen(g.zLog, "a"); | |
1313 }else{ | |
1314 g.pLog = stdout; | |
1315 } | |
1316 | |
1317 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0); | |
1318 if( zClient ){ | |
1319 iClient = atoi(zClient); | |
1320 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient); | |
1321 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d", | |
1322 GETPID(), iClient); | |
1323 }else{ | |
1324 if( g.iTrace>0 ){ | |
1325 printf("BEGIN: %s", argv[0]); | |
1326 for(i=1; i<argc; i++) printf(" %s", argv[i]); | |
1327 printf("\n"); | |
1328 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" ); | |
1329 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){ | |
1330 printf("-DSQLITE_%s\n", zCOption); | |
1331 } | |
1332 fflush(stdout); | |
1333 } | |
1334 iClient = 0; | |
1335 unlink(g.zDbFile); | |
1336 openFlags |= SQLITE_OPEN_CREATE; | |
1337 } | |
1338 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs); | |
1339 if( rc ) fatalError("cannot open [%s]", g.zDbFile); | |
1340 if( zJMode ){ | |
1341 #if defined(_WIN32) | |
1342 if( sqlite3_stricmp(zJMode,"persist")==0 | |
1343 || sqlite3_stricmp(zJMode,"truncate")==0 | |
1344 ){ | |
1345 printf("Changing journal mode to DELETE from %s", zJMode); | |
1346 zJMode = "DELETE"; | |
1347 } | |
1348 #endif | |
1349 runSql("PRAGMA journal_mode=%Q;", zJMode); | |
1350 } | |
1351 if( !g.bSync ) trySql("PRAGMA synchronous=OFF"); | |
1352 sqlite3_enable_load_extension(g.db, 1); | |
1353 sqlite3_busy_handler(g.db, busyHandler, 0); | |
1354 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0, | |
1355 vfsNameFunc, 0, 0); | |
1356 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0, | |
1357 evalFunc, 0, 0); | |
1358 g.iTimeout = DEFAULT_TIMEOUT; | |
1359 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0); | |
1360 if( iClient>0 ){ | |
1361 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2); | |
1362 if( g.iTrace ) logMessage("start-client"); | |
1363 while(1){ | |
1364 char *zTaskName = 0; | |
1365 rc = startScript(iClient, &zScript, &taskId, &zTaskName); | |
1366 if( rc==SQLITE_DONE ) break; | |
1367 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId); | |
1368 runScript(iClient, taskId, zScript, zTaskName); | |
1369 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId); | |
1370 finishScript(iClient, taskId, 0); | |
1371 sqlite3_free(zTaskName); | |
1372 sqlite3_sleep(10); | |
1373 } | |
1374 if( g.iTrace ) logMessage("end-client"); | |
1375 }else{ | |
1376 sqlite3_stmt *pStmt; | |
1377 int iTimeout; | |
1378 if( n==0 ){ | |
1379 fatalError("missing script filename"); | |
1380 } | |
1381 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2); | |
1382 runSql( | |
1383 "DROP TABLE IF EXISTS task;\n" | |
1384 "DROP TABLE IF EXISTS counters;\n" | |
1385 "DROP TABLE IF EXISTS client;\n" | |
1386 "CREATE TABLE task(\n" | |
1387 " id INTEGER PRIMARY KEY,\n" | |
1388 " name TEXT,\n" | |
1389 " client INTEGER,\n" | |
1390 " starttime DATE,\n" | |
1391 " endtime DATE,\n" | |
1392 " script TEXT\n" | |
1393 ");" | |
1394 "CREATE INDEX task_i1 ON task(client, starttime);\n" | |
1395 "CREATE INDEX task_i2 ON task(client, endtime);\n" | |
1396 "CREATE TABLE counters(nError,nTest);\n" | |
1397 "INSERT INTO counters VALUES(0,0);\n" | |
1398 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n" | |
1399 ); | |
1400 zScript = readFile(argv[2]); | |
1401 for(iRep=1; iRep<=nRep; iRep++){ | |
1402 if( g.iTrace ) logMessage("begin script [%s] cycle %d\n", argv[2], iRep); | |
1403 runScript(0, 0, zScript, argv[2]); | |
1404 if( g.iTrace ) logMessage("end script [%s] cycle %d\n", argv[2], iRep); | |
1405 } | |
1406 sqlite3_free(zScript); | |
1407 waitForClient(0, 2000, "during shutdown...\n"); | |
1408 trySql("UPDATE client SET wantHalt=1"); | |
1409 sqlite3_sleep(10); | |
1410 g.iTimeout = 0; | |
1411 iTimeout = 1000; | |
1412 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY | |
1413 || rc==SQLITE_ROW) && iTimeout>0 ){ | |
1414 sqlite3_sleep(10); | |
1415 iTimeout -= 10; | |
1416 } | |
1417 sqlite3_sleep(100); | |
1418 pStmt = prepareSql("SELECT nError, nTest FROM counters"); | |
1419 iTimeout = 1000; | |
1420 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){ | |
1421 sqlite3_sleep(10); | |
1422 iTimeout -= 10; | |
1423 } | |
1424 if( rc==SQLITE_ROW ){ | |
1425 g.nError += sqlite3_column_int(pStmt, 0); | |
1426 g.nTest += sqlite3_column_int(pStmt, 1); | |
1427 } | |
1428 sqlite3_finalize(pStmt); | |
1429 } | |
1430 sqlite3_close(g.db); | |
1431 maybeClose(g.pLog); | |
1432 maybeClose(g.pErrLog); | |
1433 if( iClient==0 ){ | |
1434 printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest); | |
1435 printf("END: %s", argv[0]); | |
1436 for(i=1; i<argc; i++) printf(" %s", argv[i]); | |
1437 printf("\n"); | |
1438 } | |
1439 return g.nError>0; | |
1440 } | |
OLD | NEW |