changed MovingMedian to MovingPercentile

This commit is contained in:
wangyix 2014-06-04 10:59:32 -07:00
parent 50746a6540
commit 12ac6105f1
8 changed files with 83 additions and 49 deletions

View file

@ -90,7 +90,6 @@ prevN = temp3;
int skew = 0;
while (NodeList::getInstance()->getNodeSocket().hasPendingDatagrams()) {
incomingPacket.resize(nodeList->getNodeSocket().pendingDatagramSize());
@ -210,14 +209,15 @@ currTypes[*currN] = (unsigned char)type;
}
default:
int s = nodeList->processNodeData(senderSockAddr, incomingPacket);
if (s!=1234567890)
if (s!=1234567890 && abs(s) > abs(skew))
skew = s;
break;
}
}
}
if (abs(skew) > 1000) {
if (abs(skew) > 3000) {
printf("large skew! %d ----------------------------\n", skew);

View file

@ -151,6 +151,10 @@ void LimitedNodeList::changeSendSocketBufferSize(int numSendBytes) {
}
bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
quint64 start = usecTimestampNow();
quint64 end;
PacketType checkType = packetTypeForPacket(packet);
int numPacketTypeBytes = numBytesArithmeticCodingFromBuffer(packet.data());
@ -169,6 +173,11 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
versionDebugSuppressMap.insert(senderUUID, checkType);
}
if ((end=usecTimestampNow()) - start > 100) {
printf("\t\t\t\t version and hash match long diff: %d\n", end-start);
}
return false;
}
@ -178,6 +187,11 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
if (sendingNode) {
// check if the md5 hash in the header matches the hash we would expect
if (hashFromPacketHeader(packet) == hashForPacketAndConnectionUUID(packet, sendingNode->getConnectionSecret())) {
if ((end = usecTimestampNow()) - start > 100) {
printf("\t\t\t\t version and hash match long diff: %d\n", end - start);
}
return true;
} else {
qDebug() << "Packet hash mismatch on" << checkType << "- Sender"
@ -188,9 +202,18 @@ bool LimitedNodeList::packetVersionAndHashMatch(const QByteArray& packet) {
<< uuidFromPacketHeader(packet);
}
} else {
if ((end = usecTimestampNow()) - start > 100) {
printf("\t\t\t\t version and hash match long diff: %d\n", end - start);
}
return true;
}
if ((end = usecTimestampNow()) - start > 100) {
printf("\t\t\t\t version and hash match long diff: %d\n", end - start);
}
return false;
}

View file

@ -58,8 +58,7 @@ Node::Node(const QUuid& uuid, NodeType_t type, const HifiSockAddr& publicSocket,
_isAlive(true),
_clockSkewUsec(0),
_mutex(),
_clockSkewMovingMedian(31)
_clockSkewMovingPercentile(30, 0.8f) // moving upper quartile of 21 samples
{
}
@ -136,10 +135,10 @@ float Node::getAverageKilobitsPerSecond() {
}
void Node::setClockSkewUsec(int clockSkew) {
//_clockSkewMovingMedian.updateMedian((float)clockSkew);
//_clockSkewUsec = (int)_clockSkewMovingMedian.getMedian();
_clockSkewMovingPercentile.updatePercentile((float)clockSkew);
_clockSkewUsec = (int)_clockSkewMovingPercentile.getValueAtPercentile();
_clockSkewUsec = clockSkew;
//_clockSkewUsec = clockSkew;
}
QDataStream& operator<<(QDataStream& out, const Node& node) {

View file

@ -24,7 +24,7 @@
#include "NodeData.h"
#include "SimpleMovingAverage.h"
#include "MovingMedian.h"
#include "MovingPercentile.h"
typedef quint8 NodeType_t;
@ -123,7 +123,7 @@ private:
int _clockSkewUsec;
QMutex _mutex;
MovingMedian _clockSkewMovingMedian;
MovingPercentile _clockSkewMovingPercentile;
};
QDebug operator<<(QDebug debug, const Node &message);

View file

@ -98,7 +98,7 @@ int NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& s
sendingNode->setPingMs(pingTime / 1000);
sendingNode->setClockSkewUsec(clockSkew);
//printf("\t\t clock skew sample: %d median: %d\n", clockSkew, sendingNode->getClockSkewUsec());
printf("\t\t clock skew sample: %d val at percentile: %d\n", clockSkew, sendingNode->getClockSkewUsec());
const bool wantDebug = false;
@ -114,8 +114,8 @@ int NodeList::timePingReply(const QByteArray& packet, const SharedNodePointer& s
" clockSkew: " << clockSkew;
}
if (abs(clockSkew) > 1000)
printf("clockskew = %d \n", clockSkew);
///if (abs(clockSkew) > 1000)
//printf("clockskew = %d \n", clockSkew);
return clockSkew;
}
@ -489,8 +489,11 @@ QByteArray NodeList::constructPingReplyPacket(const QByteArray& pingPacket) {
QByteArray replyPacket = byteArrayWithPopulatedHeader(PacketTypePingReply);
QDataStream packetStream(&replyPacket, QIODevice::Append);
packetStream << typeFromOriginalPing << timeFromOriginalPing << usecTimestampNow();
quint64 now;
packetStream << typeFromOriginalPing << timeFromOriginalPing << (now = usecTimestampNow());
printf("\n>>>>>>>> recv ping: %llu reply: %llu diff: %lld\n", timeFromOriginalPing, now, (qint64)now-(qint64)timeFromOriginalPing);
return replyPacket;
}

