Follow the wechat official account: CodingTechWork, learn and progress together.

The introduction

Concurrent programming

The purpose of concurrent programming is to improve the slow execution of serial programs, but it is not necessary to start more threads to make the program run faster. Because concurrent programs are prone to hardware and software resources and other limitations, resulting in slow context switching. Frequent context switching results in concurrent programs performing worse than serial programs, which violates the original dream of making programs run faster.

Single-core and multi-core cpus

Before introducing context switching, let’s take a look at the concepts of single-core and multi-core computer CPUS. In the beginning, the single-core CPU is more dead brain. When the CPU is energized, it will execute the instructions specified in the memory block. If you want to execute the code elsewhere in the memory block, you have to adjust the bus position to execute. Later, Intel evolved a way of working called the timeline, adding transistors to improve CPU efficiency, and based on the clock circuit to control the CPU to the specified address, with the operating system to use the clock circuit to control the CPU jump. Multi-core integrated in a chip, composed of multiple CPUS, multi-core through the internal bus interaction and sharing data, which will have a core specifically allocated to the operating system, by the operating system to allocate resources and control CPU execution program.

Windows check cores

To open the command line mode, use the Win+ R key to open the run, and then enter CMD. In addition to using the systeminfo command directly, let’s introduce the wmic command to view computer information.

  1. The inputwmicTo enter the CLI
  2. cpu get Name: Displays the CPU information of the computer
  3. cpu get NumberOfCores: Displays the number of CPU cores on the computer
  4. cpu get NumberOfLogicalProcessors: Checks the number of CPU threads on the computer
C:\Users\Lenovo>wmic wmic:root\ CLI > CPU get Name Intel(R) Core(TM) i7-8700 CPU @3.20ghz wMIC :root\cli> CPU get NumberOfCores NumberOfCores 6 wmic:root\cli>cpu get NumberOfLogicalProcessors NumberOfLogicalProcessors 12 wmic:root\cli>Copy the code

Linux check the number of cores

Linux CPU information is stored in /proc/cpuinfo and memory information is stored in /proc/meminfo.

  1. cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l: Displays the number of physical cpus
  2. cat /proc/cpuinfo| grep "cpu cores"| uniq: Displays the number of CPU cores
  3. cat /proc/cpuinfo| grep "processor"| wc -l: Displays the number of logical cpus
  4. cat /proc/meminfo| grep MemTotal: Check the memory size
[linux@01 ~]$ cat /proc/cpuinfo| grep "cpu cores"| uniq
cpu cores       : 1
[linux@01 ~]$ cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
16
[linux@01 ~]$ cat /proc/cpuinfo| grep "processor"| wc -l
16
[linux@01 ~]$ cat /proc/meminfo| grep MemTotal
MemTotal:       32780412 kB
Copy the code

Context switch

Introduction to context switching

We’re all familiar with single-core and multi-core systems, and our computers or machines always want as many cores as possible because multi-core systems allow for better multithreaded concurrency. Of course, single-core processors also support multi-threaded code execution by allocating CPU time slices to each thread. A time slice is the amount of time that the CPU allocates to each thread. How does a single CPU achieve concurrent execution of multiple threads? That’s because the time slice is so short (typically tens of milliseconds) that the CPU alternates execution by switching threads, giving you the illusion that multiple threads are executing simultaneously. CPU time slice to implement multithreaded execution, the current task execution time slice to continue after the switch to the next task, before switching tasks, in fact, the CPU will save the task of the state, so that at the next cut back to the task, can load the task status again, continue to be executed (in fact, that is, save the current thread running position, at the same time? Information about the thread environment to be restored is loaded. . The process of a task from save to reload is a context switch. Context switching, once frequent, must consume CPU resources, must be time-consuming, concurrent performance will be affected. How to reduce?

Reduction of context switches

Context switching can be reduced by lock-free concurrent programming, CAS algorithms, using minimal threads, and using coroutines.

  • Lockless concurrent programming: When multi-threads compete for locks, a large number of tasks are executed alternately, which will cause context switch. When multi-threads process data, we can use some lock-free methods to process shared data, such as dividing the ID of the data according to the Hash algorithm, so that different threads can process the data in different segments.
  • CAS algorithmJAVA’s Atomic package uses the CAS algorithm to update data without locking.
  • Using minimum threads: In the process of program development, it is necessary to avoid creating unnecessary threads, such as few concurrent tasks. If many threads are created to handle tasks, most of the threads may be waiting for idle state when the program is running. For example, reduce the number of onlineWAITING threadWe can go throughjstack [pid] > dumpNameDump the thread information of a process todumpNameFile, and then filter through (grep java.lang.Thread.State dumpName | awk '{print $2$3$4$5}' | sort | uniq -cHow many threads are in WAITING state. Or directly throughjstack -l [pid]| grep BLOCKED,jstack -l [pid]| grep WAITINGCommand to view the thread information of the process.
  • coroutines: Implements scheduling of multiple tasks in a single thread, and maintains switching of multiple tasks in a single thread.

Context switch view

You can use the vmstat and pidstat commands to view information such as the number of context switches.

The vmstat command

The vmstat command is short for Virtual Memory Statistics. You can monitor the Virtual Memory, processes, and CPU activities of an operating system (OS).

Description of the vmstat command parameters

[linux@01 ~]$ vmstat -help

Usage:
 vmstat [options] [delay [count]]

Options:
 -a, --active           active/inactive memory
 -f, --forks            number of forks since boot
 -m, --slabs            slabinfo
 -n, --one-header       do not redisplay header
 -s, --stats            event counter statistics
 -d, --disk             disk statistics
 -D, --disk-sum         summarize disk statistics
 -p, --partition <dev>  partition specific statistics
 -S, --unit <char>      define display unit
 -w, --wide             wide output
 -t, --timestamp        show timestamp

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see vmstat(8).
Copy the code

A more common approach is to use vmstat a directly, where a represents how many seconds are displayed in real time.

Vmstat collects real-time statistics on the overall situation

Grammar:

  • vmstat [count]

Parameter Description:

  • count: indicates the statistical interval.

Example:

[linux@01 ~]$ vmstat 1 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 13223320 146732 1982108 0 0 0 3 0 0 0 0 99 0 0 1 0 0 13222504 146732 1982108 0 0 0 172 747 1224 0 0 99 0 0 0 0 0 0 13222696 146732 1982108 0 0 0 44 1078 1464 0 0 99 0 0 0 0 0 0 13223492 146732 1982108 00 0 749 1189 00 100 00 00 0 13223260 146732 1982092 00 0 16 789 1272 00 100 00 00 00 00 13223620 146732 1981996 00 00 1359 1589 00 99 00 00 0 13224364 146732 1981996 00 0 156 817 1279 00 100 00 00 00 00 13222488 146732 1981996 00 0 32 792 1292 00 100 00Copy the code

Pidstat command

Vmstat can collect statistics on the entire operating system, but cannot collect statistics on a certain process. The pidstat command can be used to analyze the entire operating system or a certain process.

Pidstat installation

The pidstat command is used to monitor the Linux system. The pidstat command can be used to monitor the Linux process data and output information about each task managed by the kernel. The pidstat command is part of the sysstat software suite, so you must install the pidstat command first. Yum install sysstat for centos

Description of pidstat command parameters

[linux@01 ~]$ pidstat -help
Usage: pidstat [ options ] [ <interval> [ <count> ] ]
Options are:
[ -d ] [ -h ] [ -I ] [ -l ] [ -r ] [ -s ] [ -t ] [ -U [ <username> ] ] [ -u ]
[ -V ] [ -w ] [ -C <command> ] [ -p { <pid> [,...] | SELF | ALL } ]
[ -T { TASK | CHILD | ALL } ]
Copy the code

Among them:

  • interval: Indicates the interval at which data is collected. This parameter is optional.
  • countThis parameter is optional. If only interval is specified but count is not specified, the number of statistics counts is unlimited by default.

Pidstat CPU statistics

Grammar:

  • Full amount:pidstat -u [interval] [count]Or the defaultpidstat [interval] [count]
  • A task:pidstat -p <pid> [interval] [count]

Example of full task CPU statistics

[linux@01 ~]$pidstat Linux 3.10.0-514.el7.x86_64 (01) 03/09/2021 _x86_64_ (16 CPU) 08:45:48 AM UID PID %usr %system %guest %CPU CPU Command 08:45:48am 0 1 0.00 0.00 0.00 0.00 14 Systemd 08:45:48am 0 2 0.00 0.00 0.00 0.00 0.00 15 kthreadd 08:45:48am 0 3 0.00 0.00 0.00 0 Ksoftirqd /0 08:45:48am 0 7 0.00 0.00 0.00 0 Migration /0 08:45:48am 0 9 0.00 0.01 0.00 0.01 15 Rcu_sched 08:45:48 AM 0 10 0.00 0.00 0.00 0 Watchdog /0 08:45:48 AM 0 11 0.00 0.00 0.00 1 Watchdog /1 08:45:48am 0 12 0.00 0.00 0.00 1 Migration /1 08:45:48am 0 13 0.00 0.00 0.00 0.00 1 Ksoftirqd /1Copy the code

CPU statistics example of a PID task

[linux@01 ~]$pidstat -u -p 27680 1 Linux 3.10.0-514.el7.x86_64 (01) 03/09/202_x86_64_ (16 CPU) 09:14:19 AM UID PID %usr %system %guest %CPU CPU Command 09:14:20 AM 1000 27680 1.00 0.00 0.00 1.00 8 Java 09:14:21 AM 1000 27680 0.00 0.00 0.00 0.00 8 Java 09:14:22 AM 1000 27680 0.00 0.00 8 Java 09:14:23 AM 1000 27680 0.00 0.00 8 Java 09:14:23 AM 1000 27680 0.00 0.00Copy the code

Details of statistical parameters:

  • UID: indicates the actual user ID of a monitoring task.
  • PID: indicates the process ID of the monitored task
  • %usr: indicates that the task is inUser modeThe CPU usage of the application at execution time. CPU computation time in this field does not include time spent in the virtual processor. (User level)
  • %system: indicates that the task is inKernel modeCPU usage at execution time. (System level)
  • %guest: indicates that the task is inVm processorCPU usage when executing on. (Virtual level).
  • %CPU: indicates the total CPU usage of the task. In a multiprocessor environment, if you take-IParameter, the CALCULATION of CPU usage is divided by the number of cpus on the machine.
  • CPU: indicates the number of the processor running the task.
  • Command: indicates the name of the task command.

Pidstat I/O statistics

Grammar:

  • pidstat -d -p <pid>

Example:

[linux@01 ~]$pidstat -d -p 27680 Linux 3.10.0-514.el7.x86_64 (01) 03/09/202_x86_64_ (16 CPU) 08:52:55 AM UID PID KB_rd /s kB_wr/s kB_ccwr/s Command 08:52:55 AM 1000 27680 0.00 0.01 0.00 JavaCopy the code

Details of statistical parameters:

  • UID: indicates the actual user ID of a monitoring task.
  • PID: indicates the process ID of the monitored task
  • kB_rd/s: indicates the read speed of the task from the hard disk.rdsaidread.
  • kB_wr/s: indicates the write speed of the task to the hard disk.wrsaidwrite.
  • kB_ccwr/s : indicates the speed at which the task is cancelled.ccwrsaidcancel write.
  • Command: indicates the name of the task command.

Pidstat Memory statistics

Grammar:

  • pidstat -r -p <pid>

Example:

[linux@01 ~]$pidstat -r -p 27680 Linux 3.10.0-514.el7.x86_64 (01) 03/09/202_x86_64_ (16 CPU) 08:56:48am UID PID Minflt /s majflt/s VSZ RSS %MEM Command 08:56:48am 1000 27680 0.01 0.00 13124360 2489432 7.59 JavaCopy the code

Details of statistical parameters:

  • UID: indicates the actual user ID of a monitoring task.
  • PID: indicates the process ID of the monitored task.
  • minflt/s: represents the smaller number of errors per second when loading data from memory, which do not require loading memory pages from disk, whereminfltsaidminor fault.
  • majflt/s: represents the large number of errors per second while loading data from memory, which require loading memory pages from disk, wheremajfltsaidmajor fault.
  • VSZ: indicates the virtual capacity. The virtual memory usage of the entire process (unit: KB)
  • RSS: Indicates long-term memory usage. Non-exchangeable physical memory usage of a task (unit: KB)
  • Command: indicates the name of the task command.

Pidstat interval limited number of monitoring

Grammar:

  • pidstat -p <pid> [interval] [count]

The sample

[linux@01 ~]$ pidstat -p 27680 1 5
Linux 3.10.0-514.el7.x86_64 (01)  03/09/2021      _x86_64_        (16 CPU)

09:03:55 AM   UID       PID    %usr %system  %guest    %CPU   CPU  Command
09:03:56 AM  1000     27680    0.00    0.00    0.00    0.00     8  java
09:03:57 AM  1000     27680    0.00    0.00    0.00    0.00     8  java
09:03:58 AM  1000     27680    1.00    0.00    0.00    1.00     8  java
09:03:59 AM  1000     27680    0.00    0.00    0.00    0.00     8  java
09:04:00 AM  1000     27680    0.00    0.00    0.00    0.00     8  java
Average:     1000     27680    0.20    0.00    0.00    0.20     -  java
Copy the code

Pidstat Context switch statistics

Grammar:

  • pidstat -w <pid> [interval] [count]

The sample

[linux@01 ~]$pidstat -w -p 27680 Linux 3.10.0-514.el7.x86_64 (01) 03/09/202_x86_64_ (16 CPU) 09:11:15am UID PID CSWCH /s NVCSWCH /s Command 09:11:15AM 1000 27680 0.00 0.00 Java [linux@01 ~]$pidstat -w -p 27680 1 10 Linux 3.10.0-514.el7.x86_64 (01) 03/09/2021 _x86_64_ (16 CPU) 09:11:39am UID PID CSWCH /s NVCSWCH /s Command 09:11:40am 1000 27680 0.00 Java 09:11:41 AM 1000 27680 0.00 Java 09:11:42 AM 1000 27680 0.00 0.00 Java 09:11:43 AM 1000 27680 0.00 0.00 Java 09:11:44AM 1000 27680 0.00 Java 09:11:45am 1000 27680 0.00 0.00 Java 09:11:47AM 1000 27680 0.00 Java 09:11:48am 1000 27680 0.00 0.00 Java Average: 1000 27680 0.00 0.00 JavaCopy the code

Details of statistical parameters:

  • UID: indicates the actual user ID of a monitoring task.
  • PID: indicates the process ID of the monitored task.
  • cswch/s: indicates the number of voluntary context switches performed by the task per second. When a task is blocked, it voluntarily cedes its CPU resources. Among themcswchsaidcontext switch.
  • nvcswch/s: indicates the number of non-resource context switches per second for the task. The CPU has used up the time slice allocated to a task, so the process will be forced to surrender CPU execution rights. Among themnvcswchsaidinvoluntary context switch
  • Command: indicates the name of the task command.

REF “The Art of Concurrent Programming in Java”