https://github.com/dyorgio/cpu-watcher
A Java Library/App to limit/monitor another process CPU usage.
https://github.com/dyorgio/cpu-watcher
Last synced: 6 months ago
JSON representation
A Java Library/App to limit/monitor another process CPU usage.
- Host: GitHub
- URL: https://github.com/dyorgio/cpu-watcher
- Owner: dyorgio
- License: apache-2.0
- Created: 2018-07-19T19:36:14.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-11T23:15:10.000Z (over 4 years ago)
- Last Synced: 2025-07-31T14:57:28.850Z (11 months ago)
- Language: Java
- Homepage: https://dyorgio.github.io/cpu-watcher/
- Size: 136 KB
- Stars: 16
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Cpu Watcher
===============
[](https://github.com/dyorgio/cpu-watcher/actions/workflows/maven.yml) [](https://maven-badges.herokuapp.com/maven-central/com.github.dyorgio.runtime/cpu-watcher)
A Java Library/App to monitor/limit another process CPU usage.
Why use it?
-----
* Limit any process cpu time usage in a multiplatform way.
How it works?
-----
Using a new thread to each external process that you want to monitor/limit this library watches cpu times and sends signals according with cpu specified limit and timelapse.
* SIGSTOP/SIGCONT on macos/linux
* NtSuspendProcess/NtResumeProcess on Windows.
Usage
-----
As Java library:
```java
// Create a new CpuWatcher object with target PID (own pid throws exception to prevents deadlock).
// Target percentage is not per core, is always over the entire system load,
// on example above we want 50% of 1 core only, and host cpu has 8 cores (4 phisical, 4 HT).
// 50%/8 = 6.25%
CpuWatcher cpuWatcher = new CpuWatcher(pid, 50f * CpuWatcher.getOneCoreOnePercent());
// start watcher thread
cpuWatcher.start();
// You can monitor current cpu usage too!
cpuWatcher.getCpuUsage();
// ... or change usage limit at runtime (null to disable limiter)
cpuWatcher.setUsageLimit(null);
// Wait for process (optional);
cpuWatcher.join();
```
As Standalone App:
```bash
java -jar cpu-watcher-$VERSION.jar $PID $MAX_CPU
```
Maven
-----
```xml
com.github.dyorgio.runtime
cpu-watcher
1.3.1
```