Add clock wrapper class
This commit is contained in:
1 parent
f7bbb32788
commit
c1286b0ba9
7 files changed
+67
-45
No files matched your search
@@ -0,0 +1,34 @@
|
||||
use rustix::fs::Timespec;
|
||||
use rustix::thread::ClockId;
|
||||
use rustix::time::clock_gettime;
|
||||
|
||||
pub struct Clock {
|
||||
time_s: u64,
|
||||
time_ns: u64,
|
||||
}
|
||||
|
||||
impl Clock {
|
||||
pub fn new(id: ClockId) -> Self {
|
||||
let time: Timespec = clock_gettime(id);
|
||||
Self {
|
||||
time_s: time.tv_sec as u64,
|
||||
time_ns: time.tv_nsec as u64,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_seconds(&self) -> u64 {
|
||||
self.time_s
|
||||
}
|
||||
|
||||
pub fn get_nanoseconds(&self) -> u64 {
|
||||
self.time_ns
|
||||
}
|
||||
|
||||
pub fn get_time_since_midnight(&self) -> i64 {
|
||||
(self.time_s as i64 % 86400 * 1000) + (self.time_ns as i64 / 1000000)
|
||||
}
|
||||
|
||||
pub fn to_milliseconds(&self) -> u64 {
|
||||
(self.time_s * 1000) + (self.time_ns / 1000000)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ extern crate core;
|
||||
|
||||
mod baseliner;
|
||||
mod cake;
|
||||
mod clock;
|
||||
mod config;
|
||||
mod endian;
|
||||
mod log;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use crate::MeasurementType;
|
||||
use etherparse::ReadError;
|
||||
use log::{debug, error};
|
||||
use nix::errno::Errno;
|
||||
use socket2::{Domain, Protocol, SockAddr, Socket, Type};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};
|
||||
use std::str::FromStr;
|
||||
@@ -23,8 +22,6 @@ pub enum PingError {
|
||||
InvalidType(String),
|
||||
#[error("No transport")]
|
||||
NoTransport,
|
||||
#[error("Socket error")]
|
||||
Socket(#[from] Errno),
|
||||
#[error("Wrong ICMP identifier (expected {expected:?}, found {found:?})")]
|
||||
WrongID { expected: u16, found: u16 },
|
||||
}
|
||||
|
||||
+10
-11
@@ -1,11 +1,11 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use crate::clock::Clock;
|
||||
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
||||
use byteorder::*;
|
||||
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
||||
use etherparse::{IcmpEchoHeader, Icmpv4Header, Icmpv4Type, SlicedPacket};
|
||||
use nix::sys::time::TimeValLike;
|
||||
use nix::time::{clock_gettime, ClockId};
|
||||
use rustix::thread::ClockId;
|
||||
|
||||
pub struct PingerICMPEchoListener {}
|
||||
|
||||
@@ -32,11 +32,10 @@ impl PingListener for PingerICMPEchoListener {
|
||||
.expect("Couldn't parse payload to time")
|
||||
as i64;
|
||||
|
||||
let time_now = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap();
|
||||
let clock = Clock::new(ClockId::Monotonic);
|
||||
let time_ms = clock.to_milliseconds() as i64;
|
||||
|
||||
let time_ms = time_now.num_milliseconds();
|
||||
|
||||
let rtt = time_ms - time_sent;
|
||||
let rtt: i64 = time_ms - time_sent;
|
||||
return Ok(PingReply {
|
||||
reflector,
|
||||
seq: echo.seq,
|
||||
@@ -47,8 +46,8 @@ impl PingListener for PingerICMPEchoListener {
|
||||
originate_timestamp: 0,
|
||||
receive_timestamp: 0,
|
||||
transmit_timestamp: 0,
|
||||
last_receive_time_s: time_now.tv_sec() as f64
|
||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
||||
last_receive_time_s: clock.get_seconds() as f64
|
||||
+ (clock.get_nanoseconds() as f64 / 1e9),
|
||||
});
|
||||
}
|
||||
type_ => {
|
||||
@@ -71,9 +70,9 @@ impl PingListener for PingerICMPEchoListener {
|
||||
|
||||
impl PingSender for PingerICMPEchoSender {
|
||||
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
||||
let time = clock_gettime(ClockId::CLOCK_MONOTONIC).unwrap();
|
||||
let time_u64: u64 = time.num_milliseconds() as u64;
|
||||
let payload = time_u64.to_ne_bytes();
|
||||
let clock = Clock::new(ClockId::Monotonic);
|
||||
let time_ms = clock.to_milliseconds();
|
||||
let payload = time_ms.to_ne_bytes();
|
||||
|
||||
// Construct a header with checksum based on the payload
|
||||
let hdr = Icmpv4Header::with_checksum(
|
||||
|
||||
+9
-13
@@ -1,12 +1,11 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use crate::clock::Clock;
|
||||
use crate::endian::ToNativeEndian;
|
||||
use crate::pinger::{PingError, PingListener, PingReply, PingSender};
|
||||
use crate::utils::Utils;
|
||||
use etherparse::icmpv4::TimestampMessage;
|
||||
use etherparse::TransportSlice::{Icmpv4, Icmpv6};
|
||||
use etherparse::{Icmpv4Header, Icmpv4Type, SlicedPacket};
|
||||
use nix::time::{clock_gettime, ClockId};
|
||||
use rustix::thread::ClockId;
|
||||
use std::net::IpAddr;
|
||||
|
||||
pub struct PingerICMPTimestampListener {}
|
||||
|
||||
@@ -27,9 +26,8 @@ impl PingListener for PingerICMPTimestampListener {
|
||||
});
|
||||
}
|
||||
|
||||
let time_now = clock_gettime(ClockId::CLOCK_REALTIME).unwrap();
|
||||
let time_since_midnight: i64 = (time_now.tv_sec() as i64 % 86400 * 1000)
|
||||
+ (time_now.tv_nsec() as i64 / 1000000);
|
||||
let time_now = Clock::new(ClockId::Realtime);
|
||||
let time_since_midnight = time_now.get_time_since_midnight();
|
||||
|
||||
let originate_timestamp = reply.originate_timestamp.to_ne();
|
||||
let receive_timestamp = reply.receive_timestamp.to_ne();
|
||||
@@ -49,8 +47,8 @@ impl PingListener for PingerICMPTimestampListener {
|
||||
originate_timestamp: originate_timestamp as i64,
|
||||
receive_timestamp: receive_timestamp as i64,
|
||||
transmit_timestamp: transmit_timestamp as i64,
|
||||
last_receive_time_s: time_now.tv_sec() as f64
|
||||
+ (time_now.tv_nsec() as f64 / 1e9),
|
||||
last_receive_time_s: time_now.get_seconds() as f64
|
||||
+ (time_now.get_nanoseconds() as f64 / 1e9),
|
||||
});
|
||||
}
|
||||
type_ => {
|
||||
@@ -73,9 +71,7 @@ impl PingListener for PingerICMPTimestampListener {
|
||||
|
||||
impl PingSender for PingerICMPTimestampSender {
|
||||
fn craft_packet(&self, id: u16, seq: u16) -> Vec<u8> {
|
||||
let time = clock_gettime(ClockId::CLOCK_REALTIME).unwrap();
|
||||
let time_since_midnight: u32 =
|
||||
((time.tv_sec() % 86400 * 1000) + (time.tv_nsec() / 1000000)) as u32;
|
||||
let time_since_midnight = Clock::new(ClockId::Realtime).get_time_since_midnight();
|
||||
|
||||
let payload: [u8; 0] = [];
|
||||
|
||||
@@ -84,7 +80,7 @@ impl PingSender for PingerICMPTimestampSender {
|
||||
Icmpv4Type::TimestampRequest(TimestampMessage {
|
||||
id,
|
||||
seq,
|
||||
originate_timestamp: time_since_midnight,
|
||||
originate_timestamp: time_since_midnight as u32,
|
||||
receive_timestamp: 0,
|
||||
transmit_timestamp: 0,
|
||||
}),
|
||||
|
||||
+13
-4
@@ -1,3 +1,4 @@
|
||||
use crate::clock::Clock;
|
||||
use crate::netlink::{Netlink, NetlinkError, Qdisc};
|
||||
use crate::utils::Utils;
|
||||
use crate::{Config, ReflectorStats};
|
||||
@@ -5,6 +6,7 @@ use log::{debug, error, info, warn};
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use rand::RngCore;
|
||||
use rustix::thread::ClockId;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
@@ -281,8 +283,10 @@ impl Ratecontroller {
|
||||
let sleep_time_ns = ((self.config.min_change_interval % 1.0) * 1e9) as u32;
|
||||
let sleep_time = Duration::new(sleep_time_s, sleep_time_ns);
|
||||
|
||||
let (start_s, _) = Utils::get_current_time()?;
|
||||
let (mut lastchg_s, mut lastchg_ns) = Utils::get_current_time()?;
|
||||
let time = Clock::new(ClockId::Monotonic);
|
||||
let start_s = time.get_seconds() as f64;
|
||||
let (mut lastchg_s, mut lastchg_ns) =
|
||||
(time.get_seconds() as f64, time.get_nanoseconds() as f64);
|
||||
let mut lastchg_t = lastchg_s - start_s + lastchg_ns / 1e9;
|
||||
let mut lastdump_t = lastchg_t - 310.0;
|
||||
|
||||
@@ -331,7 +335,8 @@ impl Ratecontroller {
|
||||
loop {
|
||||
sleep(sleep_time);
|
||||
|
||||
let (mut now_s, now_ns) = Utils::get_current_time()?;
|
||||
let now = Clock::new(ClockId::Monotonic);
|
||||
let (mut now_s, now_ns) = (now.get_seconds() as f64, now.get_nanoseconds() as f64);
|
||||
let now_abstime = now_s + now_ns / 1e9;
|
||||
now_s = now_s - start_s;
|
||||
let now_t = now_s + now_ns / 1e9;
|
||||
@@ -374,7 +379,11 @@ impl Ratecontroller {
|
||||
self.state_dl.current_rate = self.state_dl.next_rate;
|
||||
self.state_ul.current_rate = self.state_ul.next_rate;
|
||||
|
||||
(lastchg_s, lastchg_ns) = Utils::get_current_time()?;
|
||||
let lastchg = Clock::new(ClockId::Monotonic);
|
||||
(lastchg_s, lastchg_ns) = (
|
||||
lastchg.get_seconds() as f64,
|
||||
lastchg.get_nanoseconds() as f64,
|
||||
);
|
||||
|
||||
debug!(
|
||||
"{},{},{},{},{},{},{},{}",
|
||||
|
||||
@@ -1,13 +1,3 @@
|
||||
use nix::errno::Errno;
|
||||
use nix::time::{clock_gettime, ClockId};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum UtilError {
|
||||
#[error("libc error")]
|
||||
Libc(#[from] Errno),
|
||||
}
|
||||
|
||||
pub struct Utils {}
|
||||
|
||||
impl Utils {
|
||||
@@ -17,8 +7,4 @@ impl Utils {
|
||||
pub fn ewma_factor(tick: f64, dur: f64) -> f64 {
|
||||
((0.5_f64).ln() / (dur / tick)).exp()
|
||||
}
|
||||
pub fn get_current_time() -> Result<(f64, f64), UtilError> {
|
||||
let time = clock_gettime(ClockId::CLOCK_MONOTONIC)?;
|
||||
Ok((time.tv_sec() as f64, time.tv_nsec() as f64))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user