Skip to main content

java rabbitmq 初始化

· 2 min read

背景

了解java的spring boot 的rabbitmq的启动流程

堆栈

declareQueues:700, RabbitAdmin (org.springframework.amqp.rabbit.core)
lambda$initialize$12:606, RabbitAdmin (org.springframework.amqp.rabbit.core)
doInRabbit:-1, 1826902085 (org.springframework.amqp.rabbit.core.RabbitAdmin$$Lambda$965)
invokeAction:2151, RabbitTemplate (org.springframework.amqp.rabbit.core)
doExecute:2110, RabbitTemplate (org.springframework.amqp.rabbit.core)
execute:2062, RabbitTemplate (org.springframework.amqp.rabbit.core)
execute:2042, RabbitTemplate (org.springframework.amqp.rabbit.core)
initialize:604, RabbitAdmin (org.springframework.amqp.rabbit.core)
lambda$null$10:532, RabbitAdmin (org.springframework.amqp.rabbit.core)
doWithRetry:-1, 999782961 (org.springframework.amqp.rabbit.core.RabbitAdmin$$Lambda$957)
doExecute:287, RetryTemplate (org.springframework.retry.support)
execute:164, RetryTemplate (org.springframework.retry.support)
lambda$afterPropertiesSet$11:531, RabbitAdmin (org.springframework.amqp.rabbit.core)
onCreate:-1, 1185831500 (org.springframework.amqp.rabbit.core.RabbitAdmin$$Lambda$950)
lambda$onCreate$0:38, CompositeConnectionListener (org.springframework.amqp.rabbit.connection)
accept:-1, 1588281004 (org.springframework.amqp.rabbit.connection.CompositeConnectionListener$$Lambda$956)
forEach:803, CopyOnWriteArrayList (java.util.concurrent)
onCreate:38, CompositeConnectionListener (org.springframework.amqp.rabbit.connection)
createConnection:757, CachingConnectionFactory (org.springframework.amqp.rabbit.connection)
createConnection:216, ConnectionFactoryUtils (org.springframework.amqp.rabbit.connection)
doExecute:2089, RabbitTemplate (org.springframework.amqp.rabbit.core)
execute:2062, RabbitTemplate (org.springframework.amqp.rabbit.core)
execute:2042, RabbitTemplate (org.springframework.amqp.rabbit.core)
declareExchange:221, RabbitAdmin (org.springframework.amqp.rabbit.core)
cdpOrderTopicExchange:27, RabbitConfig (com.patpat.mms.mdp.base.core.rest.config)
CGLIB$cdpOrderTopicExchange$7:-1, RabbitConfig$$EnhancerBySpringCGLIB$$65dbc353 (com.patpat.mms.mdp.base.core.rest.config)
invoke:-1, RabbitConfig$$EnhancerBySpringCGLIB$$65dbc353$$FastClassBySpringCGLIB$$bdc910b3 (com.patpat.mms.mdp.base.core.rest.config)
invokeSuper:244, MethodProxy (org.springframework.cglib.proxy)
intercept:331, ConfigurationClassEnhancer$BeanMethodInterceptor (org.springframework.context.annotation)
cdpOrderTopicExchange:-1, RabbitConfig$$EnhancerBySpringCGLIB$$65dbc353 (com.patpat.mms.mdp.base.core.rest.config)
invoke0:-1, NativeMethodAccessorImpl (jdk.internal.reflect)
invoke:62, NativeMethodAccessorImpl (jdk.internal.reflect)
invoke:43, DelegatingMethodAccessorImpl (jdk.internal.reflect)
invoke:566, Method (java.lang.reflect)
instantiate:154, SimpleInstantiationStrategy (org.springframework.beans.factory.support)
instantiate:652, ConstructorResolver (org.springframework.beans.factory.support)
instantiateUsingFactoryMethod:637, ConstructorResolver (org.springframework.beans.factory.support)
instantiateUsingFactoryMethod:1341, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBeanInstance:1181, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
doCreateBean:556, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
createBean:516, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
lambda$doGetBean$0:324, AbstractBeanFactory (org.springframework.beans.factory.support)
getObject:-1, 1735872041 (org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$295)
getSingleton:234, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
doGetBean:322, AbstractBeanFactory (org.springframework.beans.factory.support)
getBean:202, AbstractBeanFactory (org.springframework.beans.factory.support)
preInstantiateSingletons:897, DefaultListableBeanFactory (org.springframework.beans.factory.support)
finishBeanFactoryInitialization:879, AbstractApplicationContext (org.springframework.context.support)
refresh:551, AbstractApplicationContext (org.springframework.context.support)
refresh:755, SpringApplication (org.springframework.boot)
refresh:747, SpringApplication (org.springframework.boot)
refreshContext:402, SpringApplication (org.springframework.boot)
run:312, SpringApplication (org.springframework.boot)
loadContext:120, SpringBootContextLoader (org.springframework.boot.test.context)
loadContextInternal:99, DefaultCacheAwareContextLoaderDelegate (org.springframework.test.context.cache)
loadContext:124, DefaultCacheAwareContextLoaderDelegate (org.springframework.test.context.cache)
getApplicationContext:123, DefaultTestContext (org.springframework.test.context.support)
setUpRequestContextIfNecessary:190, ServletTestExecutionListener (org.springframework.test.context.web)
prepareTestInstance:132, ServletTestExecutionListener (org.springframework.test.context.web)
prepareTestInstance:244, TestContextManager (org.springframework.test.context)
createTest:227, SpringJUnit4ClassRunner (org.springframework.test.context.junit4)
runReflectiveCall:289, SpringJUnit4ClassRunner$1 (org.springframework.test.context.junit4)
run:12, ReflectiveCallable (org.junit.internal.runners.model)
methodBlock:291, SpringJUnit4ClassRunner (org.springframework.test.context.junit4)
runChild:246, SpringJUnit4ClassRunner (org.springframework.test.context.junit4)
runChild:97, SpringJUnit4ClassRunner (org.springframework.test.context.junit4)
run:331, ParentRunner$4 (org.junit.runners)
schedule:79, ParentRunner$1 (org.junit.runners)
runChildren:329, ParentRunner (org.junit.runners)
access$100:66, ParentRunner (org.junit.runners)
evaluate:293, ParentRunner$2 (org.junit.runners)
evaluate:61, RunBeforeTestClassCallbacks (org.springframework.test.context.junit4.statements)
evaluate:70, RunAfterTestClassCallbacks (org.springframework.test.context.junit4.statements)
evaluate:306, ParentRunner$3 (org.junit.runners)
run:413, ParentRunner (org.junit.runners)
run:190, SpringJUnit4ClassRunner (org.springframework.test.context.junit4)
run:137, JUnitCore (org.junit.runner)
startRunnerWithArgs:69, JUnit4IdeaTestRunner (com.intellij.junit4)
execute:38, IdeaTestRunner$Repeater$1 (com.intellij.rt.junit)
repeat:11, TestsRepeater (com.intellij.rt.execution.junit)
startRunnerWithArgs:35, IdeaTestRunner$Repeater (com.intellij.rt.junit)
prepareStreamsAndStart:235, JUnitStarter (com.intellij.rt.junit)
main:54, JUnitStarter (com.intellij.rt.junit)

rabbit mq 多个admin

arroyo 编译和使用

· 2 min read

背景

github 地址 Arroyo 是分布式流式引擎,使用Rust编写.因为要试用,所以写了这篇

编译

  • 安装rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 拉取代码
git clone https://github.com/ArroyoSystems/arroyo.git
  • 安装postgresql
sudo apt install postgresql

配置路径:

/etc/postgresql/14/main/pg_hba.conf
## 链接postgresql
sudo -u postgres psql
## 创建database arroyo
create database arroyo;
## 执行sql , 要执行这个路径的
source arroyo/arroyo-api/migrations/V1__initial.sql

##postgresql 创建用户
create user arroyo with password 'arroyo';
## 重启postgresql
sudo systemctl restart postgresql.service
  • 编译
## 切换目录
cd arroyo
## 编译
cargo build
## 如果编译不了,用
cargo build --no-default-features

编译结果:

  Compiling datafusion-optimizer v20.0.0
