Print this page
4045 zfs write throttle & i/o scheduler performance work
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/txg.c
+++ new/usr/src/uts/common/fs/zfs/txg.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Portions Copyright 2011 Martin Matuska
24 24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 25 */
26 26
27 27 #include <sys/zfs_context.h>
28 28 #include <sys/txg_impl.h>
29 29 #include <sys/dmu_impl.h>
30 30 #include <sys/dmu_tx.h>
31 31 #include <sys/dsl_pool.h>
32 32 #include <sys/dsl_scan.h>
33 33 #include <sys/callb.h>
34 34
35 35 /*
36 36 * ZFS Transaction Groups
37 37 * ----------------------
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
38 38 *
39 39 * ZFS transaction groups are, as the name implies, groups of transactions
40 40 * that act on persistent state. ZFS asserts consistency at the granularity of
41 41 * these transaction groups. Each successive transaction group (txg) is
42 42 * assigned a 64-bit consecutive identifier. There are three active
43 43 * transaction group states: open, quiescing, or syncing. At any given time,
44 44 * there may be an active txg associated with each state; each active txg may
45 45 * either be processing, or blocked waiting to enter the next state. There may
46 46 * be up to three active txgs, and there is always a txg in the open state
47 47 * (though it may be blocked waiting to enter the quiescing state). In broad
48 - * strokes, transactions — operations that change in-memory structures — are
48 + * strokes, transactions -- operations that change in-memory structures -- are
49 49 * accepted into the txg in the open state, and are completed while the txg is
50 50 * in the open or quiescing states. The accumulated changes are written to
51 51 * disk in the syncing state.
52 52 *
53 53 * Open
54 54 *
55 55 * When a new txg becomes active, it first enters the open state. New
56 - * transactions — updates to in-memory structures — are assigned to the
56 + * transactions -- updates to in-memory structures -- are assigned to the
57 57 * currently open txg. There is always a txg in the open state so that ZFS can
58 58 * accept new changes (though the txg may refuse new changes if it has hit
59 59 * some limit). ZFS advances the open txg to the next state for a variety of
60 60 * reasons such as it hitting a time or size threshold, or the execution of an
61 61 * administrative action that must be completed in the syncing state.
62 62 *
63 63 * Quiescing
64 64 *
65 65 * After a txg exits the open state, it enters the quiescing state. The
66 66 * quiescing state is intended to provide a buffer between accepting new
67 67 * transactions in the open state and writing them out to stable storage in
68 68 * the syncing state. While quiescing, transactions can continue their
69 69 * operation without delaying either of the other states. Typically, a txg is
70 70 * in the quiescing state very briefly since the operations are bounded by
71 71 * software latencies rather than, say, slower I/O latencies. After all
72 72 * transactions complete, the txg is ready to enter the next state.
73 73 *
74 74 * Syncing
75 75 *
76 76 * In the syncing state, the in-memory state built up during the open and (to
77 77 * a lesser degree) the quiescing states is written to stable storage. The
78 78 * process of writing out modified data can, in turn modify more data. For
79 79 * example when we write new blocks, we need to allocate space for them; those
80 80 * allocations modify metadata (space maps)... which themselves must be
81 81 * written to stable storage. During the sync state, ZFS iterates, writing out
82 82 * data until it converges and all in-memory changes have been written out.
83 83 * The first such pass is the largest as it encompasses all the modified user
84 84 * data (as opposed to filesystem metadata). Subsequent passes typically have
85 85 * far less data to write as they consist exclusively of filesystem metadata.
86 86 *
87 87 * To ensure convergence, after a certain number of passes ZFS begins
88 88 * overwriting locations on stable storage that had been allocated earlier in
89 89 * the syncing state (and subsequently freed). ZFS usually allocates new
90 90 * blocks to optimize for large, continuous, writes. For the syncing state to
91 91 * converge however it must complete a pass where no new blocks are allocated
92 92 * since each allocation requires a modification of persistent metadata.
93 93 * Further, to hasten convergence, after a prescribed number of passes, ZFS
94 94 * also defers frees, and stops compressing.
95 95 *
96 96 * In addition to writing out user data, we must also execute synctasks during
97 97 * the syncing context. A synctask is the mechanism by which some
98 98 * administrative activities work such as creating and destroying snapshots or
99 99 * datasets. Note that when a synctask is initiated it enters the open txg,
100 100 * and ZFS then pushes that txg as quickly as possible to completion of the
101 101 * syncing state in order to reduce the latency of the administrative
102 102 * activity. To complete the syncing state, ZFS writes out a new uberblock,
103 103 * the root of the tree of blocks that comprise all state stored on the ZFS
104 104 * pool. Finally, if there is a quiesced txg waiting, we signal that it can
105 105 * now transition to the syncing state.
106 106 */
107 107
108 108 static void txg_sync_thread(dsl_pool_t *dp);
109 109 static void txg_quiesce_thread(dsl_pool_t *dp);
110 110
111 111 int zfs_txg_timeout = 5; /* max seconds worth of delta per txg */
112 112
113 113 /*
114 114 * Prepare the txg subsystem.
115 115 */
116 116 void
117 117 txg_init(dsl_pool_t *dp, uint64_t txg)
118 118 {
119 119 tx_state_t *tx = &dp->dp_tx;
120 120 int c;
121 121 bzero(tx, sizeof (tx_state_t));
122 122
123 123 tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
124 124
125 125 for (c = 0; c < max_ncpus; c++) {
126 126 int i;
127 127
128 128 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
129 129 mutex_init(&tx->tx_cpu[c].tc_open_lock, NULL, MUTEX_DEFAULT,
130 130 NULL);
131 131 for (i = 0; i < TXG_SIZE; i++) {
132 132 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
133 133 NULL);
134 134 list_create(&tx->tx_cpu[c].tc_callbacks[i],
135 135 sizeof (dmu_tx_callback_t),
136 136 offsetof(dmu_tx_callback_t, dcb_node));
137 137 }
138 138 }
139 139
140 140 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
141 141
142 142 cv_init(&tx->tx_sync_more_cv, NULL, CV_DEFAULT, NULL);
143 143 cv_init(&tx->tx_sync_done_cv, NULL, CV_DEFAULT, NULL);
144 144 cv_init(&tx->tx_quiesce_more_cv, NULL, CV_DEFAULT, NULL);
145 145 cv_init(&tx->tx_quiesce_done_cv, NULL, CV_DEFAULT, NULL);
146 146 cv_init(&tx->tx_exit_cv, NULL, CV_DEFAULT, NULL);
147 147
148 148 tx->tx_open_txg = txg;
149 149 }
150 150
151 151 /*
152 152 * Close down the txg subsystem.
153 153 */
154 154 void
155 155 txg_fini(dsl_pool_t *dp)
156 156 {
157 157 tx_state_t *tx = &dp->dp_tx;
158 158 int c;
159 159
160 160 ASSERT(tx->tx_threads == 0);
161 161
162 162 mutex_destroy(&tx->tx_sync_lock);
163 163
164 164 cv_destroy(&tx->tx_sync_more_cv);
165 165 cv_destroy(&tx->tx_sync_done_cv);
166 166 cv_destroy(&tx->tx_quiesce_more_cv);
167 167 cv_destroy(&tx->tx_quiesce_done_cv);
168 168 cv_destroy(&tx->tx_exit_cv);
169 169
170 170 for (c = 0; c < max_ncpus; c++) {
171 171 int i;
172 172
173 173 mutex_destroy(&tx->tx_cpu[c].tc_open_lock);
174 174 mutex_destroy(&tx->tx_cpu[c].tc_lock);
175 175 for (i = 0; i < TXG_SIZE; i++) {
176 176 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
177 177 list_destroy(&tx->tx_cpu[c].tc_callbacks[i]);
178 178 }
179 179 }
180 180
181 181 if (tx->tx_commit_cb_taskq != NULL)
182 182 taskq_destroy(tx->tx_commit_cb_taskq);
183 183
184 184 kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
185 185
186 186 bzero(tx, sizeof (tx_state_t));
187 187 }
188 188
189 189 /*
190 190 * Start syncing transaction groups.
191 191 */
192 192 void
193 193 txg_sync_start(dsl_pool_t *dp)
194 194 {
195 195 tx_state_t *tx = &dp->dp_tx;
196 196
197 197 mutex_enter(&tx->tx_sync_lock);
198 198
199 199 dprintf("pool %p\n", dp);
200 200
201 201 ASSERT(tx->tx_threads == 0);
202 202
203 203 tx->tx_threads = 2;
204 204
205 205 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
206 206 dp, 0, &p0, TS_RUN, minclsyspri);
207 207
208 208 /*
209 209 * The sync thread can need a larger-than-default stack size on
210 210 * 32-bit x86. This is due in part to nested pools and
211 211 * scrub_visitbp() recursion.
212 212 */
213 213 tx->tx_sync_thread = thread_create(NULL, 32<<10, txg_sync_thread,
214 214 dp, 0, &p0, TS_RUN, minclsyspri);
215 215
216 216 mutex_exit(&tx->tx_sync_lock);
217 217 }
218 218
219 219 static void
220 220 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
221 221 {
222 222 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
223 223 mutex_enter(&tx->tx_sync_lock);
224 224 }
225 225
226 226 static void
227 227 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
228 228 {
229 229 ASSERT(*tpp != NULL);
230 230 *tpp = NULL;
231 231 tx->tx_threads--;
232 232 cv_broadcast(&tx->tx_exit_cv);
233 233 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
234 234 thread_exit();
235 235 }
236 236
237 237 static void
238 238 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, clock_t time)
239 239 {
240 240 CALLB_CPR_SAFE_BEGIN(cpr);
241 241
242 242 if (time)
243 243 (void) cv_timedwait(cv, &tx->tx_sync_lock,
244 244 ddi_get_lbolt() + time);
245 245 else
246 246 cv_wait(cv, &tx->tx_sync_lock);
247 247
248 248 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
249 249 }
250 250
251 251 /*
252 252 * Stop syncing transaction groups.
253 253 */
254 254 void
255 255 txg_sync_stop(dsl_pool_t *dp)
256 256 {
257 257 tx_state_t *tx = &dp->dp_tx;
258 258
259 259 dprintf("pool %p\n", dp);
260 260 /*
261 261 * Finish off any work in progress.
262 262 */
263 263 ASSERT(tx->tx_threads == 2);
264 264
265 265 /*
266 266 * We need to ensure that we've vacated the deferred space_maps.
267 267 */
268 268 txg_wait_synced(dp, tx->tx_open_txg + TXG_DEFER_SIZE);
269 269
270 270 /*
271 271 * Wake all sync threads and wait for them to die.
272 272 */
273 273 mutex_enter(&tx->tx_sync_lock);
274 274
275 275 ASSERT(tx->tx_threads == 2);
276 276
277 277 tx->tx_exiting = 1;
278 278
279 279 cv_broadcast(&tx->tx_quiesce_more_cv);
280 280 cv_broadcast(&tx->tx_quiesce_done_cv);
281 281 cv_broadcast(&tx->tx_sync_more_cv);
282 282
283 283 while (tx->tx_threads != 0)
284 284 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
285 285
286 286 tx->tx_exiting = 0;
287 287
288 288 mutex_exit(&tx->tx_sync_lock);
289 289 }
290 290
291 291 uint64_t
292 292 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
293 293 {
294 294 tx_state_t *tx = &dp->dp_tx;
295 295 tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
296 296 uint64_t txg;
297 297
298 298 mutex_enter(&tc->tc_open_lock);
299 299 txg = tx->tx_open_txg;
300 300
301 301 mutex_enter(&tc->tc_lock);
302 302 tc->tc_count[txg & TXG_MASK]++;
303 303 mutex_exit(&tc->tc_lock);
304 304
305 305 th->th_cpu = tc;
306 306 th->th_txg = txg;
307 307
308 308 return (txg);
309 309 }
310 310
311 311 void
312 312 txg_rele_to_quiesce(txg_handle_t *th)
313 313 {
314 314 tx_cpu_t *tc = th->th_cpu;
315 315
316 316 ASSERT(!MUTEX_HELD(&tc->tc_lock));
317 317 mutex_exit(&tc->tc_open_lock);
318 318 }
319 319
320 320 void
321 321 txg_register_callbacks(txg_handle_t *th, list_t *tx_callbacks)
322 322 {
323 323 tx_cpu_t *tc = th->th_cpu;
324 324 int g = th->th_txg & TXG_MASK;
325 325
326 326 mutex_enter(&tc->tc_lock);
327 327 list_move_tail(&tc->tc_callbacks[g], tx_callbacks);
328 328 mutex_exit(&tc->tc_lock);
329 329 }
330 330
331 331 void
332 332 txg_rele_to_sync(txg_handle_t *th)
333 333 {
334 334 tx_cpu_t *tc = th->th_cpu;
335 335 int g = th->th_txg & TXG_MASK;
336 336
337 337 mutex_enter(&tc->tc_lock);
338 338 ASSERT(tc->tc_count[g] != 0);
339 339 if (--tc->tc_count[g] == 0)
340 340 cv_broadcast(&tc->tc_cv[g]);
341 341 mutex_exit(&tc->tc_lock);
342 342
343 343 th->th_cpu = NULL; /* defensive */
344 344 }
345 345
346 346 /*
347 347 * Blocks until all transactions in the group are committed.
348 348 *
349 349 * On return, the transaction group has reached a stable state in which it can
350 350 * then be passed off to the syncing context.
351 351 */
352 352 static void
353 353 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
354 354 {
355 355 tx_state_t *tx = &dp->dp_tx;
356 356 int g = txg & TXG_MASK;
↓ open down ↓ |
290 lines elided |
↑ open up ↑ |
357 357 int c;
358 358
359 359 /*
360 360 * Grab all tc_open_locks so nobody else can get into this txg.
361 361 */
362 362 for (c = 0; c < max_ncpus; c++)
363 363 mutex_enter(&tx->tx_cpu[c].tc_open_lock);
364 364
365 365 ASSERT(txg == tx->tx_open_txg);
366 366 tx->tx_open_txg++;
367 + tx->tx_open_time = gethrtime();
367 368
368 369 DTRACE_PROBE2(txg__quiescing, dsl_pool_t *, dp, uint64_t, txg);
369 370 DTRACE_PROBE2(txg__opened, dsl_pool_t *, dp, uint64_t, tx->tx_open_txg);
370 371
371 372 /*
372 373 * Now that we've incremented tx_open_txg, we can let threads
373 374 * enter the next transaction group.
374 375 */
375 376 for (c = 0; c < max_ncpus; c++)
376 377 mutex_exit(&tx->tx_cpu[c].tc_open_lock);
377 378
378 379 /*
379 380 * Quiesce the transaction group by waiting for everyone to txg_exit().
380 381 */
381 382 for (c = 0; c < max_ncpus; c++) {
382 383 tx_cpu_t *tc = &tx->tx_cpu[c];
383 384 mutex_enter(&tc->tc_lock);
384 385 while (tc->tc_count[g] != 0)
385 386 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
386 387 mutex_exit(&tc->tc_lock);
387 388 }
388 389 }
389 390
390 391 static void
391 392 txg_do_callbacks(list_t *cb_list)
392 393 {
393 394 dmu_tx_do_callbacks(cb_list, 0);
394 395
395 396 list_destroy(cb_list);
396 397
397 398 kmem_free(cb_list, sizeof (list_t));
398 399 }
399 400
400 401 /*
401 402 * Dispatch the commit callbacks registered on this txg to worker threads.
402 403 *
403 404 * If no callbacks are registered for a given TXG, nothing happens.
404 405 * This function creates a taskq for the associated pool, if needed.
405 406 */
406 407 static void
407 408 txg_dispatch_callbacks(dsl_pool_t *dp, uint64_t txg)
408 409 {
409 410 int c;
410 411 tx_state_t *tx = &dp->dp_tx;
411 412 list_t *cb_list;
412 413
413 414 for (c = 0; c < max_ncpus; c++) {
414 415 tx_cpu_t *tc = &tx->tx_cpu[c];
415 416 /*
416 417 * No need to lock tx_cpu_t at this point, since this can
417 418 * only be called once a txg has been synced.
418 419 */
419 420
420 421 int g = txg & TXG_MASK;
421 422
422 423 if (list_is_empty(&tc->tc_callbacks[g]))
423 424 continue;
424 425
425 426 if (tx->tx_commit_cb_taskq == NULL) {
426 427 /*
427 428 * Commit callback taskq hasn't been created yet.
428 429 */
429 430 tx->tx_commit_cb_taskq = taskq_create("tx_commit_cb",
430 431 max_ncpus, minclsyspri, max_ncpus, max_ncpus * 2,
431 432 TASKQ_PREPOPULATE);
432 433 }
433 434
434 435 cb_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
435 436 list_create(cb_list, sizeof (dmu_tx_callback_t),
436 437 offsetof(dmu_tx_callback_t, dcb_node));
437 438
438 439 list_move_tail(cb_list, &tc->tc_callbacks[g]);
439 440
440 441 (void) taskq_dispatch(tx->tx_commit_cb_taskq, (task_func_t *)
441 442 txg_do_callbacks, cb_list, TQ_SLEEP);
442 443 }
443 444 }
444 445
445 446 static void
446 447 txg_sync_thread(dsl_pool_t *dp)
↓ open down ↓ |
70 lines elided |
↑ open up ↑ |
447 448 {
448 449 spa_t *spa = dp->dp_spa;
449 450 tx_state_t *tx = &dp->dp_tx;
450 451 callb_cpr_t cpr;
451 452 uint64_t start, delta;
452 453
453 454 txg_thread_enter(tx, &cpr);
454 455
455 456 start = delta = 0;
456 457 for (;;) {
457 - uint64_t timer, timeout = zfs_txg_timeout * hz;
458 + uint64_t timeout = zfs_txg_timeout * hz;
459 + uint64_t timer;
458 460 uint64_t txg;
459 461
460 462 /*
461 463 * We sync when we're scanning, there's someone waiting
462 464 * on us, or the quiesce thread has handed off a txg to
463 465 * us, or we have reached our timeout.
464 466 */
465 467 timer = (delta >= timeout ? 0 : timeout - delta);
466 468 while (!dsl_scan_active(dp->dp_scan) &&
467 469 !tx->tx_exiting && timer > 0 &&
468 470 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
469 - tx->tx_quiesced_txg == 0) {
471 + tx->tx_quiesced_txg == 0 &&
472 + dp->dp_dirty_total < zfs_dirty_data_sync) {
470 473 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
471 474 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
472 475 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
473 476 delta = ddi_get_lbolt() - start;
474 477 timer = (delta > timeout ? 0 : timeout - delta);
475 478 }
476 479
477 480 /*
478 481 * Wait until the quiesce thread hands off a txg to us,
479 482 * prompting it to do so if necessary.
480 483 */
481 484 while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
482 485 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
483 486 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
484 487 cv_broadcast(&tx->tx_quiesce_more_cv);
485 488 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
486 489 }
487 490
488 491 if (tx->tx_exiting)
489 492 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
490 493
491 494 /*
492 495 * Consume the quiesced txg which has been handed off to
493 496 * us. This may cause the quiescing thread to now be
494 497 * able to quiesce another txg, so we must signal it.
495 498 */
496 499 txg = tx->tx_quiesced_txg;
497 500 tx->tx_quiesced_txg = 0;
498 501 tx->tx_syncing_txg = txg;
499 502 DTRACE_PROBE2(txg__syncing, dsl_pool_t *, dp, uint64_t, txg);
500 503 cv_broadcast(&tx->tx_quiesce_more_cv);
501 504
502 505 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
503 506 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
504 507 mutex_exit(&tx->tx_sync_lock);
505 508
506 509 start = ddi_get_lbolt();
507 510 spa_sync(spa, txg);
508 511 delta = ddi_get_lbolt() - start;
509 512
510 513 mutex_enter(&tx->tx_sync_lock);
511 514 tx->tx_synced_txg = txg;
512 515 tx->tx_syncing_txg = 0;
513 516 DTRACE_PROBE2(txg__synced, dsl_pool_t *, dp, uint64_t, txg);
514 517 cv_broadcast(&tx->tx_sync_done_cv);
515 518
516 519 /*
517 520 * Dispatch commit callbacks to worker threads.
518 521 */
519 522 txg_dispatch_callbacks(dp, txg);
520 523 }
521 524 }
522 525
523 526 static void
524 527 txg_quiesce_thread(dsl_pool_t *dp)
525 528 {
526 529 tx_state_t *tx = &dp->dp_tx;
527 530 callb_cpr_t cpr;
528 531
529 532 txg_thread_enter(tx, &cpr);
530 533
531 534 for (;;) {
532 535 uint64_t txg;
533 536
534 537 /*
535 538 * We quiesce when there's someone waiting on us.
536 539 * However, we can only have one txg in "quiescing" or
537 540 * "quiesced, waiting to sync" state. So we wait until
538 541 * the "quiesced, waiting to sync" txg has been consumed
539 542 * by the sync thread.
540 543 */
541 544 while (!tx->tx_exiting &&
542 545 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
543 546 tx->tx_quiesced_txg != 0))
544 547 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
545 548
546 549 if (tx->tx_exiting)
547 550 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
548 551
549 552 txg = tx->tx_open_txg;
550 553 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
551 554 txg, tx->tx_quiesce_txg_waiting,
552 555 tx->tx_sync_txg_waiting);
553 556 mutex_exit(&tx->tx_sync_lock);
554 557 txg_quiesce(dp, txg);
555 558 mutex_enter(&tx->tx_sync_lock);
556 559
557 560 /*
558 561 * Hand this txg off to the sync thread.
559 562 */
560 563 dprintf("quiesce done, handing off txg %llu\n", txg);
561 564 tx->tx_quiesced_txg = txg;
562 565 DTRACE_PROBE2(txg__quiesced, dsl_pool_t *, dp, uint64_t, txg);
563 566 cv_broadcast(&tx->tx_sync_more_cv);
564 567 cv_broadcast(&tx->tx_quiesce_done_cv);
565 568 }
566 569 }
567 570
568 571 /*
569 572 * Delay this thread by delay nanoseconds if we are still in the open
570 573 * transaction group and there is already a waiting txg quiescing or quiesced.
571 574 * Abort the delay if this txg stalls or enters the quiescing state.
572 575 */
573 576 void
574 577 txg_delay(dsl_pool_t *dp, uint64_t txg, hrtime_t delay, hrtime_t resolution)
575 578 {
576 579 tx_state_t *tx = &dp->dp_tx;
577 580 hrtime_t start = gethrtime();
578 581
579 582 /* don't delay if this txg could transition to quiescing immediately */
580 583 if (tx->tx_open_txg > txg ||
581 584 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
582 585 return;
583 586
584 587 mutex_enter(&tx->tx_sync_lock);
585 588 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
586 589 mutex_exit(&tx->tx_sync_lock);
587 590 return;
588 591 }
589 592
590 593 while (gethrtime() - start < delay &&
591 594 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp)) {
592 595 (void) cv_timedwait_hires(&tx->tx_quiesce_more_cv,
593 596 &tx->tx_sync_lock, delay, resolution, 0);
594 597 }
595 598
596 599 mutex_exit(&tx->tx_sync_lock);
597 600 }
598 601
599 602 void
600 603 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
601 604 {
602 605 tx_state_t *tx = &dp->dp_tx;
603 606
604 607 ASSERT(!dsl_pool_config_held(dp));
605 608
606 609 mutex_enter(&tx->tx_sync_lock);
607 610 ASSERT(tx->tx_threads == 2);
608 611 if (txg == 0)
609 612 txg = tx->tx_open_txg + TXG_DEFER_SIZE;
610 613 if (tx->tx_sync_txg_waiting < txg)
611 614 tx->tx_sync_txg_waiting = txg;
612 615 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
613 616 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
614 617 while (tx->tx_synced_txg < txg) {
615 618 dprintf("broadcasting sync more "
616 619 "tx_synced=%llu waiting=%llu dp=%p\n",
617 620 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
618 621 cv_broadcast(&tx->tx_sync_more_cv);
619 622 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
620 623 }
621 624 mutex_exit(&tx->tx_sync_lock);
622 625 }
623 626
624 627 void
625 628 txg_wait_open(dsl_pool_t *dp, uint64_t txg)
626 629 {
627 630 tx_state_t *tx = &dp->dp_tx;
628 631
629 632 ASSERT(!dsl_pool_config_held(dp));
630 633
631 634 mutex_enter(&tx->tx_sync_lock);
632 635 ASSERT(tx->tx_threads == 2);
633 636 if (txg == 0)
↓ open down ↓ |
154 lines elided |
↑ open up ↑ |
634 637 txg = tx->tx_open_txg + 1;
635 638 if (tx->tx_quiesce_txg_waiting < txg)
636 639 tx->tx_quiesce_txg_waiting = txg;
637 640 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
638 641 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
639 642 while (tx->tx_open_txg < txg) {
640 643 cv_broadcast(&tx->tx_quiesce_more_cv);
641 644 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
642 645 }
643 646 mutex_exit(&tx->tx_sync_lock);
647 +}
648 +
649 +/*
650 + * If there isn't a txg syncing or in the pipeline, push another txg through
651 + * the pipeline by queiscing the open txg.
652 + */
653 +void
654 +txg_kick(dsl_pool_t *dp)
655 +{
656 + tx_state_t *tx = &dp->dp_tx;
657 +
658 + ASSERT(!dsl_pool_config_held(dp));
659 +
660 + mutex_enter(&tx->tx_sync_lock);
661 + if (tx->tx_syncing_txg == 0 &&
662 + tx->tx_quiesce_txg_waiting <= tx->tx_open_txg &&
663 + tx->tx_sync_txg_waiting <= tx->tx_synced_txg &&
664 + tx->tx_quiesced_txg <= tx->tx_synced_txg) {
665 + tx->tx_quiesce_txg_waiting = tx->tx_open_txg + 1;
666 + cv_broadcast(&tx->tx_quiesce_more_cv);
667 + }
668 + mutex_exit(&tx->tx_sync_lock);
644 669 }
645 670
646 671 boolean_t
647 672 txg_stalled(dsl_pool_t *dp)
648 673 {
649 674 tx_state_t *tx = &dp->dp_tx;
650 675 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
651 676 }
652 677
653 678 boolean_t
654 679 txg_sync_waiting(dsl_pool_t *dp)
655 680 {
656 681 tx_state_t *tx = &dp->dp_tx;
657 682
658 683 return (tx->tx_syncing_txg <= tx->tx_sync_txg_waiting ||
659 684 tx->tx_quiesced_txg != 0);
660 685 }
661 686
662 687 /*
663 688 * Per-txg object lists.
664 689 */
665 690 void
666 691 txg_list_create(txg_list_t *tl, size_t offset)
667 692 {
668 693 int t;
669 694
670 695 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
671 696
672 697 tl->tl_offset = offset;
673 698
674 699 for (t = 0; t < TXG_SIZE; t++)
675 700 tl->tl_head[t] = NULL;
676 701 }
677 702
678 703 void
679 704 txg_list_destroy(txg_list_t *tl)
680 705 {
681 706 int t;
682 707
683 708 for (t = 0; t < TXG_SIZE; t++)
684 709 ASSERT(txg_list_empty(tl, t));
685 710
686 711 mutex_destroy(&tl->tl_lock);
687 712 }
688 713
689 714 boolean_t
690 715 txg_list_empty(txg_list_t *tl, uint64_t txg)
691 716 {
692 717 return (tl->tl_head[txg & TXG_MASK] == NULL);
693 718 }
694 719
695 720 /*
696 721 * Add an entry to the list (unless it's already on the list).
697 722 * Returns B_TRUE if it was actually added.
698 723 */
699 724 boolean_t
700 725 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
701 726 {
702 727 int t = txg & TXG_MASK;
703 728 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
704 729 boolean_t add;
705 730
706 731 mutex_enter(&tl->tl_lock);
707 732 add = (tn->tn_member[t] == 0);
708 733 if (add) {
709 734 tn->tn_member[t] = 1;
710 735 tn->tn_next[t] = tl->tl_head[t];
711 736 tl->tl_head[t] = tn;
712 737 }
713 738 mutex_exit(&tl->tl_lock);
714 739
715 740 return (add);
716 741 }
717 742
718 743 /*
719 744 * Add an entry to the end of the list, unless it's already on the list.
720 745 * (walks list to find end)
721 746 * Returns B_TRUE if it was actually added.
722 747 */
723 748 boolean_t
724 749 txg_list_add_tail(txg_list_t *tl, void *p, uint64_t txg)
725 750 {
726 751 int t = txg & TXG_MASK;
727 752 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
728 753 boolean_t add;
729 754
730 755 mutex_enter(&tl->tl_lock);
731 756 add = (tn->tn_member[t] == 0);
732 757 if (add) {
733 758 txg_node_t **tp;
734 759
735 760 for (tp = &tl->tl_head[t]; *tp != NULL; tp = &(*tp)->tn_next[t])
736 761 continue;
737 762
738 763 tn->tn_member[t] = 1;
739 764 tn->tn_next[t] = NULL;
740 765 *tp = tn;
741 766 }
742 767 mutex_exit(&tl->tl_lock);
743 768
744 769 return (add);
745 770 }
746 771
747 772 /*
748 773 * Remove the head of the list and return it.
749 774 */
750 775 void *
751 776 txg_list_remove(txg_list_t *tl, uint64_t txg)
752 777 {
753 778 int t = txg & TXG_MASK;
754 779 txg_node_t *tn;
755 780 void *p = NULL;
756 781
757 782 mutex_enter(&tl->tl_lock);
758 783 if ((tn = tl->tl_head[t]) != NULL) {
759 784 p = (char *)tn - tl->tl_offset;
760 785 tl->tl_head[t] = tn->tn_next[t];
761 786 tn->tn_next[t] = NULL;
762 787 tn->tn_member[t] = 0;
763 788 }
764 789 mutex_exit(&tl->tl_lock);
765 790
766 791 return (p);
767 792 }
768 793
769 794 /*
770 795 * Remove a specific item from the list and return it.
771 796 */
772 797 void *
773 798 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
774 799 {
775 800 int t = txg & TXG_MASK;
776 801 txg_node_t *tn, **tp;
777 802
778 803 mutex_enter(&tl->tl_lock);
779 804
780 805 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
781 806 if ((char *)tn - tl->tl_offset == p) {
782 807 *tp = tn->tn_next[t];
783 808 tn->tn_next[t] = NULL;
784 809 tn->tn_member[t] = 0;
785 810 mutex_exit(&tl->tl_lock);
786 811 return (p);
787 812 }
788 813 }
789 814
790 815 mutex_exit(&tl->tl_lock);
791 816
792 817 return (NULL);
793 818 }
794 819
795 820 boolean_t
796 821 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
797 822 {
798 823 int t = txg & TXG_MASK;
799 824 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
800 825
801 826 return (tn->tn_member[t] != 0);
802 827 }
803 828
804 829 /*
805 830 * Walk a txg list -- only safe if you know it's not changing.
806 831 */
807 832 void *
808 833 txg_list_head(txg_list_t *tl, uint64_t txg)
809 834 {
810 835 int t = txg & TXG_MASK;
811 836 txg_node_t *tn = tl->tl_head[t];
812 837
813 838 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
814 839 }
815 840
816 841 void *
817 842 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
818 843 {
819 844 int t = txg & TXG_MASK;
820 845 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
821 846
822 847 tn = tn->tn_next[t];
823 848
824 849 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
825 850 }
↓ open down ↓ |
172 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX