/ Check-in [78141d0a]
Login

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

Overview
Comment:Add the SQLITE_CONFIG_MMAP_LIMIT configuration option for overriding the SQLITE_DEFAULT_MMAP_LIMIT compile-time setting. Enhance "PRAGMA mmap_limit" so that without a specific database name, it sets the limit on all database files and changes the default for any future databases that might be added using ATTACH.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | experimental-mmap
Files: files | file ages | folders
SHA1: 78141d0a16dd1d56b575fccd149de7fa789cb06c
User & Date: drh 2013-04-01 22:38:06
Context
2013-04-01
22:42
Remove a debugging printf() accidently left in the previous check-in. check-in: 8198cdd8 user: drh tags: experimental-mmap
22:38
Add the SQLITE_CONFIG_MMAP_LIMIT configuration option for overriding the SQLITE_DEFAULT_MMAP_LIMIT compile-time setting. Enhance "PRAGMA mmap_limit" so that without a specific database name, it sets the limit on all database files and changes the default for any future databases that might be added using ATTACH. check-in: 78141d0a user: drh tags: experimental-mmap
18:25
Add a comment to xUnfetch on windows to say that the call to winUnmapfile() might be unnecessary. check-in: e3c63c31 user: drh tags: experimental-mmap
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/btree.c.

  1860   1860       if( pBt==0 ){
  1861   1861         rc = SQLITE_NOMEM;
  1862   1862         goto btree_open_out;
  1863   1863       }
  1864   1864       rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
  1865   1865                             EXTRA_SIZE, flags, vfsFlags, pageReinit);
  1866   1866       if( rc==SQLITE_OK ){
         1867  +      sqlite3PagerSetMmapLimit(pBt->pPager, db->mxMmap);
  1867   1868         rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
  1868   1869       }
  1869   1870       if( rc!=SQLITE_OK ){
  1870   1871         goto btree_open_out;
  1871   1872       }
  1872   1873       pBt->openFlags = (u8)flags;
  1873   1874       pBt->db = db;

Changes to src/global.c.

   152    152      500,                       /* nLookaside */
   153    153      {0,0,0,0,0,0,0,0},         /* m */
   154    154      {0,0,0,0,0,0,0,0,0},       /* mutex */
   155    155      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
   156    156      (void*)0,                  /* pHeap */
   157    157      0,                         /* nHeap */
   158    158      0, 0,                      /* mnHeap, mxHeap */
          159  +   SQLITE_DEFAULT_MMAP_LIMIT, /* mxMmap */                       
   159    160      (void*)0,                  /* pScratch */
   160    161      0,                         /* szScratch */
   161    162      0,                         /* nScratch */
   162    163      (void*)0,                  /* pPage */
   163    164      0,                         /* szPage */
   164    165      0,                         /* nPage */
   165    166      0,                         /* mxParserStack */

Changes to src/main.c.

   491    491       case SQLITE_CONFIG_SQLLOG: {
   492    492         typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
   493    493         sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
   494    494         sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
   495    495         break;
   496    496       }
   497    497   #endif
          498  +
          499  +    case SQLITE_CONFIG_MMAP_LIMIT: {
          500  +      sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
          501  +      if( mxMmap<0 ) mxMmap = SQLITE_DEFAULT_MMAP_LIMIT;
          502  +      sqlite3GlobalConfig.mxMmap = mxMmap;
          503  +      break;
          504  +    }
   498    505   
   499    506       default: {
   500    507         rc = SQLITE_ERROR;
   501    508         break;
   502    509       }
   503    510     }
   504    511     va_end(ap);
................................................................................
  2312   2319     db->magic = SQLITE_MAGIC_BUSY;
  2313   2320     db->aDb = db->aDbStatic;
  2314   2321   
  2315   2322     assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
  2316   2323     memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
  2317   2324     db->autoCommit = 1;
  2318   2325     db->nextAutovac = -1;
         2326  +  db->mxMmap = sqlite3GlobalConfig.mxMmap;
  2319   2327     db->nextPagesize = 0;
  2320   2328     db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
  2321   2329   #if SQLITE_DEFAULT_FILE_FORMAT<4
  2322   2330                    | SQLITE_LegacyFileFmt
  2323   2331   #endif
  2324   2332   #ifdef SQLITE_ENABLE_LOAD_EXTENSION
  2325   2333                    | SQLITE_LoadExtension