View file

@ -1,26 +0,0 @@
#ifndef hifi_MovingMedian_h
#define hifi_MovingMedian_h
class MovingMedian {
public:
MovingMedian(int numSamples = 11);
~MovingMedian();
void updateMedian(float sample);
float getMedian() const { return _median; }
private:
float* _samplesSorted;
int _numSamples;
int* _sampleAges; // _sampleAges[i] is the "age" of the sample _sampleSorted[i] (higher means older)
int _numExistingSamples;
float _median;
};
#endif

View file

@ -1,21 +1,23 @@
#include "MovingMedian.h"
#include "MovingPercentile.h"
//#include "stdio.h"// DEBUG
MovingMedian::MovingMedian(int numSamples)
MovingPercentile::MovingPercentile(int numSamples, float percentile)
: _numSamples(numSamples),
_percentile(percentile),
_numExistingSamples(0),
_median(0.0f)
_valueAtPercentile(0.0f),
_indexOfPercentile(0)
{
_samplesSorted = new float[numSamples];
_sampleAges = new int[numSamples];
}
MovingMedian::~MovingMedian() {
MovingPercentile::~MovingPercentile() {
delete[] _samplesSorted;
delete[] _sampleAges;
}
void MovingMedian::updateMedian(float sample) {
void MovingPercentile::updatePercentile(float sample) {
//printf("\nnew sample: %2.2f ", sample);
@ -24,8 +26,13 @@ void MovingMedian::updateMedian(float sample) {
// otherwise, it will be the spot of the oldest sample
int newSampleIndex;
if (_numExistingSamples < _numSamples) {
newSampleIndex = _numExistingSamples;
_numExistingSamples++;
// update _indexOfPercentile
float index = _percentile * (float)(_numExistingSamples - 1);
_indexOfPercentile = (int)(index + 0.5f); // round to int
}
else {
for (int i = 0; i < _numExistingSamples; i++) {
@ -72,9 +79,8 @@ void MovingMedian::updateMedian(float sample) {
newSampleIndex--;
}
// find new median
_median = _samplesSorted[_numExistingSamples/2];
// find new value at percentile
_valueAtPercentile = _samplesSorted[_indexOfPercentile];
/*
printf(" new median: %f\n", _median);

View file

@ -0,0 +1,29 @@
#ifndef hifi_MovingPercentile_h
#define hifi_MovingPercentile_h
class MovingPercentile {
public:
MovingPercentile(int numSamples, float percentile = 0.5f);
~MovingPercentile();
void updatePercentile(float sample);
float getValueAtPercentile() const { return _valueAtPercentile; }
private:
const int _numSamples;
const float _percentile;
float* _samplesSorted;
int* _sampleAges; // _sampleAges[i] is the "age" of the sample at _sampleSorted[i] (higher means older)
int _numExistingSamples;
float _valueAtPercentile;
int _indexOfPercentile;
};
#endif