/ Check-in [60953997]
Login

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

Overview
Comment:Reduce the number of mallocs required of writers in server mode.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | server-process-edition
Files: files | file ages | folders
SHA3-256:60953997f62208f82b1efb53b8a1b0c6a26370411041457f747917e10d9a0e68
User & Date: dan 2017-07-28 21:02:13
Context
2017-07-29
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: 8299bdb7 user: dan tags: server-process-edition
2017-07-28
21:02
Reduce the number of mallocs required of writers in server mode. check-in: 60953997 user: dan tags: server-process-edition
2017-07-24
19:43
Update this branch with latest changes from trunk. check-in: d0719ad7 user: dan tags: server-process-edition
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/pager.c.

5987
5988
5989
5990
5991
5992
5993


5994
5995
5996

5997
5998
5999
6000
6001
6002
6003
  int rc;
  u32 cksum;
  char *pData2;
  i64 iOff = pPager->journalOff;

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsServer(pPager) ){


    int nByte = sizeof(ServerPage) + pPager->pageSize;
    ServerPage *p = (ServerPage*)sqlite3_malloc(nByte);
    if( !p ) return SQLITE_NOMEM_BKPT;

    memset(p, 0, sizeof(ServerPage));
    p->aData = (u8*)&p[1];
    p->nData = pPager->pageSize;
    p->pgno = pPg->pgno;
    p->pNext = pPager->pServerPage;
    pPager->pServerPage = p;
    memcpy(p->aData, pPg->pData, pPager->pageSize);  







>
>
|
|
|
>







5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
  int rc;
  u32 cksum;
  char *pData2;
  i64 iOff = pPager->journalOff;

#ifdef SQLITE_SERVER_EDITION
  if( pagerIsServer(pPager) ){
    ServerPage *p = sqlite3ServerBuffer(pPager->pServer);
    if( p==0 ){
      int nByte = sizeof(ServerPage) + pPager->pageSize;
      p = (ServerPage*)sqlite3_malloc(nByte);
      if( !p ) return SQLITE_NOMEM_BKPT;
    }
    memset(p, 0, sizeof(ServerPage));
    p->aData = (u8*)&p[1];
    p->nData = pPager->pageSize;
    p->pgno = pPg->pgno;
    p->pNext = pPager->pServerPage;
    pPager->pServerPage = p;
    memcpy(p->aData, pPg->pData, pPager->pageSize);  

Changes to src/server.c.

105
106
107
108
109
110
111


112
113
114
115
116
117
118
...
277
278
279
280
281
282
283

284
285
286
287
288




289
290
291
292
293
294
295
...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
...
433
434
435
436
437
438
439

440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

461







462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
...
667
668
669
670
671
672
673
674













675

  int iNextCommit;                /* Commit id for next pre-commit call */ 
  Server *pCommit;                /* List of connections currently commiting */
  Server *pReader;                /* Connections in slower-reader transaction */
  ServerPage *pPgFirst;           /* First (oldest) in list of pages */
  ServerPage *pPgLast;            /* Last (newest) in list of pages */
  ServerPage *apPg[HMA_HASH_SIZE];


};

/*
** Once instance for each client connection open on a server mode database
** in this process.
*/
struct Server {
................................................................................
*/
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd){
  ServerDb *pDb = p->pDb;

  serverEnterMutex();
  pDb->nClient--;
  if( pDb->nClient==0 ){

    ServerDb **pp;
    serverShutdownDatabase(pDb);
    for(pp=&g_server.pDb; *pp!=pDb; pp=&((*pp)->pNext));
    *pp = pDb->pNext;
    sqlite3_mutex_free(pDb->mutex);




    sqlite3_free(pDb);
  }
  serverLeaveMutex();

  sqlite3_free(p->aLock);
  sqlite3_free(p);
}
................................................................................
** End a transaction (and release all locks).
*/
int sqlite3ServerEnd(Server *p){
  int rc = SQLITE_OK;
  if( p->eTrans!=SERVER_TRANS_NONE ){
    Server **pp;
    ServerDb *pDb = p->pDb;
    ServerPage *pFree = 0;
    ServerPage *pPg = 0;

    sqlite3_mutex_enter(pDb->mutex);

    if( p->eTrans==SERVER_TRANS_READONLY ){
      /* Remove the connection from the readers list */
      for(pp=&pDb->pReader; *pp!=p; pp = &((*pp)->pNext));
................................................................................
      }
    }

    /* See if it is possible to free any ServerPage records. If so, remove
    ** them from the linked list and hash table, but do not call sqlite3_free()
    ** on them until the mutex has been released.  */
    if( pDb->pPgFirst ){

      Server *pIter;
      int iOldest = 0x7FFFFFFF;
      for(pIter=pDb->pReader; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }
      for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }

      pFree = pDb->pPgFirst;
      for(pPg=pDb->pPgFirst; pPg && pPg->iCommitId<iOldest; pPg=pPg->pNext){
        if( pPg->pHashPrev ){
          pPg->pHashPrev->pHashNext = pPg->pHashNext;
        }else{
          int iHash = pPg->pgno % HMA_HASH_SIZE;
          assert( pDb->apPg[iHash]==pPg );
          pDb->apPg[iHash] = pPg->pHashNext;
        }
        if( pPg->pHashNext ){
          pPg->pHashNext->pHashPrev = pPg->pHashPrev;
        }

      }







      if( pPg==0 ){
        pDb->pPgFirst = pDb->pPgLast = 0;
      }else{
        pDb->pPgFirst = pPg;
      }
    }

    sqlite3_mutex_leave(pDb->mutex);

    /* Call sqlite3_free() on any pages that were unlinked from the hash
    ** table above. */
    while( pFree && pFree!=pPg ){
      ServerPage *pNext = pFree->pNext;
      sqlite3_free(pFree);
      pFree = pNext;
    }

    p->pNext = 0;
    p->eTrans = SERVER_TRANS_NONE;
    p->iTransId = -1;
    p->iCommitId = 0;
  }
  return rc;
}
................................................................................
    u32 *pSlot = &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];
    sqlite3_mutex_enter(pDb->mutex);
    serverIncrSlowReader(pSlot, -1);
    assert( slotGetSlowReaders(*pSlot)>=0 );
    sqlite3_mutex_leave(pDb->mutex);
  }
}














