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