4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)txg.c 1.4 08/03/20 SMI"
28 #include <sys/zfs_context.h>
29 #include <sys/txg_impl.h>
30 #include <sys/dmu_impl.h>
31 #include <sys/dsl_pool.h>
32 #include <sys/callb.h>
35 * Pool-wide transaction groups.
38 static void txg_sync_thread(dsl_pool_t *dp);
39 static void txg_quiesce_thread(dsl_pool_t *dp);
41 int zfs_txg_timeout = 30; /* max seconds worth of delta per txg */
42 int zfs_txg_synctime = 5; /* target seconds to sync a txg */
44 int zfs_write_limit_shift = 3; /* 1/8th of physical memory */
46 uint64_t zfs_write_limit_min = 32 << 20; /* min write limit is 32MB */
47 uint64_t zfs_write_limit_max = 0; /* max data payload per txg */
48 uint64_t zfs_write_limit_inflated = 0;
51 * Prepare the txg subsystem.
54 txg_init(dsl_pool_t *dp, uint64_t txg)
56 tx_state_t *tx = &dp->dp_tx;
58 bzero(tx, sizeof (tx_state_t));
60 tx->tx_cpu = kmem_zalloc(max_ncpus * sizeof (tx_cpu_t), KM_SLEEP);
62 for (c = 0; c < max_ncpus; c++) {
65 mutex_init(&tx->tx_cpu[c].tc_lock, NULL, MUTEX_DEFAULT, NULL);
66 for (i = 0; i < TXG_SIZE; i++) {
67 cv_init(&tx->tx_cpu[c].tc_cv[i], NULL, CV_DEFAULT,
72 rw_init(&tx->tx_suspend, NULL, RW_DEFAULT, NULL);
73 mutex_init(&tx->tx_sync_lock, NULL, MUTEX_DEFAULT, NULL);
75 tx->tx_open_txg = txg;
79 * Close down the txg subsystem.
82 txg_fini(dsl_pool_t *dp)
84 tx_state_t *tx = &dp->dp_tx;
87 ASSERT(tx->tx_threads == 0);
89 rw_destroy(&tx->tx_suspend);
90 mutex_destroy(&tx->tx_sync_lock);
92 for (c = 0; c < max_ncpus; c++) {
95 mutex_destroy(&tx->tx_cpu[c].tc_lock);
96 for (i = 0; i < TXG_SIZE; i++)
97 cv_destroy(&tx->tx_cpu[c].tc_cv[i]);
100 kmem_free(tx->tx_cpu, max_ncpus * sizeof (tx_cpu_t));
102 bzero(tx, sizeof (tx_state_t));
106 * Start syncing transaction groups.
109 txg_sync_start(dsl_pool_t *dp)
111 tx_state_t *tx = &dp->dp_tx;
113 mutex_enter(&tx->tx_sync_lock);
115 dprintf("pool %p\n", dp);
117 ASSERT(tx->tx_threads == 0);
121 tx->tx_quiesce_thread = thread_create(NULL, 0, txg_quiesce_thread,
122 dp, 0, &p0, TS_RUN, minclsyspri);
124 tx->tx_sync_thread = thread_create(NULL, 0, txg_sync_thread,
125 dp, 0, &p0, TS_RUN, minclsyspri);
127 mutex_exit(&tx->tx_sync_lock);
131 txg_thread_enter(tx_state_t *tx, callb_cpr_t *cpr)
133 CALLB_CPR_INIT(cpr, &tx->tx_sync_lock, callb_generic_cpr, FTAG);
134 mutex_enter(&tx->tx_sync_lock);
138 txg_thread_exit(tx_state_t *tx, callb_cpr_t *cpr, kthread_t **tpp)
140 ASSERT(*tpp != NULL);
143 cv_broadcast(&tx->tx_exit_cv);
144 CALLB_CPR_EXIT(cpr); /* drops &tx->tx_sync_lock */
149 txg_thread_wait(tx_state_t *tx, callb_cpr_t *cpr, kcondvar_t *cv, uint64_t time)
151 CALLB_CPR_SAFE_BEGIN(cpr);
154 (void) cv_timedwait(cv, &tx->tx_sync_lock, lbolt + time);
156 cv_wait(cv, &tx->tx_sync_lock);
158 CALLB_CPR_SAFE_END(cpr, &tx->tx_sync_lock);
162 * Stop syncing transaction groups.
165 txg_sync_stop(dsl_pool_t *dp)
167 tx_state_t *tx = &dp->dp_tx;
169 dprintf("pool %p\n", dp);
171 * Finish off any work in progress.
173 ASSERT(tx->tx_threads == 2);
174 txg_wait_synced(dp, 0);
177 * Wake all sync threads and wait for them to die.
179 mutex_enter(&tx->tx_sync_lock);
181 ASSERT(tx->tx_threads == 2);
185 cv_broadcast(&tx->tx_quiesce_more_cv);
186 cv_broadcast(&tx->tx_quiesce_done_cv);
187 cv_broadcast(&tx->tx_sync_more_cv);
189 while (tx->tx_threads != 0)
190 cv_wait(&tx->tx_exit_cv, &tx->tx_sync_lock);
194 mutex_exit(&tx->tx_sync_lock);
198 txg_hold_open(dsl_pool_t *dp, txg_handle_t *th)
200 tx_state_t *tx = &dp->dp_tx;
201 tx_cpu_t *tc = &tx->tx_cpu[CPU_SEQID];
204 mutex_enter(&tc->tc_lock);
206 txg = tx->tx_open_txg;
207 tc->tc_count[txg & TXG_MASK]++;
216 txg_rele_to_quiesce(txg_handle_t *th)
218 tx_cpu_t *tc = th->th_cpu;
220 mutex_exit(&tc->tc_lock);
224 txg_rele_to_sync(txg_handle_t *th)
226 tx_cpu_t *tc = th->th_cpu;
227 int g = th->th_txg & TXG_MASK;
229 mutex_enter(&tc->tc_lock);
230 ASSERT(tc->tc_count[g] != 0);
231 if (--tc->tc_count[g] == 0)
232 cv_broadcast(&tc->tc_cv[g]);
233 mutex_exit(&tc->tc_lock);
235 th->th_cpu = NULL; /* defensive */
239 txg_quiesce(dsl_pool_t *dp, uint64_t txg)
241 tx_state_t *tx = &dp->dp_tx;
242 int g = txg & TXG_MASK;
246 * Grab all tx_cpu locks so nobody else can get into this txg.
248 for (c = 0; c < max_ncpus; c++)
249 mutex_enter(&tx->tx_cpu[c].tc_lock);
251 ASSERT(txg == tx->tx_open_txg);
255 * Now that we've incremented tx_open_txg, we can let threads
256 * enter the next transaction group.
258 for (c = 0; c < max_ncpus; c++)
259 mutex_exit(&tx->tx_cpu[c].tc_lock);
262 * Quiesce the transaction group by waiting for everyone to txg_exit().
264 for (c = 0; c < max_ncpus; c++) {
265 tx_cpu_t *tc = &tx->tx_cpu[c];
266 mutex_enter(&tc->tc_lock);
267 while (tc->tc_count[g] != 0)
268 cv_wait(&tc->tc_cv[g], &tc->tc_lock);
269 mutex_exit(&tc->tc_lock);
274 txg_sync_thread(dsl_pool_t *dp)
276 tx_state_t *tx = &dp->dp_tx;
278 uint64_t timeout, start, delta, timer;
281 txg_thread_enter(tx, &cpr);
284 timeout = zfs_txg_timeout * hz;
286 uint64_t txg, written;
289 * We sync when there's someone waiting on us, or the
290 * quiesce thread has handed off a txg to us, or we have
291 * reached our timeout.
293 timer = (delta >= timeout ? 0 : timeout - delta);
294 while (!tx->tx_exiting && timer > 0 &&
295 tx->tx_synced_txg >= tx->tx_sync_txg_waiting &&
296 tx->tx_quiesced_txg == 0) {
297 dprintf("waiting; tx_synced=%llu waiting=%llu dp=%p\n",
298 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
299 txg_thread_wait(tx, &cpr, &tx->tx_sync_more_cv, timer);
300 delta = lbolt - start;
301 timer = (delta > timeout ? 0 : timeout - delta);
305 * Wait until the quiesce thread hands off a txg to us,
306 * prompting it to do so if necessary.
308 while (!tx->tx_exiting && tx->tx_quiesced_txg == 0) {
309 if (tx->tx_quiesce_txg_waiting < tx->tx_open_txg+1)
310 tx->tx_quiesce_txg_waiting = tx->tx_open_txg+1;
311 cv_broadcast(&tx->tx_quiesce_more_cv);
312 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_done_cv, 0);
316 txg_thread_exit(tx, &cpr, &tx->tx_sync_thread);
318 rw_enter(&tx->tx_suspend, RW_WRITER);
321 * Consume the quiesced txg which has been handed off to
322 * us. This may cause the quiescing thread to now be
323 * able to quiesce another txg, so we must signal it.
325 txg = tx->tx_quiesced_txg;
326 tx->tx_quiesced_txg = 0;
327 tx->tx_syncing_txg = txg;
328 cv_broadcast(&tx->tx_quiesce_more_cv);
329 rw_exit(&tx->tx_suspend);
331 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
332 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
333 mutex_exit(&tx->tx_sync_lock);
335 spa_sync(dp->dp_spa, txg);
336 delta = lbolt - start;
338 written = dp->dp_space_towrite[txg & TXG_MASK];
339 dp->dp_space_towrite[txg & TXG_MASK] = 0;
340 ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
343 * If the write limit max has not been explicitly set, set it
344 * to a fraction of available phisical memory (default 1/8th).
345 * Note that we must inflate the limit because the spa
346 * inflates write sizes to account for data replication.
347 * Check this each sync phase to catch changing memory size.
349 if (zfs_write_limit_inflated == 0 ||
350 (zfs_write_limit_shift && zfs_write_limit_max !=
351 physmem * PAGESIZE >> zfs_write_limit_shift)) {
352 zfs_write_limit_max =
353 physmem * PAGESIZE >> zfs_write_limit_shift;
354 zfs_write_limit_inflated =
355 spa_get_asize(dp->dp_spa, zfs_write_limit_max);
356 if (zfs_write_limit_min > zfs_write_limit_inflated)
357 zfs_write_limit_inflated = zfs_write_limit_min;
361 * Attempt to keep the sync time consistant by adjusting the
362 * amount of write traffic allowed into each transaction group.
364 target = zfs_txg_synctime * hz;
365 if (delta > target) {
366 uint64_t old = MIN(dp->dp_write_limit, written);
368 dp->dp_write_limit = MAX(zfs_write_limit_min,
369 old * target / delta);
370 } else if (written >= dp->dp_write_limit &&
371 delta >> 3 < target >> 3) {
373 MIN((100 * target) / delta, 200);
375 dp->dp_write_limit = MIN(zfs_write_limit_inflated,
376 written * rescale / 100);
379 mutex_enter(&tx->tx_sync_lock);
380 rw_enter(&tx->tx_suspend, RW_WRITER);
381 tx->tx_synced_txg = txg;
382 tx->tx_syncing_txg = 0;
383 rw_exit(&tx->tx_suspend);
384 cv_broadcast(&tx->tx_sync_done_cv);
389 txg_quiesce_thread(dsl_pool_t *dp)
391 tx_state_t *tx = &dp->dp_tx;
394 txg_thread_enter(tx, &cpr);
400 * We quiesce when there's someone waiting on us.
401 * However, we can only have one txg in "quiescing" or
402 * "quiesced, waiting to sync" state. So we wait until
403 * the "quiesced, waiting to sync" txg has been consumed
404 * by the sync thread.
406 while (!tx->tx_exiting &&
407 (tx->tx_open_txg >= tx->tx_quiesce_txg_waiting ||
408 tx->tx_quiesced_txg != 0))
409 txg_thread_wait(tx, &cpr, &tx->tx_quiesce_more_cv, 0);
412 txg_thread_exit(tx, &cpr, &tx->tx_quiesce_thread);
414 txg = tx->tx_open_txg;
415 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
416 txg, tx->tx_quiesce_txg_waiting,
417 tx->tx_sync_txg_waiting);
418 mutex_exit(&tx->tx_sync_lock);
419 txg_quiesce(dp, txg);
420 mutex_enter(&tx->tx_sync_lock);
423 * Hand this txg off to the sync thread.
425 dprintf("quiesce done, handing off txg %llu\n", txg);
426 tx->tx_quiesced_txg = txg;
427 cv_broadcast(&tx->tx_sync_more_cv);
428 cv_broadcast(&tx->tx_quiesce_done_cv);
433 * Delay this thread by 'ticks' if we are still in the open transaction
434 * group and there is already a waiting txg quiesing or quiesced. Abort
435 * the delay if this txg stalls or enters the quiesing state.
438 txg_delay(dsl_pool_t *dp, uint64_t txg, int ticks)
440 tx_state_t *tx = &dp->dp_tx;
441 int timeout = lbolt + ticks;
443 /* don't delay if this txg could transition to quiesing immediately */
444 if (tx->tx_open_txg > txg ||
445 tx->tx_syncing_txg == txg-1 || tx->tx_synced_txg == txg-1)
448 mutex_enter(&tx->tx_sync_lock);
449 if (tx->tx_open_txg > txg || tx->tx_synced_txg == txg-1) {
450 mutex_exit(&tx->tx_sync_lock);
454 while (lbolt < timeout &&
455 tx->tx_syncing_txg < txg-1 && !txg_stalled(dp))
456 (void) cv_timedwait(&tx->tx_quiesce_more_cv, &tx->tx_sync_lock,
459 mutex_exit(&tx->tx_sync_lock);
463 txg_wait_synced(dsl_pool_t *dp, uint64_t txg)
465 tx_state_t *tx = &dp->dp_tx;
467 mutex_enter(&tx->tx_sync_lock);
468 ASSERT(tx->tx_threads == 2);
470 txg = tx->tx_open_txg;
471 if (tx->tx_sync_txg_waiting < txg)
472 tx->tx_sync_txg_waiting = txg;
473 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
474 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
475 while (tx->tx_synced_txg < txg) {
476 dprintf("broadcasting sync more "
477 "tx_synced=%llu waiting=%llu dp=%p\n",
478 tx->tx_synced_txg, tx->tx_sync_txg_waiting, dp);
479 cv_broadcast(&tx->tx_sync_more_cv);
480 cv_wait(&tx->tx_sync_done_cv, &tx->tx_sync_lock);
482 mutex_exit(&tx->tx_sync_lock);
486 txg_wait_open(dsl_pool_t *dp, uint64_t txg)
488 tx_state_t *tx = &dp->dp_tx;
490 mutex_enter(&tx->tx_sync_lock);
491 ASSERT(tx->tx_threads == 2);
493 txg = tx->tx_open_txg + 1;
494 if (tx->tx_quiesce_txg_waiting < txg)
495 tx->tx_quiesce_txg_waiting = txg;
496 dprintf("txg=%llu quiesce_txg=%llu sync_txg=%llu\n",
497 txg, tx->tx_quiesce_txg_waiting, tx->tx_sync_txg_waiting);
498 while (tx->tx_open_txg < txg) {
499 cv_broadcast(&tx->tx_quiesce_more_cv);
500 cv_wait(&tx->tx_quiesce_done_cv, &tx->tx_sync_lock);
502 mutex_exit(&tx->tx_sync_lock);
506 txg_stalled(dsl_pool_t *dp)
508 tx_state_t *tx = &dp->dp_tx;
509 return (tx->tx_quiesce_txg_waiting > tx->tx_open_txg);
513 txg_suspend(dsl_pool_t *dp)
515 tx_state_t *tx = &dp->dp_tx;
516 /* XXX some code paths suspend when they are already suspended! */
517 rw_enter(&tx->tx_suspend, RW_READER);
521 txg_resume(dsl_pool_t *dp)
523 tx_state_t *tx = &dp->dp_tx;
524 rw_exit(&tx->tx_suspend);
528 * Per-txg object lists.
531 txg_list_create(txg_list_t *tl, size_t offset)
535 mutex_init(&tl->tl_lock, NULL, MUTEX_DEFAULT, NULL);
537 tl->tl_offset = offset;
539 for (t = 0; t < TXG_SIZE; t++)
540 tl->tl_head[t] = NULL;
544 txg_list_destroy(txg_list_t *tl)
548 for (t = 0; t < TXG_SIZE; t++)
549 ASSERT(txg_list_empty(tl, t));
551 mutex_destroy(&tl->tl_lock);
555 txg_list_empty(txg_list_t *tl, uint64_t txg)
557 return (tl->tl_head[txg & TXG_MASK] == NULL);
561 * Add an entry to the list.
562 * Returns 0 if it's a new entry, 1 if it's already there.
565 txg_list_add(txg_list_t *tl, void *p, uint64_t txg)
567 int t = txg & TXG_MASK;
568 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
571 mutex_enter(&tl->tl_lock);
572 already_on_list = tn->tn_member[t];
573 if (!already_on_list) {
574 tn->tn_member[t] = 1;
575 tn->tn_next[t] = tl->tl_head[t];
578 mutex_exit(&tl->tl_lock);
580 return (already_on_list);
584 * Remove the head of the list and return it.
587 txg_list_remove(txg_list_t *tl, uint64_t txg)
589 int t = txg & TXG_MASK;
593 mutex_enter(&tl->tl_lock);
594 if ((tn = tl->tl_head[t]) != NULL) {
595 p = (char *)tn - tl->tl_offset;
596 tl->tl_head[t] = tn->tn_next[t];
597 tn->tn_next[t] = NULL;
598 tn->tn_member[t] = 0;
600 mutex_exit(&tl->tl_lock);
606 * Remove a specific item from the list and return it.
609 txg_list_remove_this(txg_list_t *tl, void *p, uint64_t txg)
611 int t = txg & TXG_MASK;
612 txg_node_t *tn, **tp;
614 mutex_enter(&tl->tl_lock);
616 for (tp = &tl->tl_head[t]; (tn = *tp) != NULL; tp = &tn->tn_next[t]) {
617 if ((char *)tn - tl->tl_offset == p) {
618 *tp = tn->tn_next[t];
619 tn->tn_next[t] = NULL;
620 tn->tn_member[t] = 0;
621 mutex_exit(&tl->tl_lock);
626 mutex_exit(&tl->tl_lock);
632 txg_list_member(txg_list_t *tl, void *p, uint64_t txg)
634 int t = txg & TXG_MASK;
635 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
637 return (tn->tn_member[t]);
641 * Walk a txg list -- only safe if you know it's not changing.
644 txg_list_head(txg_list_t *tl, uint64_t txg)
646 int t = txg & TXG_MASK;
647 txg_node_t *tn = tl->tl_head[t];
649 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);
653 txg_list_next(txg_list_t *tl, void *p, uint64_t txg)
655 int t = txg & TXG_MASK;
656 txg_node_t *tn = (txg_node_t *)((char *)p + tl->tl_offset);
660 return (tn == NULL ? NULL : (char *)tn - tl->tl_offset);