InnoDB Plugin  1.0
log0log.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
4 Copyright (c) 2009, Google Inc.
5 
6 Portions of this file contain modifications contributed and copyrighted by
7 Google, Inc. Those modifications are gratefully acknowledged and are described
8 briefly in the InnoDB documentation. The contributions by Google are
9 incorporated with their permission, and subject to the conditions contained in
10 the file COPYING.Google.
11 
12 This program is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free Software
14 Foundation; version 2 of the License.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License along with
21 this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
23 
24 *****************************************************************************/
25 
26 /**************************************************/
33 #ifndef log0log_h
34 #define log0log_h
35 
36 #include "univ.i"
37 #include "ut0byte.h"
38 #include "ut0lst.h"
39 #ifndef UNIV_HOTBACKUP
40 #include "sync0sync.h"
41 #include "sync0rw.h"
42 #endif /* !UNIV_HOTBACKUP */
43 
44 /* Type used for all log sequence number storage and arithmetics */
45 typedef ib_uint64_t lsn_t;
46 #define LSN_MAX IB_UINT64_MAX
47 
48 #define LSN_PF UINT64PF
49 
51 struct log_t;
53 struct log_group_t;
54 
55 #ifdef UNIV_DEBUG
56 
57 extern ibool log_do_write;
59 extern ibool log_debug_writes;
60 #else /* UNIV_DEBUG */
61 
62 # define log_do_write TRUE
63 #endif /* UNIV_DEBUG */
64 
66 #define LOG_NO_WAIT 91
67 #define LOG_WAIT_ONE_GROUP 92
68 #define LOG_WAIT_ALL_GROUPS 93
69 /* @} */
71 #define LOG_MAX_N_GROUPS 32
72 
73 /*******************************************************************/
76 UNIV_INTERN
77 ulint
79 /*==================*/
80  ib_int64_t* log_file_offset,
82  ib_uint64_t first_header_lsn,
84  ib_uint64_t lsn,
86  ulint n_log_files,
88  ib_int64_t log_file_size);
90 #ifndef UNIV_HOTBACKUP
91 /************************************************************/
95 UNIV_INLINE
96 lsn_t
98 /*=======================*/
99  const void* str,
100  ulint len,
101  lsn_t* start_lsn);
102 /***********************************************************************/
104 UNIV_INLINE
105 void
106 log_release(void);
107 /*=============*/
108 /***********************************************************************/
113 UNIV_INLINE
114 void
115 log_free_check(void);
116 /*================*/
117 /************************************************************/
121 UNIV_INTERN
122 lsn_t
124 /*=================*/
125  ulint len);
126 /************************************************************/
129 UNIV_INTERN
130 void
132 /*==========*/
133  byte* str,
134  ulint str_len);
135 /************************************************************/
138 UNIV_INTERN
139 lsn_t
140 log_close(void);
141 /*===========*/
142 /************************************************************/
145 UNIV_INLINE
146 lsn_t
147 log_get_lsn(void);
148 /*=============*/
149 /****************************************************************
150 Gets the log group capacity. It is OK to read the value without
151 holding log_sys->mutex because it is constant.
152 @return log group capacity */
153 UNIV_INLINE
154 lsn_t
155 log_get_capacity(void);
156 /*==================*/
157 /****************************************************************
158 Get log_sys::max_modified_age_async. It is OK to read the value without
159 holding log_sys::mutex because it is constant.
160 @return max_modified_age_async */
161 UNIV_INLINE
162 lsn_t
163 log_get_max_modified_age_async(void);
164 /*================================*/
165 /******************************************************/
167 UNIV_INTERN
168 void
169 log_init(void);
170 /*==========*/
171 /******************************************************************/
173 UNIV_INTERN
174 void
176 /*===========*/
177  ulint id,
178  ulint n_files,
179  lsn_t file_size,
180  ulint space_id,
183  ulint archive_space_id);
188 /******************************************************/
190 UNIV_INTERN
191 void
193 /*============*/
194  log_group_t* group);
195 /******************************************************/
200 UNIV_INTERN
201 void
203 /*============*/
204  lsn_t lsn,
206  ulint wait,
208  ibool flush_to_disk);
211 /****************************************************************/
213 UNIV_INTERN
214 void
216 /*==========================*/
217 /****************************************************************/
222 UNIV_INTERN
223 void
225 /*==========================*/
226  ibool flush); /*<! in: flush the logs to disk */
227 /******************************************************/
233 UNIV_INTERN
234 ibool
236 /*===========*/
237  ibool sync,
239  ibool write_always);
245 /****************************************************************/
247 UNIV_INTERN
248 void
250 /*===================*/
251  lsn_t lsn,
254  ibool write_always);
261 /****************************************************************/
266 UNIV_INTERN
267 void
269 /*=======================================*/
270 /******************************************************/
272 UNIV_INTERN
273 void
275 /*===========================*/
276  log_group_t* group,
277  ulint field);
278 /*******************************************************************/
280 UNIV_INTERN
281 void
283 /*==============================*/
284  const byte* buf,
285  ulint n,
286  ulint* file_no,
287  ulint* offset);
288 /******************************************************/
290 UNIV_INTERN
291 void
293 /*==================================*/
294 /********************************************************************/
297 UNIV_INTERN
298 ibool
300 /*===========*/
301  ibool sync,
302  ulint* n_bytes);
304 /****************************************************************/
311 UNIV_INTERN
312 ulint
313 log_archive_stop(void);
314 /*==================*/
315 /****************************************************************/
318 UNIV_INTERN
319 ulint
320 log_archive_start(void);
321 /*===================*/
322 /****************************************************************/
325 UNIV_INTERN
326 ulint
328 /*==========================*/
329 /****************************************************************/
332 UNIV_INTERN
333 ulint
335 /*========================*/
336 /******************************************************/
338 UNIV_INTERN
339 void
341 /*=======================*/
342  char* buf,
343  ulint id,
344  ulint file_no);
345 #else /* !UNIV_HOTBACKUP */
346 /******************************************************/
349 UNIV_INTERN
350 void
351 log_reset_first_header_and_checkpoint(
352 /*==================================*/
353  byte* hdr_buf,
355  ib_uint64_t start);
358 #endif /* !UNIV_HOTBACKUP */
359 /********************************************************************/
364 UNIV_INTERN
365 void
366 log_check_margins(void);
367 /*===================*/
368 #ifndef UNIV_HOTBACKUP
369 /******************************************************/
371 UNIV_INTERN
372 void
374 /*===================*/
375  ulint type,
376  byte* buf,
377  log_group_t* group,
378  lsn_t start_lsn,
379  lsn_t end_lsn);
380 /******************************************************/
382 UNIV_INTERN
383 void
385 /*================*/
386  log_group_t* group,
387  byte* buf,
388  ulint len,
390  lsn_t start_lsn,
393  ulint new_data_offset);
397 /********************************************************/
401 UNIV_INTERN
402 void
404 /*=================*/
405  log_group_t* group,
406  lsn_t lsn);
408 /******************************************************/
412 UNIV_INTERN
413 lsn_t
415 /*===================*/
416  const log_group_t* group);
417 #endif /* !UNIV_HOTBACKUP */
418 /************************************************************/
421 UNIV_INLINE
422 ibool
424 /*====================*/
425  const byte* log_block);
426 /************************************************************/
429 UNIV_INLINE
430 ulint
432 /*=================*/
433  const byte* log_block);
434 /************************************************************/
437 UNIV_INLINE
438 ulint
440 /*===================*/
441  const byte* log_block);
442 /************************************************************/
444 UNIV_INLINE
445 void
447 /*===================*/
448  byte* log_block,
449  ulint len);
450 /************************************************************/
453 UNIV_INLINE
454 ulint
456 /*====================*/
457  const byte* block);
458 /************************************************************/
461 UNIV_INLINE
462 ulint
464 /*===================*/
465  const byte* log_block);
466 /************************************************************/
468 UNIV_INLINE
469 void
471 /*===================*/
472  byte* log_block,
473  ulint checksum);
474 /************************************************************/
478 UNIV_INLINE
479 ulint
481 /*==========================*/
482  const byte* log_block);
483 /************************************************************/
485 UNIV_INLINE
486 void
488 /*==========================*/
489  byte* log_block,
490  ulint offset);
491 /************************************************************/
494 UNIV_INLINE
495 ulint
497 /*========================*/
498  const byte* log_block);
499 /************************************************************/
501 UNIV_INLINE
502 void
504 /*===========*/
505  byte* log_block,
506  lsn_t lsn);
507 /************************************************************/
510 UNIV_INLINE
511 void
513 /*=========================*/
514  byte* log_block,
515  lsn_t lsn);
516 /************************************************************/
519 UNIV_INLINE
520 ulint
522 /*========================*/
523  lsn_t lsn);
524 /******************************************************/
526 UNIV_INTERN
527 void
528 log_print(
529 /*======*/
530  FILE* file);
531 /******************************************************/
534 UNIV_INTERN
535 ibool
537 /*=========*/
538  lsn_t* lsn);
539 /**********************************************************************/
541 UNIV_INTERN
542 void
543 log_refresh_stats(void);
544 /*===================*/
545 /********************************************************/
547 UNIV_INTERN
548 void
549 log_group_close_all(void);
550 /*=====================*/
551 /********************************************************/
553 UNIV_INTERN
554 void
555 log_shutdown(void);
556 /*==============*/
557 /********************************************************/
559 UNIV_INTERN
560 void
561 log_mem_free(void);
562 /*==============*/
563 
564 extern log_t* log_sys;
565 
566 /* Values used as flags */
567 #define LOG_FLUSH 7652559
568 #define LOG_CHECKPOINT 78656949
569 #ifdef UNIV_LOG_ARCHIVE
570 # define LOG_ARCHIVE 11122331
571 #endif /* UNIV_LOG_ARCHIVE */
572 #define LOG_RECOVER 98887331
573 
574 /* The counting of lsn's starts from this value: this must be non-zero */
575 #define LOG_START_LSN ((lsn_t) (16 * OS_FILE_LOG_BLOCK_SIZE))
576 
577 #define LOG_BUFFER_SIZE (srv_log_buffer_size * UNIV_PAGE_SIZE)
578 #define LOG_ARCHIVE_BUF_SIZE (srv_log_buffer_size * UNIV_PAGE_SIZE / 4)
579 
580 /* Offsets of a log block header */
581 #define LOG_BLOCK_HDR_NO 0 /* block number which must be > 0 and
582  is allowed to wrap around at 2G; the
583  highest bit is set to 1 if this is the
584  first log block in a log flush write
585  segment */
586 #define LOG_BLOCK_FLUSH_BIT_MASK 0x80000000UL
587  /* mask used to get the highest bit in
588  the preceding field */
589 #define LOG_BLOCK_HDR_DATA_LEN 4 /* number of bytes of log written to
590  this block */
591 #define LOG_BLOCK_FIRST_REC_GROUP 6 /* offset of the first start of an
592  mtr log record group in this log block,
593  0 if none; if the value is the same
594  as LOG_BLOCK_HDR_DATA_LEN, it means
595  that the first rec group has not yet
596  been catenated to this log block, but
597  if it will, it will start at this
598  offset; an archive recovery can
599  start parsing the log records starting
600  from this offset in this log block,
601  if value not 0 */
602 #define LOG_BLOCK_CHECKPOINT_NO 8 /* 4 lower bytes of the value of
603  log_sys->next_checkpoint_no when the
604  log block was last written to: if the
605  block has not yet been written full,
606  this value is only updated before a
607  log buffer flush */
608 #define LOG_BLOCK_HDR_SIZE 12 /* size of the log block header in
609  bytes */
610 
611 /* Offsets of a log block trailer from the end of the block */
612 #define LOG_BLOCK_CHECKSUM 4 /* 4 byte checksum of the log block
613  contents; in InnoDB versions
614  < 3.23.52 this did not contain the
615  checksum but the same value as
616  .._HDR_NO */
617 #define LOG_BLOCK_TRL_SIZE 4 /* trailer size in bytes */
618 
619 /* Offsets for a checkpoint field */
620 #define LOG_CHECKPOINT_NO 0
621 #define LOG_CHECKPOINT_LSN 8
622 #define LOG_CHECKPOINT_OFFSET_LOW32 16
623 #define LOG_CHECKPOINT_LOG_BUF_SIZE 20
624 #define LOG_CHECKPOINT_ARCHIVED_LSN 24
625 #define LOG_CHECKPOINT_GROUP_ARRAY 32
626 
627 /* For each value smaller than LOG_MAX_N_GROUPS the following 8 bytes: */
628 
629 #define LOG_CHECKPOINT_ARCHIVED_FILE_NO 0
630 #define LOG_CHECKPOINT_ARCHIVED_OFFSET 4
631 
632 #define LOG_CHECKPOINT_ARRAY_END (LOG_CHECKPOINT_GROUP_ARRAY\
633  + LOG_MAX_N_GROUPS * 8)
634 #define LOG_CHECKPOINT_CHECKSUM_1 LOG_CHECKPOINT_ARRAY_END
635 #define LOG_CHECKPOINT_CHECKSUM_2 (4 + LOG_CHECKPOINT_ARRAY_END)
636 #if 0
637 #define LOG_CHECKPOINT_FSP_FREE_LIMIT (8 + LOG_CHECKPOINT_ARRAY_END)
638 
652 #define LOG_CHECKPOINT_FSP_MAGIC_N (12 + LOG_CHECKPOINT_ARRAY_END)
653 
659 #define LOG_CHECKPOINT_FSP_MAGIC_N_VAL 1441231243
660 
664 #endif
665 #define LOG_CHECKPOINT_OFFSET_HIGH32 (16 + LOG_CHECKPOINT_ARRAY_END)
666 #define LOG_CHECKPOINT_SIZE (20 + LOG_CHECKPOINT_ARRAY_END)
667 
668 
669 /* Offsets of a log file header */
670 #define LOG_GROUP_ID 0 /* log group number */
671 #define LOG_FILE_START_LSN 4 /* lsn of the start of data in this
672  log file */
673 #define LOG_FILE_NO 12 /* 4-byte archived log file number;
674  this field is only defined in an
675  archived log file */
676 #define LOG_FILE_WAS_CREATED_BY_HOT_BACKUP 16
677  /* a 32-byte field which contains
678  the string 'ibbackup' and the
679  creation time if the log file was
680  created by mysqlbackup --restore;
681  when mysqld is first time started
682  on the restored database, it can
683  print helpful info for the user */
684 #define LOG_FILE_ARCH_COMPLETED OS_FILE_LOG_BLOCK_SIZE
685  /* this 4-byte field is TRUE when
686  the writing of an archived log file
687  has been completed; this field is
688  only defined in an archived log file */
689 #define LOG_FILE_END_LSN (OS_FILE_LOG_BLOCK_SIZE + 4)
690  /* lsn where the archived log file
691  at least extends: actually the
692  archived log file may extend to a
693  later lsn, as long as it is within the
694  same log block as this lsn; this field
695  is defined only when an archived log
696  file has been completely written */
697 #define LOG_CHECKPOINT_1 OS_FILE_LOG_BLOCK_SIZE
698  /* first checkpoint field in the log
699  header; we write alternately to the
700  checkpoint fields when we make new
701  checkpoints; this field is only defined
702  in the first log file of a log group */
703 #define LOG_CHECKPOINT_2 (3 * OS_FILE_LOG_BLOCK_SIZE)
704  /* second checkpoint field in the log
705  header */
706 #define LOG_FILE_HDR_SIZE (4 * OS_FILE_LOG_BLOCK_SIZE)
707 
708 #define LOG_GROUP_OK 301
709 #define LOG_GROUP_CORRUPTED 302
710 
713 struct log_group_t{
714  /* The following fields are protected by log_sys->mutex */
715  ulint id;
716  ulint n_files;
717  lsn_t file_size;
719  ulint space_id;
721  ulint state;
723  lsn_t lsn;
725  lsn_t lsn_offset;
728  byte** file_header_bufs_ptr;
731 #ifdef UNIV_LOG_ARCHIVE
732  /*-----------------------------*/
733  byte** archive_file_header_bufs_ptr;
734  byte** archive_file_header_bufs;
736  ulint archive_space_id;
739  ulint archived_file_no;
741  ulint archived_offset;
745  ulint next_archived_file_no;
751  ulint next_archived_offset;
752 #endif /* UNIV_LOG_ARCHIVE */
753  /*-----------------------------*/
754  lsn_t scanned_lsn;
760  UT_LIST_NODE_T(log_group_t)
761  log_groups;
762 };
763 
765 struct log_t{
766  byte pad[64];
769  lsn_t lsn;
770  ulint buf_free;
772 #ifndef UNIV_HOTBACKUP
773  ib_mutex_t mutex;
775  ib_mutex_t log_flush_order_mutex;
783 #endif /* !UNIV_HOTBACKUP */
784  byte* buf_ptr; /* unaligned log buffer */
785  byte* buf;
786  ulint buf_size;
787  ulint max_buf_free;
790  #ifdef UNIV_LOG_DEBUG
791  ulint old_buf_free;
794  ib_uint64_t old_lsn;
797 #endif /* UNIV_LOG_DEBUG */
798  ibool check_flush_or_checkpoint;
807  UT_LIST_BASE_NODE_T(log_group_t)
808  log_groups;
810 #ifndef UNIV_HOTBACKUP
811 
813  ulint buf_next_to_write;
820  volatile bool is_extending;
822  lsn_t written_to_some_lsn;
828  lsn_t written_to_all_lsn;
844  lsn_t write_lsn;
846  ulint write_end_offset;
851  lsn_t current_flush_lsn;
853  lsn_t flushed_to_disk_lsn;
856  ulint n_pending_writes;
858  /* NOTE on the 'flush' in names of the fields below: starting from
859  4.0.14, we separate the write of the log file and the actual fsync()
860  or other method to flush it to disk. The names below shhould really
861  be 'flush_or_write'! */
862  os_event_t no_flush_event;
868  ibool one_flushed;
872  os_event_t one_flushed_event;
881  ulint n_log_ios;
883  ulint n_log_ios_old;
885  time_t last_printout_time;
887  /* @} */
888 
890  lsn_t log_group_capacity;
895  lsn_t max_modified_age_async;
901  lsn_t max_modified_age_sync;
907  lsn_t max_checkpoint_age_async;
912  lsn_t max_checkpoint_age;
916  ib_uint64_t next_checkpoint_no;
918  lsn_t last_checkpoint_lsn;
920  lsn_t next_checkpoint_lsn;
922  ulint n_pending_checkpoint_writes;
925  rw_lock_t checkpoint_lock;
929 #endif /* !UNIV_HOTBACKUP */
930  byte* checkpoint_buf_ptr;/* unaligned checkpoint header */
931  byte* checkpoint_buf;
933  /* @} */
934 #ifdef UNIV_LOG_ARCHIVE
935 
936  ulint archiving_state;
938  lsn_t archived_lsn;
940  lsn_t max_archived_lsn_age_async;
944  lsn_t max_archived_lsn_age;
947  lsn_t next_archived_lsn;
953  ulint archiving_phase;
955  ulint n_pending_archive_ios;
958  rw_lock_t archive_lock;
962  ulint archive_buf_size;
963  byte* archive_buf;
965  os_event_t archiving_on;
968  /* @} */
969 #endif /* UNIV_LOG_ARCHIVE */
970 };
971 
973 #define log_flush_order_mutex_own() \
974  mutex_own(&log_sys->log_flush_order_mutex)
975 
977 #define log_flush_order_mutex_enter() do { \
978  mutex_enter(&log_sys->log_flush_order_mutex); \
979 } while (0)
980 
981 # define log_flush_order_mutex_exit() do { \
982  mutex_exit(&log_sys->log_flush_order_mutex); \
983 } while (0)
984 
985 #ifdef UNIV_LOG_ARCHIVE
986 
987 #define LOG_ARCH_ON 71
988 #define LOG_ARCH_STOPPING 72
989 #define LOG_ARCH_STOPPING2 73
990 #define LOG_ARCH_STOPPED 74
991 #define LOG_ARCH_OFF 75
992 /* @} */
993 #endif /* UNIV_LOG_ARCHIVE */
994 
995 #ifndef UNIV_NONINL
996 #include "log0log.ic"
997 #endif
998 
999 #endif