17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate ** 2002 January 15
37c478bd9Sstevel@tonic-gate **
47c478bd9Sstevel@tonic-gate ** The author disclaims copyright to this source code.  In place of
57c478bd9Sstevel@tonic-gate ** a legal notice, here is a blessing:
67c478bd9Sstevel@tonic-gate **
77c478bd9Sstevel@tonic-gate **    May you do good and not evil.
87c478bd9Sstevel@tonic-gate **    May you find forgiveness for yourself and forgive others.
97c478bd9Sstevel@tonic-gate **    May you share freely, never taking more than you give.
107c478bd9Sstevel@tonic-gate **
117c478bd9Sstevel@tonic-gate *************************************************************************
127c478bd9Sstevel@tonic-gate ** This file implements a simple standalone program used to test whether
137c478bd9Sstevel@tonic-gate ** or not the SQLite library is threadsafe.
147c478bd9Sstevel@tonic-gate **
157c478bd9Sstevel@tonic-gate ** Testing the thread safety of SQLite is difficult because there are very
167c478bd9Sstevel@tonic-gate ** few places in the code that are even potentially unsafe, and those
177c478bd9Sstevel@tonic-gate ** places execute for very short periods of time.  So even if the library
187c478bd9Sstevel@tonic-gate ** is compiled with its mutexes disabled, it is likely to work correctly
19*1da57d55SToomas Soome ** in a multi-threaded program most of the time.
207c478bd9Sstevel@tonic-gate **
217c478bd9Sstevel@tonic-gate ** This file is NOT part of the standard SQLite library.  It is used for
227c478bd9Sstevel@tonic-gate ** testing only.
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate #include "sqlite.h"
257c478bd9Sstevel@tonic-gate #include <pthread.h>
267c478bd9Sstevel@tonic-gate #include <sched.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate ** Enable for tracing
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate static int verbose = 0;
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate ** Come here to die.
397c478bd9Sstevel@tonic-gate */
Exit(int rc)407c478bd9Sstevel@tonic-gate static void Exit(int rc){
417c478bd9Sstevel@tonic-gate   exit(rc);
427c478bd9Sstevel@tonic-gate }
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate extern char *sqlite_mprintf(const char *zFormat, ...);
457c478bd9Sstevel@tonic-gate extern char *sqlite_vmprintf(const char *zFormat, va_list);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate ** When a lock occurs, yield.
497c478bd9Sstevel@tonic-gate */
db_is_locked(void * NotUsed,const char * zNotUsed,int iNotUsed)507c478bd9Sstevel@tonic-gate static int db_is_locked(void *NotUsed, const char *zNotUsed, int iNotUsed){
517c478bd9Sstevel@tonic-gate   /* sched_yield(); */
527c478bd9Sstevel@tonic-gate   if( verbose ) printf("BUSY %s\n", (char*)NotUsed);
537c478bd9Sstevel@tonic-gate   usleep(100);
547c478bd9Sstevel@tonic-gate   return 1;
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate ** Used to accumulate query results by db_query()
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate struct QueryResult {
617c478bd9Sstevel@tonic-gate   const char *zFile;  /* Filename - used for error reporting */
627c478bd9Sstevel@tonic-gate   int nElem;          /* Number of used entries in azElem[] */
637c478bd9Sstevel@tonic-gate   int nAlloc;         /* Number of slots allocated for azElem[] */
647c478bd9Sstevel@tonic-gate   char **azElem;      /* The result of the query */
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate ** The callback function for db_query
697c478bd9Sstevel@tonic-gate */
db_query_callback(void * pUser,int nArg,char ** azArg,char ** NotUsed)707c478bd9Sstevel@tonic-gate static int db_query_callback(
717c478bd9Sstevel@tonic-gate   void *pUser,     /* Pointer to the QueryResult structure */
727c478bd9Sstevel@tonic-gate   int nArg,        /* Number of columns in this result row */
737c478bd9Sstevel@tonic-gate   char **azArg,    /* Text of data in all columns */
747c478bd9Sstevel@tonic-gate   char **NotUsed   /* Names of the columns */
757c478bd9Sstevel@tonic-gate ){
767c478bd9Sstevel@tonic-gate   struct QueryResult *pResult = (struct QueryResult*)pUser;
777c478bd9Sstevel@tonic-gate   int i;
787c478bd9Sstevel@tonic-gate   if( pResult->nElem + nArg >= pResult->nAlloc ){
797c478bd9Sstevel@tonic-gate     if( pResult->nAlloc==0 ){
807c478bd9Sstevel@tonic-gate       pResult->nAlloc = nArg+1;
817c478bd9Sstevel@tonic-gate     }else{
827c478bd9Sstevel@tonic-gate       pResult->nAlloc = pResult->nAlloc*2 + nArg + 1;
837c478bd9Sstevel@tonic-gate     }
847c478bd9Sstevel@tonic-gate     pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
857c478bd9Sstevel@tonic-gate     if( pResult->azElem==0 ){
867c478bd9Sstevel@tonic-gate       fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
877c478bd9Sstevel@tonic-gate       return 1;
887c478bd9Sstevel@tonic-gate     }
897c478bd9Sstevel@tonic-gate   }
907c478bd9Sstevel@tonic-gate   if( azArg==0 ) return 0;
917c478bd9Sstevel@tonic-gate   for(i=0; i<nArg; i++){
927c478bd9Sstevel@tonic-gate     pResult->azElem[pResult->nElem++] =
93*1da57d55SToomas Soome         sqlite_mprintf("%s",azArg[i] ? azArg[i] : "");
947c478bd9Sstevel@tonic-gate   }
957c478bd9Sstevel@tonic-gate   return 0;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate ** Execute a query against the database.  NULL values are returned
1007c478bd9Sstevel@tonic-gate ** as an empty string.  The list is terminated by a single NULL pointer.
1017c478bd9Sstevel@tonic-gate */
db_query(sqlite * db,const char * zFile,const char * zFormat,...)1027c478bd9Sstevel@tonic-gate char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
1037c478bd9Sstevel@tonic-gate   char *zSql;
1047c478bd9Sstevel@tonic-gate   int rc;
1057c478bd9Sstevel@tonic-gate   char *zErrMsg = 0;
1067c478bd9Sstevel@tonic-gate   va_list ap;
1077c478bd9Sstevel@tonic-gate   struct QueryResult sResult;
1087c478bd9Sstevel@tonic-gate   va_start(ap, zFormat);
1097c478bd9Sstevel@tonic-gate   zSql = sqlite_vmprintf(zFormat, ap);
1107c478bd9Sstevel@tonic-gate   va_end(ap);
1117c478bd9Sstevel@tonic-gate   memset(&sResult, 0, sizeof(sResult));
1127c478bd9Sstevel@tonic-gate   sResult.zFile = zFile;
1137c478bd9Sstevel@tonic-gate   if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
1147c478bd9Sstevel@tonic-gate   rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
1157c478bd9Sstevel@tonic-gate   if( rc==SQLITE_SCHEMA ){
1167c478bd9Sstevel@tonic-gate     if( zErrMsg ) free(zErrMsg);
1177c478bd9Sstevel@tonic-gate     rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
1187c478bd9Sstevel@tonic-gate   }
1197c478bd9Sstevel@tonic-gate   if( verbose ) printf("DONE %s %s\n", zFile, zSql);
1207c478bd9Sstevel@tonic-gate   if( zErrMsg ){
1217c478bd9Sstevel@tonic-gate     fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
1227c478bd9Sstevel@tonic-gate     free(zErrMsg);
1237c478bd9Sstevel@tonic-gate     free(zSql);
1247c478bd9Sstevel@tonic-gate     Exit(1);
1257c478bd9Sstevel@tonic-gate   }
1267c478bd9Sstevel@tonic-gate   sqlite_freemem(zSql);
1277c478bd9Sstevel@tonic-gate   if( sResult.azElem==0 ){
1287c478bd9Sstevel@tonic-gate     db_query_callback(&sResult, 0, 0, 0);
1297c478bd9Sstevel@tonic-gate   }
1307c478bd9Sstevel@tonic-gate   sResult.azElem[sResult.nElem] = 0;
1317c478bd9Sstevel@tonic-gate   return sResult.azElem;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate ** Execute an SQL statement.
1367c478bd9Sstevel@tonic-gate */
db_execute(sqlite * db,const char * zFile,const char * zFormat,...)1377c478bd9Sstevel@tonic-gate void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
1387c478bd9Sstevel@tonic-gate   char *zSql;
1397c478bd9Sstevel@tonic-gate   int rc;
1407c478bd9Sstevel@tonic-gate   char *zErrMsg = 0;
1417c478bd9Sstevel@tonic-gate   va_list ap;
1427c478bd9Sstevel@tonic-gate   va_start(ap, zFormat);
1437c478bd9Sstevel@tonic-gate   zSql = sqlite_vmprintf(zFormat, ap);
1447c478bd9Sstevel@tonic-gate   va_end(ap);
1457c478bd9Sstevel@tonic-gate   if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
1467c478bd9Sstevel@tonic-gate   rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
1477c478bd9Sstevel@tonic-gate   while( rc==SQLITE_SCHEMA ){
1487c478bd9Sstevel@tonic-gate     if( zErrMsg ) free(zErrMsg);
1497c478bd9Sstevel@tonic-gate     rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
1507c478bd9Sstevel@tonic-gate   }
1517c478bd9Sstevel@tonic-gate   if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
1527c478bd9Sstevel@tonic-gate   if( zErrMsg ){
1537c478bd9Sstevel@tonic-gate     fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
1547c478bd9Sstevel@tonic-gate     free(zErrMsg);
1557c478bd9Sstevel@tonic-gate     sqlite_freemem(zSql);
1567c478bd9Sstevel@tonic-gate     Exit(1);
1577c478bd9Sstevel@tonic-gate   }
1587c478bd9Sstevel@tonic-gate   sqlite_freemem(zSql);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate ** Free the results of a db_query() call.
1637c478bd9Sstevel@tonic-gate */
db_query_free(char ** az)1647c478bd9Sstevel@tonic-gate void db_query_free(char **az){
1657c478bd9Sstevel@tonic-gate   int i;
1667c478bd9Sstevel@tonic-gate   for(i=0; az[i]; i++){
1677c478bd9Sstevel@tonic-gate     sqlite_freemem(az[i]);
1687c478bd9Sstevel@tonic-gate   }
1697c478bd9Sstevel@tonic-gate   free(az);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate ** Check results
1747c478bd9Sstevel@tonic-gate */
db_check(const char * zFile,const char * zMsg,char ** az,...)1757c478bd9Sstevel@tonic-gate void db_check(const char *zFile, const char *zMsg, char **az, ...){
1767c478bd9Sstevel@tonic-gate   va_list ap;
1777c478bd9Sstevel@tonic-gate   int i;
1787c478bd9Sstevel@tonic-gate   char *z;
1797c478bd9Sstevel@tonic-gate   va_start(ap, az);
1807c478bd9Sstevel@tonic-gate   for(i=0; (z = va_arg(ap, char*))!=0; i++){
1817c478bd9Sstevel@tonic-gate     if( az[i]==0 || strcmp(az[i],z)!=0 ){
1827c478bd9Sstevel@tonic-gate       fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
1837c478bd9Sstevel@tonic-gate         zFile, zMsg, i+1, az[i]);
1847c478bd9Sstevel@tonic-gate       db_query_free(az);
1857c478bd9Sstevel@tonic-gate       Exit(1);
1867c478bd9Sstevel@tonic-gate     }
1877c478bd9Sstevel@tonic-gate   }
1887c478bd9Sstevel@tonic-gate   va_end(ap);
1897c478bd9Sstevel@tonic-gate   db_query_free(az);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
1937c478bd9Sstevel@tonic-gate pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
1947c478bd9Sstevel@tonic-gate int thread_cnt = 0;
1957c478bd9Sstevel@tonic-gate 
worker_bee(void * pArg)1967c478bd9Sstevel@tonic-gate static void *worker_bee(void *pArg){
1977c478bd9Sstevel@tonic-gate   const char *zFilename = (char*)pArg;
1987c478bd9Sstevel@tonic-gate   char *azErr;
1997c478bd9Sstevel@tonic-gate   int i, cnt;
2007c478bd9Sstevel@tonic-gate   int t = atoi(zFilename);
2017c478bd9Sstevel@tonic-gate   char **az;
2027c478bd9Sstevel@tonic-gate   sqlite *db;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate   pthread_mutex_lock(&lock);
2057c478bd9Sstevel@tonic-gate   thread_cnt++;
2067c478bd9Sstevel@tonic-gate   pthread_mutex_unlock(&lock);
2077c478bd9Sstevel@tonic-gate   printf("%s: START\n", zFilename);
2087c478bd9Sstevel@tonic-gate   fflush(stdout);
2097c478bd9Sstevel@tonic-gate   for(cnt=0; cnt<10; cnt++){
2107c478bd9Sstevel@tonic-gate     db = sqlite_open(&zFilename[2], 0, &azErr);
2117c478bd9Sstevel@tonic-gate     if( db==0 ){
2127c478bd9Sstevel@tonic-gate       fprintf(stdout,"%s: can't open\n", zFilename);
2137c478bd9Sstevel@tonic-gate       Exit(1);
2147c478bd9Sstevel@tonic-gate     }
2157c478bd9Sstevel@tonic-gate     sqlite_busy_handler(db, db_is_locked, zFilename);
2167c478bd9Sstevel@tonic-gate     db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
2177c478bd9Sstevel@tonic-gate     for(i=1; i<=100; i++){
2187c478bd9Sstevel@tonic-gate       db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
2197c478bd9Sstevel@tonic-gate          t, i, i*2, i*i);
2207c478bd9Sstevel@tonic-gate     }
2217c478bd9Sstevel@tonic-gate     az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
222*1da57d55SToomas Soome     db_check(zFilename, "tX size", az, "100", 0);
2237c478bd9Sstevel@tonic-gate     az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
224*1da57d55SToomas Soome     db_check(zFilename, "tX avg", az, "101", 0);
2257c478bd9Sstevel@tonic-gate     db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
2267c478bd9Sstevel@tonic-gate     az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
2277c478bd9Sstevel@tonic-gate     db_check(zFilename, "tX avg2", az, "51", 0);
2287c478bd9Sstevel@tonic-gate     for(i=1; i<=50; i++){
2297c478bd9Sstevel@tonic-gate       char z1[30], z2[30];
2307c478bd9Sstevel@tonic-gate       az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
2317c478bd9Sstevel@tonic-gate       sprintf(z1, "%d", i*2);
2327c478bd9Sstevel@tonic-gate       sprintf(z2, "%d", i*i);
2337c478bd9Sstevel@tonic-gate       db_check(zFilename, "readback", az, z1, z2, 0);
2347c478bd9Sstevel@tonic-gate     }
2357c478bd9Sstevel@tonic-gate     db_execute(db, zFilename, "DROP TABLE t%d;", t);
2367c478bd9Sstevel@tonic-gate     sqlite_close(db);
2377c478bd9Sstevel@tonic-gate   }
2387c478bd9Sstevel@tonic-gate   printf("%s: END\n", zFilename);
2397c478bd9Sstevel@tonic-gate   /* unlink(zFilename); */
2407c478bd9Sstevel@tonic-gate   fflush(stdout);
2417c478bd9Sstevel@tonic-gate   pthread_mutex_lock(&lock);
2427c478bd9Sstevel@tonic-gate   thread_cnt--;
2437c478bd9Sstevel@tonic-gate   if( thread_cnt<=0 ){
2447c478bd9Sstevel@tonic-gate     pthread_cond_signal(&sig);
2457c478bd9Sstevel@tonic-gate   }
2467c478bd9Sstevel@tonic-gate   pthread_mutex_unlock(&lock);
2477c478bd9Sstevel@tonic-gate   return 0;
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
main(int argc,char ** argv)2507c478bd9Sstevel@tonic-gate int main(int argc, char **argv){
2517c478bd9Sstevel@tonic-gate   char *zFile;
2527c478bd9Sstevel@tonic-gate   int i, n;
2537c478bd9Sstevel@tonic-gate   pthread_t id;
2547c478bd9Sstevel@tonic-gate   if( argc>2 && strcmp(argv[1], "-v")==0 ){
2557c478bd9Sstevel@tonic-gate     verbose = 1;
2567c478bd9Sstevel@tonic-gate     argc--;
2577c478bd9Sstevel@tonic-gate     argv++;
2587c478bd9Sstevel@tonic-gate   }
2597c478bd9Sstevel@tonic-gate   if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
2607c478bd9Sstevel@tonic-gate   for(i=0; i<n; i++){
2617c478bd9Sstevel@tonic-gate     char zBuf[200];
2627c478bd9Sstevel@tonic-gate     sprintf(zBuf, "testdb-%d", (i+1)/2);
2637c478bd9Sstevel@tonic-gate     unlink(zBuf);
2647c478bd9Sstevel@tonic-gate   }
2657c478bd9Sstevel@tonic-gate   for(i=0; i<n; i++){
2667c478bd9Sstevel@tonic-gate     zFile = sqlite_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
2677c478bd9Sstevel@tonic-gate     unlink(zFile);
2687c478bd9Sstevel@tonic-gate     pthread_create(&id, 0, worker_bee, (void*)zFile);
2697c478bd9Sstevel@tonic-gate     pthread_detach(id);
2707c478bd9Sstevel@tonic-gate   }
2717c478bd9Sstevel@tonic-gate   pthread_mutex_lock(&lock);
2727c478bd9Sstevel@tonic-gate   while( thread_cnt>0 ){
2737c478bd9Sstevel@tonic-gate     pthread_cond_wait(&sig, &lock);
2747c478bd9Sstevel@tonic-gate   }
2757c478bd9Sstevel@tonic-gate   pthread_mutex_unlock(&lock);
2767c478bd9Sstevel@tonic-gate   for(i=0; i<n; i++){
2777c478bd9Sstevel@tonic-gate     char zBuf[200];
2787c478bd9Sstevel@tonic-gate     sprintf(zBuf, "testdb-%d", (i+1)/2);
2797c478bd9Sstevel@tonic-gate     unlink(zBuf);
2807c478bd9Sstevel@tonic-gate   }
2817c478bd9Sstevel@tonic-gate   return 0;
2827c478bd9Sstevel@tonic-gate }
283