4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file implements a simple standalone program used to stress the
13 ** SQLite library when accessing the same set of databases simultaneously
14 ** from multiple threads in shared-cache mode.
16 ** This test program runs on unix-like systems only. It uses pthreads.
19 ** gcc -g -Wall -I. threadtest4.c sqlite3.c -ldl -lpthread
25 ** The argument is the number of threads. There are also options, such
26 ** as -wal and -multithread and -serialized.
28 ** Consider also compiling with clang instead of gcc and adding the
29 ** -fsanitize=thread option.
41 ** An instance of the following structure is passed into each worker
44 typedef struct WorkerInfo WorkerInfo
;
46 int tid
; /* Thread ID */
47 int nWorker
; /* Total number of workers */
48 unsigned wkrFlags
; /* Flags */
49 sqlite3
*mainDb
; /* Database connection of the main thread */
50 sqlite3
*db
; /* Database connection of this thread */
51 int nErr
; /* Number of errors seen by this thread */
52 int nTest
; /* Number of tests run by this thread */
53 char *zMsg
; /* Message returned by this thread */
54 pthread_t id
; /* Thread id */
55 pthread_mutex_t
*pWrMutex
; /* Hold this mutex while writing */
59 ** Allowed values for WorkerInfo.wkrFlags
61 #define TT4_SERIALIZED 0x0000001 /* The --serialized option is used */
62 #define TT4_WAL 0x0000002 /* WAL mode in use */
63 #define TT4_TRACE 0x0000004 /* Trace activity */
67 ** Report an OOM error and die if the argument is NULL
69 static void check_oom(void *x
){
71 fprintf(stderr
, "out of memory\n");
77 ** Allocate memory. If the allocation fails, print an error message and
80 static void *safe_malloc(int sz
){
81 void *x
= sqlite3_malloc(sz
>0?sz
:1);
87 ** Print a trace message for a worker
89 static void worker_trace(WorkerInfo
*p
, const char *zFormat
, ...){
92 if( (p
->wkrFlags
& TT4_TRACE
)==0 ) return;
93 va_start(ap
, zFormat
);
94 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
97 fprintf(stderr
, "TRACE(%02d): %s\n", p
->tid
, zMsg
);
102 ** Prepare a single SQL query
104 static sqlite3_stmt
*prep_sql(sqlite3
*db
, const char *zFormat
, ...){
108 sqlite3_stmt
*pStmt
= 0;
110 va_start(ap
, zFormat
);
111 zSql
= sqlite3_vmprintf(zFormat
, ap
);
114 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
116 fprintf(stderr
, "SQL error (%d,%d): %s\nWhile preparing: [%s]\n",
117 rc
, sqlite3_extended_errcode(db
), sqlite3_errmsg(db
), zSql
);
125 ** Run a SQL statements. Panic if unable.
127 static void run_sql(WorkerInfo
*p
, const char *zFormat
, ...){
131 sqlite3_stmt
*pStmt
= 0;
134 va_start(ap
, zFormat
);
135 zSql
= sqlite3_vmprintf(zFormat
, ap
);
138 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
140 fprintf(stderr
, "SQL error (%d,%d): %s\nWhile preparing: [%s]\n",
141 rc
, sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
), zSql
);
144 worker_trace(p
, "running [%s]", zSql
);
145 while( (rc
= sqlite3_step(pStmt
))!=SQLITE_DONE
){
146 if( (rc
&0xff)==SQLITE_BUSY
|| (rc
&0xff)==SQLITE_LOCKED
){
147 sqlite3_reset(pStmt
);
150 worker_trace(p
, "retry %d for [%s]", nRetry
, zSql
);
154 fprintf(stderr
, "Deadlock in thread %d while running [%s]\n",
159 if( rc
!=SQLITE_ROW
){
160 fprintf(stderr
, "SQL error (%d,%d): %s\nWhile running [%s]\n",
161 rc
, sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
), zSql
);
166 sqlite3_finalize(pStmt
);
171 ** Open the database connection for WorkerInfo. The order in which
172 ** the files are opened is a function of the tid value.
174 static void worker_open_connection(WorkerInfo
*p
, int iCnt
){
178 static const unsigned char aOrder
[6][3] = {
186 x
= (p
->tid
+ iCnt
) % 6;
187 zFile
= sqlite3_mprintf("tt4-test%d.db", aOrder
[x
][0]);
189 worker_trace(p
, "open %s", zFile
);
190 rc
= sqlite3_open_v2(zFile
, &p
->db
,
191 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_SHAREDCACHE
, 0);
193 fprintf(stderr
, "sqlite_open_v2(%s) failed on thread %d\n",
198 run_sql(p
, "PRAGMA read_uncommitted=ON;");
199 sqlite3_busy_timeout(p
->db
, 10000);
200 run_sql(p
, "PRAGMA synchronous=OFF;");
201 run_sql(p
, "ATTACH 'tt4-test%d.db' AS aux1", aOrder
[x
][1]);
202 run_sql(p
, "ATTACH 'tt4-test%d.db' AS aux2", aOrder
[x
][2]);
206 ** Close the worker database connection
208 static void worker_close_connection(WorkerInfo
*p
){
210 worker_trace(p
, "close");
211 sqlite3_close(p
->db
);
217 ** Delete all content in the three databases associated with a
218 ** single thread. Make this happen all in a single transaction if
219 ** inTrans is true, or separately for each database if inTrans is
222 static void worker_delete_all_content(WorkerInfo
*p
, int inTrans
){
224 pthread_mutex_lock(p
->pWrMutex
);
226 run_sql(p
, "DELETE FROM t1 WHERE tid=%d", p
->tid
);
227 run_sql(p
, "DELETE FROM t2 WHERE tid=%d", p
->tid
);
228 run_sql(p
, "DELETE FROM t3 WHERE tid=%d", p
->tid
);
229 run_sql(p
, "COMMIT");
230 pthread_mutex_unlock(p
->pWrMutex
);
233 pthread_mutex_lock(p
->pWrMutex
);
234 run_sql(p
, "DELETE FROM t1 WHERE tid=%d", p
->tid
);
235 pthread_mutex_unlock(p
->pWrMutex
);
237 pthread_mutex_lock(p
->pWrMutex
);
238 run_sql(p
, "DELETE FROM t2 WHERE tid=%d", p
->tid
);
239 pthread_mutex_unlock(p
->pWrMutex
);
241 pthread_mutex_lock(p
->pWrMutex
);
242 run_sql(p
, "DELETE FROM t3 WHERE tid=%d", p
->tid
);
243 pthread_mutex_unlock(p
->pWrMutex
);
249 ** Create rows mn through mx in table iTab for the given worker
251 static void worker_add_content(WorkerInfo
*p
, int mn
, int mx
, int iTab
){
254 case 1: zTabDef
= "t1(tid,sp,a,b,c)"; break;
255 case 2: zTabDef
= "t2(tid,sp,d,e,f)"; break;
256 case 3: zTabDef
= "t3(tid,sp,x,y,z)"; break;
258 pthread_mutex_lock(p
->pWrMutex
);
261 " c(i) AS (VALUES(%d) UNION ALL SELECT i+1 FROM c WHERE i<%d)\n"
262 "INSERT INTO %s SELECT %d, zeroblob(3000), i, printf('%%d',i), i FROM c;",
263 mn
, mx
, zTabDef
, p
->tid
265 pthread_mutex_unlock(p
->pWrMutex
);
270 ** Set an error message on a worker
272 static void worker_error(WorkerInfo
*p
, const char *zFormat
, ...){
275 sqlite3_free(p
->zMsg
);
276 va_start(ap
, zFormat
);
277 p
->zMsg
= sqlite3_vmprintf(zFormat
, ap
);
282 ** Each thread runs the following function.
284 static void *worker_thread(void *pArg
){
285 WorkerInfo
*p
= (WorkerInfo
*)pArg
;
291 printf("worker %d startup\n", p
->tid
); fflush(stdout
);
292 for(iOuter
=1; iOuter
<=p
->nWorker
; iOuter
++){
293 worker_open_connection(p
, iOuter
);
295 worker_add_content(p
, i
*100+1, (i
+1)*100, (p
->tid
+iOuter
)%3 + 1);
296 worker_add_content(p
, i
*100+1, (i
+1)*100, (p
->tid
+iOuter
+1)%3 + 1);
297 worker_add_content(p
, i
*100+1, (i
+1)*100, (p
->tid
+iOuter
+2)%3 + 1);
300 pStmt
= prep_sql(p
->db
, "SELECT count(a) FROM t1 WHERE tid=%d", p
->tid
);
301 worker_trace(p
, "query [%s]", sqlite3_sql(pStmt
));
302 rc
= sqlite3_step(pStmt
);
303 if( rc
!=SQLITE_ROW
){
304 worker_error(p
, "Failed to step: %s", sqlite3_sql(pStmt
));
305 }else if( sqlite3_column_int(pStmt
, 0)!=400 ){
306 worker_error(p
, "Wrong result: %d", sqlite3_column_int(pStmt
,0));
308 sqlite3_finalize(pStmt
);
311 if( ((iOuter
+p
->tid
)%3)==0 ){
312 sqlite3_db_release_memory(p
->db
);
316 pthread_mutex_lock(p
->pWrMutex
);
317 run_sql(p
, "BEGIN;");
318 run_sql(p
, "UPDATE t1 SET c=NULL WHERE a=55");
319 run_sql(p
, "UPDATE t2 SET f=NULL WHERE d=42");
320 run_sql(p
, "UPDATE t3 SET z=NULL WHERE x=31");
321 run_sql(p
, "ROLLBACK;");
323 pthread_mutex_unlock(p
->pWrMutex
);
326 if( iOuter
==p
->tid
){
327 pthread_mutex_lock(p
->pWrMutex
);
328 run_sql(p
, "VACUUM");
329 pthread_mutex_unlock(p
->pWrMutex
);
332 pStmt
= prep_sql(p
->db
,
333 "SELECT t1.rowid, t2.rowid, t3.rowid"
335 " WHERE t1.tid=%d AND t2.tid=%d AND t3.tid=%d"
336 " AND t1.a<>t2.d AND t2.d<>t3.x"
338 ,p
->tid
, p
->tid
, p
->tid
);
339 worker_trace(p
, "query [%s]", sqlite3_sql(pStmt
));
340 for(i
=0; i
<p
->nWorker
; i
++){
341 rc
= sqlite3_step(pStmt
);
342 if( rc
!=SQLITE_ROW
){
343 worker_error(p
, "Failed to step: %s", sqlite3_sql(pStmt
));
348 sqlite3_finalize(pStmt
);
351 worker_delete_all_content(p
, (p
->tid
+iOuter
)%2);
352 worker_close_connection(p
);
355 worker_close_connection(p
);
356 printf("worker %d finished\n", p
->tid
); fflush(stdout
);
360 int main(int argc
, char **argv
){
361 int nWorker
= 0; /* Number of worker threads */
362 int i
; /* Loop counter */
363 WorkerInfo
*aInfo
; /* Information for each worker */
364 unsigned wkrFlags
= 0; /* Default worker flags */
365 int nErr
= 0; /* Number of errors */
366 int nTest
= 0; /* Number of tests */
367 int rc
; /* Return code */
368 sqlite3
*db
= 0; /* Main database connection */
369 pthread_mutex_t wrMutex
; /* The write serialization mutex */
370 WorkerInfo infoTop
; /* WorkerInfo for the main thread */
371 WorkerInfo
*p
; /* Pointer to infoTop */
373 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
374 for(i
=1; i
<argc
; i
++){
375 const char *z
= argv
[i
];
377 if( z
[1]=='-' && z
[2]!=0 ) z
++;
378 if( strcmp(z
,"-multithread")==0 ){
379 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
380 wkrFlags
&= ~TT4_SERIALIZED
;
381 }else if( strcmp(z
,"-serialized")==0 ){
382 sqlite3_config(SQLITE_CONFIG_SERIALIZED
);
383 wkrFlags
|= TT4_SERIALIZED
;
384 }else if( strcmp(z
,"-wal")==0 ){
386 }else if( strcmp(z
,"-trace")==0 ){
387 wkrFlags
|= TT4_TRACE
;
389 fprintf(stderr
, "unknown command-line option: %s\n", argv
[i
]);
392 }else if( z
[0]>='1' && z
[0]<='9' && nWorker
==0 ){
395 fprintf(stderr
, "minimum of 2 threads\n");
399 fprintf(stderr
, "extra command-line argument: \"%s\"\n", argv
[i
]);
405 "usage: %s ?OPTIONS? N\n"
406 "N is the number of threads and must be at least 2.\n"
416 if( !sqlite3_threadsafe() ){
417 fprintf(stderr
, "requires a threadsafe build of SQLite\n");
420 sqlite3_initialize();
421 sqlite3_enable_shared_cache(1);
422 pthread_mutex_init(&wrMutex
, 0);
424 /* Initialize the test database files */
425 (void)unlink("tt4-test1.db");
426 (void)unlink("tt4-test2.db");
427 (void)unlink("tt4-test3.db");
428 rc
= sqlite3_open("tt4-test1.db", &db
);
430 fprintf(stderr
, "Unable to open test database: tt4-test2.db\n");
433 memset(&infoTop
, 0, sizeof(infoTop
));
435 infoTop
.wkrFlags
= wkrFlags
;
437 if( wkrFlags
& TT4_WAL
){
438 run_sql(p
, "PRAGMA journal_mode=WAL");
440 run_sql(p
, "PRAGMA synchronous=OFF");
441 run_sql(p
, "CREATE TABLE IF NOT EXISTS t1(tid INTEGER, sp, a, b, c)");
442 run_sql(p
, "CREATE INDEX t1tid ON t1(tid)");
443 run_sql(p
, "CREATE INDEX t1ab ON t1(a,b)");
444 run_sql(p
, "ATTACH 'tt4-test2.db' AS 'test2'");
445 run_sql(p
, "CREATE TABLE IF NOT EXISTS test2.t2(tid INTEGER, sp, d, e, f)");
446 run_sql(p
, "CREATE INDEX test2.t2tid ON t2(tid)");
447 run_sql(p
, "CREATE INDEX test2.t2de ON t2(d,e)");
448 run_sql(p
, "ATTACH 'tt4-test3.db' AS 'test3'");
449 run_sql(p
, "CREATE TABLE IF NOT EXISTS test3.t3(tid INTEGER, sp, x, y, z)");
450 run_sql(p
, "CREATE INDEX test3.t3tid ON t3(tid)");
451 run_sql(p
, "CREATE INDEX test3.t3xy ON t3(x,y)");
452 aInfo
= safe_malloc( sizeof(*aInfo
)*nWorker
);
453 memset(aInfo
, 0, sizeof(*aInfo
)*nWorker
);
454 for(i
=0; i
<nWorker
; i
++){
456 aInfo
[i
].nWorker
= nWorker
;
457 aInfo
[i
].wkrFlags
= wkrFlags
;
458 aInfo
[i
].mainDb
= db
;
459 aInfo
[i
].pWrMutex
= &wrMutex
;
460 rc
= pthread_create(&aInfo
[i
].id
, 0, worker_thread
, &aInfo
[i
]);
462 fprintf(stderr
, "thread creation failed for thread %d\n", i
+1);
467 for(i
=0; i
<nWorker
; i
++){
468 pthread_join(aInfo
[i
].id
, 0);
469 printf("Joined thread %d: %d errors in %d tests",
470 aInfo
[i
].tid
, aInfo
[i
].nErr
, aInfo
[i
].nTest
);
472 printf(": %s\n", aInfo
[i
].zMsg
);
476 nErr
+= aInfo
[i
].nErr
;
477 nTest
+= aInfo
[i
].nTest
;
482 printf("Total %d errors in %d tests\n", nErr
, nTest
);