Linux multi-process binding issues

Source: Internet
Author: User

Hardware interrupts occur frequently, is a very CPU-intensive thing, in multi-core CPU conditions if there is a way to allocate a large number of hardware interrupts to different CPU (core) processing is clearly a good balance of performance. Now the server is still multi-CPU multi-core, multi-card, multi-disk, if you can interrupt the network card exclusive 1 CPU (CORE), Disk IO interrupt exclusive 1 CPUs will greatly reduce the burden of a single CPU, improve the overall processing efficiency. The day before yesterday I received an email from a netizen referring to the SMP IRQ Affinity, which sparked the topic of today. The following operations are performed on the SUN fire X2100 M2 Server + 64-bit version of CentOS 5.5 + Linux 2.6.18-194.3.1.el5.

What is an interrupt

Chinese textbooks on the definition of "interrupted" too blunt, simply said, each hardware device (such as: Hard disk, network card, etc.) need to have some form of communication with the CPU so that the CPU in time to know what happened, so that the CPU may put down the matter to deal with emergency events, hardware devices actively interrupt the CPU The phenomenon can be called hardware interruption, as you are working at the time of QQ interference, the same time QQ shake can be called interruption.

Interrupt is a good way of CPU and hardware communication, there is a way called polling (polling), is to let the CPU timing the hardware status and then do the corresponding processing, as if you every 5 minutes to check QQ to see if there are people looking for you, this way is not very wasteful of you (CPU) Time? So interrupts are a hardware-active approach that is more efficient than polling (CPU active).

OK, here's another problem, every hardware device is interrupted, so how do you differentiate between different hardware? How do you know which interrupts are from the hard disk and which one comes from the NIC? This is easy, not every QQ number is different? Similarly, each hardware device on the system is assigned an IRQ number , this unique IRQ number makes it possible to differentiate between Zhang San and Li Four.

In a computer, an interrupt is an electrical signal that is generated by the hardware and sent directly to the interrupt controller (such as 8259A) and then signaled to the CPU by the interrupt controller, and the CPU detects the signal and interrupts the current work in turn to handle the interrupt. The processor then notifies the operating system that an interrupt has been generated so that the operating system handles the interrupt appropriately. Now take a look at the interrupt controller, there are two common interrupt controllers: Programmable Interrupt controller 8259A and Advanced Programmable Interrupt Controller (APIC), interrupt controller should be taught in the university's hardware interface and computer architecture related courses. Traditional 8259A is only suitable for single CPU, now is multi-CPU multi-core SMP system, so in order to take full advantage of the SMP architecture, the interruption to each CPU on the system to better achieve parallelism and improve performance, Intel introduced the Advanced Programmable Interrupt Controller (APIC).

The hardware support of the Advanced Programmable interrupt controller is not enough, the Linux kernel must also be able to take advantage of these hardware characteristics, so only kernel 2.4 later support the different hardware interrupt request (IRQS) allocated to the specific CPU, this binding technology is called SMP IRQ Affinity. For more information, see the documentation for the Linux kernel source code: Linux-2.6.31.8/documentation/irq-affinity.txt

How to use

Let's see how the interrupts on the system are distributed on the CPU, and it's clear that the CPU0 is dealing with more interrupts:

# cat/proc/interrupts

CPU0 CPU1

0:918926335 0 Io-apic-edge Timer

1:2 0 Io-apic-edge i8042

8:0 0 Io-apic-edge RTC

9:0 0 Io-apic-level ACPI

12:4 0 Io-apic-edge i8042

14:8248017 0 Io-apic-edge Ide0

50:194 0 Io-apic-level OHCI_HCD:USB2

58:31673 0 Io-apic-level Sata_nv

90:1070374 0 Pci-msi eth0

233:10 0 Io-apic-level EHCI_HCD:USB1

nmi:5077 2032

loc:918809969 918809894

err:0

mis:0

In order not to let CPU0 very tired how to transfer the partial interruption to the CPU1? or how to transfer the ETH0 network card interrupt to CPU1? First look at the SMP affinity for IRQ 90 interrupts to see how the current interrupts are distributed on different CPUs (ffffffff means Allocated on all available CPUs):

