1 // Copyright 2014 Google
2 
3 #ifndef btr0scrub_h
4 #define btr0scrub_h
5 
6 #include "dict0dict.h"
7 
8 /**
9  * enum describing page allocation status
10  */
11 enum btr_scrub_page_allocation_status_t {
12 	BTR_SCRUB_PAGE_FREE,
13 	BTR_SCRUB_PAGE_ALLOCATED,
14 	BTR_SCRUB_PAGE_ALLOCATION_UNKNOWN
15 };
16 
17 /**
18 * constants returned by btr_page_needs_scrubbing & btr_scrub_recheck_page
19 */
20 #define BTR_SCRUB_PAGE                         1 /* page should be scrubbed */
21 #define BTR_SCRUB_SKIP_PAGE                    2 /* no scrub & no action */
22 #define BTR_SCRUB_SKIP_PAGE_AND_CLOSE_TABLE    3 /* no scrub & close table */
23 #define BTR_SCRUB_SKIP_PAGE_AND_COMPLETE_SPACE 4 /* no scrub & complete space */
24 #define BTR_SCRUB_TURNED_OFF                   5 /* we detected that scrubbing
25 						 was disabled by global
26 						 variable */
27 
28 /**************************************************************//**
29 struct for keeping scrub statistics. */
30 struct btr_scrub_stat_t {
31 	/* page reorganizations */
32 	ulint page_reorganizations;
33 	/* page splits */
34 	ulint page_splits;
35 	/* scrub failures */
36 	ulint page_split_failures_underflow;
37 	ulint page_split_failures_out_of_filespace;
38 	ulint page_split_failures_missing_index;
39 	ulint page_split_failures_unknown;
40 };
41 
42 /**************************************************************//**
43 struct for thread local scrub state. */
44 struct btr_scrub_t {
45 
46 	/* current space */
47 	ulint space;
48 
49 	/* is scrubbing enabled for this space */
50 	bool scrubbing;
51 
52 	/* is current space compressed */
53 	bool compressed;
54 
55 	dict_table_t* current_table;
56 	dict_index_t* current_index;
57 	/* savepoint for X_LATCH of block */
58 	ulint savepoint;
59 
60 	/* statistic counters */
61 	btr_scrub_stat_t scrub_stat;
62 };
63 
64 /*********************************************************************
65 Init scrub global variables */
66 UNIV_INTERN
67 void
68 btr_scrub_init();
69 
70 /*********************************************************************
71 Cleanup scrub globals */
72 UNIV_INTERN
73 void
74 btr_scrub_cleanup();
75 
76 /***********************************************************************
77 Return crypt statistics */
78 UNIV_INTERN
79 void
80 btr_scrub_total_stat(
81 /*==================*/
82 	btr_scrub_stat_t *stat); /*!< out: stats to update */
83 
84 /**************************************************************//**
85 Check if a page needs scrubbing
86 * @return BTR_SCRUB_PAGE if page should be scrubbed
87 * else btr_scrub_skip_page should be called
88 * with this return value (and without any latches held)
89 */
90 UNIV_INTERN
91 int
92 btr_page_needs_scrubbing(
93 /*=====================*/
94 	btr_scrub_t*	scrub_data, /*!< in: scrub data  */
95 	buf_block_t*	block,	    /*!< in: block to check, latched */
96 	btr_scrub_page_allocation_status_t allocated); /*!< in: is block
97 						       allocated, free or
98 						       unknown */
99 
100 /****************************************************************
101 Recheck if a page needs scrubbing, and if it does load appropriate
102 table and index
103 * @return BTR_SCRUB_PAGE if page should be scrubbed
104 * else btr_scrub_skip_page should be called
105 * with this return value (and without any latches held)
106 */
107 UNIV_INTERN
108 int
109 btr_scrub_recheck_page(
110 /*====================*/
111 	btr_scrub_t* scrub_data,  /*!< inut: scrub data */
112 	buf_block_t* block,       /*!< in: block */
113 	btr_scrub_page_allocation_status_t allocated, /*!< in: is block
114 						      allocated or free */
115 	mtr_t* mtr);              /*!< in: mtr */
116 
117 /****************************************************************
118 Perform actual scrubbing of page */
119 UNIV_INTERN
120 int
121 btr_scrub_page(
122 /*============*/
123 	btr_scrub_t* scrub_data,  /*!< in/out: scrub data */
124 	buf_block_t* block,       /*!< in: block */
125 	btr_scrub_page_allocation_status_t allocated, /*!< in: is block
126 						      allocated or free */
127 	mtr_t* mtr);              /*!< in: mtr */
128 
129 /****************************************************************
130 Perform cleanup needed for a page not needing scrubbing */
131 UNIV_INTERN
132 void
133 btr_scrub_skip_page(
134 /*============*/
135 	btr_scrub_t* scrub_data,  /*!< in/out: scrub data */
136 	int needs_scrubbing);     /*!< in:  return value from
137 				  btr_page_needs_scrubbing or
138 				  btr_scrub_recheck_page which encodes what kind
139 				  of cleanup is needed */
140 
141 /****************************************************************
142 Start iterating a space
143 * @return true if scrubbing is turned on */
144 bool btr_scrub_start_space(const fil_space_t &space, btr_scrub_t *scrub_data);
145 
146 /** Complete iterating a space.
147 @param[in,out]	scrub_data	 scrub data */
148 UNIV_INTERN
149 void
150 btr_scrub_complete_space(btr_scrub_t* scrub_data);
151 
152 #endif
153