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
|
/*-
* Copyright (c) 2014-2016 MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* __cache_config_local --
* Configure the underlying cache.
*/
static int
__cache_config_local(WT_SESSION_IMPL *session, bool shared, const char *cfg[])
{
WT_CACHE *cache;
WT_CONFIG_ITEM cval;
WT_CONNECTION_IMPL *conn;
uint32_t evict_threads_max, evict_threads_min;
conn = S2C(session);
cache = conn->cache;
/*
* If not using a shared cache configure the cache size, otherwise
* check for a reserved size. All other settings are independent of
* whether we are using a shared cache or not.
*/
if (!shared) {
WT_RET(__wt_config_gets(session, cfg, "cache_size", &cval));
conn->cache_size = (uint64_t)cval.val;
}
WT_RET(__wt_config_gets(session, cfg, "cache_overhead", &cval));
cache->overhead_pct = (u_int)cval.val;
WT_RET(__wt_config_gets(session, cfg, "eviction_target", &cval));
cache->eviction_target = (u_int)cval.val;
WT_RET(__wt_config_gets(session, cfg, "eviction_trigger", &cval));
cache->eviction_trigger = (u_int)cval.val;
WT_RET(__wt_config_gets(
session, cfg, "eviction_checkpoint_target", &cval));
cache->eviction_checkpoint_target = (u_int)cval.val;
WT_RET(__wt_config_gets(session, cfg, "eviction_dirty_target", &cval));
cache->eviction_dirty_target = (u_int)cval.val;
/*
* Don't allow the dirty target to be larger than the overall
* target.
*/
if (cache->eviction_dirty_target > cache->eviction_target)
cache->eviction_dirty_target = cache->eviction_target;
/*
* Sanity check the checkpoint target: don't allow a value
* lower than the dirty target.
*/
if (cache->eviction_checkpoint_target > 0 &&
cache->eviction_checkpoint_target < cache->eviction_dirty_target)
cache->eviction_checkpoint_target =
cache->eviction_dirty_target;
WT_RET(__wt_config_gets(session, cfg, "eviction_dirty_trigger", &cval));
cache->eviction_dirty_trigger = (u_int)cval.val;
/*
* Don't allow the dirty trigger to be larger than the overall
* trigger or we can get stuck with a cache full of dirty data.
*/
if (cache->eviction_dirty_trigger > cache->eviction_trigger)
cache->eviction_dirty_trigger = cache->eviction_trigger;
WT_RET(__wt_config_gets(session, cfg, "eviction.threads_max", &cval));
WT_ASSERT(session, cval.val > 0);
evict_threads_max = (uint32_t)cval.val;
WT_RET(__wt_config_gets(session, cfg, "eviction.threads_min", &cval));
WT_ASSERT(session, cval.val > 0);
evict_threads_min = (uint32_t)cval.val;
if (evict_threads_min > evict_threads_max)
WT_RET_MSG(session, EINVAL,
"eviction=(threads_min) cannot be greater than "
"eviction=(threads_max)");
conn->evict_threads_max = evict_threads_max;
conn->evict_threads_min = evict_threads_min;
return (0);
}
/*
* __wt_cache_config --
* Configure or reconfigure the current cache and shared cache.
*/
int
__wt_cache_config(WT_SESSION_IMPL *session, bool reconfigure, const char *cfg[])
{
WT_CONFIG_ITEM cval;
WT_CONNECTION_IMPL *conn;
bool now_shared, was_shared;
conn = S2C(session);
WT_ASSERT(session, conn->cache != NULL);
WT_RET(__wt_config_gets_none(session, cfg, "shared_cache.name", &cval));
now_shared = cval.len != 0;
was_shared = F_ISSET(conn, WT_CONN_CACHE_POOL);
/* Cleanup if reconfiguring */
if (reconfigure && was_shared && !now_shared)
/* Remove ourselves from the pool if necessary */
WT_RET(__wt_conn_cache_pool_destroy(session));
else if (reconfigure && !was_shared && now_shared)
/*
* Cache size will now be managed by the cache pool - the
* start size always needs to be zero to allow the pool to
* manage how much memory is in-use.
*/
conn->cache_size = 0;
/*
* Always setup the local cache - it's used even if we are
* participating in a shared cache.
*/
WT_RET(__cache_config_local(session, now_shared, cfg));
if (now_shared) {
WT_RET(__wt_cache_pool_config(session, cfg));
WT_ASSERT(session, F_ISSET(conn, WT_CONN_CACHE_POOL));
if (!was_shared)
WT_RET(__wt_conn_cache_pool_open(session));
}
/*
* Resize the thread group if reconfiguring, otherwise the thread group
* will be initialized as part of creating the cache.
*/
if (reconfigure)
WT_RET(__wt_thread_group_resize(
session, &conn->evict_threads,
conn->evict_threads_min,
conn->evict_threads_max,
WT_THREAD_CAN_WAIT | WT_THREAD_PANIC_FAIL));
return (0);
}
/*
* __wt_cache_create --
* Create the underlying cache.
*/
int
__wt_cache_create(WT_SESSION_IMPL *session, const char *cfg[])
{
WT_CACHE *cache;
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
int i;
conn = S2C(session);
WT_ASSERT(session, conn->cache == NULL);
WT_RET(__wt_calloc_one(session, &conn->cache));
cache = conn->cache;
/* Use a common routine for run-time configuration options. */
WT_RET(__wt_cache_config(session, false, cfg));
/*
* The lowest possible page read-generation has a special meaning, it
* marks a page for forcible eviction; don't let it happen by accident.
*/
cache->read_gen = WT_READGEN_START_VALUE;
/*
* The target size must be lower than the trigger size or we will never
* get any work done.
*/
if (cache->eviction_target >= cache->eviction_trigger)
WT_RET_MSG(session, EINVAL,
"eviction target must be lower than the eviction trigger");
WT_RET(__wt_cond_auto_alloc(session,
"cache eviction server", 10000, WT_MILLION, &cache->evict_cond));
WT_RET(__wt_spin_init(session, &cache->evict_pass_lock, "evict pass"));
WT_RET(__wt_spin_init(session,
&cache->evict_queue_lock, "cache eviction queue"));
WT_RET(__wt_spin_init(session, &cache->evict_walk_lock, "cache walk"));
if ((ret = __wt_open_internal_session(conn, "evict pass",
false, WT_SESSION_NO_DATA_HANDLES, &cache->walk_session)) != 0)
WT_RET_MSG(NULL, ret,
"Failed to create session for eviction walks");
/* Allocate the LRU eviction queue. */
cache->evict_slots = WT_EVICT_WALK_BASE + WT_EVICT_WALK_INCR;
for (i = 0; i < WT_EVICT_QUEUE_MAX; ++i) {
WT_RET(__wt_calloc_def(session,
cache->evict_slots, &cache->evict_queues[i].evict_queue));
WT_RET(__wt_spin_init(session,
&cache->evict_queues[i].evict_lock, "cache eviction"));
}
/* Ensure there are always non-NULL queues. */
cache->evict_current_queue = cache->evict_fill_queue =
&cache->evict_queues[0];
cache->evict_other_queue = &cache->evict_queues[1];
cache->evict_urgent_queue = &cache->evict_queues[WT_EVICT_URGENT_QUEUE];
/*
* We get/set some values in the cache statistics (rather than have
* two copies), configure them.
*/
__wt_cache_stats_update(session);
return (0);
}
/*
* __wt_cache_stats_update --
* Update the cache statistics for return to the application.
*/
void
__wt_cache_stats_update(WT_SESSION_IMPL *session)
{
WT_CACHE *cache;
WT_CONNECTION_IMPL *conn;
WT_CONNECTION_STATS **stats;
uint64_t inuse, leaf;
conn = S2C(session);
cache = conn->cache;
stats = conn->stats;
inuse = __wt_cache_bytes_inuse(cache);
/*
* There are races updating the different cache tracking values so
* be paranoid calculating the leaf byte usage.
*/
leaf = inuse > cache->bytes_internal ?
inuse - cache->bytes_internal : 0;
WT_STAT_SET(session, stats, cache_bytes_max, conn->cache_size);
WT_STAT_SET(session, stats, cache_bytes_inuse, inuse);
WT_STAT_SET(session, stats, cache_overhead, cache->overhead_pct);
WT_STAT_SET(
session, stats, cache_bytes_dirty, __wt_cache_dirty_inuse(cache));
WT_STAT_SET(
session, stats, cache_bytes_image, __wt_cache_bytes_image(cache));
WT_STAT_SET(
session, stats, cache_pages_inuse, __wt_cache_pages_inuse(cache));
WT_STAT_SET(
session, stats, cache_bytes_internal, cache->bytes_internal);
WT_STAT_SET(session, stats, cache_bytes_leaf, leaf);
WT_STAT_SET(
session, stats, cache_bytes_other, __wt_cache_bytes_other(cache));
WT_STAT_SET(session, stats,
cache_eviction_maximum_page_size, cache->evict_max_page_size);
WT_STAT_SET(session, stats, cache_pages_dirty,
cache->pages_dirty_intl + cache->pages_dirty_leaf);
/*
* The number of files with active walks ~= number of hazard pointers
* in the walk session. Note: reading without locking.
*/
if (conn->evict_server_running)
WT_STAT_SET(session, stats, cache_eviction_walks_active,
cache->walk_session->nhazard);
}
/*
* __wt_cache_destroy --
* Discard the underlying cache.
*/
int
__wt_cache_destroy(WT_SESSION_IMPL *session)
{
WT_CACHE *cache;
WT_CONNECTION_IMPL *conn;
WT_DECL_RET;
WT_SESSION *wt_session;
int i;
conn = S2C(session);
cache = conn->cache;
if (cache == NULL)
return (0);
/* The cache should be empty at this point. Complain if not. */
if (cache->pages_inmem != cache->pages_evict)
__wt_errx(session,
"cache server: exiting with %" PRIu64 " pages in "
"memory and %" PRIu64 " pages evicted",
cache->pages_inmem, cache->pages_evict);
if (cache->bytes_inmem != 0)
__wt_errx(session,
"cache server: exiting with %" PRIu64 " bytes in memory",
cache->bytes_inmem);
if (cache->bytes_dirty_intl + cache->bytes_dirty_leaf != 0 ||
cache->pages_dirty_intl + cache->pages_dirty_leaf != 0)
__wt_errx(session,
"cache server: exiting with %" PRIu64
" bytes dirty and %" PRIu64 " pages dirty",
cache->bytes_dirty_intl + cache->bytes_dirty_leaf,
cache->pages_dirty_intl + cache->pages_dirty_leaf);
WT_TRET(__wt_cond_destroy(session, &cache->evict_cond));
__wt_spin_destroy(session, &cache->evict_pass_lock);
__wt_spin_destroy(session, &cache->evict_queue_lock);
__wt_spin_destroy(session, &cache->evict_walk_lock);
wt_session = &cache->walk_session->iface;
if (wt_session != NULL)
WT_TRET(wt_session->close(wt_session, NULL));
for (i = 0; i < WT_EVICT_QUEUE_MAX; ++i) {
__wt_spin_destroy(session, &cache->evict_queues[i].evict_lock);
__wt_free(session, cache->evict_queues[i].evict_queue);
}
__wt_free(session, conn->cache);
return (ret);
}
|