~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/innobase/include/log0log.ic

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 
 
3
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
 
4
 
 
5
This program is free software; you can redistribute it and/or modify it under
 
6
the terms of the GNU General Public License as published by the Free Software
 
7
Foundation; version 2 of the License.
 
8
 
 
9
This program is distributed in the hope that it will be useful, but WITHOUT
 
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along with
 
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 
15
Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 
 
17
*****************************************************************************/
 
18
 
 
19
/**************************************************//**
 
20
@file include/log0log.ic
 
21
Database log
 
22
 
 
23
Created 12/9/1995 Heikki Tuuri
 
24
*******************************************************/
 
25
 
 
26
#include "os0file.h"
 
27
#include "mach0data.h"
 
28
#include "mtr0mtr.h"
 
29
 
 
30
/******************************************************//**
 
31
Checks by parsing that the catenated log segment for a single mtr is
 
32
consistent. */
 
33
UNIV_INTERN
 
34
ibool
 
35
log_check_log_recs(
 
36
/*===============*/
 
37
        byte*           buf,            /*!< in: pointer to the start of
 
38
                                        the log segment in the
 
39
                                        log_sys->buf log buffer */
 
40
        ulint           len,            /*!< in: segment length in bytes */
 
41
        ib_uint64_t     buf_start_lsn); /*!< in: buffer start lsn */
 
42
 
 
43
/************************************************************//**
 
44
Gets a log block flush bit.
 
45
@return TRUE if this block was the first to be written in a log flush */
 
46
UNIV_INLINE
 
47
ibool
 
48
log_block_get_flush_bit(
 
49
/*====================*/
 
50
        const byte*     log_block)      /*!< in: log block */
 
51
{
 
52
        if (LOG_BLOCK_FLUSH_BIT_MASK
 
53
            & mach_read_from_4(log_block + LOG_BLOCK_HDR_NO)) {
 
54
 
 
55
                return(TRUE);
 
56
        }
 
57
 
 
58
        return(FALSE);
 
59
}
 
60
 
 
61
/************************************************************//**
 
62
Sets the log block flush bit. */
 
63
UNIV_INLINE
 
64
void
 
65
log_block_set_flush_bit(
 
66
/*====================*/
 
67
        byte*   log_block,      /*!< in/out: log block */
 
68
        ibool   val)            /*!< in: value to set */
 
69
{
 
70
        ulint   field;
 
71
 
 
72
        field = mach_read_from_4(log_block + LOG_BLOCK_HDR_NO);
 
73
 
 
74
        if (val) {
 
75
                field = field | LOG_BLOCK_FLUSH_BIT_MASK;
 
76
        } else {
 
77
                field = field & ~LOG_BLOCK_FLUSH_BIT_MASK;
 
78
        }
 
79
 
 
80
        mach_write_to_4(log_block + LOG_BLOCK_HDR_NO, field);
 
81
}
 
82
 
 
83
/************************************************************//**
 
84
Gets a log block number stored in the header.
 
85
@return log block number stored in the block header */
 
86
UNIV_INLINE
 
87
ulint
 
88
log_block_get_hdr_no(
 
89
/*=================*/
 
90
        const byte*     log_block)      /*!< in: log block */
 
91
{
 
92
        return(~LOG_BLOCK_FLUSH_BIT_MASK
 
93
               & mach_read_from_4(log_block + LOG_BLOCK_HDR_NO));
 
94
}
 
95
 
 
96
/************************************************************//**
 
97
Sets the log block number stored in the header; NOTE that this must be set
 
98
before the flush bit! */
 
99
UNIV_INLINE
 
100
void
 
101
log_block_set_hdr_no(
 
102
/*=================*/
 
103
        byte*   log_block,      /*!< in/out: log block */
 
104
        ulint   n)              /*!< in: log block number: must be > 0 and
 
105
                                < LOG_BLOCK_FLUSH_BIT_MASK */
 
