1. 程式人生 > >Java 7與偽共享的新仇舊恨

Java 7與偽共享的新仇舊恨

我有個疑問,就是怎麼才能確保底層能夠正確的填充?
下面是我通過工具JOL生成的物件佈局:

# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

com.lmax.disruptor.localtest.FalseSharing$PaddedAtomicLong object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 (alignment/padding gap) N/A
16 8 long AtomicLong.value N/A
24 8 long PaddedAtomicLong.p1 N/A
32 8 long PaddedAtomicLong.p2 N/A
40 8 long PaddedAtomicLong.p3 N/A
48 8 long PaddedAtomicLong.p4 N/A
56 8 long PaddedAtomicLong.p5 N/A
64 8 long PaddedAtomicLong.p6 N/A
Instance size: 72 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

發現PaddedAtomicLong整個物件本身會佔到72位元組,那麼載入這個物件本身的64位元組,那會填充哪些部分?如果只是填充靠近value附近的,那會不會填充別的資料進來?即使你加了7個long的欄位,但底層如何不會載入其他的資料保證64位元組.在disruptor裡面,有如下物件:

/usr/lib/jvm/jdk1.8.0_25/bin/java -Djol.tryWithSudo=true -Didea.launcher.port=7533 -Didea.launcher.bin.path=/usr/local/intellij/idea-IU-141.1532.4/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/jdk1.8.0_25/jre/lib/deploy.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/management-agent.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/jfxswt.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/jce.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/plugin.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/javaws.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/resources.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/rt.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/localedata.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/nashorn.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/zipfs.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/sunec.jar:/usr/lib/jvm/jdk1.8.0_25/jre/lib/ext/dnsns.jar:/home/danny/kamon-learning/disruptor/build/classes/test:/home/danny/kamon-learning/disruptor/build/classes/main:/home/danny/kamon-learning/disruptor/build/resources/test:/home/danny/kamon-learning/disruptor/build/resources/main:/home/danny/.gradle/caches/modules-2/files-2.1/junit/junit/4.5/7059d3d3bedc1fca9b33cde8503095b3576a0bba/junit-4.5.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.jmock/jmock-junit4/2.5.1/d60d391d7df84fb98ebfbec99de7365e50a6f935/jmock-junit4-2.5.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.jmock/jmock-legacy/2.5.1/dab10cb736c6fb55c0c9afa648820721f6a997cb/jmock-legacy-2.5.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.jmock/jmock/2.5.1/1dca0b1353964964a64cbcca379bf813933c046/jmock-2.5.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/junit/junit-dep/4.4/a7580ffb722a7fa34e3748e7139bc1db9312542c/junit-dep-4.4.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/1.0/9b473564e792c2bdf1449da1f0b1b5bff9805704/objenesis-1.0.jar:/home/danny/.gradle/caches/modules-2/files-2.1/cglib/cglib-nodep/2.1_3/58d3be5953547c0019e5704d6ed4ffda3b0c7c66/cglib-nodep-2.1_3.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.1/860340562250678d1a344907ac75754e259cdb14/hamcrest-core-1.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-library/1.1/b988c01468e3398d46678a2eb48aeb5bde271e9f/hamcrest-library-1.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.hdrhistogram/HdrHistogram/1.2.1/c594a5fbfd9046e0b493fbf351b75eeecc0195f0/HdrHistogram-1.2.1.jar:/home/danny/.gradle/caches/modules-2/files-2.1/org.openjdk.jol/jol-core/0.6/93befc07f05791757dc7b5e7ffc480fd30a4a25c/jol-core-0.6.jar:/home/danny/kamon-learning/disruptor/lib/jol-core-0.6.jar:/usr/local/intellij/idea-IU-141.1532.4/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain com.lmax.disruptor.localtest.JOLSample_04_Inheritance
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with escalated privileges: Stream closed
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

com.lmax.disruptor.SingleProducerSequencer object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 12 (object header) N/A
12 4 int AbstractSequencer.bufferSize N/A
16 4 WaitStrategy AbstractSequencer.waitStrategy N/A
20 4 Sequence AbstractSequencer.cursor N/A
24 4 Sequence[] AbstractSequencer.gatingSequences N/A
28 4 (alignment/padding gap) N/A
32 8 long SingleProducerSequencerPad.p1 N/A
40 8 long SingleProducerSequencerPad.p2 N/A
48 8 long SingleProducerSequencerPad.p3 N/A
56 8 long SingleProducerSequencerPad.p4 N/A
64 8 long SingleProducerSequencerPad.p5 N/A
72 8 long SingleProducerSequencerPad.p6 N/A
80 8 long SingleProducerSequencerPad.p7 N/A
88 8 long SingleProducerSequencerFields.nextValue N/A
96 8 long SingleProducerSequencerFields.cachedValue N/A
104 8 long SingleProducerSequencer.p1 N/A
112 8 long SingleProducerSequencer.p2 N/A
120 8 long SingleProducerSequencer.p3 N/A
128 8 long SingleProducerSequencer.p4 N/A
136 8 long SingleProducerSequencer.p5 N/A
144 8 long SingleProducerSequencer.p6 N/A
152 8 long SingleProducerSequencer.p7 N/A
Instance size: 160 bytes
Space losses: 4 bytes internal + 0 bytes external = 4 bytes total

官方說考慮到快取行為64位元組和128位元組的情況,那64位元組好理解,左右的資料都可以保證載入的任何方向都只有填充的long資料,但128位元組怎麼辦,如果載入方向的往下載入那麼就會載入到其他資料?