Knowledge about processes and CPUs and simple and monotonous Solutions

Source: Internet
Author: User
Tags php server

A process is an execution program. Every time a program runs, a process is created for the operating system and corresponding resources are allocated. Processes can be divided into three categories:
1. Interactive Process (I/O)
2. batch processing process (CPU)
3. REAL-TIME Processes)

For an interactive process, generally the cpu time slice is very short, but the priority is high. The batch process occupies a long cpu time slice, but the priority is low. The real-time process is used by the kernel, the priority is higher than the previous two types.

As mentioned above, linux processes have priorities, which are generally divided into two types:
1. Real-time priority
2. Static Priority

The value range of the real-time priority is 1-99. The lower the value, the lower the priority, which is generally used by the system kernel. The value range of the static priority is 100-139, the lower the value, the higher the priority. Generally, it is used by the application. You can use the nice value to adjust the static priority. The value range is-20 to 19, corresponding to 100-139; the real-time priority is higher than the static priority.

Linux supports three types of internal scheduling:
Real-time processes support two types of scheduling:
1. SCHED_FIFO: first in first out
2. SCHED_RR: round robin
FF-type processes are called through SCHED_FIFO; RR-type processes are called through SCHED_RR.
A user process supports one type of scheduling:
1. SCHED_OTHER
OS-type processes are called through SCHED_OTHER.

So how can I view the information used by the current operating system process? You can use the following command to view
# Ps-e-o class, rtprio, pri, nice, command

CLS RTPRIO PRI NI COMMAND
TS-24 0 init [3]
FF 9999 139-[migration/0]
TS-5 19 [ksoftirqd/0]
TS-29-5 [events/0]
TS-28-5 [khelper]
TS-29-5 [kthread]
TS-29-5 [kblockd/0]
TS-19-5 [kacpid]
TS-20-5 [cqueue/0]
TS-29-5 [khubd]
TS-29-5 [kseriod]
TS-24 0 [khungtaskd]
TS-24 0 [pdflush]
TS-29-5 [kswapd0]
TS-19-5 [aio/0]
TS-28-5 [kpsmoused]
TS-29-5 [mpt_poll_0]
TS-19-5 [mpt/0]
TS-19-5 [scsi_eh_0]
TS-19-5 [ata/0]
TS-19-5 [ata_aux]
TS-19-5 [kstriped]
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/apache/bin/httpd-k start
TS-29-5 [kjournald]
TS-22 0/usr/local/apache/bin/httpd-k start
TS-22 0/usr/local/apache/bin/httpd-k start
TS-22 0/usr/local/apache/bin/httpd-k start
TS-29-5 [kauditd]
TS-24-4/sbin/udevd-d
TS-21 0/usr/local/apache/bin/httpd-k start
TS-17 0/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/data -- pi
TS-21 0/usr/local/mysql/bin/mysqld -- basedir =/usr/local/mysql -- data
TS-24 0/sbin/dhclient-1-q-lf/var/lib/dhclient/dhclient-eth0.leas
TS-20-5 [kgameportd]
TS-23 0 sshd: chenqiguo [priv]
TS-24 0 sshd: chenqiguo @ pts/0
TS-24 0-bash
TS-23 0 su-
TS-24 0-bash
TS-19-5 [kmpathd/0]
TS-19-5 [kmpath_handlerd]
TS-29-5 [kjournald]
TS-29-5 [kjournald]
TS-24 0/usr/sbin/vmtoolsd
TS-24 0 cupsd
TS-24 0 tpvmlpd2
TS-26-5 [iscsi_eh]
TS-22-5 [cnic_wq]
TS-36-20 [bnx2i_thread/0]
TS-29-5 [ib_addr]
TS-19-5 [ib_mcast]
TS-19-5 [ib_inform]
TS-19-5 [local_sa]
TS-19-5 [iw_cm_wq]
TS-19-5 [ib_cm/0]
TS-19-5 [rdma_cm]
TS-25-10 iscsiuio
TS-21 0 iscsid
TS-34-10 iscsid
TS-28-4 auditd
TS-32-8/sbin/audispd
TS-24 0 syslogd-m 0
TS-24 0 klogd-x
TS-24 0 portmap
TS-29-5 [rpciod/0]
TS-14 0 rpc. statd
TS-24 0 running-daemon -- system
TS-24 0/usr/sbin/hcid
TS-14 0/usr/sbin/sdpd
TS-30-10 [krfcommd]
TS-17 0 pcscd
TS-21 0/usr/sbin/acpid
TS-24 0 hald
TS-24 0 hald-runner
TS-21 0 hald-addon-acpi: listening on acpid socket/var/run/acpid. soc
TS-24 0 hald-addon-keyboard: listening on/dev/input/event0
TS-23 0 hald-addon-storage: polling/dev/hdc
TS-14 0/usr/bin/hidd -- server
TS-17 0 automount -- pid-file/var/run/autofs. pid
TS-24 0/usr/sbin/sshd
TS-24 0 sendmail: accepting connections
TS-24 0 sendmail: Queue runner @ 01:00:00 for/var/spool/clientmqueue
TS-24 0 gpm-m/dev/input/mice-t exps2
TS-24 0 crond
TS-21 0 xfs-droppriv-daemon
TS-21 0/usr/sbin/atd
TS-21 0 libmongod -- daemon
TS-24 0 avahi-daemon: running [localhost-2.local]
TS-15 0 avahi-daemon: chroot helper
TS-24 0/usr/sbin/dnsmasq -- strict-order -- bind-interfaces -- pid-file
TS-24 0/usr/sbin/smartd-q never
TS-23 0 login -- root
TS-24 0/sbin/mingetty tty2
TS-24 0/sbin/mingetty tty3
TS-24 0/sbin/mingetty tty4
TS-19 0/sbin/mingetty tty5
TS-20 0/sbin/mingetty tty6
TS-24 0-bash
TS-5 19/usr/bin/python-tt/usr/sbin/yum-updatesd
TS-5 19/usr/libexec/gam_server
TS-5 19/usr/bin/python-tt/usr/libexec/yum-updatesd-helper -- check
TS-22 0 ps-e-o class, rtprio, pri, nice, command
TS-21 0 rpc. idmapd
TS-29-5 [kjournald]
TS-24 0 [pdflush]
TS-24 0/usr/sbin/snmptrapd-Lsd-p/var/run/snmptrapd. pid
TS-24 0 smbd-D
TS-24 0 nmbd-D
TS-21 0 smbd-D
TS-24 0/usr/local/php/bin/php server. php
TS-20 0/usr/local/php/bin/php server. php
TS-24 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-21 0/usr/local/php/bin/php server. php
TS-24 0/usr/sbin/snmpd-Lsd-Lf/dev/null-p/var/run/snmpd. pid-

