Understanding Cacheline and Writing better Java

Source: Internet
Author: User

Today we looked at a lot of information, mainly to find out what is the relationship between Java and cacheline and how we write better Java programs for Cacheline.

CPU and memory

CPU is the brain of the computer, it is responsible for the operation, memory is the data, it provides data for the CPU. The reason for ignoring other storage devices here is to simplify the model. Assuming we're dealing with a CPU with two cores, our model looks like this:

CPU computing core does not directly deal with memory, it will be directly from the cache to take the data, if the cache is not received, the professional point is that the cache misses the time to go to the memory to take, but also update the cache. In this process, the CPU does not just read the content of one of the bytes or words needed, but reads a piece of content by policy. A typical processor strategy is to read data that is less than 2048 bytes in the forward direction. As shown, the L1 cache (first-level cache) is closest to the CPU core, and the capacity is minimal and the cost is the highest, which is used by the kernel independently. The L2 cache is far away, and the capacity is more than L1 uppercase but still belongs to a separate kernel. The L3 cache is the farthest and slowest cache, which is shared for all cores.

Cache rows

in the previous section we covered the model between CPU and memory, and this section describes the next cache line. The CPU reads from the cache not a byte or a word read, but a line, or a piece of reading . The CPU is designed like this, we think, the correlation of neighboring data is often very large, so the design can increase the hit rate of the cache, also reduce the number of access to the cache. The row mentioned above is called a cache line. The typical size is 32-256 bytes, the most common of which is 64 bytes. This is the minimum granularity of cache consistency, and if one byte in a row is modified and written back to memory within a single bit of content, the cache row of the other kernel will be invalidated by the flag bit. Assuming that two cores are executing different threads and operating the same cache line, the A thread modifies the first byte of the cache line, and the B thread needs to access the second byte, which in fact has been performed once and in memory synchronously, ensuring that the segment is consistent with the data in memory. This process, however, does not have a relationship with the bytes accessed by the first thread. This time some students have doubts, then if the two kernel thread at the same time to operate the segment, that is, no one who first who after the situation, what will happen. In fact, there is another concept, called Cache Consistency Protocol (caches coherency). That is, we have a guarantee that at any one time, the content in the cache segment of any level is equivalent to the contents of its corresponding memory.

About Atomicreference

atomicreference is introduced by JAVA5 for atomic manipulation of an object reference. We can see that the implementation of Atomicreference is using CAS technology to modify the directive level of the reference and then use the memory barrier feature of volatile to ensure that the referenced modifications are immediately visible to other threads. It is mentioned here that a volatile modified variable will insert a store barrier after it is written, and a load barrier before reading. The store barrier guarantees that the write operation is immediately visible to the subsequent thread. The load barrier guarantees that all writes before the read operation take effect immediately. However, Atomicreference does not avoid the cache hit-ratio issue that is associated with cache rows. A Atomicreference object includes a volatile object reference, which accounts for 4 bytes in a 32-bit operating system and 8 bytes in a 64-bit operating system. Although multiple threads do not have concurrency problems with the same Atomicreference object operation, there is a possibility that when multiple threads are operating on multiple atomicreference, there may be a cache hit ratio problem. With the model above we assume that two atomicreference variables A and B are in the same contiguous area of memory, and when the thread executing in core 1 is operating on a variable, the CPU reads the A variable into the core 1 cache area, and incidentally reads the B variable into the cache area. This is the same cache line as the A variable. Another thread executed by Core 2 performs a simultaneous operation on B, at which time the cache row is invalidated and a read memory operation occurs.

Cache row Padding

the Exchanger class is JAVA5 provides a tool class for exchanging data between multiple threads , let's look at the implementation of Exchanger's internal class slot:

?
1234  privatestatic final class Slot extends AtomicReference<Object> {        // Improve likelihood of isolation on <= 64 byte cache lines        longq0, q1, q2, q3, q4, q5, q6, q7, q8, q9, qa, qb, qc, qd, qe;    }

The slot simply inherits the Atomicreference class and declares 15 variables of type long. If you do not understand the meaning of this useless variable, the declaration of 15 variables, a long type of 8 bytes, plus the above reference in the 64-bit operating system environment is 128 bytes, 32-bit operating system environment of 124 bytes is no problem, Because two slot type variables cannot be in the same cache row, this solves the cache row failure problem in multi-core CPU environments.

Understanding Cacheline and Writing better Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.