2009-11-18 03:31:40 +01:00
|
|
|
/* QQ: TODO multi-pinbox */
|
2011-06-30 17:46:53 +02:00
|
|
|
/* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2011-06-30 17:46:53 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
wait-free concurrent allocator based on pinning addresses
|
|
|
|
|
|
|
|
It works as follows: every thread (strictly speaking - every CPU, but
|
|
|
|
it's too difficult to do) has a small array of pointers. They're called
|
|
|
|
"pins". Before using an object its address must be stored in this array
|
|
|
|
(pinned). When an object is no longer necessary its address must be
|
|
|
|
removed from this array (unpinned). When a thread wants to free() an
|
|
|
|
object it scans all pins of all threads to see if somebody has this
|
|
|
|
object pinned. If yes - the object is not freed (but stored in a
|
|
|
|
"purgatory"). To reduce the cost of a single free() pins are not scanned
|
|
|
|
on every free() but only added to (thread-local) purgatory. On every
|
|
|
|
LF_PURGATORY_SIZE free() purgatory is scanned and all unpinned objects
|
|
|
|
are freed.
|
|
|
|
|
|
|
|
Pins are used to solve ABA problem. To use pins one must obey
|
|
|
|
a pinning protocol:
|
|
|
|
|
|
|
|
1. Let's assume that PTR is a shared pointer to an object. Shared means
|
|
|
|
that any thread may modify it anytime to point to a different object
|
|
|
|
and free the old object. Later the freed object may be potentially
|
|
|
|
allocated by another thread. If we're unlucky that other thread may
|
|
|
|
set PTR to point to this object again. This is ABA problem.
|
|
|
|
2. Create a local pointer LOCAL_PTR.
|
|
|
|
3. Pin the PTR in a loop:
|
|
|
|
do
|
|
|
|
{
|
|
|
|
LOCAL_PTR= PTR;
|
|
|
|
pin(PTR, PIN_NUMBER);
|
|
|
|
} while (LOCAL_PTR != PTR)
|
|
|
|
4. It is guaranteed that after the loop has ended, LOCAL_PTR
|
|
|
|
points to an object (or NULL, if PTR may be NULL), that
|
|
|
|
will never be freed. It is not guaranteed though
|
|
|
|
that LOCAL_PTR == PTR (as PTR can change any time)
|
|
|
|
5. When done working with the object, remove the pin:
|
|
|
|
unpin(PIN_NUMBER)
|
|
|
|
6. When copying pins (as in the list traversing loop:
|
|
|
|
pin(CUR, 1);
|
|
|
|
while ()
|
|
|
|
{
|
|
|
|
do // standard
|
|
|
|
{ // pinning
|
|
|
|
NEXT=CUR->next; // loop
|
|
|
|
pin(NEXT, 0); // see #3
|
|
|
|
} while (NEXT != CUR->next); // above
|
|
|
|
...
|
|
|
|
...
|
|
|
|
CUR=NEXT;
|
|
|
|
pin(CUR, 1); // copy pin[0] to pin[1]
|
|
|
|
}
|
|
|
|
which keeps CUR address constantly pinned), note than pins may be
|
|
|
|
copied only upwards (!!!), that is pin[N] to pin[M], M > N.
|
|
|
|
7. Don't keep the object pinned longer than necessary - the number of
|
|
|
|
pins you have is limited (and small), keeping an object pinned
|
|
|
|
prevents its reuse and cause unnecessary mallocs.
|
|
|
|
|
|
|
|
Explanations:
|
|
|
|
|
|
|
|
3. The loop is important. The following can occur:
|
|
|
|
thread1> LOCAL_PTR= PTR
|
|
|
|
thread2> free(PTR); PTR=0;
|
|
|
|
thread1> pin(PTR, PIN_NUMBER);
|
|
|
|
now thread1 cannot access LOCAL_PTR, even if it's pinned,
|
|
|
|
because it points to a freed memory. That is, it *must*
|
|
|
|
verify that it has indeed pinned PTR, the shared pointer.
|
|
|
|
|
|
|
|
6. When a thread wants to free some LOCAL_PTR, and it scans
|
|
|
|
all lists of pins to see whether it's pinned, it does it
|
|
|
|
upwards, from low pin numbers to high. Thus another thread
|
|
|
|
must copy an address from one pin to another in the same
|
|
|
|
direction - upwards, otherwise the scanning thread may
|
|
|
|
miss it.
|
|
|
|
|
|
|
|
Implementation details:
|
|
|
|
|
|
|
|
Pins are given away from a "pinbox". Pinbox is stack-based allocator.
|
|
|
|
It used dynarray for storing pins, new elements are allocated by dynarray
|
|
|
|
as necessary, old are pushed in the stack for reuse. ABA is solved by
|
|
|
|
versioning a pointer - because we use an array, a pointer to pins is 16 bit,
|
|
|
|
upper 16 bits are used for a version.
|
|
|
|
|
|
|
|
It is assumed that pins belong to a THD and are not transferable
|
|
|
|
between THD's (LF_PINS::stack_ends_here being a primary reason
|
|
|
|
for this limitation).
|
|
|
|
*/
|
|
|
|
#include <my_global.h>
|
|
|
|
#include <my_sys.h>
|
|
|
|
#include <lf.h>
|
|
|
|
|
2016-03-17 21:29:52 +01:00
|
|
|
/*
|
|
|
|
when using alloca() leave at least that many bytes of the stack -
|
|
|
|
for functions we might be calling from within this stack frame
|
|
|
|
*/
|
|
|
|
#define ALLOCA_SAFETY_MARGIN 8192
|
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
#define LF_PINBOX_MAX_PINS 65536
|
|
|
|
|
2015-01-12 17:03:45 +01:00
|
|
|
static void lf_pinbox_real_free(LF_PINS *pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Initialize a pinbox. Normally called from lf_alloc_init.
|
|
|
|
See the latter for details.
|
|
|
|
*/
|
|
|
|
void lf_pinbox_init(LF_PINBOX *pinbox, uint free_ptr_offset,
|
|
|
|
lf_pinbox_free_func *free_func, void *free_func_arg)
|
|
|
|
{
|
|
|
|
DBUG_ASSERT(free_ptr_offset % sizeof(void *) == 0);
|
This patch is a collection of patches from from Sanja, Sergei and Monty.
Added logging and pinning of pages to block format.
Integration of transaction manager, log handler.
Better page cache intergration
Split trnman.h into two files, so that we don't have to include my_atomic.h into C++ programs.
Renaming of structures, more comments, more debugging etc.
Fixed problem with small head block + long varchar.
Added extra argument to delete_record() and update_record() (needed for UNDO logging)
Small changes to interface of pagecache and log handler.
Change initialization of log_record_type_descriptors to not be depending on enum order.
Use array of LEX_STRING's to send data to log handler
Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists.
include/lf.h:
Interface fixes
Rename of structures
(Patch from Sergei via Sanja)
include/my_atomic.h:
More comments
include/my_global.h:
Added MY_ERRPTR
include/pagecache.h:
Added undo LSN when unlocking pages
mysql-test/r/maria.result:
Updated results
mysql-test/t/maria.test:
Added autocommit around lock tables
(Patch from Sanja)
mysys/lf_alloc-pin.c:
Post-review fixes, simple optimizations
More comments
Struct slot renames
Check amount of memory on stack
(Patch from Sergei)
mysys/lf_dynarray.c:
More comments
mysys/lf_hash.c:
More comments
After review fixes
(Patch from Sergei)
storage/maria/ha_maria.cc:
Split trnman.h into two files, so that we don't have to include my_atomic.h into the .cc program.
(Temporary fix to avoid bug in gcc)
Move out all deferencing of the transaction structure.
Transaction manager integrated (Patch from Sergei)
storage/maria/ha_maria.h:
Added prototype for start_stmt()
storage/maria/lockman.c:
Function call rename
storage/maria/ma_bitmap.c:
Mark deleted pages free from page cache
storage/maria/ma_blockrec.c:
Offset -> rownr
More debugging
Fixed problem with small head block + long varchar
Added logging of changed pages
Added logging of undo (Including only loggging of changed fields in case of update)
Added pinning/unpinning of all changed pages
More comments
Added free_full_pages() as the same code was used in several places.
fill_rows_parts() renamed as fill_insert_undo_parts()
offset -> rownr
Added some optimization of not transactional tables
_ma_update_block_record() has new parameter, as we need original row to do efficent undo for update
storage/maria/ma_blockrec.h:
Added ROW_EXTENTS_ON_STACK
Changed prototype for update and delete of row
storage/maria/ma_check.c:
Added original row to delete_record() call
storage/maria/ma_control_file.h:
Added ifdefs for C++
storage/maria/ma_delete.c:
Added original row to delete_record() call
(Needed for efficent undo logging)
storage/maria/ma_dynrec.c:
Added extra argument to delete_record() and update_record()
Removed not used variable
storage/maria/ma_init.c:
Initialize log handler
storage/maria/ma_loghandler.c:
Removed not used variable
Change initialization of log_record_type_descriptors to not be depending on enum order
Use array of LEX_STRING's to send data to log handler
storage/maria/ma_loghandler.h:
New defines
Use array of LEX_STRING's to send data to log handler
storage/maria/ma_open.c:
Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists.
Store in MARIA_SHARE->page_type if pages will have up to date LSN's
storage/maria/ma_pagecache.c:
Don't decrease number of readers when using pagecache_write()/pagecache_read()
In pagecache_write() decrement request count if page was left pinned
Added pagecache_delete_pages()
Removed some casts
Make trace output consistent with rest of code
Simplify calling of DBUG_ASSERT(0)
Only update LSN if the LSN is bigger than what's already on the page
Added LSN parameter pagecache_unpin_page(), pagecache_unpin(), and pagecache_unlock()
(Part of patch from Sanja)
storage/maria/ma_static.c:
Added 'dummy' transaction option to MARIA_INFO so that we can always assume 'trn' exists.
Added default page cache
storage/maria/ma_statrec.c:
Added extra argument to delete_record() and update_record()
storage/maria/ma_test1.c:
Added option -T for transactions
storage/maria/ma_test2.c:
Added option -T for transactions
storage/maria/ma_test_all.sh:
Test with transactions
storage/maria/ma_update.c:
Changed prototype for update of row
storage/maria/maria_def.h:
Changed prototype for update & delete of row as block records need to access the old row
Store in MARIA_SHARE->page_type if pages will have up to date LSN's
Added MARIA_MAX_TREE_LEVELS to allow us to calculate the number of possible pinned pages we may need.
Removed not used 'empty_bits_buffer'
Added pointer to transaction object
Added array for pinned pages
Added log_row_parts array for logging of field data.
Added MARIA_PINNED_PAGE to store pinned pages
storage/maria/trnman.c:
Added accessor functions to transaction object
Added missing DBUG_RETURN()
More debugging
More comments
Changed // comment of code to #ifdef NOT_USED
Transaction manager integrated.
Post review fixes
Part of patch originally from Sergei
storage/maria/trnman.h:
Split trnman.h into two files, so that we don't have to include my_atomic.h into the .cc program.
(Temporary fix to avoid bug in gcc)
storage/maria/unittest/ma_pagecache_single.c:
Added missing argument
Added SKIP_BIG_TESTS
(Patch from Sanja)
storage/maria/unittest/ma_test_loghandler-t.c:
Test logging with new LEX_STRING parameter
(Patch from Sanja)
storage/maria/unittest/ma_test_loghandler_multigroup-t.c:
Test logging with new LEX_STRING parameter
(Patch from Sanja)
storage/maria/unittest/ma_test_loghandler_multithread-t.c:
Test logging with new LEX_STRING parameter
(Patch from Sanja)
storage/maria/unittest/ma_test_loghandler_pagecache-t.c:
Test logging with new LEX_STRING parameter
(Patch from Sanja)
storage/maria/unittest/trnman-t.c:
Stack overflow detection
(Patch from Sergei)
unittest/unit.pl:
Command-line options --big and --verbose
(Patch from Sergei)
unittest/mytap/tap.c:
Detect --big
(Patch from Sergei)
unittest/mytap/tap.h:
Skip_big_tests and SKIP_BIG_TESTS
(Patch from Sergei)
storage/maria/trnman_public.h:
New BitKeeper file ``storage/maria/trnman_public.h''
2007-05-29 19:13:56 +02:00
|
|
|
compile_time_assert(sizeof(LF_PINS) == 128);
|
2009-11-18 03:31:40 +01:00
|
|
|
lf_dynarray_init(&pinbox->pinarray, sizeof(LF_PINS));
|
|
|
|
pinbox->pinstack_top_ver= 0;
|
|
|
|
pinbox->pins_in_array= 0;
|
|
|
|
pinbox->free_ptr_offset= free_ptr_offset;
|
|
|
|
pinbox->free_func= free_func;
|
|
|
|
pinbox->free_func_arg= free_func_arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lf_pinbox_destroy(LF_PINBOX *pinbox)
|
|
|
|
{
|
|
|
|
lf_dynarray_destroy(&pinbox->pinarray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Get pins from a pinbox. Usually called via lf_alloc_get_pins() or
|
|
|
|
lf_hash_get_pins().
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
pinbox -
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
get a new LF_PINS structure from a stack of unused pins,
|
|
|
|
or allocate a new one out of dynarray.
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
It is assumed that pins belong to a thread and are not transferable
|
|
|
|
between threads.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
LF_PINS *lf_pinbox_get_pins(LF_PINBOX *pinbox)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
struct st_my_thread_var *var;
|
2009-11-18 03:31:40 +01:00
|
|
|
uint32 pins, next, top_ver;
|
|
|
|
LF_PINS *el;
|
|
|
|
/*
|
|
|
|
We have an array of max. 64k elements.
|
|
|
|
The highest index currently allocated is pinbox->pins_in_array.
|
|
|
|
Freed elements are in a lifo stack, pinstack_top_ver.
|
|
|
|
pinstack_top_ver is 32 bits; 16 low bits are the index in the
|
|
|
|
array, to the first element of the list. 16 high bits are a version
|
|
|
|
(every time the 16 low bits are updated, the 16 high bits are
|
|
|
|
incremented). Versioniong prevents the ABA problem.
|
|
|
|
*/
|
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(pins= top_ver % LF_PINBOX_MAX_PINS))
|
|
|
|
{
|
|
|
|
/* the stack of free elements is empty */
|
|
|
|
pins= my_atomic_add32((int32 volatile*) &pinbox->pins_in_array, 1)+1;
|
|
|
|
if (unlikely(pins >= LF_PINBOX_MAX_PINS))
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
note that the first allocated element has index 1 (pins==1).
|
|
|
|
index 0 is reserved to mean "NULL pointer"
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
el= (LF_PINS *)lf_dynarray_lvalue(&pinbox->pinarray, pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
if (unlikely(!el))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-12 17:03:45 +01:00
|
|
|
el= (LF_PINS *)lf_dynarray_value(&pinbox->pinarray, pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
next= el->link;
|
|
|
|
} while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
|
|
|
|
(int32*) &top_ver,
|
|
|
|
top_ver-pins+next+LF_PINBOX_MAX_PINS));
|
|
|
|
/*
|
|
|
|
set el->link to the index of el in the dynarray (el->link has two usages:
|
|
|
|
- if element is allocated, it's its own index
|
|
|
|
- if element is free, it's its next element in the free stack
|
|
|
|
*/
|
|
|
|
el->link= pins;
|
|
|
|
el->purgatory_count= 0;
|
|
|
|
el->pinbox= pinbox;
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
var= my_thread_var;
|
|
|
|
/*
|
|
|
|
Threads that do not call my_thread_init() should still be
|
|
|
|
able to use the LF_HASH.
|
|
|
|
*/
|
|
|
|
el->stack_ends_here= (var ? & var->stack_ends_here : NULL);
|
2009-11-18 03:31:40 +01:00
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Put pins back to a pinbox. Usually called via lf_alloc_put_pins() or
|
|
|
|
lf_hash_put_pins().
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
empty the purgatory (XXX deadlock warning below!),
|
|
|
|
push LF_PINS structure to a stack
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void lf_pinbox_put_pins(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
|
|
|
uint32 top_ver, nr;
|
|
|
|
nr= pins->link;
|
2012-03-28 17:54:30 +02:00
|
|
|
|
|
|
|
#ifndef DBUG_OFF
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2012-03-28 17:54:30 +02:00
|
|
|
/* This thread should not hold any pin. */
|
2009-11-18 03:31:40 +01:00
|
|
|
int i;
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
DBUG_ASSERT(pins->pin[i] == 0);
|
|
|
|
}
|
2012-03-28 17:54:30 +02:00
|
|
|
#endif /* DBUG_OFF */
|
|
|
|
|
2009-11-18 03:31:40 +01:00
|
|
|
/*
|
|
|
|
XXX this will deadlock if other threads will wait for
|
2015-01-12 17:03:45 +01:00
|
|
|
the caller to do something after lf_pinbox_put_pins(),
|
2009-11-18 03:31:40 +01:00
|
|
|
and they would have pinned addresses that the caller wants to free.
|
|
|
|
Thus: only free pins when all work is done and nobody can wait for you!!!
|
|
|
|
*/
|
|
|
|
while (pins->purgatory_count)
|
|
|
|
{
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pinbox_real_free(pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
if (pins->purgatory_count)
|
|
|
|
pthread_yield();
|
|
|
|
}
|
|
|
|
top_ver= pinbox->pinstack_top_ver;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
pins->link= top_ver % LF_PINBOX_MAX_PINS;
|
|
|
|
} while (!my_atomic_cas32((int32 volatile*) &pinbox->pinstack_top_ver,
|
|
|
|
(int32*) &top_ver,
|
|
|
|
top_ver-pins->link+nr+LF_PINBOX_MAX_PINS));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ptr_cmp(void **a, void **b)
|
|
|
|
{
|
|
|
|
return *a < *b ? -1 : *a == *b ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define add_to_purgatory(PINS, ADDR) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
*(void **)((char *)(ADDR)+(PINS)->pinbox->free_ptr_offset)= \
|
|
|
|
(PINS)->purgatory; \
|
|
|
|
(PINS)->purgatory= (ADDR); \
|
|
|
|
(PINS)->purgatory_count++; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Free an object allocated via pinbox allocator
|
|
|
|
|
|
|
|
DESCRIPTION
|
2015-01-12 17:03:45 +01:00
|
|
|
add an object to purgatory. if necessary, calllf_pinbox_real_free()
|
2009-11-18 03:31:40 +01:00
|
|
|
to actually free something.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void lf_pinbox_free(LF_PINS *pins, void *addr)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
add_to_purgatory(pins, addr);
|
2012-09-04 12:12:28 +02:00
|
|
|
if (pins->purgatory_count % LF_PURGATORY_SIZE == 0)
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pinbox_real_free(pins);
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
struct st_harvester {
|
|
|
|
void **granary;
|
|
|
|
int npins;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2015-01-12 17:03:45 +01:00
|
|
|
callback forlf_dynarray_iterate:
|
2009-11-18 03:31:40 +01:00
|
|
|
scan all pins of all threads and accumulate all pins
|
|
|
|
*/
|
|
|
|
static int harvest_pins(LF_PINS *el, struct st_harvester *hv)
|
|
|
|
{
|
|
|
|
int i;
|
2013-03-25 23:03:13 +01:00
|
|
|
LF_PINS *el_end= el+MY_MIN(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
|
2009-11-18 03:31:40 +01:00
|
|
|
for (; el < el_end; el++)
|
|
|
|
{
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
{
|
|
|
|
void *p= el->pin[i];
|
|
|
|
if (p)
|
|
|
|
*hv->granary++= p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
hv->npins may become negative below, but it means that
|
|
|
|
we're on the last dynarray page and harvest_pins() won't be
|
|
|
|
called again. We don't bother to make hv->npins() correct
|
|
|
|
(that is 0) in this case.
|
|
|
|
*/
|
|
|
|
hv->npins-= LF_DYNARRAY_LEVEL_LENGTH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-12 17:03:45 +01:00
|
|
|
callback forlf_dynarray_iterate:
|
2009-11-18 03:31:40 +01:00
|
|
|
scan all pins of all threads and see if addr is present there
|
|
|
|
*/
|
|
|
|
static int match_pins(LF_PINS *el, void *addr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
LF_PINS *el_end= el+LF_DYNARRAY_LEVEL_LENGTH;
|
|
|
|
for (; el < el_end; el++)
|
|
|
|
for (i= 0; i < LF_PINBOX_PINS; i++)
|
|
|
|
if (el->pin[i] == addr)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if STACK_DIRECTION < 0
|
|
|
|
#define available_stack_size(CUR,END) (long) ((char*)(CUR) - (char*)(END))
|
|
|
|
#else
|
|
|
|
#define available_stack_size(CUR,END) (long) ((char*)(END) - (char*)(CUR))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define next_node(P, X) (*((uchar * volatile *)(((uchar *)(X)) + (P)->free_ptr_offset)))
|
|
|
|
#define anext_node(X) next_node(&allocator->pinbox, (X))
|
|
|
|
|
|
|
|
/*
|
|
|
|
Scan the purgatory and free everything that can be freed
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
static void lf_pinbox_real_free(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
int npins;
|
|
|
|
void *list;
|
|
|
|
void **addr= NULL;
|
2010-05-26 16:12:23 +02:00
|
|
|
void *first= NULL, *last= NULL;
|
2009-11-18 03:31:40 +01:00
|
|
|
LF_PINBOX *pinbox= pins->pinbox;
|
|
|
|
|
|
|
|
npins= pinbox->pins_in_array+1;
|
|
|
|
|
|
|
|
#ifdef HAVE_ALLOCA
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
if (pins->stack_ends_here != NULL)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
2011-05-16 22:47:59 +02:00
|
|
|
int alloca_size= sizeof(void *)*LF_PINBOX_PINS*npins;
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
/* create a sorted list of pinned addresses, to speed up searches */
|
2016-03-17 21:29:52 +01:00
|
|
|
if (available_stack_size(&pinbox, *pins->stack_ends_here) >
|
|
|
|
alloca_size + ALLOCA_SAFETY_MARGIN)
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
{
|
|
|
|
struct st_harvester hv;
|
|
|
|
addr= (void **) alloca(alloca_size);
|
|
|
|
hv.granary= addr;
|
|
|
|
hv.npins= npins;
|
|
|
|
/* scan the dynarray and accumulate all pinned addresses */
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_dynarray_iterate(&pinbox->pinarray,
|
Bug#12552516 LF_HASH REQUIRES MY_THREAD_INIT()
Before this fix, a thread instrumented for the performance schema,
that would perform file io operations, could crash inside the LF_HASH
implementation, in cases when my_thread_init is not called.
The crash itself has not been reported in 5.5 but similar crashes have
been found in 5.6-based development branches, using LF_HASH for
more instrumentation.
The possibility of a crash in 5.5 is confirmed by code analysis.
The problem is that, when my_thread_init() is not called,
which can happen for threads in storage engines or thirs party code,
my_thread_var is NULL.
Using my_thread_var->stacks_ends_here in mysys/lf_alloc-pin.c is unsafe.
Given that my_thread_var is used:
- only for stacks_ends_here
- only on platform with HAVE_ALLOCA
- only when there is enough room on the stack
and given that the LF_HASH implementation has a fallback
algorythm implemented already when using alloca is not possible,
using my_thread_var->stacks_ends_here is in fact not a strict requirement,
and can be relaxed.
The fix is to:
- test explicitly if my_thread_var is NULL, to account for cases
when my_thread_init() is not used by the calling thread.
- not use alloca in this case, and rely on the fall back code already in place.
so that the LF_HASH can be supported even without my_thread_init().
The implementation of mysys/lf_alloc-pin.c has been fixed to support this new usage.
The units tests in unittest/mysys/lf-t.c have been adjusted accordingly.
2011-05-13 18:04:49 +02:00
|
|
|
(lf_dynarray_func)harvest_pins, &hv);
|
|
|
|
|
|
|
|
npins= hv.granary-addr;
|
|
|
|
/* and sort them */
|
|
|
|
if (npins)
|
|
|
|
qsort(addr, npins, sizeof(void *), (qsort_cmp)ptr_cmp);
|
|
|
|
}
|
2009-11-18 03:31:40 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
list= pins->purgatory;
|
|
|
|
pins->purgatory= 0;
|
|
|
|
pins->purgatory_count= 0;
|
|
|
|
while (list)
|
|
|
|
{
|
|
|
|
void *cur= list;
|
|
|
|
list= *(void **)((char *)cur+pinbox->free_ptr_offset);
|
|
|
|
if (npins)
|
|
|
|
{
|
|
|
|
if (addr) /* use binary search */
|
|
|
|
{
|
|
|
|
void **a, **b, **c;
|
|
|
|
for (a= addr, b= addr+npins-1, c= a+(b-a)/2; (b-a) > 1; c= a+(b-a)/2)
|
|
|
|
if (cur == *c)
|
|
|
|
a= b= c;
|
|
|
|
else if (cur > *c)
|
|
|
|
a= c;
|
|
|
|
else
|
|
|
|
b= c;
|
|
|
|
if (cur == *a || cur == *b)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
else /* no alloca - no cookie. linear search here */
|
|
|
|
{
|
2015-01-12 17:03:45 +01:00
|
|
|
if (lf_dynarray_iterate(&pinbox->pinarray,
|
2009-11-18 03:31:40 +01:00
|
|
|
(lf_dynarray_func)match_pins, cur))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not pinned - freeing */
|
|
|
|
if (last)
|
|
|
|
last= next_node(pinbox, last)= (uchar *)cur;
|
|
|
|
else
|
|
|
|
first= last= (uchar *)cur;
|
|
|
|
continue;
|
|
|
|
found:
|
|
|
|
/* pinned - keeping */
|
|
|
|
add_to_purgatory(pins, cur);
|
|
|
|
}
|
|
|
|
if (last)
|
|
|
|
pinbox->free_func(first, last, pinbox->free_func_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lock-free memory allocator for fixed-size objects */
|
|
|
|
|
|
|
|
/*
|
2015-01-12 17:03:45 +01:00
|
|
|
callback forlf_pinbox_real_free to free a list of unpinned objects -
|
2009-11-18 03:31:40 +01:00
|
|
|
add it back to the allocator stack
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
'first' and 'last' are the ends of the linked list of nodes:
|
|
|
|
first->el->el->....->el->last. Use first==last to free only one element.
|
|
|
|
*/
|
|
|
|
static void alloc_free(uchar *first,
|
|
|
|
uchar volatile *last,
|
|
|
|
LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
we need a union here to access type-punned pointer reliably.
|
|
|
|
otherwise gcc -fstrict-aliasing will not see 'tmp' changed in the loop
|
|
|
|
*/
|
|
|
|
union { uchar * node; void *ptr; } tmp;
|
|
|
|
tmp.node= allocator->top;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
anext_node(last)= tmp.node;
|
|
|
|
} while (!my_atomic_casptr((void **)(char *)&allocator->top,
|
|
|
|
(void **)&tmp.ptr, first) && LF_BACKOFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
initialize lock-free allocator
|
|
|
|
|
|
|
|
SYNOPSYS
|
|
|
|
allocator -
|
|
|
|
size a size of an object to allocate
|
|
|
|
free_ptr_offset an offset inside the object to a sizeof(void *)
|
|
|
|
memory that is guaranteed to be unused after
|
|
|
|
the object is put in the purgatory. Unused by ANY
|
|
|
|
thread, not only the purgatory owner.
|
|
|
|
This memory will be used to link waiting-to-be-freed
|
|
|
|
objects in a purgatory list.
|
|
|
|
*/
|
|
|
|
void lf_alloc_init(LF_ALLOCATOR *allocator, uint size, uint free_ptr_offset)
|
|
|
|
{
|
|
|
|
lf_pinbox_init(&allocator->pinbox, free_ptr_offset,
|
|
|
|
(lf_pinbox_free_func *)alloc_free, allocator);
|
|
|
|
allocator->top= 0;
|
|
|
|
allocator->mallocs= 0;
|
|
|
|
allocator->element_size= size;
|
2008-07-29 16:10:24 +02:00
|
|
|
allocator->constructor= 0;
|
|
|
|
allocator->destructor= 0;
|
2009-11-18 03:31:40 +01:00
|
|
|
DBUG_ASSERT(size >= sizeof(void*) + free_ptr_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
destroy the allocator, free everything that's in it
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
As every other init/destroy function here and elsewhere it
|
|
|
|
is not thread safe. No, this function is no different, ensure
|
|
|
|
that no thread needs the allocator before destroying it.
|
|
|
|
We are not responsible for any damage that may be caused by
|
|
|
|
accessing the allocator when it is being or has been destroyed.
|
|
|
|
Oh yes, and don't put your cat in a microwave.
|
|
|
|
*/
|
|
|
|
void lf_alloc_destroy(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
uchar *node= allocator->top;
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
uchar *tmp= anext_node(node);
|
2008-07-29 16:10:24 +02:00
|
|
|
if (allocator->destructor)
|
|
|
|
allocator->destructor(node);
|
Bug#34043: Server loops excessively in _checkchunk() when safemalloc is enabled
Essentially, the problem is that safemalloc is excruciatingly
slow as it checks all allocated blocks for overrun at each
memory management primitive, yielding a almost exponential
slowdown for the memory management functions (malloc, realloc,
free). The overrun check basically consists of verifying some
bytes of a block for certain magic keys, which catches some
simple forms of overrun. Another minor problem is violation
of aliasing rules and that its own internal list of blocks
is prone to corruption.
Another issue with safemalloc is rather the maintenance cost
as the tool has a significant impact on the server code.
Given the magnitude of memory debuggers available nowadays,
especially those that are provided with the platform malloc
implementation, maintenance of a in-house and largely obsolete
memory debugger becomes a burden that is not worth the effort
due to its slowness and lack of support for detecting more
common forms of heap corruption.
Since there are third-party tools that can provide the same
functionality at a lower or comparable performance cost, the
solution is to simply remove safemalloc. Third-party tools
can provide the same functionality at a lower or comparable
performance cost.
The removal of safemalloc also allows a simplification of the
malloc wrappers, removing quite a bit of kludge: redefinition
of my_malloc, my_free and the removal of the unused second
argument of my_free. Since free() always check whether the
supplied pointer is null, redudant checks are also removed.
Also, this patch adds unit testing for my_malloc and moves
my_realloc implementation into the same file as the other
memory allocation primitives.
client/mysqldump.c:
Pass my_free directly as its signature is compatible with the
callback type -- which wasn't the case for free_table_ent.
2010-07-08 23:20:08 +02:00
|
|
|
my_free(node);
|
2009-11-18 03:31:40 +01:00
|
|
|
node= tmp;
|
|
|
|
}
|
|
|
|
lf_pinbox_destroy(&allocator->pinbox);
|
|
|
|
allocator->top= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Allocate and return an new object.
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Pop an unused object from the stack or malloc it is the stack is empty.
|
|
|
|
pin[0] is used, it's removed on return.
|
|
|
|
*/
|
2015-01-12 17:03:45 +01:00
|
|
|
void *lf_alloc_new(LF_PINS *pins)
|
2009-11-18 03:31:40 +01:00
|
|
|
{
|
|
|
|
LF_ALLOCATOR *allocator= (LF_ALLOCATOR *)(pins->pinbox->free_func_arg);
|
|
|
|
uchar *node;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
node= allocator->top;
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_pin(pins, 0, node);
|
2009-11-18 03:31:40 +01:00
|
|
|
} while (node != allocator->top && LF_BACKOFF);
|
|
|
|
if (!node)
|
|
|
|
{
|
|
|
|
node= (void *)my_malloc(allocator->element_size, MYF(MY_WME));
|
2008-07-29 16:10:24 +02:00
|
|
|
if (allocator->constructor)
|
|
|
|
allocator->constructor(node);
|
2009-11-18 03:31:40 +01:00
|
|
|
#ifdef MY_LF_EXTRA_DEBUG
|
|
|
|
if (likely(node != 0))
|
|
|
|
my_atomic_add32(&allocator->mallocs, 1);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (my_atomic_casptr((void **)(char *)&allocator->top,
|
|
|
|
(void *)&node, anext_node(node)))
|
|
|
|
break;
|
|
|
|
}
|
2015-01-12 17:03:45 +01:00
|
|
|
lf_unpin(pins, 0);
|
2009-11-18 03:31:40 +01:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
count the number of objects in a pool.
|
|
|
|
|
|
|
|
NOTE
|
|
|
|
This is NOT thread-safe !!!
|
|
|
|
*/
|
|
|
|
uint lf_alloc_pool_count(LF_ALLOCATOR *allocator)
|
|
|
|
{
|
|
|
|
uint i;
|
|
|
|
uchar *node;
|
|
|
|
for (node= allocator->top, i= 0; node; node= anext_node(node), i++)
|
|
|
|
/* no op */;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|