threadtest3.c 41 KB

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