Reproduced from: http://ifeve.com/from-javaeye-cpu-cache/
http://ifeve.com/from-javaeye-false-sharing/
The CPU is the computer's brain, which is responsible for executing the program's instructions, and the memory is responsible for keeping the data, including the program itself. Memory is much slower than CPU, now getting one of the data in memory requires more than 200 CPU cycles (CPU cycles), and CPU registers typically 1 CPU cycles is enough.
In order to speed up, the Web browser caches previously browsed data in the native cache, and the traditional database or NoSQL database, in order to speed up the query, often sets a cache in memory and reduces the IO for disk (slow). The same memory and CPU speed is too far away, so the CPU designers to the CPU plus cache (CPU cache). If you need to work on the same batch of data many times, then put the data to the CPU closer to the cache, the program will bring a large speed increase. For example, to do a loop count, put the count variable into the cache, without having to access the data to the memory every loop. Here is a simple schematic diagram of the CPU cache:
With the development of multi-core, CPU cache is divided into three levels: L1, L2, L3. The smaller the level, the closer the CPU, the faster it is, and the smaller the capacity. L1 is the closest to the CPU, it has the smallest capacity, such as 32K, the fastest, each core has a L1 cache (accurately said each core has two L1 cache, a stored data l1d cache, a Save command l1i cache). L2 Cache is bigger, for example 256K, the speed is slower, in general, each core has a separate L2 cache;l3 cache is the largest level of level three cache, such as 12MB, but also the slowest level, the same CPU slot between the kernel share a L3 cache.
Just like the database cache, get the data first will be in the fastest cache to find data, if there is no hit (Cache miss) down the level to find, until three Cache can not find, that only to the memory to data. Again and again misses, the longer the representative takes the data to consume.
In order to efficiently access the cache, it is not easy to write a single piece of data to the cache. The cache is made up of cached rows, typically 64 bytes in a row. CPU access caching is done by the smallest unit of behavior. A Java Long is 8 bytes, so you can get 8 long variables from a single cache row. So if you access a long array, when a long is loaded into the cache, the other 7 will be loaded without consuming, so you can quickly enumerate the groups.
Since the typical CPU microarchitecture has a Level 3 cache, each kernel has its own private L1, L2 cache, what happens when multithreaded programming, another kernel thread wants to access the current core L1, L2 cached data?
There is a way to access the 1th core cache line directly through the 2nd kernel. This is feasible, but this method is not fast enough. Cross-core access needs to go through memory Controller, typically when the 2nd nucleus regularly accesses this data in the 1th core, so each time there is a trans-core consumption. Worse, it is possible that the 2nd and 1th cores are not in one slot, and that the memory controller bus bandwidth is limited and cannot carry so much data. So the CPU designers are more inclined to another approach: if the 2nd kernel needs this data, the 1th core will send the data directly, and the data will only need to be transmitted once.
So when will the transfer of cached rows occur? The answer is simple: when a kernel needs to read a dirty cached row of another kernel, it occurs. But the former how to judge the latter's cache line has been dirty (write).
The above questions will be answered in detail below. First you need to talk about a protocol---mesi protocol. Now the mainstream processor is used to ensure the coherence of the cache and the coherence of the memory. The M, E, S, and I represent the four states of the cache line when the Mesi protocol is used:
M (Modified, Modified): The local processor has modified the cache line, which is a dirty row, and its contents are not the same as the contents in memory. And this cache has only a local copy (proprietary).
E (Proprietary, Exclusive): The cache line content is the same as in memory, and no other processor has this row of data.
S (Share, shared): Caching row content as in memory, it is possible that other processors also have copies of this cached row.
I (Invalid, Invalid): Cache line Invalid, cannot be used.
The following is a simple description of how the four states of the cached row are converted:
Initial: At first, the cache row did not load any data, so it was in the I state.
Local write: If the local processor writes data to a cached row in the I state, the state of the cached row becomes m.
Local read: If the local processor reads a cached row in the I state, it is clear that the cache has no data for it. This is in two cases: (1) There is no data in the cache of other processors, load data from memory to this cache row, and then set it to the E state, which means that only my family has this data, the other processors do not have; (2) The state of the cached row is set to S state if the other processor has the row data cached. P.S. If the cached row in the M state is written/read out by the local processor, the state will not change.
Remote read: Suppose there are two processors C1 and C2. If C2 needs to read the cache line content of another processor C1, C1 needs to send the contents of its cached rows through the memory controller (Memory Controller) to the C2,C2 and set the corresponding cache row state to S. Before setting, memory also has to get the data from the bus and save it.
Remote write: In fact, it is not a remote write, but C2 get C1 data, not to read, but to write, is also a local write, but C1 also have the copy of the data, this how to do it. C2 will issue a RFO (Request for owner) request, which requires permission to own this row of data, and the corresponding cache line for the other processor is set to I, who does not move the row of data except it itself. This guarantees the security of the data, while processing RFO requests and setting I will bring a significant performance drain to the write operation.
As you know, write operations are expensive, especially when you need to send RFO messages. When the program is written, when will the RFO request occur? There are two kinds of the following:
1. The work of a thread moves from one processor to another, and all of the cached rows it operates need to be moved to a new processor. Then, if you write the cache line again, the cache line has multiple copies on the different cores, and you need to send a RFO request.
2. Two different processors do need to operate the same cache line.
In Java programs, the members of an array are also contiguous in the cache. In fact, the adjacent member variables from the Java object are also loaded into the same cache row. If multiple threads manipulate different member variables, but the same cache row, the pseudo share (false sharing) problem occurs.
For example: A thread running on processor core 1 wants to update the value of the variable x, while another thread running on the processor Core 2 wants to update the value of the variable Y. However, these two frequently changed variables are in the same cache row. Two threads will take turns to send RFO messages, taking up the ownership of this cache line. When core 1 gets ownership to begin updating X, the corresponding cache line for Core 2 needs to be set to I state. When Core 2 gets ownership to start updating Y, the corresponding cache line for Core 1 needs to be set to I state (failure state). Capturing ownership in turns not only brings a lot of RFO messages, but if a thread needs to read this line of data, L1 and L2 cache are invalid data, only the L3 cache is synchronized good data. Read the L3 data very affect performance, the worse case is the cross slot read, L3 to miss, can only load from memory.
On the surface x and Y are all operated by independent threads, and there is no relationship between the two operations. It's just that they share a cache row, but all competing conflicts come from sharing.
So how to avoid false sharing it. A cache line has 64 bytes, while the Java program's object header is fixed to 8 bytes (32-bit system) or 12-byte (64-bit system defaults to open compression, not open compression to 16 bytes). Just fill in the 6*8=48 byte with 6 useless long integers, let the different variables in different cache rows, you can avoid pseudo-share (64-bit system exceeds the cache line of 64 bytes does not matter, as long as the different threads do not operate the same cache line can), this method is called complement (Padding). For example:
Public final Static class Volatilelong {public
volatile Long value = 0L;
Public long P1, p2, p3, P4, P5, P6;
Pseudo-sharing is very easy to happen in multi-core programming, and it is relatively covert. For example, in the JDK's linkedblockingqueue, there is a reference head pointing to the queue header and a reference to the end of the queue. This queue is often made in asynchronous programming, and the values of these two references are often modified by different threads, but they are likely to be in the same cache line, resulting in spurious sharing. The more threads, the more cores, the greater the negative effects on performance.
Some Java compilers will not use the padded data, even if the 6 long integers in the sample code are optimized at compile time, you can add some code to the program to prevent the compiler from being optimized.
public static long Preventfromoptimization (Volatilelong v) {return
V.P1 + v.p2 + v.p3 + v.p4 + v.p5 + v.p6;
}
In addition, due to the GC problem of Java. The data is likely to change in memory and the position of the corresponding CPU cache line, so you should pay attention to the effect of GC when using pad.