Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is pseudo sharing?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what is pseudo-sharing". In daily operation, I believe that many people have doubts about what is pseudo-sharing. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is pseudo-sharing"! Next, please follow the editor to study!

What is pseudo sharing?

First of all, as we all know, with the difference in the development speed of CPU and memory, the speed of CPU is much faster than memory, so generally now CPU has added cache, which is often said to solve the problem of performance differences between different hardware.

In this way, it is very simple that adding the cache will inevitably lead to the problem of cache consistency, which leads to the introduction of the cache consistency protocol. (if you don't know, it is recommended to go to Baidu, there is no expansion here.)

CPU cache, as the name implies, the closer to CPU, the faster the cache speed, the smaller the capacity, and the higher the cost. Cache can generally be divided into L1, L2, L3 caches, according to performance: L1 > L2 > L3.

In fact, data is stored in rows inside the cache, which is called cache rows. Cache lines are generally 2 integer power bytes, generally ranging from 32 to 256 bytes, and the most common cache lines now have a size of 64 bytes.

So, according to this storage method, the data in the cache is not stored as individual variables, but multiple variables are placed on one line.

We often say an example is the array and linked list, the memory address of the array is contiguous, when we go to read the elements in the array, CPU will also load several subsequent elements in the array into the cache, in order to improve efficiency, but the linked list will not, that is to say, variables with continuous memory addresses can be put into a cache line.

When multiple threads modify multiple variables in a cache row concurrently, only one thread can operate the cache row at the same time, which will lead to performance degradation. This problem is called pseudo-sharing.

Why only one thread can operate? Let's take a practical chestnut to illustrate this situation:

Let's say there are two variables xQuery y in the cache, and they are already in different three levels of cache at the same time.

At this point, two threads An and B modify the variables x and y at Core1 and Core2 at the same time.

If thread A modifies the x variable in Core1's cache, the corresponding cache line in Core2 that caches the x variable will be invalidated because of the cache consistency protocol, and he will be forced to reload the variable from the main memory.

In this way, frequent access to the main memory, the cache is basically invalid, will lead to performance degradation, this is the problem of pseudo-sharing.

How to avoid it?

Now that you know what pseudo-sharing is, how can this be avoided?

Change the way rows are stored? Forget it.

The rest of the feasible way is to fill it. Wouldn't it be nice if I was the only data in this line?

This is indeed the case, and there are usually two solutions.

Byte filling

Before JDK8, the problem of pseudo-sharing can be avoided by filling in bytes, as shown in the following code:

Custom fill

Generally speaking, the cache line has 64 bytes. We know that a long is 8 bytes. After filling 5 long, the total is 48 bytes.

The object header in Java occupies 8 bytes in 32-bit system and 16 bytes in 64-bit system, so filling 5 longs can fill 64 bytes, that is, a cache line.

@ Contented comment

JDK8 and later versions of Java provide sun.misc.Contended annotations that can solve the problem of pseudo-sharing through @ Contented annotations.

Annotation mode

The @ Contented annotation adds 128byte padding, and the-XX:-RestrictContended option needs to be turned on to take effect.

Therefore, through the above two ways, you will find that the size of the object header and the size of the cache line are related to the number of bits of the operating system. JDK annotations help you solve this problem, so it is recommended to use annotations as far as possible.

Although the problem of pseudo-sharing is solved, this filling method also wastes cache resources, which is obviously only 8B in size, and 64B cache space is used, resulting in a waste of cache resources.

And we know that caching is small and expensive, and the choice of time and space should be considered at your own discretion.

Practical application

The operation classes of multiple atomic variables, such as AtomicLong and AtomicInteger, are provided in Java to update variables by CAS, but failure will lead to unlimited spin attempts, resulting in a waste of CPU resources.

In order to solve this disadvantage under high concurrency, LongAdder class is added to JDK8, which is used to solve the practical application of pseudo-sharing.

LongAdder inherits from Striped64 and maintains an Cell array internally. The core idea is to split the competition of a single variable. If a Cell competition fails in multithreading, it will go to other Cell and try again CAS.

Striped64 member variable

The real core of solving pseudo-sharing is the Cell array, where you can see that the Cell array uses Contented annotations.

As we mentioned above, the memory addresses of arrays are contiguous, so elements in the array are often placed in a cache line, which leads to pseudo-sharing problems and affects performance.

Using Contented to populate here avoids the problem of pseudo-sharing so that elements in the array no longer share a cache row.

At this point, the study of "what is pseudo-sharing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report