Skip to content

Commit 6de7ec9

Browse files
authored
feat: more efficient network stats (#106)
* feat: more efficient network stats - Add --network-refresh-rate parameter for network stats optimization - Enhance modular architecture with ProcessedFlags and StatsContext - Improve CPU temperature detection with better comments * chore: update dependencies - anyhow 1.0.99 -> 1.0.100 - clap 4.5.47 -> 4.5.48 - cc 1.2.37 -> 1.2.38 - find-msvc-tools 0.1.1 -> 0.1.2
1 parent ae65117 commit 6de7ec9

File tree

6 files changed

+303
-97
lines changed

6 files changed

+303
-97
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ edition = "2021"
66
build = "build.rs"
77

88
[dependencies]
9-
anyhow = "1.0.99"
10-
clap = { version = "4.5.47", features = ["derive"] }
9+
anyhow = "1.0.100"
10+
clap = { version = "4.5.48", features = ["derive"] }
1111
sysinfo = { version = "0.37.0", default-features = false, features = ["component", "disk", "network", "system"] }
1212
tokio = { version = "1", features = ["full"] }
1313

1414
[build-dependencies]
15-
cc = "1.2.37"
15+
cc = "1.2.38"

README.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,32 @@ A simple system stats event provider for Sketchybar.
3636
Usage: stats_provider [OPTIONS]
3737

3838
Options:
39-
-a, --all Get all stats
40-
-c, --cpu <CPU>... Get CPU stats [possible values: count, frequency, temperature, usage]
41-
-d, --disk <DISK>... Get disk stats [possible values: count, free, total, usage, used]
42-
-m, --memory <MEMORY>... Get memory stats [possible values: ram_available, ram_total, ram_usage, ram_used, swp_free, swp_total, swp_usage, swp_used]
43-
-n, --network <NETWORK>... Network rx/tx in KB/s. Specify network interfaces (e.g., -n eth0 en0 lo0). At least one is required.
44-
-s, --system <SYSTEM>... Get system stats [possible values: arch, distro, host_name, kernel_version, name, os_version, long_os_version]
45-
-u, --uptime <UPTIME>... Get uptime stats [possible values: week, day, hour, min, sec]
46-
-i, --interval <INTERVAL> Refresh interval in seconds [default: 5]
47-
-b, --bar <BAR> Bar name (optional)
48-
--verbose Enable verbose output
49-
-h, --help Print help
50-
-V, --version Print version
39+
-a, --all Get all stats
40+
-c, --cpu <CPU>... Get CPU stats [possible values: count, frequency, temperature, usage]
41+
-d, --disk <DISK>... Get disk stats [possible values: count, free, total, usage, used]
42+
-m, --memory <MEMORY>... Get memory stats [possible values: ram_available, ram_total, ram_usage, ram_used, swp_free, swp_total, swp_usage, swp_used]
43+
-n, --network <NETWORK>... Network rx/tx in KB/s. Specify network interfaces (e.g., -n eth0 en0 lo0). At least one is required.
44+
-s, --system <SYSTEM>... Get system stats [possible values: arch, distro, host_name, kernel_version, name, os_version, long_os_version]
45+
-u, --uptime <UPTIME>... Get uptime stats [possible values: week, day, hour, min, sec]
46+
-i, --interval <INTERVAL> Refresh interval in seconds [default: 5]
47+
--network-refresh-rate <NETWORK_REFRESH_RATE> Network refresh rate (how often to refresh network interface list, in stat intervals) [default: 5]
48+
-b, --bar <BAR> Bar name (optional)
49+
--verbose Enable verbose output
50+
-h, --help Print help
51+
-V, --version Print version
5152
```
5253

5354
Example: trigger event with cpu, disk and ram usage percentages at a refresh interval of 2 seconds:
5455
```bash
5556
stats_provider --cpu usage --disk usage --memory ram_usage --interval 2
5657
```
5758

59+
Example: network monitoring with optimized refresh rate:
60+
```bash
61+
# Monitor network with interface refresh every 8 stat intervals
62+
stats_provider --network en0 --interval 3 --network-refresh-rate 8
63+
```
64+
5865
### Uptime Usage
5966

6067
The uptime system supports customizable time units. You can specify which units to display:
@@ -77,6 +84,28 @@ Available uptime units:
7784

7885
Units are automatically sorted from largest to smallest, with intelligent carry-over (e.g., excess hours carry into days).
7986

87+
### Network Optimization
88+
89+
The `--network-refresh-rate` parameter controls how frequently the network interface list is refreshed:
90+
91+
```bash
92+
# Default: refresh network interfaces every 5 stat intervals
93+
stats_provider --network en0 --interval 2 --network-refresh-rate 5
94+
95+
# More frequent refresh (every 2 intervals) for dynamic environments
96+
stats_provider --network en0 wlan0 --network-refresh-rate 2
97+
98+
# Less frequent refresh (every 10 intervals) for stable setups to reduce overhead
99+
stats_provider --network en0 --network-refresh-rate 10
100+
```
101+
102+
**Benefits:**
103+
- **Performance**: Reduces system calls by refreshing interface list less frequently
104+
- **Efficiency**: Network interfaces don't change rapidly, so frequent rescanning is unnecessary
105+
- **Customizable**: Adjust based on your network environment stability
106+
107+
**Recommendation:** Use higher values (8-15) for stable network setups, lower values (2-5) for environments where interfaces frequently change.
108+
80109
Add the `--verbose` flag to see more detailed output:
81110

82111
```console

src/cli.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
use anyhow::{bail, Result};
12
use clap::Parser;
23

4+
// Default values as constants
5+
pub const DEFAULT_INTERVAL: u32 = 5;
6+
pub const DEFAULT_NETWORK_REFRESH_RATE: u32 = 5;
7+
pub const MIN_INTERVAL: u32 = 1;
8+
pub const MAX_INTERVAL: u32 = 3600; // 1 hour max
9+
pub const MIN_NETWORK_REFRESH_RATE: u32 = 1;
10+
pub const MAX_NETWORK_REFRESH_RATE: u32 = 100;
11+
312
#[derive(Parser, Debug)]
413
#[command(name = "stats_provider", version, about, long_about = None, arg_required_else_help = true)]
514
pub struct Cli {
@@ -27,11 +36,18 @@ pub struct Cli {
2736
#[arg(
2837
short = 'i',
2938
long,
30-
default_value_t = 5,
39+
default_value_t = DEFAULT_INTERVAL,
3140
help = "Refresh interval in seconds"
3241
)]
3342
pub interval: u32,
3443

44+
#[arg(
45+
long,
46+
default_value_t = DEFAULT_NETWORK_REFRESH_RATE,
47+
help = "Network refresh rate (how often to refresh network interface list, in stat intervals)"
48+
)]
49+
pub network_refresh_rate: u32,
50+
3551
#[arg(short = 'b', long, help = "Bar name (optional)")]
3652
pub bar: Option<String>,
3753

