您的位置:首页 > 其它

Affinity Mask and Process Affinity

2015-11-08 15:05 1356 查看
Affinity 中文翻译是:亲和性,关联,关系密切

CPU 亲和性affinity) 就是进程要在某个给定的 CPU 上尽量长时间地运行而不被迁移到其他CPU的倾向性。

The process must have affinity with at least one processor.

进程必须至少和一个处理器相关。(这是由于Process 必须运行在CPU上)

The association between a processor and a thread is called the processor affinity.

处理器和线程之间的关联称为处理器关联。

在Sql Server中,Process Affinity是指将Scheduler和CPU关联起来,使一个Scheduler绑定到一个特定的CPU上,那么在Scheduler下运行的Worker将会使用指定的CPU来执行Task,而不会切换到其他CPU上去。

Sql Sever 2012 internals:If the Affinity mask configuration option is set, or process affinity has been configured using Alter Server Configuration, each scheduler is mapped to a particular CPU.Thus, each workder is also associated with a single CPU.

Affinity mask:This feature will be removed in a future version of Microsoft SQL Server. Do not use this feature in new development work, and modify applications that currently use this feature as soon as possible. Use ALTER SERVER CONFIGURATION (Transact-SQL) instead.

To carry out multitasking, Microsoft Windows 2000 and Windows Server 2003 sometimes move process threads among different processors. Although efficient from an operating system point of view, this activity can reduce SQL Server performance under heavy system loads, as each processor cache is repeatedly reloaded with data. Assigning processors to specific threads can improve performance under these conditions by eliminating processor reloads and reducing thread migration across processors (thereby reducing context switching); such an association between a thread and a processor is called processor affinity.

SQL Server supports processor affinity by means of two affinity mask options: affinity mask (also known as CPU affinity mask) and affinity I/O mask. For more information on the affinity I/O mask option, see affinity I/O mask Server Configuration Option. CPU and I/O affinity support for servers with 33 to 64 processors requires the additional use of the affinity64 mask Server Configuration Option and affinity64 I/O mask Server Configuration Option, respectively.

查看Sql Server的Affinity 配置

select *
from sys.configurations
where name like '%affinity%'




配置Affinity之后,Scheduler会有两种Status: Online和Offline

If a scheduler is Online, it means the SQL Instance has access to it. Offline, means that you have set CPU Affinity, and SQL is not allowed to use it. The reason it is set to Offline, rather than removed, is because it allows you to change your CPU affinity, without the need to restart the Instance.

1,Affinity mask

The affinity mask option, which existed in earlier releases of SQL Server, dynamically controls CPU affinity.

In SQL Server, the affinity mask option can be configured without requiring a restart of the instance of SQL Server. When you are using sp_configure, you must run either RECONFIGURE or RECONFIGURE WITH OVERRIDE after setting a configuration option. When you are using SQL Server Express, changing the affinity mask option does require a restart.

Changes to the affinity masks occur dynamically, allowing for on-demand startup and shutdown of the CPU schedulers that bind process threads within SQL Server. This can occur as conditions change on the server. For example, if a new instance of SQL Server is added to the server, it may be necessary to make adjustments to the affinity mask option to redistribute processor load.

Modifications to the affinity bitmasks require SQL Server to enable a new CPU scheduler and disable the existing CPU scheduler. New batches can then be processed on the new or remaining schedulers.

To start a new CPU scheduler, SQL Server creates a new scheduler and adds it to the list of its standard schedulers. The new scheduler is considered only for the new incoming batches. Current batches continue to run on the same scheduler. The workers migrate to the new scheduler as they free up, or as new workers are created.

Shutting down a scheduler requires all batches on the scheduler to complete their activities and exit. A scheduler that has been shut down is marked as offline so that no new batch is scheduled on it.

Whether a new scheduler is added or removed, the permanent system tasks such as lockmonitor, checkpoint, system task thread (processing DTC), and signal process continue to run on the scheduler while the server is operational. These permanent system tasks do not dynamically migrate. To redistribute processor load for these system tasks across schedulers, it is necessary to restart the SQL Server instance. If SQL Server attempts to shut down a scheduler associated with a permanent system task, the task continues to run on the offline scheduler (no migration). This scheduler is bound to the processors in the modified affinity mask and should not put any load on the processor it was affinitized with before the change. Having extra offline schedulers, should not significantly affect the load of the system. If this is not the case, a database server reboot is required to reconfigure these tasks.

