From 87be19ee6031f01444b6073e6b5808ba58afc6a0 Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Tue, 10 Jan 2012 17:29:14 +0100 Subject: [Mnesia] First try with ordered_set instead of bag Switch to ordered_set so match_object matches partially bound keys, more efficient. --- lib/mnesia/src/mnesia_locker.erl | 74 +++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 38 deletions(-) (limited to 'lib') diff --git a/lib/mnesia/src/mnesia_locker.erl b/lib/mnesia/src/mnesia_locker.erl index de4811f8e4..6e21332480 100644 --- a/lib/mnesia/src/mnesia_locker.erl +++ b/lib/mnesia/src/mnesia_locker.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 1996-2011. All Rights Reserved. +%% Copyright Ericsson AB 1996-2012. All Rights Reserved. %% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in @@ -66,8 +66,8 @@ -record(queue, {oid, tid, op, pid, lucky}). -%% mnesia_held_locks: contain {Oid, Op, Tid} entries (bag) --define(match_oid_held_locks(Oid), {Oid, '_', '_'}). +%% mnesia_held_locks: contain {{Oid,Tid}, Op} entries +-define(match_oid_held_locks(Oid), {{Oid, '_'}, '_'}). %% mnesia_tid_locks: contain {Tid, Oid, Op} entries (bag) -define(match_oid_tid_locks(Tid), {Tid, '_', '_'}). %% mnesia_sticky_locks: contain {Oid, Node} entries and {Tab, Node} entries (set) @@ -83,7 +83,7 @@ start() -> init(Parent) -> register(?MODULE, self()), process_flag(trap_exit, true), - ?ets_new_table(mnesia_held_locks, [bag, private, named_table]), + ?ets_new_table(mnesia_held_locks, [ordered_set, private, named_table]), ?ets_new_table(mnesia_tid_locks, [bag, private, named_table]), ?ets_new_table(mnesia_sticky_locks, [set, private, named_table]), ?ets_new_table(mnesia_lock_queue, [bag, private, named_table, {keypos, 2}]), @@ -237,7 +237,7 @@ loop(State) -> set_lock(Tid, Oid, Op) -> ?dbg("Granted ~p ~p ~p~n", [Tid,Oid,Op]), - ?ets_insert(mnesia_held_locks, {Oid, Op, Tid}), + ?ets_insert(mnesia_held_locks, {{Oid, Tid}, Op}), ?ets_insert(mnesia_tid_locks, {Tid, Oid, Op}). %%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -321,19 +321,19 @@ grant_lock(Tid, write, Lock, Oid) -> can_lock(Tid, read, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> %% The key is bound, no need for the other BIF Oid = {Tab, Key}, - ObjLocks = ?ets_match_object(mnesia_held_locks, {Oid, write, '_'}), - TabLocks = ?ets_match_object(mnesia_held_locks, {{Tab, ?ALL}, write, '_'}), + ObjLocks = ?ets_match_object(mnesia_held_locks, {{Oid, '_'}, write}), + TabLocks = ?ets_match_object(mnesia_held_locks, {{{Tab, ?ALL}, '_'}, write}), check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, read); can_lock(Tid, read, Oid, AlreadyQ) -> % Whole tab Tab = element(1, Oid), - ObjLocks = ?ets_match_object(mnesia_held_locks, {{Tab, '_'}, write, '_'}), + ObjLocks = ?ets_match_object(mnesia_held_locks, {{{Tab, '_'}, '_'}, write}), check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, read); can_lock(Tid, write, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> Oid = {Tab, Key}, - ObjLocks = ?ets_lookup(mnesia_held_locks, Oid), - TabLocks = ?ets_lookup(mnesia_held_locks, {Tab, ?ALL}), + ObjLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks(Oid)), + TabLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks({Tab, ?ALL})), check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, write); can_lock(Tid, write, Oid, AlreadyQ) -> % Whole tab @@ -342,31 +342,28 @@ can_lock(Tid, write, Oid, AlreadyQ) -> % Whole tab check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, write). %% Check held locks for conflicting locks -check_lock(Tid, Oid, [Lock | Locks], TabLocks, X, AlreadyQ, Type) -> - case element(3, Lock) of - Tid -> - check_lock(Tid, Oid, Locks, TabLocks, X, AlreadyQ, Type); - WaitForTid -> - Queue = allowed_to_be_queued(WaitForTid,Tid), - if Queue == true -> - check_lock(Tid, Oid, Locks, TabLocks, {queue, WaitForTid}, AlreadyQ, Type); - Tid#tid.pid == WaitForTid#tid.pid -> - dbg_out("Spurious lock conflict ~w ~w: ~w -> ~w~n", - [Oid, Lock, Tid, WaitForTid]), - %% Test.. - {Tab, _Key} = Oid, - HaveQ = (ets:lookup(mnesia_lock_queue, Oid) /= []) - orelse (ets:lookup(mnesia_lock_queue,{Tab,?ALL}) /= []), - if - HaveQ -> - {no, WaitForTid}; - true -> - check_lock(Tid,Oid,Locks,TabLocks,{queue,WaitForTid},AlreadyQ,Type) - end; - %%{no, WaitForTid}; Safe solution - true -> - {no, WaitForTid} - end +check_lock(Tid, Oid, [{{_,Tid}, _} | Locks], TabLocks, X, AlreadyQ, Type) -> + check_lock(Tid, Oid, Locks, TabLocks, X, AlreadyQ, Type); +check_lock(Tid, Oid, [Lock = {{_,WaitForTid}, _} | Locks], TabLocks, _X, AlreadyQ, Type) -> + Queue = allowed_to_be_queued(WaitForTid,Tid), + if Queue == true -> + check_lock(Tid, Oid, Locks, TabLocks, {queue, WaitForTid}, AlreadyQ, Type); + Tid#tid.pid == WaitForTid#tid.pid -> + dbg_out("Spurious lock conflict ~w ~w: ~w -> ~w~n", + [Oid, Lock, Tid, WaitForTid]), + %% Test.. + {Tab, _Key} = Oid, + HaveQ = (ets:lookup(mnesia_lock_queue, Oid) /= []) + orelse (ets:lookup(mnesia_lock_queue,{Tab,?ALL}) /= []), + if + HaveQ -> + {no, WaitForTid}; + true -> + check_lock(Tid,Oid,Locks,TabLocks,{queue,WaitForTid},AlreadyQ,Type) + end; + %%{no, WaitForTid}; Safe solution + true -> + {no, WaitForTid} end; check_lock(_, _, [], [], X, {queue, bad_luck}, _) -> @@ -523,9 +520,9 @@ release_lock({Tid, Oid, {queued, _}}) -> release_lock({Tid, Oid, Op}) -> if Op == write -> - ?ets_delete(mnesia_held_locks, Oid); + ?ets_delete(mnesia_held_locks, {Oid, Tid}); Op == read -> - ets:delete_object(mnesia_held_locks, {Oid, Op, Tid}) + ets:delete_object(mnesia_held_locks, {{Oid, Tid}, Op}) end. rearrange_queue([{_Tid, {Tab, Key}, _} | Locks]) -> @@ -1116,7 +1113,8 @@ rec_requests([], _Oid, _Store) -> get_held_locks() -> ?MODULE ! {get_table, self(), mnesia_held_locks}, - receive {mnesia_held_locks, Locks} -> Locks end. + Locks = receive {mnesia_held_locks, Ls} -> Ls end, + [{Oid, Op, Tid} || {{Oid, Tid}, Op} <- Locks]. get_lock_queue() -> ?MODULE ! {get_table, self(), mnesia_lock_queue}, -- cgit v1.2.3 From 5e1972a6d03bb67b903ccd0d1e45eb00f42b9ac0 Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Wed, 11 Jan 2012 13:28:30 +0100 Subject: [Mnesia] Optimize for lookup instead of match_object 5% faster on tpcb --- lib/mnesia/src/mnesia_locker.erl | 114 +++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 39 deletions(-) (limited to 'lib') diff --git a/lib/mnesia/src/mnesia_locker.erl b/lib/mnesia/src/mnesia_locker.erl index 6e21332480..55df074c29 100644 --- a/lib/mnesia/src/mnesia_locker.erl +++ b/lib/mnesia/src/mnesia_locker.erl @@ -66,8 +66,8 @@ -record(queue, {oid, tid, op, pid, lucky}). -%% mnesia_held_locks: contain {{Oid,Tid}, Op} entries --define(match_oid_held_locks(Oid), {{Oid, '_'}, '_'}). +%% mnesia_held_locks: contain {Oid, MaxLock, [{Op, Tid}]} entries +-define(match_oid_held_locks(Oid), {Oid, '_', '_'}). %% mnesia_tid_locks: contain {Tid, Oid, Op} entries (bag) -define(match_oid_tid_locks(Tid), {Tid, '_', '_'}). %% mnesia_sticky_locks: contain {Oid, Node} entries and {Tab, Node} entries (set) @@ -237,7 +237,14 @@ loop(State) -> set_lock(Tid, Oid, Op) -> ?dbg("Granted ~p ~p ~p~n", [Tid,Oid,Op]), - ?ets_insert(mnesia_held_locks, {{Oid, Tid}, Op}), + case ?ets_lookup(mnesia_held_locks, Oid) of + [] -> + ?ets_insert(mnesia_held_locks, {Oid, Op, [{Op, Tid}]}); + [{Oid, Prev, Items}] when Op == read -> + ?ets_insert(mnesia_held_locks, {Oid, Prev, [{Op, Tid}|Items]}); + [{Oid, _, Items}] when Op == write -> + ?ets_insert(mnesia_held_locks, {Oid, Op, [{Op, Tid}|Items]}) + end, ?ets_insert(mnesia_tid_locks, {Tid, Oid, Op}). %%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -321,19 +328,19 @@ grant_lock(Tid, write, Lock, Oid) -> can_lock(Tid, read, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> %% The key is bound, no need for the other BIF Oid = {Tab, Key}, - ObjLocks = ?ets_match_object(mnesia_held_locks, {{Oid, '_'}, write}), - TabLocks = ?ets_match_object(mnesia_held_locks, {{{Tab, ?ALL}, '_'}, write}), + ObjLocks = filter_write(?ets_lookup(mnesia_held_locks, Oid)), + TabLocks = filter_write(?ets_lookup(mnesia_held_locks, {Tab, ?ALL})), check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, read); can_lock(Tid, read, Oid, AlreadyQ) -> % Whole tab Tab = element(1, Oid), - ObjLocks = ?ets_match_object(mnesia_held_locks, {{{Tab, '_'}, '_'}, write}), + ObjLocks = ?ets_match_object(mnesia_held_locks, {{Tab, '_'}, write, '_'}), check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, read); can_lock(Tid, write, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> Oid = {Tab, Key}, - ObjLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks(Oid)), - TabLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks({Tab, ?ALL})), + ObjLocks = ?ets_lookup(mnesia_held_locks, Oid), + TabLocks = ?ets_lookup(mnesia_held_locks, {Tab, ?ALL}), check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, write); can_lock(Tid, write, Oid, AlreadyQ) -> % Whole tab @@ -341,29 +348,16 @@ can_lock(Tid, write, Oid, AlreadyQ) -> % Whole tab ObjLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks({Tab, '_'})), check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, write). +filter_write([{_, read, _}]) -> []; +filter_write(Res) -> Res. + %% Check held locks for conflicting locks -check_lock(Tid, Oid, [{{_,Tid}, _} | Locks], TabLocks, X, AlreadyQ, Type) -> - check_lock(Tid, Oid, Locks, TabLocks, X, AlreadyQ, Type); -check_lock(Tid, Oid, [Lock = {{_,WaitForTid}, _} | Locks], TabLocks, _X, AlreadyQ, Type) -> - Queue = allowed_to_be_queued(WaitForTid,Tid), - if Queue == true -> - check_lock(Tid, Oid, Locks, TabLocks, {queue, WaitForTid}, AlreadyQ, Type); - Tid#tid.pid == WaitForTid#tid.pid -> - dbg_out("Spurious lock conflict ~w ~w: ~w -> ~w~n", - [Oid, Lock, Tid, WaitForTid]), - %% Test.. - {Tab, _Key} = Oid, - HaveQ = (ets:lookup(mnesia_lock_queue, Oid) /= []) - orelse (ets:lookup(mnesia_lock_queue,{Tab,?ALL}) /= []), - if - HaveQ -> - {no, WaitForTid}; - true -> - check_lock(Tid,Oid,Locks,TabLocks,{queue,WaitForTid},AlreadyQ,Type) - end; - %%{no, WaitForTid}; Safe solution - true -> - {no, WaitForTid} +check_lock(Tid, Oid, [{_, _, Lock} | Locks], TabLocks, _X, AlreadyQ, Type) -> + case can_queue(Lock, Tid, Oid, _X) of + {no, _} = Res -> + Res; + Res -> + check_lock(Tid, Oid, Locks, TabLocks, Res, AlreadyQ, Type) end; check_lock(_, _, [], [], X, {queue, bad_luck}, _) -> @@ -400,6 +394,26 @@ check_lock(Tid, Oid, [], [], X, AlreadyQ, Type) -> check_lock(Tid, Oid, [], TabLocks, X, AlreadyQ, Type) -> check_lock(Tid, Oid, TabLocks, [], X, AlreadyQ, Type). +can_queue([{_Op, Tid}|Locks], Tid, Oid, Res) -> + can_queue(Locks, Tid, Oid, Res); +can_queue([{Op, WaitForTid}|Locks], Tid, Oid = {Tab, _}, _) -> + case allowed_to_be_queued(WaitForTid,Tid) of + true when Tid#tid.pid == WaitForTid#tid.pid -> + dbg_out("Spurious lock conflict ~w ~w: ~w -> ~w~n", + [Oid, Op, Tid, WaitForTid]), + HaveQ = (ets:lookup(mnesia_lock_queue, Oid) /= []) + orelse (ets:lookup(mnesia_lock_queue,{Tab,?ALL}) /= []), + case HaveQ of + true -> {no, WaitForTid}; + false -> can_queue(Locks, Tid, Oid, {queue, WaitForTid}) + end; + true -> + can_queue(Locks, Tid, Oid, {queue, WaitForTid}); + false -> + {no, WaitForTid} + end; +can_queue([], _, _, Res) -> Res. + %% True if WaitForTid > Tid -> % Important order allowed_to_be_queued(WaitForTid, Tid) -> case get(pid_sort_order) of @@ -517,12 +531,23 @@ release_locks([]) -> release_lock({Tid, Oid, {queued, _}}) -> ?ets_match_delete(mnesia_lock_queue, #queue{oid=Oid, tid = Tid, op = '_', pid = '_', lucky = '_'}); -release_lock({Tid, Oid, Op}) -> - if - Op == write -> - ?ets_delete(mnesia_held_locks, {Oid, Tid}); - Op == read -> - ets:delete_object(mnesia_held_locks, {{Oid, Tid}, Op}) +release_lock({This, Oid, Op}) -> + case ?ets_lookup(mnesia_held_locks, Oid) of + [] -> ok; + [{Oid, Prev, Locks0}] -> + Locks = [{TOp, Tid} || {TOp, Tid} <- Locks0, Tid =/= This], + if Locks =:= [] -> + ?ets_delete(mnesia_held_locks, Oid); + Op =:= read -> + ?ets_insert(mnesia_held_locks, {Oid, Prev, Locks}); + true -> + case lists:any(fun({TOp, _}) -> TOp =:= write end, Locks) of + true -> + ?ets_insert(mnesia_held_locks, {Oid, write, Locks}); + false -> + ?ets_insert(mnesia_held_locks, {Oid, read, Locks}) + end + end end. rearrange_queue([{_Tid, {Tab, Key}, _} | Locks]) -> @@ -1113,12 +1138,23 @@ rec_requests([], _Oid, _Store) -> get_held_locks() -> ?MODULE ! {get_table, self(), mnesia_held_locks}, - Locks = receive {mnesia_held_locks, Ls} -> Ls end, - [{Oid, Op, Tid} || {{Oid, Tid}, Op} <- Locks]. + Locks = receive {mnesia_held_locks, Ls} -> Ls after 5000 -> [] end, + rewrite_locks(Locks, []). + +rewrite_locks([{Oid, _, Ls}|Locks], Acc0) -> + Acc = rewrite_locks(Ls, Oid, Acc0), + rewrite_locks(Locks, Acc); +rewrite_locks([], Acc) -> + lists:reverse(Acc). + +rewrite_locks([{Op, Tid}|Ls], Oid, Acc) -> + rewrite_locks(Ls, Oid, [{Oid, Op, Tid}|Acc]); +rewrite_locks([], _, Acc) -> + Acc. get_lock_queue() -> ?MODULE ! {get_table, self(), mnesia_lock_queue}, - Q = receive {mnesia_lock_queue, Locks} -> Locks end, + Q = receive {mnesia_lock_queue, Locks} -> Locks after 5000 -> [] end, [{Oid, Op, Pid, Tid, WFT} || {queue, Oid, Tid, Op, Pid, WFT} <- Q]. do_stop() -> -- cgit v1.2.3 From 0c6d10bf1ee4a46c0d84a18fa99f1d99c144906a Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Wed, 11 Jan 2012 16:16:14 +0100 Subject: [Mnesia] Optimize double ets_lookups --- lib/mnesia/src/mnesia_locker.erl | 100 +++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 47 deletions(-) (limited to 'lib') diff --git a/lib/mnesia/src/mnesia_locker.erl b/lib/mnesia/src/mnesia_locker.erl index 55df074c29..a9eb27dea2 100644 --- a/lib/mnesia/src/mnesia_locker.erl +++ b/lib/mnesia/src/mnesia_locker.erl @@ -235,17 +235,17 @@ loop(State) -> loop(State) end. -set_lock(Tid, Oid, Op) -> - ?dbg("Granted ~p ~p ~p~n", [Tid,Oid,Op]), - case ?ets_lookup(mnesia_held_locks, Oid) of - [] -> - ?ets_insert(mnesia_held_locks, {Oid, Op, [{Op, Tid}]}); - [{Oid, Prev, Items}] when Op == read -> - ?ets_insert(mnesia_held_locks, {Oid, Prev, [{Op, Tid}|Items]}); - [{Oid, _, Items}] when Op == write -> - ?ets_insert(mnesia_held_locks, {Oid, Op, [{Op, Tid}|Items]}) - end, - ?ets_insert(mnesia_tid_locks, {Tid, Oid, Op}). +set_lock(Tid, Oid, Op, []) -> + ?ets_insert(mnesia_tid_locks, {Tid, Oid, Op}), + ?ets_insert(mnesia_held_locks, {Oid, Op, [{Op, Tid}]}); +set_lock(Tid, Oid, read, [{Oid, Prev, Items}]) -> + ?ets_insert(mnesia_tid_locks, {Tid, Oid, read}), + ?ets_insert(mnesia_held_locks, {Oid, Prev, [{read, Tid}|Items]}); +set_lock(Tid, Oid, write, [{Oid, _Prev, Items}]) -> + ?ets_insert(mnesia_tid_locks, {Tid, Oid, write}), + ?ets_insert(mnesia_held_locks, {Oid, write, [{write, Tid}|Items]}); +set_lock(Tid, Oid, Op, undefined) -> + set_lock(Tid, Oid, Op, ?ets_lookup(mnesia_held_locks, Oid)). %%%%%%%%%%%%%%%%%%%%%%%%%%% %% Acquire locks @@ -268,14 +268,14 @@ try_lock(Tid, Op, Pid, Oid) -> try_lock(Tid, Op, SimpleOp, Lock, Pid, Oid) -> case can_lock(Tid, Lock, Oid, {no, bad_luck}) of - yes -> - Reply = grant_lock(Tid, SimpleOp, Lock, Oid), + {yes, Default} -> + Reply = grant_lock(Tid, SimpleOp, Lock, Oid, Default), reply(Pid, Reply); - {no, Lucky} -> + {{no, Lucky},_} -> C = #cyclic{op = SimpleOp, lock = Lock, oid = Oid, lucky = Lucky}, ?dbg("Rejected ~p ~p ~p ~p ~n", [Tid, Oid, Lock, Lucky]), reply(Pid, {not_granted, C}); - {queue, Lucky} -> + {{queue, Lucky},_} -> ?dbg("Queued ~p ~p ~p ~p ~n", [Tid, Oid, Lock, Lucky]), %% Append to queue: Nice place for trace output ?ets_insert(mnesia_lock_queue, @@ -284,16 +284,16 @@ try_lock(Tid, Op, SimpleOp, Lock, Pid, Oid) -> ?ets_insert(mnesia_tid_locks, {Tid, Oid, {queued, Op}}) end. -grant_lock(Tid, read, Lock, Oid = {Tab, Key}) +grant_lock(Tid, read, Lock, Oid = {Tab, Key}, Default) when Key /= ?ALL, Tab /= ?GLOBAL -> case node(Tid#tid.pid) == node() of true -> - set_lock(Tid, Oid, Lock), + set_lock(Tid, Oid, Lock, Default), {granted, lookup_in_client}; false -> try Val = mnesia_lib:db_get(Tab, Key), %% lookup as well - set_lock(Tid, Oid, Lock), + set_lock(Tid, Oid, Lock, Default), {granted, Val} catch _:_Reason -> %% Table has been deleted from this node, @@ -303,19 +303,19 @@ grant_lock(Tid, read, Lock, Oid = {Tab, Key}) {not_granted, C} end end; -grant_lock(Tid, {ix_read,IxKey,Pos}, Lock, Oid = {Tab, _}) -> +grant_lock(Tid, {ix_read,IxKey,Pos}, Lock, Oid = {Tab, _}, Default) -> try Res = ix_read_res(Tab, IxKey,Pos), - set_lock(Tid, Oid, Lock), + set_lock(Tid, Oid, Lock, Default), {granted, Res, [?ALL]} catch _:_ -> {not_granted, {no_exists, Tab, {index, [Pos]}}} end; -grant_lock(Tid, read, Lock, Oid) -> - set_lock(Tid, Oid, Lock), +grant_lock(Tid, read, Lock, Oid, Default) -> + set_lock(Tid, Oid, Lock, Default), {granted, ok}; -grant_lock(Tid, write, Lock, Oid) -> - set_lock(Tid, Oid, Lock), +grant_lock(Tid, write, Lock, Oid, Default) -> + set_lock(Tid, Oid, Lock, Default), granted. %% 1) Impose an ordering on all transactions favour old (low tid) transactions @@ -325,28 +325,29 @@ grant_lock(Tid, write, Lock, Oid) -> %% 3) TabLocks is the problem :-) They should not starve and not deadlock %% handle tablocks in queue as they had locks on unlocked records. -can_lock(Tid, read, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> - %% The key is bound, no need for the other BIF - Oid = {Tab, Key}, - ObjLocks = filter_write(?ets_lookup(mnesia_held_locks, Oid)), - TabLocks = filter_write(?ets_lookup(mnesia_held_locks, {Tab, ?ALL})), - check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, read); +can_lock(Tid, read, Oid = {Tab, Key}, AlreadyQ) when Key /= ?ALL -> + ObjLocks = ?ets_lookup(mnesia_held_locks, Oid), + TabLocks = ?ets_lookup(mnesia_held_locks, {Tab, ?ALL}), + {check_lock(Tid, Oid, + filter_write(ObjLocks), + filter_write(TabLocks), + yes, AlreadyQ, read), + ObjLocks}; can_lock(Tid, read, Oid, AlreadyQ) -> % Whole tab Tab = element(1, Oid), ObjLocks = ?ets_match_object(mnesia_held_locks, {{Tab, '_'}, write, '_'}), - check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, read); + {check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, read), undefined}; -can_lock(Tid, write, {Tab, Key}, AlreadyQ) when Key /= ?ALL -> - Oid = {Tab, Key}, +can_lock(Tid, write, Oid = {Tab, Key}, AlreadyQ) when Key /= ?ALL -> ObjLocks = ?ets_lookup(mnesia_held_locks, Oid), TabLocks = ?ets_lookup(mnesia_held_locks, {Tab, ?ALL}), - check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, write); + {check_lock(Tid, Oid, ObjLocks, TabLocks, yes, AlreadyQ, write), ObjLocks}; can_lock(Tid, write, Oid, AlreadyQ) -> % Whole tab Tab = element(1, Oid), ObjLocks = ?ets_match_object(mnesia_held_locks, ?match_oid_held_locks({Tab, '_'})), - check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, write). + {check_lock(Tid, Oid, ObjLocks, [], yes, AlreadyQ, write), undefined}. filter_write([{_, read, _}]) -> []; filter_write(Res) -> Res. @@ -366,8 +367,7 @@ check_lock(_, _, [], [], X, {queue, bad_luck}, _) -> check_lock(_, _, [], [], X = {queue, _Tid}, _AlreadyQ, _) -> X; -check_lock(Tid, Oid, [], [], X, AlreadyQ, Type) -> - {Tab, Key} = Oid, +check_lock(Tid, Oid = {Tab, Key}, [], [], X, AlreadyQ, Type) -> if Type == write -> check_queue(Tid, Tab, X, AlreadyQ); @@ -467,14 +467,14 @@ set_read_lock_on_all_keys(Tid, From, Tab, IxKey, Pos) -> Op = {ix_read,IxKey, Pos}, Lock = read, case can_lock(Tid, Lock, Oid, {no, bad_luck}) of - yes -> - Reply = grant_lock(Tid, Op, Lock, Oid), + {yes, Default} -> + Reply = grant_lock(Tid, Op, Lock, Oid, Default), reply(From, Reply); - {no, Lucky} -> + {{no, Lucky},_} -> C = #cyclic{op = Op, lock = Lock, oid = Oid, lucky = Lucky}, ?dbg("Rejected ~p ~p ~p ~p ~n", [Tid, Oid, Lock, Lucky]), reply(From, {not_granted, C}); - {queue, Lucky} -> + {{queue, Lucky},_} -> ?dbg("Queued ~p ~p ~p ~p ~n", [Tid, Oid, Lock, Lucky]), %% Append to queue: Nice place for trace output ?ets_insert(mnesia_lock_queue, @@ -531,11 +531,11 @@ release_locks([]) -> release_lock({Tid, Oid, {queued, _}}) -> ?ets_match_delete(mnesia_lock_queue, #queue{oid=Oid, tid = Tid, op = '_', pid = '_', lucky = '_'}); -release_lock({This, Oid, Op}) -> +release_lock({Tid, Oid, Op}) -> case ?ets_lookup(mnesia_held_locks, Oid) of [] -> ok; [{Oid, Prev, Locks0}] -> - Locks = [{TOp, Tid} || {TOp, Tid} <- Locks0, Tid =/= This], + Locks = remove_tid(Locks0, Tid, []), if Locks =:= [] -> ?ets_delete(mnesia_held_locks, Oid); Op =:= read -> @@ -550,6 +550,12 @@ release_lock({This, Oid, Op}) -> end end. +remove_tid([{_Op, Tid}|Ls], Tid, Acc) -> + remove_tid(Ls,Tid, Acc); +remove_tid([Keep|Ls], Tid, Acc) -> + remove_tid(Ls,Tid, [Keep|Acc]); +remove_tid([], _, Acc) -> Acc. + rearrange_queue([{_Tid, {Tab, Key}, _} | Locks]) -> if Key /= ?ALL-> @@ -614,18 +620,18 @@ try_waiter({queue, Oid, Tid, Op, ReplyTo, _}) -> try_waiter(Oid, Op, SimpleOp, Lock, ReplyTo, Tid) -> case can_lock(Tid, Lock, Oid, {queue, bad_luck}) of - yes -> + {yes, Default} -> %% Delete from queue: Nice place for trace output ?ets_match_delete(mnesia_lock_queue, #queue{oid=Oid, tid = Tid, op = Op, pid = ReplyTo, lucky = '_'}), - Reply = grant_lock(Tid, SimpleOp, Lock, Oid), + Reply = grant_lock(Tid, SimpleOp, Lock, Oid, Default), reply(ReplyTo,Reply), locked; - {queue, _Why} -> + {{queue, _Why}, _} -> ?dbg("Keep ~p ~p ~p ~p~n", [Tid, Oid, Lock, _Why]), queued; % Keep waiter in queue - {no, Lucky} -> + {{no, Lucky}, _} -> C = #cyclic{op = SimpleOp, lock = Lock, oid = Oid, lucky = Lucky}, verbose("** WARNING ** Restarted transaction, possible deadlock in lock queue ~w: cyclic = ~w~n", [Tid, C]), -- cgit v1.2.3 From e9072b356cc917cacbffabc5086eb7d880ec5bb2 Mon Sep 17 00:00:00 2001 From: Dan Gudmundsson Date: Thu, 12 Jan 2012 11:54:09 +0100 Subject: [Mnesia] More optimizations If a transaction releases a write, it can be deleted directly since no read locks or other write locks can be present --- lib/mnesia/src/mnesia_locker.erl | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/mnesia/src/mnesia_locker.erl b/lib/mnesia/src/mnesia_locker.erl index a9eb27dea2..a22c95d454 100644 --- a/lib/mnesia/src/mnesia_locker.erl +++ b/lib/mnesia/src/mnesia_locker.erl @@ -531,23 +531,16 @@ release_locks([]) -> release_lock({Tid, Oid, {queued, _}}) -> ?ets_match_delete(mnesia_lock_queue, #queue{oid=Oid, tid = Tid, op = '_', pid = '_', lucky = '_'}); -release_lock({Tid, Oid, Op}) -> +release_lock({_Tid, Oid, write}) -> + ?ets_delete(mnesia_held_locks, Oid); +release_lock({Tid, Oid, read}) -> case ?ets_lookup(mnesia_held_locks, Oid) of - [] -> ok; [{Oid, Prev, Locks0}] -> - Locks = remove_tid(Locks0, Tid, []), - if Locks =:= [] -> - ?ets_delete(mnesia_held_locks, Oid); - Op =:= read -> - ?ets_insert(mnesia_held_locks, {Oid, Prev, Locks}); - true -> - case lists:any(fun({TOp, _}) -> TOp =:= write end, Locks) of - true -> - ?ets_insert(mnesia_held_locks, {Oid, write, Locks}); - false -> - ?ets_insert(mnesia_held_locks, {Oid, read, Locks}) - end - end + case remove_tid(Locks0, Tid, []) of + [] -> ?ets_delete(mnesia_held_locks, Oid); + Locks -> ?ets_insert(mnesia_held_locks, {Oid, Prev, Locks}) + end; + [] -> ok end. remove_tid([{_Op, Tid}|Ls], Tid, Acc) -> -- cgit v1.2.3