KLALB V3.6.0 写了一半

This commit is contained in:
Administrator
2026-02-27 08:32:03 +08:00
parent 816e672843
commit 620afef715
164 changed files with 8381 additions and 13495 deletions
+48 -39
View File
@@ -3,7 +3,6 @@ package org.kne.cloud.network.ntp;
import java.io.Closeable;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetSocketAddress;
import java.security.Timestamp;
import java.util.ArrayList;
@@ -33,12 +32,14 @@ import org.kne.math.Long128;
public class NTPContext implements Closeable, AutoCloseable {
private HighAccuracyClock clock;
private static final boolean debug = true;
private static final boolean debug = false;
private static final int REQUEST_COUNT = 10;
private BigInteger systemFrequencyOffset = NTPTimestamps.nanosToNtp128BitTimeInterval(BigInteger.valueOf(5000));
private BigInteger localPrecision = NTPTimestamps.nanosToNtp128BitTimeInterval(BigInteger.valueOf(1000));
private static final BigInteger adjustThreshold = BigInteger.valueOf(2000000L);
private Long128 systemFrequencyOffset = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(5000));
private Long128 localPrecision = NTPTimestamps.nanosToNtp128BitTimeInterval(new Long128(1000));
private static final Long128 adjustThreshold0 = Long128.valueOf(1000000000L);
private static final Long128 adjustThreshold1 = Long128.valueOf(2000000L);
private AtomicBoolean firstSync=new AtomicBoolean(true);
private int minStratum = 16;
private volatile PeerInfo currentClock = new PeerInfo();
@@ -49,6 +50,7 @@ public class NTPContext implements Closeable, AutoCloseable {
public void run() {
while (!closed) {
try {
clearPackets();
Map<NTPv4Protocol, List<NTPPeer>> mlp = new HashMap<>();
ios.forEach((v) -> {
mlp.put(v, v.getPeersWillSend());
@@ -117,6 +119,10 @@ public class NTPContext implements Closeable, AutoCloseable {
private ConcurrentHashMap<MultipurposeSocketAddress, List<NTPv4Packet>> recvmap = new ConcurrentHashMap<MultipurposeSocketAddress, List<NTPv4Packet>>();
protected void clearPackets() {
recvmap.clear();
}
protected void putPacket(NTPv4Packet nv4, MultipurposeSocketAddress inetSocketAddress) {
checkIP();
List<NTPv4Packet> newv = new Vector<NTPv4Packet>();
@@ -127,7 +133,7 @@ public class NTPContext implements Closeable, AutoCloseable {
synchronized (oldv) {
oldv.add(nv4);
while (oldv.size() > 8) {
while (oldv.size() > 10) {
oldv.remove(0);
}
}
@@ -162,10 +168,10 @@ public class NTPContext implements Closeable, AutoCloseable {
private byte precision;
private long rootDelay = Integer.MAX_VALUE;
private long rootDispersion = Integer.MAX_VALUE;
private BigInteger referenceTimestamp = BigInteger.ZERO;
private BigInteger uploadDelay;
private BigInteger downloadDelay;
private BigInteger rtt;
private Long128 referenceTimestamp = Long128.ZERO;
private Long128 uploadDelay;
private Long128 downloadDelay;
private Long128 rtt;
@Override
public String toString() {
@@ -185,12 +191,12 @@ public class NTPContext implements Closeable, AutoCloseable {
return Long.compare(getRootDistance(), o.getRootDistance());
}
public BigInteger getCurrentSelfDispersion128() {
BigInteger vk = (clock.getCurrentTimeNTP128().subtract(referenceTimestamp)).multiply(systemFrequencyOffset)
.divide(BigInteger.TWO.pow(64));
BigInteger vkl;
public Long128 getCurrentSelfDispersion128() {
Long128 vk = (clock.getCurrentTimeNTP128().subtract(referenceTimestamp)).multiply(systemFrequencyOffset)
.divide(Long128.ONE.shiftLeft(64));
Long128 vkl;
if (vk.signum() < 0) {
vkl = BigInteger.ZERO;
vkl = Long128.ZERO;
} else {
vkl = vk;
}
@@ -198,7 +204,7 @@ public class NTPContext implements Closeable, AutoCloseable {
}
public long getCurrentRootDispersion() {
BigInteger vkl = getCurrentSelfDispersion128().shiftRight(16 + 32);
Long128 vkl = getCurrentSelfDispersion128().shiftRight(16 + 32);
long rez = rootDispersion + vkl.longValue();
if (rez > Integer.MAX_VALUE) {
rez = Integer.MAX_VALUE;
@@ -206,7 +212,7 @@ public class NTPContext implements Closeable, AutoCloseable {
return rez;
}
public BigInteger getAdj() {
public Long128 getAdj() {
return NTPTimestamps.ntp128BitToNanosInterval(uploadDelay.subtract(downloadDelay).shiftRight(1));
}
}
@@ -217,7 +223,7 @@ public class NTPContext implements Closeable, AutoCloseable {
}
private AtomicLong prevAdjustTime = new AtomicLong(System.nanoTime());
private volatile BigInteger deltaRemaining;
private volatile Long128 deltaRemaining;
private long avgAdj=0;
private void selectAndApply(List<PeerInfo> peerInfo) {
Collections.sort(peerInfo);
@@ -228,32 +234,35 @@ public class NTPContext implements Closeable, AutoCloseable {
currentClock = pix;
int i;
int m = Math.min(peerInfo.size(), 3);
BigInteger bi = BigInteger.ZERO;
Long128 bi = Long128.ZERO;
for (i = 0; i < m; i++) {
PeerInfo pi = peerInfo.get(i);
BigInteger adjt = pi.getAdj();
Long128 adjt = pi.getAdj();
bi = bi.add(adjt);
}
BigInteger adj = bi.divide(BigInteger.valueOf(i));
Long128 adj = bi.divide(Long128.valueOf(i));
avgAdj=(avgAdj*7+adj.abs().longValue())/8;
BigInteger adjustment;
if (adj.abs().compareTo(adjustThreshold) > 0) {
adjustment = new BigDecimal(adj).multiply(BigDecimal.valueOf(0.5)).toBigInteger();
Long128 adjustment;
Long128 adjabs=adj.abs();
if (adjabs.compareTo(adjustThreshold0) > 0&&firstSync.compareAndSet(true, false)) {
adjustment=adj;
}else if (adjabs.compareTo(adjustThreshold1) > 0) {
adjustment = Long128.valueOf(new BigDecimal(adj.toBigInteger()).multiply(BigDecimal.valueOf(0.5)).toBigInteger());
} else {
adjustment = new BigDecimal(adj).multiply(BigDecimal.valueOf(0.0625)).toBigInteger();
adjustment = Long128.valueOf(new BigDecimal(adj.toBigInteger()).multiply(BigDecimal.valueOf(0.0625/2)).toBigInteger());
if (deltaRemaining != null) {
long curr = System.nanoTime();
long old = prevAdjustTime.getAndSet(curr);
BigInteger fadj = adjustment.multiply(BigInteger.valueOf(TimeUnit.SECONDS.toNanos(1)))
.divide(BigInteger.valueOf(curr - old));
BigInteger fadjustment = new BigDecimal(fadj).multiply(BigDecimal.valueOf(0.015625/4)).toBigInteger();
Long128 fadj = adjustment.multiply(Long128.valueOf(TimeUnit.SECONDS.toNanos(1)))
.divide(Long128.valueOf(curr - old));
Long128 fadjustment = Long128.valueOf( new BigDecimal(fadj.toBigInteger()).multiply(BigDecimal.valueOf(0.015625/4)).toBigInteger());
clock.adjustFrequency(fadjustment.longValue());
if(debug)
System.out.println("fadj:" + fadjustment + " f:" + clock.getFrequency());
}
deltaRemaining = adj.subtract(adjustment);
}
clock.adjustClock(Long128.valueOf( adjustment));
clock.adjustClock( adjustment);
if(debug)
System.out.println("delta:" + adjustment + " deltaRemaining:" + deltaRemaining);
}
@@ -305,9 +314,9 @@ public class NTPContext implements Closeable, AutoCloseable {
pi.rootDelay = pack.getRootDelay();// (1/65536.0*1000000000)
pi.rootDispersion = pack.getRootDispersion();
pi.referenceTimestamp = pack.getReferenceTimestamp128();
BigInteger uploadD = pack.getReceiveTimestamp128().subtract(pack.getOriginateTimestamp128());
BigInteger downloadD = pack.getDestinationTimestamp128().subtract(pack.getTransmitTimestamp128());
BigInteger rtt = uploadD.add(downloadD);
Long128 uploadD = pack.getReceiveTimestamp128().subtract(pack.getOriginateTimestamp128());
Long128 downloadD = pack.getDestinationTimestamp128().subtract(pack.getTransmitTimestamp128());
Long128 rtt = uploadD.add(downloadD);
if (pi.uploadDelay == null) {
pi.uploadDelay = uploadD;
} else {
@@ -353,19 +362,19 @@ public class NTPContext implements Closeable, AutoCloseable {
+ NTPTimestamps.ntp128ToString(currentClock.referenceTimestamp) + "]";
}
public BigInteger getSystemFrequencyOffset() {
public Long128 getSystemFrequencyOffset() {
return systemFrequencyOffset;
}
public void setSystemFrequencyOffset(BigInteger systemFrequencyOffset) {
public void setSystemFrequencyOffset(Long128 systemFrequencyOffset) {
this.systemFrequencyOffset = systemFrequencyOffset;
}
public BigInteger getLocalPrecision() {
public Long128 getLocalPrecision() {
return localPrecision;
}
public void setLocalPrecision(BigInteger localPrecision) {
public void setLocalPrecision(Long128 localPrecision) {
this.localPrecision = localPrecision;
}
@@ -389,15 +398,15 @@ public class NTPContext implements Closeable, AutoCloseable {
return currentClock.rootDispersion;
}
public BigInteger getReferenceTimestamp() {
public Long128 getReferenceTimestamp() {
return currentClock.referenceTimestamp;
}
public BigInteger getReferenceTimestamp64() {
public Long128 getReferenceTimestamp64() {
return NTPTimestamps.ntp128To64(currentClock.referenceTimestamp);
}
public BigInteger getCurrentSelfDispersion128() {
public Long128 getCurrentSelfDispersion128() {
return currentClock.getCurrentSelfDispersion128();
}
+20 -20
View File
@@ -1,7 +1,6 @@
package org.kne.cloud.network.ntp;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
@@ -13,6 +12,7 @@ import org.kne.cloud.clock.HighAccuracyClock;
import org.kne.cloud.clock.NTPTimestamps;
import org.kne.cloud.network.NetworkPacket;
import org.kne.io.KNEChannels;
import org.kne.math.Long128;
public class NTPv4Packet extends NetworkPacket {
public static final int NTP_VERSION_NUMBER = 4;
@@ -29,14 +29,14 @@ public class NTPv4Packet extends NetworkPacket {
public static final int BASE_HEADER_LENGTH = 48; // 基本头部长度
public static final int AUTH_FIELD_LENGTH = 20; // 认证字段长度
private static final BigInteger TWO_POW_64 = BigInteger.TWO.pow(64);
private static final Long128 TWO_POW_64 = Long128.ONE.shiftLeft(64);
private ByteBuffer header = NetworkPacket.bufferAllocator.allocate(BASE_HEADER_LENGTH);
private List<ExtensionField> extensionFields = new ArrayList<>();
private AuthenticationField authField;
private BigInteger destinationTimeStamp;
private Long128 destinationTimeStamp;
private HighAccuracyClock clock;
public NTPv4Packet(HighAccuracyClock clock) {
@@ -275,48 +275,48 @@ public class NTPv4Packet extends NetworkPacket {
public void setReferenceTimestamp64(BigInteger longValue) {
public void setReferenceTimestamp64(Long128 longValue) {
setReferenceTimestamp(longValue.longValue());
}
public void setOriginateTimestamp64(BigInteger originateTimestamp) {
public void setOriginateTimestamp64(Long128 originateTimestamp) {
setOriginateTimestamp(originateTimestamp.longValue());
}
public void setReceiveTimestamp64(BigInteger longValue) {
public void setReceiveTimestamp64(Long128 longValue) {
setReceiveTimestamp(longValue.longValue());
}
public void setTransmitTimestamp64(BigInteger longValue) {
public void setTransmitTimestamp64(Long128 longValue) {
setTransmitTimestamp(longValue.longValue());
}
public BigInteger getReferenceTimestamp64() {
BigInteger rez=BigInteger.valueOf(getReferenceTimestamp());
public Long128 getReferenceTimestamp64() {
Long128 rez=Long128.valueOf(getReferenceTimestamp());
if(rez.signum()<0) {
rez=rez.add(TWO_POW_64);
}
return rez;
}
public BigInteger getOriginateTimestamp64() {
BigInteger rez=BigInteger.valueOf(getOriginateTimestamp());
public Long128 getOriginateTimestamp64() {
Long128 rez=Long128.valueOf(getOriginateTimestamp());
if(rez.signum()<0) {
rez=rez.add(TWO_POW_64);
}
return rez;
}
public BigInteger getReceiveTimestamp64() {
BigInteger rez=BigInteger.valueOf(getReceiveTimestamp());
public Long128 getReceiveTimestamp64() {
Long128 rez=Long128.valueOf(getReceiveTimestamp());
if(rez.signum()<0) {
rez=rez.add(TWO_POW_64);
}
return rez;
}
public BigInteger getTransmitTimestamp64() {
BigInteger rez=BigInteger.valueOf(getTransmitTimestamp());
public Long128 getTransmitTimestamp64() {
Long128 rez=Long128.valueOf(getTransmitTimestamp());
if(rez.signum()<0) {
rez=rez.add(TWO_POW_64);
}
@@ -367,7 +367,7 @@ public class NTPv4Packet extends NetworkPacket {
public BigInteger getDestinationTimestamp128() {
public Long128 getDestinationTimestamp128() {
return destinationTimeStamp;
}
@@ -493,19 +493,19 @@ public class NTPv4Packet extends NetworkPacket {
}
}
public BigInteger getReceiveTimestamp128() {
public Long128 getReceiveTimestamp128() {
return NTPTimestamps.inferNtp64To128(getReceiveTimestamp64(),clock.getCurrentTimeNTP128());
}
public BigInteger getOriginateTimestamp128() {
public Long128 getOriginateTimestamp128() {
return NTPTimestamps.inferNtp64To128(getOriginateTimestamp64(),clock.getCurrentTimeNTP128());
}
public BigInteger getTransmitTimestamp128() {
public Long128 getTransmitTimestamp128() {
return NTPTimestamps.inferNtp64To128(getTransmitTimestamp64(),clock.getCurrentTimeNTP128());
}
public BigInteger getReferenceTimestamp128() {
public Long128 getReferenceTimestamp128() {
return NTPTimestamps.inferNtp64To128(getReferenceTimestamp64(),clock.getCurrentTimeNTP128());
}
@@ -25,6 +25,7 @@ import org.kne.cloud.clock.HighAccuracyClock;
import org.kne.cloud.clock.NTPTimestamps;
import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.io.KNEChannels;
import org.kne.math.Long128;
public class NTPv4Protocol implements Closeable, AutoCloseable {
private static final boolean showPacket = false;
@@ -134,10 +135,10 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
private static class CheckListItem {
private NTPPeer peer;
private BigInteger timeStamp;
private Long128 timeStamp;
private long createTime = System.nanoTime();
private CheckListItem(BigInteger timeStamp, NTPPeer peer) {
private CheckListItem(Long128 timeStamp, NTPPeer peer) {
this.timeStamp = timeStamp;
this.peer = peer;
}
@@ -188,7 +189,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
stratum = 0;
}
nv4.setStratum(stratum);
BigInteger precision = context.getLocalPrecision();
Long128 precision = context.getLocalPrecision();
byte b = (byte) Math.ceil(log(precision.doubleValue() / (Math.pow(2, 64)), 2));
nv4.setPrecision(b);
nv4.setRootDelay((int) context.getRootDelay());
@@ -253,7 +254,7 @@ public class NTPv4Protocol implements Closeable, AutoCloseable {
private NTPPeer checkOriginTimestamp(NTPv4Packet nv4) {
checkLock.lock();
try {
BigInteger ori = nv4.getOriginateTimestamp128();
Long128 ori = nv4.getOriginateTimestamp128();
removeTimeoutTimestamp();
for (Iterator<CheckListItem> iterator = checkList.iterator(); iterator.hasNext();) {
CheckListItem item = (CheckListItem) iterator.next();