Changes to src/os_unix.c.

  3707   3707           unixGetTempname(pFile->pVfs->mxPathname, zTFile);
  3708   3708           *(char**)pArg = zTFile;
  3709   3709         }
  3710   3710         return SQLITE_OK;
  3711   3711       }
  3712   3712       case SQLITE_FCNTL_MMAP_LIMIT: {
  3713   3713         pFile->mmapLimit = *(i64*)pArg;
         3714  +printf("MMAP-LIMIT(%s) -> %lld\n", pFile->zPath, pFile->mmapLimit);
  3714   3715         return SQLITE_OK;
  3715   3716       }
  3716   3717   #ifdef SQLITE_DEBUG
  3717   3718       /* The pager calls this method to signal that it has done
  3718   3719       ** a rollback and that the database is therefore unchanged and
  3719   3720       ** it hence it is OK for the transaction change counter to be
  3720   3721       ** unchanged.

Changes to src/pager.c.

  4745   4745     }else if( memDb ){
  4746   4746       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
  4747   4747     }
  4748   4748     /* pPager->xBusyHandler = 0; */
  4749   4749     /* pPager->pBusyHandlerArg = 0; */
  4750   4750     pPager->xReiniter = xReinit;
  4751   4751     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
  4752         -  pPager->mxMmap = SQLITE_DEFAULT_MMAP_LIMIT;
         4752  +  /* pPager->mxMmap = SQLITE_DEFAULT_MMAP_LIMIT // will be set by btree.c */
  4753   4753   
  4754   4754     *ppPager = pPager;
  4755   4755     return SQLITE_OK;
  4756   4756   }
  4757   4757   
  4758   4758   
  4759   4759   

Changes to src/pragma.c.

   746    746   
   747    747     /*
   748    748     **  PRAGMA [database.]mmap_limit(N)
   749    749     **
   750    750     ** Used to set mapping size limit. The mapping size limit is
   751    751     ** used to limit the aggregate size of all memory mapped regions of the
   752    752     ** database file. If this parameter is set to zero, then memory mapping
   753         -  ** is not used at all. The parameter N is measured in bytes.
          753  +  ** is not used at all.  If N is negative, then the default memory map
          754  +  ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_LIMIT) is set.
          755  +  ** The parameter N is measured in bytes.
   754    756     **
   755    757     ** This value is advisory.  The underlying VFS is free to memory map
   756    758     ** as little or as much as it wants.  Except, if N is set to 0 then the
   757    759     ** upper layers will never invoke the xFetch interfaces to the VFS.
   758    760     */
   759    761     if( sqlite3StrICmp(zLeft,"mmap_limit")==0 ){
   760    762       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
   761    763       if( zRight ){
   762    764         sqlite3_int64 size;
          765  +      int ii;
   763    766         sqlite3Atoi64(zRight, &size, 1000, SQLITE_UTF8);
   764         -      sqlite3BtreeSetMmapLimit(pDb->pBt, size);
          767  +      if( size<0 ) size = sqlite3GlobalConfig.mxMmap;
          768  +      if( pId2->n==0 ) db->mxMmap = size;
          769  +      for(ii=db->nDb-1; ii>=0; ii--){
          770  +        if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
          771  +          sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, size);
          772  +        }
          773  +      }
   765    774       }
   766    775     }else
   767    776   
   768    777     /*
   769    778     **   PRAGMA temp_store
   770    779     **   PRAGMA temp_store = "default"|"memory"|"file"
   771    780     **

Changes to src/sqlite.h.in.

  1633   1633   ** fourth parameter. If the fourth parameter is 0, then the database connection
  1634   1634   ** passed as the second argument has just been opened. The third argument
  1635   1635   ** points to a buffer containing the name of the main database file. If the
  1636   1636   ** fourth parameter is 1, then the SQL statement that the third parameter
  1637   1637   ** points to has just been executed. Or, if the fourth parameter is 2, then
  1638   1638   ** the connection being passed as the second parameter is being closed. The
  1639   1639   ** third parameter is passed NULL In this case.
         1640  +**
         1641  +** [[SQLITE_CONFIG_MMAP_LIMIT]]
         1642  +** <dt>SQLITE_CONFIG_MMAP_LIMIT
         1643  +** <dd>The sole argument should be a 64-bit integer (an sqlite3_int64) that
         1644  +** is the default maximum number of bytes of process address space that
         1645  +** should be used for accessing each database file using memory mapping.
         1646  +** The default setting can be overridden by each database connection using
         1647  +** either the [PRAGMA mmap_limit] command or the "mmaplimit" query parameter
         1648  +** on the [URI filename] when opening the databaes file or by using the
         1649  +** [SQLITE_FCNTL_MMAP_LIMIT] file control.  The value set here overrides the
         1650  +** compile-time default that is set using the [SQLITE_DEFAULT_MMAP_LIMIT]
         1651  +** compile-time option.  If the argument to this option is negative, then
         1652  +** the memory map limit is set to the compile-time default.
  1640   1653   ** </dl>
  1641   1654   */
  1642   1655   #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
  1643   1656   #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
  1644   1657   #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
  1645   1658   #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
  1646   1659   #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
