当前位置: 首页 > 编程日记 > 正文

Oracle Mutex 机制 说明

 

之前也整理过一篇文章来说明Oracle Lock的,参考:

            死锁 阻塞 Latch 等待 详解

            http://blog.csdn.net/tianlesoftware/archive/2010/08/19/5822674.aspx

 

在这篇文章里,提到了System Locks,它包含:

            1Latches

            2Mutexes

            3Internal Locks

 

官方文档上关于Mutex 的说明如下

 

Mutexes

            A mutual exclusion object (mutex) is a low-level mechanism that prevents an object in memory from aging out or from being corrupted when accessed by concurrent processes. A mutex is similar to a latch, but whereas a latch typically protects a group of objects, a mutex protects a single object.

 

Mutexes provide several benefits:

1A mutex can reduce the possibility of contention.

            Because a latch protects multiple objects, it can become a bottleneck when processes attempt to access any of these objects concurrently. By serializing access to an individual object rather than a group, a mutex increases availability.

2A mutex consumes less memory than a latch.

3When in shared mode, a mutex permits concurrent reference by multiple sessions.

 

 

.  eygle blog上搜的信息

            Mutex 的发音是 /mjuteks/ ,其含义为互斥(),这个词是Mutual Exclude的缩写。

            Mutex在计算机中是互斥也就是排他持有的一种方式,和信号量-Semaphore有可以对比之处。有人做过如下类比:

 

            Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个。一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行。

 

            Semaphore是一件可以容纳N人的房间,如果人不满就可以进去,如果人满了,就要等待有人出来。对于N=1的情况,称为binary semaphore一般的用法是,用于限制对于某一资源的同时访问。

 

 

对于Binary semaphoreMutex,这两者之间就存在了很多相似之处:

            在有的系统中Binary semaphoreMutex是没有差异的。

            在有的系统上,主要的差异是mutex一定要由获得锁的进程来释放。而semaphore可以由其它进程释放(这时的semaphore实际就是个原子的变量,大家可以加或减),因此semaphore可以用于进程间同步

            Semaphore的同步功能是所有系统都支持的,而Mutex能否由其他进程释放则未定,因此建议mutex只用于保护critical section。而semaphore则用于保护某变量,或者同步。

 

 

.  google的其他信息

 

Blog搜的信息如下:

            http://www.hellodb.net/2010/06/oracle-mutex.html

 

            LatchOracle用来在内存中做串行控制的机构,从10g R2开始,Oracle引入了一个新的技术MutexMutex并不是Oracle的发明,而是系统提供的一个底层调用,Oracle只是利用它实现串行控制的功能,并替换部分Latch

           

            注意这里是部分替代:

            Lateches can't be replaced by Mutex and Mutex are not replaced by latches. latches have more functionality than mutex. It is like enqueue is big ompared to lateches, lateches bigger than mutex. each and every low level serailaization device is seperate and have seperate roles.

 

            Mutex是操作系统为了实现PV操作提供的原子功能,P/V两个操作都必须是原子的,才能保证并发不被打乱,与其相似的还有信号量Semaphore,这些都是操作系统原理上的东西了

 

            Mutex中有两个变量:分别是Holider identiferReference countHolider identifer记录持有mutexSID,而Reference count是一个计数,记录了当前正在以share方式访问mutex的数量,每当sessionshare方式持有mutex时,计数会加1,而释放时会减1如果Reference count大于零,则表示该内存结构正在被Oracle pin住。

 

我们看一段伪代码,演示mutex的申请过程:

Function Mutex_get(mutex_name)

{

  if mutex.holder:=SID

    case mode:

    'exclusive':

      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

}

 

            Mutex是如何实现串行控制的,实际上它是利用了操作系统的一个原子操作CAS(compare-and-swap)实现的

            我们看到函数的开始处:mutex.holder:=SID,将SID赋值给mutexHolider Identifer,这里就是一个原子的CAS操作,首先比较mutex.holder是否为空,如果不为空则赋值sessionSIDCAS操作由OS来保证其原子性,在同一时刻这个操所是串行的。

            如果这个赋值操作失败,整个申请过程失败。赋值成功后,如果是share方式,则mutex.ref_count1,并清空mutex.holder如果是exclusive方式,需要判断mutex.ref_count是否为零(是否被pin住),如果大于0,则失败,并清空mutex.holder,如果等于0,则成功,这时不清空mutex.holder,保持当前sessionmutexexclusive占用,直到释放为止。

 

