mysql 自旋锁

 自旋(spin)是一种通过不间断地测试来查看一个资源是否变为可用状态的等待操作,用于仅需要等待很短的时间等待所需资源的场景。使用自旋这种“空闲循环(busy-loop)”来完成资源等待的方式要比通过上下文切换使线程转入睡眠状态的方式要高效得多。但如果自旋了一个很短的时间后其依然无法获取资源,则仍然会转入前述第二种资源等待方式。

innodb_sync_spin_loops参数是自旋锁的轮转数,可以通过show engine innodb status来查看。相较于系统等待,自旋锁是低成本的等待;不过它是一个活跃的等待,会浪费一些cpu资源。因此如果看到大量的自旋等待和自旋轮转,则很显然它浪费了很多cpu资源。浪费cpu时间和无谓的上下文切换之间可以通过该值来平衡。

innodb_spin_wait_delay

    Vadim Tkachenko 在http://www.mysqlperformanceblog.com/2011/12/02/kernel_mutex-problem-or-double-throughput-with-single-variable/中对该参数进行了测试,并给出了结论。

    Peter Zaitsev在http://www.mysqlperformanceblog.com/2011/07/28/how-innodb-contention-may-manifest-itself/中也进行了解释在不同状况下,该值的影响。并在http://www.mysqlperformanceblog.com/2006/07/17/show-innodb-status-walk-through/中详细解释了show innodb status的输出进行了详细解释。

 

innodb_thread_concurrency

关于innodb_thread_concurrency参数

http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_thread_concurrency

Command-Line Format	--innodb_thread_concurrency=#
Option-File Format	innodb_thread_concurrency
Option Sets Variable	Yes, innodb_thread_concurrency
Variable Name	innodb_thread_concurrency
Variable Scope	Global
Dynamic Variable	Yes
 	Permitted Values
Type	numeric
Default	0
Range	0 .. 1000

InnoDB tries to keep the number of operating system threads concurrently inside InnoDB less than or equal to the limit given by this variable. Once the number of threads reaches this limit, additional threads are placed into a wait state within a FIFO queue for execution. Threads waiting for locks are not counted in the number of concurrently executing threads.

The correct value for this variable is dependent on environment and workload. Try a range of different values to determine what value works for your applications. A recommended value is 2 times the number of CPUs plus the number of disks.

The range of this variable is 0 to 1000. A value of 0 (the default) is interpreted as infinite concurrency (no concurrency checking). Disabling thread concurrency checking enables InnoDB to create as many threads as it needs.

参数的含义是: InnoDB内部的并发线程数.
可以动态修改
具体解析: InnoDB 试图保持InnoDB内部的并发操作系统的线程数少于innodb_thread_concurrency设置的值
如果innodb并发线程数快要到达innodb_thread_concurrency=x,其他的innodb线程会被设置为等待状态,队列的算法是FIFO
处于等待拿锁状态的线程数,不会被计算入正在执行的并发线程数

innodb_thread_concurrency=x,x该设怎样的值,视服务器配置和服务器的负载情况。
默认推荐的值是 (cpu的数量+磁盘数量)x2 (我的理解,对于raid10的磁盘阵列,应该是磁盘总数/2)

参数取值范围 0-1000 当为默认值的时候,不是说0个并发线程。 而是被解释为无限并发(没有并发检查)
当innodb_thread_concurrency=0的时候,可以理解为 禁用线程并发检查,使InnoDB按照请求的需求, 创造尽可能多的线程.

http://www.dbathink.com/2012/10/trouble-shooting-the-high-sys-cpu-in-mysql-server/


并发控制

  并发控制点:

并发控制的目的是最大化提高系统的资源利用率,并减少管理和调度开销。在MySQL实例中,主要处理sql请求,所以期望系统资源最大化提供给sql的执行过程。

  sql的执行牵涉到server层和引擎层:

1. server层:比如cost计算,生成sql执行计划的过程
2. Innodb层:比如根据执行计划,查找和更新数据page的过程

  所以在MySQL实例中,有两个最佳的并发控制点:

1. server层:sql开始执行时。 MySQL在5.6后,在server层引入了thread pool进行并发控制
2. Innodb层:记录查找和记录更新时。 Innodb存储引擎,使用innodb_thread_concurrency参数进行并发控制


  并发控制大小:

设置过大:造成系统调度消耗过大
设置过小:不能完全的使用系统资源,造成资源浪费

  经验值:# Try number of CPU's*2 for thread_concurrency

  但还需要配合具体的平台和业务系统进行测试,才能找到最佳值。


Innodb并发控制

  Innodb使用参数innodb_thread_concurrency控制并发线程的个数,源码中使用一对函数:

innodb_srv_conc_enter_innodb
innodb_srv_conc_exit_innodb

  Innodb实现语句级的并发控制,在语句执行结束,stmt commit的时候,强制释放资源。

权衡和优化

1. 一方面进行并发控制,提高资源利用率,
2. 另一方还需要控制调度公平,防饿死等。

  Innodb引入了n_tickets_to_enter_innodb参数,sql进入innodb执行时进行初始化,默认值500。

  在执行过程中,依次进行递减,递减到0时,强制退出并发线程,重新抢占。

  好处:

1. 一方面单条sql可能写入或者更新多条记录,节省每次enter innodb的线程抢占代价。
2. 另一方面防止单条sql过多的长时间占用并发线程,导致其它线程饿死的情况。
原文地址:https://www.cnblogs.com/yuyue2014/p/4809549.html