106
{
 
107
        ut_ad(n > 0);
 
108
        ut_ad(n < LOG_BLOCK_FLUSH_BIT_MASK);
 
109
 
 
110
        mach_write_to_4(log_block + LOG_BLOCK_HDR_NO, n);
 
111
}
 
112
 
 
113
/************************************************************//**
 
114
Gets a log block data length.
 
115
@return log block data length measured as a byte offset from the block start */
 
116
UNIV_INLINE
 
117
ulint
 
118
log_block_get_data_len(
 
119
/*===================*/
 
120
        const byte*     log_block)      /*!< in: log block */
 
121
{
 
122
        return(mach_read_from_2(log_block + LOG_BLOCK_HDR_DATA_LEN));
 
123
}
 
124
 
 
125
/************************************************************//**
 
126
Sets the log block data length. */
 
127
UNIV_INLINE
 
128
void
 
129
log_block_set_data_len(
 
130
/*===================*/
 
131
        byte*   log_block,      /*!< in/out: log block */
 
132
        ulint   len)            /*!< in: data length */
 
133
{
 
134
        mach_write_to_2(log_block + LOG_BLOCK_HDR_DATA_LEN, len);
 
135
}
 
136
 
 
137
/************************************************************//**
 
138
Gets a log block first mtr log record group offset.
 
139
@return first mtr log record group byte offset from the block start, 0
 
140
if none */
 
141
UNIV_INLINE
 
142
ulint
 
143
log_block_get_first_rec_group(
 
144
/*==========================*/
 
145
        const byte*     log_block)      /*!< in: log block */
 
146
{
 
147
        return(mach_read_from_2(log_block + LOG_BLOCK_FIRST_REC_GROUP));
 
148
}
 
149
 
 
150
/************************************************************//**
 
151
Sets the log block first mtr log record group offset. */
 
152
UNIV_INLINE
 
153
void
 
154
log_block_set_first_rec_group(
 
155
/*==========================*/
 
156
        byte*   log_block,      /*!< in/out: log block */
 
157
        ulint   offset)         /*!< in: offset, 0 if none */
 
158
{
 
159
        mach_write_to_2(log_block + LOG_BLOCK_FIRST_REC_GROUP, offset);
 
160
}
 
161
 
 
162
/************************************************************//**
 
163
Gets a log block checkpoint number field (4 lowest bytes).
 
164
@return checkpoint no (4 lowest bytes) */
 
165
UNIV_INLINE
 
166
ulint
 
167
log_block_get_checkpoint_no(
 
168
/*========================*/
 
169
        const byte*     log_block)      /*!< in: log block */
 
170
{
 
171
        return(mach_read_from_4(log_block + LOG_BLOCK_CHECKPOINT_NO));
 
172
}
 
173
 
 
174
/************************************************************//**
 
175
Sets a log block checkpoint number field (4 lowest bytes). */
 
176
UNIV_INLINE
 
177
void
 
178
log_block_set_checkpoint_no(
 
179
/*========================*/
 
180
        byte*           log_block,      /*!< in/out: log block */
 
181
        ib_uint64_t     no)             /*!< in: checkpoint no */
 
182
{
 
183
        mach_write_to_4(log_block + LOG_BLOCK_CHECKPOINT_NO, (ulint) no);
 
184
}
 
185
 
 
186
/************************************************************//**
 
187
Converts a lsn to a log block number.
 
188
@return log block number, it is > 0 and <= 1G */
 
189
UNIV_INLINE
 
190
ulint
 
191
log_block_convert_lsn_to_no(
 
192
/*========================*/
 
193
        ib_uint64_t     lsn)    /*!< in: lsn of a byte within the block */
 
194
{
 
195
        return(((ulint) (lsn / OS_FILE_LOG_BLOCK_SIZE) & 0x3FFFFFFFUL) + 1);
 
196
}
 
197
 
 
198
/************************************************************//**
 
199
Calculates the checksum for a log block.
 
200
@return checksum */
 
201
UNIV_INLINE
 
202
ulint
 
203
log_block_calc_checksum(
 
204
/*====================*/
 
205
        const byte*     block)  /*!< in: log block */
 
