|
| 1 | +# Service Stats Functionality |
| 2 | + |
| 3 | +This document describes the stats functionality in Zinit, which provides memory and CPU usage information for services and their child processes. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The stats functionality allows you to monitor the resource usage of services managed by Zinit. It provides information about: |
| 8 | + |
| 9 | +- Memory usage (in bytes) |
| 10 | +- CPU usage (as a percentage) |
| 11 | +- Child processes and their resource usage |
| 12 | + |
| 13 | +This is particularly useful for monitoring system resources and identifying services that might be consuming excessive resources. |
| 14 | + |
| 15 | +## Command Line Usage |
| 16 | + |
| 17 | +To get stats for a service using the command line: |
| 18 | + |
| 19 | +```bash |
| 20 | +zinit stats <service-name> |
| 21 | +``` |
| 22 | + |
| 23 | +Example: |
| 24 | +```bash |
| 25 | +zinit stats nginx |
| 26 | +``` |
| 27 | + |
| 28 | +This will output YAML-formatted stats information: |
| 29 | + |
| 30 | +```yaml |
| 31 | +name: nginx |
| 32 | +pid: 1234 |
| 33 | +memory_usage: 10485760 # Memory usage in bytes (10MB) |
| 34 | +cpu_usage: 2.5 # CPU usage as percentage |
| 35 | +children: # Stats for child processes |
| 36 | + - pid: 1235 |
| 37 | + memory_usage: 5242880 |
| 38 | + cpu_usage: 1.2 |
| 39 | + - pid: 1236 |
| 40 | + memory_usage: 4194304 |
| 41 | + cpu_usage: 0.8 |
| 42 | +``` |
| 43 | +
|
| 44 | +## JSON-RPC API |
| 45 | +
|
| 46 | +The stats functionality is also available through the JSON-RPC API: |
| 47 | +
|
| 48 | +### Method: `service_stats` |
| 49 | + |
| 50 | +Get memory and CPU usage statistics for a service. |
| 51 | + |
| 52 | +**Parameters:** |
| 53 | +- `name` (string, required): The name of the service to get stats for |
| 54 | + |
| 55 | +**Returns:** |
| 56 | +- Object containing stats information: |
| 57 | + - `name` (string): Service name |
| 58 | + - `pid` (integer): Process ID of the service |
| 59 | + - `memory_usage` (integer): Memory usage in bytes |
| 60 | + - `cpu_usage` (number): CPU usage as a percentage (0-100) |
| 61 | + - `children` (array): Stats for child processes |
| 62 | + - Each child has: |
| 63 | + - `pid` (integer): Process ID of the child process |
| 64 | + - `memory_usage` (integer): Memory usage in bytes |
| 65 | + - `cpu_usage` (number): CPU usage as a percentage (0-100) |
| 66 | + |
| 67 | +**Example Request:** |
| 68 | +```json |
| 69 | +{ |
| 70 | + "jsonrpc": "2.0", |
| 71 | + "id": 1, |
| 72 | + "method": "service_stats", |
| 73 | + "params": { |
| 74 | + "name": "nginx" |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +**Example Response:** |
| 80 | +```json |
| 81 | +{ |
| 82 | + "jsonrpc": "2.0", |
| 83 | + "id": 1, |
| 84 | + "result": { |
| 85 | + "name": "nginx", |
| 86 | + "pid": 1234, |
| 87 | + "memory_usage": 10485760, |
| 88 | + "cpu_usage": 2.5, |
| 89 | + "children": [ |
| 90 | + { |
| 91 | + "pid": 1235, |
| 92 | + "memory_usage": 5242880, |
| 93 | + "cpu_usage": 1.2 |
| 94 | + }, |
| 95 | + { |
| 96 | + "pid": 1236, |
| 97 | + "memory_usage": 4194304, |
| 98 | + "cpu_usage": 0.8 |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +**Possible Errors:** |
| 106 | +- `-32000`: Service not found |
| 107 | +- `-32003`: Service is down |
| 108 | + |
| 109 | +## Implementation Details |
| 110 | + |
| 111 | +The stats functionality works by: |
| 112 | + |
| 113 | +1. Reading process information from `/proc/<pid>/` directories on Linux systems |
| 114 | +2. Calculating memory usage from `/proc/<pid>/status` (VmRSS field) |
| 115 | +3. Calculating CPU usage by sampling `/proc/<pid>/stat` over a short interval |
| 116 | +4. Identifying child processes by checking the PPid field in `/proc/<pid>/status` |
| 117 | + |
| 118 | +On non-Linux systems, the functionality provides placeholder values as the `/proc` filesystem is specific to Linux. |
| 119 | + |
| 120 | +## Notes |
| 121 | + |
| 122 | +- Memory usage is reported in bytes |
| 123 | +- CPU usage is reported as a percentage (0-100) |
| 124 | +- The service must be running to get stats (otherwise an error is returned) |
| 125 | +- Child processes are identified by their parent PID matching the service's PID |
0 commit comments