CLS corresponds to the scheduling algorithm of the kernel. RTPRIO corresponds to the real-time priority, PRI corresponds to the static priority, NICE corresponds to the specified level of the static priority, and COMMAND corresponds to the process COMMAND. Note that the above [] are all kernel threads.
Because a process has a priority concept, in extreme cases, a process with a higher priority may never run, this introduces the concept of dynamic priority. In order to prevent some processes with low priority from running, the Linux kernel will temporarily increase these processes with low priority. Of course, for some processes with high priority, the priority will be adjusted temporarily. The Dynamic Priority takes effect on SCHED_OTHER, mainly the process of user space. The algorithm is dynamic priority = max (100, min (static priority-bonus + 5,139 )). Bonux value range: 0-10

Of course, dynamic priority is not omnipotent, and sometimes it may violate our will. For example, if we are running a web server with a relatively high concurrency and need to run in real time, the dynamic priority may sometimes reduce the process of our web server, it seems that this is against our will, and we expect that the web server itself should have a higher right to use the cpu. Therefore, you can manually adjust the priority of a process.

The nice and renice commands can be used to adjust the process priority. Nice indicates to manually adjust the priority of a process when starting a process; renice indicates to change the priority of a running process.
For example, execute the priority command for the newly started httpd hand:
# Nice -- 5/usr/local/apache/bin/apachectl start
# Ps-e-o class, rtprio, pri, nice, command | grep httpd
TS-24-5/usr/local/apache/bin/httpd-k start
TS-23-5/usr/local/apache/bin/httpd-k start
TS-23-5/usr/local/apache/bin/httpd-k start
TS-23-5/usr/local/apache/bin/httpd-k start
We can see that the priority has been changed to-5. Command for changing the priority of a running process:
# Ps-ef | grep httpd
Root 5474 1 0 06:50? 00:00:00/usr/local/apache/bin/httpd-k start
# Renice-10 5474
# Ps-e-o class, rtprio, pri, nice, command | grep httpd
TS-34-10/usr/local/apache/bin/httpd-k start
We can see that the httpd process with the previous priority of-5 has been changed to-10.

