/ Check-in [f213e133]
Login

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

Overview
Comment:Reduce the amount of memory taken up by WAL mmaped regions under Windows.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f213e133f6e69b0edd73d96142014bdcab9dfe41
User & Date: shaneh 2010-09-03 04:29:31
Context
2010-09-03
10:58
Add a few more subquery tests to e_expr.test. check-in: 189cba00 user: dan tags: trunk
04:29
Reduce the amount of memory taken up by WAL mmaped regions under Windows. check-in: f213e133 user: shaneh tags: trunk
03:32
Fix a discrepancy between the documented behavior of SQLITE_DBCONFIG_LOOKASIDE and what it actually does. Also add evidence marks on the DBCONFIG_LOOKASIDE implementation. check-in: f483be44 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_win.c.

  1208   1208   static int winDeviceCharacteristics(sqlite3_file *id){
  1209   1209     UNUSED_PARAMETER(id);
  1210   1210     return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
  1211   1211   }
  1212   1212   
  1213   1213   #ifndef SQLITE_OMIT_WAL
  1214   1214   
         1215  +/* 
         1216  +** Windows will only let you create file view mappings
         1217  +** on allocation size granularity boundaries.
         1218  +** During sqlite3_os_init() we do a GetSystemInfo()
         1219  +** to get the granularity size.
         1220  +*/
         1221  +SYSTEM_INFO winSysInfo;
         1222  +
  1215   1223   /*
  1216   1224   ** Helper functions to obtain and relinquish the global mutex. The
  1217   1225   ** global mutex is used to protect the winLockInfo objects used by 
  1218   1226   ** this file, all of which may be shared by multiple threads.
  1219   1227   **
  1220   1228   ** Function winShmMutexHeld() is used to assert() that the global mutex 
  1221   1229   ** is held when required. This function is only used as part of assert() 
................................................................................
  1376   1384   **
  1377   1385   ** This is not a VFS shared-memory method; it is a utility function called
  1378   1386   ** by VFS shared-memory methods.
  1379   1387   */
  1380   1388   static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
  1381   1389     winShmNode **pp;
  1382   1390     winShmNode *p;
         1391  +  BOOL bRc;
  1383   1392     assert( winShmMutexHeld() );
  1384   1393     pp = &winShmNodeList;
  1385   1394     while( (p = *pp)!=0 ){
  1386   1395       if( p->nRef==0 ){
  1387   1396         int i;
  1388   1397         if( p->mutex ) sqlite3_mutex_free(p->mutex);
  1389   1398         for(i=0; i<p->nRegion; i++){
  1390         -        UnmapViewOfFile(p->aRegion[i].pMap);
  1391         -        CloseHandle(p->aRegion[i].hMap);
         1399  +        bRc = UnmapViewOfFile(p->aRegion[i].pMap);
         1400  +        OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
         1401  +                 (int)GetCurrentProcessId(), i,
         1402  +                 bRc ? "ok" : "failed"));
         1403  +        bRc = CloseHandle(p->aRegion[i].hMap);
         1404  +        OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
         1405  +                 (int)GetCurrentProcessId(), i,
         1406  +                 bRc ? "ok" : "failed"));
  1392   1407         }
  1393   1408         if( p->hFile.h != INVALID_HANDLE_VALUE ){
  1394   1409           SimulateIOErrorBenign(1);
  1395   1410           winClose((sqlite3_file *)&p->hFile);
  1396   1411           SimulateIOErrorBenign(0);
  1397   1412         }
  1398   1413         if( deleteFlag ){
................................................................................
  1461   1476       winShmNodeList = pShmNode;
  1462   1477   
  1463   1478       pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  1464   1479       if( pShmNode->mutex==0 ){
  1465   1480         rc = SQLITE_NOMEM;
  1466   1481         goto shm_open_err;
  1467   1482       }
         1483  +
  1468   1484       rc = winOpen(pDbFd->pVfs,
  1469   1485                    pShmNode->zFilename,             /* Name of the file (UTF-8) */
  1470   1486                    (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
  1471   1487                    SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
  1472   1488                    0);
  1473   1489       if( SQLITE_OK!=rc ){
  1474   1490         rc = SQLITE_CANTOPEN_BKPT;
................................................................................
  1772   1788       while( pShmNode->nRegion<=iRegion ){
  1773   1789         HANDLE hMap;                /* file-mapping handle */
  1774   1790         void *pMap = 0;             /* Mapped memory region */
  1775   1791        
  1776   1792         hMap = CreateFileMapping(pShmNode->hFile.h, 
  1777   1793             NULL, PAGE_READWRITE, 0, nByte, NULL
  1778   1794         );
         1795  +      OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
         1796  +               (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
         1797  +               hMap ? "ok" : "failed"));
  1779   1798         if( hMap ){
         1799  +        int iOffset = pShmNode->nRegion*szRegion;
         1800  +        int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
  1780   1801           pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
  1781         -            0, 0, nByte
         1802  +            0, iOffset - iOffsetShift, szRegion + iOffsetShift
  1782   1803           );
         1804  +        OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
         1805  +                 (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
         1806  +                 pMap ? "ok" : "failed"));
  1783   1807         }
  1784   1808         if( !pMap ){
  1785   1809           pShmNode->lastErrno = GetLastError();
  1786   1810           rc = SQLITE_IOERR;
  1787   1811           if( hMap ) CloseHandle(hMap);
  1788   1812           goto shmpage_out;
  1789   1813         }
................................................................................
  1792   1816         pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
  1793   1817         pShmNode->nRegion++;
  1794   1818       }
  1795   1819     }
  1796   1820   
  1797   1821   shmpage_out:
  1798   1822     if( pShmNode->nRegion>iRegion ){
         1823  +    int iOffset = iRegion*szRegion;
         1824  +    int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
  1799   1825       char *p = (char *)pShmNode->aRegion[iRegion].pMap;
  1800         -    *pp = (void *)&p[iRegion*szRegion];
         1826  +    *pp = (void *)&p[iOffsetShift];
  1801   1827     }else{
  1802   1828       *pp = 0;
  1803   1829     }
  1804   1830     sqlite3_mutex_leave(pShmNode->mutex);
  1805   1831     return rc;
  1806   1832   }
  1807   1833   
................................................................................
  2733   2759       winDlClose,          /* xDlClose */
  2734   2760       winRandomness,       /* xRandomness */
  2735   2761       winSleep,            /* xSleep */
  2736   2762       winCurrentTime,      /* xCurrentTime */
  2737   2763       winGetLastError,     /* xGetLastError */
  2738   2764       winCurrentTimeInt64, /* xCurrentTimeInt64 */
  2739   2765     };
         2766  +
         2767  +#ifndef SQLITE_OMIT_WAL
         2768  +  /* get memory map allocation granularity */
         2769  +  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
         2770  +  GetSystemInfo(&winSysInfo);
         2771  +  assert(winSysInfo.dwAllocationGranularity > 0);
         2772  +#endif
  2740   2773   
  2741   2774     sqlite3_vfs_register(&winVfs, 1);
  2742   2775     return SQLITE_OK; 
  2743   2776   }
  2744   2777   int sqlite3_os_end(void){ 
  2745   2778     return SQLITE_OK;
  2746   2779   }
  2747   2780   
  2748   2781   #endif /* SQLITE_OS_WIN */