@@ -43,6 +59,44 @@ pub fn parse_args() -> Cli {
4359
Cli::parse()
4460
}
4561

62+
pub fn validate_cli(cli: &Cli) -> Result<()> {
63+
// Validate interval
64+
if cli.interval < MIN_INTERVAL || cli.interval > MAX_INTERVAL {
65+
bail!(
66+
"Interval must be between {} and {} seconds, got {}",
67+
MIN_INTERVAL,
68+
MAX_INTERVAL,
69+
cli.interval
70+
);
71+
}
72+
73+
// Validate network refresh rate
74+
if cli.network_refresh_rate < MIN_NETWORK_REFRESH_RATE
75+
|| cli.network_refresh_rate > MAX_NETWORK_REFRESH_RATE
76+
{
77+
bail!(
78+
"Network refresh rate must be between {} and {}, got {}",
79+
MIN_NETWORK_REFRESH_RATE,
80+
MAX_NETWORK_REFRESH_RATE,
81+
cli.network_refresh_rate
82+
);
83+
}
84+
85+
// Validate that at least one stat type is requested if not using --all
86+
if !cli.all
87+
&& cli.cpu.is_none()
88+
&& cli.disk.is_none()
89+
&& cli.memory.is_none()
90+
&& cli.network.is_none()
91+
&& cli.system.is_none()
92+
&& cli.uptime.is_none()
93+
{
94+
bail!("At least one stat type must be specified, or use --all");
95+
}
96+
97+
Ok(())
98+
}
99+
46100
pub fn all_cpu_flags() -> Vec<&'static str> {
47101
vec!["count", "frequency", "temperature", "usage"]
48102
}

0 commit comments

Comments
 (0)