Mutex相比latch带来了以下的好处:

            1.更少的资源消耗mutexlatch不同,它不是独立存在的,而是在每个内存结构中,并随着内存结构创建和释放,mutex同时也被创建和释放。mutex暂用的空间比latch小很多,创建和释放消耗更少的资源。

 

            2.有效降低竞争,因为mutex是每个内存结构中的一部分,这样意味着mutex的数量可以有很多,而不同于latch一个latch需要管理很多个内存结构,当你访问同一latch管理的不同内存结构时,也会发生竞争,而mutex则不会

            另外,因为latch的数量有限,很多时候latch本身的竞争会很厉害,之前,我们只能增加latch数量或者减少latch持有的时间,而现在,mutex是一个更好的选择。

 

            3. 更快的pinblock被访问时,它必须被pinbuffer cache中,当一个cursor执行时,它也必须被pinlibrary cache中,如果大量并发频繁执行同一个cursorlibrary cache pin会耗费大量的CPU资源。

            mutex使用reference count来解决并发访问的问题,只要它大于零,就表示它已经被pin在了内存中,不能被交换出去。而且mutex.ref_count++这个操所是非常快的,只占用非常少的资源。

           

            Mutex申请的过程和latch类似,同样需要spinsleep,不同的是Oracle硬编码了mutex spin的次数为255次(Latch spin的次数默认为2000,由隐含参数_spin_count控制)。

            latch sleep会随着等待次数的逐步增加,每次sleep的时间也会逐步增加。    mutex sleep则比较特别,它有三个选项,分别是yield CPUsleep或者block other process,允许开发人员来决定采用哪种选项。

 

            由于在某些RISC的操作系统中(HP-UNIX),由于系统不支持CAS操作,Oracle通过创建一个latch pool来模拟了CAS操作,被称为KGX latch,如果你发现系统中存在这种latch竞争,说明操作系统不支持CAS操作,可以通过_kks_use_mutex_pin关闭mutex

            mutex主要使用在library cache中,用来取代原来的library cache pinlibrary cache lock

 

            KGX mutexes are not OS mutexes, ORACLE KGX Mutex 同样是基于spinlock构建.

 

 

Data 1

            things get more fun in 10.2, you can pin cursors without getting library cache pin latch, using KGX mutexes. Mutexes are new in 10.2 and they enable shared access to objects in somewhat similar manner to shared latche; every successful get of a particular mutex will increment its value and a release will decrement. When the count is zero, no one has the mutex and it is safe to get it in exclusive mode. However, they are more fine-grained than kgl latches and provide a better wait mechanism, as far as I understand.
            So if your environment supports atomic compare and swap operation (such as CMPXCHG on Intel), you might get away without cursor_space_for_time setting for ultrahigh execution rates. Otherwise the atomic mutex operations would be achieved using the new KGX latches.

            At least on my laptop this feature isn't enabled by default (from an OracleWorld paper I remember that it should become default in 10.2.0.2), but so far you can experiment with it if you set _kks_use_mutex_pin = true and bounce the instance (mutex structures will be stored in the shared pool, so you might need to increase shared pool size).

 

            There are also X$MUTEX_SLEEP and X$MUTEX_SLEEP_HISTORY fixed tables that can show some interesting information if you generate some mutex waits into them.

 

Data 2

            Mutex is the short form mutual exclusion object. A mutex, similar to a latch, is a low-level serialization mechanism used to control access to a shared data structure in the SGA.

 

2.1 Serialization is required to avoid an object being:

1 Deallocated while someone is accessing it

2 Read while someone is modifying it

3 Modified while someone is modifying it

