Commit 45109713 authored by unknown's avatar unknown

Merge bk-internal.mysql.com:/home/bk/mysql-maria

into  janus.mylan:/usr/home/serg/Abk/mysql-maria

parents e39aae7c aad040fd
...@@ -71,13 +71,15 @@ void lf_dynarray_destroy(LF_DYNARRAY *array) ...@@ -71,13 +71,15 @@ void lf_dynarray_destroy(LF_DYNARRAY *array)
bzero(array, sizeof(*array)); bzero(array, sizeof(*array));
} }
static const int dynarray_idxes_in_prev_level[LF_DYNARRAY_LEVELS]= static const long dynarray_idxes_in_prev_level[LF_DYNARRAY_LEVELS]=
{ {
0, /* +1 here to to avoid -1's below */ 0, /* +1 here to to avoid -1's below */
LF_DYNARRAY_LEVEL_LENGTH, LF_DYNARRAY_LEVEL_LENGTH,
LF_DYNARRAY_LEVEL_LENGTH * LF_DYNARRAY_LEVEL_LENGTH, LF_DYNARRAY_LEVEL_LENGTH * LF_DYNARRAY_LEVEL_LENGTH +
LF_DYNARRAY_LEVEL_LENGTH,
LF_DYNARRAY_LEVEL_LENGTH * LF_DYNARRAY_LEVEL_LENGTH * LF_DYNARRAY_LEVEL_LENGTH * LF_DYNARRAY_LEVEL_LENGTH *
LF_DYNARRAY_LEVEL_LENGTH LF_DYNARRAY_LEVEL_LENGTH + LF_DYNARRAY_LEVEL_LENGTH *
LF_DYNARRAY_LEVEL_LENGTH + LF_DYNARRAY_LEVEL_LENGTH
}; };
/* /*
......
...@@ -53,7 +53,7 @@ maria_pack_LDADD= @CLIENT_EXTRA_LDFLAGS@ libmaria.a \ ...@@ -53,7 +53,7 @@ maria_pack_LDADD= @CLIENT_EXTRA_LDFLAGS@ libmaria.a \
noinst_PROGRAMS = ma_test1 ma_test2 ma_test3 ma_rt_test ma_sp_test noinst_PROGRAMS = ma_test1 ma_test2 ma_test3 ma_rt_test ma_sp_test
noinst_HEADERS = maria_def.h ma_rt_index.h ma_rt_key.h ma_rt_mbr.h \ noinst_HEADERS = maria_def.h ma_rt_index.h ma_rt_key.h ma_rt_mbr.h \
ma_sp_defs.h ma_fulltext.h ma_ftdefs.h ma_ft_test1.h \ ma_sp_defs.h ma_fulltext.h ma_ftdefs.h ma_ft_test1.h \
ma_ft_eval.h trnman.h lockman.h \ ma_ft_eval.h trnman.h lockman.h tablockman.h \
ma_control_file.h ha_maria.h ma_control_file.h ha_maria.h
ma_test1_DEPENDENCIES= $(LIBRARIES) ma_test1_DEPENDENCIES= $(LIBRARIES)
ma_test1_LDADD= @CLIENT_EXTRA_LDFLAGS@ libmaria.a \ ma_test1_LDADD= @CLIENT_EXTRA_LDFLAGS@ libmaria.a \
...@@ -108,7 +108,7 @@ libmaria_a_SOURCES = ma_init.c ma_open.c ma_extra.c ma_info.c ma_rkey.c \ ...@@ -108,7 +108,7 @@ libmaria_a_SOURCES = ma_init.c ma_open.c ma_extra.c ma_info.c ma_rkey.c \
ma_keycache.c ma_preload.c ma_ft_parser.c \ ma_keycache.c ma_preload.c ma_ft_parser.c \
ma_ft_update.c ma_ft_boolean_search.c \ ma_ft_update.c ma_ft_boolean_search.c \
ma_ft_nlq_search.c ft_maria.c ma_sort.c \ ma_ft_nlq_search.c ft_maria.c ma_sort.c \
ha_maria.cc trnman.c lockman.c \ ha_maria.cc trnman.c lockman.c tablockman.c \
ma_rt_index.c ma_rt_key.c ma_rt_mbr.c ma_rt_split.c \ ma_rt_index.c ma_rt_key.c ma_rt_mbr.c ma_rt_split.c \
ma_sp_key.c ma_control_file.c ma_sp_key.c ma_control_file.c
CLEANFILES = test?.MA? FT?.MA? isam.log ma_test_all ma_rt_test.MA? sp_test.MA? CLEANFILES = test?.MA? FT?.MA? isam.log ma_test_all ma_rt_test.MA? sp_test.MA?
......
// TODO - allocate everything from dynarrays !!! (benchmark)
// TODO instant duration locks // TODO instant duration locks
// automatically place S instead of LS if possible // automatically place S instead of LS if possible
/*
TODO optimization: table locks - they have completely
different characteristics. long lists, few distinct resources -
slow to scan, [possibly] high retry rate
*/
/* Copyright (C) 2006 MySQL AB /* Copyright (C) 2006 MySQL AB
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
...@@ -68,9 +64,9 @@ ...@@ -68,9 +64,9 @@
it will wait for other locks. Here's an exception to "locks are added it will wait for other locks. Here's an exception to "locks are added
to the end" rule - upgraded locks are added after the last active lock to the end" rule - upgraded locks are added after the last active lock
but before all waiting locks. Old lock (the one we upgraded from) is but before all waiting locks. Old lock (the one we upgraded from) is
not removed from the list, indeed we may need to return to it later if not removed from the list, indeed it may be needed if the new lock was
the new lock was in a savepoint that gets rolled back. So old lock is in a savepoint that gets rolled back. So old lock is marked as "ignored"
marked as "ignored" (IGNORE_ME flag). New lock gets an UPGRADED flag. (IGNORE_ME flag). New lock gets an UPGRADED flag.
Loose locks add an important exception to the above. Loose locks do not Loose locks add an important exception to the above. Loose locks do not
always commute with other locks. In the list IX-LS both locks are active, always commute with other locks. In the list IX-LS both locks are active,
...@@ -90,12 +86,12 @@ ...@@ -90,12 +86,12 @@
variable a conflicting lock is returned and the calling thread waits on a variable a conflicting lock is returned and the calling thread waits on a
pthread condition in the LOCK_OWNER structure of the owner of the pthread condition in the LOCK_OWNER structure of the owner of the
conflicting lock. Or a new lock is compatible with all locks, but some conflicting lock. Or a new lock is compatible with all locks, but some
existing locks are not compatible with previous locks (example: request IS, existing locks are not compatible with each other (example: request IS,
when the list is S-IX) - that is not all locks are active. In this case a when the list is S-IX) - that is not all locks are active. In this case a
first waiting lock is returned in the 'blocker' variable, first waiting lock is returned in the 'blocker' variable, lockman_getlock()
lockman_getlock() notices that a "blocker" does not conflict with the notices that a "blocker" does not conflict with the requested lock, and
requested lock, and "dereferences" it, to find the lock that it's waiting "dereferences" it, to find the lock that it's waiting on. The calling
on. The calling thread than begins to wait on the same lock. thread than begins to wait on the same lock.
To better support table-row relations where one needs to lock the table To better support table-row relations where one needs to lock the table
with an intention lock before locking the row, extended diagnostics is with an intention lock before locking the row, extended diagnostics is
...@@ -107,6 +103,10 @@ ...@@ -107,6 +103,10 @@
whether it's possible to lock the row, but no need to lock it - perhaps whether it's possible to lock the row, but no need to lock it - perhaps
the thread has a loose lock on this table). This is defined by the thread has a loose lock on this table). This is defined by
getlock_result[] table. getlock_result[] table.
TODO optimization: table locks - they have completely
different characteristics. long lists, few distinct resources -
slow to scan, [possibly] high retry rate
*/ */
#include <my_global.h> #include <my_global.h>
...@@ -316,7 +316,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node, ...@@ -316,7 +316,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node,
DBUG_ASSERT(prev_active == TRUE); DBUG_ASSERT(prev_active == TRUE);
else else
cur_active&= lock_compatibility_matrix[prev_lock][cur_lock]; cur_active&= lock_compatibility_matrix[prev_lock][cur_lock];
if (upgrading && !cur_active) if (upgrading && !cur_active /*&& !(cur_flags & UPGRADED)*/)
break; break;
if (prev_active && !cur_active) if (prev_active && !cur_active)
{ {
...@@ -327,7 +327,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node, ...@@ -327,7 +327,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node,
{ {
/* we already have a lock on this resource */ /* we already have a lock on this resource */
DBUG_ASSERT(lock_combining_matrix[cur_lock][lock] != N); DBUG_ASSERT(lock_combining_matrix[cur_lock][lock] != N);
DBUG_ASSERT(!upgrading); /* can happen only once */ DBUG_ASSERT(!upgrading || (flags & IGNORE_ME));
if (lock_combining_matrix[cur_lock][lock] == cur_lock) if (lock_combining_matrix[cur_lock][lock] == cur_lock)
{ {
/* new lock is compatible */ /* new lock is compatible */
...@@ -380,7 +380,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node, ...@@ -380,7 +380,7 @@ static int lockfind(LOCK * volatile *head, LOCK *node,
*/ */
if (upgrading) if (upgrading)
{ {
if (compatible) if (compatible /*&& prev_active*/)
return PLACE_NEW_DISABLE_OLD; return PLACE_NEW_DISABLE_OLD;
else else
return REQUEST_NEW_DISABLE_OLD; return REQUEST_NEW_DISABLE_OLD;
...@@ -431,6 +431,9 @@ static int lockinsert(LOCK * volatile *head, LOCK *node, LF_PINS *pins, ...@@ -431,6 +431,9 @@ static int lockinsert(LOCK * volatile *head, LOCK *node, LF_PINS *pins,
} }
if (res & LOCK_UPGRADE) if (res & LOCK_UPGRADE)
cursor.upgrade_from->flags|= IGNORE_ME; cursor.upgrade_from->flags|= IGNORE_ME;
#warning is this OK ? if a reader has already read upgrade_from, \
it may find it conflicting with node :(
//#error another bug - see the last test from test_lockman_simple()
} }
} while (res == REPEAT_ONCE_MORE); } while (res == REPEAT_ONCE_MORE);
...@@ -439,8 +442,8 @@ static int lockinsert(LOCK * volatile *head, LOCK *node, LF_PINS *pins, ...@@ -439,8 +442,8 @@ static int lockinsert(LOCK * volatile *head, LOCK *node, LF_PINS *pins,
_lf_unpin(pins, 2); _lf_unpin(pins, 2);
/* /*
note that blocker is not necessarily pinned here (when it's == curr). note that blocker is not necessarily pinned here (when it's == curr).
this is ok as it's either a dummy node then for initialize_bucket this is ok as in such a case it's either a dummy node for
and dummy nodes don't need pinning, initialize_bucket() and dummy nodes don't need pinning,
or it's a lock of the same transaction for lockman_getlock, or it's a lock of the same transaction for lockman_getlock,
and it cannot be removed by another thread and it cannot be removed by another thread
*/ */
...@@ -484,9 +487,15 @@ static int lockdelete(LOCK * volatile *head, LOCK *node, LF_PINS *pins) ...@@ -484,9 +487,15 @@ static int lockdelete(LOCK * volatile *head, LOCK *node, LF_PINS *pins)
res= lockfind(head, node, &cursor, pins); res= lockfind(head, node, &cursor, pins);
DBUG_ASSERT(res & ALREADY_HAVE); DBUG_ASSERT(res & ALREADY_HAVE);
if (cursor.upgrade_from) /*
cursor.upgrade_from->flags&= ~IGNORE_ME; XXX this does not work with savepoints, as old lock is left ignored.
It cannot be unignored, as would basically mean moving the lock back
in the lock chain (from upgraded). And the latter is not allowed -
because it breaks list scanning. So old ignored lock must be deleted,
new - same - lock must be installed right after the lock we're deleting,
then we can delete. Good news is - this is only required when rolling
back a savepoint.
*/
if (my_atomic_casptr((void **)&(cursor.curr->link), if (my_atomic_casptr((void **)&(cursor.curr->link),
(void **)&cursor.next, 1+(char *)cursor.next)) (void **)&cursor.next, 1+(char *)cursor.next))
{ {
...@@ -497,11 +506,7 @@ static int lockdelete(LOCK * volatile *head, LOCK *node, LF_PINS *pins) ...@@ -497,11 +506,7 @@ static int lockdelete(LOCK * volatile *head, LOCK *node, LF_PINS *pins)
lockfind(head, node, &cursor, pins); lockfind(head, node, &cursor, pins);
} }
else else
{
res= REPEAT_ONCE_MORE; res= REPEAT_ONCE_MORE;
if (cursor.upgrade_from) /* to satisfy the assert in lockfind */
cursor.upgrade_from->flags|= IGNORE_ME;
}
} while (res == REPEAT_ONCE_MORE); } while (res == REPEAT_ONCE_MORE);
_lf_unpin(pins, 0); _lf_unpin(pins, 0);
_lf_unpin(pins, 1); _lf_unpin(pins, 1);
......
This diff is collapsed.
/* Copyright (C) 2006 MySQL AB
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; either version 2 of the License, or
(at your option) any later version.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifndef _tablockman_h
#define _tablockman_h
/*
Lock levels:
^^^^^^^^^^^
N - "no lock", not a lock, used sometimes internally to simplify the code
S - Shared
X - eXclusive
IS - Intention Shared
IX - Intention eXclusive
SIX - Shared + Intention eXclusive
LS - Loose Shared
LX - Loose eXclusive
SLX - Shared + Loose eXclusive
LSIX - Loose Shared + Intention eXclusive
*/
#ifndef _lockman_h
enum lock_type { N, S, X, IS, IX, SIX, LS, LX, SLX, LSIX };
enum lockman_getlock_result {
DIDNT_GET_THE_LOCK=0, GOT_THE_LOCK,
GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE,
GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE
};
#endif
#define LOCK_TYPES LSIX
typedef struct st_table_lock_owner TABLE_LOCK_OWNER;
typedef struct st_table_lock TABLE_LOCK;
typedef struct st_locked_table LOCKED_TABLE;
typedef TABLE_LOCK_OWNER *loid_to_tlo_func(uint16);
typedef struct {
pthread_mutex_t pool_mutex;
TABLE_LOCK *pool; /* lifo pool of free locks */
uint lock_timeout;
loid_to_tlo_func *loid_to_tlo; /* for mapping loid to TABLE_LOCK_OWNER */
} TABLOCKMAN;
struct st_table_lock_owner {
TABLE_LOCK *active_locks; /* list of active locks */
TABLE_LOCK *waiting_lock; /* waiting lock (one lock only) */
TABLE_LOCK_OWNER *waiting_for; /* transaction we're wating for */
pthread_cond_t *cond; /* transactions waiting for us, wait on 'cond' */
pthread_mutex_t *mutex; /* mutex is required to use 'cond' */
uint16 loid; /* Lock Owner IDentifier */
};
struct st_locked_table {
pthread_mutex_t mutex; /* mutex for everything below */
HASH active; /* active locks ina hash */
TABLE_LOCK *active_locks[LOCK_TYPES]; /* dl-list of locks per type */
TABLE_LOCK *wait_queue_in, *wait_queue_out; /* wait deque */
};
void tablockman_init(TABLOCKMAN *, loid_to_tlo_func *, uint);
void tablockman_destroy(TABLOCKMAN *);
enum lockman_getlock_result tablockman_getlock(TABLOCKMAN *, TABLE_LOCK_OWNER *,
LOCKED_TABLE *, enum lock_type);
void tablockman_release_locks(TABLOCKMAN *, TABLE_LOCK_OWNER *);
void tablockman_init_locked_table(LOCKED_TABLE *, int);
void tablockman_destroy_locked_table(LOCKED_TABLE *);
#ifdef EXTRA_DEBUG
void print_tlo(TABLE_LOCK_OWNER *);
#endif
#endif
...@@ -25,5 +25,5 @@ LDADD= $(top_builddir)/unittest/mytap/libmytap.a \ ...@@ -25,5 +25,5 @@ LDADD= $(top_builddir)/unittest/mytap/libmytap.a \
$(top_builddir)/mysys/libmysys.a \ $(top_builddir)/mysys/libmysys.a \
$(top_builddir)/dbug/libdbug.a \ $(top_builddir)/dbug/libdbug.a \
$(top_builddir)/strings/libmystrings.a @ZLIB_LIBS@ $(top_builddir)/strings/libmystrings.a @ZLIB_LIBS@
noinst_PROGRAMS = ma_control_file-t trnman-t lockman-t noinst_PROGRAMS = ma_control_file-t trnman-t lockman-t lockman1-t lockman2-t
CLEANFILES = maria_control CLEANFILES = maria_control
...@@ -268,7 +268,7 @@ int main() ...@@ -268,7 +268,7 @@ int main()
test_lockman_simple(); test_lockman_simple();
#define CYCLES 1000 #define CYCLES 10000
#define THREADS Nlos /* don't change this line */ #define THREADS Nlos /* don't change this line */
/* mixed load, stress-test with random locks */ /* mixed load, stress-test with random locks */
......
/* Copyright (C) 2006 MySQL AB
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; either version 2 of the License, or
(at your option) any later version.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
//#define EXTRA_VERBOSE
#include <tap.h>
#include <my_global.h>
#include <my_sys.h>
#include <my_atomic.h>
#include <lf.h>
#include "../lockman.h"
#include "../tablockman.h"
#define Nlos 100
#define Ntbls 10
LOCK_OWNER loarray[Nlos];
TABLE_LOCK_OWNER loarray1[Nlos];
pthread_mutex_t mutexes[Nlos];
pthread_cond_t conds[Nlos];
LOCKED_TABLE ltarray[Ntbls];
LOCKMAN lockman;
TABLOCKMAN tablockman;
#ifndef EXTRA_VERBOSE
#define print_lo1(X) /* no-op */
#define DIAG(X) /* no-op */
#else
#define DIAG(X) diag X
#endif
LOCK_OWNER *loid2lo(uint16 loid)
{
return loarray+loid-1;
}
TABLE_LOCK_OWNER *loid2lo1(uint16 loid)
{
return loarray1+loid-1;
}
#define unlock_all(O) diag("lo" #O "> release all locks"); \
tablockman_release_locks(&tablockman, loid2lo1(O));
#define test_lock(O, R, L, S, RES) \
ok(tablockman_getlock(&tablockman, loid2lo1(O), &ltarray[R], L) == RES, \
"lo" #O "> " S "lock resource " #R " with " #L "-lock"); \
print_lo1(loid2lo1(O));
#define lock_ok_a(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK)
#define lock_ok_i(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE)
#define lock_ok_l(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE)
#define lock_conflict(O, R, L) \
test_lock(O, R, L, "cannot ", DIDNT_GET_THE_LOCK);
void test_tablockman_simple()
{
/* simple */
lock_ok_a(1, 1, S);
lock_ok_i(2, 2, IS);
lock_ok_i(1, 2, IX);
/* lock escalation */
lock_ok_a(1, 1, X);
lock_ok_i(2, 2, IX);
/* failures */
lock_conflict(2, 1, X);
unlock_all(2);
lock_ok_a(1, 2, S);
lock_ok_a(1, 2, IS);
lock_ok_a(1, 2, LS);
lock_ok_i(1, 3, IX);
lock_ok_a(2, 3, LS);
lock_ok_i(1, 3, IX);
lock_ok_l(2, 3, IS);
unlock_all(1);
unlock_all(2);
lock_ok_i(1, 1, IX);
lock_conflict(2, 1, S);
lock_ok_a(1, 1, LS);
unlock_all(1);
unlock_all(2);
lock_ok_i(1, 1, IX);
lock_ok_a(2, 1, LS);
lock_ok_a(1, 1, LS);
lock_ok_i(1, 1, IX);
lock_ok_i(3, 1, IS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
lock_ok_i(1, 4, IS);
lock_ok_i(2, 4, IS);
lock_ok_i(3, 4, IS);
lock_ok_a(3, 4, LS);
lock_ok_i(4, 4, IS);
lock_conflict(4, 4, IX);
lock_conflict(2, 4, IX);
lock_ok_a(1, 4, LS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
unlock_all(4);
lock_ok_i(1, 1, IX);
lock_ok_i(2, 1, IX);
lock_conflict(1, 1, S);
lock_conflict(2, 1, X);
unlock_all(1);
unlock_all(2);
}
int rt_num_threads;
int litmus;
int thread_number= 0, timeouts= 0;
void run_test(const char *test, pthread_handler handler, int n, int m)
{
pthread_t *threads;
ulonglong now= my_getsystime();
int i;
thread_number= timeouts= 0;
litmus= 0;
threads= (pthread_t *)my_malloc(sizeof(void *)*n, MYF(0));
if (!threads)
{
diag("Out of memory");
abort();
}
diag("Running %s with %d threads, %d iterations... ", test, n, m);
rt_num_threads= n;
for (i= 0; i < n ; i++)
if (pthread_create(threads+i, 0, handler, &m))
{
diag("Could not create thread");
abort();
}
for (i= 0 ; i < n ; i++)
pthread_join(threads[i], 0);
now= my_getsystime()-now;
ok(litmus == 0, "Finished %s in %g secs (%d)", test, ((double)now)/1e7, litmus);
my_free((void*)threads, MYF(0));
}
pthread_mutex_t rt_mutex;
int Nrows= 100;
int Ntables= 10;
int table_lock_ratio= 10;
enum lock_type lock_array[6]= {S, X, LS, LX, IS, IX};
char *lock2str[6]= {"S", "X", "LS", "LX", "IS", "IX"};
char *res2str[4]= {
"DIDN'T GET THE LOCK",
"GOT THE LOCK",
"GOT THE LOCK NEED TO LOCK A SUBRESOURCE",
"GOT THE LOCK NEED TO INSTANT LOCK A SUBRESOURCE"};
pthread_handler_t test_lockman(void *arg)
{
int m= (*(int *)arg);
uint x, loid, row, table, res, locklevel, timeout= 0;
LOCK_OWNER *lo; TABLE_LOCK_OWNER *lo1; DBUG_ASSERT(Ntables <= Ntbls);
pthread_mutex_lock(&rt_mutex);
loid= ++thread_number;
pthread_mutex_unlock(&rt_mutex);
lo= loid2lo(loid); lo1= loid2lo1(loid);
for (x= ((int)(intptr)(&m)); m > 0; m--)
{
x= (x*3628273133 + 1500450271) % 9576890767; /* three prime numbers */
row= x % Nrows + Ntables;
table= row % Ntables;
locklevel= (x/Nrows) & 3;
if (table_lock_ratio && (x/Nrows/4) % table_lock_ratio == 0)
{ /* table lock */
res= tablockman_getlock(&tablockman, lo1, ltarray+table, lock_array[locklevel]);
DIAG(("loid %2d, table %d, lock %s, res %s", loid, table,
lock2str[locklevel], res2str[res]));
if (res == DIDNT_GET_THE_LOCK)
{
lockman_release_locks(&lockman, lo); tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
}
DBUG_ASSERT(res == GOT_THE_LOCK);
}
else
{ /* row lock */
locklevel&= 1;
res= tablockman_getlock(&tablockman, lo1, ltarray+table, lock_array[locklevel + 4]);
DIAG(("loid %2d, row %d, lock %s, res %s", loid, row,
lock2str[locklevel+4], res2str[res]));
switch (res)
{
case DIDNT_GET_THE_LOCK:
lockman_release_locks(&lockman, lo); tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
case GOT_THE_LOCK:
continue;
case GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE:
/* not implemented, so take a regular lock */
case GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE:
res= lockman_getlock(&lockman, lo, row, lock_array[locklevel]);
DIAG(("loid %2d, ROW %d, lock %s, res %s", loid, row,
lock2str[locklevel], res2str[res]));
if (res == DIDNT_GET_THE_LOCK)
{
lockman_release_locks(&lockman, lo);
tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
}
DBUG_ASSERT(res == GOT_THE_LOCK);
continue;
default:
DBUG_ASSERT(0);
}
}
}
lockman_release_locks(&lockman, lo);
tablockman_release_locks(&tablockman, lo1);
pthread_mutex_lock(&rt_mutex);
rt_num_threads--;
timeouts+= timeout;
if (!rt_num_threads)
diag("number of timeouts: %d", timeouts);
pthread_mutex_unlock(&rt_mutex);
return 0;
}
int main()
{
int i;
my_init();
pthread_mutex_init(&rt_mutex, 0);
plan(35);
if (my_atomic_initialize())
return exit_status();
lockman_init(&lockman, &loid2lo, 50);
tablockman_init(&tablockman, &loid2lo1, 50);
for (i= 0; i < Nlos; i++)
{
pthread_mutex_init(&mutexes[i], MY_MUTEX_INIT_FAST);
pthread_cond_init (&conds[i], 0);
loarray[i].pins= lf_alloc_get_pins(&lockman.alloc);
loarray[i].all_locks= 0;
loarray[i].waiting_for= 0;
loarray[i].mutex= &mutexes[i];
loarray[i].cond= &conds[i];
loarray[i].loid= i+1;
loarray1[i].active_locks= 0;
loarray1[i].waiting_lock= 0;
loarray1[i].waiting_for= 0;
loarray1[i].mutex= &mutexes[i];
loarray1[i].cond= &conds[i];
loarray1[i].loid= i+1;
}
for (i= 0; i < Ntbls; i++)
{
tablockman_init_locked_table(ltarray+i, Nlos);
}
test_tablockman_simple();
#define CYCLES 10000
#define THREADS Nlos /* don't change this line */
/* mixed load, stress-test with random locks */
Nrows= 100;
Ntables= 10;
table_lock_ratio= 10;
run_test("\"random lock\" stress test", test_lockman, THREADS, CYCLES);
/* "real-life" simulation - many rows, no table locks */
Nrows= 1000000;
Ntables= 10;
table_lock_ratio= 0;
run_test("\"real-life\" simulation test", test_lockman, THREADS, CYCLES*10);
for (i= 0; i < Nlos; i++)
{
lockman_release_locks(&lockman, &loarray[i]);
pthread_mutex_destroy(loarray[i].mutex);
pthread_cond_destroy(loarray[i].cond);
lf_pinbox_put_pins(loarray[i].pins);
}
{
ulonglong now= my_getsystime();
lockman_destroy(&lockman);
now= my_getsystime()-now;
diag("lockman_destroy: %g secs", ((double)now)/1e7);
}
pthread_mutex_destroy(&rt_mutex);
my_end(0);
return exit_status();
}
/* Copyright (C) 2006 MySQL AB
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; either version 2 of the License, or
(at your option) any later version.
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
//#define EXTRA_VERBOSE
#include <tap.h>
#include <my_global.h>
#include <my_sys.h>
#include <my_atomic.h>
#include <lf.h>
#include "../tablockman.h"
#define Nlos 100
#define Ntbls 110
TABLE_LOCK_OWNER loarray1[Nlos];
pthread_mutex_t mutexes[Nlos];
pthread_cond_t conds[Nlos];
LOCKED_TABLE ltarray[Ntbls];
TABLOCKMAN tablockman;
#ifndef EXTRA_VERBOSE
#define print_lo1(X) /* no-op */
#define DIAG(X) /* no-op */
#else
#define DIAG(X) diag X
#endif
TABLE_LOCK_OWNER *loid2lo1(uint16 loid)
{
return loarray1+loid-1;
}
#define unlock_all(O) diag("lo" #O "> release all locks"); \
tablockman_release_locks(&tablockman, loid2lo1(O));
#define test_lock(O, R, L, S, RES) \
ok(tablockman_getlock(&tablockman, loid2lo1(O), &ltarray[R], L) == RES, \
"lo" #O "> " S "lock resource " #R " with " #L "-lock"); \
print_lo1(loid2lo1(O));
#define lock_ok_a(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK)
#define lock_ok_i(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE)
#define lock_ok_l(O, R, L) \
test_lock(O, R, L, "", GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE)
#define lock_conflict(O, R, L) \
test_lock(O, R, L, "cannot ", DIDNT_GET_THE_LOCK);
void test_tablockman_simple()
{
/* simple */
lock_ok_a(1, 1, S);
lock_ok_i(2, 2, IS);
lock_ok_i(1, 2, IX);
/* lock escalation */
lock_ok_a(1, 1, X);
lock_ok_i(2, 2, IX);
/* failures */
lock_conflict(2, 1, X);
unlock_all(2);
lock_ok_a(1, 2, S);
lock_ok_a(1, 2, IS);
lock_ok_a(1, 2, LS);
lock_ok_i(1, 3, IX);
lock_ok_a(2, 3, LS);
lock_ok_i(1, 3, IX);
lock_ok_l(2, 3, IS);
unlock_all(1);
unlock_all(2);
lock_ok_i(1, 1, IX);
lock_conflict(2, 1, S);
lock_ok_a(1, 1, LS);
unlock_all(1);
unlock_all(2);
lock_ok_i(1, 1, IX);
lock_ok_a(2, 1, LS);
lock_ok_a(1, 1, LS);
lock_ok_i(1, 1, IX);
lock_ok_i(3, 1, IS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
lock_ok_i(1, 4, IS);
lock_ok_i(2, 4, IS);
lock_ok_i(3, 4, IS);
lock_ok_a(3, 4, LS);
lock_ok_i(4, 4, IS);
lock_conflict(4, 4, IX);
lock_conflict(2, 4, IX);
lock_ok_a(1, 4, LS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
unlock_all(4);
lock_ok_i(1, 1, IX);
lock_ok_i(2, 1, IX);
lock_conflict(1, 1, S);
lock_conflict(2, 1, X);
unlock_all(1);
unlock_all(2);
lock_ok_i(1, 1, IS);
lock_conflict(2, 1, X);
lock_conflict(3, 1, IS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
lock_ok_a(1, 1, S);
lock_conflict(2, 1, IX);
lock_conflict(3, 1, IS);
unlock_all(1);
unlock_all(2);
unlock_all(3);
}
int rt_num_threads;
int litmus;
int thread_number= 0, timeouts= 0;
void run_test(const char *test, pthread_handler handler, int n, int m)
{
pthread_t *threads;
ulonglong now= my_getsystime();
int i;
thread_number= timeouts= 0;
litmus= 0;
threads= (pthread_t *)my_malloc(sizeof(void *)*n, MYF(0));
if (!threads)
{
diag("Out of memory");
abort();
}
diag("Running %s with %d threads, %d iterations... ", test, n, m);
rt_num_threads= n;
for (i= 0; i < n ; i++)
if (pthread_create(threads+i, 0, handler, &m))
{
diag("Could not create thread");
abort();
}
for (i= 0 ; i < n ; i++)
pthread_join(threads[i], 0);
now= my_getsystime()-now;
ok(litmus == 0, "Finished %s in %g secs (%d)", test, ((double)now)/1e7, litmus);
my_free((void*)threads, MYF(0));
}
pthread_mutex_t rt_mutex;
int Nrows= 100;
int Ntables= 10;
int table_lock_ratio= 10;
enum lock_type lock_array[6]= {S, X, LS, LX, IS, IX};
char *lock2str[6]= {"S", "X", "LS", "LX", "IS", "IX"};
char *res2str[4]= {
"DIDN'T GET THE LOCK",
"GOT THE LOCK",
"GOT THE LOCK NEED TO LOCK A SUBRESOURCE",
"GOT THE LOCK NEED TO INSTANT LOCK A SUBRESOURCE"};
pthread_handler_t test_lockman(void *arg)
{
int m= (*(int *)arg);
uint x, loid, row, table, res, locklevel, timeout= 0;
TABLE_LOCK_OWNER *lo1;
DBUG_ASSERT(Ntables <= Ntbls);
DBUG_ASSERT(Nrows + Ntables <= Ntbls);
pthread_mutex_lock(&rt_mutex);
loid= ++thread_number;
pthread_mutex_unlock(&rt_mutex);
lo1= loid2lo1(loid);
for (x= ((int)(intptr)(&m)); m > 0; m--)
{
x= (x*3628273133 + 1500450271) % 9576890767; /* three prime numbers */
row= x % Nrows + Ntables;
table= row % Ntables;
locklevel= (x/Nrows) & 3;
if (table_lock_ratio && (x/Nrows/4) % table_lock_ratio == 0)
{ /* table lock */
res= tablockman_getlock(&tablockman, lo1, ltarray+table, lock_array[locklevel]);
DIAG(("loid %2d, table %d, lock %s, res %s", loid, table,
lock2str[locklevel], res2str[res]));
if (res == DIDNT_GET_THE_LOCK)
{
tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
}
DBUG_ASSERT(res == GOT_THE_LOCK);
}
else
{ /* row lock */
locklevel&= 1;
res= tablockman_getlock(&tablockman, lo1, ltarray+table, lock_array[locklevel + 4]);
DIAG(("loid %2d, row %d, lock %s, res %s", loid, row,
lock2str[locklevel+4], res2str[res]));
switch (res)
{
case DIDNT_GET_THE_LOCK:
tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
case GOT_THE_LOCK:
continue;
case GOT_THE_LOCK_NEED_TO_INSTANT_LOCK_A_SUBRESOURCE:
/* not implemented, so take a regular lock */
case GOT_THE_LOCK_NEED_TO_LOCK_A_SUBRESOURCE:
res= tablockman_getlock(&tablockman, lo1, ltarray+row, lock_array[locklevel]);
DIAG(("loid %2d, ROW %d, lock %s, res %s", loid, row,
lock2str[locklevel], res2str[res]));
if (res == DIDNT_GET_THE_LOCK)
{
tablockman_release_locks(&tablockman, lo1);
DIAG(("loid %2d, release all locks", loid));
timeout++;
continue;
}
DBUG_ASSERT(res == GOT_THE_LOCK);
continue;
default:
DBUG_ASSERT(0);
}
}
}
tablockman_release_locks(&tablockman, lo1);
pthread_mutex_lock(&rt_mutex);
rt_num_threads--;
timeouts+= timeout;
if (!rt_num_threads)
diag("number of timeouts: %d", timeouts);
pthread_mutex_unlock(&rt_mutex);
return 0;
}
int main()
{
int i;
my_init();
pthread_mutex_init(&rt_mutex, 0);
plan(35);
if (my_atomic_initialize())
return exit_status();
tablockman_init(&tablockman, &loid2lo1, 50);
for (i= 0; i < Nlos; i++)
{
pthread_mutex_init(&mutexes[i], MY_MUTEX_INIT_FAST);
pthread_cond_init (&conds[i], 0);
loarray1[i].active_locks= 0;
loarray1[i].waiting_lock= 0;
loarray1[i].waiting_for= 0;
loarray1[i].mutex= &mutexes[i];
loarray1[i].cond= &conds[i];
loarray1[i].loid= i+1;
}
for (i= 0; i < Ntbls; i++)
{
tablockman_init_locked_table(ltarray+i, Nlos);
}
test_tablockman_simple();
#define CYCLES 10000
#define THREADS Nlos /* don't change this line */
/* mixed load, stress-test with random locks */
Nrows= 100;
Ntables= 10;
table_lock_ratio= 10;
//run_test("\"random lock\" stress test", test_lockman, THREADS, CYCLES);
#if 0
/* "real-life" simulation - many rows, no table locks */
Nrows= 1000000;
Ntables= 10;
table_lock_ratio= 0;
run_test("\"real-life\" simulation test", test_lockman, THREADS, CYCLES*10);
#endif
for (i= 0; i < Nlos; i++)
{
tablockman_release_locks(&tablockman, &loarray1[i]);
pthread_mutex_destroy(loarray1[i].mutex);
pthread_cond_destroy(loarray1[i].cond);
}
{
ulonglong now= my_getsystime();
for (i= 0; i < Ntbls; i++)
{
tablockman_destroy_locked_table(ltarray+i);
}
tablockman_destroy(&tablockman);
now= my_getsystime()-now;
diag("lockman_destroy: %g secs", ((double)now)/1e7);
}
pthread_mutex_destroy(&rt_mutex);
my_end(0);
return exit_status();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment