This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

If the profile

The so-called, know the enemy, know the enemy, one hundred battles are not dangerous, for the optimization of the computer technology, the bottom is just for the OPTIMIZATION of THE CPU technology, but if you want to optimize the program, involving CPU execution ability, it must understand the principles and concepts of CPU and execution process, So the purpose of writing this film is to explain the relevant knowledge of CPU, when you are not strange to CPU, that is, when you are super god, ha ha……

CPU related knowledge

This article introduces the knowledge of multi-CPU, physical core, logical core, hyperthreading, process, thread, concurrent, parallel, and Linux query CPU and core information methods

Confusable concept

A multiprocessor

Identify the context carefully, usually means multiple CPU; Sometimes many logic processors that means many cores, like foreigners often put a CPU in a number of cores as a number of CPUS to describe.

Logic processor

Windows 10’s Chinese translation of this very, very misunderstood word, I think it can be understood as a kernel program unit.

Logical CPU

Someone has used this term to mean logical core, but I doubt the basis of the usage, and it’s easy to misunderstand, because I would think that a core is a core and a CPU is a CPU, as if a core is a CPU and the same cache.

CPU level 1 to level 4 cache

CPU Cache Memory, sometimes called caching, is used to compensate for the large speed difference between the CPU and main Memory (the main Memory, or Memory bar) to improve efficiency.

There are many levels of caching, from one to four, which is not much to listen to but there are. Level 1 and level 2 are unique to each core, level 3 cache is shared by all cores, and level 4 cache seems to solve the problem of CPU display and CPU speed difference (CPU can have graphics card). CPU items are usually labeled as level 3 caches, and the rest are not labeled. As shown in the figure:

There is no L4 Cache, mostly because only L1 to L3 are packaged in the CPU.

Similar to multi-core cpus, there are some protocols that guarantee consistency between multiple cpus’ internal caches (MESI). There should also be a solution to the problem of consistency between multiple cpus’ internal caches.


Physical, logical, and hyperthreading

  • Physical core: the physical hardware concurrency that is actually encapsulated in the CPU.
  • Logical core: the use of hyperthreading technology to simulate the core, usually a physical core can be virtual out of 2, is the number of threads marked by CPU merchandise, some information is also called logical CPU number
  • Hyper-threading: HT, hyper-threading. Hyper-threading allows one core to emulate the technical capabilities of two cores
  • Physical CPU: indicates the number of CPU chips inserted on the mainboard
  • Virtual CPU: A fake CPU implemented by virtual technology, similar to the CPU in VMWare virtual machines

Confused:

The logical core seems to be sometimes translated as the logical processor, and the processor is generally understood by me as the CPU, so the logical core, is the logical CPU? But this kind of translation can be a little bit vague and misleading.

Threads and cores

  • Threads require cores to execute, and each core can execute only one thread at a time.
  • It is the logic core that determines the number of concurrent threads
  • As many logical cores as you have, you can execute as many threads “simultaneously.”
conclusion

N physical cores, usually 2N logical cores, if you can only run several threads (n) of physical cores at the same time, then what is the use of hyperthreading simulated cores? Moreover, many CPU products are not marked “logical core number”, with the word “thread number”, meaning should be the number of logical cores can run at the same time how many threads (2n).

What do I mean by x core y thread (y is greater than x)

CPU commodity said 2 cores 4 threads, refers to 2 physical cores, 4 logical cores. Four threads means four logic cores.

Processes and threads

Processes are system level concepts, threads are CPU level things, the CPU is really executing threads, not processes

Process is a static concept, is a collection of resources, such as the process has its own memory; The thread is a dynamic concept, the process can have multiple threads, these threads share the same process of resources multi-process, under the current multi-core condition, can do multiple processes at the same time;

  • Is also able to do multiple threads at the same time. A single-core CPU, however, cannot execute processes simultaneously. Does the process have to have a port number? Not necessarily. Not if there’s no Internet exposure. But there must be a process number.

Concurrency and parallelism

Concurrency and ParallelismCopy the code
  • Parallelism means that physical “simultaneity” is being performed.
  • Concurrency is a type of programming that allows multiple tasks to be logically interwoven.

A program designed concurrently can start n threads, say 2, and hand them over to two cores, in which case the two threads are executing in parallel (” simultaneously “); These two threads can also be executed “alternately” by a single core.

Many times, will think that parallel is really simultaneous execution, and concurrency is alternate execution, this is the usual understanding, but the real meaning of concurrency refers to the design of the program allows simultaneous or alternate execution, is a program design scheme.

Multi-cpu and single-CPU multi-core

It is estimated that it is the two schemes to improve the computing capacity, and the final choice of single CPU multi-core scheme is a little more.

There is also a market for multi-CPU servers.

Why is the single-CPU multi-core solution better, mainly because it is better?
  • Communication between multiple cores does not need to go through an external bus, but only through the INTERNAL CPU bus, which is much faster. In addition, the cost of single CPU is also more low CPU, only a set of chipset, a set of storage, communication between multiple cores through the internal bus chip, shared memory.

  • Multi-core CPU cache consistency protocols include MSI, Illinois Protocol (MESI), MOSI, Synapse, Firefly and Dragon Protocol.

Multi-cpu market: Multi-CPU is suitable for computationally intensive tasks that are not sensitive to speed (time), such as engineering modeling, or such extreme tasks as SATI searching for aliens that can take thousands of years to run.

