This commit is contained in:
2026-07-07 00:22:32 +08:00
parent 620afef715
commit 74e4360d71
89 changed files with 1986 additions and 3374 deletions
@@ -63,28 +63,32 @@ public class ArrayListTimestampMonitor<K> implements TimestampMonitor<K> {
private volatile long prev = System.nanoTime();
public boolean recordPacket(K key, int length) {
return recordPacket(key, clock.getCurrentTimeNanos(), length, Long.MAX_VALUE / 1024);
/*public boolean recordPacket(K key,int count, int length) {
return recordPacket(key, clock.getCurrentTimeNanos(),count, length, Long.MAX_VALUE / 1024);
}*/
public boolean recordPacket(K key,int count, int length) {
return recordPacket(key,clock.getCurrentTimeNanos(),count,length);
}
public boolean recordPacket(K key,int count, int length, long delay) {
return recordPacket(key, clock.getCurrentTimeNanos(),count, length, delay);
}
public boolean recordPacket(K key, int length, long delay) {
return recordPacket(key, clock.getCurrentTimeNanos(), length, delay);
}
public boolean recordPacket(K key, Long128 timestamp, int length) {
return recordPacket(key, timestamp, length, Long.MAX_VALUE / 1024);
public boolean recordPacket(K key, Long128 timestamp,int count, int length) {
return recordPacket(key, timestamp,count, length, Long.MAX_VALUE / 1024);
}
/**
* 记录数据包 - 使用插入排序保持列表有序,添加时间戳验证
*/
public boolean recordPacket(K key, Long128 timestamp, int length, long delay) {
public boolean recordPacket(K key, Long128 timestamp,int count, int length, long delay) {
// 验证时间戳的合理性
validateTimestamp(timestamp);
// 快速路径:尝试sequence=0
TimestampMonitorKey<K> monitorKey = new TimestampMonitorKey<>(key, 0);
TimestampMonitorValue monitorValue = new TimestampMonitorValue(monitorKey, timestamp, length, delay);
TimestampMonitorValue monitorValue = new TimestampMonitorValue(monitorKey, timestamp,count, length, delay);
autoCleanup(cleanupThreshold);
rwLock.writeLock().lock();
@@ -327,7 +331,7 @@ public class ArrayListTimestampMonitor<K> implements TimestampMonitor<K> {
/**
* 从已排序的列表中计算包数 - 优化版本,只需要startTime
*/
private long calculatePacketCountFromSortedList(List<TimestampMonitorValue> sortedList, Long128 startTime) {
/*private long calculatePacketCountFromSortedList(List<TimestampMonitorValue> sortedList, Long128 startTime) {
if (sortedList.isEmpty()) {
return 0;
}
@@ -340,8 +344,30 @@ public class ArrayListTimestampMonitor<K> implements TimestampMonitor<K> {
// 计算从startIndex到末尾的所有包数(因为数据包不会来自未来)
return sortedList.size() - startIndex;
}*/
private long calculatePacketCountFromSortedList(List<TimestampMonitorValue> sortedList, Long128 startTime) {
if (sortedList.isEmpty()) {
return 0;
}
// 使用二分查找找到第一个 >= startTime 的元素位置
int startIndex = findFirstValidIndexFromSorted(sortedList, startTime);
if (startIndex >= sortedList.size()) {
return 0;
}
// 计算从startIndex到末尾的所有数据量(因为数据包不会来自未来)
long sum = 0;
for (int i = startIndex; i < sortedList.size(); i++) {
sum += sortedList.get(i).getCount();
}
return sum;
}
public long calculateBandwidth(long timeWindowNs) {
return calculateBandwidth(clock.getCurrentTimeNanos(), timeWindowNs);
}
@@ -413,7 +439,7 @@ public class ArrayListTimestampMonitor<K> implements TimestampMonitor<K> {
// 计算从startIndex到末尾的所有数据(因为数据包不会来自未来)
for (int i = startIndex; i < sortedList.size(); i++) {
TimestampMonitorValue value = sortedList.get(i);
stats.packetCount++;
stats.packetCount+=value.getCount();
stats.totalBytes += value.getLength();
if (stats.earliestTimestamp.equals(Long128.ZERO)
@@ -490,7 +516,7 @@ public class ArrayListTimestampMonitor<K> implements TimestampMonitor<K> {
100, 200000000L);
Thread t = new Thread(() -> {
while (true) {
monitor.recordPacket(along.getAndIncrement(), 1000);
monitor.recordPacket(along.getAndIncrement(),1, 1000);
}
});
t.start();
@@ -65,27 +65,31 @@ private String name;
public boolean recordPacket(K key, int length) {
return recordPacket(key,clock.getCurrentTimeNanos(),length);
public boolean recordPacket(K key, int count,int length) {
return recordPacket(key,clock.getCurrentTimeNanos(),count,length);
}
@Override
public boolean recordPacket(K key, Long128 timestamp, int length) {
return recordPacket(key, timestamp, length, Long.MAX_VALUE/1024);
public boolean recordPacket(K key, Long128 timestamp, int count,int length) {
return recordPacket(key, timestamp,count, length, Long.MAX_VALUE/1024);
}
@Override
public boolean recordPacket(K key, int length, long delay) {
return recordPacket(key,clock.getCurrentTimeNanos(),length,delay);
public boolean recordPacket(K key,int count, int length, long delay) {
return recordPacket(key,clock.getCurrentTimeNanos(),count,length,delay);
}
/**
* 超高性能记录数据包 - 针对路由器优化
*/
public boolean recordPacket(K key, Long128 timestamp, int length, long delay) {
public boolean recordPacket(K key, Long128 timestamp,int count, int length, long delay) {
autoCleanup(cleanupThreshold);
// 快速路径:尝试sequence=0
TimestampMonitorKey<K> monitorKey = new TimestampMonitorKey<>(key, 0);
TimestampMonitorValue monitorValue = new TimestampMonitorValue(monitorKey,timestamp, length,delay);
TimestampMonitorValue monitorValue = new TimestampMonitorValue(monitorKey,timestamp, count,length,delay);
if (entries.putIfAbsent(monitorKey, monitorValue) == null) {
totalDataSize.add(length);
@@ -96,7 +100,7 @@ private String name;
// 重试路径(极少执行)
for (int sequence = 0; sequence < maxRetry; sequence++) {
monitorKey = new TimestampMonitorKey<>(key, sequence);
monitorValue = new TimestampMonitorValue(monitorKey,timestamp, length);
monitorValue = new TimestampMonitorValue(monitorKey,timestamp,count, length);
if (entries.putIfAbsent(monitorKey, monitorValue) == null) {
totalDataSize.add(length);
totalPackets.increment();
@@ -174,8 +178,9 @@ private String name;
return entries.values().stream()
.filter(value -> (value.getTimestamp() .compareTo( startTime )>=0)&&
(value.getTimestamp().compareTo( currentTime) <=0) )
.count();
(value.getTimestamp().compareTo( currentTime) <=0) ).mapToLong(TimestampMonitorValue::getCount)
.sum();
// .count();
}
public long calculateBandwidth(long timeWindowNs) {
@@ -226,7 +231,7 @@ private String name;
.collect(
TrafficStats::new,
(ts, value) -> {
ts.packetCount++;
ts.packetCount+=value.getCount();
ts.totalBytes += value.getLength();
if (ts.earliestTimestamp .equals(Long128.ZERO) || (value.getTimestamp() .compareTo( ts.earliestTimestamp)<0)) {
ts.earliestTimestamp = value.getTimestamp();
@@ -277,7 +282,7 @@ private String name;
Thread t=new Thread(()->{
long l=0;
while(true) {
monitor.recordPacket(l++, 1000);
monitor.recordPacket(l++,1, 1000);
}
});
t.start();
@@ -3,33 +3,12 @@ package org.kne.cloud.network.monitor;
import java.util.function.Consumer;
public interface MonitorData extends Cloneable {
public static final int DOWN=0;
public static final int UNSTABLE=1;
public static final int UP=2;
public Consumer<MonitorData> getChangeListener();
public void setChangeListener(Consumer<MonitorData> changeListener);
public String getName();
public void setName(String name);
@Override
public String toString();
public int getState();
public void setState(int state);
public double getReliability();
public static String parseStateToString(int state) {
switch (state) {
case DOWN:
return "offline";
case UNSTABLE:
return "unstable";
case UP:
return "online";
default:
throw new IllegalArgumentException("Unknown state:"+state);
}
}
public void update();
public Object clone() throws CloneNotSupportedException;
}
@@ -5,41 +5,9 @@ import java.util.TimerTask;
import java.util.function.Consumer;
public class MonitorDataImpl implements MonitorData {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private static Timer timer=new Timer("Monitor Update Thread",true);
public MonitorDataImpl() {
timer.scheduleAtFixedRate(ptt, 0, 100);
}
public MonitorDataImpl(long period) {
timer.scheduleAtFixedRate(ptt, 0, period);
}
private TimerTask ptt=new TimerTask() {
@Override
public void run() {
public abstract class MonitorDataImpl implements MonitorData {
reliability=(reliability*999+(state==UP?1:0))/1000;
}
};
@Override
protected void finalize() throws Throwable {
ptt.cancel();
}
public static Timer getTimer() {
return timer;
}
private Consumer<MonitorData>changeListener;
public Consumer<MonitorData> getChangeListener() {
@@ -48,42 +16,11 @@ private TimerTask ptt=new TimerTask() {
public void setChangeListener(Consumer<MonitorData> changeListener) {
this.changeListener = changeListener;
}
private volatile int state=0;
private double reliability=0.0;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
private String getDsc() {
switch(state) {
case DOWN:
return "○离线";
case UNSTABLE:
return "◐不稳定";
case UP:
return "●在线";
}
return null;
}
@Override
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append(name);
sb.append('\n');
sb.append(getDsc());
/*sb.append('\t');
sb.append((int)(reliability*100));
sb.append('%');*/
return sb.toString();
}
@Override
public double getReliability() {
return reliability;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
@@ -10,32 +10,42 @@ import org.kne.cloud.network.klalb.KLALBUtils;
public class SpeedAndTrafficAndDelayMonitorDataImpl extends SpeedAndTrafficMonitorDataImpl implements SpeedAndTrafficMonitorData,DelayMonitorData{
private long timewindowmax=500000000L;
private DelaySampler upSampler=new DelaySampler();
private DelaySampler downSampler=new DelaySampler();
private TimerTask pttxr=new TimerTask() {
@Override
public void run() {
long idelay=getInDelay();
if(inDelayMin==Long.MAX_VALUE||idelay<=inDelayMin) {
inDelayMin=idelay;
}else{
inDelayMin=(inDelayMin*9999+idelay)/10000;
}
long odelay=getOutDelay();
if(outDelayMin==Long.MAX_VALUE||odelay<=outDelayMin) {
outDelayMin=odelay;
}else{
outDelayMin=(outDelayMin*9999+odelay)/10000;
}
long stime=System.nanoTime();
@Override
public void update() {
super.update();
upSampler.update();
downSampler.update();
if(upSampler.getSnapshotPacketCount()>0&&upSampler.getSnapshotTotalDelay()>0) {
getUploadBandwidth(). recordPacket(KLALBUtils.createGlobalUUID(),0, 0, upSampler.getAvgDelayNanos());
}
if(downSampler.getSnapshotPacketCount()>0&&downSampler.getSnapshotTotalDelay()>0) {
getDownloadBandwidth(). recordPacket(KLALBUtils.createGlobalUUID(),0, 0, downSampler.getAvgDelayNanos());
}
long time=System.nanoTime();
if(time-stime>timewindowmax) {
stime=time;
long idelay=getInDelay();
if(inDelayMin==Long.MAX_VALUE||idelay<=inDelayMin) {
inDelayMin=idelay;
}else{
inDelayMin=(inDelayMin*99+idelay)/100;
}
long odelay=getOutDelay();
if(outDelayMin==Long.MAX_VALUE||odelay<=outDelayMin) {
outDelayMin=odelay;
}else{
outDelayMin=(outDelayMin*99+odelay)/100;
}
}
};
{
getTimer().scheduleAtFixedRate(pttxr, timewindowmax/1000000, timewindowmax/1000000);
}
@Override
protected void finalize() throws Throwable {
pttxr.cancel();
}
@Override
public String toString() {
@@ -62,6 +72,13 @@ private TimerTask pttxr=new TimerTask() {
public void recordUploadDelay(long delay) {
upSampler.recordDelay(delay);
}
public void recordDownloadDelay(long delay) {
downSampler.recordDelay(delay);
}
@Override
@@ -13,18 +13,19 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
private TimestampMonitor<UUID>uploadBandwidth;
private TimestampMonitor<UUID>downloadBandwidth;
private long timewindow=200000000L;
private long timewindowmax=200000000L;
private long timewindow=1000000000L;
private long timewindowmax=1000000000L;
private BandwidthSampler upSampler=new BandwidthSampler();
private BandwidthSampler downSampler=new BandwidthSampler();
private static final long CALC_TIMEOUT=50000000;
public SpeedAndTrafficMonitorDataImpl(HighAccuracyClock clock) {
super();
this.clock=clock;
uploadBandwidth=new ArrayListTimestampMonitor<UUID>(clock,"UploadMonitor", 100, 500000000L);
downloadBandwidth=new ArrayListTimestampMonitor<UUID>(clock,"DownloadMonitor", 100, 500000000L);
uploadBandwidth=new ArrayListTimestampMonitor<UUID>(clock,"UploadMonitor", 100, 5000000000L);
downloadBandwidth=new ArrayListTimestampMonitor<UUID>(clock,"DownloadMonitor", 100, 5000000000L);
getTimer().scheduleAtFixedRate(pttx, timewindowmax/1000000, timewindowmax/1000000);
}
private HighAccuracyClock clock;
@@ -42,7 +43,6 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
this.uploadBandwidth = uploadBandwidth;
this.downloadBandwidth = downloadBandwidth;
getTimer().scheduleAtFixedRate(pttx, timewindowmax/1000000, timewindowmax/1000000);
}
@@ -70,9 +70,11 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
@Override
public String toString() {
StringBuilder sb=new StringBuilder(super.toString());
sb.append('\t');
sb.append(KLALBUtils. convertIBUint(uploadBandwidth.getTotalDataSize())).append("\t").append(KLALBUtils.convertIBUint(downloadBandwidth.getTotalDataSize())).append("\t").append(KLALBUtils.convertIBUint(uploadBandwidth.calculateBandwidth(timewindow))).append("/s\t").append(KLALBUtils.convertIBUint(downloadBandwidth.calculateBandwidth(timewindow))).append("/s\t");
StringBuilder sb=new StringBuilder();
sb.append(KLALBUtils.convertIBUint(uploadBandwidth.calculateBandwidth(timewindow))).append("/s\t");
sb.append(KLALBUtils.convertIBUint(downloadBandwidth.calculateBandwidth(timewindow))).append("/s\t");
sb.append(KLALBUtils.convertUintDefalut(uploadBandwidth.calculatePacketRate(timewindow))).append("PPS\t");
sb.append(KLALBUtils.convertUintDefalut(downloadBandwidth.calculatePacketRate(timewindow))).append("PPS\t");
/*if(getState()==OFFLINE) {
sb.append((coolingTime-(System.currentTimeMillis()-mls))/1000L);
@@ -89,24 +91,11 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
private volatile long outspeedTimeout=System.nanoTime();
public long getOutSpeed() {
long curr=System.nanoTime();
if(curr-outspeedTimeout>CALC_TIMEOUT) {
outspeedTimeout=curr;
outSpeed=uploadBandwidth.calculateBandwidth(timewindow);
}
return outSpeed;
return uploadBandwidth.calculateBandwidth(timewindow);
}
private volatile long inspeedTimeout=System.nanoTime();
public long getInSpeed() {
long curr=System.nanoTime();
if(curr-inspeedTimeout>CALC_TIMEOUT) {
inspeedTimeout=curr;
inSpeed=downloadBandwidth.calculateBandwidth(timewindow);
}
return inSpeed;
return downloadBandwidth.calculateBandwidth(timewindow);
}
@@ -161,40 +150,49 @@ public class SpeedAndTrafficMonitorDataImpl extends MonitorDataImpl implements S
return inPPSMax;
}
private TimerTask pttx=new TimerTask() {
@Override
public void run() {
long os=uploadBandwidth.calculateBandwidth(timewindowmax);
long is=downloadBandwidth.calculateBandwidth(timewindowmax);
long op=uploadBandwidth.calculatePacketRate(timewindowmax);
long ip=uploadBandwidth.calculatePacketRate(timewindowmax);
if(os>outSpeeedMax) {
outSpeeedMax=(outSpeeedMax+os)/2;
}else {
outSpeeedMax=(outSpeeedMax*999+os)/1000;
}
if(is>inSpeedMax) {
inSpeedMax=(inSpeedMax+is)/2;
}else {
inSpeedMax=(inSpeedMax*999+is)/1000;
}
if(op>outPPSMax) {
outPPSMax=(outPPSMax+op)/2;
}else {
outPPSMax=(outPPSMax*999+op)/1000;
}
if(ip>inPPSMax) {
inPPSMax=(inPPSMax+ip)/2;
}else {
inPPSMax=(inPPSMax*999+ip)/1000;
}
}
};
public void recordUploadPacket(int databytes) {
upSampler.recordPacket(databytes);
}
public void recordDownloadPacket(int databytes) {
downSampler.recordPacket(databytes);
}
long stime=System.nanoTime();
@Override
protected void finalize() throws Throwable {
pttx.cancel();
public void update() {
upSampler.update();
downSampler.update();
uploadBandwidth.recordPacket(KLALBUtils.createGlobalUUID(),(int)upSampler.getPacketCountSinceLastSnapshot() ,(int)upSampler.getByteCountSinceLastSnapshot());
downloadBandwidth.recordPacket(KLALBUtils.createGlobalUUID(),(int)downSampler.getPacketCountSinceLastSnapshot() ,(int) downSampler.getByteCountSinceLastSnapshot());
long time=System.nanoTime();
if(time-stime>timewindowmax) {
stime=time;
long os=uploadBandwidth.calculateBandwidth(timewindowmax);
long is=downloadBandwidth.calculateBandwidth(timewindowmax);
long op=uploadBandwidth.calculatePacketRate(timewindowmax);
long ip=uploadBandwidth.calculatePacketRate(timewindowmax);
if(os>outSpeeedMax) {
outSpeeedMax=(outSpeeedMax+os)/2;
}else {
outSpeeedMax=(outSpeeedMax*999+os)/1000;
}
if(is>inSpeedMax) {
inSpeedMax=(inSpeedMax+is)/2;
}else {
inSpeedMax=(inSpeedMax*999+is)/1000;
}
if(op>outPPSMax) {
outPPSMax=(outPPSMax+op)/2;
}else {
outPPSMax=(outPPSMax*999+op)/1000;
}
if(ip>inPPSMax) {
inPPSMax=(inPPSMax+ip)/2;
}else {
inPPSMax=(inPPSMax*999+ip)/1000;
}
}
}
}
@@ -35,7 +35,7 @@ public interface TimestampMonitor<K> {
* @param length 数据包长度
* @return 是否记录成功
*/
boolean recordPacket(K key, int length);
boolean recordPacket(K key,int count, int length);
/**
* 记录数据包(使用指定时间戳)
@@ -44,12 +44,12 @@ public interface TimestampMonitor<K> {
* @param length 数据包长度
* @return 是否记录成功
*/
boolean recordPacket(K key, Long128 timestamp, int length);
boolean recordPacket(K key, Long128 timestamp,int count, int length);
boolean recordPacket(K key, Long128 timestamp, int length, long dsndDelay);
boolean recordPacket(K key, Long128 timestamp, int count,int length, long dsndDelay);
boolean recordPacket(K key, int length, long dsndDelay);
boolean recordPacket(K key, int count,int length, long dsndDelay);
// ==================== 清理维护方法 ====================
@@ -6,15 +6,17 @@ import org.kne.math.Long128;
public class TimestampMonitorValue implements Comparable<TimestampMonitorValue> {
private final Long128 timestamp;
private final int count;
private final int length;
private final TimestampMonitorKey<?> key;
private long delay;
public TimestampMonitorValue(TimestampMonitorKey<?> key, Long128 timestamp, int length) {
this(key,timestamp,length,Long.MAX_VALUE/1024);
public TimestampMonitorValue(TimestampMonitorKey<?> key, Long128 timestamp,int count, int length) {
this(key,timestamp,count,length,Long.MAX_VALUE/1024);
}
public TimestampMonitorValue(TimestampMonitorKey<?> key, Long128 timestamp, int length,long delay) {
public TimestampMonitorValue(TimestampMonitorKey<?> key, Long128 timestamp,int count, int length,long delay) {
this.key = key;
this.timestamp = timestamp;
this.count=count;
this.length = length;
this.delay=delay;
}
@@ -57,7 +59,11 @@ public class TimestampMonitorValue implements Comparable<TimestampMonitorValue>
}
public Long128 getTimestamp() { return timestamp; }
public int getLength() { return length; }
public int getCount() {
return count;
}
public int getLength() { return length; }
public TimestampMonitorKey<?> getKey() { return key; }