Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Use integer storage and arithmetic for nanosec pcap
Browse files Browse the repository at this point in the history
quint64 has larger range than double so has better accuracy. However,
for calculating packet rate, use floating-point arithmetic since the
packet rate is a double

Updates #238
  • Loading branch information
pstavirs committed Jan 19, 2022
1 parent 45d5e15 commit 226705f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions common/pcapfileformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,20 +503,22 @@ bool PcapFileFormat::open(const QString fileName,
stream->mutable_control()->set_num_packets(1);

// setup packet rate to the timing in pcap (as close as possible)
const double kXsecsInSec = nsecResolution ? 1e9 : 1e6;
// use quint64 rather than double to store micro/nano second as
// it has a larger range (~580 years) and therefore better accuracy
const quint64 kXsecsInSec = nsecResolution ? 1e9 : 1e6;
quint64 xsec = (pktHdr.tsSec*kXsecsInSec + pktHdr.tsUsec);
quint64 delta = xsec - lastXsec;
qDebug("pktCount = %d, delta = %llu", pktCount, delta);

if ((pktCount != 1) && delta)
stream->mutable_control()->set_packets_per_sec(kXsecsInSec/delta);
stream->mutable_control()->set_packets_per_sec(double(kXsecsInSec)/delta);

if (prevStream)
prevStream->mutable_control()->CopyFrom(stream->control());

lastXsec = xsec;
prevStream = stream;
pktCount++;
qDebug("pktCount = %d", pktCount);
byteCount += pktHdr.inclLen + sizeof(pktHdr);
emit progress(int(byteCount*100/byteTotal)); // in percentage
if (stop_)
Expand Down

0 comments on commit 226705f

Please sign in to comment.