4 Modified while someone is reading it

 

2.2 Mutexes can be defined and used in different ways, as in the following examples:

            1Each structure being protected by a mutex can have its own mutex (for example, a parent cursor has its own mutex, and each child cursor has its own mutex)

            2Each structure can be protected by more than one mutex, with each mutex protecting a different part of the structure.

            3 A mutex can protect more than one structure.

 

2.3 Although mutexes an latches are both serialization mechanisms, mutexes have certain features that latches do not:

1Smaller and Faster

            Mutexes are an alternative to latches because they are smaller and much faster to get. A mutex get uses fewer instructions compared to a latch get. A mutex takes less memory space compared to a latch.

2 Less Potential for False Contention

            Latch typically protect multiple objects. When a latch protects one or more hot objects, the latch itself can become a serialization point when accessing any of the objects protected by that latch. This can be a false contention point, where the contention is for the protection mechanism (that is, latch), rather than the target object you are attempting to access. Unlike latches, with mutexes it is possible to create mutex for each structure protected. This mean that false contention is much less likely because each structure can be protected by its own mutex.

3Replace Latches and Pins

            A mutex can be concurrently referenced by many sessions, providing all sessions reference the mutex in S (Shared) mode. The total number of sessions referencing a mutex in S mode is called the reference count ("ref count"). The ref count for a mutex is stored within the mutex itself. A mutex can also be held in X (eXclusive) mode by one session only.

            Mutexes have a dual nature; they can act as a serialization mechanism (for example, latch) and also as a pin (for example, preventing an object from aging out).             For example, the ref count of a mutex is a replacement for a library cache pin. Instead of each session creating and then deleting a library cache pin when executing a cursor, each session increments and decrements the ref count (so the ref count replace n distinct pins).

            Note: Latches and mutexes are independent mechanisms, that is, a process can hold a latch and a mutex at the same time.

 

            2.4  Mutex operations are faster and have less contention than latches, but mutex operations still have waits associated with them. Two V$ view provide detail of mutex sleeps:

            1V$MUTEX_SLEEP shows a summary of sleeps and wait time for particular mutex_type/location combination.

 

SQL>select * from v$mutex_sleep;

MUTEX_TYPE LOCATION SLEEPS WAIT_TIME

-------------------------------- ---------------------------------------- ---------- ----------

Cursor Stat kksIterCursorStat [KKSSTALOC6] 103 163

Cursor Stat kksFindCursorStat [KKSSTALOC3] 23157 36724

Cursor Parent kksfbc [KKSCHLCREA] 1799 10170

Cursor Parent kkspsc0 [KKSPRTLOC27] 26 627

Cursor Parent kkspsc0 [KKSPRTLOC26] 122 2872

Cursor Parent kkshbbr [KKSPRTLOC15] 660 1779

Cursor Parent kksLoadChild [KKSPRTLOC4] 1181 6932

Cursor Parent kksfbc [KKSPRTLOC2] 9006 34053

Cursor Parent kksfbc [KKSPRTLOC1] 2831 144439

Cursor Pin kksLockDelete [KKSCHLPIN6] 5021 1055990

Cursor Pin kkslce [KKSCHLPIN2] 265549 2792810468

Cursor Pin kksfbc [KKSCHLPIN1] 1203 5132409

Cursor Pin kksfbc [KKSCHLFSP2] 9279 56902065

 

            2V$MUTEX_SLEEP_HISTORY show sessions sleeping for a particular mutex_type/location combination by time while it is held by a specific holding session.

 

2.5 Mutex wait event have two categories:

            1cursor:mutex indicates that the mutex waits on parent cursor operations and statistics block operations.

            2cursor:pin events are wait for cursor pin operations, where a mutex has replaced the latch:library cache pin.

 

2.6 Mutex wait events are of two types

            1Short-duration events that should rarely be seen. These occur when one process attempts to update the mutex while it is being changed by another process. The waiting process will spin waiting for the mutex to be available. For example, cursor:pin S is incremented when another process is updating the reference count(pin) of shared cursor.

            2 Long-duration events occur when a process must wait for other processes to finish their operation. For example, cursor:mutex X is incremented when a process wants an exclusive access but the mutex is being held exclusive or shared by another process.

 