For processes from 1 to 99, you can use the chrt command to change the priority. For real-time priority, sched_fifo and sched_rr are classified. Therefore, you must specify the scheduling algorithm when adjusting the priority.
Here we will adjust the priority of a fifo category:
# Ps-e-o class, rtprio, pri, nice, command, pid | grep FF
FF 9999 139-[migration/0] 2
# Chrt-f-p 90 2 (-f specifies the fifo class,-p specifies the level, and 2 specifies the pid of the process)
# Ps-e-o class, rtprio, pri, nice, command, pid | grep FF
FF 90 130-[migration/0] 2
For the same rr category, replace-f with-r.

So how does linux select the processes that need to be used in many processes? Because there are a total of 139 process priority levels, 139 queues are created, each queue stores the current corresponding process level. When a process needs to be called, the header of the entire Queue (the first element) is scanned according to the priority of the process. If there is a higher priority, if not, continue searching until one is found. This search method can ignore the number of processes, so that the algorithm complexity of processes is always 0 (1) to quickly find high-priority processes. In fact, each queue is divided into two scenarios: Active Queue and expired queue. When a process is scheduled but not completed, it will be placed in the queue for expiration. After all the processes in the active queue are completed, exchange the active queue and the expired queue and re-execute the Active Queue (the old expired Queue ).

In a multi-cpu operating system, because the memory is a shared resource, resource competition may occur when accessing memory resources, we call this cpu architecture SMP (Symmetric Multi-Processor)
A cpu requires at least three cpu clock cycles after Accessing memory data:
1. transmit an addressing requirement to the memory controller. The memory controller returns an addressing instruction.
2. Find the corresponding memory address and apply a certain request mechanism. For example, read and write locks.
3. Complete read or write operations.
Therefore, when a cpu is dealing with memory, other CPUs cannot access the memory. Therefore, in an smp architecture, it is not necessarily a good thing to have more CPUs, because the more CPUs there are, the higher the chance of competing resources, and the lower the performance. Therefore, another cpu architecture numa (Non-Unified Memory Access, Non-consistent Memory Access) is generated)

Hardware tends to use multiple system buses, each of which serves a group of processors. Each group of processors has its own memory and may have its own I/O channels. However, each CPU can access the memory associated with other groups in the same way. Each group is called a "NUMA node ". The number of CPUs in the NUMA node depends on the hardware supplier. Accessing local memory is faster than Accessing memory associated with other NUMA nodes. This is the origin of the "non-consistent memory access architecture" name.

On the numa-based cpu, because the speed of accessing the memory of other "NUMA" nodes is not faster locally, we should try to make the cpu only access the memory controlled by itself. Because the kernel itself balances processes, the process will switch between two CPUs frequently, and the final result will be cross-memory access, that is, access to the memory of the "NUMA" node is not your own, resulting in reduced performance. Therefore, on a busy server, such as a web server, you can use cpu binding to ensure that the web server runs on a certain cpu to avoid cross-memory access.

In some cases, the balance is inevitable. Otherwise, a cpu is very idle, and another cpu is very busy, so you must find a balance. In general, in the numa architecture, this effect is useful when our memory hits very low. You can use the numastat command to view details.
# Numastat
Node0
Numa_hit 25102521
Numa_miss 0
Numa_foreign 0
Interleave_hit46349
Local _ node 25102521
Other_node 0
Because the machine is not a numa-based cpu, only node0 is displayed. In The Real numa architecture, node1 is displayed. Numa_hit indicates the number of accesses to the local memory, and numa_miss indicates the number of times no hits were made. Because there is only one piece of memory space, all of them are hit here. If the numa_miss value is too high in the numa architecture, bind the core service process to the specified cpu and start the numad process. Numad can bind our processes with some CPUs at the hardware level.

To bind a specified process to a specified cpu, use the taskset command and use the mask to mark the cpu. 0x0000 0001 indicates 0th CPUs, and 0x00000003 indicates 0th and first CPUs. The basic command of taskset: taskset-p mask pid. For example, if you want to bind the 5132 process to the 1 cpu, you can use:
# Taskset-p 0x00000002 5132
However, this method is not intuitive enough, so you can use the-c option to directly specify the number of bound CPUs. For example, the preceding method with the-c option added is:
# Taskset-p-c 1 5132
In this way, the 5132 process is directly bound to the first cpu.

