1
2
3
4
5
6
7
8
9
10
11
12
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
|
1
2
3
4
5
6
7
8
9
10
11
12
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
|
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
+
-
+
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
+
-
+
+
+
-
-
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
+
+
-
+
-
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
-
+
-
-
-
+
+
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
-
-
+
+
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-
+
+
+
+
-
-
+
+
+
-
-
-
-
-
+
-
-
+
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
-
-
-
-
-
-
+
+
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
+
|
/*
** 2008 August 05
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file implements that page cache.
**
** @(#) $Id: pcache.c,v 1.36 2008/11/11 18:43:00 danielk1977 Exp $
** @(#) $Id: pcache.c,v 1.37 2008/11/13 14:28:29 danielk1977 Exp $
*/
#include "sqliteInt.h"
/*
** A complete page cache is an instance of this structure.
**
** A cache may only be deleted by its owner and while holding the
** SQLITE_MUTEX_STATUS_LRU mutex.
*/
struct PCache {
/*********************************************************************
** The first group of elements may be read or written at any time by
** the cache owner without holding the mutex. No thread other than the
** cache owner is permitted to access these elements at any time.
*/
PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
PgHdr *pSynced; /* Last synced page in dirty page list */
int nRef; /* Number of pinned pages */
int nRef; /* Number of referenced pages */
int nPinned; /* Number of pinned and/or dirty pages */
int nMax; /* Configured cache size */
int nMin; /* Configured minimum cache size */
/**********************************************************************
** The next group of elements are fixed when the cache is created and
** may not be changed afterwards. These elements can read at any time by
** the cache owner or by any thread holding the the mutex. Non-owner
** threads must hold the mutex when reading these elements to prevent
** the entire PCache object from being deleted during the read.
*/
int szPage; /* Size of every page in this cache */
int szExtra; /* Size of extra space for each page */
int bPurgeable; /* True if pages are on backing store */
int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
void *pStress; /* Argument to xStress */
/**********************************************************************
** The final group of elements can only be accessed while holding the
** mutex. Both the cache owner and any other thread must hold the mutex
** to read or write any of these elements.
*/
int nPage; /* Total number of pages in apHash */
sqlite3_pcache *pCache; /* Pluggable cache module */
int nHash; /* Number of slots in apHash[] */
PgHdr **apHash; /* Hash table for fast lookup by pgno */
PgHdr *pClean; /* List of clean pages in use */
};
PgHdr *pPage1;
/*
** Free slots in the page block allocator
*/
typedef struct PgFreeslot PgFreeslot;
struct PgFreeslot {
PgFreeslot *pNext; /* Next free slot */
};
/*
** Global data for the page cache.
*/
static SQLITE_WSD struct PCacheGlobal {
int isInit; /* True when initialized */
sqlite3_mutex *mutex; /* static mutex MUTEX_STATIC_LRU */
int nMaxPage; /* Sum of nMaxPage for purgeable caches */
int nMinPage; /* Sum of nMinPage for purgeable caches */
int nCurrentPage; /* Number of purgeable pages allocated */
PgHdr *pLruHead, *pLruTail; /* LRU list of unused clean pgs */
/* Variables related to SQLITE_CONFIG_PAGECACHE settings. */
int szSlot; /* Size of each free slot */
void *pStart, *pEnd; /* Bounds of pagecache malloc range */
PgFreeslot *pFree; /* Free page blocks */
} pcache = {0};
/*
** All code in this file should access the global pcache structure via the
** alias "pcache_g". This ensures that the WSD emulation is used when
** compiling for systems that do not support real WSD.
*/
#define pcache_g (GLOBAL(struct PCacheGlobal, pcache))
/*
** All global variables used by this module (all of which are grouped
** together in global structure "pcache" above) are protected by the static
** SQLITE_MUTEX_STATIC_LRU mutex. A pointer to this mutex is stored in
** variable "pcache.mutex".
**
** Some elements of the PCache and PgHdr structures are protected by the
** SQLITE_MUTEX_STATUS_LRU mutex and other are not. The protected
** elements are grouped at the end of the structures and are clearly
** marked.
**
** Use the following macros must surround all access (read or write)
** of protected elements. The mutex is not recursive and may not be
** entered more than once. The pcacheMutexHeld() macro should only be
** used within an assert() to verify that the mutex is being held.
*/
#define pcacheEnterMutex() sqlite3_mutex_enter(pcache_g.mutex)
#define pcacheExitMutex() sqlite3_mutex_leave(pcache_g.mutex)
#define pcacheMutexHeld() sqlite3_mutex_held(pcache_g.mutex)
/*
** Some of the assert() macros in this code are too expensive to run
** even during normal debugging. Use them only rarely on long-running
** tests. Enable the expensive asserts using the
** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
*/
#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
# define expensive_assert(X) assert(X)
#else
# define expensive_assert(X)
#endif
/********************************** Linked List Management ********************/
#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
/*
** This routine verifies that the number of entries in the hash table
** is pCache->nPage. This routine is used within assert() statements
** only and is therefore disabled during production builds.
*/
static int pcacheCheckHashCount(PCache *pCache){
int i;
int nPage = 0;
for(i=0; i<pCache->nHash; i++){
PgHdr *p;
for(p=pCache->apHash[i]; p; p=p->pNextHash){
nPage++;
}
}
assert( nPage==pCache->nPage );
return 1;
}
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
/*
** Based on the current value of PCache.nRef and the contents of the
** PCache.pDirty list, return the expected value of the PCache.nPinned
** counter. This is only used in debugging builds, as follows:
**
** expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
*/
static int pcachePinnedCount(PCache *pCache){
PgHdr *p;
int nPinned = pCache->nRef;
for(p=pCache->pDirty; p; p=p->pNext){
if( p->nRef==0 ){
nPinned++;
}
}
return nPinned;
}
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
#if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
/*
** Check that the pCache->pSynced variable is set correctly. If it
** is not, either fail an assert or return zero. Otherwise, return
** non-zero. This is only used in debugging builds, as follows:
**
** expensive_assert( pcacheCheckSynced(pCache) );
*/
static int pcacheCheckSynced(PCache *pCache){
PgHdr *p = pCache->pDirtyTail;
for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pPrev){
PgHdr *p;
for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
}
return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
}
#endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
/*
** Remove a page from its hash table (PCache.apHash[]).
** Remove page pPage from the list of dirty pages.
*/
static void pcacheRemoveFromHash(PgHdr *pPage){
static void pcacheRemoveFromDirtyList(PgHdr *pPage){
assert( pcacheMutexHeld() );
if( pPage->pPrevHash ){
pPage->pPrevHash->pNextHash = pPage->pNextHash;
}else{
PCache *pCache = pPage->pCache;
PCache *p = pPage->pCache;
u32 h = pPage->pgno % pCache->nHash;
assert( pCache->apHash[h]==pPage );
pCache->apHash[h] = pPage->pNextHash;
}
if( pPage->pNextHash ){
pPage->pNextHash->pPrevHash = pPage->pPrevHash;
}
pPage->pCache->nPage--;
expensive_assert( pcacheCheckHashCount(pPage->pCache) );
}
assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
/*
** Insert a page into the hash table
**
** The mutex must be held by the caller.
*/
static void pcacheAddToHash(PgHdr *pPage){
PCache *pCache = pPage->pCache;
u32 h = pPage->pgno % pCache->nHash;
assert( pcacheMutexHeld() );
pPage->pNextHash = pCache->apHash[h];
pPage->pPrevHash = 0;
if( pCache->apHash[h] ){
pCache->apHash[h]->pPrevHash = pPage;
}
pCache->apHash[h] = pPage;
pCache->nPage++;
expensive_assert( pcacheCheckHashCount(pCache) );
}
assert( pPage->pDirtyPrev || pPage==p->pDirty );
/*
** Attempt to increase the size the hash table to contain
** at least nHash buckets.
*/
static int pcacheResizeHash(PCache *pCache, int nHash){
PgHdr *p;
PgHdr **pNew;
assert( pcacheMutexHeld() );
#ifdef SQLITE_MALLOC_SOFT_LIMIT
if( nHash*sizeof(PgHdr*)>SQLITE_MALLOC_SOFT_LIMIT ){
nHash = SQLITE_MALLOC_SOFT_LIMIT/sizeof(PgHdr *);
}
#endif
pcacheExitMutex();
pNew = (PgHdr **)sqlite3Malloc(sizeof(PgHdr*)*nHash);
pcacheEnterMutex();
if( !pNew ){
return SQLITE_NOMEM;
}
memset(pNew, 0, sizeof(PgHdr *)*nHash);
sqlite3_free(pCache->apHash);
pCache->apHash = pNew;
pCache->nHash = nHash;
pCache->nPage = 0;
/* Update the PCache1.pSynced variable if necessary. */
for(p=pCache->pClean; p; p=p->pNext){
pcacheAddToHash(p);
}
for(p=pCache->pDirty; p; p=p->pNext){
pcacheAddToHash(p);
}
return SQLITE_OK;
}
/*
** Remove a page from a linked list that is headed by *ppHead.
** *ppHead is either PCache.pClean or PCache.pDirty.
*/
static void pcacheRemoveFromList(PgHdr **ppHead, PgHdr *pPage){
int isDirtyList = (ppHead==&pPage->pCache->pDirty);
assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
assert( pcacheMutexHeld() || ppHead!=&pPage->pCache->pClean );
if( pPage->pPrev ){
pPage->pPrev->pNext = pPage->pNext;
}else{
assert( *ppHead==pPage );
*ppHead = pPage->pNext;
}
if( pPage->pNext ){
pPage->pNext->pPrev = pPage->pPrev;
}
if( isDirtyList ){
PCache *pCache = pPage->pCache;
assert( pPage->pNext || pCache->pDirtyTail==pPage );
if( !pPage->pNext ){
pCache->pDirtyTail = pPage->pPrev;
}
if( pCache->pSynced==pPage ){
PgHdr *pSynced = pPage->pPrev;
while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
pSynced = pSynced->pPrev;
}
pCache->pSynced = pSynced;
}
}
if( p->pSynced==pPage ){
PgHdr *pSynced = pPage->pDirtyPrev;
while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
pSynced = pSynced->pDirtyPrev;
}
p->pSynced = pSynced;
}
}
/*
** Add a page from a linked list that is headed by *ppHead.
** *ppHead is either PCache.pClean or PCache.pDirty.
*/
static void pcacheAddToList(PgHdr **ppHead, PgHdr *pPage){
int isDirtyList = (ppHead==&pPage->pCache->pDirty);
assert( ppHead==&pPage->pCache->pClean || ppHead==&pPage->pCache->pDirty );
if( (*ppHead) ){
(*ppHead)->pPrev = pPage;
}
pPage->pNext = *ppHead;
pPage->pPrev = 0;
*ppHead = pPage;
if( isDirtyList ){
PCache *pCache = pPage->pCache;
if( !pCache->pDirtyTail ){
assert( pPage->pNext==0 );
pCache->pDirtyTail = pPage;
}
if( !pCache->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
pCache->pSynced = pPage;
}
}
}
/*
** Remove a page from the global LRU list
*/
static void pcacheRemoveFromLruList(PgHdr *pPage){
assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( (pPage->flags&PGHDR_DIRTY)==0 );
if( pPage->pCache->bPurgeable==0 ) return;
if( pPage->pNextLru ){
if( pPage->pDirtyNext ){
assert( pcache_g.pLruTail!=pPage );
pPage->pNextLru->pPrevLru = pPage->pPrevLru;
pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
}else{
assert( pcache_g.pLruTail==pPage );
pcache_g.pLruTail = pPage->pPrevLru;
assert( pPage==p->pDirtyTail );
p->pDirtyTail = pPage->pDirtyPrev;
}
if( pPage->pPrevLru ){
assert( pcache_g.pLruHead!=pPage );
pPage->pPrevLru->pNextLru = pPage->pNextLru;
}else{
assert( pcache_g.pLruHead==pPage );
pcache_g.pLruHead = pPage->pNextLru;
}
}
/*
** Add a page to the global LRU list. The page is normally added
** to the front of the list so that it will be the last page recycled.
** However, if the PGHDR_REUSE_UNLIKELY bit is set, the page is added
** to the end of the LRU list so that it will be the next to be recycled.
*/
static void pcacheAddToLruList(PgHdr *pPage){
assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( (pPage->flags&PGHDR_DIRTY)==0 );
if( pPage->pCache->bPurgeable==0 ) return;
if( pPage->pDirtyPrev ){
if( pcache_g.pLruTail && (pPage->flags & PGHDR_REUSE_UNLIKELY)!=0 ){
/* If reuse is unlikely. Put the page at the end of the LRU list
** where it will be recycled sooner rather than later.
*/
assert( pcache_g.pLruHead );
pPage->pNextLru = 0;
pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
pPage->pPrevLru = pcache_g.pLruTail;
pcache_g.pLruTail->pNextLru = pPage;
pcache_g.pLruTail = pPage;
pPage->flags &= ~PGHDR_REUSE_UNLIKELY;
}else{
/* If reuse is possible. the page goes at the beginning of the LRU
** list so that it will be the last to be recycled.
*/
if( pcache_g.pLruHead ){
pcache_g.pLruHead->pPrevLru = pPage;
}
pPage->pNextLru = pcache_g.pLruHead;
assert( pPage==p->pDirty );
p->pDirty = pPage->pDirtyNext;
}
pPage->pDirtyNext = 0;
pcache_g.pLruHead = pPage;
pPage->pPrevLru = 0;
pPage->pDirtyPrev = 0;
if( pcache_g.pLruTail==0 ){
pcache_g.pLruTail = pPage;
}
}
expensive_assert( pcacheCheckSynced(p) );
}
}
/*********************************************** Memory Allocation ***********
**
** Initialize the page cache memory pool.
**
** This must be called at start-time when no page cache lines are
** checked out. This function is not threadsafe.
*/
void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
PgFreeslot *p;
sz &= ~7;
pcache_g.szSlot = sz;
pcache_g.pStart = pBuf;
pcache_g.pFree = 0;
while( n-- ){
p = (PgFreeslot*)pBuf;
p->pNext = pcache_g.pFree;
pcache_g.pFree = p;
pBuf = (void*)&((char*)pBuf)[sz];
}
pcache_g.pEnd = pBuf;
}
/*
** Allocate a page cache line. Look in the page cache memory pool first
** and use an element from it first if available. If nothing is available
** in the page cache memory pool, go to the general purpose memory allocator.
*/
static void *pcacheMalloc(int sz, PCache *pCache){
assert( sqlite3_mutex_held(pcache_g.mutex) );
if( sz<=pcache_g.szSlot && pcache_g.pFree ){
PgFreeslot *p = pcache_g.pFree;
pcache_g.pFree = p->pNext;
sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, sz);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
return (void*)p;
}else{
void *p;
** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
/* Allocate a new buffer using sqlite3Malloc. Before doing so, exit the
** global pcache mutex and unlock the pager-cache object pCache. This is
** so that if the attempt to allocate a new buffer causes the the
** configured soft-heap-limit to be breached, it will be possible to
** reclaim memory from this pager-cache.
*/
pcacheExitMutex();
p = sqlite3Malloc(sz);
pcacheEnterMutex();
** pPage).
if( p ){
sz = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
}
return p;
}
}
void *sqlite3PageMalloc(int sz){
void *p;
pcacheEnterMutex();
p = pcacheMalloc(sz, 0);
pcacheExitMutex();
return p;
}
/*
** Release a pager memory allocation
*/
static void pcacheFree(void *p){
static void pcacheAddToDirtyList(PgHdr *pPage){
assert( sqlite3_mutex_held(pcache_g.mutex) );
if( p==0 ) return;
if( p>=pcache_g.pStart && p<pcache_g.pEnd ){
PgFreeslot *pSlot;
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
pSlot = (PgFreeslot*)p;
pSlot->pNext = pcache_g.pFree;
pcache_g.pFree = pSlot;
PCache *p = pPage->pCache;
}else{
int iSize = sqlite3MallocSize(p);
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
sqlite3_free(p);
}
}
void sqlite3PageFree(void *p){
pcacheEnterMutex();
assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
pPage->pDirtyNext = p->pDirty;
if( pPage->pDirtyNext ){
pcacheFree(p);
pcacheExitMutex();
}
assert( pPage->pDirtyNext->pDirtyPrev==0 );
pPage->pDirtyNext->pDirtyPrev = pPage;
}
p->pDirty = pPage;
/*
** Allocate a new page.
*/
static PgHdr *pcachePageAlloc(PCache *pCache){
PgHdr *p;
int sz = sizeof(*p) + pCache->szPage + pCache->szExtra;
assert( sqlite3_mutex_held(pcache_g.mutex) );
p = pcacheMalloc(sz, pCache);
if( p==0 ) return 0;
if( !p->pDirtyTail ){
memset(p, 0, sizeof(PgHdr));
p->pData = (void*)&p[1];
p->pDirtyTail = pPage;
p->pExtra = (void*)&((char*)p->pData)[pCache->szPage];
if( pCache->bPurgeable ){
pcache_g.nCurrentPage++;
}
return p;
}
if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
/*
** Deallocate a page
*/
static void pcachePageFree(PgHdr *p){
assert( sqlite3_mutex_held(pcache_g.mutex) );
if( p->pCache->bPurgeable ){
pcache_g.nCurrentPage--;
p->pSynced = pPage;
}
pcacheFree(p);
expensive_assert( pcacheCheckSynced(p) );
}
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
/*
** Return the number of bytes that will be returned to the heap when
** the argument is passed to pcachePageFree().
*/
static int pcachePageSize(PgHdr *p){
assert( sqlite3_mutex_held(pcache_g.mutex) );
assert( !pcache_g.pStart );
assert( p && p->pCache );
return sqlite3MallocSize(p);
}
#endif
/*
** Attempt to 'recycle' a page from the global LRU list. Only clean,
** unreferenced pages from purgeable caches are eligible for recycling.
**
** This function removes page pcache.pLruTail from the global LRU list,
** and from the hash-table and PCache.pClean list of the owner pcache.
** There should be no other references to the page.
**
** A pointer to the recycled page is returned, or NULL if no page is
** eligible for recycling.
*/
static PgHdr *pcacheRecyclePage(void){
PgHdr *p = 0;
assert( sqlite3_mutex_held(pcache_g.mutex) );
** Wrapper around the pluggable caches xUnpin method. If the cache is
if( (p=pcache_g.pLruTail)!=0 ){
assert( (p->flags&PGHDR_DIRTY)==0 );
pcacheRemoveFromLruList(p);
pcacheRemoveFromHash(p);
pcacheRemoveFromList(&p->pCache->pClean, p);
}
** being used for an in-memory database, this function is a no-op.
return p;
}
/*
** Obtain space for a page. Try to recycle an old page if the limit on the
** number of pages has been reached. If the limit has not been reached or
** there are no pages eligible for recycling, allocate a new page.
**
** Return a pointer to the new page, or NULL if an OOM condition occurs.
*/
static int pcacheRecycleOrAlloc(PCache *pCache, PgHdr **ppPage){
PgHdr *p = 0;
int szPage = pCache->szPage;
static void pcacheUnpin(PgHdr *p){
PCache *pCache = p->pCache;
if( pCache->bPurgeable ){
int szExtra = pCache->szExtra;
if( p->pgno==1 ){
assert( pcache_g.isInit );
assert( sqlite3_mutex_held(pcache_g.mutex) );
pCache->pPage1 = 0;
*ppPage = 0;
}
/* If we have reached either the global or the local limit for
** pinned+dirty pages, and there is at least one dirty page,
** invoke the xStress callback to cause a page to become clean.
*/
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
expensive_assert( pcacheCheckSynced(pCache) );
if( pCache->xStress
&& pCache->pDirty
&& (pCache->nPinned>=(pcache_g.nMaxPage+pCache->nMin-pcache_g.nMinPage)
|| pCache->nPinned>=pCache->nMax)
){
PgHdr *pPg;
assert(pCache->pDirtyTail);
sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
for(pPg=pCache->pSynced;
pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
pPg=pPg->pPrev
);
if( !pPg ){
for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pPrev);
}
}
if( pPg ){
int rc;
pcacheExitMutex();
rc = pCache->xStress(pCache->pStress, pPg);
pcacheEnterMutex();
if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
return rc;
}
}
}
/* If either the local or the global page limit has been reached,
** try to recycle a page.
*/
if( pCache->bPurgeable && (pCache->nPage>=pCache->nMax-1 ||
pcache_g.nCurrentPage>=pcache_g.nMaxPage) ){
p = pcacheRecyclePage();
}
/* If a page has been recycled but it is the wrong size, free it. */
if( p && (p->pCache->szPage!=szPage || p->pCache->szPage!=szExtra) ){
pcachePageFree(p);
p = 0;
}
if( !p ){
p = pcachePageAlloc(pCache);
}
*ppPage = p;
return (p?SQLITE_OK:SQLITE_NOMEM);
}
/*************************************************** General Interfaces ******
**
** Initialize and shutdown the page cache subsystem. Neither of these
** functions are threadsafe.
*/
int sqlite3PcacheInitialize(void){
assert( pcache_g.isInit==0 );
memset(&pcache_g, 0, sizeof(pcache));
if( sqlite3GlobalConfig.bCoreMutex ){
/* No need to check the return value of sqlite3_mutex_alloc().
if( sqlite3GlobalConfig.pcache.xInit==0 ){
sqlite3PCacheSetDefault();
** Allocating a static mutex cannot fail.
*/
pcache_g.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
}
pcache_g.isInit = 1;
return SQLITE_OK;
return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
}
void sqlite3PcacheShutdown(void){
memset(&pcache_g, 0, sizeof(pcache));
if( sqlite3GlobalConfig.pcache.xShutdown ){
sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
}
}
/*
** Return the size in bytes of a PCache object.
*/
int sqlite3PcacheSize(void){ return sizeof(PCache); }
/*
** Create a new PCache object. Storage space to hold the object
** has already been allocated and is passed in as the p pointer.
** Create a new PCache object. Storage space to hold the object
** has already been allocated and is passed in as the p pointer.
** The caller discovers how much space needs to be allocated by
** calling sqlite3PcacheSize().
*/
void sqlite3PcacheOpen(
int szPage, /* Size of every page */
int szExtra, /* Extra space associated with each page */
int bPurgeable, /* True if pages are on backing store */
int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
void *pStress, /* Argument to xStress */
PCache *p /* Preallocated space for the PCache */
){
assert( pcache_g.isInit );
memset(p, 0, sizeof(PCache));
p->szPage = szPage;
p->szExtra = szExtra;
p->bPurgeable = bPurgeable;
p->xStress = xStress;
p->pStress = pStress;
p->nMax = 100;
p->nMin = 10;
pcacheEnterMutex();
if( bPurgeable ){
pcache_g.nMaxPage += p->nMax;
pcache_g.nMinPage += p->nMin;
}
pcacheExitMutex();
}
/*
** Change the page size for PCache object. This can only happen
** when the cache is empty.
** Change the page size for PCache object. The caller must ensure that there
** are no outstanding page references when this function is called.
*/
void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
assert( pCache->nRef==0 && pCache->pDirty==0 );
if( pCache->pCache ){
sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
assert(pCache->nPage==0);
pCache->pCache = 0;
}
pCache->szPage = szPage;
}
/*
** Try to obtain a page from the cache.
*/
int sqlite3PcacheFetch(
PCache *pCache, /* Obtain the page from this cache */
Pgno pgno, /* Page number to obtain */
int createFlag, /* If true, create page if it does not exist already */
PgHdr **ppPage /* Write the page here */
){
int rc = SQLITE_OK;
PgHdr *pPage = 0;
int eCreate;
assert( pcache_g.isInit );
assert( pCache!=0 );
assert( pgno>0 );
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
pcacheEnterMutex();
/* If the pluggable cache (sqlite3_pcache*) has not been allocated,
** allocate it now.
/* Search the hash table for the requested page. Exit early if it is found. */
if( pCache->apHash ){
u32 h = pgno % pCache->nHash;
for(pPage=pCache->apHash[h]; pPage; pPage=pPage->pNextHash){
if( pPage->pgno==pgno ){
if( pPage->nRef==0 ){
if( 0==(pPage->flags&PGHDR_DIRTY) ){
*/
if( !pCache->pCache && createFlag ){
sqlite3_pcache *p;
int nByte;
nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
if( !p ){
return SQLITE_NOMEM;
pcacheRemoveFromLruList(pPage);
pCache->nPinned++;
}
pCache->nRef++;
}
}
sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
pCache->pCache = p;
}
pPage->nRef++;
break;
}
}
eCreate = createFlag ? 1 : 0;
if( eCreate && (!pCache->bPurgeable || !pCache->pDirty) ){
eCreate = 2;
}
if( pCache->pCache ){
pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
}
if( !pPage && createFlag ){
if( !pPage && eCreate==1 ){
if( pCache->nHash<=pCache->nPage ){
rc = pcacheResizeHash(pCache, pCache->nHash<256 ? 256 : pCache->nHash*2);
}
PgHdr *pPg;
if( rc==SQLITE_OK ){
rc = pcacheRecycleOrAlloc(pCache, &pPage);
/* Find a dirty page to write-out and recycle. First try to find a
** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
** cleared), but if that is not possible settle for any other
** unreferenced dirty page.
*/
expensive_assert( pcacheCheckSynced(pCache) );
}
if( rc==SQLITE_OK ){
pPage->pPager = 0;
pPage->flags = 0;
pPage->pDirty = 0;
pPage->pgno = pgno;
pPage->pCache = pCache;
pPage->nRef = 1;
pCache->nRef++;
pCache->nPinned++;
for(pPg=pCache->pSynced;
pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
pPg=pPg->pDirtyPrev
);
if( !pPg ){
for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
}
if( pPg ){
int rc;
rc = pCache->xStress(pCache->pStress, pPg);
if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
return rc;
}
}
pcacheAddToList(&pCache->pClean, pPage);
pcacheAddToHash(pPage);
}
}
pcacheExitMutex();
pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
}
if( pPage ){
if( 0==pPage->nRef ){
pCache->nRef++;
}
pPage->nRef++;
pPage->pData = (void*)&pPage[1];
pPage->pExtra = (void*)&((char*)pPage->pData)[pCache->szPage];
pPage->pCache = pCache;
pPage->pgno = pgno;
if( pgno==1 ){
pCache->pPage1 = pPage;
}
}
*ppPage = pPage;
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
assert( pPage || !createFlag || rc!=SQLITE_OK );
return rc;
return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
}
/*
** Dereference a page. When the reference count reaches zero,
** move the page to the LRU list if it is clean.
** Decrement the reference count on a page. If the page is clean and the
** reference count drops to 0, then it is made elible for recycling.
*/
void sqlite3PcacheRelease(PgHdr *p){
assert( p->nRef>0 );
p->nRef--;
if( p->nRef==0 ){
PCache *pCache = p->pCache;
pCache->nRef--;
if( (p->flags&PGHDR_DIRTY)==0 ){
pCache->nPinned--;
pcacheEnterMutex();
if( pcache_g.nCurrentPage>pcache_g.nMaxPage ){
pcacheRemoveFromList(&pCache->pClean, p);
pcacheRemoveFromHash(p);
pcachePageFree(p);
}else{
pcacheUnpin(p);
}else{
pcacheAddToLruList(p);
}
pcacheExitMutex();
}else{
/* Move the page to the head of the caches dirty list. */
pcacheRemoveFromList(&pCache->pDirty, p);
pcacheAddToList(&pCache->pDirty, p);
/* Move the page to the head of the dirty list. */
pcacheRemoveFromDirtyList(p);
pcacheAddToDirtyList(p);
}
}
}
/*
** Increase the reference count of a supplied page by 1.
*/
void sqlite3PcacheRef(PgHdr *p){
assert(p->nRef>0);
p->nRef++;
}
/*
** Drop a page from the cache. There must be exactly one reference to the
** page. This function deletes that reference, so after it returns the
** page pointed to by p is invalid.
*/
void sqlite3PcacheDrop(PgHdr *p){
PCache *pCache;
assert( p->nRef==1 );
assert( 0==(p->flags&PGHDR_DIRTY) );
if( p->flags&PGHDR_DIRTY ){
pcacheRemoveFromDirtyList(p);
}
pCache = p->pCache;
pCache->nRef--;
if( p->pgno==1 ){
pCache->nPinned--;
pcacheEnterMutex();
pCache->pPage1 = 0;
}
sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
pcacheRemoveFromList(&pCache->pClean, p);
pcacheRemoveFromHash(p);
pcachePageFree(p);
pcacheExitMutex();
}
/*
** Make sure the page is marked as dirty. If it isn't dirty already,
** Make sure the page is marked as dirty. If it isn't dirty already,
** make it so.
*/
void sqlite3PcacheMakeDirty(PgHdr *p){
PCache *pCache;
p->flags &= ~PGHDR_DONT_WRITE;
if( p->flags & PGHDR_DIRTY ) return;
assert( (p->flags & PGHDR_DIRTY)==0 );
assert( p->nRef>0 );
if( 0==(p->flags & PGHDR_DIRTY) ){
pCache = p->pCache;
pCache = p->pCache;
pcacheEnterMutex();
pcacheRemoveFromList(&pCache->pClean, p);
pcacheAddToList(&pCache->pDirty, p);
pcacheExitMutex();
p->flags |= PGHDR_DIRTY;
p->flags |= PGHDR_DIRTY;
}
static void pcacheMakeClean(PgHdr *p){
PCache *pCache = p->pCache;
assert( p->flags & PGHDR_DIRTY );
pcacheRemoveFromList(&pCache->pDirty, p);
pcacheAddToList(&pCache->pClean, p);
p->flags &= ~PGHDR_DIRTY;
if( p->nRef==0 ){
pcacheAddToLruList(p);
pcacheAddToDirtyList( p);
pCache->nPinned--;
}
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
}
/*
** Make sure the page is marked as clean. If it isn't clean already,
** Make sure the page is marked as clean. If it isn't clean already,
** make it so.
*/
void sqlite3PcacheMakeClean(PgHdr *p){
if( (p->flags & PGHDR_DIRTY) ){
pcacheEnterMutex();
pcacheMakeClean(p);
pcacheExitMutex();
pcacheRemoveFromDirtyList(p);
p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
if( p->nRef==0 ){
pcacheUnpin(p);
}
}
}
/*
** Make every page in the cache clean.
*/
void sqlite3PcacheCleanAll(PCache *pCache){
PgHdr *p;
pcacheEnterMutex();
while( (p = pCache->pDirty)!=0 ){
pcacheRemoveFromList(&pCache->pDirty, p);
p->flags &= ~PGHDR_DIRTY;
pcacheAddToList(&pCache->pClean, p);
if( p->nRef==0 ){
pcacheAddToLruList(p);
sqlite3PcacheMakeClean(p);
pCache->nPinned--;
}
}
sqlite3PcacheAssertFlags(pCache, 0, PGHDR_DIRTY);
expensive_assert( pCache->nPinned==pcachePinnedCount(pCache) );
pcacheExitMutex();
}
}
/*
** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
*/
void sqlite3PcacheClearSyncFlags(PCache *pCache){
PgHdr *p;
for(p=pCache->pDirty; p; p=p->pDirtyNext){
p->flags &= ~PGHDR_NEED_SYNC;
}
pCache->pSynced = pCache->pDirtyTail;
}
/*
** Change the page number of page p to newPgno. If newPgno is 0, then the
** Change the page number of page p to newPgno.
** page object is added to the clean-list and the PGHDR_REUSE_UNLIKELY
** flag set.
*/
void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
PCache *pCache = p->pCache;
assert( p->nRef>0 );
pcacheEnterMutex();
pcacheRemoveFromHash(p);
assert( newPgno>0 );
sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
p->pgno = newPgno;
if( newPgno==0 ){
if( (p->flags & PGHDR_DIRTY) ){
pcacheMakeClean(p);
}
p->flags = PGHDR_REUSE_UNLIKELY;
}
pcacheAddToHash(p);
pcacheExitMutex();
}
if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
/*
** Remove all content from a page cache
*/
static void pcacheClear(PCache *pCache){
PgHdr *p, *pNext;
assert( sqlite3_mutex_held(pcache_g.mutex) );
for(p=pCache->pClean; p; p=pNext){
pNext = p->pNext;
pcacheRemoveFromLruList(p);
pcachePageFree(p);
pcacheRemoveFromDirtyList(p);
pcacheAddToDirtyList(p);
}
for(p=pCache->pDirty; p; p=pNext){
pNext = p->pNext;
pcachePageFree(p);
}
}
pCache->pClean = 0;
pCache->pDirty = 0;
pCache->pDirtyTail = 0;
pCache->nPage = 0;
pCache->nPinned = 0;
memset(pCache->apHash, 0, pCache->nHash*sizeof(pCache->apHash[0]));
}
/*
** Drop every cache entry whose page number is greater than "pgno".
** Drop every cache entry whose page number is greater than "pgno". The
** caller must ensure that there are no outstanding references to any pages
** other than page 1 with a page number greater than pgno.
**
** If there is a reference to page 1 and the pgno parameter passed to this
** function is 0, then the data area associated with page 1 is zeroed, but
** the page object is not dropped.
*/
void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
if( pCache->pCache ){
PgHdr *p, *pNext;
PgHdr *pDirty = pCache->pDirty;
PgHdr *p;
PgHdr *pNext;
pcacheEnterMutex();
for(p=pCache->pClean; p||pDirty; p=pNext){
for(p=pCache->pDirty; p; p=pNext){
if( !p ){
p = pDirty;
pDirty = 0;
}
pNext = p->pNext;
if( p->pgno>pgno ){
pNext = p->pDirtyNext;
if( p->pgno>pgno ){
if( p->nRef==0 ){
pcacheRemoveFromHash(p);
if( p->flags&PGHDR_DIRTY ){
assert( p->flags&PGHDR_DIRTY );
pcacheRemoveFromList(&pCache->pDirty, p);
pCache->nPinned--;
}else{
pcacheRemoveFromList(&pCache->pClean, p);
pcacheRemoveFromLruList(p);
}
pcachePageFree(p);
sqlite3PcacheMakeClean(p);
}else{
/* If there are references to the page, it cannot be freed. In this
** case, zero the page content instead.
*/
memset(p->pData, 0, pCache->szPage);
}
}
}
pcacheExitMutex();
}
if( pgno==0 && pCache->pPage1 ){
memset(pCache->pPage1->pData, 0, pCache->szPage);
pgno = 1;
}
sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
/*
** If there are currently more than pcache.nMaxPage pages allocated, try
** to recycle pages to reduce the number allocated to pcache.nMaxPage.
*/
static void pcacheEnforceMaxPage(void){
PgHdr *p;
assert( sqlite3_mutex_held(pcache_g.mutex) );
while( pcache_g.nCurrentPage>pcache_g.nMaxPage
&& (p = pcacheRecyclePage())!=0 ){
pcachePageFree(p);
}
}
/*
** Close a cache.
*/
void sqlite3PcacheClose(PCache *pCache){
pcacheEnterMutex();
/* Free all the pages used by this pager and remove them from the LRU list. */
pcacheClear(pCache);
if( pCache->bPurgeable ){
if( pCache->pCache ){
pcache_g.nMaxPage -= pCache->nMax;
pcache_g.nMinPage -= pCache->nMin;
pcacheEnforceMaxPage();
}
sqlite3_free(pCache->apHash);
pcacheExitMutex();
}
sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
}
#ifndef NDEBUG
/*
** Assert flags settings on all pages. Debugging only.
*/
void sqlite3PcacheAssertFlags(PCache *pCache, int trueMask, int falseMask){
PgHdr *p;
for(p=pCache->pDirty; p; p=p->pNext){
assert( (p->flags&trueMask)==trueMask );
assert( (p->flags&falseMask)==0 );
}
}
for(p=pCache->pClean; p; p=p->pNext){
assert( (p->flags&trueMask)==trueMask );
assert( (p->flags&falseMask)==0 );
}
}
#endif
/*
** Discard the contents of the cache.
*/
int sqlite3PcacheClear(PCache *pCache){
assert(pCache->nRef==0);
pcacheEnterMutex();
pcacheClear(pCache);
sqlite3PcacheTruncate(pCache, 0);
pcacheExitMutex();
return SQLITE_OK;
}
/*
** Merge two lists of pages connected by pDirty and in pgno order.
** Do not both fixing the pPrevDirty pointers.
** Do not both fixing the pDirtyPrev pointers.
*/
static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
PgHdr result, *pTail;
pTail = &result;
while( pA && pB ){
if( pA->pgno<pB->pgno ){
pTail->pDirty = pA;
|
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
-
-
+
+
-
+
+
+
+
-
-
+
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
-
-
-
+
+
-
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-
-
+
-
-
-
-
+
+
-
-
-
-
+
+
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
}
/*
** Return a list of all dirty pages in the cache, sorted by page number.
*/
PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
PgHdr *p;
for(p=pCache->pDirty; p; p=p->pNext){
p->pDirty = p->pNext;
for(p=pCache->pDirty; p; p=p->pDirtyNext){
p->pDirty = p->pDirtyNext;
}
return pcacheSortDirtyList(pCache->pDirty);
}
/*
** Return the total number of outstanding page references.
** Return the total number of referenced pages held by the cache.
*/
int sqlite3PcacheRefCount(PCache *pCache){
return pCache->nRef;
}
/*
** Return the number of references to the page supplied as an argument.
*/
int sqlite3PcachePageRefcount(PgHdr *p){
return p->nRef;
}
/*
** Return the total number of pages in the cache.
*/
int sqlite3PcachePagecount(PCache *pCache){
assert( pCache->nPage>=0 );
return pCache->nPage;
int nPage = 0;
if( pCache->pCache ){
}
nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
#ifdef SQLITE_CHECK_PAGES
/*
** This function is used by the pager.c module to iterate through all
** pages in the cache. At present, this is only required if the
** SQLITE_CHECK_PAGES macro (used for debugging) is specified.
*/
void sqlite3PcacheIterate(PCache *pCache, void (*xIter)(PgHdr *)){
PgHdr *p;
for(p=pCache->pClean; p; p=p->pNext){
xIter(p);
}
for(p=pCache->pDirty; p; p=p->pNext){
xIter(p);
}
}
#endif
return nPage;
/*
** Set flags on all pages in the page cache
*/
void sqlite3PcacheClearFlags(PCache *pCache, int mask){
PgHdr *p;
}
/* Obtain the global mutex before modifying any PgHdr.flags variables
** or traversing the LRU list.
*/
pcacheEnterMutex();
mask = ~mask;
for(p=pCache->pDirty; p; p=p->pNext){
p->flags &= mask;
}
for(p=pCache->pClean; p; p=p->pNext){
p->flags &= mask;
}
if( 0==(mask&PGHDR_NEED_SYNC) ){
pCache->pSynced = pCache->pDirtyTail;
assert( !pCache->pSynced || (pCache->pSynced->flags&PGHDR_NEED_SYNC)==0 );
}
pcacheExitMutex();
}
/*
** Set the suggested cache-size value.
** Get the suggested cache-size value.
*/
int sqlite3PcacheGetCachesize(PCache *pCache){
return pCache->nMax;
}
/*
** Set the suggested cache-size value.
*/
void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
if( mxPage<10 ){
pCache->nMax = mxPage;
mxPage = 10;
}
if( pCache->bPurgeable ){
pcacheEnterMutex();
if( pCache->pCache ){
sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
pcache_g.nMaxPage -= pCache->nMax;
pcache_g.nMaxPage += mxPage;
pcacheEnforceMaxPage();
pcacheExitMutex();
}
pCache->nMax = mxPage;
}
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
#ifdef SQLITE_CHECK_PAGES
/*
** This function is called to free superfluous dynamically allocated memory
** held by the pager system. Memory in use by any SQLite pager allocated
** by the current thread may be sqlite3_free()ed.
**
** For all dirty pages currently in the cache, invoke the specified
** callback. This is only used if the SQLITE_CHECK_PAGES macro is
** defined.
** nReq is the number of bytes of memory required. Once this much has
** been released, the function returns. The return value is the total number
** of bytes of memory released.
*/
int sqlite3PcacheReleaseMemory(int nReq){
void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
int nFree = 0;
if( pcache_g.pStart==0 ){
PgHdr *p;
pcacheEnterMutex();
PgHdr *pDirty;
for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
while( (nReq<0 || nFree<nReq) && (p=pcacheRecyclePage()) ){
nFree += pcachePageSize(p);
pcachePageFree(p);
}
xIter(pDirty);
}
pcacheExitMutex();
}
}
return nFree;
}
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
#endif
#ifdef SQLITE_TEST
void sqlite3PcacheStats(
int *pnCurrent,
int *pnMax,
int *pnMin,
int *pnRecyclable
){
PgHdr *p;
int nRecyclable = 0;
for(p=pcache_g.pLruHead; p; p=p->pNextLru){
nRecyclable++;
}
*pnCurrent = pcache_g.nCurrentPage;
*pnMax = pcache_g.nMaxPage;
*pnMin = pcache_g.nMinPage;
*pnRecyclable = nRecyclable;
}
#endif
|