The I/O affinity tasks (such as lazywriter and logwriter) are directly affected by the I/O affinity mask. If the lazywriter and logwriter tasks are not affinitized, they follow the same rules defined for the other permanent tasks such as lockmonitor or checkpoint.

To ensure that the new affinity mask is valid, the RECONFIGURE command verifies that the normal CPU and I/O affinities are mutually exclusive. If this is not the case, an error message is reported to the client session and to the SQL Server error log, indicating that such a setting is not recommended. Running RECONFIGURE WITH OVERRIDE options allows CPU and I/O affinities that are not mutually exclusive.

If you specify an affinity mask that attempts to map to a nonexistent CPU, the RECONFIGURE command reports an error message to both the client session and the SQL Server error log. Using the RECONFIGURE WITH OVERRIDE option has no effect in this case, and the same configuration error is reported again.

You can also exclude SQL Server activity from processors assigned specific workload assignments by the Windows 2000 or Windows Server 2003 operating system. If you set a bit representing a processor to 1, that processor is selected by the SQL Server Database Engine for thread assignment. When you set affinity mask to 0 (the default), the Microsoft Windows 2000 or Windows Server 2003 scheduling algorithms set the thread's affinity. When you set affinity mask to any nonzero value, SQL Server affinity interprets the value as a bitmask that specifies those processors eligible for selection.

By segregating SQL Server threads from running on particular processors, Microsoft Windows 2000 or Windows Server 2003 can better evaluate the system's handling of processes specific to Windows. For example, on an 8-CPU server running two instances of SQL Server (instance A and B), the system administrator could use the affinity mask option to assign the first set of 4 CPUs to instance A and the second set of 4 to instance B. To configure more than 32 processors, set both the affinity mask and the affinity64 mask. The values for affinity mask are as follows:

A one-byte affinity mask covers up to 8 CPUs in a multiprocessor computer.

A two-byte affinity mask covers up to 16 CPUs in a multiprocessor computer.

A three-byte affinity mask covers up to 24 CPUs in a multiprocessor computer.

A four-byte affinity mask covers up to 32 CPUs in a multiprocessor computer.

To cover more than 32 CPUs, configure a four-byte affinity mask for the first 32 CPUs and up to a four-byte affinity64 mask for the remaining CPUs.

Because setting SQL Server processor affinity is a specialized operation, it is recommended that it be used only when necessary. In most cases, the Microsoft Windows 2000 or Windows Server 2003 default affinity provides the best performance. You should also consider the CPU requirements for other applications when setting the affinity masks. For more information, see your Windows operating system documentation.

When specifying the affinity I/O mask option, you must use it in connection with the affinity mask configuration option. Do not enable the same CPU in both the affinity mask switch and the affinity I/O mask option. The bits corresponding to each CPU should be in one of these three states:

0 in both the affinity mask option and the affinity I/O mask option.

1 in the affinity mask option and 0 in the affinity I/O mask option.

0 in the affinity mask option and 1 in the affinity I/O mask option.

Caution:Do not configure CPU affinity in the Windows operating system and also configure the affinity mask in SQL Server. These settings are attempting to achieve the same result, and if the configurations are inconsistent, you may have unpredictable results. SQL Server CPU affinity is best configured using the sp_configure option in SQL Server.

Example
As an example of setting the affinity mask option, if processors 1, 2, and 5 are selected as available with bits 1, 2, and 5 set to 1 and bits 0, 3, 4, 6, and 7 set to 0, a hexadecimal value of 0x26 or the decimal equivalent of 38 is specified. Number the bits from right to left. The affinity mask option starts counting processors from 0 to 31, so that in the following example the counter 1 represents the second processor on the server.

sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
sp_configure 'affinity mask', 38;
RECONFIGURE;
GO


These are affinity mask values for an 8-CPU system.

Decimal value

Binary bit mask

Allow SQL Server threads on processors

1

00000001

0

3

00000011

0 and 1

7

00000111

0, 1, and 2

15

00001111

0, 1, 2, and 3

