xref: /illumos-gate/usr/src/uts/common/fs/zfs/sys/txg_impl.h (revision fa41d87d)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
51ab7f2deSmaybee  * Common Development and Distribution License (the "License").
61ab7f2deSmaybee  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
2169962b56SMatthew Ahrens 
22fa9e4066Sahrens /*
23d20e665cSRicardo M. Correia  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fa9e4066Sahrens  * Use is subject to license terms.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
270689f76cSAdam Leventhal /*
28*fa41d87dSSerapheim Dimitropoulos  * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
290689f76cSAdam Leventhal  */
300689f76cSAdam Leventhal 
31fa9e4066Sahrens #ifndef _SYS_TXG_IMPL_H
32fa9e4066Sahrens #define	_SYS_TXG_IMPL_H
33fa9e4066Sahrens 
34fa9e4066Sahrens #include <sys/spa.h>
35fa9e4066Sahrens #include <sys/txg.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens #ifdef	__cplusplus
38fa9e4066Sahrens extern "C" {
39fa9e4066Sahrens #endif
40fa9e4066Sahrens 
414a923759SGeorge Wilson /*
424a923759SGeorge Wilson  * The tx_cpu structure is a per-cpu structure that is used to track
434a923759SGeorge Wilson  * the number of active transaction holds (tc_count). As transactions
444a923759SGeorge Wilson  * are assigned into a transaction group the appropriate tc_count is
454a923759SGeorge Wilson  * incremented to indicate that there are pending changes that have yet
464a923759SGeorge Wilson  * to quiesce. Consumers evenutally call txg_rele_to_sync() to decrement
474a923759SGeorge Wilson  * the tc_count. A transaction group is not considered quiesced until all
484a923759SGeorge Wilson  * tx_cpu structures have reached a tc_count of zero.
494a923759SGeorge Wilson  *
504a923759SGeorge Wilson  * This structure is a per-cpu structure by design. Updates to this structure
514a923759SGeorge Wilson  * are frequent and concurrent. Having a single structure would result in
524a923759SGeorge Wilson  * heavy lock contention so a per-cpu design was implemented. With the fanned
534a923759SGeorge Wilson  * out mutex design, consumers only need to lock the mutex associated with
544a923759SGeorge Wilson  * thread's cpu.
554a923759SGeorge Wilson  *
564a923759SGeorge Wilson  * The tx_cpu contains two locks, the tc_lock and tc_open_lock.
574a923759SGeorge Wilson  * The tc_lock is used to protect all members of the tx_cpu structure with
584a923759SGeorge Wilson  * the exception of the tc_open_lock. This lock should only be held for a
594a923759SGeorge Wilson  * short period of time, typically when updating the value of tc_count.
604a923759SGeorge Wilson  *
614a923759SGeorge Wilson  * The tc_open_lock protects the tx_open_txg member of the tx_state structure.
624a923759SGeorge Wilson  * This lock is used to ensure that transactions are only assigned into
634a923759SGeorge Wilson  * the current open transaction group. In order to move the current open
644a923759SGeorge Wilson  * transaction group to the quiesce phase, the txg_quiesce thread must
654a923759SGeorge Wilson  * grab all tc_open_locks, increment the tx_open_txg, and drop the locks.
664a923759SGeorge Wilson  * The tc_open_lock is held until the transaction is assigned into the
674a923759SGeorge Wilson  * transaction group. Typically, this is a short operation but if throttling
684a923759SGeorge Wilson  * is occuring it may be held for longer periods of time.
694a923759SGeorge Wilson  */
70fa9e4066Sahrens struct tx_cpu {
714a923759SGeorge Wilson 	kmutex_t	tc_open_lock;	/* protects tx_open_txg */
724a923759SGeorge Wilson 	kmutex_t	tc_lock;	/* protects the rest of this struct */
73fa9e4066Sahrens 	kcondvar_t	tc_cv[TXG_SIZE];
740689f76cSAdam Leventhal 	uint64_t	tc_count[TXG_SIZE];	/* tx hold count on each txg */
75d20e665cSRicardo M. Correia 	list_t		tc_callbacks[TXG_SIZE]; /* commit cb list */
764a923759SGeorge Wilson 	char		tc_pad[8];		/* pad to fill 3 cache lines */
77fa9e4066Sahrens };
78fa9e4066Sahrens 
794a923759SGeorge Wilson /*
804a923759SGeorge Wilson  * The tx_state structure maintains the state information about the different
814a923759SGeorge Wilson  * stages of the pool's transcation groups. A per pool tx_state structure
824a923759SGeorge Wilson  * is used to track this information. The tx_state structure also points to
834a923759SGeorge Wilson  * an array of tx_cpu structures (described above). Although the tx_sync_lock
844a923759SGeorge Wilson  * is used to protect the members of this structure, it is not used to
854a923759SGeorge Wilson  * protect the tx_open_txg. Instead a special lock in the tx_cpu structure
864a923759SGeorge Wilson  * is used. Readers of tx_open_txg must grab the per-cpu tc_open_lock.
874a923759SGeorge Wilson  * Any thread wishing to update tx_open_txg must grab the tc_open_lock on
884a923759SGeorge Wilson  * every cpu (see txg_quiesce()).
894a923759SGeorge Wilson  */
90fa9e4066Sahrens typedef struct tx_state {
910689f76cSAdam Leventhal 	tx_cpu_t	*tx_cpu;	/* protects access to tx_open_txg */
920689f76cSAdam Leventhal 	kmutex_t	tx_sync_lock;	/* protects the rest of this struct */
9369962b56SMatthew Ahrens 
94fa9e4066Sahrens 	uint64_t	tx_open_txg;	/* currently open txg id */
95*fa41d87dSSerapheim Dimitropoulos 	uint64_t	tx_quiescing_txg; /* currently quiescing txg id */
96fa9e4066Sahrens 	uint64_t	tx_quiesced_txg; /* quiesced txg waiting for sync */
97fa9e4066Sahrens 	uint64_t	tx_syncing_txg;	/* currently syncing txg id */
98fa9e4066Sahrens 	uint64_t	tx_synced_txg;	/* last synced txg id */
99fa9e4066Sahrens 
10069962b56SMatthew Ahrens 	hrtime_t	tx_open_time;	/* start time of tx_open_txg */
10169962b56SMatthew Ahrens 
102fa9e4066Sahrens 	uint64_t	tx_sync_txg_waiting; /* txg we're waiting to sync */
103fa9e4066Sahrens 	uint64_t	tx_quiesce_txg_waiting; /* txg we're waiting to open */
104fa9e4066Sahrens 
105fa9e4066Sahrens 	kcondvar_t	tx_sync_more_cv;
106fa9e4066Sahrens 	kcondvar_t	tx_sync_done_cv;
107fa9e4066Sahrens 	kcondvar_t	tx_quiesce_more_cv;
108fa9e4066Sahrens 	kcondvar_t	tx_quiesce_done_cv;
1091ab7f2deSmaybee 	kcondvar_t	tx_timeout_cv;
110fa9e4066Sahrens 	kcondvar_t	tx_exit_cv;	/* wait for all threads to exit */
111fa9e4066Sahrens 
112fa9e4066Sahrens 	uint8_t		tx_threads;	/* number of threads */
113fa9e4066Sahrens 	uint8_t		tx_exiting;	/* set when we're exiting */
114fa9e4066Sahrens 
115fa9e4066Sahrens 	kthread_t	*tx_sync_thread;
116fa9e4066Sahrens 	kthread_t	*tx_quiesce_thread;
117d20e665cSRicardo M. Correia 
118d20e665cSRicardo M. Correia 	taskq_t		*tx_commit_cb_taskq; /* commit callback taskq */
119fa9e4066Sahrens } tx_state_t;
120fa9e4066Sahrens 
121fa9e4066Sahrens #ifdef	__cplusplus
122fa9e4066Sahrens }
123fa9e4066Sahrens #endif
124fa9e4066Sahrens 
125fa9e4066Sahrens #endif	/* _SYS_TXG_IMPL_H */
126