................................................................................
  1656   1669   #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1657   1670   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1658   1671   #define SQLITE_CONFIG_URI          17  /* int */
  1659   1672   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1660   1673   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  1661   1674   #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  1662   1675   #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
         1676  +#define SQLITE_CONFIG_MMAP_LIMIT   22  /* sqlite3_int64 */
  1663   1677   
  1664   1678   /*
  1665   1679   ** CAPI3REF: Database Connection Configuration Options
  1666   1680   **
  1667   1681   ** These constants are the available integer configuration options that
  1668   1682   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1669   1683   **

Changes to src/sqliteInt.h.

   830    830     struct Vdbe *pVdbe;           /* List of active virtual machines */
   831    831     CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
   832    832     sqlite3_mutex *mutex;         /* Connection mutex */
   833    833     Db *aDb;                      /* All backends */
   834    834     int nDb;                      /* Number of backends currently in use */
   835    835     int flags;                    /* Miscellaneous flags. See below */
   836    836     i64 lastRowid;                /* ROWID of most recent insert (see above) */
          837  +  i64 mxMmap;                   /* Default mmap_limit setting */
   837    838     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
   838    839     int errCode;                  /* Most recent error code (SQLITE_*) */
   839    840     int errMask;                  /* & result codes with this before returning */
   840    841     u16 dbOptFlags;               /* Flags to enable/disable optimizations */
   841    842     u8 autoCommit;                /* The auto-commit flag. */
   842    843     u8 temp_store;                /* 1: file 2: memory 0: default */
   843    844     u8 mallocFailed;              /* True if we have seen a malloc failure */
................................................................................
  2501   2502     int nLookaside;                   /* Default lookaside buffer count */
  2502   2503     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  2503   2504     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  2504   2505     sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
  2505   2506     void *pHeap;                      /* Heap storage space */
  2506   2507     int nHeap;                        /* Size of pHeap[] */
  2507   2508     int mnReq, mxReq;                 /* Min and max heap requests sizes */
         2509  +  sqlite3_int64 mxMmap;             /* Maximum mmap() space per open file */
  2508   2510     void *pScratch;                   /* Scratch memory */
  2509   2511     int szScratch;                    /* Size of each scratch buffer */
  2510   2512     int nScratch;                     /* Number of scratch buffers */
  2511   2513     void *pPage;                      /* Page cache memory */
  2512   2514     int szPage;                       /* Size of each page in pPage[] */
  2513   2515     int nPage;                        /* Number of pages in pPage[] */
  2514   2516     int mxParserStack;                /* maximum depth of the parser stack */