A single CPU mononuclear

Does the efficiency of single-cpu running multi-threading inevitably decrease?

Don’t must:

  • Drop: If multiple threads are running CPU-intensive tasks, there may be a decrease in efficiency, because only one core is full and has to be scheduled to waste time
  • Improved: If you have multiple threads running IO intensive tasks, it may be improved, because IO runs much slower than CPU. Switch threads back and forth to make the core use. The scheduling loss is worthwhile.

For example: I have two stores (two tasks) that are across the street from each other, and I hire a worker (one CPU core) who is too busy to run one store, and you have him running two stores and running back and forth between the two stores, which reduces efficiency. If both of these stores are idle for 3 hours after 1 hour of work, it is obvious that running back and forth between the two stores can drain his value and improve his efficiency.

A single multi-core CPU

Often hear the POPULARITY of the CPU multi-core, programming should make good use of, in fact, there is indeed the current programming did not make full use of the performance of the multi-core CPU, but more because of multi-threaded programming is more complex.

Single CPU multi – core, for multithreading really improve the use of large. Do not pay for the word is sure to be more than a single core CPU good ah.

View CPU and core information in Linux

cat /proc/cpuinfo 
Copy the code

The information obtained should be list information in logical cores. Information about each logical core includes the ID of the physical core to which it belongs and the ID of the physical CPU to which it belongs.

List the physical cpus and their number of physical cores

cat /proc/cpuinfo | grep -E "physical id|cpu cores" | sed 'N; s/\n/ /' | sort | uniqCopy the code

If the output is as follows, there are two physical cpus, the first CPU has four physical cores, and the second CPU also has four:

physical id : 0 cpu cores : 4
physical id : 1 cpu cores : 4
Copy the code

Lists the number of physical cpus and their logical cores

cat /proc/cpuinfo | grep "physical id" | sort | uniq -c
Copy the code

Assuming the following output, there are two physical cpus, each containing eight logical cores

8 physical id : 0
8 physical id : 1
Copy the code

Querying the CPU Type (Virtual or real)

Run the cat /proc/cpuinfo command to query the model name.

Such as:

Intel(R) Xeon(R) CPU E5-2682 V4 @ 2.50GHz Indicates a real CPU or QEMU Virtual CPU Version (CPU64-RHEL6) indicates a Virtual CPU

Example Query whether the CPU supports hyperthreading

  • Physical cores and cpus: Each physical CPU has four physical cores
physical id : 0 cpu cores : 4
physical id : 1 cpu cores : 4
Copy the code
  • Logical core number, and the corresponding physical CPU ID
8 physical id : 0
8 physical id : 1
Copy the code

The first CPU (whose physical id is 0) is supported, and the second CPU (whose ID is 1) is supported as well, because it has 4 physical cores and 8 logical cores. If the number of physical and logical cores is equal, it does not indicate that the hyperthreading technology is not supported and may not be switched on.

What number of cores is displayed in the top command?

Input top and press 1 to display all logical cores, from Cpu0 to CpuN, which means there are N+1 logical cores.

Is the logical kernel.

6. Java process CPU usage Exceeds 100% Top Check the %CPU usage of processes. Why does the CPU usage exceed 100%

Since the process occupies more than one core, two cores is 200%.

The /proc/cpuinfo file is explained

  • Model Name: indicates the CPU model and frequency

For example, Intel(R) Xeon(R) CPU E5-2682 V4 @ 2.50GHz or QEMU Virtual CPU Version (CPU64-RHEL6) should be able to tell whether a Virtual CPU is a real CPU


  • Processor: indicates the ID of a logical core. For example, 0 indicates the first logical core
  • Physical ID: indicates the ID of the physical CPU. 0 indicates the first PHYSICAL CPU
  • Core ID: indicates the ID of the physical core. 0 indicates the first one
  • CPU cores: How many physical cores the physical CPU has
  • Siblings: How many logical cores does the physical CPU have? (As distinct from CPU cores, hyperthreading is enabled or not supported.)

Instruction summary (focus on learning, later may use the top oh!

  • Number of physical cpus

cat /proc/cpuinfo | grep ‘physical id’ | uniq |wc -l

Uniq is to remove multiple logical cores belonging to the same physical CPU

  • 2. Number of physical cores (all CPUS)

cat /proc/cpuinfo | grep ‘core id’ | uniq |wc -l

Uniq to remove multiple logical cores belonging to the same physical core

The above calculation method is to calculate the total number of cpus, not the number of cores per CPU

  • 3. Number of physical cores (one CPU)

Cat /proc/cpuinfo to view CPU cores, which record the number of physical cpus (identified by the physical ID in this entry).

  • Number of logical cores (all CPUS)

Number of logical cores, or in some cases, number of logical cpus

cat /proc/cpuinfo | grep ‘processor’ | wc -l

  • Five, the number of logical cores (a CPU)

Run the cat /proc/cpuinfo command to check the number of logical cores within the siblings that are grouped by the physical ID in the entry.

  • Check whether hyperthreading is supported

Run the cat /proc/cpuinfo command to check whether the siblings and CPU cores of the physical ID are the same. If the siblings and CPU cores are the same, the physical ID does not support hyperthreading or the hyperthreading is disabled.