SQLite

Check-in [8299bdb7cb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Update test program "tserver" to use a native pthreads mutex/condition variable to efficiently manage wal file checkpoints without the wal file growing indefinitely.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | server-process-edition
Files: files | file ages | folders
SHA3-256: 8299bdb7cbede30c665dda131bdcbd1d260b3ae9bd16d9b414d8c3776b08f1b3
User & Date: dan 2017-07-29 17:01:06.529
Context
2017-07-29
17:10
Merge latest trunk changes with this branch. (check-in: b42c87790e user: dan tags: server-process-edition)
17:01
Update test program "tserver" to use a native pthreads mutex/condition variable to efficiently manage wal file checkpoints without the wal file growing indefinitely. (check-in: 8299bdb7cb user: dan tags: server-process-edition)
2017-07-28
21:02
Reduce the number of mallocs required of writers in server mode. (check-in: 60953997f6 user: dan tags: server-process-edition)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/pager.c.
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
int sqlite3PagerWalFramesize(Pager *pPager){
  assert( pPager->eState>=PAGER_READER );
  return sqlite3WalFramesize(pPager->pWal);
}
#endif

#ifdef SQLITE_SERVER_EDITION
int sqlite3PagerIsServer(Pager *pPager){
  return pagerIsServer(pPager);
}
int sqlite3PagerPagelock(Pager *pPager, Pgno pgno, int bWrite){
  if( pagerIsServer(pPager)==0 ) return SQLITE_OK;
  return sqlite3ServerLock(pPager->pServer, pgno, bWrite, 0);
}
#endif

#endif /* SQLITE_OMIT_DISKIO */







<
<
<







7743
7744
7745
7746
7747
7748
7749



7750
7751
7752
7753
7754
7755
7756
int sqlite3PagerWalFramesize(Pager *pPager){
  assert( pPager->eState>=PAGER_READER );
  return sqlite3WalFramesize(pPager->pWal);
}
#endif

#ifdef SQLITE_SERVER_EDITION



int sqlite3PagerPagelock(Pager *pPager, Pgno pgno, int bWrite){
  if( pagerIsServer(pPager)==0 ) return SQLITE_OK;
  return sqlite3ServerLock(pPager->pServer, pgno, bWrite, 0);
}
#endif

#endif /* SQLITE_OMIT_DISKIO */
Changes to src/pager.h.
234
235
236
237
238
239
240
241
242
243
244
245
246
#else
# define disable_simulated_io_errors()
# define enable_simulated_io_errors()
#endif

#ifdef SQLITE_SERVER_EDITION
  int sqlite3PagerRollbackJournal(Pager*, sqlite3_file*);
  int sqlite3PagerIsServer(Pager *pPager);
  int sqlite3PagerPagelock(Pager *pPager, Pgno, int);
  void sqlite3PagerServerJournal(Pager*, sqlite3_file*, const char*);
#endif

#endif /* SQLITE_PAGER_H */







<





234
235
236
237
238
239
240

241
242
243
244
245
#else
# define disable_simulated_io_errors()
# define enable_simulated_io_errors()
#endif

#ifdef SQLITE_SERVER_EDITION
  int sqlite3PagerRollbackJournal(Pager*, sqlite3_file*);

  int sqlite3PagerPagelock(Pager *pPager, Pgno, int);
  void sqlite3PagerServerJournal(Pager*, sqlite3_file*, const char*);
#endif

#endif /* SQLITE_PAGER_H */
Changes to src/server.c.
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  if( pDb->nClient==0 ){
    ServerPage *pFree;
    ServerDb **pp;
    serverShutdownDatabase(pDb);
    for(pp=&g_server.pDb; *pp!=pDb; pp=&((*pp)->pNext));
    *pp = pDb->pNext;
    sqlite3_mutex_free(pDb->mutex);
    while( pFree=pDb->pFree ){
      pDb->pFree = pFree->pNext;
      sqlite3_free(pFree);
    }
    sqlite3_free(pDb);
  }
  serverLeaveMutex();








|







285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
  if( pDb->nClient==0 ){
    ServerPage *pFree;
    ServerDb **pp;
    serverShutdownDatabase(pDb);
    for(pp=&g_server.pDb; *pp!=pDb; pp=&((*pp)->pNext));
    *pp = pDb->pNext;
    sqlite3_mutex_free(pDb->mutex);
    while( (pFree = pDb->pFree) ){
      pDb->pFree = pFree->pNext;
      sqlite3_free(pFree);
    }
    sqlite3_free(pDb);
  }
  serverLeaveMutex();

Changes to tool/tserver.c.
28
29
30
31
32
33
34


35
36
37
38
39
40
41
** Dot-commands are:
**
**   .list                    Display all SQL statements in the list.
**   .quit                    Disconnect.
**   .run                     Run all SQL statements in the list.
**   .repeats N               Configure the number of repeats per ".run".
**   .seconds N               Configure the number of seconds to ".run" for.


**
** Example input:
**
**   BEGIN;
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));