Compiling datafusion v20.0.0
Compiling arroyo-sql v0.1.0 (/home/dai/rust/arroyo/arroyo-sql)
Compiling arroyo-sql-macro v0.1.0 (/home/dai/rust/arroyo/arroyo-sql-macro)
Compiling arroyo-sql-testing v0.1.0 (/home/dai/rust/arroyo/arroyo-sql-testing)
Finished dev [unoptimized + debuginfo] target(s) in 2m 13s

错误和处理

发现没有ssl的库,我的系统是Ubuntu,所以执行sudo apt install libssl-dev , 如果编译不了用cargo build --no-default-features 试试

相关阅读

direct memory in java

· 28 min read

背景

lucene 使用了direct memory,这类内存是非jvm直接管理的内存

DirectByteBufferR 是read only 版本的DirectByteBuffer,所以DirectByteBufferDirectByteBufferR是差不多的类

lucene的mmap

lucene 会使用DirectByteBufferR,这里申请的内存地址是140063879776283

main[1] dump receiver
receiver = {
$assertionsDisabled: true
java.nio.DirectByteBuffer.ARRAY_BASE_OFFSET: 16
java.nio.DirectByteBuffer.UNALIGNED: true
java.nio.DirectByteBuffer.att: instance of java.nio.DirectByteBufferR(id=1500)
java.nio.DirectByteBuffer.cleaner: null
java.nio.DirectByteBuffer.$assertionsDisabled: true
java.nio.MappedByteBuffer.fd: instance of java.io.FileDescriptor(id=1501)
java.nio.MappedByteBuffer.isSync: false
java.nio.MappedByteBuffer.SCOPED_MEMORY_ACCESS: instance of jdk.internal.misc.ScopedMemoryAccess(id=1502)
java.nio.ByteBuffer.ARRAY_BASE_OFFSET: 16
java.nio.ByteBuffer.hb: null
java.nio.ByteBuffer.offset: 0
java.nio.ByteBuffer.isReadOnly: true
java.nio.ByteBuffer.bigEndian: false
java.nio.ByteBuffer.nativeByteOrder: true
java.nio.ByteBuffer.$assertionsDisabled: true
java.nio.Buffer.UNSAFE: instance of jdk.internal.misc.Unsafe(id=1503)
java.nio.Buffer.SCOPED_MEMORY_ACCESS: instance of jdk.internal.misc.ScopedMemoryAccess(id=1502)
java.nio.Buffer.SPLITERATOR_CHARACTERISTICS: 16464
java.nio.Buffer.mark: -1
java.nio.Buffer.position: 0
java.nio.Buffer.limit: 7
java.nio.Buffer.capacity: 7
java.nio.Buffer.address: 140063879776283
java.nio.Buffer.segment: null
java.nio.Buffer.$assertionsDisabled: true
}
main[1] where
[1] org.apache.lucene.store.ByteBufferGuard.getByte (ByteBufferGuard.java:118)
[2] org.apache.lucene.store.ByteBufferIndexInput$SingleBufferImpl.readByte (ByteBufferIndexInput.java:593)
[3] org.apache.lucene.codecs.lucene90.Lucene90NormsProducer$3.longValue (Lucene90NormsProducer.java:388)
[4] org.apache.lucene.search.LeafSimScorer.getNormValue (LeafSimScorer.java:47)
[5] org.apache.lucene.search.LeafSimScorer.score (LeafSimScorer.java:60)
[6] org.apache.lucene.search.TermScorer.score (TermScorer.java:75)
[7] org.apache.lucene.search.TopScoreDocCollector$SimpleTopScoreDocCollector$1.collect (TopScoreDocCollector.java:73)
[8] org.apache.lucene.search.Weight$DefaultBulkScorer.scoreAll (Weight.java:305)
[9] org.apache.lucene.search.Weight$DefaultBulkScorer.score (Weight.java:247)
[10] org.apache.lucene.search.BulkScorer.score (BulkScorer.java:38)
[11] org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:770)
[12] org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:693)
[13] org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:687)
[14] org.apache.lucene.search.IndexSearcher.searchAfter (IndexSearcher.java:532)
[15] org.apache.lucene.search.IndexSearcher.search (IndexSearcher.java:542)
[16] org.apache.lucene.demo.SearchFiles.doPagingSearch (SearchFiles.java:180)
[17] org.apache.lucene.demo.SearchFiles.main (SearchFiles.java:150)

查看_7.cfs 文件:

