/ Check-in [38cbcedb]
Login

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

Overview
Comment:Experimental changes to pre-cache a database file prior to it being fully opened.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | winPreCache
Files: files | file ages | folders
SHA1:38cbcedbb6e57dc3c2d452a0eb573cabc7df0c75
User & Date: mistachkin 2014-05-08 22:01:26
Context
2014-05-08
22:05
Update comments and only include the thread routine when required. check-in: a60c545f user: mistachkin tags: winPreCache
22:01
Experimental changes to pre-cache a database file prior to it being fully opened. check-in: 38cbcedb user: mistachkin tags: winPreCache
22:01
Fix static variable declaration issue on Windows. check-in: a41d2969 user: mistachkin tags: threads
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/os_win.c.

263
264
265
266
267
268
269

270
271
272
273
274
275
276
....
1040
1041
1042
1043
1044
1045
1046





1047
1048
1049
1050
1051
1052
1053
....
2349
2350
2351
2352
2353
2354
2355







2356
2357
2358
2359
2360
2361
2362
....
3193
3194
3195
3196
3197
3198
3199








































































3200
3201
3202
3203
3204
3205
3206
....
4700
4701
4702
4703
4704
4705
4706









4707
4708
4709
4710
4711
4712
4713
....
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
  int nFetchOut;                /* Number of outstanding xFetch references */
  HANDLE hMap;                  /* Handle for accessing memory mapping */
  void *pMapRegion;             /* Area memory mapped */
  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
  sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
#endif

};

/*
** Allowed values for winFile.ctrlFlags
*/
#define WINFILE_RDONLY          0x02   /* Connection is read only */
#define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
................................................................................
#else
  { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
#endif

#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)






}; /* End of the overrideable system calls */

/*
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
** system call pointer, or SQLITE_NOTFOUND if there is no configurable
** system call named zName.
................................................................................

  assert( id!=0 );
#ifndef SQLITE_OMIT_WAL
  assert( pFile->pShm==0 );
#endif
  assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
  OSTRACE(("CLOSE file=%p\n", pFile->h));








#if SQLITE_MAX_MMAP_SIZE>0
  winUnmapfile(pFile);
#endif

  do{
    rc = osCloseHandle(pFile->h);
................................................................................
** Return a vector of device characteristics.
*/
static int winDeviceCharacteristics(sqlite3_file *id){
  winFile *p = (winFile*)id;
  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
         ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
}









































































/* 
** Windows will only let you create file view mappings
** on allocation size granularity boundaries.
** During sqlite3_os_init() we do a GetSystemInfo()
** to get the granularity size.
*/
................................................................................
#if SQLITE_MAX_MMAP_SIZE>0
  pFile->hMap = NULL;
  pFile->pMapRegion = 0;
  pFile->mmapSize = 0;
  pFile->mmapSizeActual = 0;
  pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
#endif










  OpenCounter(+1);
  return rc;
}

/*
** Delete the named file.
................................................................................
    winGetSystemCall,    /* xGetSystemCall */
    winNextSystemCall,   /* xNextSystemCall */
  };
#endif

  /* Double-check that the aSyscall[] array has been constructed
  ** correctly.  See ticket [bb3a86e890c8e96ab] */
  assert( ArraySize(aSyscall)==76 );

  /* get memory map allocation granularity */
  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
#if SQLITE_OS_WINRT
  osGetNativeSystemInfo(&winSysInfo);
#else
  osGetSystemInfo(&winSysInfo);







>







 







>
>
>
>
>







 







>
>
>
>
>
>
>







 







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







 







>
>
>
>
>
>
>
>
>







 







|







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
....
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
....
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
....
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
....
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
....
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
  int nFetchOut;                /* Number of outstanding xFetch references */
  HANDLE hMap;                  /* Handle for accessing memory mapping */
  void *pMapRegion;             /* Area memory mapped */
  sqlite3_int64 mmapSize;       /* Usable size of mapped region */
  sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
  sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
#endif
  SQLiteThread *preCacheThread; /* Thread used to pre-cache file contents */
};

/*
** Allowed values for winFile.ctrlFlags
*/
#define WINFILE_RDONLY          0x02   /* Connection is read only */
#define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
................................................................................
#else
  { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
#endif

#define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)

  { "DuplicateHandle",          (SYSCALL)DuplicateHandle,        0 },

#define osDuplicateHandle ((BOOL(WINAPI*)(HANDLE, \
        HANDLE,HANDLE,LPHANDLE,DWORD,BOOL,DWORD))aSyscall[76].pCurrent)

}; /* End of the overrideable system calls */