2.7 Mutex-Protected Operations:

            A mutex is another protection mechanisms that can protect critical operations. From Oracle database V. 10.2.0.2 and later, a SELECT from the V$SQLSTATS view is protected by mutexes. The use of mutex-protected operations is significantly faster than latched operations. The child cursor lists are protected by mutexes.

 

 

小结:

            这些资料先是看了一遍,然后整理了一篇。 感觉还是有点没吃透。 Oracle 内存这块的东西还需要深入研究。 先这样吧。以后有新理解的时候在修改。

 

 

 

 

 

 

 

-------------------------------------------------------------------------------------------------------

Blog http://blog.csdn.net/tianlesoftware

Email: dvd.dba@gmail.com

DBA1 群:62697716();   DBA2 群:62697977()   DBA3 群:62697850()  

DBA 超级群:63306533();  DBA4 群: 83829929  DBA5群: 142216823   

DBA6 群:158654907  聊天 群:40132017   聊天2群:69087192

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

转载于:https://www.cnblogs.com/sqlite3/archive/2011/05/30/2568142.html

相关文章:

POJ-1860-Currency Exchange

链接:https://vjudge.net/problem/POJ-1860 题意: 有N个点,支持货币兑换,从货币a->b手续费c,汇率r。 求能否换一圈使总净额增加。 思路: bellman-ford。 找一个正权回路。 代码: #include &l…

如果asp.net mvc中某个action被执行了两次,请检查是不是以下的原因

注释 <link rel"icon" href"#"> 这一句后试试转载于:https://www.cnblogs.com/lummon/p/4559185.html

vue常见错误汇总(自看)

解决办法汇总 eslint: Expected indentation of 2 spaces but found 4缩进报错 &#xff0c;所有缩进只能用两个空格 Newline required at end of file but not found需要在最后的后面再加一行!!! Missing space before value for key ‘name’在关键字“值”之前缺少空格 …

IOS的钥匙串,确保本地隐私数据的安全

* 苹果的"生态圈"&#xff0c;钥匙串访问&#xff0c;使用 AES 256 加密算法&#xff0c;能够保证用户密码的安全 * 钥匙串访问SDK&#xff0c;是苹果在 iOS 7.0.3 版本以后公布的 * 钥匙串访问的接口是纯 C 语言的&#xff0c;但是&#xff0c;网络上有框架把它封装…

【转】PendingIntent的总结

Intent和PendingIntent的关系&#xff0c;初学的时候很迷惑&#xff0c;用PendingIntent的时候&#xff0c;还会出现奇怪的问题&#xff0c;比如无法传递数据&#xff0c;无法更新数据&#xff0c;所以我集众家之长&#xff0c;加上我个人的一些实践&#xff0c;总结如下&#…

CentOS 7 安装 GlusterFS

目录 环境说明&#xff1a; 3台机器安装 GlusterFS 组成一个集群。 使用 docker volume plugin GlusterFS 服务器&#xff1a; 10.6.0.140 10.6.0.192 10.6.0.196转载于:https://www.cnblogs.com/MeiCheng/p/10274222.html

npm安装less报错 rollbackFailedOptional: verb npm-session

解决办法 在cmd中依次输入然后回车 &#xff08;1&#xff09; npm install -g cnpm --registryhttps://registry.npm.taobao.org &#xff08;2&#xff09; cnpm install less less-loader --save-dev

Online Judge上陪审团选人问题用Java实现的一个AC解

原问题位于&#xff1a;http://poj.org/problem?id1015 以下为问题描述的摘录&#xff1a; In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a j…

【D3】transition API

摘要&#xff1a; 动画类API 一、API 使用 1. 1 d3.ease 1.2 d3.timer Start a custom animation timer, invoking the specified function repeatedly until it returns true. There is no way to cancel the timer after it starts, so make sure your timer function return…

