Thread: Mutexes and Latches....


Permlink Replies: 12 - Pages: 1 - Last Post: Feb 8, 2008 7:44 PM Last Post By: Aman....
Aman....

Posts: 9,551
Registered: 05/20/01
Mutexes and Latches....
Posted: Feb 6, 2008 8:01 AM
Click to report abuse...   Click to reply to this thread Reply
Hi there,
Oracle has started using mutexes and it is said that they are more efficient as compared to latches.
Questions
1)What is mutex?I know mutex are mutual exclusions and they are the concept of multiple threads.What I want to know that how this concept is implemented in Oracledatabase?
2) How they are better than latches?both are used for low level locking so how one is better than the other?
Any input is welcome.
Thanks and regards
Aman....
Chris Antognini

Posts: 1,039
Registered: 03/20/99
Re: Mutexes and Latches....
Posted: Feb 6, 2008 8:43 AM   in response to: Aman.... in response to: Aman....
Click to report abuse...   Click to reply to this thread Reply
Aman

1) Simply put mutexes are memory structures. They are used to serialize the access to shared structures. IMHO their most important characteristics are two. First, they can be taken in shared or exclusive mode. Second, getting a mutex can be done in wait or no-wait mode.

2) The main advantages over latches are that mutexes requires less memory and are faster to get and release.

HTH
Chris
Aman....

Posts: 9,551
Registered: 05/20/01
Re: Mutexes and Latches....
Posted: Feb 6, 2008 10:29 AM   in response to: Chris Antognini in response to: Chris Antognini
Click to report abuse...   Click to reply to this thread Reply
Chris,
Thanks for the reply.
1) Yes I understand that they are exactly nothing but the low level locks on the memory structures to protect their integerity.Thats the same thing that is latches are meant for.
Ok the benefits that you have mentioned , out of that the shared and exclusive mode access looks the relevant one. The other one is same with the latches as they are also acquired in the same mode of immediate or willing to wait correct ?
2) Well the smaller is indeed there.The faster I amnot sure still why. What Orcle says , that as like latches which are available for either one or multiple structures as one resource manager , so their contention can also come.This can be leading to an assumption to a "false contention" sort of thing like suppose there are lots of sessions looking for statements in the shared pool than the library cache latch will come under contention. Now this is a false contention if we just look at it as if sessions are looking for soft parsed cursors than its a good thing but because the looking mechanism, latch is not available so its actualy is a contention which will be removed with the help of mutexes as they wil be allocated as per the the structure so there wont be a starvation issue like latches with them. I am sure besides this , there must be some thing more relevant for mutexes also there which I am trying to look.
Thanks a bunch for the input and reply.
Aman....
Chris Antognini

Posts: 1,039
Registered: 03/20/99
Re: Mutexes and Latches....
Posted: Feb 6, 2008 11:56 AM   in response to: Aman.... in response to: Aman....
Click to report abuse...   Click to reply to this thread Reply
Aman

acquired in the same mode of immediate or willing to wait correct ?

Yes.

The faster I am not sure still why.

The code path to get and release them is shorter.

This can be leading to an assumption to a "false contention" sort of thing

The reduced size helps avoiding false contention. In fact, when a latch protects several, independent, structures, false contention might happen. Since mutexes takes less memory, the database engine is able to allocate more of them and, therefore, reduced the likelihood of having false contention.

I am sure besides this , there must be some thing more relevant for
mutexes also there which I am trying to look.

To me it seams that since mutexes can do the same things as latches do by using less memory and in a more performant way, there are already enough good reason for using them!

HTH
Chris
yingkuan

Posts: 10,898
Registered: 10/08/98
Re: Mutexes and Latches....
Posted: Feb 6, 2008 12:27 PM   in response to: Aman.... in response to: Aman....
Click to report abuse...   Click to reply to this thread Reply
First some definition of mutex for people not familiar with the term

http://en.wikipedia.org/wiki/Mutual_exclusion

Second,
Aman,
Can you post an official article the talking about mutex implementation from Oracle?
Hans Forbrich

Posts: 10,458
Registered: 03/13/99
Re: Mutexes and Latches....
Posted: Feb 6, 2008 2:41 PM   in response to: yingkuan in response to: yingkuan
Click to report abuse...   Click to reply to this thread Reply
Latch is considered a type of [generic definition of] Mutex.

However, in some recent docs, I have noticed that Oracle is starting to talk about mutex as a new lightweight mututla exclusion memory structure and code as a substitute for latch as described by Chris.

One reference is Note:433631.1
Tanel Poder

Posts: 178
Registered: 07/06/98
Re: Mutexes and Latches....
Posted: Feb 6, 2008 8:10 PM   in response to: Hans Forbrich in response to: Hans Forbrich
Click to report abuse...   Click to reply to this thread Reply
In Oracle, latches and mutexes are different things and managed using different modules. KSL* modules for latches and KGX* for mutexes.

As Chris said, general mutex operatins require less CPU instructions than latch operations (as they aren't as sophisticated as latches and don't maintain get/miss counts as latches do).

But the main scalability benefit comes from that there's a mutex structure in each child cursor handle and the mutex itself acts as cursor pin structure. So if you have a cursor open (or cached in session cursor cache) you don't need to get the library cache latch (which was previously needed for changing cursor pin status), but you can modify the cursor's mutex refcount directly (with help of pointers in open cursor state area in sessions UGA).

Therefore you have much higher scalability when pinning/unpinning cursors (no library cache latching needed, virtually no false contention) and no separate pin structures need to be allocated/maintained.

Few notes:
1) library cache latching is still needed for parsing etc, the mutexes address only the pinning issue in library cache
2) mutexes are currently used for library cache cursors (not other objects like PL/SQL stored procs, table defs etc)
3) As mutexes are a generic mechanism (not library cache specific) they're used in V$SQLSTATS underlying structures too
4) When mutexes are enabled, you won't see cursor pins from X$KGLPN anymore (as X$KGLPN is a fixed table based on the KGL pin array - which wouldn't be used for cursors anymore)

--
Tanel Poder
http://blog.tanelpoder.com
Aman....

Posts: 9,551
Registered: 05/20/01
Re: Mutexes and Latches....
Posted: Feb 7, 2008 7:41 AM   in response to: yingkuan in response to: yingkuan
Click to report abuse...   Click to reply to this thread Reply
Yingkuan,
I dont have the official aricle with me to prove that Oracle is doing mutex implementation. I tried to search alot for this details but I wasnt able to find relevant details so I asked, Believe me ( I know its not good to say, proof is required) but its true from 10g.
Aman....
Aman....

Posts: 9,551
Registered: 05/20/01
Re: Mutexes and Latches....
Posted: Feb 7, 2008 7:44 AM   in response to: Tanel Poder in response to: Tanel Poder
Click to report abuse...   Click to reply to this thread Reply
Tanel,
Excellent! Thats a good explanation. Thanks for it. I got one document sent to me just now which explains in good detail about mutexes and their benefits in Oracle. Combining that with your info,its a good one for me for now. Thanks for the explanation.
Regards,
Aman....
Aman....

Posts: 9,551
Registered: 05/20/01
Re: Mutexes and Latches....
Posted: Feb 7, 2008 8:09 AM   in response to: Chris Antognini in response to: Chris Antognini
Click to report abuse...   Click to reply to this thread Reply
Chris,
First thanks for the reply. yes I agree with the part that mutex are smaller and also they remove the "false contention" part which can be there with latches.
To me it seams that since mutexes can do the same things as latches do by using less memory and in a more performant way, there are already enough good reason for using them!
right:-).Tanel gave a good explanation of it. Also I got a good document about it just now.
Thanks for the explanation.
Aman....
543609

Posts: 775
Registered: 11/16/06
Re: Mutexes and Latches....
Posted: Feb 7, 2008 8:14 AM   in response to: Tanel Poder in response to: Tanel Poder
Click to report abuse...   Click to reply to this thread Reply
Excellent answer, as usual. My response was directed to yingkuan's wikipedia reference, which says that a mutex is a generic mutual exclusion device, including semaphores and latches.
yingkuan

Posts: 10,898
Registered: 10/08/98
Re: Mutexes and Latches....
Posted: Feb 7, 2008 1:55 PM   in response to: Aman.... in response to: Aman....
Click to report abuse...   Click to reply to this thread Reply
Yingkuan,
I dont have the official aricle with me to prove that
Oracle is doing mutex implementation. I tried to
search alot for this details but I wasnt able to
find relevant details so I asked, Believe me ( I know
its not good to say, proof is required) but its true
from 10g.
Aman....

Understand. I also failed to find good article to discuss this topic in detail. I assume Oracle thought this too technical to post to general public.

Nonetheless, Tanel seems give a good explanation .
Aman....

Posts: 9,551
Registered: 05/20/01
Re: Mutexes and Latches....
Posted: Feb 8, 2008 7:44 PM   in response to: yingkuan in response to: yingkuan
Click to report abuse...   Click to reply to this thread Reply
No issues yingkuan.I did search than only I posted. I have got a generic document.if you and anyone else wants, buzz me. Its not really like the Tanel's explanation but still much better as compared to any other doc that I did read about it.
Aman....
Legend
Guru Guru : 2500 - 1000000 pts
Expert Expert : 1000 - 2499 pts
Pro Pro : 500 - 999 pts
Journeyman Journeyman : 200 - 499 pts
Newbie Newbie : 0 - 199 pts
Oracle ACE Director
Oracle ACE Member
Oracle Employee ACE
Helpful Answer (5 pts)
Correct Answer (10 pts)

Point your RSS reader here for a feed of the latest messages in all forums