For example, my current machine has a 2-core cpu. Check which CPUs some processes are bound:
# Ps-e-o pid, psr, command
PID PSR COMMAND
1 0 init [3]
2 0 [migration/0]
3 0 [ksoftirqd/0]
4 1 [migration/1]
5 1 [ksoftirqd/1]
6 0 [events/0]
7 1 [events/1]
8 0 [khelper]
Because of the space limit, the list will not be completed. Among them, the PSR is the cpu number that the current process runs on. Observe that the mysql service is running on that cpu:
# Ps-e-o pid, psr, command | grep mysql
4498 0/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/data -- pid-file =/data/localhost. localdomain. pid
We can see that the mysql process is running on the No. 0 cpu, so we will test it and bind it to the No. 1 cpu.
# Taskset-p-c 1 4498
Pid 4498's current affinity list:
Pid 4498's new affinity list: 1
# Ps-e-o pid, psr, command | grep mysql
4498 1/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/data -- pid-file =/data/localhost. localdomain. pid
In this way, the mysql process is bound to the corresponding cpu.

But the above still cannot meet the requirement that cpu 1 only runs mysql processes, and other processes also run on cpu 1, so a large number of switching between processes is still required. Therefore, you can specify in the kener parameter in/etc/grub. conf that some CPUs do not run other processes or services since they are started. For example:
Kernel/vmlinuz-2.6.18-371.6.1.el5 ro root = LABEL =/rhgb quiet isolcpus = 1
The isolcpus syntax is isolcpus = cpu number,..., cpu number, which means to isolate the specified cpu from other processes. Specify the above and restart the operation later to observe the cpu process usage.
# Ps-e-o pid, psr, command
PID PSR COMMAND
1 0 init [3]
2 0 [migration/0]
3 0 [ksoftirqd/0]
4 1 [migration/1]
5 1 [ksoftirqd/1]
6 0 [events/0]
7 1 [events/1]
8 0 [khelper]
13 0 [kthread]
18 0 [kblockd/0]
19 1 [kblockd/1]
20 0 [kacpid]
190 0 [cqueue/0]
191 1 [cqueue/1]
194 0 [khubd]
196 0 [kseriod]
274 0 [khungtaskd]
275 0 [pdflush]
276 0 [pdflush]
277 0 [kswapd0]
278 0 [aio/0]
279 1 [aio/1]
486 0 [kpsmoused]
527 0 [mpt_poll_0]
528 0 [mpt/0]
529 0 [scsi_eh_0]
533 0 [ata/0]
534 1 [ata/1]
535 0 [ata_aux]
545 0 [kstriped]
558 0 [kjournald]
584 0 [kauditd]
617 0/sbin/udevd-d
1800 0 [kgameportd]
2409 0 [kmpathd/0]
2410 1 [kmpathd/1]
2411 0 [kmpath_handlerd]
2444 0 [kjournald]
2446 0 [kjournald]
3111 0/usr/sbin/vmtoolsd
3178 0 cupsd
3223 0 tpvmlpd2
3329 0 [iscsi_eh]
3375 0 [cnic_wq]
3382 0 [bnx2i_thread/0]
3383 1 [bnx2i_thread/1]
3395 0 [ib_addr]
3405 0 [ib_mcast]
3406 0 [ib_inform]
3407 0 [local_sa]
3411 0 [iw_cm_wq]
3415 0 [ib_cm/0]
3416 1 [ib_cm/1]
3420 0 [rdma_cm]
3437 0 iscsiuio
3442 0 iscsid
3443 0 iscsid
3850 0/sbin/dhclient-1-q-lf/var/lib/dhclient/dhclient-eth0.leases-pf/v
3958 0 auditd
3960 0/sbin/audispd
3992 0 syslogd-m 0
3995 0 klogd-x
4075 0 irqbalance
4108 0 portmap
4144 0 [rpciod/0]
4145 1 [rpciod/1]
4152 0 rpc. statd
4190 0 rpc. idmapd
4222 0 running-daemon -- system
4236 0/usr/sbin/hcid
4240 0/usr/sbin/sdpd
4256 0 [krfcommd]
4302 0 pcscd
4317 0/usr/sbin/acpid
4332 0 hald
4333 0 hald-runner
4340 0 hald-addon-acpi: listening on acpid socket/var/run/acpid. socket
4354 0 hald-addon-keyboard: listening on/dev/input/event0
4363 0 hald-addon-storage: polling/dev/hdc
4393 0/usr/bin/hidd -- server
4439 0 automount -- pid-file/var/run/autofs. pid
4464 0/usr/sbin/sshd
4481 0/bin/sh/usr/local/mysql/bin/mysqld_safe -- datadir =/data -- pid-file =/d
4632 0/usr/local/mysql/bin/mysqld -- basedir =/usr/local/mysql -- datadir =/data
4689 0 sendmail: accepting connections
4697 0 sendmail: Queue runner @ 01:00:00 for/var/spool/clientmqueue
4712 0 gpm-m/dev/input/mice-t exps2
4726 0 crond
4758 0 xfs-droppriv-daemon
4772 0 anacron-s
4785 0/usr/sbin/atd
4802 0 libmongod -- daemon
4845 0 avahi-daemon: running [localhost. local]
4846 0 avahi-daemon: chroot helper
4918 0/usr/sbin/dnsmasq -- strict-order -- bind-interfaces -- pid-file =/var/run
4940 0/usr/sbin/smartd-q never
4944 0 login -- root
4945 0/sbin/mingetty tty2
4946 0/sbin/mingetty tty3
4947 0/sbin/mingetty tty4
4952 0/sbin/mingetty tty5
4955 0/sbin/mingetty tty6
4960 0/usr/bin/python-tt/usr/sbin/yum-updatesd
4962 0/usr/libexec/gam_server