/*
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
** system call pointer, or SQLITE_NOTFOUND if there is no configurable
** system call named zName.
................................................................................

  assert( id!=0 );
#ifndef SQLITE_OMIT_WAL
  assert( pFile->pShm==0 );
#endif
  assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
  OSTRACE(("CLOSE file=%p\n", pFile->h));

#if SQLITE_MAX_WORKER_THREADS>0
  if( pFile->preCacheThread ){
    void *pOut = 0;
    sqlite3ThreadJoin(pFile->preCacheThread, &pOut);
  }
#endif

#if SQLITE_MAX_MMAP_SIZE>0
  winUnmapfile(pFile);
#endif

  do{
    rc = osCloseHandle(pFile->h);
................................................................................
** Return a vector of device characteristics.
*/
static int winDeviceCharacteristics(sqlite3_file *id){
  winFile *p = (winFile*)id;
  return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
         ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
}

/*
** Thread routine that seeks to the end of an open file and reads one byte.
** This is used to provide a hint to the operating system that the entire
** file should be held in the cache.
*/
static void *winPreCacheThread(void *pCtx){
  winFile *pFile = (winFile*)pCtx;
  void *pBuf = 0;
  DWORD lastErrno;
  HANDLE dupHandle = NULL;
  DWORD dwSize, dwRet;
  DWORD dwAmt;
  DWORD nRead;

  if( !osDuplicateHandle(GetCurrentProcess(), pFile->h, GetCurrentProcess(),
                         &dupHandle, 0, FALSE, DUPLICATE_SAME_ACCESS) ){
    pFile->lastErrno = osGetLastError();
    OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR\n", dupHandle));
    return winLogError(SQLITE_IOERR, pFile->lastErrno,
                       "winPreCacheThread1", pFile->zPath);
  }
  dwSize = osSetFilePointer(dupHandle, 0, 0, FILE_END);
  if( (dwSize==INVALID_SET_FILE_POINTER
      && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
    pFile->lastErrno = lastErrno;
    osCloseHandle(dupHandle);
    OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR_SEEK\n", dupHandle));
    return winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
                       "winPreCacheThread2", pFile->zPath);
  }
  dwRet = osSetFilePointer(dupHandle, 0, 0, FILE_BEGIN);
  if( (dwRet==INVALID_SET_FILE_POINTER
      && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
    pFile->lastErrno = lastErrno;
    osCloseHandle(dupHandle);
    OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR_SEEK\n", dupHandle));
    return winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
                       "winPreCacheThread2", pFile->zPath);
  }
  dwAmt = 4194304; /* TODO: Tuning. */
  if( dwSize<dwAmt ){
    dwAmt = dwSize;
  }
  pBuf = sqlite3MallocZero( dwAmt );
  if( pBuf==0 ){
    osCloseHandle(dupHandle);
    OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR_NOMEM\n", dupHandle));
    return SQLITE_IOERR_NOMEM;
  }
  while( 1 ){
    if( !osReadFile(dupHandle, pBuf, dwAmt, &nRead, 0) ){
      pFile->lastErrno = osGetLastError();
      osCloseHandle(dupHandle);
      OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR_READ\n", dupHandle));
      return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
                         "winPreCacheThread3", pFile->zPath);
    }
    if( nRead<dwAmt ){
      osCloseHandle(dupHandle);
      OSTRACE(("PRE-CACHE file=%p, rc=SQLITE_IOERR_SHORT_READ\n", dupHandle));
      return winLogError(SQLITE_IOERR_SHORT_READ, pFile->lastErrno,
                         "winPreCacheThread4", pFile->zPath);
    }
    dwSize -= dwAmt;
    if( dwSize==0 ){
      break;
    }
  }
  osCloseHandle(dupHandle);
  return SQLITE_OK;
}

/* 
** Windows will only let you create file view mappings
** on allocation size granularity boundaries.
** During sqlite3_os_init() we do a GetSystemInfo()
** to get the granularity size.
*/
................................................................................
#if SQLITE_MAX_MMAP_SIZE>0
  pFile->hMap = NULL;
  pFile->pMapRegion = 0;
  pFile->mmapSize = 0;
  pFile->mmapSizeActual = 0;
  pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
#endif
#if SQLITE_MAX_WORKER_THREADS>0
  sqlite3ThreadCreate(&pFile->preCacheThread, winPreCacheThread, pFile);

  {
    void *pOut = 0;
    sqlite3ThreadJoin(pFile->preCacheThread, &pOut);
    pFile->preCacheThread = 0;
  }
#endif

  OpenCounter(+1);
  return rc;
}

/*
** Delete the named file.
................................................................................
    winGetSystemCall,    /* xGetSystemCall */
    winNextSystemCall,   /* xNextSystemCall */
  };
#endif

  /* Double-check that the aSyscall[] array has been constructed
  ** correctly.  See ticket [bb3a86e890c8e96ab] */
  assert( ArraySize(aSyscall)==77 );

  /* get memory map allocation granularity */
  memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
#if SQLITE_OS_WINRT
  osGetNativeSystemInfo(&winSysInfo);
#else
  osGetSystemInfo(&winSysInfo);