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