#endif /* ifdef SQLITE_SERVER_EDITION */







>
>







 







>





>
>
>
>







 







<







 







>









<











>

>
>
>
>
>
>
>









<
<
<
<
<
<
<
<







 








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

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
...
412
413
414
415
416
417
418

419
420
421
422
423
424
425
...
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484








485
486
487
488
489
490
491
...
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694

  int iNextCommit;                /* Commit id for next pre-commit call */ 
  Server *pCommit;                /* List of connections currently commiting */
  Server *pReader;                /* Connections in slower-reader transaction */
  ServerPage *pPgFirst;           /* First (oldest) in list of pages */
  ServerPage *pPgLast;            /* Last (newest) in list of pages */
  ServerPage *apPg[HMA_HASH_SIZE];

  ServerPage *pFree;              /* List of free page buffers */
};

/*
** Once instance for each client connection open on a server mode database
** in this process.
*/
struct Server {
................................................................................
*/
void sqlite3ServerDisconnect(Server *p, sqlite3_file *dbfd){
  ServerDb *pDb = p->pDb;

  serverEnterMutex();
  pDb->nClient--;
  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();

  sqlite3_free(p->aLock);
  sqlite3_free(p);
}
................................................................................
** End a transaction (and release all locks).
*/
int sqlite3ServerEnd(Server *p){
  int rc = SQLITE_OK;
  if( p->eTrans!=SERVER_TRANS_NONE ){
    Server **pp;
    ServerDb *pDb = p->pDb;

    ServerPage *pPg = 0;

    sqlite3_mutex_enter(pDb->mutex);

    if( p->eTrans==SERVER_TRANS_READONLY ){
      /* Remove the connection from the readers list */
      for(pp=&pDb->pReader; *pp!=p; pp = &((*pp)->pNext));
................................................................................
      }
    }

    /* See if it is possible to free any ServerPage records. If so, remove
    ** them from the linked list and hash table, but do not call sqlite3_free()
    ** on them until the mutex has been released.  */
    if( pDb->pPgFirst ){
      ServerPage *pLast = 0;
      Server *pIter;
      int iOldest = 0x7FFFFFFF;
      for(pIter=pDb->pReader; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }
      for(pIter=pDb->pCommit; pIter; pIter=pIter->pNext){
        iOldest = MIN(iOldest, pIter->iCommitId);
      }


      for(pPg=pDb->pPgFirst; pPg && pPg->iCommitId<iOldest; pPg=pPg->pNext){
        if( pPg->pHashPrev ){
          pPg->pHashPrev->pHashNext = pPg->pHashNext;
        }else{
          int iHash = pPg->pgno % HMA_HASH_SIZE;
          assert( pDb->apPg[iHash]==pPg );
          pDb->apPg[iHash] = pPg->pHashNext;
        }
        if( pPg->pHashNext ){
          pPg->pHashNext->pHashPrev = pPg->pHashPrev;
        }
        pLast = pPg;
      }

      if( pLast ){
        assert( pLast->pNext==pPg );
        pLast->pNext = pDb->pFree;
        pDb->pFree = pDb->pPgFirst;
      }

      if( pPg==0 ){
        pDb->pPgFirst = pDb->pPgLast = 0;
      }else{
        pDb->pPgFirst = pPg;
      }
    }

    sqlite3_mutex_leave(pDb->mutex);









    p->pNext = 0;
    p->eTrans = SERVER_TRANS_NONE;
    p->iTransId = -1;
    p->iCommitId = 0;
  }
  return rc;
}
................................................................................
    u32 *pSlot = &pDb->aSlot[pgno % HMA_PAGELOCK_SLOTS];
    sqlite3_mutex_enter(pDb->mutex);
    serverIncrSlowReader(pSlot, -1);
    assert( slotGetSlowReaders(*pSlot)>=0 );
    sqlite3_mutex_leave(pDb->mutex);
  }
}