hexdump -C /home/dai/index/_7.cfs
00000000 3f d7 6c 17 14 4c 75 63 65 6e 65 39 30 43 6f 6d |?.l..Lucene90Com|
00000010 70 6f 75 6e 64 44 61 74 61 00 00 00 00 6b f0 66 |poundData....k.f|
00000020 56 c3 12 5b 07 08 12 3a 32 4d 4b 92 f8 00 00 00 |V..[...:2MK.....|
00000030 3f d7 6c 17 17 4c 75 63 65 6e 65 39 30 46 69 65 |?.l..Lucene90Fie|
00000040 6c 64 73 49 6e 64 65 78 4d 65 74 61 00 00 00 01 |ldsIndexMeta....|
00000050 6b f0 66 56 c3 12 5b 07 08 12 3a 32 4d 4b 92 f8 |k.fV..[...:2MK..|
00000060 00 80 80 05 07 00 00 00 0a 00 00 00 02 00 00 00 |................|
00000070 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |0...............|
00000080 00 00 e0 40 00 00 00 00 00 00 00 00 00 30 00 00 |...@.........0..|
00000090 00 00 00 00 00 36 00 00 00 00 00 00 00 00 00 5d |.....6.........]|
000000a0 43 00 00 00 00 00 00 00 00 00 30 00 00 00 00 00 |C.........0.....|
000000b0 00 00 13 01 00 00 00 00 00 00 01 01 07 c0 28 93 |..............(.|
000000c0 e8 00 00 00 00 00 00 00 00 46 80 fe 32 00 00 00 |.........F..2...|
000000d0 3f d7 6c 17 19 4c 75 63 65 6e 65 39 30 50 6f 69 |?.l..Lucene90Poi|
000000e0 6e 74 73 46 6f 72 6d 61 74 49 6e 64 65 78 00 00 |ntsFormatIndex..|
000000f0 00 00 6b f0 66 56 c3 12 5b 07 08 12 3a 32 4d 4b |..k.fV..[...:2MK|
00000100 92 f8 00 32 c0 28 93 e8 00 00 00 00 00 00 00 00 |...2.(..........|
00000110 0a 2f 94 55 00 00 00 00 3f d7 6c 17 18 4c 75 63 |./.U....?.l..Luc|
00000120 65 6e 65 39 30 50 6f 69 6e 74 73 46 6f 72 6d 61 |ene90PointsForma|
00000130 74 4d 65 74 61 00 00 00 00 6b f0 66 56 c3 12 5b |tMeta....k.fV..[|
00000140 07 08 12 3a 32 4d 4b 92 f8 00 01 00 00 00 3f d7 |...:2MK.......?.|
00000150 6c 17 03 42 4b 44 00 00 00 09 01 01 80 04 08 01 |l..BKD..........|
00000160 80 00 01 81 b4 f6 00 0f 80 00 01 81 b5 2b 3d 9d |.............+=.|
00000170 07 07 01 32 00 00 00 00 00 00 00 33 00 00 00 00 |...2.......3....|
00000180 00 00 00 ff ff ff ff 44 00 00 00 00 00 00 00 72 |.......D.......r|
00000190 00 00 00 00 00 00 00 c0 28 93 e8 00 00 00 00 00 |........(.......|
000001a0 00 00 00 09 71 1c 79 00 3f d7 6c 17 12 42 6c 6f |....q.y.?.l..Blo|
000001b0 63 6b 54 72 65 65 54 65 72 6d 73 4d 65 74 61 00 |ckTreeTermsMeta.|
000001c0 00 00 00 6b f0 66 56 c3 12 5b 07 08 12 3a 32 4d |...k.fV..[...:2M|
000001d0 4b 92 f8 0a 4c 75 63 65 6e 65 39 30 5f 30 3f d7 |K...Lucene90_0?.|
000001e0 6c 17 1b 4c 75 63 65 6e 65 39 30 50 6f 73 74 69 |l..Lucene90Posti|
000001f0 6e 67 73 57 72 69 74 65 72 54 65 72 6d 73 00 00 |ngsWriterTerms..|
00000200 00 00 6b f0 66 56 c3 12 5b 07 08 12 3a 32 4d 4b |..k.fV..[...:2MK|
00000210 92 f8 0a 4c 75 63 65 6e 65 39 30 5f 30 80 01 02 |...Lucene90_0...|
00000220 02 8c 01 0c db 01 03 62 af 05 67 cf 09 6d 95 14 |.......b..g..m..|
00000230 c2 02 a5 01 06 01 30 03 cd b1 69 37 3f d7 6c 17 |......0...i7?.l.|
00000240 03 46 53 54 00 00 00 08 01 0d 14 95 6d 09 cf 67 |.FST........m..g|
00000250 05 af 62 03 01 db 0c 00 00 01 00 07 02 a2 37 07 |..b...........7.|
00000260 07 16 2f 68 6f 6d 65 2f 64 61 69 2f 64 6f 63 73 |../home/dai/docs|
00000270 2f 61 61 61 2e 74 78 74 1f 2f 68 6f 6d 65 2f 64 |/aaa.txt./home/d|
00000280 61 69 2f 64 6f 63 73 2f 69 6e 64 65 78 2f 77 72 |ai/docs/index/wr|
00000290 69 74 65 2e 6c 6f 63 6b 38 3f d7 6c 17 03 46 53 |ite.lock8?.l..FS|
000002a0 54 00 00 00 08 01 03 37 a2 02 00 00 01 49 00 00 |T......7.....I..|
000002b0 00 00 00 00 00 c9 07 00 00 00 00 00 00 c0 28 93 |..............(.|
000002c0 e8 00 00 00 00 00 00 00 00 c1 1b ff e4 00 00 00 |................|
000002d0 3f d7 6c 17 18 4c 75 63 65 6e 65 39 30 50 6f 69 |?.l..Lucene90Poi|
000002e0 6e 74 73 46 6f 72 6d 61 74 44 61 74 61 00 00 00 |ntsFormatData...|
000002f0 00 6b f0 66 56 c3 12 5b 07 08 12 3a 32 4d 4b 92 |.k.fV..[...:2MK.|
00000300 f8 00 07 10 00 01 00 06 00 04 00 00 00 05 00 03 |................|
00000310 00 02 00 04 80 00 01 81 00 b4 03 f6 00 0f f6 55 |...............U|
00000320 d3 f8 31 29 b5 04 2b 3d 81 2b 3d 81 2b 3d 85 2b |..1)..+=.+=.+=.+|
00000330 3d 9d c0 28 93 e8 00 00 00 00 00 00 00 00 28 e1 |=..(..........(.|
00000340 c0 de 00 00 00 00 00 00 3f d7 6c 17 19 4c 75 63 |........?.l..Luc|
00000350 65 6e 65 39 30 50 6f 73 74 69 6e 67 73 57 72 69 |ene90PostingsWri|
00000360 74 65 72 50 6f 73 00 00 00 00 6b f0 66 56 c3 12 |terPos....k.fV..|
00000370 5b 07 08 12 3a 32 4d 4b 92 f8 0a 4c 75 63 65 6e |[...:2MK...Lucen|
00000380 65 39 30 5f 30 1e 01 03 b1 01 06 0b 0b a4 01 05 |e90_0...........|
00000390 0d 21 06 02 0b a5 01 0c 0d 0a 0e 19 18 20 09 0b |.!........... ..|
000003a0 5f 19 45 06 30 08 0a 22 02 02 75 51 58 06 0b 03 |_.E.0.."..uQX...|
000003b0 05 03 07 7c 05 23 96 01 02 3c 54 30 37 01 11 18 |...|.#...<T07...|
000003c0 0a 40 30 29 0b 32 92 01 ae 01 03 1f 21 03 88 01 |.@0).2......!...|
000003d0 23 27 d5 01 73 18 0f 5f 07 3a 04 04 06 06 07 06 |#'..s.._.:......|
000003e0 12 19 38 04 00 72 0c 7d 52 3b 04 04 06 06 07 06 |..8..r.}R;......|
000003f0 25 06 38 04 11 46 3e 08 4c 42 11 10 0f 1f bc 01 |%.8..F>.LB......|
00000400 0b 1c 1a 06 8a 01 20 39 04 04 06 06 07 06 12 51 |...... 9.......Q|
00000410 04 c8 01 15 00 7e 44 06 06 07 06 17 06 08 06 04 |.....~D.........|
00000420 38 04 30 30 12 1d 05 07 19 06 05 02 00 05 05 06 |8.00............|
00000430 02 07 0c 05 07 31 05 2a 06 01 06 09 06 06 08 0b |.....1.*........|
00000440 04 00 00 1a 00 1c 0c d1 01 06 2f 07 60 07 15 06 |........../.`...|
00000450 01 01 cb 01 63 1a 26 a8 01 9f 01 13 06 2b 99 01 |....c.&......+..|
00000460 b4 01 01 68 28 09 d4 01 09 1b 0d 6f 0a 16 1b 10 |...h(......o....|
00000470 17 80 01 05 71 cf 01 d0 01 06 d2 01 06 17 1e 04 |....q...........|
00000480 05 0d 07 0c 05 07 31 05 2a 07 06 09 06 06 17 08 |......1.*.......|
00000490 04 04 0c 04 0d 12 2a 01 25 76 0e 07 0f 20 14 1e |......*.%v... ..|
000004a0 53 06 1e 08 a3 01 38 0a 0b a6 01 da 01 03 5e 2b |S.....8.......^+|
000004b0 c5 01 61 18 01 ba 01 38 03 05 0d 07 0c 05 07 31 |..a....8.......1|
000004c0 05 2a 07 06 09 06 06 17 03 04 04 03 03 02 05 0d |.*..............|
000004d0 07 0c 05 07 31 05 2a 07 06 09 06 06 17 02 08 02 |....1.*.........|
000004e0 02 c9 01 c0 28 93 e8 00 00 00 00 00 00 00 00 63 |....(..........c|
000004f0 69 b5 c7 00 00 00 00 00 3f d7 6c 17 15 4c 75 63 |i.......?.l..Luc|
00000500 65 6e 65 39 30 4e 6f 72 6d 73 4d 65 74 61 64 61 |ene90NormsMetada|
00000510 74 61 00 00 00 00 6b f0 66 56 c3 12 5b 07 08 12 |ta....k.fV..[...|
00000520 3a 32 4d 4b 92 f8 00 02 00 00 00 ff ff ff ff ff |:2MK............|
00000530 ff ff ff 00 00 00 00 00 00 00 00 ff ff ff 07 00 |................|
00000540 00 00 01 2b 00 00 00 00 00 00 00 ff ff ff ff c0 |...+............|
00000550 28 93 e8 00 00 00 00 00 00 00 00 72 ba cc 7e 00 |(..........r..~.|
00000560 3f d7 6c 17 12 4c 75 63 65 6e 65 39 30 46 69 65 |?.l..Lucene90Fie|
00000570 6c 64 49 6e 66 6f 73 00 00 00 00 6b f0 66 56 c3 |ldInfos....k.fV.|
00000580 12 5b 07 08 12 3a 32 4d 4b 92 f8 00 03 04 70 61 |.[...:2MK.....pa|
00000590 74 68 00 02 01 00 ff ff ff ff ff ff ff ff 02 1d |th..............|
000005a0 50 65 72 46 69 65 6c 64 50 6f 73 74 69 6e 67 73 |PerFieldPostings|
000005b0 46 6f 72 6d 61 74 2e 66 6f 72 6d 61 74 08 4c 75 |Format.format.Lu|
000005c0 63 65 6e 65 39 30 1d 50 65 72 46 69 65 6c 64 50 |cene90.PerFieldP|
000005d0 6f 73 74 69 6e 67 73 46 6f 72 6d 61 74 2e 73 75 |ostingsFormat.su|
000005e0 66 66 69 78 01 30 00 00 00 08 6d 6f 64 69 66 69 |ffix.0....modifi|
000005f0 65 64 01 00 00 00 ff ff ff ff ff ff ff ff 00 01 |ed..............|
00000600 01 08 00 00 08 63 6f 6e 74 65 6e 74 73 02 00 03 |.....contents...|
00000610 00 ff ff ff ff ff ff ff ff 02 1d 50 65 72 46 69 |...........PerFi|
00000620 65 6c 64 50 6f 73 74 69 6e 67 73 46 6f 72 6d 61 |eldPostingsForma|
00000630 74 2e 66 6f 72 6d 61 74 08 4c 75 63 65 6e 65 39 |t.format.Lucene9|
00000640 30 1d 50 65 72 46 69 65 6c 64 50 6f 73 74 69 6e |0.PerFieldPostin|
00000650 67 73 46 6f 72 6d 61 74 2e 73 75 66 66 69 78 01 |gsFormat.suffix.|
00000660 30 00 00 00 c0 28 93 e8 00 00 00 00 00 00 00 00 |0....(..........|
00000670 1f ee 84 f9 00 00 00 00 3f d7 6c 17 1c 4c 75 63 |........?.l..Luc|
00000680 65 6e 65 39 30 53 74 6f 72 65 64 46 69 65 6c 64 |ene90StoredField|
00000690 73 46 61 73 74 44 61 74 61 00 00 00 01 6b f0 66 |sFastData....k.f|
000006a0 56 c3 12 5b 07 08 12 3a 32 4d 4b 92 f8 00 00 1e |V..[...:2MK.....|
000006b0 00 01 08 18 1d 21 21 1d 1c 18 0a 13 0b 15 12 10 |.....!!.........|
000006c0 15 0f 15 12 10 15 12 a0 00 16 2f 68 6f 6d 65 2f |........../home/|
000006d0 64 61 f0 04 69 2f 64 6f 63 73 2f 62 62 62 2e 74 |da..i/docs/bbb.t|
000006e0 78 74 00 1b 2f 68 6f 01 05 00 e0 69 2f 64 6f 63 |xt../ho....i/doc|
000006f0 73 2f 69 6e 64 65 78 2f 5f 73 30 2e 63 66 73 00 |s/index/_s0.cfs.|
00000700 1f 0f 00 50 61 69 2f 64 6f f0 04 63 73 2f 69 6e |...Pai/do..cs/in|
00000710 64 65 78 2f 73 65 67 6d 65 6e 74 73 5f 31 24 00 |dex/segments_1$.|
00000720 1f 0a 00 90 69 2f 64 6f 63 73 2f 69 6e f0 04 64 |....i/docs/in..d|
00000730 65 78 2f 77 72 69 74 65 2e 6c 6f 63 6b 00 1b 2f |ex/write.lock../|
00000740 68 6f 01 05 00 e0 69 2f 64 6f 63 73 2f 69 6e 64 |ho....i/docs/ind|
00000750 65 78 2f 5f 73 30 2e 63 66 65 00 1a 0f 00 50 61 |ex/_s0.cfe....Pa|
00000760 69 2f 64 6f f0 04 63 73 2f 69 6e 64 65 78 2f 5f |i/do..cs/index/_|
00000770 30 2e 73 69 00 16 2f 68 6f 01 05 00 e0 69 2f 64 |0.si../ho....i/d|
00000780 6f 63 73 2f 61 61 61 2e 74 78 74 c0 28 93 e8 00 |ocs/aaa.txt.(...|
00000790 00 00 00 00 00 00 00 52 80 f1 02 00 00 00 00 00 |.......R........|
000007a0 3f d7 6c 17 13 42 6c 6f 63 6b 54 72 65 65 54 65 |?.l..BlockTreeTe|
000007b0 72 6d 73 49 6e 64 65 78 00 00 00 00 6b f0 66 56 |rmsIndex....k.fV|
000007c0 c3 12 5b 07 08 12 3a 32 4d 4b 92 f8 0a 4c 75 63 |..[...:2MK...Luc|
000007d0 65 6e 65 39 30 5f 30 00 00 c0 28 93 e8 00 00 00 |ene90_0...(.....|
000007e0 00 00 00 00 00 6e c7 b4 6e 00 00 00 00 00 00 00 |.....n..n.......|
000007f0 3f d7 6c 17 11 4c 75 63 65 6e 65 39 30 4e 6f 72 |?.l..Lucene90Nor|
00000800 6d 73 44 61 74 61 00 00 00 00 6b f0 66 56 c3 12 |msData....k.fV..|
00000810 5b 07 08 12 3a 32 4d 4b 92 f8 00 08 44 0e 00 21 |[...:2MK....D..!| <------------- here 08 44 0e 00 21
00000820 29 04 c0 28 93 e8 00 00 00 00 00 00 00 00 43 ab |)..(..........C.| <------------ 04 就是norm
00000830 9e 6c 00 00 00 00 00 00 3f d7 6c 17 19 4c 75 63 |.l......?.l..Luc|
00000840 65 6e 65 39 30 50 6f 73 74 69 6e 67 73 57 72 69 |ene90PostingsWri|
00000850 74 65 72 44 6f 63 00 00 00 00 6b f0 66 56 c3 12 |terDoc....k.fV..|
00000860 5b 07 08 12 3a 32 4d 4b 92 f8 0a 4c 75 63 65 6e |[...:2MK...Lucen|
00000870 65 39 30 5f 30 03 03 02 05 08 03 01 02 02 0b 03 |e90_0...........|
00000880 07 02 02 07 01 03 02 02 07 02 02 07 03 07 03 07 |................|
00000890 05 02 15 03 04 02 03 02 10 03 05 03 05 05 02 10 |................|
000008a0 02 03 05 03 02 10 02 02 05 03 c0 28 93 e8 00 00 |...........(....|
000008b0 00 00 00 00 00 00 8d fa 92 14 00 00 00 00 00 00 |................|
000008c0 3f d7 6c 17 12 42 6c 6f 63 6b 54 72 65 65 54 65 |?.l..BlockTreeTe|
000008d0 72 6d 73 44 69 63 74 00 00 00 00 6b f0 66 56 c3 |rmsDict....k.fV.|
000008e0 12 5b 07 08 12 3a 32 4d 4b 92 f8 0a 4c 75 63 65 |.[...:2MK...Luce|
000008f0 6e 65 39 30 5f 30 36 84 0e 30 30 75 62 75 6e 74 |ne90_06..00ubunt|
00000900 75 30 2e 32 32 2e 30 34 2e 31 31 31 30 2e 30 2e |u0.22.04.1110.0.|
00000910 30 31 36 35 36 36 30 31 39 31 38 38 33 36 31 37 |0165660191883617|
00000920 2e 30 2e 33 31 65 31 69 31 6d 31 6d 31 6d 32 33 |.0.31e1i1m1m1m23|
00000930 33 33 35 2e 31 35 2e 30 36 37 39 5f 30 5f 30 5f |335.15.0679_0_0_|
00000940 6c 75 63 65 6e 65 39 30 66 69 65 6c 64 5f 30 5f |lucene90field_0_|
00000950 6c 75 63 65 6e 65 39 30 66 69 65 6c 64 73 69 6e |lucene90fieldsin|
00000960 64 65 78 5f 30 5f 6c 75 63 65 6e 65 39 30 66 69 |dex_0_lucene90fi|
00000970 65 6c 64 73 69 6e 64 65 78 66 69 6c 65 5f 70 6f |eldsindexfile_po|
00000980 69 6e 74 65 72 73 5f 31 5f 30 cb b9 5f 6c 75 63 |inters_1_0.._luc|
00000990 65 6e 65 39 30 5f 30 5f 6c 75 63 65 6e 65 39 30 |ene90_0_lucene90|
000009a0 66 69 65 6c 64 73 69 6e 64 65 78 61 61 61 61 2e |fieldsindexaaaa.|
000009b0 74 78 74 61 61 6d 62 6f 79 64 6f 67 6f 6f 64 69 |txtaamboydogoodi|
000009c0 69 73 6b 6e 6f 77 74 68 69 6e 67 77 68 61 74 79 |isknowthingwhaty|
000009d0 6f 75 61 6d 61 6d 64 36 34 36 01 10 01 06 0d 06 |ouamamd646......|
000009e0 08 02 01 01 02 06 01 01 01 02 10 16 25 04 0b 14 |............%...|
000009f0 01 07 1f 02 05 1c 02 04 02 01 04 00 03 02 02 03 |................|
00000a00 02 01 07 02 01 02 01 04 06 07 02 04 01 06 01 02 |................|
00000a10 02 05 3a 7a 01 3d 11 06 00 02 04 05 03 01 01 01 |..:z.=..........|
00000a20 01 0f 03 01 02 01 01 01 02 11 01 01 01 0f 01 11 |................|
00000a30 01 0f 02 00 02 08 01 08 01 01 01 01 05 01 09 01 |................|
00000a40 0b 05 00 01 08 01 05 01 03 15 01 03 01 38 b4 09 |.............8..|
00000a50 62 62 62 62 2e 74 78 74 62 65 73 74 5f 73 70 65 |bbbb.txtbest_spe|
00000a60 65 64 62 6b 64 62 6c 6f 63 6b 74 72 65 65 74 65 |edbkdblocktreete|
00000a70 72 6d 73 64 69 63 74 62 6c 6f 63 6b 74 72 65 65 |rmsdictblocktree|
00000a80 74 65 72 6d 73 69 6e 64 65 78 62 6c 6f 63 6b 74 |termsindexblockt|
00000a90 72 65 65 74 65 72 6d 73 6d 65 74 61 62 6f 79 62 |reetermsmetaboyb|
00000aa0 75 69 6c 64 63 63 66 65 63 66 73 63 6f 6e 74 65 |uildccfecfsconte|
00000ab0 6e 74 73 63 73 64 64 61 69 64 6f 64 6f 63 64 6f |ntscsddaidodocdo|
00000ac0 63 5f 64 6f 63 5f 69 64 73 5f 30 64 6f 63 73 65 |c_doc_ids_0docse|
00000ad0 75 66 66 64 6d 66 64 74 66 64 78 66 6c 75 73 68 |uffdmfdtfdxflush|
00000ae0 66 6e 6d 66 73 74 38 01 07 0a 03 12 13 12 03 05 |fnmfst8.........|
00000af0 01 03 03 08 02 01 03 02 03 04 09 04 03 03 03 03 |................|
00000b00 05 03 03 1b 04 00 02 01 0d 02 02 05 02 01 04 01 |................|
00000b10 02 0a 04 00 05 02 0a 01 04 01 04 01 05 02 01 3d |...............=|
00000b20 8e 01 77 04 01 02 11 02 0f 01 01 01 01 01 01 02 |..w.............|
00000b30 15 02 03 01 0f 01 11 04 01 01 0f 01 01 02 00 02 |................|
00000b40 06 01 03 00 0b 04 04 02 0b 01 01 01 01 01 01 0b |................|
00000b50 00 01 06 03 06 04 03 05 01 03 01 0b 01 50 cc 20 |.............P. |
00000b60 67 67 65 6e 65 72 69 63 67 6f 6f 64 68 68 6f 6d |ggenericgoodhhom|
00000b70 65 69 69 64 73 5f 30 69 6e 64 65 78 69 73 6a 6a |eiids_0indexisjj|
00000b80 61 76 61 2e 72 75 6e 74 69 6d 65 2e 76 65 72 73 |ava.runtime.vers|
00000b90 69 6f 6e 6a 61 76 61 2e 76 65 6e 64 6f 72 6a 61 |ionjava.vendorja|
00000ba0 76 61 2e 76 65 72 73 69 6f 6e 6a 61 76 61 2e 76 |va.versionjava.v|
00000bb0 6d 2e 76 65 72 73 69 6f 6e 6b 64 64 6b 64 69 6b |m.versionkddkdik|
00000bc0 64 6d 30 6b 6e 6f 77 6c 6c 69 6e 75 78 6c 75 63 |dm0knowllinuxluc|
00000bd0 65 6e 65 2e 76 65 72 73 69 6f 6e 6c 75 63 65 6e |ene.versionlucen|
00000be0 65 39 30 6c 75 63 65 6e 65 39 30 5f 30 6c 75 63 |e90lucene90_0luc|
00000bf0 65 6e 65 39 30 63 6f 6d 70 6f 75 6e 64 64 61 74 |ene90compounddat|
00000c00 61 6c 75 63 65 6e 65 39 30 63 6f 6d 70 6f 75 6e |alucene90compoun|
00000c10 64 65 6e 74 72 69 65 73 6c 75 63 65 6e 65 39 30 |dentrieslucene90|
00000c20 66 69 65 6c 64 69 6e 66 6f 73 6c 75 63 65 6e 65 |fieldinfoslucene|
00000c30 39 30 66 69 65 6c 64 73 69 6e 64 65 78 69 64 78 |90fieldsindexidx|
00000c40 6c 75 63 65 6e 65 39 30 66 69 65 6c 64 73 69 6e |lucene90fieldsin|
00000c50 64 65 78 6d 65 74 61 6c 75 63 65 6e 65 39 30 6e |dexmetalucene90n|
00000c60 6f 72 6d 73 64 61 74 61 6c 75 63 65 6e 65 39 30 |ormsdatalucene90|
00000c70 6e 6f 72 6d 73 6d 65 74 61 64 61 74 61 6c 75 63 |normsmetadataluc|
00000c80 65 6e 65 39 30 70 6f 69 6e 74 73 66 6f 72 6d 61 |ene90pointsforma|
00000c90 74 64 61 74 61 6c 75 63 65 6e 65 39 30 70 6f 69 |tdatalucene90poi|
00000ca0 6e 74 73 66 6f 72 6d 61 74 69 6e 64 65 78 6c 75 |ntsformatindexlu|
00000cb0 63 65 6e 65 39 30 70 6f 69 6e 74 73 66 6f 72 6d |cene90pointsform|
00000cc0 61 74 6d 65 74 61 6c 75 63 65 6e 65 39 30 70 6f |atmetalucene90po|
00000cd0 73 74 69 6e 67 73 77 72 69 74 65 72 64 6f 63 6c |stingswriterdocl|
00000ce0 75 63 65 6e 65 39 30 70 6f 73 74 69 6e 67 73 77 |ucene90postingsw|
00000cf0 72 69 74 65 72 70 6f 73 6c 75 63 65 6e 65 39 30 |riterposlucene90|
00000d00 70 6f 73 74 69 6e 67 73 77 72 69 74 65 72 74 65 |postingswriterte|
00000d10 72 6d 73 6c 75 63 65 6e 65 39 30 73 65 67 6d 65 |rmslucene90segme|
00000d20 6e 74 69 6e 66 6f 6c 75 63 65 6e 65 39 30 73 74 |ntinfolucene90st|
00000d30 6f 72 65 64 66 69 65 6c 64 73 66 61 73 74 64 61 |oredfieldsfastda|
00000d40 74 61 6c 75 63 65 6e 65 39 30 73 74 6f 72 65 64 |talucene90stored|
00000d50 66 69 65 6c 64 73 66 6f 72 6d 61 74 2e 6d 6f 64 |fieldsformat.mod|
00000d60 65 6c 75 63 65 6e 65 39 33 50 01 07 04 01 04 01 |elucene93P......|
00000d70 05 05 02 01 14 0b 0c 0f 03 03 04 04 01 05 0e 08 |................|
00000d80 0a 14 17 12 16 17 11 15 18 19 18 19 19 1b 13 1c |................|
00000d90 1f 08 16 05 04 00 02 09 06 00 01 02 0a 01 02 01 |................|
00000da0 0f 08 15 03 02 01 02 05 21 56 a8 01 04 b9 01 05 |........!V......|
00000db0 01 13 01 00 01 04 01 03 00 0a 06 01 04 01 01 03 |................|
00000dc0 0b 05 01 11 02 01 01 01 01 01 01 03 01 01 01 01 |................|
00000dd0 01 0f 01 00 01 0c 05 19 01 01 0f 01 01 03 01 06 |................|
00000de0 0d 01 0b 01 01 02 01 01 01 01 01 01 01 02 01 02 |................|
00000df0 01 01 01 01 01 01 01 02 11 02 0f 01 11 01 0b 01 |................|
00000e00 5b a4 0f 6d 6f 64 69 66 69 65 64 6e 76 64 6e 76 |[..modifiednvdnv|
00000e10 6d 6f 6f 63 73 6f 73 6f 73 2e 61 72 63 68 6f 73 |moocsosos.archos|
00000e20 2e 76 65 72 73 69 6f 6e 70 70 61 69 70 61 74 68 |.versionppaipath|
00000e30 70 65 72 66 69 65 6c 64 70 6f 73 74 69 6e 67 73 |perfieldpostings|
00000e40 66 6f 72 6d 61 74 2e 66 6f 72 6d 61 74 70 65 72 |format.formatper|
00000e50 66 69 65 6c 64 70 6f 73 74 69 6e 67 73 66 6f 72 |fieldpostingsfor|
00000e60 6d 61 74 2e 73 75 66 66 69 78 70 6f 73 70 72 69 |mat.suffixpospri|
00000e70 76 61 74 65 70 d7 99 70 d7 9b 70 d7 9c 71 71 78 |vatep..p..p..qqx|
00000e80 72 73 65 67 6d 65 6e 74 73 73 69 73 69 6e 64 65 |rsegmentssisinde|
00000e90 78 66 69 6c 65 5f 70 6f 69 6e 74 65 72 73 5f 31 |xfile_pointers_1|
00000ea0 73 6f 75 72 63 65 74 68 69 6e 67 74 69 6d 74 69 |sourcethingtimti|
00000eb0 6d 65 73 74 61 6d 70 74 69 70 78 74 6d 64 74 6d |mestamptipxtmdtm|
00000ec0 70 75 75 62 75 6e 74 75 76 78 77 63 77 68 61 74 |puubuntuvxwcwhat|
00000ed0 77 72 69 74 65 2e 6c 6f 63 6b 77 72 69 74 65 2e |write.lockwrite.|
00000ee0 6c 6f 63 6b 38 78 79 79 6f 75 79 6f 75 37 7a 7a |lock8xyyouyou7zz|
00000ef0 74 37 cb b9 cd b1 69 5a 08 03 03 01 03 02 07 0a |t7....iZ........|
00000f00 01 03 04 1d 1d 03 07 03 03 03 01 03 08 02 15 06 |................|
00000f10 05 03 09 04 03 03 01 06 02 02 04 0a 0b 01 01 03 |................|
00000f20 04 01 03 02 03 21 07 02 02 05 02 01 03 02 01 02 |.....!..........|
00000f30 01 03 08 0f 03 04 00 13 02 03 02 01 02 01 05 02 |................|
00000f40 01 0b 08 11 08 10 01 60 be 01 01 9e 02 0d 02 01 |.......`........|
00000f50 01 01 01 0b 01 11 03 01 01 01 01 0f 01 01 03 01 |................|
00000f60 01 01 02 01 03 0d 03 05 01 00 01 0a 02 13 01 01 |................|
00000f70 00 01 04 05 02 0b 01 0d 01 0f 01 11 01 13 01 11 |................|
00000f80 01 05 01 03 01 01 01 0b 01 01 04 11 03 0f 02 01 |................|
00000f90 02 03 02 05 01 01 02 01 02 0d 01 0f 01 05 01 01 |................|
00000fa0 02 00 01 0c 15 0c 01 14 0f d4 0b 2f 68 6f 6d 65 |.........../home|
00000fb0 2f 64 61 69 2f 64 6f 63 73 2f 61 61 61 2e 74 78 |/dai/docs/aaa.tx|
00000fc0 74 2f 68 6f 6d 65 2f 64 61 69 2f 64 6f 63 73 2f |t/home/dai/docs/|
00000fd0 62 62 62 2e 74 78 74 2f 68 6f 6d 65 2f 64 61 69 |bbb.txt/home/dai|
00000fe0 2f 64 6f 63 73 2f 69 6e 64 65 78 2f 5f 30 2e 63 |/docs/index/_0.c|
00000ff0 66 65 2f 68 6f 6d 65 2f 64 61 69 2f 64 6f 63 73 |fe/home/dai/docs|
00001000 2f 69 6e 64 65 78 2f 5f 30 2e 63 66 73 2f 68 6f |/index/_0.cfs/ho|
00001010 6d 65 2f 64 61 69 2f 64 6f 63 73 2f 69 6e 64 65 |me/dai/docs/inde|
00001020 78 2f 5f 30 2e 73 69 2f 68 6f 6d 65 2f 64 61 69 |x/_0.si/home/dai|
00001030 2f 64 6f 63 73 2f 69 6e 64 65 78 2f 73 65 67 6d |/docs/index/segm|
00001040 65 6e 74 73 5f 31 2f 68 6f 6d 65 2f 64 61 69 2f |ents_1/home/dai/|
00001050 64 6f 63 73 2f 69 6e 64 65 78 2f 77 72 69 74 65 |docs/index/write|
00001060 2e 6c 6f 63 6b 0e 16 16 1b 1b 1a 1f 1f 01 0d 09 |.lock...........|
00001070 e4 01 06 17 11 0b 11 0b 05 c0 28 93 e8 00 00 00 |..........(.....|
00001080 00 00 00 00 00 26 7d 6b cb 00 00 00 00 00 00 00 |.....&}k........|
00001090 3f d7 6c 17 16 4c 75 63 65 6e 65 39 30 46 69 65 |?.l..Lucene90Fie|
000010a0 6c 64 73 49 6e 64 65 78 49 64 78 00 00 00 00 6b |ldsIndexIdx....k|
000010b0 f0 66 56 c3 12 5b 07 08 12 3a 32 4d 4b 92 f8 00 |.fV..[...:2MK...|
000010c0 c0 28 93 e8 00 00 00 00 00 00 00 00 be 7c 21 a1 |.(...........|!.|
000010d0 c0 28 93 e8 00 00 00 00 00 00 00 00 15 f4 63 e8 |.(............c.|
000010e0

gdb 读取内容:

(gdb) x/32xb 140063879776283
0x7f6329ccc81b: 0x08 0x44 0x0e 0x00 0x21 0x29 0x04 0xc0
0x7f6329ccc823: 0x28 0x93 0xe8 0x00 0x00 0x00 0x00 0x00
0x7f6329ccc82b: 0x00 0x00 0x00 0x43 0xab 0x9e 0x6c 0x00
0x7f6329ccc833: 0x00 0x00 0x00 0x00 0x00 0x3f 0xd7 0x6c

源码分析

DirectByteBufferR 继承关系

DirectByteBufferR extend

依赖以下的脚本自动根据平台自动实现nio的DirectByteBufferR这个类:

// 源码地址 jdk/make/modules/java.base/gensrc/GensrcBuffer.gmk
# Direct byte buffer
#
DIRECT_X_BUF := Direct-X-Buffer

$(eval $(call SetupGenBuffer,DirectByteBuffer, $(DIRECT_X_BUF), type:=byte, BIN:=1))
$(eval $(call SetupGenBuffer,DirectByteBufferR,$(DIRECT_X_BUF), type:=byte, BIN:=1, RW:=R))

DirectByteBufferR 继承 DirectByteBuffer , DirectByteBuffer 则继承ByteBuffer
下面是编译后通过宏自动构建的DirectByteBufferR类,需要编译jvm的时候才能生成,我的在这个目录生成(编译jdk之后才会有这个文件,直接下载是没有这个文件的) jdk/build/linux-x86_64-server-slowdebug/support/gensrc/java.base/java/nio/DirectByteBufferR.java



// -- This file was mechanically generated: Do not edit! -- //

package java.nio;

import java.io.FileDescriptor;
import java.lang.ref.Reference;
import java.util.Objects;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.ScopedMemoryAccess.Scope;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
import sun.nio.ch.DirectBuffer;


class DirectByteBufferR extends DirectByteBuffer implements DirectBuffer
{
...
// Primary constructor
//
DirectByteBufferR(int cap) { // package-private
super(cap);
this.isReadOnly = true;

}



// For memory-mapped buffers -- invoked by FileChannelImpl via reflection
//
protected DirectByteBufferR(int cap, long addr,
FileDescriptor fd,
Runnable unmapper,
boolean isSync, MemorySegmentProxy segment)
{
super(cap, addr, fd, unmapper, isSync, segment);
this.isReadOnly = true;
}

...

}

他的读取方法DirectByteBufferR.get是从DirectByteBuffer继承的,下面是实现:

实际是调用SCOPED_MEMORY_ACCESS.getByte

// jdk/build/linux-x86_64-server-slowdebug/support/gensrc/java.base/java/nio/DirectByteBuffer.java
public byte get() {
try {
return ((SCOPED_MEMORY_ACCESS.getByte(scope(), null, ix(nextGetIndex()))));
} finally {
Reference.reachabilityFence(this);
}
}

public byte get(int i) {
try {
return ((SCOPED_MEMORY_ACCESS.getByte(scope(), null, ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
}

SCOPED_MEMORY_ACCESS是在MappedByteBuffer里面定义的 ,而DirectByteBufferMappedByteBuffer 子类

class DirectByteBuffer  extends MappedByteBuffer implements DirectBuffer
{
...
}

SCOPED_MEMORY_ACCESS.getByte最后调用的是UNSAFE.getByte


public class ScopedMemoryAccess {

private static final Unsafe UNSAFE = Unsafe.getUnsafe();


@ForceInline
public byte getByte(Scope scope, Object base, long offset) {
...
return getByteInternal(scope, base, offset); // 调用 内部函数
...
}

@ForceInline @Scoped
private byte getByteInternal(Scope scope, Object base, long offset) {
...
return UNSAFE.getByte(base, offset); // 最后调用的是UNSAFE.getByte
...
}

UNSAFE是一个全局的静态变量,最后调用的是

// jdk/src/hotspot/share/prims/unsafe.cpp

UNSAFE_ENTRY(java_type, Unsafe_Get##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \
return MemoryAccess<java_type>(thread, obj, offset).get(); \
} UNSAFE_END \

展开之后是调用MemoryAccess的get方法,实际是获取内存的值

相关阅读

java 基本类型

· One min read

背景

了解java的基本类型,基本类型的大小和取值范围

platform:amd64

源码分析

在c++ standard 里面

类型是否有符号最小范围字节数type
charimplement defined [Type char is a distinct type that has an implementation-defined choice of “signed char” or “unsigned char” as its underlying type]--
signed charsigned--signed type
short intsigned--signed type
intsigned--signed type
long intsigned--signed type
long long intsigned--signed type

在linux 64位下面

java基本类型c/c++宏
jintint
jlonglong
jbytesigned char
jbooleanunsigned char
jcharunsigned short
jfloatfloat
jdoubledouble
jsizejint 也就是int
// jdk/src/java.base/unix/native/include/jni_md.h
typedef int jint;
#ifdef _LP64
typedef long jlong;
#else
typedef long long jlong;
#endif

typedef signed char jbyte;
// jdk/src/java.base/share/native/include/jni.h
#ifndef JNI_TYPES_ALREADY_DEFINED_IN_JNI_MD_H

typedef unsigned char jboolean;
typedef unsigned short jchar;
typedef short jshort;
typedef float jfloat;
typedef double jdouble;

typedef jint jsize;

相关阅读

java unsafe

· 2 min read

背景

java的unsafe包是有很多底层的api暴露出来,举例,java的netty就大量使用这个api

例子

下面是java的unsafe包里面的allocateMemory方法.

public class UnsafeDemo {

public static void main(String[] args) {
var unsafe = getUnsafe();
var memory = unsafe.allocateMemory(100);
System.out.println(memory);
}

private static Unsafe getUnsafe() {
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
return (Unsafe) field.get(null);
} catch (Exception e) {
return null;
}
}
}

这里memory 返回的是一个地址

实际上是调用Unsafe_AllocateMemory0

UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory0(JNIEnv *env, jobject unsafe, jlong size)) {
size_t sz = (size_t)size;

assert(is_aligned(sz, HeapWordSize), "sz not aligned");

void* x = os::malloc(sz, mtOther);

return addr_to_java(x);
} UNSAFE_END

最后调用的是glibc 的malloc

void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {

// Special handling for NMT preinit phase before arguments are parsed
void* rc = NULL;
if (NMTPreInit::handle_malloc(&rc, size)) {
// No need to fill with 0 because DumpSharedSpaces doesn't use these
// early allocations.
return rc;
}

DEBUG_ONLY(check_crash_protection());

// On malloc(0), implementations of malloc(3) have the choice to return either
// NULL or a unique non-NULL pointer. To unify libc behavior across our platforms
// we chose the latter.
size = MAX2((size_t)1, size);

// For the test flag -XX:MallocMaxTestWords
if (has_reached_max_malloc_test_peak(size)) {
return NULL;
}

const size_t outer_size = size + MemTracker::overhead_per_malloc();

// Check for overflow.
if (outer_size < size) {
return NULL;
}

ALLOW_C_FUNCTION(::malloc, void* const outer_ptr = ::malloc(outer_size);) <-- malloc 分配内存
if (outer_ptr == NULL) {
return NULL;
}

void* const inner_ptr = MemTracker::record_malloc((address)outer_ptr, size, memflags, stack);

if (DumpSharedSpaces) {
// Need to deterministically fill all the alignment gaps in C++ structures.
::memset(inner_ptr, 0, size);
} else {
DEBUG_ONLY(::memset(inner_ptr, uninitBlockPad, size);)
}
DEBUG_ONLY(break_if_ptr_caught(inner_ptr);)
return inner_ptr;
}

相关阅读

tensorflow 入门

· One min read

背景

最近chatgpt 很流行 ,所以想了解一下TensorFlow是怎么拟合数据的

python 版本

$python3 -V
Python 3.10.6

安装

安装分为pip 方式和jupyter方式

我用的是pip安装的方式

  • 下载安装 Miniconda
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
  • 创建一个叫tf 的environment
## 我的python是3.10 , 根据自己情况改
conda create --name tf python=3.10

  • 安装tf
(tf) dai@myhost:~$ pip install tensorflow==2.11.*
  • 测试运行
(tf) dai@myhost:~$ python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

相关阅读

each jvm bytecode implement in x86 with asm

· 7 min read

背景

想要了解jvm的bytecode 的汇编实现 ,目标平台是x86

汇编格式

同样一个汇编语句:将1赋值给rax

汇编有两种表达方式

desc/描述intelAT&T
将1写入rax寄存器mov eax,1movl $1,%eax
将rab+3 的地址的值写入raxmov eax,[ebx+3]movl 3(%ebx),%eax

stack frame

在x86 64 位的模式下 rbcp 是用r13 , 描述的是下一个指令,i = instruction
r14则存了本地变量指针

// Global Register Names
static const Register rbcp = LP64_ONLY(r13) NOT_LP64(rsi);
static const Register rlocals = LP64_ONLY(r14) NOT_LP64(rdi);

这里LP64_ONLY()和NOT_LP64()是通过宏_LP64来确定的

__LP64__
_LP64
These macros are defined, with value 1, if (and only if) the compilation is for a target where long int and pointer both use 64-bits and int uses 32-bit.

amd64 下面的寄存器

java的stack frame

寄存器含义、描述
r14存了本地变量的基地址
r13指向下一个执行的bytecode

类似c的堆栈,java 的栈如下:

stack

相关阅读

frame 用下面的结构描述

主要包括:

  • _sp :指向栈
  • _pc : 指向指令
jdk/src/hotspot/share/runtime/frame.hpp
class frame {
private:
// Instance variables:
intptr_t* _sp; // stack pointer (from Thread::last_Java_sp) , java 的stack 指针
address _pc; // program counter (the next instruction after the call) 下一个指令的指针

CodeBlob* _cb; // CodeBlob that "owns" pc
enum deopt_state {
not_deoptimized,
is_deoptimized,
unknown
};

deopt_state _deopt_state;

...

};

bytecode

enum TosState {         // describes the tos cache contents
btos = 0, // byte, bool tos cached
ztos = 1, // byte, bool tos cached
ctos = 2, // char tos cached
stos = 3, // short tos cached
itos = 4, // int tos cached
ltos = 5, // long tos cached
ftos = 6, // float tos cached
dtos = 7, // double tos cached
atos = 8, // object cached
vtos = 9, // tos not cached
number_of_states,
ilgl // illegal state: should not occur
};

iload

bytecodeenumasm
iload21
$65 = (address) 0x7fffe1012693 "A\017\266]\002\203\373\025\017\204J"
(gdb) x/20i 0x7fffe1012693
0x7fffe1012693: movzbl 0x2(%r13),%ebx
0x7fffe1012698: cmp $0x15,%ebx <--- 下一个bytecode
0x7fffe101269b: je 0x7fffe10126eb <-- 跳转到 done
0x7fffe10126a1: cmp $0xe0,%ebx <-- 判断下一个是否是_fast_iload
0x7fffe10126a7: mov $0xe1,%ecx <------ 下一个是_fast_iload 则重写成fast_iload2
0x7fffe10126ac: je 0x7fffe10126bd <-------- 跳转到rewrite label
0x7fffe10126ae: cmp $0x34,%ebx
0x7fffe10126b1: mov $0xe2,%ecx
0x7fffe10126b6: je 0x7fffe10126bd
0x7fffe10126b8: mov $0xe0,%ecx
0x7fffe10126bd: movzbl 0x0(%r13),%ebx
0x7fffe10126c2: cmp $0x15,%ebx
0x7fffe10126c5: je 0x7fffe10126e7
0x7fffe10126cb: cmp %ecx,%ebx
0x7fffe10126cd: je 0x7fffe10126e7
0x7fffe10126d3: movabs $0x7ffff74ef9d7,%rdi
0x7fffe10126dd: and $0xfffffffffffffff0,%rsp
0x7fffe10126e1: call 0x7ffff694f3c0 <_ZN14MacroAssembler7debug64EPclPl>
0x7fffe10126e6: hlt
0x7fffe10126e7: mov %cl,0x0(%r13)

源码分析

void TemplateTable::iload_internal(RewriteControl rc) {
transition(vtos, itos);
if (RewriteFrequentPairs && rc == may_rewrite) {
Label rewrite, done;
Register bc = r4;

// get next bytecode
__ load_unsigned_byte(r1, at_bcp(Bytecodes::length_for(Bytecodes::_iload)));

// if _iload, wait to rewrite to iload2. We only want to rewrite the
// last two iloads in a pair. Comparing against fast_iload means that
// the next bytecode is neither an iload or a caload, and therefore
// an iload pair.
__ cmpw(r1, Bytecodes::_iload); <--- 下一个bytecode
__ br(Assembler::EQ, done); <---- 跳转到done

// if _fast_iload rewrite to _fast_iload2
__ cmpw(r1, Bytecodes::_fast_iload); <-- 判断下一个是否是_fast_iload
__ movw(bc, Bytecodes::_fast_iload2); <------ 下一个是_fast_iload 则重写成fast_iload2
__ br(Assembler::EQ, rewrite); <-------- 跳转到rewrite label

// if _caload rewrite to _fast_icaload
__ cmpw(r1, Bytecodes::_caload);
__ movw(bc, Bytecodes::_fast_icaload);
__ br(Assembler::EQ, rewrite);

// else rewrite to _fast_iload
__ movw(bc, Bytecodes::_fast_iload);

// rewrite
// bc: new bytecode
__ bind(rewrite);
patch_bytecode(Bytecodes::_iload, bc, r1, false);
__ bind(done);

}

// do iload, get the local value into tos
locals_index(r1);
__ ldr(r0, iaddress(r1));

}

aconst_null

bytecodedescenum
aconst_nullpush a null reference onto the stack0x01
void TemplateTable::aconst_null() {
transition(vtos, atos);
__ xorl(rax, rax); // rax 就是栈顶
}

istore

bytecodedescenum
istoreStore int into local variable54, // 0x36

可以通过这个bytecode 了解怎么访问本地变量

void TemplateTable::istore() {
transition(itos, vtos); // 这里只是一个断言assert , 断言之前的状态是itos , 之后的状态是vtos , 实际上是由def来定义的
locals_index(rbx); // 将偏移 也就是index 写入rbx
__ movl(iaddress(rbx), rax); //iaddress 就是 rlocal + rbx 也就是获取最后的跳转地址 ,然后将rax写入偏移地址
}

这里iaddress(rbx) 其实是rlocals+rbx 的偏移,也就是相对于本地变量的偏移

static inline Address iaddress(Register r) {
return Address(rlocals, r, Address::times_ptr);
}

iaddress 的源码在这里: src\hotspot\cpu\x86\assembler_x86.hpp 调用顺序是iaddress -> Address

static inline Address iaddress(Register r) {
return Address(rlocals, r, Address::times_ptr);
}
Address(Register base, Register index, ScaleFactor scale, int disp = 0)
: _base (base),
_index(index),
_xmmindex(xnoreg),
_scale(scale),
_disp (disp),
_isxmmindex(false) {
assert(!index->is_valid() == (scale == Address::no_scale),
"inconsistent address");
}

def istore展开

前面不是看到transition(itos, vtos); , 这个transition只是一个类似测试时候的断言,真正是在def 处理的

  def(Bytecodes::_istore              , ubcp|____|clvm|____, itos, vtos, istore              ,  _           );

下面我们看看def展开,会慢慢展开成

void TemplateTable::def(Bytecodes::Code code, int flags, TosState in, TosState out, void (*gen)(int arg), int arg) {
...
Template* t = is_wide ? template_for_wide(code) : template_for(code);
// setup entry
t->initialize(flags, in, out, gen, arg);
assert(t->bytecode() == code, "just checkin'");
}

这里的 in 和out 会在TemplateInterpreterGenerator::generate_and_dispatch的时候使用


//------------------------------------------------------------------------------------------------------------------------

void TemplateInterpreterGenerator::generate_and_dispatch(Template* t, TosState tos_out) {
#ifndef PRODUCT
// debugging code
if (CountBytecodes || TraceBytecodes || StopInterpreterAt > 0) count_bytecode();
if (PrintBytecodeHistogram) histogram_bytecode(t);
if (PrintBytecodePairHistogram) histogram_bytecode_pair(t);
if (TraceBytecodes) trace_bytecode(t);
if (StopInterpreterAt > 0) stop_interpreter_at();
__ verify_FPU(1, t->tos_in());
#endif // !PRODUCT
int step = 0;
if (!t->does_dispatch()) {
step = t->is_wide() ? Bytecodes::wide_length_for(t->bytecode()) : Bytecodes::length_for(t->bytecode());
if (tos_out == ilgl) tos_out = t->tos_out();
// compute bytecode size
assert(step > 0, "just checkin'");
// setup stuff for dispatching next bytecode
if (ProfileInterpreter && VerifyDataPointer
&& MethodData::bytecode_has_profile(t->bytecode())) {
__ verify_method_data_pointer();
}
__ dispatch_prolog(tos_out, step);
}
// generate template
t->generate(_masm);
// advance
if (t->does_dispatch()) {
#ifdef ASSERT
// make sure execution doesn't go beyond this point if code is broken
__ should_not_reach_here();
#endif // ASSERT
} else {
// dispatch to next bytecode
__ dispatch_epilog(tos_out, step);
}
}

reference

utf8 and utf16 and encoding and java

· One min read

背景

java 的字符串会设计很多编码相关的问题,全部整理一下

知识点

Code Unit

code unit 描述的是一个编码的的最小单位(注意一个Unicode 平面对应的字符可能由多个code unit 组成)

编码unit code
utf-81字节
utf-162字节

java 的char

java 的char 是2个字节,类型的范围是 0 到 2^16 - 1.

clickhouse mybatis batch insert cpu raise up

· 2 min read

背景

背景

使用jdbc clickhouse,批量写入,发现cpu升高非常多,升到了90%多

排查及原因

相关环境

jdk: jdk11 clickhouse 使用的sql驱动:

               <groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2-patch11</version>

原因: 使用mybatis plus 拼写sql,批量写入2000条 sql 用mybatis 的xml 拼写 类似:

insert into table values (row1_field1 , row1_field2 ),(row1_field1 , row1_field2) .... 这里是用mybatis 的xml foreache 2000 次

线上warning日志:

Please consider to use one and only one values expression, for example: use 'values(?)' instead of 'values(?),(?)'.

由于jdbc 的parser 比较慢,需要将perpare语句改成以下形式:

insert into table values ( ?, ? )  ## 只有一次

采用的是

// Note: "insert into table values(?,?)" is treated as "insert into mytable"
try (PreparedStatement ps = conn.prepareStatement("insert into table values(?,?)")) {
ps.setString(1, "test"); // id
ps.setObject(2, LocalDateTime.now()); // timestamp
ps.addBatch(); // append parameters to the query
...
ps.executeBatch(); // issue the composed query: insert into mytable values(...)(...)...(...)
}

结果

cpu 从80%降低到30%以内 优化前:

优化前

优化后: 优化后

相关阅读

java 常用命令

· One min read
命令描述
java -XshowSettings:all -version获取所有配置