206
{
 
207
        ulint   sum;
 
208
        ulint   sh;
 
209
        ulint   i;
 
210
 
 
211
        sum = 1;
 
212
        sh = 0;
 
213
 
 
214
        for (i = 0; i < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE; i++) {
 
215
                ulint   b = (ulint) block[i];
 
216
                sum &= 0x7FFFFFFFUL;
 
217
                sum += b;
 
218
                sum += b << sh;
 
219
                sh++;
 
220
                if (sh > 24) {
 
221
                        sh = 0;
 
222
                }
 
223
        }
 
224
 
 
225
        return(sum);
 
226
}
 
227
 
 
228
/************************************************************//**
 
229
Gets a log block checksum field value.
 
230
@return checksum */
 
231
UNIV_INLINE
 
232
ulint
 
233
log_block_get_checksum(
 
234
/*===================*/
 
235
        const byte*     log_block)      /*!< in: log block */
 
236
{
 
237
        return(mach_read_from_4(log_block + OS_FILE_LOG_BLOCK_SIZE
 
238
                                - LOG_BLOCK_CHECKSUM));
 
239
}
 
240
 
 
241
/************************************************************//**
 
242
Sets a log block checksum field value. */
 
243
UNIV_INLINE
 
244
void
 
245
log_block_set_checksum(
 
246
/*===================*/
 
247
        byte*   log_block,      /*!< in/out: log block */
 
248
        ulint   checksum)       /*!< in: checksum */
 
249
{
 
250
        mach_write_to_4(log_block + OS_FILE_LOG_BLOCK_SIZE
 
251
                        - LOG_BLOCK_CHECKSUM,
 
252
                        checksum);
 
253
}
 
254
 
 
255
/************************************************************//**
 
256
Initializes a log block in the log buffer. */
 
257
UNIV_INLINE
 
258
void
 
259
log_block_init(
 
260
/*===========*/
 
261
        byte*           log_block,      /*!< in: pointer to the log buffer */
 
262
        ib_uint64_t     lsn)            /*!< in: lsn within the log block */
 
263
{
 
264
        ulint   no;
 
265
 
 
266
        ut_ad(mutex_own(&(log_sys->mutex)));
 
267
 
 
268
        no = log_block_convert_lsn_to_no(lsn);
 
269
 
 
270
        log_block_set_hdr_no(log_block, no);
 
271
 
 
272
        log_block_set_data_len(log_block, LOG_BLOCK_HDR_SIZE);
 
273
        log_block_set_first_rec_group(log_block, 0);
 
274
}
 
275
 
 
276
/************************************************************//**
 
277
Initializes a log block in the log buffer in the old format, where there
 
278
was no checksum yet. */
 
279
UNIV_INLINE
 
280
void
 
281
log_block_init_in_old_format(
 
282
/*=========================*/
 
283
        byte*           log_block,      /*!< in: pointer to the log buffer */
 
284
        ib_uint64_t     lsn)            /*!< in: lsn within the log block */
 
285
{
 
286
        ulint   no;
 
287
 
 
288
        ut_ad(mutex_own(&(log_sys->mutex)));
 
289
 
 
290
        no = log_block_convert_lsn_to_no(lsn);
 
291
 
 
292
        log_block_set_hdr_no(log_block, no);
 
293
        mach_write_to_4(log_block + OS_FILE_LOG_BLOCK_SIZE
 
294
                        - LOG_BLOCK_CHECKSUM, no);
 
295
        log_block_set_data_len(log_block, LOG_BLOCK_HDR_SIZE);
 
296
        log_block_set_first_rec_group(log_block, 0);
 
297
}
 
298
 
 
299
#ifndef UNIV_HOTBACKUP
 
300
/************************************************************//**
 
301
Writes to the log the string given. The log must be released with
 
302
log_release.
 
303
@return end lsn of the log record, zero if did not succeed */
 
304
UNIV_INLINE
 
305
ib_uint64_t
 
