From f460dab2b38cf0030ebf78257ecb2f4968e6bea9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?=
Date: Sun, 27 Mar 2022 03:33:22 +0200
Subject: [PATCH] udp: add statistics latencies flag, only calculate when set
---
TODO.md | 1 -
aquatic_udp/src/common.rs | 6 ++++++
aquatic_udp/src/config.rs | 3 +++
aquatic_udp/src/workers/socket.rs | 14 +++++++++++---
aquatic_udp/src/workers/statistics.rs | 13 +++++++++----
aquatic_udp/templates/statistics.html | 14 +++++++++-----
6 files changed, 38 insertions(+), 13 deletions(-)
diff --git a/TODO.md b/TODO.md
index dddbef86..c9586747 100644
--- a/TODO.md
+++ b/TODO.md
@@ -5,7 +5,6 @@
latency histogram
* use SyncHistogram?
* avoid reporting zero (send more often?)
- * add config flag, don't calculate when turned off
* cleaning
## Medium priority
diff --git a/aquatic_udp/src/common.rs b/aquatic_udp/src/common.rs
index e3cac67c..3f0b41da 100644
--- a/aquatic_udp/src/common.rs
+++ b/aquatic_udp/src/common.rs
@@ -17,6 +17,12 @@ pub const MAX_PACKET_SIZE: usize = 8192;
#[derive(Clone, Copy, Debug)]
pub struct RequestTag(pub usize);
+impl RequestTag {
+ pub fn placeholder() -> Self {
+ Self(0)
+ }
+}
+
#[derive(Debug)]
pub struct PendingScrapeRequest {
pub slab_key: usize,
diff --git a/aquatic_udp/src/config.rs b/aquatic_udp/src/config.rs
index 6e235992..617ec4da 100644
--- a/aquatic_udp/src/config.rs
+++ b/aquatic_udp/src/config.rs
@@ -141,6 +141,8 @@ pub struct StatisticsConfig {
pub write_html_to_file: bool,
/// Path to save HTML file to
pub html_file_path: PathBuf,
+ /// Report response latencies
+ pub latencies: bool,
}
impl StatisticsConfig {
@@ -156,6 +158,7 @@ impl Default for StatisticsConfig {
print_to_stdout: false,
write_html_to_file: false,
html_file_path: "tmp/statistics.html".into(),
+ latencies: true,
}
}
}
diff --git a/aquatic_udp/src/workers/socket.rs b/aquatic_udp/src/workers/socket.rs
index 85ba6c1e..f9837c8c 100644
--- a/aquatic_udp/src/workers/socket.rs
+++ b/aquatic_udp/src/workers/socket.rs
@@ -308,7 +308,9 @@ pub fn run_socket_worker(
last_pending_scrape_cleaning = now;
}
- if now > last_histogram_sending + statistics_update_interval {
+ if config.statistics.latencies
+ & (now > last_histogram_sending + statistics_update_interval)
+ {
if let Err(err) = histogram_sender.try_send(latency_recorder.extract_histogram()) {
::log::error!("Couldn't send latency data to statistics worker: {:#}", err);
}
@@ -345,7 +347,11 @@ fn read_requests(
loop {
match socket.recv_from(&mut buffer[..]) {
Ok((amt, src)) => {
- let request_tag = latency_recorder.request_received();
+ let request_tag = if config.statistics.latencies {
+ latency_recorder.request_received()
+ } else {
+ RequestTag::placeholder()
+ };
let res_request =
Request::from_bytes(&buffer[..amt], config.protocol.max_scrape_torrents);
@@ -580,7 +586,9 @@ fn send_response(
match socket.send_to(&cursor.get_ref()[..amt], addr) {
Ok(amt) if config.statistics.active() => {
- latency_recorder.response_sent(tag);
+ if config.statistics.latencies {
+ latency_recorder.response_sent(tag);
+ }
let stats = if canonical_addr_is_ipv4 {
&state.statistics_ipv4
diff --git a/aquatic_udp/src/workers/statistics.rs b/aquatic_udp/src/workers/statistics.rs
index 423863a5..49f5aac6 100644
--- a/aquatic_udp/src/workers/statistics.rs
+++ b/aquatic_udp/src/workers/statistics.rs
@@ -171,7 +171,8 @@ struct TemplateData {
ipv6_active: bool,
ipv4: FormattedStatistics,
ipv6: FormattedStatistics,
- latency: LatencyData,
+ latencies_active: bool,
+ latencies: LatencyData,
last_updated: String,
peer_update_interval: String,
}
@@ -201,7 +202,7 @@ pub fn run_statistics_worker(
loop {
::std::thread::sleep(Duration::from_secs(config.statistics.interval));
- let latency_data: LatencyData = histogram_receivers
+ let latency_data = histogram_receivers
.iter()
.filter_map(|receiver| receiver.try_recv().ok())
.sum::>()
@@ -215,7 +216,10 @@ pub fn run_statistics_worker(
if config.statistics.print_to_stdout {
println!("General:");
println!(" access list entries: {}", state.access_list.load().len());
- println!(" {}", latency_data);
+
+ if config.statistics.latencies {
+ println!(" {}", latency_data);
+ }
if config.network.ipv4_active() {
println!("IPv4:");
@@ -236,7 +240,8 @@ pub fn run_statistics_worker(
ipv6_active: config.network.ipv6_active(),
ipv4: statistics_ipv4,
ipv6: statistics_ipv6,
- latency: latency_data,
+ latencies_active: config.statistics.latencies,
+ latencies: latency_data,
last_updated: OffsetDateTime::now_utc()
.format(&Rfc2822)
.unwrap_or("(formatting error)".into()),
diff --git a/aquatic_udp/templates/statistics.html b/aquatic_udp/templates/statistics.html
index ead35bda..87a1a43a 100644
--- a/aquatic_udp/templates/statistics.html
+++ b/aquatic_udp/templates/statistics.html
@@ -20,31 +20,35 @@
Updated: { last_updated } (UTC)
+ {{ if latencies_active }}
+
Response latencies
| Mean |
- { latency.mean } ms |
+ { latencies.mean } ms |
| Standard deviation |
- { latency.stdev } ms |
+ { latencies.stdev } ms |
| 95:th percentile |
- { latency.p95 } ms |
+ { latencies.p95 } ms |
| 99:th percentile |
- { latency.p99 } ms |
+ { latencies.p99 } ms |
| 99.9:th percentile |
- { latency.p999 } ms |
+ { latencies.p999 } ms |
+ {{ endif }}
+
{{ if ipv4_active }}
IPv4