微信小程序 - 富文本图片宽度自适应(正则)

引言&#xff1a;在微信小程序里&#xff0c;比如商品展示页面的商品详情会有图片展示&#xff0c;PC端设置的商品详情是PC端的宽度&#xff0c;所以在小程序里图片会显示不全&#xff0c;这时就应该做相应的处理&#xff0c;使小程序里图片显示正确 思路 把图片的宽度改为手机…

(1)01背包问题

#include <stdio.h> #define N 6 #define M 21 #define W 20 int dp[N][M]; int w[N] {0}; int v[N] {0}; void knapsack(){int i, j;int value1, value2;for(i 1; i < N; i){ // 前i件物品for(j 1; j < N; j){ // 背包剩余空间if(w[i] > j){ // 第i件物品太…

Web.Config文件配置之限制上传文件大小和时间

在邮件发送系统或者其他一些传送文件的网站中&#xff0c;用户传送文件的大小是有限制的&#xff0c;因为这样不但可以节省服务器的空间&#xff0c;还可以提高传送文件的速度。下面介绍如何在Web.Config文件中配置限制上传文件大小与时间。 在Web.Config文件中配置限制上传文件…

成为软件高手的几个忌讳

1&#xff09; 不会英语&#xff1a;CS源于美国&#xff0c;重量级的文档都是英文的。不会英语&#xff0c;那么你只能忍受拙劣的翻译和大延迟的文档&#xff08;翻译出来的文档几乎都是很久以前出版的东西&#xff09;。2&#xff09; 急于求成&#xff1a;什么都没学习就开始…

Linux下的redis的持久化,主从同步及哨兵

redis持久化 Redis是一种内存型数据库&#xff0c;一旦服务器进程退出&#xff0c;数据库的数据就会丢失&#xff0c; 为了解决这个问题&#xff0c;Redis提供了两种持久化的方案&#xff0c;将内存中的数据保存到磁盘中&#xff0c;避免数据的丢失。 RDB持久化 redis提供了RDB…

vue/require-v-for-key]Elements in iteration expect to have ‘v-bind:key‘ directives

报错内容&#xff1a;[vue/require-v-for-key]Elements in iteration expect to have v-bind:key directives.解决&#xff1a;加上v-bind:key"index"<p>主演:<!--显示电影主演&#xff0c;循环--><span v-for"(actor, index) in scope.row.act…

@Ignore_JUnit - Ignore Test

Ignore 用法很简单, 如果你的测试用例还没有准备好而不想被执行, 又不想删掉或注释掉, 可以使用 Ignore 标注来忽略测试。 方法一旦用 Ignore 注解了将不会被执行. 如果一个类用 Ignore 注解了 他下面的所有测试方法将不会被执行. 看个应用 Create a Class Create a java clas…

Redis5.0之Stream案例应用解读

2019独角兽企业重金招聘Python工程师标准>>> 非常高兴有机会和大家在这里交流Redis5.0之Stream应用。今天的分享更多的是一个抛砖引玉&#xff0c;欢迎大家提出更多关于Redis的思考。 首先&#xff0c;我们来个假设&#xff0c;这里有个杯子&#xff0c;这个杯子是去…

往阿里云服务器上安装Mysql

在安装完Mysql后要进行登录&#xff0c;这时候显示让你输密码&#xff0c; 我点击键盘发现怎么没有密码出现&#xff0c; 然后我就把Mysql卸载了重新装的&#xff0c;折腾了2个多小时&#xff0c; 最后&#xff0c;我突然想试试直接输密码然后就回车&#xff0c; 发现成功进入数…

PHP中spl_autoload_register函数的用法

spl_autoload_register(PHP 5 > 5.1.2)spl_autoload_register — 注册__autoload()函数说明bool spl_autoload_register ([ callback $autoload_function ] )将函数注册到SPL __autoload函数栈中。如果该栈中的函数尚未激活&#xff0c;则激活它们。如果在你的程序中已经实现…

Linux初步——常用简单命令