>
>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
** Dot-commands are:
**
**   .list                    Display all SQL statements in the list.
**   .quit                    Disconnect.
**   .run                     Run all SQL statements in the list.
**   .repeats N               Configure the number of repeats per ".run".
**   .seconds N               Configure the number of seconds to ".run" for.
**   .mutex_commit            Add a "COMMIT" protected by a g.commit_mutex
**                            to the current SQL.
**
** Example input:
**
**   BEGIN;
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
**     INSERT INTO t1 VALUES(randomblob(10), randomblob(100));
56
57
58
59
60
61
62




63
64

65









66







67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>

#include "sqlite3.h"





/* Database used by this server */
static char *zDatabaseName = 0;











static char *zGlobalVfs = 0;








typedef struct ClientCtx ClientCtx;
struct ClientCtx {
  sqlite3 *db;                    /* Database handle for this client */
  int fd;                         /* Client fd */
  int nRepeat;                    /* Number of times to repeat SQL */
  int nSecond;                    /* Number of seconds to run for */
  sqlite3_stmt **apPrepare;       /* Array of prepared statements */
  int nPrepare;                   /* Valid size of apPrepare[] */
  int nAlloc;                     /* Allocated size of apPrepare[] */
};

static int is_eol(int i){
  return (i=='\n' || i=='\r');
}







>
>
>
>
|
|
>

>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>







|







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>

#include "sqlite3.h"

#define TSERVER_DEFAULT_CHECKPOINT_THRESHOLD 3900

/* Global variables */
struct TserverGlobal {
  char *zDatabaseName;             /* Database used by this server */
  char *zVfs;
  sqlite3_mutex *commit_mutex;

  /* The following use native pthreads instead of a portable interface. This
  ** is because a condition variable, as well as a mutex, is required.  */
  pthread_mutex_t ckpt_mutex;
  pthread_cond_t ckpt_cond;
  int nThreshold;                  /* Checkpoint when wal is this large */
  int bCkptRequired;               /* True if wal checkpoint is required */
  int nRun;                        /* Number of clients in ".run" */
  int nWait;                       /* Number of clients waiting on ckpt_cond */
};

static struct TserverGlobal g = {0};

typedef struct ClientSql ClientSql;
struct ClientSql {
  sqlite3_stmt *pStmt;
  int bMutex;
};

typedef struct ClientCtx ClientCtx;
struct ClientCtx {
  sqlite3 *db;                    /* Database handle for this client */
  int fd;                         /* Client fd */
  int nRepeat;                    /* Number of times to repeat SQL */
  int nSecond;                    /* Number of seconds to run for */
  ClientSql *aPrepare;            /* Array of prepared statements */
  int nPrepare;                   /* Valid size of apPrepare[] */
  int nAlloc;                     /* Allocated size of apPrepare[] */
};

static int is_eol(int i){
  return (i=='\n' || i=='\r');
}
133
134
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182








































































































183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280







281
282
283
284
285

286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314



315
316
317
318
319
320
321
static int handle_some_sql(ClientCtx *p, const char *zSql, int nSql){
  const char *zTail = zSql;
  int nTail = nSql;
  int rc = SQLITE_OK;

  while( rc==SQLITE_OK ){
    if( p->nPrepare>=p->nAlloc ){
      int nByte = (p->nPrepare+32) * sizeof(sqlite3_stmt*);
      sqlite3_stmt **apNew = sqlite3_realloc(p->apPrepare, nByte);
      if( apNew ){

        p->apPrepare = apNew;
        p->nAlloc = p->nPrepare+32;
      }else{
        rc = SQLITE_NOMEM;
        break;
      }
    }
    rc = sqlite3_prepare_v2(
        p->db, zTail, nTail, &p->apPrepare[p->nPrepare], &zTail
    );
    if( rc!=SQLITE_OK ){
      send_message(p, "error - %s\n", sqlite3_errmsg(p->db));
      rc = 1;
      break;
    }
    if( p->apPrepare[p->nPrepare]==0 ){
      break;
    }
    p->nPrepare++;
    nTail = nSql - (zTail-zSql);
    rc = send_message(p, "ok (%d SQL statements)\n", p->nPrepare);
  }

  return rc;
}

