Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce timestamp overhead #48

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/drivers/rest/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use lazy_static::lazy_static;
use mavlink::{self, Message};
use serde::{Deserialize, Serialize};

use crate::protocol::timestamp_micros;

lazy_static! {
static ref DATA: Data = Data {
messages: Arc::new(Mutex::new(MAVLinkVehiclesData::default())),
Expand Down Expand Up @@ -135,7 +137,7 @@ struct Temporal {

impl Default for Temporal {
fn default() -> Self {
let now_us = chrono::Local::now().timestamp_micros() as u64;
let now_us = timestamp_micros();
Self {
first_update_us: now_us,
last_update_us: now_us,
Expand All @@ -147,7 +149,7 @@ impl Default for Temporal {

impl Temporal {
fn update(&mut self) {
self.last_update_us = chrono::Local::now().timestamp_micros() as u64;
self.last_update_us = timestamp_micros();
self.counter = self.counter.wrapping_add(1);
self.frequency =
(10e6 * self.counter as f32) / ((self.last_update_us - self.first_update_us) as f32);
Expand Down
5 changes: 2 additions & 3 deletions src/drivers/tlog/reader.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Result};
use chrono::DateTime;
use mavlink::ardupilotmega::MavMessage;
use mavlink_server::callbacks::{Callbacks, MessageCallback};
use tokio::sync::{broadcast, RwLock};
use tracing::*;

use crate::{
drivers::{Driver, DriverInfo},
protocol::Protocol,
protocol::{check_timestamp_us, Protocol},
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
Expand Down Expand Up @@ -85,7 +84,7 @@ impl TlogReader {
u64::from_be_bytes(timestamp_bytes)
};

if DateTime::from_timestamp_micros(us_since_epoch as i64).is_none() {
if !check_timestamp_us(us_since_epoch) {
warn!("Failed to convert unix time: {us_since_epoch:?}");

reader.consume(1);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/tlog/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::*;

use crate::{
drivers::{Driver, DriverInfo},
protocol::Protocol,
protocol::{timestamp_micros, Protocol},
stats::{
accumulated::driver::{AccumulatedDriverStats, AccumulatedDriverStatsProvider},
driver::DriverUuid,
Expand Down Expand Up @@ -70,7 +70,7 @@ impl TlogWriter {
loop {
match hub_receiver.recv().await {
Ok(message) => {
let timestamp = chrono::Utc::now().timestamp_micros() as u64;
let timestamp = timestamp_micros();

self.stats.write().await.stats.update_output(&message);

Expand Down
16 changes: 15 additions & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Protocol {
pub fn new(origin: &str, message: MAVLinkV2MessageRaw) -> Self {
Self {
origin: origin.to_string(),
timestamp: chrono::Utc::now().timestamp_micros() as u64,
timestamp: timestamp_micros(),
message,
}
}
Expand Down Expand Up @@ -81,3 +81,17 @@ impl DerefMut for Protocol {
&mut self.message
}
}

#[inline(always)]
pub fn timestamp_micros() -> u64 {
std::time::SystemTime::now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are doing this, I would vote for avoiding SystemTime and using a monotonic source if possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the monotonic would be std::Instant::now()

.duration_since(std::time::UNIX_EPOCH)
.map(|duration| duration.as_micros() as u64)
.unwrap()
}

#[inline(always)]
pub fn check_timestamp_us(micros: u64) -> bool {
let duration = std::time::Duration::from_micros(micros);
std::time::UNIX_EPOCH.checked_add(duration).is_some()
}
6 changes: 3 additions & 3 deletions src/stats/accumulated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use serde::Serialize;

use crate::protocol::Protocol;
use crate::protocol::{timestamp_micros, Protocol};

#[derive(Clone, Debug, Serialize)]
pub struct AccumulatedStatsInner {
Expand All @@ -20,7 +20,7 @@ impl Default for AccumulatedStatsInner {
fn default() -> Self {
Self {
last_message: None,
last_update_us: chrono::Utc::now().timestamp_micros() as u64,
last_update_us: timestamp_micros(),
messages: 0,
bytes: 0,
delay: 0,
Expand All @@ -31,7 +31,7 @@ impl Default for AccumulatedStatsInner {
impl AccumulatedStatsInner {
pub fn update(&mut self, message: &Arc<Protocol>) {
self.last_message = Some(message.clone());
self.last_update_us = chrono::Utc::now().timestamp_micros() as u64;
self.last_update_us = timestamp_micros();
self.bytes = self.bytes.wrapping_add(message.raw_bytes().len() as u64);
self.messages = self.messages.wrapping_add(1);
self.delay = self
Expand Down
5 changes: 3 additions & 2 deletions src/stats/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use tracing::*;

use crate::{
hub,
protocol::timestamp_micros,
stats::{
accumulated::{
driver::AccumulatedDriversStats, messages::AccumulatedHubMessagesStats,
Expand Down Expand Up @@ -128,7 +129,7 @@ impl StatsActor {
let last_accumulated_hub_messages_stats =
Arc::new(Mutex::new(AccumulatedHubMessagesStats::default()));
let hub_messages_stats = Arc::new(RwLock::new(HubMessagesStats::default()));
let start_time = Arc::new(RwLock::new(chrono::Utc::now().timestamp_micros() as u64));
let start_time = Arc::new(RwLock::new(timestamp_micros()));

Self {
start_time,
Expand Down Expand Up @@ -180,7 +181,7 @@ impl StatsActor {
if let Err(error) = hub::reset_all_stats().await {
error!("Failed resetting stats: {error:?}");
}
*self.start_time.write().await = chrono::Utc::now().timestamp_micros() as u64;
*self.start_time.write().await = timestamp_micros();

self.last_accumulated_drivers_stats.lock().await.clear();
driver_stats.clear();
Expand Down
Loading