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