static sqlite3_int64 get_timer(void){
  struct timeval t;
  gettimeofday(&t, 0);
  return ((sqlite3_int64)t.tv_usec / 1000) + ((sqlite3_int64)t.tv_sec * 1000);
}

static void clear_sql(ClientCtx *p){
  int j;
  for(j=0; j<p->nPrepare; j++){
    sqlite3_finalize(p->apPrepare[j]);
  }
  p->nPrepare = 0;
}









































































































static int handle_dot_command(ClientCtx *p, const char *zCmd, int nCmd){
  assert( zCmd[0]=='.' );
  int n;
  int rc = 0;
  const char *z = &zCmd[1];
  const char *zArg;
  int nArg;


  for(n=0; n<(nCmd-1); n++){
    if( is_whitespace(z[n]) ) break;
  }

  zArg = &z[n];
  nArg = nCmd-n;
  trim_string(&zArg, &nArg);

  if( n>=1 && n<=4 && 0==strncmp(z, "list", n) ){
    int i;
    for(i=0; rc==0 && i<p->nPrepare; i++){
      const char *zSql = sqlite3_sql(p->apPrepare[i]);
      int nSql = strlen(zSql);
      trim_string(&zSql, &nSql);
      rc = send_message(p, "%d: %.*s\n", i, nSql, zSql);
    }
  }

  else if( n>=1 && n<=4 && 0==strncmp(z, "quit", n) ){
    rc = 1;
  }

  else if( n>=2 && n<=7 && 0==strncmp(z, "repeats", n) ){
    if( nArg ){
      p->nRepeat = strtol(zArg, 0, 0);
      if( p->nRepeat>0 ) p->nSecond = 0;
    }
    rc = send_message(p, "ok (repeat=%d)\n", p->nRepeat);
  }

  else if( n>=2 && n<=3 && 0==strncmp(z, "run", n) ){
    int i, j;
    int nBusy = 0;
    sqlite3_int64 t0 = get_timer();
    sqlite3_int64 t1 = t0;
    int nT1 = 0;
    int nTBusy1 = 0;

    for(j=0; (p->nRepeat<=0 || j<p->nRepeat) && rc==SQLITE_OK; j++){
      sqlite3_int64 t2;

      for(i=0; i<p->nPrepare && rc==SQLITE_OK; i++){
        sqlite3_stmt *pStmt = p->apPrepare[i];

        /* Execute the statement */
        while( sqlite3_step(pStmt)==SQLITE_ROW );
        rc = sqlite3_reset(pStmt);

        if( (rc & 0xFF)==SQLITE_BUSY ){
          if( sqlite3_get_autocommit(p->db)==0 ){
            sqlite3_exec(p->db, "ROLLBACK", 0, 0, 0);
          }
          nBusy++;
          rc = SQLITE_OK;
          break;
        }
        else if( rc!=SQLITE_OK ){
          send_message(p, "error - %s\n", sqlite3_errmsg(p->db));
        }
      }

      t2 = get_timer();
      if( t2>=(t1+1000) ){
        int nMs = (t2 - t1);
        int nDone = (j+1 - nBusy - nT1);

        rc = send_message(
            p, "(%d done @ %d per second, %d busy)\n", 
            nDone, (1000*nDone + nMs/2) / nMs, nBusy - nTBusy1
        );
        t1 = t2;
        nT1 = j+1 - nBusy;
        nTBusy1 = nBusy;
        if( p->nSecond>0 && (p->nSecond*1000)<=t1-t0 ) break;
      }
    }

    if( rc==SQLITE_OK ){
      send_message(p, "ok (%d/%d SQLITE_BUSY)\n", nBusy, j);
    }
    clear_sql(p);
  }

  else if( n>=1 && n<=7 && 0==strncmp(z, "seconds", n) ){
    if( nArg ){
      p->nSecond = strtol(zArg, 0, 0);
      if( p->nSecond>0 ) p->nRepeat = 0;
    }
    rc = send_message(p, "ok (repeat=%d)\n", p->nRepeat);
  }








  else{
    send_message(p, 
        "unrecognized dot command: %.*s\n"
        "should be \"list\", \"run\", \"repeats\", or \"seconds\"\n", n, z

    );
    rc = 1;
  }

  return rc;
}