散乱的记录&#xff0c;目前是边学边用&#xff0c;以后有机会再整理 curl命令 发起一个HTTP请求&#xff0c;如&#xff1a;curl "http://www.baidu.com" 加上-I选项查看HTTP协议头的信息&#xff0c;如&#xff1a;curl "http://www.baidu.com" -I Linux…

Centos-Mysql配置my.cnf内容

#v1.0 [mysqld] #通用 #skip-grant-tables 跳过授权密码登录 port3306 #使用mysql系统账号操作进程 usermysql socket/var/lib/mysql/mysql.sock #basedir/usr datadir/var/lib/mysql #mysql错误日志 log_error /tmp/ch_mysql_log/error.log #mysql所有操作日志 生产服务器不…

本地navicat连接阿里云数据库

自己起连接名字&#xff1b; prot:填公网IP&#xff08;服务器给的&#xff09; password&#xff1a;填阿里云数据库的密码

adb logcat命令查看并过滤android输出log

adb logcat命令查看并过滤android输出log cmd命令行中使用adb logcat命令查看android系统和应用的log&#xff0c;dos窗口按ctrlc中断输出log记录。 logcat日志中的优先级/tag标记&#xff1a; android输出的每一条日志都有一个标记和优先级与其关联。 优先级是下面的字符&…

重读TCP协议(3)

重读TCP协议&#xff08;3&#xff09; TCP 的数据流TCP的数据流大致可以分为两类&#xff0c;交互数据流与成块的数据流。交互数据流就是发送控制命令的数据流&#xff0c;比如relogin&#xff0c;telnet&#xff0c;ftp命令等等&#xff1b;成块数据流是用来发送数据的包&…

拥有2000家门店,他如何晋升为服装界的新宠?

—— iwarm3.0加热组件、碳纳米管膜炎、管状石墨结构体...你看到并不是一款高科技电子产品&#xff0c;这是快鱼服饰在这个冬天推出的黑科技产品 - 智能温控羽绒服。 在竞争激烈的服装行业&#xff0c;快鱼&#xff08;Fast Fish&#xff09;将“快时尚”的理念推广至全国&…

navicat连接云数据库报错2003,2005

一开始报2003&#xff0c;好吧&#xff0c;是Mysql挂掉了&#xff0c; 然后重启Mysql服务 systemctl restart mysqld.service #重启 mysql然后再连接&#xff0c;报错2005&#xff0c; 好吧&#xff0c;是复制ip的时候多了一个空格&#xff0c;再输入一次即可 我服了。

基础学习总结(四)--SQLite

1. SQLiteDatabase操作SQLite数据库的类。可以执行SQL语句&#xff0c;对数据库进行增、删、查、改的操作。也可以进行transaction的控制。很多类对数据库的操作最终都是通过SQLiteDatabase实例来调用执行的。需要注意的是&#xff0c;数据库对于一个应用来说是私有的&#xff…

用node实现websocket协议

协议 WebSocket是一种基于TCP之上的客户端与服务器全双工通讯的协议&#xff0c;它在HTML5中被定义&#xff0c;也是新一代webapp的基础规范之一。 它突破了早先的AJAX的限制&#xff0c;关键在于实时性&#xff0c;服务器可以主动推送内容 到客户端&#xff01;可能的应用有&a…

反向春运成为新趋势 客流年增9%

资料图&#xff1a;春运。殷立勤 摄 中新社北京1月18日电 (记者 周音)近年来&#xff0c;反向春运成为新趋势。中国铁路总公司18日披露&#xff0c;反向春运客流以年增9%左右的速度增长。 传统春运是大城市返乡回家过年。反向春运是年轻人选择将老家的父母和孩子接来自己工作的…

把.sql文件上传到服务器上

使用xftp工具&#xff0c;在root文件夹下新建myProject文件夹&#xff0c;然后把.sql文件拖拽过去即可。 进入xshell&#xff0c; 进入到mysql&#xff1a; mysql -u root -p输入密码&#xff1a; create database 数据库名&#xff1b;use 数据库名&#xff1b;source ~/新…