306
log_reserve_and_write_fast(
 
307
/*=======================*/
 
308
        byte*           str,    /*!< in: string */
 
309
        ulint           len,    /*!< in: string length */
 
310
        ib_uint64_t*    start_lsn,/*!< out: start lsn of the log record */
 
311
        ibool*          success)/*!< out: TRUE if success */
 
312
{
 
313
        log_t*          log     = log_sys;
 
314
        ulint           data_len;
 
315
        ib_uint64_t     lsn;
 
316
 
 
317
        *success = TRUE;
 
318
 
 
319
        mutex_enter(&(log->mutex));
 
320
 
 
321
        data_len = len + log->buf_free % OS_FILE_LOG_BLOCK_SIZE;
 
322
 
 
323
        if (data_len >= OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
 
324
 
 
325
                /* The string does not fit within the current log block
 
326
                or the log block would become full */
 
327
 
 
328
                *success = FALSE;
 
329
 
 
330
                mutex_exit(&(log->mutex));
 
331
 
 
332
                return(0);
 
333
        }
 
334
 
 
335
        *start_lsn = log->lsn;
 
336
 
 
337
        ut_memcpy(log->buf + log->buf_free, str, len);
 
338
 
 
339
        log_block_set_data_len((byte*) ut_align_down(log->buf + log->buf_free,
 
340
                                                     OS_FILE_LOG_BLOCK_SIZE),
 
341
                               data_len);
 
342
#ifdef UNIV_LOG_DEBUG
 
343
        log->old_buf_free = log->buf_free;
 
344
        log->old_lsn = log->lsn;
 
345
#endif
 
346
        log->buf_free += len;
 
347
 
 
348
        ut_ad(log->buf_free <= log->buf_size);
 
349
 
 
350
        lsn = log->lsn += len;
 
351
 
 
352
#ifdef UNIV_LOG_DEBUG
 
353
        log_check_log_recs(log->buf + log->old_buf_free,
 
354
                           log->buf_free - log->old_buf_free, log->old_lsn);
 
355
#endif
 
356
        return(lsn);
 
357
}
 
358
 
 
359
/***********************************************************************//**
 
360
Releases the log mutex. */
 
361
UNIV_INLINE
 
362
void
 
363
log_release(void)
 
364
/*=============*/
 
365
{
 
366
        mutex_exit(&(log_sys->mutex));
 
367
}
 
368
 
 
369
/************************************************************//**
 
370
Gets the current lsn.
 
371
@return current lsn */
 
372
UNIV_INLINE
 
373
ib_uint64_t
 
374
log_get_lsn(void)
 
375
/*=============*/
 
376
{
 
377
        ib_uint64_t     lsn;
 
378
 
 
379
        mutex_enter(&(log_sys->mutex));
 
380
 
 
381
        lsn = log_sys->lsn;
 
382
 
 
383
        mutex_exit(&(log_sys->mutex));
 
384
 
 
385
        return(lsn);
 
386
}
 
387
 
 
388
/****************************************************************
 
389
Gets the log group capacity. It is OK to read the value without
 
390
holding log_sys->mutex because it is constant.
 
391
@return log group capacity */
 
392
UNIV_INLINE
 
393
ulint
 
394
log_get_capacity(void)
 
395
/*==================*/
 
396
{
 
397
        return(log_sys->log_group_capacity);
 
398
}
 
399
 
 
400
/***********************************************************************//**
 
401
Checks if there is need for a log buffer flush or a new checkpoint, and does
 
402
this if yes. Any database operation should call this when it has modified
 
403
more than about 4 pages. NOTE that this function may only be called when the
 
404
OS thread owns no synchronization objects except the dictionary mutex. */
 
405
UNIV_INLINE
 
406
void
 
407
log_free_check(void)
 
408
/*================*/
 
409
{
 
410
        /* ut_ad(sync_thread_levels_empty()); */
 
411
 
 
412
        if (log_sys->check_flush_or_checkpoint) {
 
413
 
 
414
                log_check_margins();
 
415
        }
 
416
}
 
417
#endif /* !UNIV_HOTBACKUP */