static void *handle_client(void *pArg){
  char zCmd[32*1024];             /* Read buffer */
  int nCmd = 0;                   /* Valid bytes in zCmd[] */
  int res;                        /* Result of read() call */
  int rc = SQLITE_OK;

  ClientCtx ctx;
  memset(&ctx, 0, sizeof(ClientCtx));

  ctx.fd = (int)(intptr_t)pArg;
  ctx.nRepeat = 1;
  rc = sqlite3_open_v2(zDatabaseName, &ctx.db,
      SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zGlobalVfs
  );
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(ctx.db));
    return 0;
  }
  sqlite3_create_function(
      ctx.db, "usleep", 1, SQLITE_UTF8, (void*)sqlite3_vfs_find(0), 
      usleepFunc, 0, 0
  );




  while( rc==SQLITE_OK ){
    int i;
    int iStart;
    int nConsume;
    res = read(ctx.fd, &zCmd[nCmd], sizeof(zCmd)-nCmd-1);
    if( res<=0 ) break;







|
|
|
>
|







|






|



















|




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<






>











|



















<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<









>
>
>
>
>
>
>




|
>


















|
|









>
>
>







156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349






350











































351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
static int handle_some_sql(ClientCtx *p, const char *zSql, int nSql){
  const char *zTail = zSql;
  int nTail = nSql;
  int rc = SQLITE_OK;

  while( rc==SQLITE_OK ){
    if( p->nPrepare>=p->nAlloc ){
      int nByte = (p->nPrepare+32) * sizeof(ClientSql);
      ClientSql *aNew = sqlite3_realloc(p->aPrepare, nByte);
      if( aNew ){
        memset(&aNew[p->nPrepare], 0, sizeof(ClientSql)*32);
        p->aPrepare = aNew;
        p->nAlloc = p->nPrepare+32;
      }else{
        rc = SQLITE_NOMEM;
        break;
      }
    }
    rc = sqlite3_prepare_v2(
        p->db, zTail, nTail, &p->aPrepare[p->nPrepare].pStmt, &zTail
    );
    if( rc!=SQLITE_OK ){
      send_message(p, "error - %s\n", sqlite3_errmsg(p->db));
      rc = 1;
      break;
    }
    if( p->aPrepare[p->nPrepare].pStmt==0 ){
      break;
    }
    p->nPrepare++;
    nTail = nSql - (zTail-zSql);
    rc = send_message(p, "ok (%d SQL statements)\n", p->nPrepare);
  }

  return rc;
}

static sqlite3_int64 get_timer(void){
  struct timeval t;
  gettimeofday(&t, 0);
  return ((sqlite3_int64)t.tv_usec / 1000) + ((sqlite3_int64)t.tv_sec * 1000);
}

static void clear_sql(ClientCtx *p){
  int j;
  for(j=0; j<p->nPrepare; j++){
    sqlite3_finalize(p->aPrepare[j].pStmt);
  }
  p->nPrepare = 0;
}

/*
** The sqlite3_wal_hook() callback used by all client database connections.
*/
static int clientWalHook(void *pArg, sqlite3 *db, const char *zDb, int nFrame){
  if( nFrame>=g.nThreshold ){
    g.bCkptRequired = 1;
  }
  return SQLITE_OK;
}

static int handle_run_command(ClientCtx *p){
  int i, j;
  int nBusy = 0;
  sqlite3_int64 t0 = get_timer();
  sqlite3_int64 t1 = t0;
  int nT1 = 0;
  int nTBusy1 = 0;
  int rc = SQLITE_OK;

  pthread_mutex_lock(&g.ckpt_mutex);
  g.nRun++;
  pthread_mutex_unlock(&g.ckpt_mutex);


  for(j=0; (p->nRepeat<=0 || j<p->nRepeat) && rc==SQLITE_OK; j++){
    sqlite3_int64 t2;

    for(i=0; i<p->nPrepare && rc==SQLITE_OK; i++){
      sqlite3_stmt *pStmt = p->aPrepare[i].pStmt;

      /* If the bMutex flag is set, grab g.commit_mutex before executing
      ** the SQL statement (which is always "COMMIT" in this case). */
      if( p->aPrepare[i].bMutex ){
        sqlite3_mutex_enter(g.commit_mutex);
      }

      /* Execute the statement */
      while( sqlite3_step(pStmt)==SQLITE_ROW );
      rc = sqlite3_reset(pStmt);

      /* Relinquish the g.commit_mutex mutex if required. */
      if( p->aPrepare[i].bMutex ){
        sqlite3_mutex_leave(g.commit_mutex);
      }

      if( (rc & 0xFF)==SQLITE_BUSY ){
        if( sqlite3_get_autocommit(p->db)==0 ){
          sqlite3_exec(p->db, "ROLLBACK", 0, 0, 0);
        }
        nBusy++;
        rc = SQLITE_OK;
        break;
      }
      else if( rc!=SQLITE_OK ){
        send_message(p, "error - %s\n", sqlite3_errmsg(p->db));
      }
    }

    t2 = get_timer();
    if( t2>=(t1+1000) ){
      int nMs = (t2 - t1);
      int nDone = (j+1 - nBusy - nT1);

      rc = send_message(
          p, "(%d done @ %d per second, %d busy)\n", 
          nDone, (1000*nDone + nMs/2) / nMs, nBusy - nTBusy1
          );
      t1 = t2;
      nT1 = j+1 - nBusy;
      nTBusy1 = nBusy;
      if( p->nSecond>0 && (p->nSecond*1000)<=t1-t0 ) break;
    }

    /* Checkpoint handling. */
    pthread_mutex_lock(&g.ckpt_mutex);
    if( rc==SQLITE_OK && g.bCkptRequired ){
      if( g.nWait==g.nRun-1 ){
        /* All other clients are already waiting on the condition variable.
        ** Run the checkpoint, signal the condition and move on.  */
        rc = sqlite3_wal_checkpoint(p->db, "main");
        g.bCkptRequired = 0;
        pthread_cond_broadcast(&g.ckpt_cond);
      }else{
        assert( g.nWait<g.nRun-1 );
        g.nWait++;
        pthread_cond_wait(&g.ckpt_cond, &g.ckpt_mutex);
        g.nWait--;
      }
    }
    pthread_mutex_unlock(&g.ckpt_mutex);
  }

  if( rc==SQLITE_OK ){
    send_message(p, "ok (%d/%d SQLITE_BUSY)\n", nBusy, j);
  }
  clear_sql(p);

  pthread_mutex_lock(&g.ckpt_mutex);
  g.nRun--;
  pthread_mutex_unlock(&g.ckpt_mutex);

  return rc;
}

static int handle_dot_command(ClientCtx *p, const char *zCmd, int nCmd){

  int n;
  int rc = 0;
  const char *z = &zCmd[1];
  const char *zArg;
  int nArg;

  assert( zCmd[0]=='.' );
  for(n=0; n<(nCmd-1); n++){
    if( is_whitespace(z[n]) ) break;
  }

  zArg = &z[n];
  nArg = nCmd-n;
  trim_string(&zArg, &nArg);

  if( n>=1 && n<=4 && 0==strncmp(z, "list", n) ){
    int i;
    for(i=0; rc==0 && i<p->nPrepare; i++){
      const char *zSql = sqlite3_sql(p->aPrepare[i].pStmt);
      int nSql = strlen(zSql);
      trim_string(&zSql, &nSql);
      rc = send_message(p, "%d: %.*s\n", i, nSql, zSql);
    }
  }

  else if( n>=1 && n<=4 && 0==strncmp(z, "quit", n) ){
    rc = 1;
  }

  else if( n>=2 && n<=7 && 0==strncmp(z, "repeats", n) ){
    if( nArg ){
      p->nRepeat = strtol(zArg, 0, 0);
      if( p->nRepeat>0 ) p->nSecond = 0;
    }
    rc = send_message(p, "ok (repeat=%d)\n", p->nRepeat);
  }

  else if( n>=2 && n<=3 && 0==strncmp(z, "run", n) ){






    rc = handle_run_command(p);











































  }

  else if( n>=1 && n<=7 && 0==strncmp(z, "seconds", n) ){
    if( nArg ){
      p->nSecond = strtol(zArg, 0, 0);
      if( p->nSecond>0 ) p->nRepeat = 0;
    }
    rc = send_message(p, "ok (repeat=%d)\n", p->nRepeat);
  }

  else if( n>=1 && n<=12 && 0==strncmp(z, "mutex_commit", n) ){
    rc = handle_some_sql(p, "COMMIT;", 7);
    if( rc==SQLITE_OK ){
      p->aPrepare[p->nPrepare-1].bMutex = 1;
    }
  }

  else{
    send_message(p, 
        "unrecognized dot command: %.*s\n"
        "should be \"list\", \"run\", \"repeats\", \"mutex_commit\" "
        "or \"seconds\"\n", n, z
    );
    rc = 1;
  }

  return rc;
}

static void *handle_client(void *pArg){
  char zCmd[32*1024];             /* Read buffer */
  int nCmd = 0;                   /* Valid bytes in zCmd[] */
  int res;                        /* Result of read() call */
  int rc = SQLITE_OK;

  ClientCtx ctx;
  memset(&ctx, 0, sizeof(ClientCtx));

  ctx.fd = (int)(intptr_t)pArg;
  ctx.nRepeat = 1;
  rc = sqlite3_open_v2(g.zDatabaseName, &ctx.db,
      SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, g.zVfs
  );
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(ctx.db));
    return 0;
  }
  sqlite3_create_function(
      ctx.db, "usleep", 1, SQLITE_UTF8, (void*)sqlite3_vfs_find(0), 
      usleepFunc, 0, 0
  );

  /* Register the wal-hook with the new client connection */
  sqlite3_wal_hook(ctx.db, clientWalHook, (void*)&ctx);

  while( rc==SQLITE_OK ){
    int i;
    int iStart;
    int nConsume;
    res = read(ctx.fd, &zCmd[nCmd], sizeof(zCmd)-nCmd-1);
    if( res<=0 ) break;
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
      }
    }while( rc==SQLITE_OK && nConsume>0 );
  }

  fprintf(stdout, "Client %d disconnects\n", ctx.fd);
  close(ctx.fd);
  clear_sql(&ctx);
  sqlite3_free(ctx.apPrepare);
  sqlite3_close(ctx.db);
  return 0;
} 