# cat/proc/irq/90/smp_affinity

7fffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff,ffffffff

Before we go any further, we need to stop the IRQ auto-tuning process so that we can manually bind the IRQ to different CPUs, otherwise the custom-made changes will be overwritten by the auto-tuning process. If you want to modify the interrupt handling for IRQ 90, bind to the 2nd CPU (CPU1):

#/etc/init.d/irqbalance Stop

# echo "2" >/proc/irq/90/smp_affinity

After a while watching/proc/interrupts, is not 90:eth0 on the CPU1 on the interruption increased (145), the interruption in the CPU0 has not changed? Printing/proc/interrupts will find that the number of interrupts eth0 on CPU0 remains constant, while The number of interrupts on the CPU1 continues to increase, which is exactly what we want:

# cat/proc/interrupts

CPU0 CPU1

0:922506515 0 Io-apic-edge Timer

1:2 0 Io-apic-edge i8042

8:0 0 Io-apic-edge RTC

9:0 0 Io-apic-level ACPI

12:4 0 Io-apic-edge i8042

14:8280147 0 Io-apic-edge Ide0

50:194 0 Io-apic-level OHCI_HCD:USB2

58:31907 0 Io-apic-level Sata_nv

90:1073399 145 Pci-msi Eth0

233:10 0 Io-apic-level EHCI_HCD:USB1

nmi:5093 2043

loc:922389696 922389621

err:0

mis:0

What's the use?

In the case of the network is very heavy, for the file server, high-traffic Web server such applications, the different network card IRQ equalization to different CPUs will reduce the burden on a certain CPU, improve the overall processing of multiple CPUs the ability to interrupt; For applications such as database servers, the disk Binding the controller to one CPU and tying the NIC to another CPU will increase the response time of the database and optimize performance. A reasonable balance of IRQ interrupts based on your production environment and application characteristics can help improve the overall throughput and performance of the system.

I often received a letter asked how to optimize the Linux, the optimization of the VPS, the problem is not very good answer, to remember is that performance optimization is a process rather than the result, not to see some of the document changed the parameters called optimization, the latter also require a lot of testing, monitoring and continuous observation and improvement.

Bind processes to different CPUs

Describes how to bind hardware interrupts to different CPUs under Linux multicore, in fact, you can use a similar approach to manually assign the process to a specific CPU, usually running on Linux all kinds of processes are distributed and managed by the Linux kernel, the process scheduling algorithm to determine which process can begin to use CPU, which process needs to sleep or wait, which process runs on which CPU. If you are interested in the kernel and process scheduler of the operating system, take a look at the classic Operating Systems Design and implementation (Linus Torvalds was inspired by the book and wrote Linux), Starting from a simple Minix, hack kernel is a very interesting thing, I have previously modified the Minix kernel process scheduling, learned a lot of the kernel aspect of things. Also recommend an extra-curricular reading: Just for Fun,linus Torvalds wrote an autobiography.

Linux provides us with a handy tool for manually allocating processes to different CPUs (CPU Affinity) so that we can schedule specific processes to specific CPUs based on server and application features, such as Oracle consuming a lot of CPU and I/O resources, if we can allocate Oracle processes to one or more CPUs and is dedicated to Oracle by these CPUs will undoubtedly improve application responsiveness and performance. There are also special cases where an application must be bound to a CPU, such as a single CPU for a particular software, and if it wants to run on a multi-CPU machine, it must restrict the software to one CPU.

Installing Schedutils

Install the schedutils under Centos/fedora:

# yum Install Schedutils

Install the schedutils under Debian/ubuntu:

# Apt-get Install Schedutils

If you are using the latest version of Centos/fedora/debian/ubuntu, the Schedutils/util-linux package may already be installed.

Calculate CPU Affinity and compute SMP IRQ Affinity almost:

0x00000001 (CPU0)

0x00000002 (CPU1)

0x00000003 (CPU0+CPU1)

0x00000004 (CPU2)

...

Using Schedutils

If you want to set the process number (PID) to 12212 of the process to CPU0:

# Taskset 0x00000001-p 12212

Linux multi-process binding issues

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.