OracleMutex Implementation Mechanism

Source: Internet
Author: User
We all know that Latch is the mechanism that Oracle uses for serial control in memory. From 10gR2, Oracle introduced a new technology-Mutex. Mutex is not an Oracle release

We all know that Latch is the mechanism that Oracle uses for serial control in memory. From 10g R2, Oracle introduced a new technology-Mutex. Mutex is not an Oracle release

We all know that Latch is the mechanism that Oracle uses for serial control in memory. From 10g R2, Oracle introduced a new technology-Mutex. Mutex is not an invention of Oracle, but an underlying call provided by the system. Oracle only uses it to implement serial control and replaces some Latch.

Mutex has two variables: Holider identifer and Reference count. The Holider identifer record holds the mutex SID, and the Reference count is a count that records the number of mutex users currently accessing in the share mode, each time a session holds mutex in the share mode, the count is incremented by 1, and 1 is subtracted when the session is released. If Reference count is greater than zero, the memory structure is being occupied by the Oracle pin.

Let's look at a piece of pseudo code to demonstrate the mutex application process:

Function Mutex_get (mutex_name)
{
If mutex. holder: = SID
Case mode:
'Clusive ':
If mutex. ref_count = 0
Return TRUE
Else
Mutex. holder. clear;
Reture FALSE
End if
'Share ':
Mutex. ref_count ++
Mutex. holder. clear
Return TRUE
End case
Else
Reture FALSE
End if
}
How does Mutex implement serial control? In fact, it uses an atomic CAS (compare-and-swap) operation in the operating system. We can see the starting point of the function: mutex. holder: = SID: Assign SID to mutex's Holider Identifer. Here is an atomic CAS operation. First, compare mutex. whether the holder is null. If it is not null, the session SID is assigned. The CAS operation is performed by the OS to ensure its atomicity. At the same time, the operation is serial. If this assignment operation fails, the entire application process fails. After the value is assigned successfully, if the share mode is used, mutex. add ref_count to 1 and clear mutex. holder. for exclusive mode, determine mutex. whether ref_count is zero (whether it is pin). If it is greater than 0, it fails and mutex is cleared. holder. If it is equal to 0, the operation is successful. At this time, the mutex is not cleared. holder to keep the exclusive usage of the current session for mutex until it is released.

Compared with latch, Mutex brings the following benefits:

1. less resource consumption. Unlike latch, mutex does not exist independently. Instead, it is created and released in each memory structure. Mutex currently uses much less space than latch, and creation and release consume less resources.

2. this effectively reduces competition because mutex is a part of each memory structure, which means that there can be a large number of mutex resources. Unlike latch, one latch needs to manage many memory structures, when you access different memory structures managed by the same latch, competition also occurs, while mutex does not. In addition, because of the limited number of latch, latch itself is very competitive. Previously, we had to increase the number of latch or reduce the latch holding time. Now, mutex is a better choice.

3. A faster pin. When a block is accessed, it must be pin in the buffer cache. When a cursor is executed, it must also be pin in the library cache, if the same cursor is frequently executed concurrently, the library cache pin consumes a lot of CPU resources. Mutex uses reference count to solve the problem of concurrent access. If it is greater than zero, it indicates that it has been pin in the memory and cannot be exchanged. In addition, the mutex. ref_count ++ operation is very fast and only occupies a very small amount of resources.

The Mutex application process is similar to latch, and spin and sleep are also required. The difference is that the number of times that the Oracle hard-coded mutex spin is 255 (the default number of Latch spin requests is 2000, controlled by the implicit parameter _ spin_count ). Latch sleep increases gradually with the number of waits, and the time for each sleep increases gradually. Mutex sleep is special. It has three options: yield CPU, sleep, or block other process, allowing developers to decide which option to adopt.

Since in some of the operating systems of some assume (HP-UNIX), since the system does not support CAS operations, Oracle creates a latch pool to simulate CAS operations, known as KGX latch, if you find such latch competition in the system, the operating system does not support CAS operations. You can use _ kks_use_mutex_pin to disable mutex.

Mutex is mainly used in library cache to replace the original library cache pin and library cache lock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.