1 /***********************************************************************
2 
3 Copyright (c) 2010, 2015, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2012, Facebook Inc.
5 Copyright (c) 2013, 2020, MariaDB Corporation.
6 
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
19 
20 ***********************************************************************/
21 
22 /**************************************************//**
23 @file include/srv0mon.h
24 Server monitor counter related defines
25 
26 Created 12/15/2009	Jimmy Yang
27 *******************************************************/
28 
29 #ifndef srv0mon_h
30 #define srv0mon_h
31 
32 #include "univ.i"
33 
34 #ifndef __STDC_LIMIT_MACROS
35 /* Required for FreeBSD so that INT64_MAX is defined. */
36 #define __STDC_LIMIT_MACROS
37 #endif /* __STDC_LIMIT_MACROS */
38 
39 #include <stdint.h>
40 #include "my_atomic.h"
41 #include "my_atomic_wrapper.h"
42 
43 /** Possible status values for "mon_status" in "struct monitor_value" */
44 enum monitor_running_status {
45 	MONITOR_STARTED = 1,	/*!< Monitor has been turned on */
46 	MONITOR_STOPPED = 2	/*!< Monitor has been turned off */
47 };
48 
49 typedef enum monitor_running_status	monitor_running_t;
50 
51 /** Monitor counter value type */
52 typedef	int64_t				mon_type_t;
53 
54 /** Two monitor structures are defined in this file. One is
55 "monitor_value_t" which contains dynamic counter values for each
56 counter. The other is "monitor_info_t", which contains
57 static information (counter name, desc etc.) for each counter.
58 In addition, an enum datatype "monitor_id_t" is also defined,
59 it identifies each monitor with an internally used symbol, whose
60 integer value indexes into above two structure for its dynamic
61 and static information.
62 Developer who intend to add new counters would require to
63 fill in counter information as described in "monitor_info_t" and
64 create the internal counter ID in "monitor_id_t". */
65 
66 /** Structure containing the actual values of a monitor counter. */
67 struct monitor_value_t {
68 	time_t	mon_start_time;	/*!< Start time of monitoring  */
69 	time_t	mon_stop_time;	/*!< Stop time of monitoring */
70 	time_t	mon_reset_time;	/*!< Time of resetting the counter */
71 	mon_type_t	mon_value;	/*!< Current counter Value */
72 	mon_type_t	mon_max_value;	/*!< Current Max value */
73 	mon_type_t	mon_min_value;	/*!< Current Min value */
74 	mon_type_t	mon_value_reset;/*!< value at last reset */
75 	mon_type_t	mon_max_value_start; /*!< Max value since start */
76 	mon_type_t	mon_min_value_start; /*!< Min value since start */
77 	mon_type_t	mon_start_value;/*!< Value at the start time */
78 	mon_type_t	mon_last_value;	/*!< Last set of values */
79 	monitor_running_t mon_status;	/* whether monitor still running */
80 };
81 
82 /** Follwoing defines are possible values for "monitor_type" field in
83 "struct monitor_info" */
84 enum monitor_type_t {
85 	MONITOR_NONE = 0,	/*!< No monitoring */
86 	MONITOR_MODULE = 1,	/*!< This is a monitor module type,
87 				not a counter */
88 	MONITOR_EXISTING = 2,	/*!< The monitor carries information from
89 				an existing system status variable */
90 	MONITOR_NO_AVERAGE = 4,	/*!< Set this status if we don't want to
91 				calculate the average value for the counter */
92 	MONITOR_DISPLAY_CURRENT = 8, /*!< Display current value of the
93 				counter, rather than incremental value
94 				over the period. Mostly for counters
95 				displaying current resource usage */
96 	MONITOR_GROUP_MODULE = 16, /*!< Monitor can be turned on/off
97 				only as a module, but not individually */
98 	MONITOR_DEFAULT_ON = 32,/*!< Monitor will be turned on by default at
99 				server start up */
100 	MONITOR_SET_OWNER = 64,	/*!< Owner of "monitor set", a set of
101 				monitor counters */
102 	MONITOR_SET_MEMBER = 128,/*!< Being part of a "monitor set" */
103 	MONITOR_HIDDEN = 256	/*!< Do not display this monitor in the
104 				metrics table */
105 };
106 
107 /** Counter minimum value is initialized to be max value of
108  mon_type_t (int64_t) */
109 #ifndef INT64_MAX
110 #define INT64_MAX		(9223372036854775807LL)
111 #endif
112 #ifndef INT64_MIN
113 #define INT64_MIN		(-9223372036854775807LL-1)
114 #endif
115 #define	MIN_RESERVED		INT64_MAX
116 #define	MAX_RESERVED		INT64_MIN
117 
118 /** This enumeration defines internal monitor identifier used internally
119 to identify each particular counter. Its value indexes into two arrays,
120 one is the "innodb_counter_value" array which records actual monitor
121 counter values, the other is "innodb_counter_info" array which describes
122 each counter's basic information (name, desc etc.). A couple of
123 naming rules here:
124 1) If the monitor defines a module, it starts with MONITOR_MODULE
125 2) If the monitor uses exisitng counters from "status variable", its ID
126 name shall start with MONITOR_OVLD
127 
128 Please refer to "innodb_counter_info" in srv/srv0mon.cc for detail
129 information for each monitor counter */
130 
131 enum monitor_id_t {
132 	/* This is to identify the default value set by the metrics
133 	control global variables */
134 	MONITOR_DEFAULT_START = 0,
135 
136 	/* Start of Metadata counter */
137 	MONITOR_MODULE_METADATA,
138 	MONITOR_TABLE_OPEN,
139 	MONITOR_TABLE_CLOSE,
140 	MONITOR_TABLE_REFERENCE,
141 
142 	/* Lock manager related counters */
143 	MONITOR_MODULE_LOCK,
144 	MONITOR_DEADLOCK,
145 	MONITOR_TIMEOUT,
146 	MONITOR_LOCKREC_WAIT,
147 	MONITOR_TABLELOCK_WAIT,
148 	MONITOR_NUM_RECLOCK_REQ,
149 	MONITOR_RECLOCK_CREATED,
150 	MONITOR_RECLOCK_REMOVED,
151 	MONITOR_NUM_RECLOCK,
152 	MONITOR_TABLELOCK_CREATED,
153 	MONITOR_TABLELOCK_REMOVED,
154 	MONITOR_NUM_TABLELOCK,
155 	MONITOR_OVLD_ROW_LOCK_CURRENT_WAIT,
156 	MONITOR_OVLD_LOCK_WAIT_TIME,
157 	MONITOR_OVLD_LOCK_MAX_WAIT_TIME,
158 	MONITOR_OVLD_ROW_LOCK_WAIT,
159 	MONITOR_OVLD_LOCK_AVG_WAIT_TIME,
160 
161 	/* Buffer and I/O realted counters. */
162 	MONITOR_MODULE_BUFFER,
163 	MONITOR_OVLD_BUFFER_POOL_SIZE,
164 	MONITOR_OVLD_BUF_POOL_READS,
165 	MONITOR_OVLD_BUF_POOL_READ_REQUESTS,
166 	MONITOR_OVLD_BUF_POOL_WRITE_REQUEST,
167 	MONITOR_OVLD_BUF_POOL_WAIT_FREE,
168 	MONITOR_OVLD_BUF_POOL_READ_AHEAD,
169 	MONITOR_OVLD_BUF_POOL_READ_AHEAD_EVICTED,
170 	MONITOR_OVLD_BUF_POOL_PAGE_TOTAL,
171 	MONITOR_OVLD_BUF_POOL_PAGE_MISC,
172 	MONITOR_OVLD_BUF_POOL_PAGES_DATA,
173 	MONITOR_OVLD_BUF_POOL_BYTES_DATA,
174 	MONITOR_OVLD_BUF_POOL_PAGES_DIRTY,
175 	MONITOR_OVLD_BUF_POOL_BYTES_DIRTY,
176 	MONITOR_OVLD_BUF_POOL_PAGES_FREE,
177 	MONITOR_OVLD_PAGE_CREATED,
178 	MONITOR_OVLD_PAGES_WRITTEN,
179 	MONITOR_OVLD_INDEX_PAGES_WRITTEN,
180 	MONITOR_OVLD_NON_INDEX_PAGES_WRITTEN,
181 	MONITOR_OVLD_PAGES_READ,
182 	MONITOR_OVLD_INDEX_SEC_REC_CLUSTER_READS,
183 	MONITOR_OVLD_INDEX_SEC_REC_CLUSTER_READS_AVOIDED,
184 	MONITOR_OVLD_BYTE_READ,
185 	MONITOR_OVLD_BYTE_WRITTEN,
186 	MONITOR_FLUSH_BATCH_SCANNED,
187 	MONITOR_FLUSH_BATCH_SCANNED_NUM_CALL,
188 	MONITOR_FLUSH_BATCH_SCANNED_PER_CALL,
189 	MONITOR_FLUSH_BATCH_TOTAL_PAGE,
190 	MONITOR_FLUSH_BATCH_COUNT,
191 	MONITOR_FLUSH_BATCH_PAGES,
192 	MONITOR_FLUSH_NEIGHBOR_TOTAL_PAGE,
193 	MONITOR_FLUSH_NEIGHBOR_COUNT,
194 	MONITOR_FLUSH_NEIGHBOR_PAGES,
195 	MONITOR_FLUSH_N_TO_FLUSH_REQUESTED,
196 
197 	MONITOR_FLUSH_N_TO_FLUSH_BY_AGE,
198 	MONITOR_FLUSH_ADAPTIVE_AVG_TIME_SLOT,
199 	MONITOR_LRU_BATCH_FLUSH_AVG_TIME_SLOT,
200 
201 	MONITOR_FLUSH_ADAPTIVE_AVG_TIME_THREAD,
202 	MONITOR_LRU_BATCH_FLUSH_AVG_TIME_THREAD,
203 	MONITOR_FLUSH_ADAPTIVE_AVG_TIME_EST,
204 	MONITOR_LRU_BATCH_FLUSH_AVG_TIME_EST,
205 	MONITOR_FLUSH_AVG_TIME,
206 
207 	MONITOR_FLUSH_ADAPTIVE_AVG_PASS,
208 	MONITOR_LRU_BATCH_FLUSH_AVG_PASS,
209 	MONITOR_FLUSH_AVG_PASS,
210 
211 	MONITOR_LRU_GET_FREE_LOOPS,
212 	MONITOR_LRU_GET_FREE_WAITS,
213 
214 	MONITOR_FLUSH_AVG_PAGE_RATE,
215 	MONITOR_FLUSH_LSN_AVG_RATE,
216 	MONITOR_FLUSH_PCT_FOR_DIRTY,
217 	MONITOR_FLUSH_PCT_FOR_LSN,
218 	MONITOR_FLUSH_SYNC_WAITS,
219 	MONITOR_FLUSH_ADAPTIVE_TOTAL_PAGE,
220 	MONITOR_FLUSH_ADAPTIVE_COUNT,
221 	MONITOR_FLUSH_ADAPTIVE_PAGES,
222 	MONITOR_FLUSH_SYNC_TOTAL_PAGE,
223 	MONITOR_FLUSH_SYNC_COUNT,
224 	MONITOR_FLUSH_SYNC_PAGES,
225 	MONITOR_FLUSH_BACKGROUND_TOTAL_PAGE,
226 	MONITOR_FLUSH_BACKGROUND_COUNT,
227 	MONITOR_FLUSH_BACKGROUND_PAGES,
228 	MONITOR_LRU_BATCH_SCANNED,
229 	MONITOR_LRU_BATCH_SCANNED_NUM_CALL,
230 	MONITOR_LRU_BATCH_SCANNED_PER_CALL,
231 	MONITOR_LRU_BATCH_FLUSH_TOTAL_PAGE,
232 	MONITOR_LRU_BATCH_FLUSH_COUNT,
233 	MONITOR_LRU_BATCH_FLUSH_PAGES,
234 	MONITOR_LRU_BATCH_EVICT_TOTAL_PAGE,
235 	MONITOR_LRU_BATCH_EVICT_COUNT,
236 	MONITOR_LRU_BATCH_EVICT_PAGES,
237 	MONITOR_LRU_SINGLE_FLUSH_SCANNED,
238 	MONITOR_LRU_SINGLE_FLUSH_SCANNED_NUM_CALL,
239 	MONITOR_LRU_SINGLE_FLUSH_SCANNED_PER_CALL,
240 	MONITOR_LRU_SINGLE_FLUSH_FAILURE_COUNT,
241 	MONITOR_LRU_GET_FREE_SEARCH,
242 	MONITOR_LRU_SEARCH_SCANNED,
243 	MONITOR_LRU_SEARCH_SCANNED_NUM_CALL,
244 	MONITOR_LRU_SEARCH_SCANNED_PER_CALL,
245 	MONITOR_LRU_UNZIP_SEARCH_SCANNED,
246 	MONITOR_LRU_UNZIP_SEARCH_SCANNED_NUM_CALL,
247 	MONITOR_LRU_UNZIP_SEARCH_SCANNED_PER_CALL,
248 
249 	/* Buffer Page I/O specific counters. */
250 	MONITOR_MODULE_BUF_PAGE,
251 	MONITOR_INDEX_LEAF_PAGE_READ,
252 	MONITOR_INDEX_NON_LEAF_PAGE_READ,
253 	MONITOR_INDEX_IBUF_LEAF_PAGE_READ,
254 	MONITOR_INDEX_IBUF_NON_LEAF_PAGE_READ,
255 	MONITOR_UNDO_LOG_PAGE_READ,
256 	MONITOR_INODE_PAGE_READ,
257 	MONITOR_IBUF_FREELIST_PAGE_READ,
258 	MONITOR_IBUF_BITMAP_PAGE_READ,
259 	MONITOR_SYSTEM_PAGE_READ,
260 	MONITOR_TRX_SYSTEM_PAGE_READ,
261 	MONITOR_FSP_HDR_PAGE_READ,
262 	MONITOR_XDES_PAGE_READ,
263 	MONITOR_BLOB_PAGE_READ,
264 	MONITOR_ZBLOB_PAGE_READ,
265 	MONITOR_ZBLOB2_PAGE_READ,
266 	MONITOR_OTHER_PAGE_READ,
267 	MONITOR_INDEX_LEAF_PAGE_WRITTEN,
268 	MONITOR_INDEX_NON_LEAF_PAGE_WRITTEN,
269 	MONITOR_INDEX_IBUF_LEAF_PAGE_WRITTEN,
270 	MONITOR_INDEX_IBUF_NON_LEAF_PAGE_WRITTEN,
271 	MONITOR_UNDO_LOG_PAGE_WRITTEN,
272 	MONITOR_INODE_PAGE_WRITTEN,
273 	MONITOR_IBUF_FREELIST_PAGE_WRITTEN,
274 	MONITOR_IBUF_BITMAP_PAGE_WRITTEN,
275 	MONITOR_SYSTEM_PAGE_WRITTEN,
276 	MONITOR_TRX_SYSTEM_PAGE_WRITTEN,
277 	MONITOR_FSP_HDR_PAGE_WRITTEN,
278 	MONITOR_XDES_PAGE_WRITTEN,
279 	MONITOR_BLOB_PAGE_WRITTEN,
280 	MONITOR_ZBLOB_PAGE_WRITTEN,
281 	MONITOR_ZBLOB2_PAGE_WRITTEN,
282 	MONITOR_OTHER_PAGE_WRITTEN,
283 
284 	/* OS level counters (I/O) */
285 	MONITOR_MODULE_OS,
286 	MONITOR_OVLD_OS_FILE_READ,
287 	MONITOR_OVLD_OS_FILE_WRITE,
288 	MONITOR_OVLD_OS_FSYNC,
289 	MONITOR_OS_PENDING_READS,
290 	MONITOR_OS_PENDING_WRITES,
291 	MONITOR_OVLD_OS_LOG_WRITTEN,
292 	MONITOR_OVLD_OS_LOG_FSYNC,
293 	MONITOR_OVLD_OS_LOG_PENDING_FSYNC,
294 	MONITOR_OVLD_OS_LOG_PENDING_WRITES,
295 
296 	/* Transaction related counters */
297 	MONITOR_MODULE_TRX,
298 	MONITOR_TRX_RW_COMMIT,
299 	MONITOR_TRX_RO_COMMIT,
300 	MONITOR_TRX_NL_RO_COMMIT,
301 	MONITOR_TRX_COMMIT_UNDO,
302 	MONITOR_TRX_ROLLBACK,
303 	MONITOR_TRX_ROLLBACK_SAVEPOINT,
304 	MONITOR_TRX_ACTIVE,
305 	MONITOR_RSEG_HISTORY_LEN,
306 	MONITOR_NUM_UNDO_SLOT_USED,
307 	MONITOR_NUM_UNDO_SLOT_CACHED,
308 	MONITOR_RSEG_CUR_SIZE,
309 
310 	/* Purge related counters */
311 	MONITOR_MODULE_PURGE,
312 	MONITOR_N_DEL_ROW_PURGE,
313 	MONITOR_N_UPD_EXIST_EXTERN,
314 	MONITOR_PURGE_INVOKED,
315 	MONITOR_PURGE_N_PAGE_HANDLED,
316 	MONITOR_DML_PURGE_DELAY,
317 	MONITOR_PURGE_STOP_COUNT,
318 	MONITOR_PURGE_RESUME_COUNT,
319 
320 	/* Recovery related counters */
321 	MONITOR_MODULE_RECOVERY,
322 	MONITOR_NUM_CHECKPOINT,
323 	MONITOR_OVLD_LSN_FLUSHDISK,
324 	MONITOR_OVLD_LSN_CHECKPOINT,
325 	MONITOR_OVLD_LSN_CURRENT,
326 	MONITOR_LSN_CHECKPOINT_AGE,
327 	MONITOR_OVLD_BUF_OLDEST_LSN,
328 	MONITOR_OVLD_MAX_AGE_ASYNC,
329 	MONITOR_OVLD_MAX_AGE_SYNC,
330 	MONITOR_PENDING_LOG_FLUSH,
331 	MONITOR_PENDING_CHECKPOINT_WRITE,
332 	MONITOR_LOG_IO,
333 	MONITOR_OVLD_LOG_WAITS,
334 	MONITOR_OVLD_LOG_WRITE_REQUEST,
335 	MONITOR_OVLD_LOG_WRITES,
336 	MONITOR_OVLD_LOG_PADDED,
337 
338 	/* Page Manager related counters */
339 	MONITOR_MODULE_PAGE,
340 	MONITOR_PAGE_COMPRESS,
341 	MONITOR_PAGE_DECOMPRESS,
342 	MONITOR_PAD_INCREMENTS,
343 	MONITOR_PAD_DECREMENTS,
344 	/* New monitor variables for page compression */
345 	MONITOR_OVLD_PAGE_COMPRESS_SAVED,
346 	MONITOR_OVLD_PAGES_PAGE_COMPRESSED,
347 	MONITOR_OVLD_PAGE_COMPRESSED_TRIM_OP,
348 	MONITOR_OVLD_PAGES_PAGE_DECOMPRESSED,
349 	MONITOR_OVLD_PAGES_PAGE_COMPRESSION_ERROR,
350 
351 	/* New monitor variables for page encryption */
352 	MONITOR_OVLD_PAGES_ENCRYPTED,
353 	MONITOR_OVLD_PAGES_DECRYPTED,
354 
355 	/* Index related counters */
356 	MONITOR_MODULE_INDEX,
357 	MONITOR_INDEX_SPLIT,
358 	MONITOR_INDEX_MERGE_ATTEMPTS,
359 	MONITOR_INDEX_MERGE_SUCCESSFUL,
360 	MONITOR_INDEX_REORG_ATTEMPTS,
361 	MONITOR_INDEX_REORG_SUCCESSFUL,
362 	MONITOR_INDEX_DISCARD,
363 
364 #ifdef BTR_CUR_HASH_ADAPT
365 	/* Adaptive Hash Index related counters */
366 	MONITOR_MODULE_ADAPTIVE_HASH,
367 	MONITOR_OVLD_ADAPTIVE_HASH_SEARCH,
368 #endif /* BTR_CUR_HASH_ADAPT */
369 	MONITOR_OVLD_ADAPTIVE_HASH_SEARCH_BTREE,
370 #ifdef BTR_CUR_HASH_ADAPT
371 	MONITOR_ADAPTIVE_HASH_PAGE_ADDED,
372 	MONITOR_ADAPTIVE_HASH_PAGE_REMOVED,
373 	MONITOR_ADAPTIVE_HASH_ROW_ADDED,
374 	MONITOR_ADAPTIVE_HASH_ROW_REMOVED,
375 	MONITOR_ADAPTIVE_HASH_ROW_REMOVE_NOT_FOUND,
376 	MONITOR_ADAPTIVE_HASH_ROW_UPDATED,
377 #endif /* BTR_CUR_HASH_ADAPT */
378 
379 	/* Tablespace related counters */
380 	MONITOR_MODULE_FIL_SYSTEM,
381 	MONITOR_OVLD_N_FILE_OPENED,
382 
383 	/* InnoDB Change Buffer related counters */
384 	MONITOR_MODULE_IBUF_SYSTEM,
385 	MONITOR_OVLD_IBUF_MERGE_INSERT,
386 	MONITOR_OVLD_IBUF_MERGE_DELETE,
387 	MONITOR_OVLD_IBUF_MERGE_PURGE,
388 	MONITOR_OVLD_IBUF_MERGE_DISCARD_INSERT,
389 	MONITOR_OVLD_IBUF_MERGE_DISCARD_DELETE,
390 	MONITOR_OVLD_IBUF_MERGE_DISCARD_PURGE,
391 	MONITOR_OVLD_IBUF_MERGES,
392 	MONITOR_OVLD_IBUF_SIZE,
393 
394 	/* Counters for server operations */
395 	MONITOR_MODULE_SERVER,
396 	MONITOR_MASTER_THREAD_SLEEP,
397 	MONITOR_OVLD_SERVER_ACTIVITY,
398 	MONITOR_MASTER_ACTIVE_LOOPS,
399 	MONITOR_MASTER_IDLE_LOOPS,
400 	MONITOR_SRV_BACKGROUND_DROP_TABLE_MICROSECOND,
401 	MONITOR_SRV_IBUF_MERGE_MICROSECOND,
402 	MONITOR_SRV_LOG_FLUSH_MICROSECOND,
403 	MONITOR_SRV_MEM_VALIDATE_MICROSECOND,
404 	MONITOR_SRV_PURGE_MICROSECOND,
405 	MONITOR_SRV_DICT_LRU_MICROSECOND,
406 	MONITOR_SRV_DICT_LRU_EVICT_COUNT_ACTIVE,
407 	MONITOR_SRV_DICT_LRU_EVICT_COUNT_IDLE,
408 	MONITOR_SRV_CHECKPOINT_MICROSECOND,
409 	MONITOR_OVLD_SRV_DBLWR_WRITES,
410 	MONITOR_OVLD_SRV_DBLWR_PAGES_WRITTEN,
411 	MONITOR_OVLD_SRV_PAGE_SIZE,
412 	MONITOR_OVLD_RWLOCK_S_SPIN_WAITS,
413 	MONITOR_OVLD_RWLOCK_X_SPIN_WAITS,
414 	MONITOR_OVLD_RWLOCK_SX_SPIN_WAITS,
415 	MONITOR_OVLD_RWLOCK_S_SPIN_ROUNDS,
416 	MONITOR_OVLD_RWLOCK_X_SPIN_ROUNDS,
417 	MONITOR_OVLD_RWLOCK_SX_SPIN_ROUNDS,
418 	MONITOR_OVLD_RWLOCK_S_OS_WAITS,
419 	MONITOR_OVLD_RWLOCK_X_OS_WAITS,
420 	MONITOR_OVLD_RWLOCK_SX_OS_WAITS,
421 
422 	/* Data DML related counters */
423 	MONITOR_MODULE_DML_STATS,
424 	MONITOR_OLVD_ROW_READ,
425 	MONITOR_OLVD_ROW_INSERTED,
426 	MONITOR_OLVD_ROW_DELETED,
427 	MONITOR_OLVD_ROW_UPDTATED,
428 	MONITOR_OLVD_SYSTEM_ROW_READ,
429 	MONITOR_OLVD_SYSTEM_ROW_INSERTED,
430 	MONITOR_OLVD_SYSTEM_ROW_DELETED,
431 	MONITOR_OLVD_SYSTEM_ROW_UPDATED,
432 
433 	/* Data DDL related counters */
434 	MONITOR_MODULE_DDL_STATS,
435 	MONITOR_BACKGROUND_DROP_INDEX,
436 	MONITOR_BACKGROUND_DROP_TABLE,
437 	MONITOR_ONLINE_CREATE_INDEX,
438 	MONITOR_PENDING_ALTER_TABLE,
439 	MONITOR_ALTER_TABLE_SORT_FILES,
440 	MONITOR_ALTER_TABLE_LOG_FILES,
441 
442 	MONITOR_MODULE_ICP,
443 	MONITOR_ICP_ATTEMPTS,
444 	MONITOR_ICP_NO_MATCH,
445 	MONITOR_ICP_OUT_OF_RANGE,
446 	MONITOR_ICP_MATCH,
447 
448 	/* Mutex/RW-Lock related counters */
449 	MONITOR_MODULE_LATCHES,
450 	MONITOR_LATCHES,
451 
452 	/* This is used only for control system to turn
453 	on/off and reset all monitor counters */
454 	MONITOR_ALL_COUNTER,
455 
456 	/* This must be the last member */
457 	NUM_MONITOR
458 };
459 
460 /** This informs the monitor control system to turn
461 on/off and reset monitor counters through wild card match */
462 #define	MONITOR_WILDCARD_MATCH		(NUM_MONITOR + 1)
463 
464 /** Cannot find monitor counter with a specified name */
465 #define	MONITOR_NO_MATCH		(NUM_MONITOR + 2)
466 
467 /** struct monitor_info describes the basic/static information
468 about each monitor counter. */
469 struct monitor_info_t {
470 	const char*	monitor_name;	/*!< Monitor name */
471 	const char*	monitor_module;	/*!< Sub Module the monitor
472 					belongs to */
473 	const char*	monitor_desc;	/*!< Brief desc of monitor counter */
474 	monitor_type_t	monitor_type;	/*!< Type of Monitor Info */
475 	monitor_id_t	monitor_related_id;/*!< Monitor ID of counter that
476 					related to this monitor. This is
477 					set when the monitor belongs to
478 					a "monitor set" */
479 	monitor_id_t	monitor_id;	/*!< Monitor ID as defined in enum
480 					monitor_id_t */
481 };
482 
483 /** Following are the "set_option" values allowed for
484 srv_mon_process_existing_counter() and srv_mon_process_existing_counter()
485 functions. To turn on/off/reset the monitor counters. */
486 enum mon_option_t {
487 	MONITOR_TURN_ON = 1,		/*!< Turn on the counter */
488 	MONITOR_TURN_OFF,		/*!< Turn off the counter */
489 	MONITOR_RESET_VALUE,		/*!< Reset current values */
490 	MONITOR_RESET_ALL_VALUE,	/*!< Reset all values */
491 	MONITOR_GET_VALUE		/*!< Option for
492 					srv_mon_process_existing_counter()
493 					function */
494 };
495 
496 /** Number of bit in a ulint datatype */
497 #define	NUM_BITS_ULINT	(sizeof(ulint) * CHAR_BIT)
498 
499 /** This "monitor_set_tbl" is a bitmap records whether a particular monitor
500 counter has been turned on or off */
501 extern Atomic_relaxed<ulint>
502     monitor_set_tbl[(NUM_MONITOR + NUM_BITS_ULINT - 1) / NUM_BITS_ULINT];
503 
504 /** Macros to turn on/off the control bit in monitor_set_tbl for a monitor
505 counter option. */
506 #define MONITOR_ON(monitor)                                                   \
507   (monitor_set_tbl[unsigned(monitor) / NUM_BITS_ULINT].fetch_or(              \
508       (ulint(1) << (unsigned(monitor) % NUM_BITS_ULINT))))
509 
510 #define MONITOR_OFF(monitor)                                                  \
511   (monitor_set_tbl[unsigned(monitor) / NUM_BITS_ULINT].fetch_and(             \
512       ~(ulint(1) << (unsigned(monitor) % NUM_BITS_ULINT))))
513 
514 /** Check whether the requested monitor is turned on/off */
515 #define MONITOR_IS_ON(monitor)                                                \
516   (monitor_set_tbl[unsigned(monitor) / NUM_BITS_ULINT] &                      \
517    (ulint(1) << (unsigned(monitor) % NUM_BITS_ULINT)))
518 
519 /** The actual monitor counter array that records each monintor counter
520 value */
521 extern monitor_value_t	 innodb_counter_value[NUM_MONITOR];
522 
523 /** Following are macro defines for basic montior counter manipulations.
524 Please note we do not provide any synchronization for these monitor
525 operations due to performance consideration. Most counters can
526 be placed under existing mutex protections in respective code
527 module. */
528 
529 /** Macros to access various fields of a monitor counters */
530 #define MONITOR_FIELD(monitor, field)			\
531 		(innodb_counter_value[monitor].field)
532 
533 #define MONITOR_VALUE(monitor)				\
534 		MONITOR_FIELD(monitor, mon_value)
535 
536 #define MONITOR_MAX_VALUE(monitor)			\
537 		MONITOR_FIELD(monitor, mon_max_value)
538 
539 #define MONITOR_MIN_VALUE(monitor)			\
540 		MONITOR_FIELD(monitor, mon_min_value)
541 
542 #define MONITOR_VALUE_RESET(monitor)			\
543 		MONITOR_FIELD(monitor, mon_value_reset)
544 
545 #define MONITOR_MAX_VALUE_START(monitor)		\
546 		MONITOR_FIELD(monitor, mon_max_value_start)
547 
548 #define MONITOR_MIN_VALUE_START(monitor)		\
549 		MONITOR_FIELD(monitor, mon_min_value_start)
550 
551 #define MONITOR_LAST_VALUE(monitor)			\
552 		MONITOR_FIELD(monitor, mon_last_value)
553 
554 #define MONITOR_START_VALUE(monitor)			\
555 		MONITOR_FIELD(monitor, mon_start_value)
556 
557 #define MONITOR_VALUE_SINCE_START(monitor)		\
558 		(MONITOR_VALUE(monitor) + MONITOR_VALUE_RESET(monitor))
559 
560 #define MONITOR_STATUS(monitor)				\
561 		MONITOR_FIELD(monitor, mon_status)
562 
563 #define MONITOR_SET_START(monitor)					\
564 	do {								\
565 		MONITOR_STATUS(monitor) = MONITOR_STARTED;		\
566 		MONITOR_FIELD((monitor), mon_start_time) = time(NULL);	\
567 	} while (0)
568 
569 #define MONITOR_SET_OFF(monitor)					\
570 	do {								\
571 		MONITOR_STATUS(monitor) = MONITOR_STOPPED;		\
572 		MONITOR_FIELD((monitor), mon_stop_time) = time(NULL);	\
573 	} while (0)
574 
575 #define	MONITOR_INIT_ZERO_VALUE		0
576 
577 /** Max and min values are initialized when we first turn on the monitor
578 counter, and set the MONITOR_STATUS. */
579 #define MONITOR_MAX_MIN_NOT_INIT(monitor)				\
580 		(MONITOR_STATUS(monitor) == MONITOR_INIT_ZERO_VALUE	\
581 		 && MONITOR_MIN_VALUE(monitor) == MONITOR_INIT_ZERO_VALUE \
582 		 && MONITOR_MAX_VALUE(monitor) == MONITOR_INIT_ZERO_VALUE)
583 
584 #define MONITOR_INIT(monitor)						\
585 	if (MONITOR_MAX_MIN_NOT_INIT(monitor)) {			\
586 		MONITOR_MIN_VALUE(monitor) = MIN_RESERVED;		\
587 		MONITOR_MIN_VALUE_START(monitor) = MIN_RESERVED;	\
588 		MONITOR_MAX_VALUE(monitor) = MAX_RESERVED;		\
589 		MONITOR_MAX_VALUE_START(monitor) = MAX_RESERVED;	\
590 	}
591 
592 /** Macros to increment/decrement the counters. The normal
593 monitor counter operation expects appropriate synchronization
594 already exists. No additional mutex is necessary when operating
595 on the counters */
596 #define	MONITOR_INC(monitor)						\
597 	if (MONITOR_IS_ON(monitor)) {					\
598 		MONITOR_VALUE(monitor)++;				\
599 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
600 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
601 		}							\
602 	}
603 
604 /** Atomically increment a monitor counter.
605 Use MONITOR_INC if appropriate mutex protection exists.
606 @param monitor	monitor to be incremented by 1
607 @param enabled	whether the monitor is enabled */
608 #define MONITOR_ATOMIC_INC_LOW(monitor, enabled)			\
609 	if (enabled) {							\
610 		ib_uint64_t	value;					\
611 		value  = my_atomic_add64_explicit(			\
612 			(int64*) &MONITOR_VALUE(monitor), 1,		\
613 			MY_MEMORY_ORDER_RELAXED) + 1;			\
614 		/* Note: This is not 100% accurate because of the	\
615 		inherent race, we ignore it due to performance. */	\
616 		if (value > (ib_uint64_t) MONITOR_MAX_VALUE(monitor)) {	\
617 			MONITOR_MAX_VALUE(monitor) = value;		\
618 		}							\
619 	}
620 
621 /** Atomically decrement a monitor counter.
622 Use MONITOR_DEC if appropriate mutex protection exists.
623 @param monitor	monitor to be decremented by 1
624 @param enabled	whether the monitor is enabled */
625 #define MONITOR_ATOMIC_DEC_LOW(monitor, enabled)			\
626 	if (enabled) {							\
627 		ib_uint64_t	value;					\
628 		value = my_atomic_add64_explicit(			\
629 			(int64*) &MONITOR_VALUE(monitor), -1,		\
630 			MY_MEMORY_ORDER_RELAXED) - 1;			\
631 		/* Note: This is not 100% accurate because of the	\
632 		inherent race, we ignore it due to performance. */	\
633 		if (value < (ib_uint64_t) MONITOR_MIN_VALUE(monitor)) {	\
634 			MONITOR_MIN_VALUE(monitor) = value;		\
635 		}							\
636 	}
637 
638 /** Atomically increment a monitor counter if it is enabled.
639 Use MONITOR_INC if appropriate mutex protection exists.
640 @param monitor	monitor to be incremented by 1 */
641 #define MONITOR_ATOMIC_INC(monitor)				\
642 	MONITOR_ATOMIC_INC_LOW(monitor, MONITOR_IS_ON(monitor))
643 /** Atomically decrement a monitor counter if it is enabled.
644 Use MONITOR_DEC if appropriate mutex protection exists.
645 @param monitor	monitor to be decremented by 1 */
646 #define MONITOR_ATOMIC_DEC(monitor)				\
647 	MONITOR_ATOMIC_DEC_LOW(monitor, MONITOR_IS_ON(monitor))
648 
649 #define	MONITOR_DEC(monitor)						\
650 	if (MONITOR_IS_ON(monitor)) {					\
651 		MONITOR_VALUE(monitor)--;				\
652 		if (MONITOR_VALUE(monitor) < MONITOR_MIN_VALUE(monitor)) {  \
653 			MONITOR_MIN_VALUE(monitor) = MONITOR_VALUE(monitor);\
654 		}							\
655 	}
656 
657 #ifdef HAVE_valgrind
658 # define MONITOR_CHECK_DEFINED(value) do {	\
659     mon_type_t m __attribute__((unused))= value;        \
660 	MEM_CHECK_DEFINED(&m, sizeof m);	\
661 } while (0)
662 #else /* HAVE_valgrind */
663 # define MONITOR_CHECK_DEFINED(value) (void) 0
664 #endif /* HAVE_valgrind */
665 
666 #define	MONITOR_INC_VALUE(monitor, value)				\
667 	MONITOR_CHECK_DEFINED(value);					\
668 	if (MONITOR_IS_ON(monitor)) {					\
669 		MONITOR_VALUE(monitor) += (mon_type_t) (value);		\
670 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
671 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
672 		}							\
673 	}
674 
675 #define	MONITOR_DEC_VALUE(monitor, value)				\
676 	MONITOR_CHECK_DEFINED(value);					\
677 	if (MONITOR_IS_ON(monitor)) {					\
678 		ut_ad(MONITOR_VALUE(monitor) >= (mon_type_t) (value);	\
679 		MONITOR_VALUE(monitor) -= (mon_type_t) (value);		\
680 		if (MONITOR_VALUE(monitor) < MONITOR_MIN_VALUE(monitor)) {  \
681 			MONITOR_MIN_VALUE(monitor) = MONITOR_VALUE(monitor);\
682 		}							\
683 	}
684 
685 /* Increment/decrement counter without check the monitor on/off bit, which
686 could already be checked as a module group */
687 #define	MONITOR_INC_NOCHECK(monitor)					\
688 	do {								\
689 		MONITOR_VALUE(monitor)++;				\
690 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
691 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
692 		}							\
693 	} while (0)							\
694 
695 #define	MONITOR_DEC_NOCHECK(monitor)					\
696 	do {								\
697 		MONITOR_VALUE(monitor)--;				\
698 		if (MONITOR_VALUE(monitor) < MONITOR_MIN_VALUE(monitor)) {  \
699 			MONITOR_MIN_VALUE(monitor) = MONITOR_VALUE(monitor);\
700 		}							\
701 	} while (0)
702 
703 /** Directly set a monitor counter's value */
704 #define	MONITOR_SET(monitor, value)					\
705 	MONITOR_CHECK_DEFINED(value);					\
706 	if (MONITOR_IS_ON(monitor)) {					\
707 		MONITOR_VALUE(monitor) = (mon_type_t) (value);		\
708 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
709 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
710 		}							\
711 		if (MONITOR_VALUE(monitor) < MONITOR_MIN_VALUE(monitor)) {  \
712 			MONITOR_MIN_VALUE(monitor) = MONITOR_VALUE(monitor);\
713 		}							\
714 	}
715 
716 /** Add time difference between now and input "value" (in seconds) to the
717 monitor counter
718 @param monitor monitor to update for the time difference
719 @param value the start time value */
720 #define	MONITOR_INC_TIME_IN_MICRO_SECS(monitor, value)			\
721 	MONITOR_CHECK_DEFINED(value);					\
722 	if (MONITOR_IS_ON(monitor)) {					\
723 		uintmax_t	old_time = value;			\
724 		value = microsecond_interval_timer();			\
725 		MONITOR_VALUE(monitor) += (mon_type_t) (value - old_time);\
726 	}
727 
728 /** This macro updates 3 counters in one call. However, it only checks the
729 main/first monitor counter 'monitor', to see it is on or off to decide
730 whether to do the update.
731 @param monitor the main monitor counter to update. It accounts for
732 			the accumulative value for the counter.
733 @param monitor_n_calls counter that counts number of times this macro is
734 			called
735 @param monitor_per_call counter that records the current and max value of
736 			each incremental value
737 @param value incremental value to record this time */
738 #define MONITOR_INC_VALUE_CUMULATIVE(					\
739 		monitor, monitor_n_calls, monitor_per_call, value)	\
740 	MONITOR_CHECK_DEFINED(value);					\
741 	if (MONITOR_IS_ON(monitor)) {					\
742 		MONITOR_VALUE(monitor_n_calls)++;			\
743 		MONITOR_VALUE(monitor_per_call) = (mon_type_t) (value);	\
744 		if (MONITOR_VALUE(monitor_per_call)			\
745 		    > MONITOR_MAX_VALUE(monitor_per_call)) {		\
746 			MONITOR_MAX_VALUE(monitor_per_call) =		\
747 				 (mon_type_t) (value);			\
748 		}							\
749 		MONITOR_VALUE(monitor) += (mon_type_t) (value);		\
750 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
751 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
752 		}							\
753 	}
754 
755 /** Directly set a monitor counter's value, and if the value
756 is monotonically increasing, only max value needs to be updated */
757 #define	MONITOR_SET_UPD_MAX_ONLY(monitor, value)			\
758 	MONITOR_CHECK_DEFINED(value);					\
759 	if (MONITOR_IS_ON(monitor)) {					\
760 		MONITOR_VALUE(monitor) = (mon_type_t) (value);		\
761 		if (MONITOR_VALUE(monitor) > MONITOR_MAX_VALUE(monitor)) {  \
762 			MONITOR_MAX_VALUE(monitor) = MONITOR_VALUE(monitor);\
763 		}							\
764 	}
765 
766 /** Some values such as log sequence number are montomically increasing
767 number, do not need to record max/min values */
768 #define MONITOR_SET_SIMPLE(monitor, value)				\
769 	MONITOR_CHECK_DEFINED(value);					\
770 	if (MONITOR_IS_ON(monitor)) {					\
771 		MONITOR_VALUE(monitor) = (mon_type_t) (value);		\
772 	}
773 
774 /** Reset the monitor value and max/min value to zero. The reset
775 operation would only be conducted when the counter is turned off */
776 #define MONITOR_RESET_ALL(monitor)					\
777 	do {								\
778 		MONITOR_VALUE(monitor) = MONITOR_INIT_ZERO_VALUE;	\
779 		MONITOR_MAX_VALUE(monitor) = MAX_RESERVED;		\
780 		MONITOR_MIN_VALUE(monitor) = MIN_RESERVED;		\
781 		MONITOR_VALUE_RESET(monitor) = MONITOR_INIT_ZERO_VALUE;	\
782 		MONITOR_MAX_VALUE_START(monitor) = MAX_RESERVED;	\
783 		MONITOR_MIN_VALUE_START(monitor) = MIN_RESERVED;	\
784 		MONITOR_LAST_VALUE(monitor) = MONITOR_INIT_ZERO_VALUE;	\
785 		MONITOR_FIELD(monitor, mon_start_time) =		\
786 					MONITOR_INIT_ZERO_VALUE;	\
787 		MONITOR_FIELD(monitor, mon_stop_time) =			\
788 					MONITOR_INIT_ZERO_VALUE;	\
789 		MONITOR_FIELD(monitor, mon_reset_time) =		\
790 					MONITOR_INIT_ZERO_VALUE;	\
791 	} while (0)
792 
793 /** Following four macros defines necessary operations to fetch and
794 consolidate information from existing system status variables. */
795 
796 /** Save the passed-in value to mon_start_value field of monitor
797 counters */
798 #define MONITOR_SAVE_START(monitor, value) do {				\
799 	MONITOR_CHECK_DEFINED(value);					\
800 	(MONITOR_START_VALUE(monitor) =					\
801 		(mon_type_t) (value) - MONITOR_VALUE_RESET(monitor));	\
802 	} while (0)
803 
804 /** Save the passed-in value to mon_last_value field of monitor
805 counters */
806 #define MONITOR_SAVE_LAST(monitor)					\
807 	do {								\
808 		MONITOR_LAST_VALUE(monitor) = MONITOR_VALUE(monitor);	\
809 		MONITOR_START_VALUE(monitor) += MONITOR_VALUE(monitor);	\
810 	} while (0)
811 
812 /** Set monitor value to the difference of value and mon_start_value
813 compensated by mon_last_value if accumulated value is required. */
814 #define MONITOR_SET_DIFF(monitor, value)				\
815 	MONITOR_SET_UPD_MAX_ONLY(monitor, ((value)			\
816 	- MONITOR_VALUE_RESET(monitor)					\
817 	- MONITOR_FIELD(monitor, mon_start_value)			\
818 	+ MONITOR_FIELD(monitor, mon_last_value)))
819 
820 /****************************************************************//**
821 Get monitor's monitor_info_t by its monitor id (index into the
822 innodb_counter_info array
823 @return Point to corresponding monitor_info_t, or NULL if no such
824 monitor */
825 monitor_info_t*
826 srv_mon_get_info(
827 /*=============*/
828 	monitor_id_t	monitor_id);	/*!< id index into the
829 					innodb_counter_info array */
830 /****************************************************************//**
831 Get monitor's name by its monitor id (index into the
832 innodb_counter_info array
833 @return corresponding monitor name, or NULL if no such
834 monitor */
835 const char*
836 srv_mon_get_name(
837 /*=============*/
838 	monitor_id_t	monitor_id);	/*!< id index into the
839 					innodb_counter_info array */
840 
841 /****************************************************************//**
842 Turn on/off/reset monitor counters in a module. If module_value
843 is NUM_MONITOR then turn on all monitor counters.
844 @return 0 if successful, or the first monitor that cannot be
845 turned on because it is already turned on. */
846 void
847 srv_mon_set_module_control(
848 /*=======================*/
849 	monitor_id_t	module_id,	/*!< in: Module ID as in
850 					monitor_counter_id. If it is
851 					set to NUM_MONITOR, this means
852 					we shall turn on all the counters */
853 	mon_option_t	set_option);	/*!< in: Turn on/off reset the
854 					counter */
855 /****************************************************************//**
856 This function consolidates some existing server counters used
857 by "system status variables". These existing system variables do not have
858 mechanism to start/stop and reset the counters, so we simulate these
859 controls by remembering the corresponding counter values when the
860 corresponding monitors are turned on/off/reset, and do appropriate
861 mathematics to deduct the actual value. */
862 void
863 srv_mon_process_existing_counter(
864 /*=============================*/
865 	monitor_id_t	monitor_id,	/*!< in: the monitor's ID as in
866 					monitor_counter_id */
867 	mon_option_t	set_option);	/*!< in: Turn on/off reset the
868 					counter */
869 /*************************************************************//**
870 This function is used to calculate the maximum counter value
871 since the start of monitor counter
872 @return max counter value since start. */
873 UNIV_INLINE
874 mon_type_t
875 srv_mon_calc_max_since_start(
876 /*=========================*/
877 	monitor_id_t	monitor);	/*!< in: monitor id */
878 /*************************************************************//**
879 This function is used to calculate the minimum counter value
880 since the start of monitor counter
881 @return min counter value since start. */
882 UNIV_INLINE
883 mon_type_t
884 srv_mon_calc_min_since_start(
885 /*=========================*/
886 	monitor_id_t	monitor);	/*!< in: monitor id*/
887 /*************************************************************//**
888 Reset a monitor, create a new base line with the current monitor
889 value. This baseline is recorded by MONITOR_VALUE_RESET(monitor) */
890 void
891 srv_mon_reset(
892 /*==========*/
893 	monitor_id_t	monitor);	/*!< in: monitor id*/
894 /*************************************************************//**
895 This function resets all values of a monitor counter */
896 UNIV_INLINE
897 void
898 srv_mon_reset_all(
899 /*==============*/
900 	monitor_id_t	monitor);	/*!< in: monitor id*/
901 /*************************************************************//**
902 Turn on monitor counters that are marked as default ON. */
903 void
904 srv_mon_default_on(void);
905 /*====================*/
906 
907 #include "srv0mon.inl"
908 
909 #endif
910