https://github.com/karantan/psusage
Tool for monitoring CPU usage of a program (and its forks)
https://github.com/karantan/psusage
Last synced: 4 months ago
JSON representation
Tool for monitoring CPU usage of a program (and its forks)
- Host: GitHub
- URL: https://github.com/karantan/psusage
- Owner: karantan
- License: bsd-3-clause
- Created: 2022-08-05T11:53:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-08-19T08:16:34.000Z (over 3 years ago)
- Last Synced: 2025-04-07T13:15:46.891Z (9 months ago)
- Language: Go
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# psusage 
Tool for monitoring CPU usage for a program (and its forks).
Inspired by https://github.com/struCoder/pidusage, but I've decided only to get the
information from the [`ps`](https://man7.org/linux/man-pages/man1/ps.1.html) tool and
not parse `/proc//stat` file because this is already done by the `ps` tool.
> /proc/[pid]/stat
> Status information about the process. This is used by
> [ps(1)](https://man7.org/linux/man-pages/man1/ps.1.html).
Ref: https://man7.org/linux/man-pages/man5/proc.5.html
With this change, the code should be simpler and easier to hack.
## Key concepts and definitions
We use the same logic as AWS (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/burstable-credits-baseline-concepts.html).
### CPU credit
A unit of vCPU-time.
Examples:
1 CPU credit = 1 vCPU * 100% utilization * 60 seconds.
1 CPU credit = 6000 vCPU utilisation seconds.
So one server has 8_640_000 vCPU utilisation seconds (i.e. CPU credits) per day per CPU.
Around this number, one must balance out processes and their CPU usage on a server.
## How it works
Basically run `ps -o pcpu=,time=,pid=,user:32=,comm= $(pidof )` and add process this
information. Example of output for program `php-fpm`
```
0.1 00:00:00 3477510 root myprogram
36.5 00:01:23 3477549 foo_com myprogram
10.1 00:00:21 3477680 bar_com myprogram
0.2 00:00:00 3477884 baz_com myprogram
```
We update CPU utilization of the process every second, and when the process no longer
exists (i.e. it stopped), we send CPU credits to the InfluxDB.
From there, we can then do all sorts of aggregation because we have all the information we
need:
1. Average CPU utilisation (in %)
2. Duration (in seconds)
3. Program name (e.g. `php-fpm`)
4. Effective user (e.g. `foo_com`)
## (Known) Limitations
TODO
## Testing
Use `stress` tool. It imposes a configurable amount of CPU, memory, I/O, and disk stress
on the system.
## Build the package with nix
Run the following command:
```
$ nix-build -E "with import {}; callPackage ./default.nix {}"
```
The version of the program will be passed during the build process via buildFlagsArray
in the `default.nix`.
## Usage
TODO
## Installation
TODO
## Development
TODO
## Contributing
1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request