Commit 898e100
committed
Replace the usage_count clock sweep with a cooling-stage evictor
Replace the 0..5 usage_count buffer-replacement policy with a cooling-stage
clock (the LeanStore / 2Q-A1 model): a buffer is simply HOT (recently used)
or COOL (an eviction candidate), with "pinned" being the existing refcount.
There is no per-buffer access counter.
- A demand-loaded page is admitted COOL (probationary), not HOT. A second
access via PinBuffer promotes it COOL -> HOT (the rescue). So a page
touched once -- a sequential scan -- fills and drains the COOL stage and
is evicted from it without ever displacing the HOT working set. Scan
resistance is intrinsic to the replacement algorithm, which is what lets a
later commit remove the BufferAccessStrategy ring buffers entirely.
- The foreground sweep in StrategyGetBuffer() reclaims an already-COOL,
unpinned buffer, pinning it with a CAS so a racing PinBuffer always wins.
Promotion (COOL -> HOT) and demotion (HOT -> COOL) are single-bit
transitions; only the eviction claim is a CAS.
- The background writer maintains the supply of COOL victims. As its LRU
scan runs ahead of the clock hand it demotes HOT buffers to COOL, so the
foreground finds a victim in a single pass rather than having to cool
buffers itself. The demotion is demand-driven -- bounded by the predicted
allocation for the next cycle -- so it stages just enough COOL buffers
without cooling the whole pool, and it is done under the buffer header
lock the scan already holds. A single second-chance reference bit
(set by PinBuffer, cleared on the bgwriter's first pass over a HOT buffer)
spares a recently-accessed buffer one cooling pass, keeping the genuinely
hot set out of the COOL stage under scan pressure. BgBufferSync also
tracks the reusable-buffer density it directly observes on a shorter
smoothing window, so a burst of probationary/scan COOL pages is followed
promptly rather than averaged away. Under a bulk-dirtying workload the
per-cycle clean-write cap is raised from bgwriter_lru_maxpages to predicted
demand so the bgwriter keeps supplying clean victims, rather than the
foreground sweep having to flush dirty victims inline; normal workloads,
where demand is below the cap, are unaffected.
The 4-bit usage_count field is reinterpreted in place: bit 0 is the HOT/COOL
state (BUF_COOLSTATE_ONE), bit 1 the reference bit (BUF_REFBIT). The 64-bit
buffer-state layout -- refcount, flag and lock offsets and their StaticAsserts
-- is unchanged; only the meaning of the field and the instructions that touch
it change. A StaticAssert requires the field to be at least 2 bits wide so a
future width change cannot push the reference bit into the flag bits.
BM_MAX_USAGE_COUNT becomes BUF_COOLSTATE_HOT (1), so the pin fast path
saturates at HOT. Local (temp-table) buffers get the same two-state
treatment; being single-backend they need no background cooler.
Because the reference bit shares the field, the full 4-bit value can be 0..3;
readers that mean "the cooling state" must use BUF_STATE_GET_COOLSTATE(), which
masks to bit 0 and returns only 0 (COOL) or 1 (HOT), never the raw field.
contrib/pg_buffercache is updated accordingly: its usagecount column and the
pg_buffercache_summary average report the cooling state (0 = COOL, 1 = HOT),
and pg_buffercache_usage_counts() buckets on it. Using the raw 4-bit getter
there would index its BM_MAX_USAGE_COUNT+1 = 2-element arrays with values up
to 3 and overrun the stack; masking to the cooling bit keeps the index in
range. The reference bit is deliberately not exposed as usagecount.
Depends on the batched clock sweep from the previous commit.1 parent 40679bd commit 898e100
5 files changed
Lines changed: 239 additions & 87 deletions
File tree
- contrib/pg_buffercache
- src
- backend/storage/buffer
- include/storage
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| |||
605 | 605 | | |
606 | 606 | | |
607 | 607 | | |
608 | | - | |
| 608 | + | |
609 | 609 | | |
610 | 610 | | |
611 | 611 | | |
| |||
655 | 655 | | |
656 | 656 | | |
657 | 657 | | |
658 | | - | |
| 658 | + | |
659 | 659 | | |
660 | 660 | | |
661 | 661 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| |||
634 | 635 | | |
635 | 636 | | |
636 | 637 | | |
637 | | - | |
| 638 | + | |
638 | 639 | | |
639 | 640 | | |
640 | 641 | | |
| |||
2333 | 2334 | | |
2334 | 2335 | | |
2335 | 2336 | | |
2336 | | - | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
2337 | 2341 | | |
2338 | 2342 | | |
2339 | 2343 | | |
| |||
3002 | 3006 | | |
3003 | 3007 | | |
3004 | 3008 | | |
3005 | | - | |
| 3009 | + | |
| 3010 | + | |
| 3011 | + | |
3006 | 3012 | | |
3007 | 3013 | | |
3008 | 3014 | | |
| |||
3332 | 3338 | | |
3333 | 3339 | | |
3334 | 3340 | | |
3335 | | - | |
3336 | | - | |
3337 | | - | |
3338 | | - | |
3339 | | - | |
3340 | | - | |
3341 | | - | |
3342 | | - | |
3343 | | - | |
3344 | | - | |
3345 | | - | |
3346 | | - | |
3347 | | - | |
3348 | | - | |
3349 | | - | |
| 3341 | + | |
| 3342 | + | |
| 3343 | + | |
| 3344 | + | |
| 3345 | + | |
| 3346 | + | |
| 3347 | + | |
| 3348 | + | |
| 3349 | + | |
| 3350 | + | |
| 3351 | + | |
3350 | 3352 | | |
3351 | 3353 | | |
3352 | 3354 | | |
| |||
3785 | 3787 | | |
3786 | 3788 | | |
3787 | 3789 | | |
3788 | | - | |
| 3790 | + | |
3789 | 3791 | | |
3790 | 3792 | | |
3791 | 3793 | | |
| |||
3876 | 3878 | | |
3877 | 3879 | | |
3878 | 3880 | | |
| 3881 | + | |
| 3882 | + | |
| 3883 | + | |
| 3884 | + | |
| 3885 | + | |
| 3886 | + | |
| 3887 | + | |
| 3888 | + | |
| 3889 | + | |
| 3890 | + | |
| 3891 | + | |
| 3892 | + | |
| 3893 | + | |
3879 | 3894 | | |
3880 | 3895 | | |
3881 | 3896 | | |
| |||
3889 | 3904 | | |
3890 | 3905 | | |
3891 | 3906 | | |
| 3907 | + | |
3892 | 3908 | | |
3893 | 3909 | | |
3894 | 3910 | | |
| |||
4063 | 4079 | | |
4064 | 4080 | | |
4065 | 4081 | | |
4066 | | - | |
4067 | | - | |
| 4082 | + | |
| 4083 | + | |
| 4084 | + | |
| 4085 | + | |
| 4086 | + | |
| 4087 | + | |
| 4088 | + | |
| 4089 | + | |
| 4090 | + | |
| 4091 | + | |
| 4092 | + | |
| 4093 | + | |
| 4094 | + | |
| 4095 | + | |
| 4096 | + | |
| 4097 | + | |
4068 | 4098 | | |
4069 | 4099 | | |
4070 | 4100 | | |
| |||
4073 | 4103 | | |
4074 | 4104 | | |
4075 | 4105 | | |
4076 | | - | |
| 4106 | + | |
| 4107 | + | |
| 4108 | + | |
| 4109 | + | |
| 4110 | + | |
| 4111 | + | |
| 4112 | + | |
4077 | 4113 | | |
4078 | 4114 | | |
4079 | 4115 | | |
| |||
4086 | 4122 | | |
4087 | 4123 | | |
4088 | 4124 | | |
4089 | | - | |
| 4125 | + | |
4090 | 4126 | | |
4091 | 4127 | | |
4092 | 4128 | | |
| |||
4121 | 4157 | | |
4122 | 4158 | | |
4123 | 4159 | | |
4124 | | - | |
| 4160 | + | |
4125 | 4161 | | |
4126 | 4162 | | |
4127 | 4163 | | |
| |||
4140 | 4176 | | |
4141 | 4177 | | |
4142 | 4178 | | |
| 4179 | + | |
| 4180 | + | |
| 4181 | + | |
| 4182 | + | |
| 4183 | + | |
| 4184 | + | |
| 4185 | + | |
| 4186 | + | |
4143 | 4187 | | |
4144 | 4188 | | |
4145 | 4189 | | |
4146 | | - | |
| 4190 | + | |
| 4191 | + | |
4147 | 4192 | | |
4148 | 4193 | | |
4149 | 4194 | | |
4150 | 4195 | | |
4151 | 4196 | | |
4152 | | - | |
| 4197 | + | |
| 4198 | + | |
4153 | 4199 | | |
4154 | 4200 | | |
4155 | 4201 | | |
| |||
4171 | 4217 | | |
4172 | 4218 | | |
4173 | 4219 | | |
| 4220 | + | |
| 4221 | + | |
| 4222 | + | |
| 4223 | + | |
| 4224 | + | |
| 4225 | + | |
| 4226 | + | |
| 4227 | + | |
| 4228 | + | |
| 4229 | + | |
| 4230 | + | |
| 4231 | + | |
| 4232 | + | |
| 4233 | + | |
| 4234 | + | |
| 4235 | + | |
| 4236 | + | |
| 4237 | + | |
| 4238 | + | |
| 4239 | + | |
| 4240 | + | |
| 4241 | + | |
| 4242 | + | |
| 4243 | + | |
| 4244 | + | |
| 4245 | + | |
| 4246 | + | |
| 4247 | + | |
| 4248 | + | |
| 4249 | + | |
4174 | 4250 | | |
4175 | | - | |
| 4251 | + | |
4176 | 4252 | | |
4177 | 4253 | | |
4178 | 4254 | | |
| |||
0 commit comments