We can see that a large number of processes run on the cpu No. 0, except for some threads in the kernel. That is to say, the so-called cpu isolation is not complete, because a few threads in the kernel will still run on the isolated cpu. So let's take the mysql process above for example. Let it run on cpu 1 first.
# Taskset-p-c 1 4481
Pid 4481's current affinity list: 0
Pid 4481's new affinity list: 1
See the difference here? Previously, the value of current affinity list was not isolated from 0 and 1. Here, the value is only 0, indicating that the isolation of cpu was successful.

Of course, even if this is done, there is still no guarantee that only the mysql process is served on the No. 1 cpu, because the cpu must be interrupted. When the No. 1 cpu is serving mysql, other hardware suddenly sends an interrupt request. The No. 1 cpu may be transferred to the kernel mode to process the interrupt request. Therefore, we can also isolate the No. 1 cpu without handling interrupted requests, so that the No. 1 cpu can completely serve the mysql process and a few kernel threads.

To bind cpu interruptions, we also need to disable a service in the system called irqbalance. This service will regularly and evenly reallocate hardware service interruptions to various cpu numbers. In order to allocate the interrupted service to the isolated cpu number for the textile irqbalance service, the service should be disabled.

# Service irqbalance stop
# Chkconfig irqbalance off

The syntax for binding some interrupt numbers to one or more CPUs is echo cpu_mask>/proc/irq/<irq_num>/smp_affinity, the interrupt number on my current host is:
# Cat/proc/irq/
0/10/12/14/2/4/51/6/7/8/
1/11/13/15/3/5/59/67/75/9/
# Cat/proc/interrupts view the current interrupt Information
CPU0 CPU1
0: 4719693 0 IO-APIC-edge timer
1: 83 0 IO-APIC-edge i8042
6: 5 0 IO-APIC-edge floppy
7: 0 0 IO-APIC-edge parport0
8: 1 0 IO-APIC-edge rtc
9: 0 0 IO-APIC-level acpi
12: 132 0 IO-APIC-edge i8042
15: 2964 39221 IO-APIC-edge ide1
51: 12271 13247 IO-APIC-level ehci_hcd: usb1, ioc0
59: 9142 3854647 IO-APIC-level uhci_hcd: usb2
67: 86 10107 IO-APIC-level eth0
75: 0 0 IO-APIC-level Ensoniq AudioPCI
NMI: 0 0
LOC: 4719083 4720165
ERR: 0
MIS: 0
You can see some interruption information. Let's take the 0 interrupt to describe:
0 indicates the interrupt number, 4719693 indicates that 4719693 interrupts are returned on cpu0, and 0 indicates that 0 interrupts are returned on cpu1, IO-APIC-edge indicates the device interface linked to the interrupt linked list of this port, and timer indicates the device name.

Note: The interrupt numbers 0 and 2 are special. The kernel does not allow these two interrupt numbers to be modified. Therefore, in my current system, all except 0 can be set to fix the disconnection on a certain cpu number. If we want to bind the interrupt number 59 to cpu0, we can use:
# Echo 00000001>/proc/irq/59/smp_affinity
In this way, all interruptions that occur on the 59 th will be taught to cpu0 for processing, thus completely isolating cpu1 (the 59 th interrupt was originally handled by cpu1 ). Here, we use the mask to calculate the number of cpus. Of course, we can also use the decimal method. The number of CPUs and the corresponding decimal number are as follows:
Zero-based cpu id: 7 6 5 4 3 2 1 0
Decimal Value: 128 64 32 16 8 4 2 1
Assume that we want to bind all interrupts to the cpu No. 1-4, you can use echo "15">/proc/irq/59/smp_affinity (15 = 1 + 2 + 4 + 8 ).
Since these settings will not take effect after the shutdown, how can we make them take effect? I will not explain them here. I think everyone knows.





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.