Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

Can a bird eat and poop at the same time? For someone who has owned a parrot, the answer is yes.

From the beak to the large intestine, the whole process is sequential. Although some twists and turns, but the direction is single, the exit is certain. If the bird can overcome some psychological discomfort, it can do it.

The situation is awkward for some primitive species. Hydra, for example, belong to coelenterates and reproduce asexually. I mean, those are a lot of words, but there’s only one cavity in it. Eat where you eat, eat where you eat.

As a species on Earth, we are, in the long run, its closest relatives.

As advanced mammals, we can breathe, talk, hear and see at the same time. You can do what a parrot does if you want to.

This information is collected and sent to the brain for processing. It could be like deep learning, storing a bunch of weights, but how that information is processed, we don’t know yet.

So programmers turn to computers, which, after all, are like toys compared to the human brain, which is philosophy at the end of the road.


As we all know, it is better to concentrate on one thing than to do a lot of things well. This is not to say that a person is incapable of doing everything well, but switching between tasks is resource-intensive.

Your brain gets used to one task, then suddenly switches to another, and it takes a long time to switch to the new one.

Sometimes I write so much code that I start stuttering. But I kept talking and talking, and then I got it back.

So, everyone hates the odd job. Especially those who constantly give you small, unrelated tasks.

At the end of the day, I feel like I’ve done a lot, but I haven’t achieved anything. I feel like I’m a loser.

Don’t worry, let’s see what the CPU does.


Instead of processing one task at a time, the CPU processes tasks by assigning each thread a slice of CPU time, and then switching to the next thread when the slice runs out.

The time slice is very short, usually only a few tens of milliseconds, and the CPU executes by constantly switching threads, but we hardly feel the task stall. For humans, a high-quality game only needs 60 frames per second to be smooth.

This time is still considerable, especially in the case of a lot of process context switching, it is easy to cause the CPU to spend a lot of time on registers, kernel stack, virtual memory and other resources to save and restore, thus greatly reducing the time of the actual running process.

For Linux. Programs typically run in user and kernel states. The CPU pairs in kernel state are further subdivided according to the context, resulting in the following three states:

  • Kernel-state, running in the process context, the kernel represents the process running in kernel space.
  • Kernel-state, running in an interrupt context, where the kernel represents hardware running in kernel space.
  • User mode, running in user space

Let’s take a look at the Linux top command and see how the kernel and user modes are displayed.

As shown above, us means user; Sy means system. Represents the user state and kernel state respectively.

If the SY footprint is too high, there may be too many context switches and interrupts.

So what is context?

The so-called context, to put it bluntly, is an environment. You take your lunch box to the canteen and your toilet paper to the toilet. If you mess up, taking your lunch box to the bathroom doesn’t feel normal. The operating system assigns a context to each process for storing code, data, user stacks, shared storage, registers, process control blocks, and so on.

Don’t take care of the details inside first, anyway, the content is a lot, switching is certainly to have a copy. For example, toilet paper is in the third-floor cubicle of the bedroom cabinet at home.

This is what the vmstat command shows in the columns. Cs is not short for CSGO, it’s context Switch.

Within each process, you can also see the cumulative value.

[root@localhost ~]# cat /proc/2788/status. voluntary_ctxt_switches: 93950 nonvoluntary_ctxt_switches: 171204Copy the code

If cs is too high, there are too many threads or processes open.


There are two types of context switches.

Concessional and preemptive context switches.

Let’s start with concession context switches. Consider the CAS operation in Java.

Cas requires a loop in addition to the original compare and switch instruction support.

public final long getAndAddLong(Object var1, long var2, long var4) {
        long var6;
        do {
            var6 = this.getLongVolatile(var1, var2);
        } while(!this.compareAndSwapLong(var1, var2, var6, var6 + var4));
        return var6;
}
Copy the code

Code is placed in a loop, and in the case of high concurrency, if many threads repeatedly try to update a variable, but have not been successful, the loop will put a lot of stress on the CPU. Therefore, concessionary context switch refers to the active release of CPU by the executing thread, which is proportional to the severity of lock contention and can be avoided by reducing lock contention.

Preemptive context switching, on the other hand, is when a thread is forced to abandon the CPU due to the exhausted time slice, or is preempted by another thread with a higher priority. Generally, the number of threads is larger than the number of available CPU cores. You can adjust the number of threads to reduce the number of threads.

So why is a Java thread faster than a multiprocess? Because the Java thread is also a lightweight process by nature, but its virtual memory and other information is shared, only need to switch the thread private data, registers and other non-shared data. Even then, it can take a long time.

This context switch to process and quantity can also be observed using the perf command. Such as:

Trace all context switches until Ctrl-c:
perf record -e context-switches -c 1 -a

# Include the original Settings used (see: man perf_event_open) :
perf record -vv -e context-switches -a

# Example context switch using stack trace up to Ctrl-c:
perf record -e context-switches -ag
Copy the code

Use perf Report to view the results.

Computers are still most efficient when they concentrate on one thing. To some extent, you are the boss of the computer. Your computer won’t necessarily be as efficient if you keep it doing chores and treating it like a chore.

Someone smart enough to know that switching back and forth has a huge impact on your productivity. Rule out the stupid attributes, and you’re left with the bad: give you a bunch of crap, wear you out, and end up being results-oriented — which your boss must be doing on purpose.

Xjjdog is a public account that doesn’t allow programmers to get sidetracked. Focus on infrastructure and Linux. Ten years architecture, ten billion daily flow, and you discuss the world of high concurrency, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further communication.