pwrzv 0.3.0

A Rolls-Royce–inspired performance reserve meter for Linux systems
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# pwrzv

[![CI](https://github.com/kookyleo/pwrzv/workflows/CI/badge.svg)](https://github.com/kookyleo/pwrzv/actions)
[![codecov](https://codecov.io/gh/kookyleo/pwrzv/graph/badge.svg?token=CqfBIyojDm)](https://codecov.io/gh/kookyleo/pwrzv)
[![Crates.io](https://img.shields.io/crates/v/pwrzv.svg)](https://crates.io/crates/pwrzv)
[![Documentation](https://docs.rs/pwrzv/badge.svg)](https://docs.rs/pwrzv)
[![License](https://img.shields.io/crates/l/pwrzv.svg)](https://github.com/kookyleo/pwrzv#license)


![pwrzv](./assets/Pwrzv-in-Rolls-Royce.jpg)

A Rolls-Royce–inspired performance reserve meter for Linux and macOS systems.
Elegant, minimal, and focused on what really matters: how much performance your machine has left to give.

## ⚠️ Beta Stage Notice

**This library is currently in Beta stage and not yet fully mature.**

- Parameter tuning may not be precise enough and might need adjustment for specific systems
- API and behavior may change in future versions
- We welcome your feedback and contributions through [Issues]https://github.com/kookyleo/pwrzv/issues and [Pull Requests]https://github.com/kookyleo/pwrzv/pulls
- Please test thoroughly before using in production environments

Your feedback is crucial for improving this project!

## 🛠 What is pwrzv?

Inspired by the Power Reserve gauge in Rolls-Royce cars — which shows how much engine power is still available — pwrzv brings the same philosophy to Unix-like systems. Instead of showing raw usage, it estimates how much headroom remains in your system's core resources.

It provides a simple 0–5 score, calculated from multiple real-time metrics:

- **CPU usage and I/O wait**
- **Memory availability**
- **Swap activity**
- **Disk I/O**
- **Network throughput and packet loss**
- **File descriptor consumption**

All inputs are weighted and transformed via sigmoid functions to reflect practical bottlenecks, not just raw numbers.

## 🚦 Example Output

### Basic Usage
```bash
$ pwrzv
System Metrics:
  CPU Usage: 12.34% (iowait: 0.00%)
  Memory Available: 78.50%
  Swap Usage: 0.00%
  Disk I/O Usage: 5.10%
  Network I/O Usage: 0.75%
  File Descriptor Usage: 3.42%
Power Reserve Score: 5 (Excellent - Abundant resources)
```

### Detailed Analysis
```bash
$ pwrzv --detailed
=== System Power Reserve Analysis ===

System Metrics:
  CPU Usage: 12.34% (iowait: 0.00%)
  Memory Available: 78.50%
  Swap Usage: 0.00%
  Disk I/O Usage: 5.10%
  Network I/O Usage: 0.75%
  File Descriptor Usage: 3.42%

Component Scores (0-5):
  CPU:              5
  I/O Wait:         5
  Memory:           4
  Swap:             5
  Disk I/O:         5
  Network I/O:      5
  File Descriptors: 5

Overall Assessment:
  Power Reserve Score: 4 (Good - Ample resources)
  Bottlenecks: None

✅ System has ample performance headroom.
```

## 📦 Installation

### From Source
```bash
git clone https://github.com/kookyleo/pwrzv.git
cd pwrzv
cargo install --path .
```

### Using Cargo
```bash
cargo install pwrzv
```

## 🖥️ Platform Support

**pwrzv supports Linux and macOS systems** for now. Other platforms will display an error message.

Check platform compatibility:
```bash
pwrzv --check-platform
```

### Platform-Specific Implementation

- **Linux**: Uses `/proc` filesystem for direct system metrics access
- **macOS**: Uses system commands (`sysctl`, `vm_stat`, `iostat`, etc.) for metrics collection

## 🔧 Usage

### Command Line Interface

```bash
# Basic usage
pwrzv

# Detailed component analysis
pwrzv --detailed

# JSON output
pwrzv --format json

# YAML output
pwrzv --format yaml

# Quiet mode (suppress warnings)
pwrzv --quiet

# Check platform compatibility
pwrzv --check-platform
```

### Library Usage

```rust
use pwrzv::{PowerReserveCalculator, PwrzvError};

fn main() -> Result<(), PwrzvError> {
    let calculator = PowerReserveCalculator::new();
    let metrics = calculator.collect_metrics()?;
    let score = calculator.calculate_power_reserve(&metrics)?;
    println!("Power Reserve Score: {}", score);
    Ok(())
}
```

#### Detailed Analysis

```rust
use pwrzv::{PowerReserveCalculator, PwrzvError};

fn main() -> Result<(), PwrzvError> {
    let calculator = PowerReserveCalculator::new();
    let metrics = calculator.collect_metrics()?;
    let detailed = calculator.calculate_detailed_score(&metrics)?;
    
    println!("Overall Score: {} ({})", detailed.final_score, detailed.level);
    println!("Bottlenecks: {}", detailed.bottleneck);
    println!("CPU Score: {}", detailed.component_scores.cpu);
    Ok(())
}
```

#### Custom Configuration

```rust
use pwrzv::{PowerReserveCalculator, SigmoidConfig, PwrzvError};

fn main() -> Result<(), PwrzvError> {
    let mut config = SigmoidConfig::default();
    config.cpu_threshold = 0.8;  // More sensitive CPU threshold
    
    let calculator = PowerReserveCalculator::with_config(config);
    let metrics = calculator.collect_metrics()?;
    let score = calculator.calculate_power_reserve(&metrics)?;
    println!("Power Reserve Score: {}", score);
    Ok(())
}
```

## 📊 Scoring System

The scoring system uses sigmoid functions to map resource utilization to a 0-5 scale:

- **5 (Excellent)**: Abundant resources, system running smoothly
- **4 (Good)**: Ample resources available, good performance
- **3 (Moderate)**: Adequate performance, resources sufficient
- **2 (Low)**: Resource constrained, consider optimization
- **0-1 (Critical)**: System under heavy load, immediate attention needed

### How It Works

1. **Resource Collection**: Gathers metrics from `/proc` filesystem or system commands
2. **Normalization**: Converts raw metrics to 0-1 scale
3. **Sigmoid Transformation**: Applies configurable thresholds and curves
4. **Bottleneck Detection**: Takes the minimum score (worst resource)
5. **Final Scoring**: Maps to 0-5 range with level descriptions

## 🧮 Numerical Calculation Methods

pwrzv employs sophisticated mathematical algorithms to convert raw system metrics into meaningful power reserve scores:

### Sigmoid Function Transformation

The core calculation uses the **sigmoid function** to transform linear resource utilization into a smooth 0-1 scale:

```
f(x) = 1 / (1 + e^(-k * (x - x₀)))
```

Where:
- **x**: Raw metric value (0-1 range after normalization)
- **x₀ (midpoint)**: The threshold where the metric begins significantly impacting the score
- **k (steepness)**: Controls the curve's steepness; higher values create more dramatic score changes

### Multi-Stage Processing Pipeline

1. **Raw Data Collection**: Platform-specific metric gathering (Linux: `/proc` filesystem, macOS: system commands)
2. **Normalization**: Convert raw values to 0-1 scale for consistent processing
3. **Sigmoid Transformation**: Apply individual sigmoid curves to each metric based on its characteristics
4. **Bottleneck Analysis**: Identify the worst-performing resource (minimum score)
5. **Final Mapping**: Transform the 0-1 result to the 0-5 Power Reserve scale

### Adaptive Thresholds

Each metric uses carefully tuned parameters:
- **CPU metrics**: Balanced sensitivity for both usage spikes and sustained load
- **Memory metrics**: Higher thresholds to account for normal OS caching behavior  
- **I/O metrics**: Moderate sensitivity to distinguish between light and heavy workloads
- **Network metrics**: Separate handling for bandwidth utilization vs. packet loss sensitivity

This mathematical approach ensures that pwrzv provides intuitive, actionable scores that reflect real system performance bottlenecks rather than raw utilization percentages.

## ⚙️ Environment Variable Configuration

pwrzv supports customizing sigmoid function parameters for each metric via environment variables to adapt to different system characteristics and use cases.

### macOS Platform Environment Variables

```bash
# CPU usage configuration (default: midpoint=0.60, steepness=8.0)
export PWRZV_MACOS_CPU_USAGE_MIDPOINT=0.60
export PWRZV_MACOS_CPU_USAGE_STEEPNESS=8.0

# CPU load configuration (default: midpoint=1.2, steepness=5.0)
export PWRZV_MACOS_CPU_LOAD_MIDPOINT=1.2
export PWRZV_MACOS_CPU_LOAD_STEEPNESS=5.0

# Memory usage configuration (default: midpoint=0.85, steepness=20.0)
export PWRZV_MACOS_MEMORY_USAGE_MIDPOINT=0.85
export PWRZV_MACOS_MEMORY_USAGE_STEEPNESS=20.0

# Memory compression configuration (default: midpoint=0.60, steepness=15.0)
export PWRZV_MACOS_MEMORY_COMPRESSED_MIDPOINT=0.60
export PWRZV_MACOS_MEMORY_COMPRESSED_STEEPNESS=15.0

# Disk I/O configuration (default: midpoint=0.70, steepness=10.0)
export PWRZV_MACOS_DISK_IO_MIDPOINT=0.70
export PWRZV_MACOS_DISK_IO_STEEPNESS=10.0

# Network bandwidth configuration (default: midpoint=0.80, steepness=6.0)
export PWRZV_MACOS_NETWORK_MIDPOINT=0.80
export PWRZV_MACOS_NETWORK_STEEPNESS=6.0

# Network packet loss configuration (default: midpoint=0.01, steepness=50.0)
export PWRZV_MACOS_NETWORK_DROPPED_MIDPOINT=0.01
export PWRZV_MACOS_NETWORK_DROPPED_STEEPNESS=50.0

# File descriptor configuration (default: midpoint=0.90, steepness=30.0)
export PWRZV_MACOS_FD_MIDPOINT=0.90
export PWRZV_MACOS_FD_STEEPNESS=30.0

# Process count configuration (default: midpoint=0.80, steepness=12.0)
export PWRZV_MACOS_PROCESS_MIDPOINT=0.80
export PWRZV_MACOS_PROCESS_STEEPNESS=12.0
```

### Linux Platform Environment Variables

```bash
# CPU usage configuration (default: midpoint=0.65, steepness=8.0)
export PWRZV_LINUX_CPU_USAGE_MIDPOINT=0.65
export PWRZV_LINUX_CPU_USAGE_STEEPNESS=8.0

# CPU I/O wait configuration (default: midpoint=0.20, steepness=20.0)
export PWRZV_LINUX_CPU_IOWAIT_MIDPOINT=0.20
export PWRZV_LINUX_CPU_IOWAIT_STEEPNESS=20.0

# CPU load configuration (default: midpoint=1.2, steepness=5.0)
export PWRZV_LINUX_CPU_LOAD_MIDPOINT=1.2
export PWRZV_LINUX_CPU_LOAD_STEEPNESS=5.0

# Memory usage configuration (default: midpoint=0.85, steepness=18.0)
export PWRZV_LINUX_MEMORY_USAGE_MIDPOINT=0.85
export PWRZV_LINUX_MEMORY_USAGE_STEEPNESS=18.0

# Memory pressure configuration (default: midpoint=0.30, steepness=12.0)
export PWRZV_LINUX_MEMORY_PRESSURE_MIDPOINT=0.30
export PWRZV_LINUX_MEMORY_PRESSURE_STEEPNESS=12.0

# Disk I/O configuration (default: midpoint=0.70, steepness=10.0)
export PWRZV_LINUX_DISK_IO_MIDPOINT=0.70
export PWRZV_LINUX_DISK_IO_STEEPNESS=10.0

# Network bandwidth configuration (default: midpoint=0.80, steepness=6.0)
export PWRZV_LINUX_NETWORK_MIDPOINT=0.80
export PWRZV_LINUX_NETWORK_STEEPNESS=6.0

# Network packet loss configuration (default: midpoint=0.01, steepness=50.0)
export PWRZV_LINUX_NETWORK_DROPPED_MIDPOINT=0.01
export PWRZV_LINUX_NETWORK_DROPPED_STEEPNESS=50.0

# File descriptor configuration (default: midpoint=0.90, steepness=25.0)
export PWRZV_LINUX_FD_MIDPOINT=0.90
export PWRZV_LINUX_FD_STEEPNESS=25.0

# Process count configuration (default: midpoint=0.80, steepness=12.0)
export PWRZV_LINUX_PROCESS_MIDPOINT=0.80
export PWRZV_LINUX_PROCESS_STEEPNESS=12.0
```

### Parameter Meanings

- **midpoint**: Sigmoid function midpoint value, representing the threshold where this metric starts significantly affecting the score
- **steepness**: Sigmoid function steepness, higher values make the curve steeper and score changes more dramatic

### Usage Example

```bash
# Adjust CPU threshold for high-performance server
export PWRZV_LINUX_CPU_USAGE_MIDPOINT=0.80
export PWRZV_LINUX_CPU_USAGE_STEEPNESS=15.0

# Run pwrzv
pwrzv --detailed
```

## 🧪 Philosophy

While most system monitors highlight how much is used, pwrzv tells you how much is left. This makes it a useful tool for:

- **Minimal dashboards** - Single metric overview
- **Autoscaling decisions** - When to scale up/down
- **Performance monitoring** - Proactive resource management
- **System health checks** - Quick status assessment

## 🔄 Examples

Run the included examples:

```bash
# Basic usage example
cargo run --example basic_usage

# Detailed analysis with different configurations
PWRZV_JSON_OUTPUT=1 cargo run --example detailed_analysis
```

## 🧪 Testing

```bash
# Run all tests
cargo test

# Run only unit tests
cargo test --lib

# Run documentation tests
cargo test --doc

# Run examples
cargo run --example basic_usage
```

## 📚 API Documentation

Generate and view the full API documentation:

```bash
cargo doc --open
```

## 🤝 Contributing

1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass: `cargo test`
5. Submit a pull request

## 📄 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- Inspired by the Power Reserve gauge in Rolls-Royce automobiles
- Built with Rust for performance and reliability
- Thanks to the Linux kernel for providing comprehensive `/proc` metrics