1 /* 2 ** 2010-07-22 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 ** The code in this file runs a few multi-threaded test cases using the 14 ** SQLite library. It can be compiled to an executable on unix using the 15 ** following command: 16 ** 17 ** gcc -O2 threadtest3.c sqlite3.c -ldl -lpthread -lm 18 ** 19 ** Even though threadtest3.c is the only C source code file mentioned on 20 ** the compiler command-line, #include macros are used to pull in additional 21 ** C code files named "tt3_*.c". 22 ** 23 ** After compiling, run this program with an optional argument telling 24 ** which test to run. All tests are run if no argument is given. The 25 ** argument can be a glob pattern to match multiple tests. Examples: 26 ** 27 ** ./a.out -- Run all tests 28 ** ./a.out walthread3 -- Run the "walthread3" test 29 ** ./a.out 'wal*' -- Run all of the wal* tests 30 ** ./a.out --help -- List all available tests 31 ** 32 ** The exit status is non-zero if any test fails. 33 */ 34 35 /* 36 ** The "Set Error Line" macro. 37 */ 38 #define SEL(e) ((e)->iLine = ((e)->rc ? (e)->iLine : __LINE__)) 39 40 /* Database functions */ 41 #define opendb(w,x,y,z) (SEL(w), opendb_x(w,x,y,z)) 42 #define closedb(y,z) (SEL(y), closedb_x(y,z)) 43 44 /* Functions to execute SQL */ 45 #define sql_script(x,y,z) (SEL(x), sql_script_x(x,y,z)) 46 #define integrity_check(x,y) (SEL(x), integrity_check_x(x,y)) 47 #define execsql_i64(x,y,...) (SEL(x), execsql_i64_x(x,y,__VA_ARGS__)) 48 #define execsql_text(x,y,z,...) (SEL(x), execsql_text_x(x,y,z,__VA_ARGS__)) 49 #define execsql(x,y,...) (SEL(x), (void)execsql_i64_x(x,y,__VA_ARGS__)) 50 #define sql_script_printf(x,y,z,...) ( \ 51 SEL(x), sql_script_printf_x(x,y,z,__VA_ARGS__) \ 52 ) 53 54 /* Thread functions */ 55 #define launch_thread(w,x,y,z) (SEL(w), launch_thread_x(w,x,y,z)) 56 #define join_all_threads(y,z) (SEL(y), join_all_threads_x(y,z)) 57 58 /* Timer functions */ 59 #define setstoptime(y,z) (SEL(y), setstoptime_x(y,z)) 60 #define timetostop(z) (SEL(z), timetostop_x(z)) 61 62 /* Report/clear errors. */ 63 #define test_error(z, ...) test_error_x(z, sqlite3_mprintf(__VA_ARGS__)) 64 #define clear_error(y,z) clear_error_x(y, z) 65 66 /* File-system operations */ 67 #define filesize(y,z) (SEL(y), filesize_x(y,z)) 68 #define filecopy(x,y,z) (SEL(x), filecopy_x(x,y,z)) 69 70 #define PTR2INT(x) ((int)((intptr_t)x)) 71 #define INT2PTR(x) ((void*)((intptr_t)x)) 72 73 /* 74 ** End of test code/infrastructure interface macros. 75 *************************************************************************/ 76 77 78 79 80 #include <sqlite3.h> 81 82 #ifdef _WIN32 83 # include <stdio.h> 84 # include <string.h> 85 # include <assert.h> 86 # include <process.h> 87 # include <windows.h> 88 # include <sys/types.h> 89 # include <sys/stat.h> 90 # include <errno.h> 91 # include <fcntl.h> 92 # include <io.h> 93 #else 94 # include <unistd.h> 95 # include <stdio.h> 96 # include <pthread.h> 97 # include <assert.h> 98 # include <sys/types.h> 99 # include <sys/stat.h> 100 # include <string.h> 101 # include <fcntl.h> 102 # include <errno.h> 103 104 # define O_BINARY 0 105 #endif 106 107 #include "test_multiplex.h" 108 109 /* Required to link test_multiplex.c */ 110 #ifndef SQLITE_OMIT_WSD 111 int sqlite3PendingByte = 0x40000000; 112 #endif 113 114 /* 115 * This code implements the MD5 message-digest algorithm. 116 * The algorithm is due to Ron Rivest. This code was 117 * written by Colin Plumb in 1993, no copyright is claimed. 118 * This code is in the public domain; do with it what you wish. 119 * 120 * Equivalent code is available from RSA Data Security, Inc. 121 * This code has been tested against that, and is equivalent, 122 * except that you don't need to include two pages of legalese 123 * with every copy. 124 * 125 * To compute the message digest of a chunk of bytes, declare an 126 * MD5Context structure, pass it to MD5Init, call MD5Update as 127 * needed on buffers full of bytes, and then call MD5Final, which 128 * will fill a supplied 16-byte array with the digest. 129 */ 130 131 /* 132 * If compiled on a machine that doesn't have a 32-bit integer, 133 * you just set "uint32" to the appropriate datatype for an 134 * unsigned 32-bit integer. For example: 135 * 136 * cc -Duint32='unsigned long' md5.c 137 * 138 */ 139 #ifndef uint32 140 # define uint32 unsigned int 141 #endif 142 143 struct MD5Context { 144 int isInit; 145 uint32 buf[4]; 146 uint32 bits[2]; 147 union { 148 unsigned char in[64]; 149 uint32 in32[16]; 150 } u; 151 }; 152 typedef struct MD5Context MD5Context; 153 154 /* 155 * Note: this code is harmless on little-endian machines. 156 */ 157 static void byteReverse (unsigned char *buf, unsigned longs){ 158 uint32 t; 159 do { 160 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 | 161 ((unsigned)buf[1]<<8 | buf[0]); 162 *(uint32 *)buf = t; 163 buf += 4; 164 } while (--longs); 165 } 166 /* The four core functions - F1 is optimized somewhat */ 167 168 /* #define F1(x, y, z) (x & y | ~x & z) */ 169 #define F1(x, y, z) (z ^ (x & (y ^ z))) 170 #define F2(x, y, z) F1(z, x, y) 171 #define F3(x, y, z) (x ^ y ^ z) 172 #define F4(x, y, z) (y ^ (x | ~z)) 173 174 /* This is the central step in the MD5 algorithm. */ 175 #define MD5STEP(f, w, x, y, z, data, s) \ 176 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 177 178 /* 179 * The core of the MD5 algorithm, this alters an existing MD5 hash to 180 * reflect the addition of 16 longwords of new data. MD5Update blocks 181 * the data and converts bytes into longwords for this routine. 182 */ 183 static void MD5Transform(uint32 buf[4], const uint32 in[16]){ 184 register uint32 a, b, c, d; 185 186 a = buf[0]; 187 b = buf[1]; 188 c = buf[2]; 189 d = buf[3]; 190 191 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7); 192 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12); 193 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17); 194 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22); 195 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7); 196 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12); 197 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17); 198 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22); 199 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7); 200 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12); 201 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17); 202 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22); 203 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7); 204 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12); 205 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17); 206 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22); 207 208 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5); 209 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9); 210 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14); 211 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20); 212 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5); 213 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9); 214 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14); 215 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20); 216 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5); 217 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9); 218 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14); 219 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20); 220 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5); 221 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9); 222 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14); 223 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20); 224 225 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4); 226 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11); 227 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16); 228 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23); 229 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4); 230 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11); 231 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16); 232 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23); 233 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4); 234 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11); 235 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16); 236 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23); 237 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4); 238 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11); 239 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16); 240 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23); 241 242 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6); 243 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10); 244 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15); 245 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21); 246 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6); 247 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10); 248 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15); 249 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21); 250 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6); 251 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10); 252 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15); 253 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21); 254 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6); 255 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10); 256 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15); 257 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21); 258 259 buf[0] += a; 260 buf[1] += b; 261 buf[2] += c; 262 buf[3] += d; 263 } 264 265 /* 266 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 267 * initialization constants. 268 */ 269 static void MD5Init(MD5Context *ctx){ 270 ctx->isInit = 1; 271 ctx->buf[0] = 0x67452301; 272 ctx->buf[1] = 0xefcdab89; 273 ctx->buf[2] = 0x98badcfe; 274 ctx->buf[3] = 0x10325476; 275 ctx->bits[0] = 0; 276 ctx->bits[1] = 0; 277 } 278 279 /* 280 * Update context to reflect the concatenation of another buffer full 281 * of bytes. 282 */ 283 static 284 void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){ 285 uint32 t; 286 287 /* Update bitcount */ 288 289 t = ctx->bits[0]; 290 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t) 291 ctx->bits[1]++; /* Carry from low to high */ 292 ctx->bits[1] += len >> 29; 293 294 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 295 296 /* Handle any leading odd-sized chunks */ 297 298 if ( t ) { 299 unsigned char *p = (unsigned char *)ctx->u.in + t; 300 301 t = 64-t; 302 if (len < t) { 303 memcpy(p, buf, len); 304 return; 305 } 306 memcpy(p, buf, t); 307 byteReverse(ctx->u.in, 16); 308 MD5Transform(ctx->buf, (uint32 *)ctx->u.in); 309 buf += t; 310 len -= t; 311 } 312 313 /* Process data in 64-byte chunks */ 314 315 while (len >= 64) { 316 memcpy(ctx->u.in, buf, 64); 317 byteReverse(ctx->u.in, 16); 318 MD5Transform(ctx->buf, (uint32 *)ctx->u.in); 319 buf += 64; 320 len -= 64; 321 } 322 323 /* Handle any remaining bytes of data. */ 324 325 memcpy(ctx->u.in, buf, len); 326 } 327 328 /* 329 * Final wrapup - pad to 64-byte boundary with the bit pattern 330 * 1 0* (64-bit count of bits processed, MSB-first) 331 */ 332 static void MD5Final(unsigned char digest[16], MD5Context *ctx){ 333 unsigned count; 334 unsigned char *p; 335 336 /* Compute number of bytes mod 64 */ 337 count = (ctx->bits[0] >> 3) & 0x3F; 338 339 /* Set the first char of padding to 0x80. This is safe since there is 340 always at least one byte free */ 341 p = ctx->u.in + count; 342 *p++ = 0x80; 343 344 /* Bytes of padding needed to make 64 bytes */ 345 count = 64 - 1 - count; 346 347 /* Pad out to 56 mod 64 */ 348 if (count < 8) { 349 /* Two lots of padding: Pad the first block to 64 bytes */ 350 memset(p, 0, count); 351 byteReverse(ctx->u.in, 16); 352 MD5Transform(ctx->buf, (uint32 *)ctx->u.in); 353 354 /* Now fill the next block with 56 bytes */ 355 memset(ctx->u.in, 0, 56); 356 } else { 357 /* Pad block to 56 bytes */ 358 memset(p, 0, count-8); 359 } 360 byteReverse(ctx->u.in, 14); 361 362 /* Append length in bits and transform */ 363 ctx->u.in32[14] = ctx->bits[0]; 364 ctx->u.in32[15] = ctx->bits[1]; 365 366 MD5Transform(ctx->buf, (uint32 *)ctx->u.in); 367 byteReverse((unsigned char *)ctx->buf, 4); 368 memcpy(digest, ctx->buf, 16); 369 memset(ctx, 0, sizeof(*ctx)); /* In case it is sensitive */ 370 } 371 372 /* 373 ** Convert a 128-bit MD5 digest into a 32-digit base-16 number. 374 */ 375 static void MD5DigestToBase16(unsigned char *digest, char *zBuf){ 376 static char const zEncode[] = "0123456789abcdef"; 377 int i, j; 378 379 for(j=i=0; i<16; i++){ 380 int a = digest[i]; 381 zBuf[j++] = zEncode[(a>>4)&0xf]; 382 zBuf[j++] = zEncode[a & 0xf]; 383 } 384 zBuf[j] = 0; 385 } 386 387 /* 388 ** During testing, the special md5sum() aggregate function is available. 389 ** inside SQLite. The following routines implement that function. 390 */ 391 static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){ 392 MD5Context *p; 393 int i; 394 if( argc<1 ) return; 395 p = sqlite3_aggregate_context(context, sizeof(*p)); 396 if( p==0 ) return; 397 if( !p->isInit ){ 398 MD5Init(p); 399 } 400 for(i=0; i<argc; i++){ 401 const char *zData = (char*)sqlite3_value_text(argv[i]); 402 if( zData ){ 403 MD5Update(p, (unsigned char*)zData, strlen(zData)); 404 } 405 } 406 } 407 static void md5finalize(sqlite3_context *context){ 408 MD5Context *p; 409 unsigned char digest[16]; 410 char zBuf[33]; 411 p = sqlite3_aggregate_context(context, sizeof(*p)); 412 MD5Final(digest,p); 413 MD5DigestToBase16(digest, zBuf); 414 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT); 415 } 416 417 /* 418 ** End of copied md5sum() code. 419 **************************************************************************/ 420 421 typedef sqlite3_int64 i64; 422 423 typedef struct Error Error; 424 typedef struct Sqlite Sqlite; 425 typedef struct Statement Statement; 426 427 typedef struct Threadset Threadset; 428 typedef struct Thread Thread; 429 430 /* Total number of errors in this process so far. */ 431 static int nGlobalErr = 0; 432 433 struct Error { 434 int rc; 435 int iLine; 436 char *zErr; 437 }; 438 439 struct Sqlite { 440 sqlite3 *db; /* Database handle */ 441 Statement *pCache; /* Linked list of cached statements */ 442 int nText; /* Size of array at aText[] */ 443 char **aText; /* Stored text results */ 444 }; 445 446 struct Statement { 447 sqlite3_stmt *pStmt; /* Pre-compiled statement handle */ 448 Statement *pNext; /* Next statement in linked-list */ 449 }; 450 451 struct Thread { 452 int iTid; /* Thread number within test */ 453 void* pArg; /* Pointer argument passed by caller */ 454 455 #ifdef _WIN32 456 uintptr_t winTid; /* Thread handle */ 457 #else 458 pthread_t tid; /* Thread id */ 459 #endif 460 char *(*xProc)(int, void*); /* Thread main proc */ 461 char *zRes; /* Value returned by xProc */ 462 Thread *pNext; /* Next in this list of threads */ 463 }; 464 465 struct Threadset { 466 int iMaxTid; /* Largest iTid value allocated so far */ 467 Thread *pThread; /* Linked list of threads */ 468 }; 469 470 static void free_err(Error *p){ 471 sqlite3_free(p->zErr); 472 p->zErr = 0; 473 p->rc = 0; 474 } 475 476 static void print_err(Error *p){ 477 if( p->rc!=SQLITE_OK ){ 478 int isWarn = 0; 479 if( p->rc==SQLITE_SCHEMA ) isWarn = 1; 480 if( sqlite3_strglob("* - no such table: *",p->zErr)==0 ) isWarn = 1; 481 printf("%s: (%d) \"%s\" at line %d\n", isWarn ? "Warning" : "Error", 482 p->rc, p->zErr, p->iLine); 483 if( !isWarn ) nGlobalErr++; 484 fflush(stdout); 485 } 486 } 487 488 static void print_and_free_err(Error *p){ 489 print_err(p); 490 free_err(p); 491 } 492 493 static void system_error(Error *pErr, int iSys){ 494 pErr->rc = iSys; 495 #if _WIN32 496 pErr->zErr = sqlite3_mprintf("%s", strerror(iSys)); 497 #else 498 pErr->zErr = (char *)sqlite3_malloc(512); 499 strerror_r(iSys, pErr->zErr, 512); 500 pErr->zErr[511] = '\0'; 501 #endif 502 } 503 504 static void sqlite_error( 505 Error *pErr, 506 Sqlite *pDb, 507 const char *zFunc 508 ){ 509 pErr->rc = sqlite3_errcode(pDb->db); 510 pErr->zErr = sqlite3_mprintf( 511 "sqlite3_%s() - %s (%d)", zFunc, sqlite3_errmsg(pDb->db), 512 sqlite3_extended_errcode(pDb->db) 513 ); 514 } 515 516 static void test_error_x( 517 Error *pErr, 518 char *zErr 519 ){ 520 if( pErr->rc==SQLITE_OK ){ 521 pErr->rc = 1; 522 pErr->zErr = zErr; 523 }else{ 524 sqlite3_free(zErr); 525 } 526 } 527 528 static void clear_error_x( 529 Error *pErr, 530 int rc 531 ){ 532 if( pErr->rc==rc ){ 533 pErr->rc = SQLITE_OK; 534 sqlite3_free(pErr->zErr); 535 pErr->zErr = 0; 536 } 537 } 538 539 static int busyhandler(void *pArg, int n){ 540 sqlite3_sleep(10); 541 return 1; 542 } 543 544 static void opendb_x( 545 Error *pErr, /* IN/OUT: Error code */ 546 Sqlite *pDb, /* OUT: Database handle */ 547 const char *zFile, /* Database file name */ 548 int bDelete /* True to delete db file before opening */ 549 ){ 550 if( pErr->rc==SQLITE_OK ){ 551 int rc; 552 int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI; 553 if( bDelete ) unlink(zFile); 554 rc = sqlite3_open_v2(zFile, &pDb->db, flags, 0); 555 if( rc ){ 556 sqlite_error(pErr, pDb, "open"); 557 sqlite3_close(pDb->db); 558 pDb->db = 0; 559 }else{ 560 sqlite3_create_function( 561 pDb->db, "md5sum", -1, SQLITE_UTF8, 0, 0, md5step, md5finalize 562 ); 563 sqlite3_busy_handler(pDb->db, busyhandler, 0); 564 sqlite3_exec(pDb->db, "PRAGMA synchronous=OFF", 0, 0, 0); 565 } 566 } 567 } 568 569 static void closedb_x( 570 Error *pErr, /* IN/OUT: Error code */ 571 Sqlite *pDb /* OUT: Database handle */ 572 ){ 573 int rc; 574 int i; 575 Statement *pIter; 576 Statement *pNext; 577 for(pIter=pDb->pCache; pIter; pIter=pNext){ 578 pNext = pIter->pNext; 579 sqlite3_finalize(pIter->pStmt); 580 sqlite3_free(pIter); 581 } 582 for(i=0; i<pDb->nText; i++){ 583 sqlite3_free(pDb->aText[i]); 584 } 585 sqlite3_free(pDb->aText); 586 rc = sqlite3_close(pDb->db); 587 if( rc && pErr->rc==SQLITE_OK ){ 588 pErr->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(pDb->db)); 589 } 590 memset(pDb, 0, sizeof(Sqlite)); 591 } 592 593 static void sql_script_x( 594 Error *pErr, /* IN/OUT: Error code */ 595 Sqlite *pDb, /* Database handle */ 596 const char *zSql /* SQL script to execute */ 597 ){ 598 if( pErr->rc==SQLITE_OK ){ 599 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr); 600 } 601 } 602 603 static void sql_script_printf_x( 604 Error *pErr, /* IN/OUT: Error code */ 605 Sqlite *pDb, /* Database handle */ 606 const char *zFormat, /* SQL printf format string */ 607 ... /* Printf args */ 608 ){ 609 va_list ap; /* ... printf arguments */ 610 va_start(ap, zFormat); 611 if( pErr->rc==SQLITE_OK ){ 612 char *zSql = sqlite3_vmprintf(zFormat, ap); 613 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr); 614 sqlite3_free(zSql); 615 } 616 va_end(ap); 617 } 618 619 static Statement *getSqlStatement( 620 Error *pErr, /* IN/OUT: Error code */ 621 Sqlite *pDb, /* Database handle */ 622 const char *zSql /* SQL statement */ 623 ){ 624 Statement *pRet; 625 int rc; 626 627 for(pRet=pDb->pCache; pRet; pRet=pRet->pNext){ 628 if( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ){ 629 return pRet; 630 } 631 } 632 633 pRet = sqlite3_malloc(sizeof(Statement)); 634 rc = sqlite3_prepare_v2(pDb->db, zSql, -1, &pRet->pStmt, 0); 635 if( rc!=SQLITE_OK ){ 636 sqlite_error(pErr, pDb, "prepare_v2"); 637 return 0; 638 } 639 assert( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ); 640 641 pRet->pNext = pDb->pCache; 642 pDb->pCache = pRet; 643 return pRet; 644 } 645 646 static sqlite3_stmt *getAndBindSqlStatement( 647 Error *pErr, /* IN/OUT: Error code */ 648 Sqlite *pDb, /* Database handle */ 649 va_list ap /* SQL followed by parameters */ 650 ){ 651 Statement *pStatement; /* The SQLite statement wrapper */ 652 sqlite3_stmt *pStmt; /* The SQLite statement to return */ 653 int i; /* Used to iterate through parameters */ 654 655 pStatement = getSqlStatement(pErr, pDb, va_arg(ap, const char *)); 656 if( !pStatement ) return 0; 657 pStmt = pStatement->pStmt; 658 for(i=1; i<=sqlite3_bind_parameter_count(pStmt); i++){ 659 const char *zName = sqlite3_bind_parameter_name(pStmt, i); 660 void * pArg = va_arg(ap, void*); 661 662 switch( zName[1] ){ 663 case 'i': 664 sqlite3_bind_int64(pStmt, i, *(i64 *)pArg); 665 break; 666 667 default: 668 pErr->rc = 1; 669 pErr->zErr = sqlite3_mprintf("Cannot discern type: \"%s\"", zName); 670 pStmt = 0; 671 break; 672 } 673 } 674 675 return pStmt; 676 } 677 678 static i64 execsql_i64_x( 679 Error *pErr, /* IN/OUT: Error code */ 680 Sqlite *pDb, /* Database handle */ 681 ... /* SQL and pointers to parameter values */ 682 ){ 683 i64 iRet = 0; 684 if( pErr->rc==SQLITE_OK ){ 685 sqlite3_stmt *pStmt; /* SQL statement to execute */ 686 va_list ap; /* ... arguments */ 687 va_start(ap, pDb); 688 pStmt = getAndBindSqlStatement(pErr, pDb, ap); 689 if( pStmt ){ 690 int first = 1; 691 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 692 if( first && sqlite3_column_count(pStmt)>0 ){ 693 iRet = sqlite3_column_int64(pStmt, 0); 694 } 695 first = 0; 696 } 697 if( SQLITE_OK!=sqlite3_reset(pStmt) ){ 698 sqlite_error(pErr, pDb, "reset"); 699 } 700 } 701 va_end(ap); 702 } 703 return iRet; 704 } 705 706 static char * execsql_text_x( 707 Error *pErr, /* IN/OUT: Error code */ 708 Sqlite *pDb, /* Database handle */ 709 int iSlot, /* Db handle slot to store text in */ 710 ... /* SQL and pointers to parameter values */ 711 ){ 712 char *zRet = 0; 713 714 if( iSlot>=pDb->nText ){ 715 int nByte = sizeof(char *)*(iSlot+1); 716 pDb->aText = (char **)sqlite3_realloc(pDb->aText, nByte); 717 memset(&pDb->aText[pDb->nText], 0, sizeof(char*)*(iSlot+1-pDb->nText)); 718 pDb->nText = iSlot+1; 719 } 720 721 if( pErr->rc==SQLITE_OK ){ 722 sqlite3_stmt *pStmt; /* SQL statement to execute */ 723 va_list ap; /* ... arguments */ 724 va_start(ap, iSlot); 725 pStmt = getAndBindSqlStatement(pErr, pDb, ap); 726 if( pStmt ){ 727 int first = 1; 728 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 729 if( first && sqlite3_column_count(pStmt)>0 ){ 730 zRet = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); 731 sqlite3_free(pDb->aText[iSlot]); 732 pDb->aText[iSlot] = zRet; 733 } 734 first = 0; 735 } 736 if( SQLITE_OK!=sqlite3_reset(pStmt) ){ 737 sqlite_error(pErr, pDb, "reset"); 738 } 739 } 740 va_end(ap); 741 } 742 743 return zRet; 744 } 745 746 static void integrity_check_x( 747 Error *pErr, /* IN/OUT: Error code */ 748 Sqlite *pDb /* Database handle */ 749 ){ 750 if( pErr->rc==SQLITE_OK ){ 751 Statement *pStatement; /* Statement to execute */ 752 char *zErr = 0; /* Integrity check error */ 753 754 pStatement = getSqlStatement(pErr, pDb, "PRAGMA integrity_check"); 755 if( pStatement ){ 756 sqlite3_stmt *pStmt = pStatement->pStmt; 757 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 758 const char *z = (const char*)sqlite3_column_text(pStmt, 0); 759 if( strcmp(z, "ok") ){ 760 if( zErr==0 ){ 761 zErr = sqlite3_mprintf("%s", z); 762 }else{ 763 zErr = sqlite3_mprintf("%z\n%s", zErr, z); 764 } 765 } 766 } 767 sqlite3_reset(pStmt); 768 769 if( zErr ){ 770 pErr->zErr = zErr; 771 pErr->rc = 1; 772 } 773 } 774 } 775 } 776 777 #ifdef _WIN32 778 static unsigned __stdcall launch_thread_main(void *pArg){ 779 Thread *p = (Thread *)pArg; 780 p->zRes = p->xProc(p->iTid, p->pArg); 781 _endthreadex(0); 782 return 0; /* NOT REACHED */ 783 } 784 #else 785 static void *launch_thread_main(void *pArg){ 786 Thread *p = (Thread *)pArg; 787 p->zRes = p->xProc(p->iTid, p->pArg); 788 return 0; 789 } 790 #endif 791 792 static void launch_thread_x( 793 Error *pErr, /* IN/OUT: Error code */ 794 Threadset *pThreads, /* Thread set */ 795 char *(*xProc)(int, void*), /* Proc to run */ 796 void *pArg /* Argument passed to thread proc */ 797 ){ 798 if( pErr->rc==SQLITE_OK ){ 799 int iTid = ++pThreads->iMaxTid; 800 Thread *p; 801 int rc; 802 803 p = (Thread *)sqlite3_malloc(sizeof(Thread)); 804 memset(p, 0, sizeof(Thread)); 805 p->iTid = iTid; 806 p->pArg = pArg; 807 p->xProc = xProc; 808 809 #ifdef _WIN32 810 rc = SQLITE_OK; 811 p->winTid = _beginthreadex(0, 0, launch_thread_main, (void*)p, 0, 0); 812 if( p->winTid==0 ) rc = errno ? errno : rc; 813 #else 814 rc = pthread_create(&p->tid, NULL, launch_thread_main, (void *)p); 815 #endif 816 if( rc!=0 ){ 817 system_error(pErr, rc); 818 sqlite3_free(p); 819 }else{ 820 p->pNext = pThreads->pThread; 821 pThreads->pThread = p; 822 } 823 } 824 } 825 826 static void join_all_threads_x( 827 Error *pErr, /* IN/OUT: Error code */ 828 Threadset *pThreads /* Thread set */ 829 ){ 830 Thread *p; 831 Thread *pNext; 832 for(p=pThreads->pThread; p; p=pNext){ 833 void *ret; 834 int rc; 835 pNext = p->pNext; 836 837 #ifdef _WIN32 838 do { 839 rc = WaitForSingleObjectEx((HANDLE)p->winTid, INFINITE, TRUE); 840 }while( rc==WAIT_IO_COMPLETION ); 841 CloseHandle((HANDLE)p->winTid); 842 #else 843 rc = pthread_join(p->tid, &ret); 844 #endif 845 846 if( rc!=0 ){ 847 if( pErr->rc==SQLITE_OK ) system_error(pErr, rc); 848 }else{ 849 printf("Thread %d says: %s\n", p->iTid, (p->zRes==0 ? "..." : p->zRes)); 850 fflush(stdout); 851 } 852 sqlite3_free(p->zRes); 853 sqlite3_free(p); 854 } 855 pThreads->pThread = 0; 856 } 857 858 #ifdef _WIN32 859 # define THREADTEST3_STAT _stat 860 #else 861 # define THREADTEST3_STAT stat 862 #endif 863 864 static i64 filesize_x( 865 Error *pErr, 866 const char *zFile 867 ){ 868 i64 iRet = 0; 869 if( pErr->rc==SQLITE_OK ){ 870 struct THREADTEST3_STAT sStat; 871 if( THREADTEST3_STAT(zFile, &sStat) ){ 872 iRet = -1; 873 }else{ 874 iRet = sStat.st_size; 875 } 876 } 877 return iRet; 878 } 879 880 static void filecopy_x( 881 Error *pErr, 882 const char *zFrom, 883 const char *zTo 884 ){ 885 if( pErr->rc==SQLITE_OK ){ 886 i64 nByte = filesize_x(pErr, zFrom); 887 if( nByte<0 ){ 888 test_error_x(pErr, sqlite3_mprintf("no such file: %s", zFrom)); 889 }else{ 890 i64 iOff; 891 char aBuf[1024]; 892 int fd1; 893 int fd2; 894 unlink(zTo); 895 896 fd1 = open(zFrom, O_RDONLY|O_BINARY); 897 if( fd1<0 ){ 898 system_error(pErr, errno); 899 return; 900 } 901 fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0644); 902 if( fd2<0 ){ 903 system_error(pErr, errno); 904 close(fd1); 905 return; 906 } 907 908 iOff = 0; 909 while( iOff<nByte ){ 910 int nCopy = sizeof(aBuf); 911 if( nCopy+iOff>nByte ){ 912 nCopy = nByte - iOff; 913 } 914 if( nCopy!=read(fd1, aBuf, nCopy) ){ 915 system_error(pErr, errno); 916 break; 917 } 918 if( nCopy!=write(fd2, aBuf, nCopy) ){ 919 system_error(pErr, errno); 920 break; 921 } 922 iOff += nCopy; 923 } 924 925 close(fd1); 926 close(fd2); 927 } 928 } 929 } 930 931 /* 932 ** Used by setstoptime() and timetostop(). 933 */ 934 static double timelimit = 0.0; 935 936 static double currentTime(void){ 937 double t; 938 static sqlite3_vfs *pTimelimitVfs = 0; 939 if( pTimelimitVfs==0 ) pTimelimitVfs = sqlite3_vfs_find(0); 940 if( pTimelimitVfs->iVersion>=2 && pTimelimitVfs->xCurrentTimeInt64!=0 ){ 941 sqlite3_int64 tm; 942 pTimelimitVfs->xCurrentTimeInt64(pTimelimitVfs, &tm); 943 t = tm/86400000.0; 944 }else{ 945 pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t); 946 } 947 return t; 948 } 949 950 static void setstoptime_x( 951 Error *pErr, /* IN/OUT: Error code */ 952 int nMs /* Milliseconds until "stop time" */ 953 ){ 954 if( pErr->rc==SQLITE_OK ){ 955 double t = currentTime(); 956 timelimit = t + ((double)nMs)/(1000.0*60.0*60.0*24.0); 957 } 958 } 959 960 static int timetostop_x( 961 Error *pErr /* IN/OUT: Error code */ 962 ){ 963 int ret = 1; 964 if( pErr->rc==SQLITE_OK ){ 965 double t = currentTime(); 966 ret = (t >= timelimit); 967 } 968 return ret; 969 } 970 971 972 /************************************************************************* 973 ************************************************************************** 974 ************************************************************************** 975 ** End infrastructure. Begin tests. 976 */ 977 978 #define WALTHREAD1_NTHREAD 10 979 #define WALTHREAD3_NTHREAD 6 980 981 static char *walthread1_thread(int iTid, void *pArg){ 982 Error err = {0}; /* Error code and message */ 983 Sqlite db = {0}; /* SQLite database connection */ 984 int nIter = 0; /* Iterations so far */ 985 986 opendb(&err, &db, "test.db", 0); 987 while( !timetostop(&err) ){ 988 const char *azSql[] = { 989 "SELECT md5sum(x) FROM t1 WHERE rowid != (SELECT max(rowid) FROM t1)", 990 "SELECT x FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)", 991 }; 992 char *z1, *z2, *z3; 993 994 execsql(&err, &db, "BEGIN"); 995 integrity_check(&err, &db); 996 z1 = execsql_text(&err, &db, 1, azSql[0]); 997 z2 = execsql_text(&err, &db, 2, azSql[1]); 998 z3 = execsql_text(&err, &db, 3, azSql[0]); 999 execsql(&err, &db, "COMMIT"); 1000 1001 if( strcmp(z1, z2) || strcmp(z1, z3) ){ 1002 test_error(&err, "Failed read: %s %s %s", z1, z2, z3); 1003 } 1004 1005 sql_script(&err, &db, 1006 "BEGIN;" 1007 "INSERT INTO t1 VALUES(randomblob(100));" 1008 "INSERT INTO t1 VALUES(randomblob(100));" 1009 "INSERT INTO t1 SELECT md5sum(x) FROM t1;" 1010 "COMMIT;" 1011 ); 1012 nIter++; 1013 } 1014 closedb(&err, &db); 1015 1016 print_and_free_err(&err); 1017 return sqlite3_mprintf("%d iterations", nIter); 1018 } 1019 1020 static char *walthread1_ckpt_thread(int iTid, void *pArg){ 1021 Error err = {0}; /* Error code and message */ 1022 Sqlite db = {0}; /* SQLite database connection */ 1023 int nCkpt = 0; /* Checkpoints so far */ 1024 1025 opendb(&err, &db, "test.db", 0); 1026 while( !timetostop(&err) ){ 1027 sqlite3_sleep(500); 1028 execsql(&err, &db, "PRAGMA wal_checkpoint"); 1029 if( err.rc==SQLITE_OK ) nCkpt++; 1030 clear_error(&err, SQLITE_BUSY); 1031 } 1032 closedb(&err, &db); 1033 1034 print_and_free_err(&err); 1035 return sqlite3_mprintf("%d checkpoints", nCkpt); 1036 } 1037 1038 static void walthread1(int nMs){ 1039 Error err = {0}; /* Error code and message */ 1040 Sqlite db = {0}; /* SQLite database connection */ 1041 Threadset threads = {0}; /* Test threads */ 1042 int i; /* Iterator variable */ 1043 1044 opendb(&err, &db, "test.db", 1); 1045 sql_script(&err, &db, 1046 "PRAGMA journal_mode = WAL;" 1047 "CREATE TABLE t1(x PRIMARY KEY);" 1048 "INSERT INTO t1 VALUES(randomblob(100));" 1049 "INSERT INTO t1 VALUES(randomblob(100));" 1050 "INSERT INTO t1 SELECT md5sum(x) FROM t1;" 1051 ); 1052 closedb(&err, &db); 1053 1054 setstoptime(&err, nMs); 1055 for(i=0; i<WALTHREAD1_NTHREAD; i++){ 1056 launch_thread(&err, &threads, walthread1_thread, 0); 1057 } 1058 launch_thread(&err, &threads, walthread1_ckpt_thread, 0); 1059 join_all_threads(&err, &threads); 1060 1061 print_and_free_err(&err); 1062 } 1063 1064 static char *walthread2_thread(int iTid, void *pArg){ 1065 Error err = {0}; /* Error code and message */ 1066 Sqlite db = {0}; /* SQLite database connection */ 1067 int anTrans[2] = {0, 0}; /* Number of WAL and Rollback transactions */ 1068 int iArg = PTR2INT(pArg); 1069 1070 const char *zJournal = "PRAGMA journal_mode = WAL"; 1071 if( iArg ){ zJournal = "PRAGMA journal_mode = DELETE"; } 1072 1073 while( !timetostop(&err) ){ 1074 int journal_exists = 0; 1075 int wal_exists = 0; 1076 1077 opendb(&err, &db, "test.db", 0); 1078 1079 sql_script(&err, &db, zJournal); 1080 clear_error(&err, SQLITE_BUSY); 1081 sql_script(&err, &db, "BEGIN"); 1082 sql_script(&err, &db, "INSERT INTO t1 VALUES(NULL, randomblob(100))"); 1083 1084 journal_exists = (filesize(&err, "test.db-journal") >= 0); 1085 wal_exists = (filesize(&err, "test.db-wal") >= 0); 1086 if( (journal_exists+wal_exists)!=1 ){ 1087 test_error(&err, "File system looks incorrect (%d, %d)", 1088 journal_exists, wal_exists 1089 ); 1090 } 1091 anTrans[journal_exists]++; 1092 1093 sql_script(&err, &db, "COMMIT"); 1094 integrity_check(&err, &db); 1095 closedb(&err, &db); 1096 } 1097 1098 print_and_free_err(&err); 1099 return sqlite3_mprintf("W %d R %d", anTrans[0], anTrans[1]); 1100 } 1101 1102 static void walthread2(int nMs){ 1103 Error err = {0}; 1104 Sqlite db = {0}; 1105 Threadset threads = {0}; 1106 1107 opendb(&err, &db, "test.db", 1); 1108 sql_script(&err, &db, "CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE)"); 1109 closedb(&err, &db); 1110 1111 setstoptime(&err, nMs); 1112 launch_thread(&err, &threads, walthread2_thread, 0); 1113 launch_thread(&err, &threads, walthread2_thread, 0); 1114 launch_thread(&err, &threads, walthread2_thread, (void*)1); 1115 launch_thread(&err, &threads, walthread2_thread, (void*)1); 1116 join_all_threads(&err, &threads); 1117 1118 print_and_free_err(&err); 1119 } 1120 1121 static char *walthread3_thread(int iTid, void *pArg){ 1122 Error err = {0}; /* Error code and message */ 1123 Sqlite db = {0}; /* SQLite database connection */ 1124 i64 iNextWrite; /* Next value this thread will write */ 1125 int iArg = PTR2INT(pArg); 1126 1127 opendb(&err, &db, "test.db", 0); 1128 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 10"); 1129 1130 iNextWrite = iArg+1; 1131 while( 1 ){ 1132 i64 sum1; 1133 i64 sum2; 1134 int stop = 0; /* True to stop executing (test timed out) */ 1135 1136 while( 0==(stop = timetostop(&err)) ){ 1137 i64 iMax = execsql_i64(&err, &db, "SELECT max(cnt) FROM t1"); 1138 if( iMax+1==iNextWrite ) break; 1139 } 1140 if( stop ) break; 1141 1142 sum1 = execsql_i64(&err, &db, "SELECT sum(cnt) FROM t1"); 1143 sum2 = execsql_i64(&err, &db, "SELECT sum(sum1) FROM t1"); 1144 execsql_i64(&err, &db, 1145 "INSERT INTO t1 VALUES(:iNextWrite, :iSum1, :iSum2)", 1146 &iNextWrite, &sum1, &sum2 1147 ); 1148 integrity_check(&err, &db); 1149 1150 iNextWrite += WALTHREAD3_NTHREAD; 1151 } 1152 1153 closedb(&err, &db); 1154 print_and_free_err(&err); 1155 return 0; 1156 } 1157 1158 static void walthread3(int nMs){ 1159 Error err = {0}; 1160 Sqlite db = {0}; 1161 Threadset threads = {0}; 1162 int i; 1163 1164 opendb(&err, &db, "test.db", 1); 1165 sql_script(&err, &db, 1166 "PRAGMA journal_mode = WAL;" 1167 "CREATE TABLE t1(cnt PRIMARY KEY, sum1, sum2);" 1168 "CREATE INDEX i1 ON t1(sum1);" 1169 "CREATE INDEX i2 ON t1(sum2);" 1170 "INSERT INTO t1 VALUES(0, 0, 0);" 1171 ); 1172 closedb(&err, &db); 1173 1174 setstoptime(&err, nMs); 1175 for(i=0; i<WALTHREAD3_NTHREAD; i++){ 1176 launch_thread(&err, &threads, walthread3_thread, INT2PTR(i)); 1177 } 1178 join_all_threads(&err, &threads); 1179 1180 print_and_free_err(&err); 1181 } 1182 1183 static char *walthread4_reader_thread(int iTid, void *pArg){ 1184 Error err = {0}; /* Error code and message */ 1185 Sqlite db = {0}; /* SQLite database connection */ 1186 1187 opendb(&err, &db, "test.db", 0); 1188 while( !timetostop(&err) ){ 1189 integrity_check(&err, &db); 1190 } 1191 closedb(&err, &db); 1192 1193 print_and_free_err(&err); 1194 return 0; 1195 } 1196 1197 static char *walthread4_writer_thread(int iTid, void *pArg){ 1198 Error err = {0}; /* Error code and message */ 1199 Sqlite db = {0}; /* SQLite database connection */ 1200 i64 iRow = 1; 1201 1202 opendb(&err, &db, "test.db", 0); 1203 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 15;"); 1204 while( !timetostop(&err) ){ 1205 execsql_i64( 1206 &err, &db, "REPLACE INTO t1 VALUES(:iRow, randomblob(300))", &iRow 1207 ); 1208 iRow++; 1209 if( iRow==10 ) iRow = 0; 1210 } 1211 closedb(&err, &db); 1212 1213 print_and_free_err(&err); 1214 return 0; 1215 } 1216 1217 static void walthread4(int nMs){ 1218 Error err = {0}; 1219 Sqlite db = {0}; 1220 Threadset threads = {0}; 1221 1222 opendb(&err, &db, "test.db", 1); 1223 sql_script(&err, &db, 1224 "PRAGMA journal_mode = WAL;" 1225 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);" 1226 ); 1227 closedb(&err, &db); 1228 1229 setstoptime(&err, nMs); 1230 launch_thread(&err, &threads, walthread4_reader_thread, 0); 1231 launch_thread(&err, &threads, walthread4_writer_thread, 0); 1232 join_all_threads(&err, &threads); 1233 1234 print_and_free_err(&err); 1235 } 1236 1237 static char *walthread5_thread(int iTid, void *pArg){ 1238 Error err = {0}; /* Error code and message */ 1239 Sqlite db = {0}; /* SQLite database connection */ 1240 i64 nRow; 1241 1242 opendb(&err, &db, "test.db", 0); 1243 nRow = execsql_i64(&err, &db, "SELECT count(*) FROM t1"); 1244 closedb(&err, &db); 1245 1246 if( nRow!=65536 ) test_error(&err, "Bad row count: %d", (int)nRow); 1247 print_and_free_err(&err); 1248 return 0; 1249 } 1250 static void walthread5(int nMs){ 1251 Error err = {0}; 1252 Sqlite db = {0}; 1253 Threadset threads = {0}; 1254 1255 opendb(&err, &db, "test.db", 1); 1256 sql_script(&err, &db, 1257 "PRAGMA wal_autocheckpoint = 0;" 1258 "PRAGMA page_size = 1024;" 1259 "PRAGMA journal_mode = WAL;" 1260 "CREATE TABLE t1(x);" 1261 "BEGIN;" 1262 "INSERT INTO t1 VALUES(randomblob(900));" 1263 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */" 1264 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */" 1265 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */" 1266 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */" 1267 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */" 1268 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */" 1269 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */" 1270 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */" 1271 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */" 1272 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */" 1273 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */" 1274 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */" 1275 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */" 1276 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */" 1277 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32768 */" 1278 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 65536 */" 1279 "COMMIT;" 1280 ); 1281 filecopy(&err, "test.db", "test_sv.db"); 1282 filecopy(&err, "test.db-wal", "test_sv.db-wal"); 1283 closedb(&err, &db); 1284 1285 filecopy(&err, "test_sv.db", "test.db"); 1286 filecopy(&err, "test_sv.db-wal", "test.db-wal"); 1287 1288 if( err.rc==SQLITE_OK ){ 1289 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal")); 1290 printf(" DB file is %d.\n", (int)filesize(&err,"test.db")); 1291 } 1292 1293 setstoptime(&err, nMs); 1294 launch_thread(&err, &threads, walthread5_thread, 0); 1295 launch_thread(&err, &threads, walthread5_thread, 0); 1296 launch_thread(&err, &threads, walthread5_thread, 0); 1297 launch_thread(&err, &threads, walthread5_thread, 0); 1298 launch_thread(&err, &threads, walthread5_thread, 0); 1299 join_all_threads(&err, &threads); 1300 1301 if( err.rc==SQLITE_OK ){ 1302 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal")); 1303 printf(" DB file is %d.\n", (int)filesize(&err,"test.db")); 1304 } 1305 1306 print_and_free_err(&err); 1307 } 1308 1309 /*------------------------------------------------------------------------ 1310 ** Test case "cgt_pager_1" 1311 */ 1312 #define CALLGRINDTEST1_NROW 10000 1313 static void cgt_pager_1_populate(Error *pErr, Sqlite *pDb){ 1314 const char *zInsert = "INSERT INTO t1 VALUES(:iRow, zeroblob(:iBlob))"; 1315 i64 iRow; 1316 sql_script(pErr, pDb, "BEGIN"); 1317 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){ 1318 i64 iBlob = 600 + (iRow%300); 1319 execsql(pErr, pDb, zInsert, &iRow, &iBlob); 1320 } 1321 sql_script(pErr, pDb, "COMMIT"); 1322 } 1323 static void cgt_pager_1_update(Error *pErr, Sqlite *pDb){ 1324 const char *zUpdate = "UPDATE t1 SET b = zeroblob(:iBlob) WHERE a = :iRow"; 1325 i64 iRow; 1326 sql_script(pErr, pDb, "BEGIN"); 1327 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){ 1328 i64 iBlob = 600 + ((iRow+100)%300); 1329 execsql(pErr, pDb, zUpdate, &iBlob, &iRow); 1330 } 1331 sql_script(pErr, pDb, "COMMIT"); 1332 } 1333 static void cgt_pager_1_read(Error *pErr, Sqlite *pDb){ 1334 i64 iRow; 1335 sql_script(pErr, pDb, "BEGIN"); 1336 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){ 1337 execsql(pErr, pDb, "SELECT * FROM t1 WHERE a = :iRow", &iRow); 1338 } 1339 sql_script(pErr, pDb, "COMMIT"); 1340 } 1341 static void cgt_pager_1(int nMs){ 1342 void (*xSub)(Error *, Sqlite *); 1343 Error err = {0}; 1344 Sqlite db = {0}; 1345 1346 opendb(&err, &db, "test.db", 1); 1347 sql_script(&err, &db, 1348 "PRAGMA cache_size = 2000;" 1349 "PRAGMA page_size = 1024;" 1350 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);" 1351 ); 1352 1353 xSub = cgt_pager_1_populate; xSub(&err, &db); 1354 xSub = cgt_pager_1_update; xSub(&err, &db); 1355 xSub = cgt_pager_1_read; xSub(&err, &db); 1356 1357 closedb(&err, &db); 1358 print_and_free_err(&err); 1359 } 1360 1361 /*------------------------------------------------------------------------ 1362 ** Test case "dynamic_triggers" 1363 ** 1364 ** Two threads executing statements that cause deeply nested triggers 1365 ** to fire. And one thread busily creating and deleting triggers. This 1366 ** is an attempt to find a bug reported to us. 1367 */ 1368 1369 static char *dynamic_triggers_1(int iTid, void *pArg){ 1370 Error err = {0}; /* Error code and message */ 1371 Sqlite db = {0}; /* SQLite database connection */ 1372 int nDrop = 0; 1373 int nCreate = 0; 1374 1375 opendb(&err, &db, "test.db", 0); 1376 while( !timetostop(&err) ){ 1377 int i; 1378 1379 for(i=1; i<9; i++){ 1380 char *zSql = sqlite3_mprintf( 1381 "CREATE TRIGGER itr%d BEFORE INSERT ON t%d BEGIN " 1382 "INSERT INTO t%d VALUES(new.x, new.y);" 1383 "END;", i, i, i+1 1384 ); 1385 execsql(&err, &db, zSql); 1386 sqlite3_free(zSql); 1387 nCreate++; 1388 } 1389 1390 for(i=1; i<9; i++){ 1391 char *zSql = sqlite3_mprintf( 1392 "CREATE TRIGGER dtr%d BEFORE DELETE ON t%d BEGIN " 1393 "DELETE FROM t%d WHERE x = old.x; " 1394 "END;", i, i, i+1 1395 ); 1396 execsql(&err, &db, zSql); 1397 sqlite3_free(zSql); 1398 nCreate++; 1399 } 1400 1401 for(i=1; i<9; i++){ 1402 char *zSql = sqlite3_mprintf("DROP TRIGGER itr%d", i); 1403 execsql(&err, &db, zSql); 1404 sqlite3_free(zSql); 1405 nDrop++; 1406 } 1407 1408 for(i=1; i<9; i++){ 1409 char *zSql = sqlite3_mprintf("DROP TRIGGER dtr%d", i); 1410 execsql(&err, &db, zSql); 1411 sqlite3_free(zSql); 1412 nDrop++; 1413 } 1414 } 1415 closedb(&err, &db); 1416 1417 print_and_free_err(&err); 1418 return sqlite3_mprintf("%d created, %d dropped", nCreate, nDrop); 1419 } 1420 1421 static char *dynamic_triggers_2(int iTid, void *pArg){ 1422 Error err = {0}; /* Error code and message */ 1423 Sqlite db = {0}; /* SQLite database connection */ 1424 i64 iVal = 0; 1425 int nInsert = 0; 1426 int nDelete = 0; 1427 1428 opendb(&err, &db, "test.db", 0); 1429 while( !timetostop(&err) ){ 1430 do { 1431 iVal = (iVal+1)%100; 1432 execsql(&err, &db, "INSERT INTO t1 VALUES(:iX, :iY+1)", &iVal, &iVal); 1433 nInsert++; 1434 } while( iVal ); 1435 1436 do { 1437 iVal = (iVal+1)%100; 1438 execsql(&err, &db, "DELETE FROM t1 WHERE x = :iX", &iVal); 1439 nDelete++; 1440 } while( iVal ); 1441 } 1442 closedb(&err, &db); 1443 1444 print_and_free_err(&err); 1445 return sqlite3_mprintf("%d inserts, %d deletes", nInsert, nDelete); 1446 } 1447 1448 static void dynamic_triggers(int nMs){ 1449 Error err = {0}; 1450 Sqlite db = {0}; 1451 Threadset threads = {0}; 1452 1453 opendb(&err, &db, "test.db", 1); 1454 sql_script(&err, &db, 1455 "PRAGMA page_size = 1024;" 1456 "PRAGMA journal_mode = WAL;" 1457 "CREATE TABLE t1(x, y);" 1458 "CREATE TABLE t2(x, y);" 1459 "CREATE TABLE t3(x, y);" 1460 "CREATE TABLE t4(x, y);" 1461 "CREATE TABLE t5(x, y);" 1462 "CREATE TABLE t6(x, y);" 1463 "CREATE TABLE t7(x, y);" 1464 "CREATE TABLE t8(x, y);" 1465 "CREATE TABLE t9(x, y);" 1466 ); 1467 closedb(&err, &db); 1468 1469 setstoptime(&err, nMs); 1470 1471 sqlite3_enable_shared_cache(1); 1472 launch_thread(&err, &threads, dynamic_triggers_2, 0); 1473 launch_thread(&err, &threads, dynamic_triggers_2, 0); 1474 1475 sqlite3_sleep(2*1000); 1476 sqlite3_enable_shared_cache(0); 1477 1478 launch_thread(&err, &threads, dynamic_triggers_2, 0); 1479 launch_thread(&err, &threads, dynamic_triggers_1, 0); 1480 1481 join_all_threads(&err, &threads); 1482 1483 print_and_free_err(&err); 1484 } 1485 1486 1487 1488 #include "tt3_checkpoint.c" 1489 #include "tt3_index.c" 1490 #include "tt3_lookaside1.c" 1491 #include "tt3_vacuum.c" 1492 #include "tt3_stress.c" 1493 #include "tt3_shared.c" 1494 1495 int main(int argc, char **argv){ 1496 struct ThreadTest { 1497 void (*xTest)(int); /* Routine for running this test */ 1498 const char *zTest; /* Name of this test */ 1499 int nMs; /* How long to run this test, in milliseconds */ 1500 } aTest[] = { 1501 { walthread1, "walthread1", 20000 }, 1502 { walthread2, "walthread2", 20000 }, 1503 { walthread3, "walthread3", 20000 }, 1504 { walthread4, "walthread4", 20000 }, 1505 { walthread5, "walthread5", 1000 }, 1506 1507 { cgt_pager_1, "cgt_pager_1", 0 }, 1508 { dynamic_triggers, "dynamic_triggers", 20000 }, 1509 1510 { checkpoint_starvation_1, "checkpoint_starvation_1", 10000 }, 1511 { checkpoint_starvation_2, "checkpoint_starvation_2", 10000 }, 1512 1513 { create_drop_index_1, "create_drop_index_1", 10000 }, 1514 { lookaside1, "lookaside1", 10000 }, 1515 { vacuum1, "vacuum1", 10000 }, 1516 { stress1, "stress1", 10000 }, 1517 { stress2, "stress2", 60000 }, 1518 { shared1, "shared1", 10000 }, 1519 }; 1520 static char *substArgv[] = { 0, "*", 0 }; 1521 int i, iArg; 1522 int nTestfound = 0; 1523 1524 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 1525 if( argc<2 ){ 1526 argc = 2; 1527 argv = substArgv; 1528 } 1529 1530 /* Loop through the command-line arguments to ensure that each argument 1531 ** selects at least one test. If not, assume there is a typo on the 1532 ** command-line and bail out with the usage message. */ 1533 for(iArg=1; iArg<argc; iArg++){ 1534 const char *zArg = argv[iArg]; 1535 if( zArg[0]=='-' ){ 1536 if( sqlite3_stricmp(zArg, "-multiplexor")==0 ){ 1537 /* Install the multiplexor VFS as the default */ 1538 int rc = sqlite3_multiplex_initialize(0, 1); 1539 if( rc!=SQLITE_OK ){ 1540 fprintf(stderr, "Failed to install multiplexor VFS (%d)\n", rc); 1541 return 253; 1542 } 1543 } 1544 else { 1545 goto usage; 1546 } 1547 1548 continue; 1549 } 1550 1551 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){ 1552 if( sqlite3_strglob(zArg, aTest[i].zTest)==0 ) break; 1553 } 1554 if( i>=sizeof(aTest)/sizeof(aTest[0]) ) goto usage; 1555 } 1556 1557 for(iArg=1; iArg<argc; iArg++){ 1558 if( argv[iArg][0]=='-' ) continue; 1559 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){ 1560 char const *z = aTest[i].zTest; 1561 if( sqlite3_strglob(argv[iArg],z)==0 ){ 1562 printf("Running %s for %d seconds...\n", z, aTest[i].nMs/1000); 1563 fflush(stdout); 1564 aTest[i].xTest(aTest[i].nMs); 1565 nTestfound++; 1566 } 1567 } 1568 } 1569 if( nTestfound==0 ) goto usage; 1570 1571 printf("%d errors out of %d tests\n", nGlobalErr, nTestfound); 1572 return (nGlobalErr>0 ? 255 : 0); 1573 1574 usage: 1575 printf("Usage: %s [-multiplexor] [testname|testprefix*]...\n", argv[0]); 1576 printf("Available tests are:\n"); 1577 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){ 1578 printf(" %s\n", aTest[i].zTest); 1579 } 1580 1581 return 254; 1582 } 1583