static void usage(const char *zExec){
  fprintf(stderr, "Usage: %s ?-vfs VFS? DATABASE\n", zExec);
  exit(1);







|







465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
      }
    }while( rc==SQLITE_OK && nConsume>0 );
  }

  fprintf(stdout, "Client %d disconnects\n", ctx.fd);
  close(ctx.fd);
  clear_sql(&ctx);
  sqlite3_free(ctx.aPrepare);
  sqlite3_close(ctx.db);
  return 0;
} 

static void usage(const char *zExec){
  fprintf(stderr, "Usage: %s ?-vfs VFS? DATABASE\n", zExec);
  exit(1);
402
403
404
405
406
407
408
409
410
411

412




413
414
415
416
417
418
419
420
421

  if( argc!=2 && argc!=4 ){
    usage(argv[0]);
  }
  if( argc==4 ){
    int n = strlen(argv[1]);
    if( n<2 || n>4 || memcmp("-vfs", argv[1], 4) ) usage(argv[0]);
    zGlobalVfs = argv[2];
  }
  zDatabaseName = argv[argc-1];






  rc = sqlite3_open_v2(zDatabaseName, &db,
      SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zGlobalVfs
  );
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(db));
    return 1;
  }

  rc = sqlite3_exec(db, "SELECT * FROM sqlite_master", 0, 0, 0);







|

|
>

>
>
>
>
|
|







492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

  if( argc!=2 && argc!=4 ){
    usage(argv[0]);
  }
  if( argc==4 ){
    int n = strlen(argv[1]);
    if( n<2 || n>4 || memcmp("-vfs", argv[1], 4) ) usage(argv[0]);
    g.zVfs = argv[2];
  }
  g.zDatabaseName = argv[argc-1];
  g.commit_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);

  g.nThreshold = TSERVER_DEFAULT_CHECKPOINT_THRESHOLD;
  pthread_mutex_init(&g.ckpt_mutex, 0);
  pthread_cond_init(&g.ckpt_cond, 0);

  rc = sqlite3_open_v2(g.zDatabaseName, &db,
      SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, g.zVfs
  );
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "sqlite3_open(): %s\n", sqlite3_errmsg(db));
    return 1;
  }

  rc = sqlite3_exec(db, "SELECT * FROM sqlite_master", 0, 0, 0);