31

00011111

0, 1, 2, 3, and 4

63

00111111

0, 1, 2, 3, 4, and 5

127

01111111

0, 1, 2, 3, 4, 5, and 6

255

11111111

0, 1, 2, 3, 4, 5, 6, and 7

The affinity mask option is an advanced option. If you are using the sp_configure system stored procedure to change the setting, you can change affinity mask only when show advanced options is set to 1. After executing the Transact-SQL RECONFIGURE command, the new setting takes effect immediately without requiring a restart of the SQL Server instance.

2,Process affinity

Process Affinity通过Alter Server Configuration命令来修改

This statement does not require a restart of SQL Server. In the case of a SQL Server failover cluster instance, it does not require a restart of the SQL Server cluster resource.

ALTER SERVER CONFIGURATION
SET <process_affinity>
[;]

<process_affinity> ::=
PROCESS AFFINITY
{
CPU = { AUTO | <CPU_range_spec> }
| NUMANODE = <NUMA_node_range_spec>
}
<CPU_range_spec> ::=
{ CPU_ID | CPU_ID  TO CPU_ID } [ ,...n ]

<NUMA_node_range_spec> ::=
{ NUMA_node_ID | NUMA_node_ID TO NUMA_node_ID } [ ,...n ]


Arguments

<process_affinity> ::=

PROCESS AFFINITY
Enables hardware threads to be associated with CPUs.

CPU = { AUTO | <CPU_range_spec> }
Distributes SQL Server worker threads to each CPU within the specified range. CPUs outside the specified range will not have assigned threads.

AUTO
Specifies that no thread is assigned a CPU. The operating system can freely move threads among CPUs based on the server workload. This is the default and recommended setting.

<CPU_range_spec> ::=
Specifies the CPU or range of CPUs to assign threads to.

{ CPU_ID | CPU_ID TO CPU_ID } [ ,...n ]
Is the list of one or more CPUs. CPU IDs begin at 0 and are integer values.

NUMANODE = <NUMA_node_range_spec>
Assigns threads to all CPUs that belong to the specified NUMA node or range of nodes.

<NUMA_node_range_spec> ::=
Specifies the NUMA node or range of NUMA nodes.

{ NUMA_node_ID | NUMA_node_ID TO NUMA_node_ID } [ ,...n ]
Is the list of one or more NUMA nodes. NUMA node IDs begin at 0 and are integer values.

Setting process affinity
The examples in this section show how to set process affinity to CPUs and NUMA nodes. The examples assume that the server contains 256 CPUs that are arranged into four groups of 16 NUMA nodes each. Threads are not assigned to any NUMA node or CPU.

Group 0: NUMA nodes 0 through 3, CPUs 0 to 63

Group 1: NUMA nodes 4 through 7, CPUs 64 to 127

Group 2: NUMA nodes 8 through 12, CPUs 128 to 191

Group 3: NUMA nodes 13 through 16, CPUs 192 to 255

A. Setting affinity to all CPUs in groups 0 and 2
The following example sets affinity to all the CPUs in groups 0 and 2.

ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY
CPU=0 TO 63, 128 TO 191;


B. Setting affinity to all CPUs in NUMA nodes 0 and 7
The following example sets the CPU affinity to nodes 0 and 7 only.

ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY
NUMANODE=0, 7;


C. Setting affinity to CPUs 60 through 200

The following example sets affinity to CPUs 60 through 200.

ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY
CPU=60 TO 200;


D. Setting affinity to CPU 0 on a system that has two CPUs

The following example sets the affinity to CPU=0 on a computer that has two CPUs. Before the following statement is executed the internal affinity bitmask is 00.

ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY
CPU=0;


E. Setting affinity to AUTO
The following example sets affinity to AUTO.

ALTER SERVER CONFIGURATION
SET PROCESS AFFINITY
CPU=AUTO;


参考文档:
https://msdn.microsoft.com/en-us/library/ee210585.aspx https://technet.microsoft.com/zh-cn/library/ms187104(v=sql.110).aspx http://sqlserverdownanddirty.blogspot.com/2011/02/sql-server-schedulers.html http://vishalhsingh.blogspot.com/2013/01/sql-server-handling-cpu-configuration.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: