Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Add further instrumentation to the bc_test1.c test app. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | begin-concurrent |
Files: | files | file ages | folders |
SHA1: |
5528de4a53c19557798b6169e1d738f1 |
User & Date: | dan 2016-05-26 20:52:15.750 |
Context
2016-05-30
| ||
05:45 | Minor tweaks to the bc_test1 test program. (check-in: d0d0bab4e9 user: dan tags: begin-concurrent) | |
2016-05-26
| ||
20:52 | Add further instrumentation to the bc_test1.c test app. (check-in: 5528de4a53 user: dan tags: begin-concurrent) | |
2016-05-21
| ||
18:50 | Use pthreads mutexes and conditions to synchronize threads in bc_test1. (check-in: f33aa76f07 user: dan tags: begin-concurrent) | |
Changes
Changes to test/bc_test1.c.
︙ | ︙ | |||
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #include <sqlite3.h> #include <stdlib.h> #include <stddef.h> #include "tt3_core.c" typedef struct Config Config; struct Config { int nIPT; /* --inserts-per-transaction */ int nThread; /* --threads */ int nSecond; /* --seconds */ int bMutex; /* --mutex */ int nAutoCkpt; /* --autockpt */ int bRm; /* --rm */ pthread_cond_t cond; pthread_mutex_t mutex; int nCondWait; /* Number of threads waiting on hCond */ sqlite3_vfs *pVfs; }; | > > > > > > > > > > > > > > > > > > > > > > > > > > < < < < < < > | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <sqlite3.h> #include <stdlib.h> #include <stddef.h> #include "tt3_core.c" #ifdef USE_OSINST # include "../src/test_osinst.c" #else # define vfslog_time() 0 #endif typedef struct Config Config; typedef struct ThreadCtx ThreadCtx; #define THREAD_TIME_INSERT 0 #define THREAD_TIME_COMMIT 1 #define THREAD_TIME_ROLLBACK 2 #define THREAD_TIME_WRITER 3 #define THREAD_TIME_CKPT 4 struct ThreadCtx { Config *pConfig; Sqlite *pDb; Error *pErr; sqlite3_int64 aTime[5]; }; struct Config { int nIPT; /* --inserts-per-transaction */ int nThread; /* --threads */ int nSecond; /* --seconds */ int bMutex; /* --mutex */ int nAutoCkpt; /* --autockpt */ int bRm; /* --rm */ int bClearCache; /* --clear-cache */ int nMmap; /* mmap limit in MB */ char *zFile; int bOsinst; /* True to use osinst */ ThreadCtx *aCtx; /* Array of size nThread */ pthread_cond_t cond; pthread_mutex_t mutex; int nCondWait; /* Number of threads waiting on hCond */ sqlite3_vfs *pVfs; }; typedef struct VfsWrapperFd VfsWrapperFd; struct VfsWrapperFd { sqlite3_file base; /* Base class */ int bWriter; /* True if holding shm WRITER lock */ int iTid; Config *pConfig; sqlite3_file *pFd; /* Underlying file descriptor */ }; /* Methods of the wrapper VFS */ static int vfsWrapOpen(sqlite3_vfs*, const char*, sqlite3_file*, int, int*); static int vfsWrapDelete(sqlite3_vfs*, const char*, int); |
︙ | ︙ | |||
102 103 104 105 106 107 108 109 110 111 112 113 114 115 | vfsWrapShmMap, vfsWrapShmLock, vfsWrapShmBarrier, vfsWrapShmUnmap, vfsWrapFetch, vfsWrapUnfetch }; Config *pConfig = (Config*)pVfs->pAppData; VfsWrapperFd *pWrapper = (VfsWrapperFd*)pFd; int rc; pWrapper->pFd = (sqlite3_file*)&pWrapper[1]; pWrapper->pConfig = pConfig; rc = pConfig->pVfs->xOpen(pConfig->pVfs, zName, pWrapper->pFd, flags, fout); if( rc==SQLITE_OK ){ pWrapper->base.pMethods = &methods; } | > > > > > | 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | vfsWrapShmMap, vfsWrapShmLock, vfsWrapShmBarrier, vfsWrapShmUnmap, vfsWrapFetch, vfsWrapUnfetch }; Config *pConfig = (Config*)pVfs->pAppData; VfsWrapperFd *pWrapper = (VfsWrapperFd*)pFd; int rc; memset(pWrapper, 0, sizeof(VfsWrapperFd)); if( flags & SQLITE_OPEN_MAIN_DB ){ pWrapper->iTid = (int)sqlite3_uri_int64(zName, "tid", 0); } pWrapper->pFd = (sqlite3_file*)&pWrapper[1]; pWrapper->pConfig = pConfig; rc = pConfig->pVfs->xOpen(pConfig->pVfs, zName, pWrapper->pFd, flags, fout); if( rc==SQLITE_OK ){ pWrapper->base.pMethods = &methods; } |
︙ | ︙ | |||
254 255 256 257 258 259 260 | if( (offset==0 && n==1) && (flags & SQLITE_SHM_LOCK) && (flags & SQLITE_SHM_EXCLUSIVE) ){ pthread_mutex_lock(&pConfig->mutex); pWrapper->bWriter = 1; bMutex = 1; | > > > | < < < < | > > > > > > > | 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 | if( (offset==0 && n==1) && (flags & SQLITE_SHM_LOCK) && (flags & SQLITE_SHM_EXCLUSIVE) ){ pthread_mutex_lock(&pConfig->mutex); pWrapper->bWriter = 1; bMutex = 1; if( pWrapper->iTid ){ sqlite3_int64 t = vfslog_time(); pConfig->aCtx[pWrapper->iTid-1].aTime[THREAD_TIME_WRITER] -= t; } } rc = pWrapper->pFd->pMethods->xShmLock(pWrapper->pFd, offset, n, flags); if( (rc!=SQLITE_OK && bMutex) || (offset==0 && (flags & SQLITE_SHM_UNLOCK) && pWrapper->bWriter) ){ assert( pWrapper->bWriter ); pthread_mutex_unlock(&pConfig->mutex); pWrapper->bWriter = 0; if( pWrapper->iTid ){ sqlite3_int64 t = vfslog_time(); pConfig->aCtx[pWrapper->iTid-1].aTime[THREAD_TIME_WRITER] += t; } } return rc; } static void vfsWrapShmBarrier(sqlite3_file *pFd){ VfsWrapperFd *pWrapper = (VfsWrapperFd*)pFd; return pWrapper->pFd->pMethods->xShmBarrier(pWrapper->pFd); |
︙ | ︙ | |||
313 314 315 316 317 318 319 | } /* ** Wal hook used by connections in thread_main(). */ static int thread_wal_hook( | | | | > > < > > > > > > > > > > > > | > > > > > > > | | > > | | | | > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > | | | > | | 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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 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 486 487 488 489 490 491 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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 | } /* ** Wal hook used by connections in thread_main(). */ static int thread_wal_hook( void *pArg, /* Pointer to ThreadCtx object */ sqlite3 *db, const char *zDb, int nFrame ){ ThreadCtx *pCtx = (ThreadCtx*)pArg; Config *pConfig = pCtx->pConfig; if( pConfig->nAutoCkpt && nFrame>=pConfig->nAutoCkpt ){ pCtx->aTime[THREAD_TIME_CKPT] -= vfslog_time(); pthread_mutex_lock(&pConfig->mutex); if( pConfig->nCondWait>=0 ){ pConfig->nCondWait++; if( pConfig->nCondWait==pConfig->nThread ){ execsql(pCtx->pErr, pCtx->pDb, "PRAGMA wal_checkpoint"); pthread_cond_broadcast(&pConfig->cond); }else{ pthread_cond_wait(&pConfig->cond, &pConfig->mutex); } pConfig->nCondWait--; } pthread_mutex_unlock(&pConfig->mutex); pCtx->aTime[THREAD_TIME_CKPT] += vfslog_time(); } return SQLITE_OK; } static char *thread_main(int iTid, void *pArg){ Config *pConfig = (Config*)pArg; Error err = {0}; /* Error code and message */ Sqlite db = {0}; /* SQLite database connection */ int nAttempt = 0; /* Attempted transactions */ int nCommit = 0; /* Successful transactions */ int j; ThreadCtx *pCtx = &pConfig->aCtx[iTid-1]; char *zUri = 0; #ifdef USE_OSINST char *zOsinstName = 0; char *zLogName = 0; if( pConfig->bOsinst ){ zOsinstName = sqlite3_mprintf("osinst%d", iTid); zLogName = sqlite3_mprintf("bc_test1.log.%d.%d", (int)getpid(), iTid); zUri = sqlite3_mprintf( "file:%s?vfs=%s&tid=%d", pConfig->zFile, zOsinstName, iTid ); sqlite3_vfslog_new(zOsinstName, 0, zLogName); opendb(&err, &db, zUri, 0); }else #endif { zUri = sqlite3_mprintf("file:%s?tid=%d", pConfig->zFile, iTid); opendb(&err, &db, zUri, 0); } sqlite3_busy_handler(db.db, 0, 0); sql_script_printf(&err, &db, "PRAGMA wal_autocheckpoint = 0;" "PRAGMA synchronous = 0;" "PRAGMA mmap_limit = %lld;", (i64)(pConfig->nMmap) * 1024 * 1024 ); pCtx->pConfig = pConfig; pCtx->pErr = &err; pCtx->pDb = &db; sqlite3_wal_hook(db.db, thread_wal_hook, (void*)pCtx); while( !timetostop(&err) ){ execsql(&err, &db, "BEGIN CONCURRENT"); pCtx->aTime[THREAD_TIME_INSERT] -= vfslog_time(); for(j=0; j<pConfig->nIPT; j++){ execsql(&err, &db, "INSERT INTO t1 VALUES" "(randomblob(10), randomblob(20), randomblob(30), randomblob(200))" ); } pCtx->aTime[THREAD_TIME_INSERT] += vfslog_time(); pCtx->aTime[THREAD_TIME_COMMIT] -= vfslog_time(); execsql(&err, &db, "COMMIT"); pCtx->aTime[THREAD_TIME_COMMIT] += vfslog_time(); pCtx->aTime[THREAD_TIME_ROLLBACK] -= vfslog_time(); nAttempt++; if( err.rc==SQLITE_OK ){ nCommit++; }else{ clear_error(&err, SQLITE_BUSY); execsql(&err, &db, "ROLLBACK"); } pCtx->aTime[THREAD_TIME_ROLLBACK] += vfslog_time(); if( pConfig->bClearCache ){ sqlite3_db_release_memory(db.db); } } closedb(&err, &db); #ifdef USE_OSINST if( pConfig->bOsinst ){ sqlite3_vfslog_finalize(zOsinstName); sqlite3_free(zOsinstName); sqlite3_free(zLogName); } #endif sqlite3_free(zUri); pthread_mutex_lock(&pConfig->mutex); pConfig->nCondWait = -1; pthread_cond_broadcast(&pConfig->cond); pthread_mutex_unlock(&pConfig->mutex); return sqlite3_mprintf("commits: %d/%d insert: %dms" " commit: %dms" " rollback: %dms" " writer: %dms" " checkpoint: %dms", nCommit, nAttempt, (int)(pCtx->aTime[THREAD_TIME_INSERT]/1000), (int)(pCtx->aTime[THREAD_TIME_COMMIT]/1000), (int)(pCtx->aTime[THREAD_TIME_ROLLBACK]/1000), (int)(pCtx->aTime[THREAD_TIME_WRITER]/1000), (int)(pCtx->aTime[THREAD_TIME_CKPT]/1000) ); } int main(int argc, const char **argv){ Error err = {0}; /* Error code and message */ Sqlite db = {0}; /* SQLite database connection */ Threadset threads = {0}; /* Test threads */ Config conf = {5, 3, 5}; int i; CmdlineArg apArg[] = { { "--seconds", CMDLINE_INT, offsetof(Config, nSecond) }, { "--inserts", CMDLINE_INT, offsetof(Config, nIPT) }, { "--threads", CMDLINE_INT, offsetof(Config, nThread) }, { "--mutex", CMDLINE_BOOL, offsetof(Config, bMutex) }, { "--rm", CMDLINE_BOOL, offsetof(Config, bRm) }, { "--autockpt",CMDLINE_INT, offsetof(Config, nAutoCkpt) }, { "--mmap", CMDLINE_INT, offsetof(Config, nMmap) }, { "--clear-cache", CMDLINE_BOOL, offsetof(Config, bClearCache) }, { "--file", CMDLINE_STRING, offsetof(Config, zFile) }, { "--osinst", CMDLINE_BOOL, offsetof(Config, bOsinst) }, { 0, 0, 0 } }; cmdline_process(apArg, argc, argv, (void*)&conf); if( err.rc==SQLITE_OK ){ char *z = cmdline_construct(apArg, (void*)&conf); printf("With: %s\n", z); sqlite3_free(z); } if( conf.zFile==0 ){ conf.zFile = "xyz.db"; } /* Create the special VFS - "wrapper". And the mutex and condition ** variable. */ create_vfs(&conf); pthread_mutex_init(&conf.mutex, 0); pthread_cond_init(&conf.cond, 0); conf.aCtx = sqlite3_malloc(sizeof(ThreadCtx) * conf.nThread); memset(conf.aCtx, 0, sizeof(ThreadCtx) * conf.nThread); /* Ensure the schema has been created */ opendb(&err, &db, conf.zFile, conf.bRm); sql_script(&err, &db, "PRAGMA journal_mode = wal;" "CREATE TABLE IF NOT EXISTS t1(a PRIMARY KEY, b, c, d) WITHOUT ROWID;" "CREATE INDEX IF NOT EXISTS t1b ON t1(b);" "CREATE INDEX IF NOT EXISTS t1c ON t1(c);" ); setstoptime(&err, conf.nSecond*1000); if( conf.nThread==1 ){ char *z = thread_main(1, (void*)&conf); printf("Thread 0 says: %s\n", (z==0 ? "..." : z)); fflush(stdout); }else{ for(i=0; i<conf.nThread; i++){ launch_thread(&err, &threads, thread_main, (void*)&conf); } join_all_threads(&err, &threads); } if( err.rc==SQLITE_OK ){ printf("Database is %dK\n", (int)(filesize(&err, conf.zFile) / 1024)); } if( err.rc==SQLITE_OK ){ char *zWal = sqlite3_mprintf("%s-wal", conf.zFile); printf("Wal file is %dK\n", (int)(filesize(&err, zWal) / 1024)); } closedb(&err, &db); print_and_free_err(&err); return 0; } |
Changes to test/tt3_core.c.
︙ | ︙ | |||
65 66 67 68 69 70 71 | ** End of test code/infrastructure interface macros. *************************************************************************/ /************************************************************************ ** Start of command line processing utilities. */ | | | > | 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | ** End of test code/infrastructure interface macros. *************************************************************************/ /************************************************************************ ** Start of command line processing utilities. */ #define CMDLINE_INT 1 #define CMDLINE_BOOL 2 #define CMDLINE_STRING 3 typedef struct CmdlineArg CmdlineArg; struct CmdlineArg { const char *zSwitch; int eType; int iOffset; }; |
︙ | ︙ | |||
94 95 96 97 98 99 100 101 102 103 104 105 106 107 | int i; fprintf(stderr, "Usage: %s SWITCHES\n", zPrg); fprintf(stderr, "\n"); fprintf(stderr, "where switches are\n"); for(i=0; apArg[i].zSwitch; i++){ const char *zExtra = ""; switch( apArg[i].eType ){ case CMDLINE_INT: zExtra = "N"; break; case CMDLINE_BOOL: zExtra = ""; break; default: zExtra = "???"; break; } fprintf(stderr, " %s %s\n", apArg[i].zSwitch, zExtra); | > | 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | int i; fprintf(stderr, "Usage: %s SWITCHES\n", zPrg); fprintf(stderr, "\n"); fprintf(stderr, "where switches are\n"); for(i=0; apArg[i].zSwitch; i++){ const char *zExtra = ""; switch( apArg[i].eType ){ case CMDLINE_STRING: zExtra = "STRING"; break; case CMDLINE_INT: zExtra = "N"; break; case CMDLINE_BOOL: zExtra = ""; break; default: zExtra = "???"; break; } fprintf(stderr, " %s %s\n", apArg[i].zSwitch, zExtra); |
︙ | ︙ | |||
116 117 118 119 120 121 122 123 124 125 126 127 128 129 | int iArg; for(iArg=0; apArg[iArg].zSwitch; iArg++){ const char *zSpace = (zRet ? " " : ""); CmdlineArg *pArg = &apArg[iArg]; switch( pArg->eType ){ case CMDLINE_INT: { zRet = sqlite3_mprintf("%z%s%s %d", zRet, zSpace, pArg->zSwitch, *(int*)(p + pArg->iOffset) ); break; }; | > > > > > > > > | 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | int iArg; for(iArg=0; apArg[iArg].zSwitch; iArg++){ const char *zSpace = (zRet ? " " : ""); CmdlineArg *pArg = &apArg[iArg]; switch( pArg->eType ){ case CMDLINE_STRING: { char *zVal = *(char**)(p + pArg->iOffset); if( zVal ){ zRet = sqlite3_mprintf("%z%s%s %s", zRet, zSpace, pArg->zSwitch,zVal); } break; }; case CMDLINE_INT: { zRet = sqlite3_mprintf("%z%s%s %d", zRet, zSpace, pArg->zSwitch, *(int*)(p + pArg->iOffset) ); break; }; |
︙ | ︙ | |||
166 167 168 169 170 171 172 173 174 175 176 177 178 179 | case CMDLINE_INT: i++; if( i==argc ){ cmdline_error("option requires an argument: %s", z); } *(int*)(p + apArg[iArg].iOffset) = atoi(argv[i]); break; case CMDLINE_BOOL: *(int*)(p + apArg[iArg].iOffset) = 1; break; default: assert( 0 ); | > > > > > > > > | 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | case CMDLINE_INT: i++; if( i==argc ){ cmdline_error("option requires an argument: %s", z); } *(int*)(p + apArg[iArg].iOffset) = atoi(argv[i]); break; case CMDLINE_STRING: i++; if( i==argc ){ cmdline_error("option requires an argument: %s", z); } *(char**)(p + apArg[iArg].iOffset) = sqlite3_mprintf("%s", argv[i]); break; case CMDLINE_BOOL: *(int*)(p + apArg[iArg].iOffset) = 1; break; default: assert( 0 ); |
︙ | ︙ |