ServerPage *sqlite3ServerBuffer(Server *p){
  ServerDb *pDb = p->pDb;
  ServerPage *pRet = 0;
  sqlite3_mutex_enter(pDb->mutex);
  if( pDb->pFree ){
    pRet = pDb->pFree;
    pDb->pFree = pRet->pNext;
    pRet->pNext = 0;
  }
  sqlite3_mutex_leave(pDb->mutex);
  return pRet;
}

#endif /* ifdef SQLITE_SERVER_EDITION */

Changes to src/server.h.

40
41
42
43
44
45
46



47
48
49
50
51
52

int sqlite3ServerReleaseWriteLocks(Server *p);

int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock);

int sqlite3ServerHasLock(Server *p, Pgno pgno, int bWrite);




void sqlite3ServerReadPage(Server*, Pgno, u8**);
void sqlite3ServerEndReadPage(Server*, Pgno);

#endif /* SQLITE_SERVER_H */
#endif /* SQLITE_SERVER_EDITION */








>
>
>






40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

int sqlite3ServerReleaseWriteLocks(Server *p);

int sqlite3ServerLock(Server *p, Pgno pgno, int bWrite, int bBlock);

int sqlite3ServerHasLock(Server *p, Pgno pgno, int bWrite);

ServerPage *sqlite3ServerBuffer(Server*);

/* For "BEGIN READONLY" clients. */
void sqlite3ServerReadPage(Server*, Pgno, u8**);
void sqlite3ServerEndReadPage(Server*, Pgno);

#endif /* SQLITE_SERVER_H */
#endif /* SQLITE_SERVER_EDITION */

Changes to tool/tserver.c.

58
59
60
61
62
63
64


65
66
67
68
69
70
71
...
295
296
297
298
299
300
301
302


303
304
305
306
307
308
309
...
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
#include <sys/time.h>
#include <unistd.h>

#include "sqlite3.h"

/* Database used by this server */
static char *zDatabaseName = 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 */
................................................................................
  int rc = SQLITE_OK;

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

  ctx.fd = (int)(intptr_t)pArg;
  ctx.nRepeat = 1;
  rc = sqlite3_open(zDatabaseName, &ctx.db);


  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
................................................................................
  fprintf(stdout, "Client %d disconnects\n", ctx.fd);
  close(ctx.fd);
  clear_sql(&ctx);
  sqlite3_free(ctx.apPrepare);
  sqlite3_close(ctx.db);
  return 0;
} 






int main(int argc, char *argv[]) {
  sqlite3 *db;
  int sfd;
  int rc;
  int yes = 1;
  struct sockaddr_in server;

  /* Ignore SIGPIPE. Otherwise the server exits if a client disconnects
  ** abruptly.  */
  signal(SIGPIPE, SIG_IGN);

  if( argc!=2 ){
    fprintf(stderr, "Usage: %s DATABASE\n", argv[0]);
    return 1;
  }





  zDatabaseName = argv[1];

  rc = sqlite3_open(zDatabaseName, &db);


  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);
  if( rc!=SQLITE_OK ){







>
>







 







|
>
>







 







>
>
>
>
>












|
|
<

>
>
>
>
>
|

|
>
>







58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
...
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
...
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
412
413
414
415
416
417
418
419
420
421
422
#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 */
................................................................................
  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
................................................................................
  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);
}

int main(int argc, char *argv[]) {
  sqlite3 *db;
  int sfd;
  int rc;
  int yes = 1;
  struct sockaddr_in server;

  /* Ignore SIGPIPE. Otherwise the server exits if a client disconnects
  ** abruptly.  */
  signal(SIGPIPE, SIG_IGN);

  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);
  if( rc!=SQLITE_OK ){