gitlab.com/danp128/sqlite@v1.0.0/internal/sqlite.org/sqlite-src-3190300/test/threadtest4.c (about)

     1  /*
     2  ** 2014-12-11
     3  **
     4  ** The author disclaims copyright to this source code.  In place of
     5  ** a legal notice, here is a blessing:
     6  **
     7  **    May you do good and not evil.
     8  **    May you find forgiveness for yourself and forgive others.
     9  **    May you share freely, never taking more than you give.
    10  **
    11  *************************************************************************
    12  ** 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.
    15  **
    16  ** This test program runs on unix-like systems only.  It uses pthreads.
    17  ** To compile:
    18  **
    19  **     gcc -g -Wall -I. threadtest4.c sqlite3.c -ldl -lpthread
    20  **
    21  ** To run:
    22  **
    23  **     ./a.out 10
    24  **
    25  ** The argument is the number of threads.  There are also options, such
    26  ** as -wal and -multithread and -serialized.
    27  **
    28  ** Consider also compiling with clang instead of gcc and adding the
    29  ** -fsanitize=thread option.
    30  */
    31  #include "sqlite3.h"
    32  #include <pthread.h>
    33  #include <sched.h>
    34  #include <stdio.h>
    35  #include <stdlib.h>
    36  #include <string.h>
    37  #include <unistd.h>
    38  #include <stdarg.h>
    39  
    40  /*
    41  ** An instance of the following structure is passed into each worker
    42  ** thread.
    43  */
    44  typedef struct WorkerInfo WorkerInfo;
    45  struct 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 */
    56  };
    57  
    58  /*
    59  ** Allowed values for WorkerInfo.wkrFlags
    60  */
    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 */
    64  
    65  
    66  /*
    67  ** Report an OOM error and die if the argument is NULL
    68  */
    69  static void check_oom(void *x){
    70    if( x==0 ){
    71      fprintf(stderr, "out of memory\n");
    72      exit(1);
    73    }
    74  }
    75  
    76  /*
    77  ** Allocate memory.  If the allocation fails, print an error message and
    78  ** kill the process.
    79  */
    80  static void *safe_malloc(int sz){
    81    void *x = sqlite3_malloc(sz>0?sz:1);
    82    check_oom(x);
    83    return x;
    84  }
    85  
    86  /*
    87  ** Print a trace message for a worker
    88  */
    89  static void worker_trace(WorkerInfo *p, const char *zFormat, ...){
    90    va_list ap;
    91    char *zMsg;
    92    if( (p->wkrFlags & TT4_TRACE)==0 ) return;
    93    va_start(ap, zFormat);
    94    zMsg = sqlite3_vmprintf(zFormat, ap);
    95    check_oom(zMsg);
    96    va_end(ap);
    97    fprintf(stderr, "TRACE(%02d): %s\n", p->tid, zMsg);
    98    sqlite3_free(zMsg);
    99  }
   100  
   101  /*
   102  ** Prepare a single SQL query
   103  */
   104  static sqlite3_stmt *prep_sql(sqlite3 *db, const char *zFormat, ...){
   105    va_list ap;
   106    char *zSql;
   107    int rc, i;
   108    sqlite3_stmt *pStmt = 0;
   109  
   110    va_start(ap, zFormat);
   111    zSql = sqlite3_vmprintf(zFormat, ap);
   112    va_end(ap);
   113    check_oom(zSql);
   114    for (i = 0; i < 1000; i++) {
   115  	  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
   116  	  if (rc == SQLITE_OK) {
   117  		  break;
   118  	  }
   119    }
   120    if( rc!=SQLITE_OK ){
   121      fprintf(stderr, "SQL error (%d,%d): %s\nWhile preparing: [%s]\n",
   122              rc, sqlite3_extended_errcode(db), sqlite3_errmsg(db), zSql);
   123      exit(1);
   124    }
   125    sqlite3_free(zSql);
   126    return pStmt;
   127  }
   128  
   129  /*
   130  ** Run a SQL statements.  Panic if unable.
   131  */
   132  static void run_sql(WorkerInfo *p, const char *zFormat, ...){
   133    va_list ap;
   134    char *zSql;
   135    int rc, i;
   136    sqlite3_stmt *pStmt = 0;
   137    int nRetry = 0;
   138  
   139    va_start(ap, zFormat);
   140    zSql = sqlite3_vmprintf(zFormat, ap);
   141    va_end(ap);
   142    check_oom(zSql);
   143    for (i = 0; i < 1000; i++) {
   144    	rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
   145  	if (rc == SQLITE_OK) {
   146  		break;
   147  	}
   148    }
   149    if( rc!=SQLITE_OK ){
   150      fprintf(stderr, "SQL error (%d,%d): %s\nWhile preparing: [%s]\n",
   151              rc, sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zSql);
   152      exit(1);
   153    }
   154    worker_trace(p, "running [%s]", zSql);
   155    while( (rc = sqlite3_step(pStmt))!=SQLITE_DONE ){
   156      if( (rc&0xff)==SQLITE_BUSY || (rc&0xff)==SQLITE_LOCKED ){
   157        sqlite3_reset(pStmt);
   158        nRetry++;
   159        if( nRetry<10 ){
   160          worker_trace(p, "retry %d for [%s]", nRetry, zSql);
   161          sched_yield();
   162          continue;
   163        }else{
   164          fprintf(stderr, "Deadlock in thread %d while running [%s]\n",
   165                  p->tid, zSql);
   166          exit(1);
   167        }
   168      }
   169      if( rc!=SQLITE_ROW ){
   170        fprintf(stderr, "SQL error (%d,%d): %s\nWhile running [%s]\n",
   171                rc, sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zSql);
   172        exit(1);
   173      }
   174    }
   175    sqlite3_free(zSql);
   176    sqlite3_finalize(pStmt);
   177  }
   178  
   179  
   180  /*
   181  ** Open the database connection for WorkerInfo.  The order in which
   182  ** the files are opened is a function of the tid value.
   183  */
   184  static void worker_open_connection(WorkerInfo *p, int iCnt){
   185    char *zFile;
   186    int x;
   187    int rc;
   188    static const unsigned char aOrder[6][3] = {
   189      { 1, 2, 3},
   190      { 1, 3, 2},
   191      { 2, 1, 3},
   192      { 2, 3, 1},
   193      { 3, 1, 2},
   194      { 3, 2, 1}
   195    };
   196    x = (p->tid + iCnt) % 6;
   197    zFile = sqlite3_mprintf("tt4-test%d.db", aOrder[x][0]);
   198    check_oom(zFile);
   199    worker_trace(p, "open %s", zFile);
   200    rc = sqlite3_open_v2(zFile, &p->db,
   201                         SQLITE_OPEN_READWRITE|SQLITE_OPEN_SHAREDCACHE, 0);
   202    if( rc!=SQLITE_OK ){
   203      fprintf(stderr, "sqlite_open_v2(%s) failed on thread %d\n",
   204              zFile, p->tid);
   205      exit(1);
   206    }
   207    sqlite3_free(zFile);
   208    run_sql(p, "PRAGMA read_uncommitted=ON;");
   209    sqlite3_busy_timeout(p->db, 10000);
   210    run_sql(p, "PRAGMA synchronous=OFF;");
   211    run_sql(p, "ATTACH 'tt4-test%d.db' AS aux1", aOrder[x][1]);
   212    run_sql(p, "ATTACH 'tt4-test%d.db' AS aux2", aOrder[x][2]);
   213  }
   214  
   215  /*
   216  ** Close the worker database connection
   217  */
   218  static void worker_close_connection(WorkerInfo *p){
   219    if( p->db ){
   220      worker_trace(p, "close");
   221      sqlite3_close(p->db);
   222      p->db = 0;
   223    }
   224  }
   225  
   226  /*
   227  ** Delete all content in the three databases associated with a
   228  ** single thread.  Make this happen all in a single transaction if
   229  ** inTrans is true, or separately for each database if inTrans is
   230  ** false.
   231  */
   232  static void worker_delete_all_content(WorkerInfo *p, int inTrans){
   233    if( inTrans ){
   234      pthread_mutex_lock(p->pWrMutex);
   235      run_sql(p, "BEGIN");
   236      run_sql(p, "DELETE FROM t1 WHERE tid=%d", p->tid);
   237      run_sql(p, "DELETE FROM t2 WHERE tid=%d", p->tid);
   238      run_sql(p, "DELETE FROM t3 WHERE tid=%d", p->tid);
   239      run_sql(p, "COMMIT");
   240      pthread_mutex_unlock(p->pWrMutex);
   241      p->nTest++;
   242    }else{
   243      pthread_mutex_lock(p->pWrMutex);
   244      run_sql(p, "DELETE FROM t1 WHERE tid=%d", p->tid);
   245      pthread_mutex_unlock(p->pWrMutex);
   246      p->nTest++;
   247      pthread_mutex_lock(p->pWrMutex);
   248      run_sql(p, "DELETE FROM t2 WHERE tid=%d", p->tid);
   249      pthread_mutex_unlock(p->pWrMutex);
   250      p->nTest++;
   251      pthread_mutex_lock(p->pWrMutex);
   252      run_sql(p, "DELETE FROM t3 WHERE tid=%d", p->tid);
   253      pthread_mutex_unlock(p->pWrMutex);
   254      p->nTest++;
   255    }
   256  }
   257  
   258  /*
   259  ** Create rows mn through mx in table iTab for the given worker
   260  */
   261  static void worker_add_content(WorkerInfo *p, int mn, int mx, int iTab){
   262    char *zTabDef;
   263    switch( iTab ){
   264      case 1:  zTabDef = "t1(tid,sp,a,b,c)";  break;
   265      case 2:  zTabDef = "t2(tid,sp,d,e,f)";  break;
   266      case 3:  zTabDef = "t3(tid,sp,x,y,z)";  break;
   267    }
   268    pthread_mutex_lock(p->pWrMutex);
   269    run_sql(p, 
   270       "WITH RECURSIVE\n"
   271       " c(i) AS (VALUES(%d) UNION ALL SELECT i+1 FROM c WHERE i<%d)\n"
   272       "INSERT INTO %s SELECT %d, zeroblob(3000), i, printf('%%d',i), i FROM c;",
   273       mn, mx, zTabDef, p->tid
   274    );
   275    pthread_mutex_unlock(p->pWrMutex);
   276    p->nTest++;
   277  }
   278  
   279  /*
   280  ** Set an error message on a worker
   281  */
   282  static void worker_error(WorkerInfo *p, const char *zFormat, ...){
   283    va_list ap;
   284    p->nErr++;
   285    sqlite3_free(p->zMsg);
   286    va_start(ap, zFormat);
   287    p->zMsg = sqlite3_vmprintf(zFormat, ap);
   288    va_end(ap);
   289  }
   290  
   291  /*
   292  ** Each thread runs the following function.
   293  */
   294  static void *worker_thread(void *pArg){
   295    WorkerInfo *p = (WorkerInfo*)pArg;
   296    int iOuter;
   297    int i;
   298    int rc;
   299    sqlite3_stmt *pStmt;
   300  
   301    printf("worker %d startup\n", p->tid);  fflush(stdout);
   302    for(iOuter=1; iOuter<=p->nWorker; iOuter++){
   303      worker_open_connection(p, iOuter);
   304      for(i=0; i<4; i++){
   305        worker_add_content(p, i*100+1, (i+1)*100, (p->tid+iOuter)%3 + 1);
   306        worker_add_content(p, i*100+1, (i+1)*100, (p->tid+iOuter+1)%3 + 1);
   307        worker_add_content(p, i*100+1, (i+1)*100, (p->tid+iOuter+2)%3 + 1);
   308      }
   309  
   310      pStmt = prep_sql(p->db, "SELECT count(a) FROM t1 WHERE tid=%d", p->tid);
   311      worker_trace(p, "query [%s]", sqlite3_sql(pStmt));
   312      rc = sqlite3_step(pStmt);
   313      if( rc!=SQLITE_ROW ){
   314        worker_error(p, "Failed to step: %s", sqlite3_sql(pStmt));
   315      }else if( sqlite3_column_int(pStmt, 0)!=400 ){
   316        worker_error(p, "Wrong result: %d", sqlite3_column_int(pStmt,0));
   317      }
   318      sqlite3_finalize(pStmt);
   319      if( p->nErr ) break;
   320  
   321      if( ((iOuter+p->tid)%3)==0 ){
   322        sqlite3_db_release_memory(p->db);
   323        p->nTest++;
   324      }
   325  
   326      pthread_mutex_lock(p->pWrMutex);
   327      run_sql(p, "BEGIN;");
   328      run_sql(p, "UPDATE t1 SET c=NULL WHERE a=55");
   329      run_sql(p, "UPDATE t2 SET f=NULL WHERE d=42");
   330      run_sql(p, "UPDATE t3 SET z=NULL WHERE x=31");
   331      run_sql(p, "ROLLBACK;");
   332      p->nTest++;
   333      pthread_mutex_unlock(p->pWrMutex);
   334  
   335  
   336      if( iOuter==p->tid ){
   337        pthread_mutex_lock(p->pWrMutex);
   338        run_sql(p, "VACUUM");
   339        pthread_mutex_unlock(p->pWrMutex);
   340      }
   341  
   342      pStmt = prep_sql(p->db,
   343         "SELECT t1.rowid, t2.rowid, t3.rowid"
   344         "  FROM t1, t2, t3"
   345         " WHERE t1.tid=%d AND t2.tid=%d AND t3.tid=%d"
   346         "   AND t1.a<>t2.d AND t2.d<>t3.x"
   347         " ORDER BY 1, 2, 3"
   348         ,p->tid, p->tid, p->tid);
   349      worker_trace(p, "query [%s]", sqlite3_sql(pStmt));
   350      for(i=0; i<p->nWorker; i++){
   351        rc = sqlite3_step(pStmt);
   352        if( rc!=SQLITE_ROW ){
   353          worker_error(p, "Failed to step: %s", sqlite3_sql(pStmt));
   354          break;
   355        }
   356        sched_yield();
   357      }
   358      sqlite3_finalize(pStmt);
   359      if( p->nErr ) break;
   360  
   361      worker_delete_all_content(p, (p->tid+iOuter)%2);
   362      worker_close_connection(p);
   363      p->db = 0;
   364    }
   365    worker_close_connection(p);
   366    printf("worker %d finished\n", p->tid); fflush(stdout);
   367    return 0;
   368  }
   369  
   370  int main(int argc, char **argv){
   371    int nWorker = 0;         /* Number of worker threads */
   372    int i;                   /* Loop counter */
   373    WorkerInfo *aInfo;       /* Information for each worker */
   374    unsigned wkrFlags = 0;   /* Default worker flags */
   375    int nErr = 0;            /* Number of errors */
   376    int nTest = 0;           /* Number of tests */
   377    int rc;                  /* Return code */
   378    sqlite3 *db = 0;         /* Main database connection */
   379    pthread_mutex_t wrMutex; /* The write serialization mutex */
   380    WorkerInfo infoTop;      /* WorkerInfo for the main thread */
   381    WorkerInfo *p;           /* Pointer to infoTop */
   382  
   383    sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
   384    for(i=1; i<argc; i++){
   385      const char *z = argv[i];
   386      if( z[0]=='-' ){
   387        if( z[1]=='-' && z[2]!=0 ) z++;
   388        if( strcmp(z,"-multithread")==0 ){
   389          sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
   390          wkrFlags &= ~TT4_SERIALIZED;
   391        }else if( strcmp(z,"-serialized")==0 ){
   392          sqlite3_config(SQLITE_CONFIG_SERIALIZED);
   393          wkrFlags |= TT4_SERIALIZED;
   394        }else if( strcmp(z,"-wal")==0 ){
   395          wkrFlags |= TT4_WAL;
   396        }else if( strcmp(z,"-trace")==0 ){
   397          wkrFlags |= TT4_TRACE;
   398        }else{
   399          fprintf(stderr, "unknown command-line option: %s\n", argv[i]);
   400          exit(1);
   401        }
   402      }else if( z[0]>='1' && z[0]<='9' && nWorker==0 ){
   403        nWorker = atoi(z);
   404        if( nWorker<2 ){
   405          fprintf(stderr, "minimum of 2 threads\n");
   406          exit(1);
   407        }
   408      }else{
   409        fprintf(stderr, "extra command-line argument: \"%s\"\n", argv[i]);
   410        exit(1);
   411      }
   412    }
   413    if( nWorker==0 ){ 
   414      fprintf(stderr,
   415         "usage:  %s ?OPTIONS? N\n"
   416         "N is the number of threads and must be at least 2.\n"
   417         "Options:\n"
   418         "  --serialized\n"
   419         "  --multithread\n"
   420         "  --wal\n"
   421         "  --trace\n"
   422         ,argv[0]
   423      );
   424      exit(1);
   425    }
   426    if( !sqlite3_threadsafe() ){
   427      fprintf(stderr, "requires a threadsafe build of SQLite\n");
   428      exit(1);
   429    }
   430    sqlite3_initialize();
   431    sqlite3_enable_shared_cache(1);
   432    pthread_mutex_init(&wrMutex, 0);
   433  
   434    /* Initialize the test database files */
   435    (void)unlink("tt4-test1.db");
   436    (void)unlink("tt4-test2.db");
   437    (void)unlink("tt4-test3.db");
   438    rc = sqlite3_open("tt4-test1.db", &db);
   439    if( rc!=SQLITE_OK ){
   440      fprintf(stderr, "Unable to open test database: tt4-test2.db\n");
   441      exit(1);
   442    }
   443    memset(&infoTop, 0, sizeof(infoTop));
   444    infoTop.db = db;
   445    infoTop.wkrFlags = wkrFlags;
   446    p = &infoTop;
   447    if( wkrFlags & TT4_WAL ){
   448      run_sql(p, "PRAGMA journal_mode=WAL");
   449    }
   450    run_sql(p, "PRAGMA synchronous=OFF");
   451    run_sql(p, "CREATE TABLE IF NOT EXISTS t1(tid INTEGER, sp, a, b, c)");
   452    run_sql(p, "CREATE INDEX t1tid ON t1(tid)");
   453    run_sql(p, "CREATE INDEX t1ab ON t1(a,b)");
   454    run_sql(p, "ATTACH 'tt4-test2.db' AS 'test2'");
   455    run_sql(p, "CREATE TABLE IF NOT EXISTS test2.t2(tid INTEGER, sp, d, e, f)");
   456    run_sql(p, "CREATE INDEX test2.t2tid ON t2(tid)");
   457    run_sql(p, "CREATE INDEX test2.t2de ON t2(d,e)");
   458    run_sql(p, "ATTACH 'tt4-test3.db' AS 'test3'");
   459    run_sql(p, "CREATE TABLE IF NOT EXISTS test3.t3(tid INTEGER, sp, x, y, z)");
   460    run_sql(p, "CREATE INDEX test3.t3tid ON t3(tid)");
   461    run_sql(p, "CREATE INDEX test3.t3xy ON t3(x,y)");
   462    aInfo = safe_malloc( sizeof(*aInfo)*nWorker );
   463    memset(aInfo, 0, sizeof(*aInfo)*nWorker);
   464    for(i=0; i<nWorker; i++){
   465      aInfo[i].tid = i+1;
   466      aInfo[i].nWorker = nWorker;
   467      aInfo[i].wkrFlags = wkrFlags;
   468      aInfo[i].mainDb = db;
   469      aInfo[i].pWrMutex = &wrMutex;
   470      rc = pthread_create(&aInfo[i].id, 0, worker_thread, &aInfo[i]);
   471      if( rc!=0 ){
   472        fprintf(stderr, "thread creation failed for thread %d\n", i+1);
   473        exit(1);
   474      }
   475      sched_yield();
   476    }
   477    for(i=0; i<nWorker; i++){
   478      pthread_join(aInfo[i].id, 0);
   479      printf("Joined thread %d: %d errors in %d tests",
   480             aInfo[i].tid, aInfo[i].nErr, aInfo[i].nTest);
   481      if( aInfo[i].zMsg ){
   482        printf(": %s\n", aInfo[i].zMsg);
   483      }else{
   484        printf("\n");
   485      }
   486      nErr += aInfo[i].nErr;
   487      nTest += aInfo[i].nTest;
   488      fflush(stdout);
   489    }
   490    sqlite3_close(db);
   491    sqlite3_free(aInfo);
   492    printf("Total %d errors in %d tests\n", nErr, nTest);
   493    return nErr;
   494  }