https://github.com/freeslave/resusage
D library for getting system and process resource usage
https://github.com/freeslave/resusage
cpu d dlang ram
Last synced: 7 months ago
JSON representation
D library for getting system and process resource usage
- Host: GitHub
- URL: https://github.com/freeslave/resusage
- Owner: FreeSlave
- License: bsl-1.0
- Created: 2015-06-07T19:17:48.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-15T22:16:40.000Z (about 2 years ago)
- Last Synced: 2025-01-22T14:45:44.036Z (9 months ago)
- Topics: cpu, d, dlang, ram
- Language: D
- Homepage:
- Size: 252 KB
- Stars: 16
- Watchers: 4
- Forks: 5
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE_1_0.txt
Awesome Lists containing this project
README
# Resusage
Obtaining of virtual memory, RAM and CPU usage by the whole system or by single process.
[](https://github.com/FreeSlave/resusage/actions/workflows/ci.yml)
[](https://ci.appveyor.com/project/FreeSlave/resusage)Currently works on Linux and Windows.
FreeBSD support is partial - only system-wide memory information and per-process CPU usage can be retrieved now.
## Generating documentation
Ddoc:
dub build --build=docs
Ddox:
dub build --build=ddox
## Brief
```d
// import module
import resusage.memory;// or the whole package
import resusage;// get system memory usage
SystemMemInfo sysMemInfo = systemMemInfo();// access properties
sysMemInfo.totalRAM;
sysMemInfo.usedRAM;
sysMemInfo.freeRAM;sysMemInfo.totalVirtMem;
sysMemInfo.usedVirtMem;
sysMemInfo.freeVirtMem;// actualize values after some amount of time
sysMemInfo.update();// get memory usage of the current process
ProcessMemInfo procMemInfo = processMemInfo();// or pass process ID to get info about specific process
int pid = ...;
ProcessMemInfo procMemInfo = processMemInfo(pid);// access properties
procMemInfo.usedVirtMem;
procMemInfo.usedRAM;// actualize values after some amount of time
procMemInfo.update();//import module
import resusage.cpu;// create watcher to watch system CPU
auto cpuWatcher = new SystemCPUWatcher();// get actual value when needed
double percent = cpuWatcher.current();// create CPU watcher for current process
auto cpuWatcher = new ProcessCPUWatcher();// or for process with given id
int pid = ...;
auto cpuWatcher = new ProcessCPUWatcher(pid);// get actual value when needed
double percent = cpuWatcher.current();
```## Examples
### [Total usage](examples/totalusage.d)
Prints total amount of virtual and physical memory (in bytes) and their current usage in the system (in percents).
dub examples/totalusage.d
### [Process usage](examples/processusage.d)
Prints amount of virtual and physical memory currently used by process, in bytes.
dub examples/processusage.d `pidof process`
### [CPU Watcher](examples/cpuwatcher.d)
All following examples show CPU time used by a system or by a process in the 0-100% range.
Watch system CPU time:
dub examples/cpuwatcher.d
Watch process CPU time:
dub examples/cpuwatcher.d `pidof process`
Spawn process and watch for its CPU time:
dub examples/cpuwatcher.d --spawn firefox
Adjust the rate of output:
dub examples/cpuwatcher.d --rate=1 --spawn firefox
### [CPU self watcher](examples/cpuselfwatcher.d)
Consume CPU time and report CPU usage by this process:
dub examples/cpuselfwatcher --threads=2
E.g. if you have 4 cores and run this example with 2 threads it will report 50% CPU time.
## Platform notes and implementation details
### Windows
In order to provide some functionality **resusage** dynamically loads the following libraries at startup:
1. [Psapi.dll](https://docs.microsoft.com/en-us/windows/win32/psapi/psapi-reference) for [GetProcessMemoryInfo](https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx).
2. [Pdh.dll](https://msdn.microsoft.com/en-us/library/windows/desktop/aa373083(v=vs.85).aspx) to calculate CPU time used by system.If Psapi.dll or Pdh.dll could not be loaded, corresponding functions will always throw *WindowsException*.
### Linux
Uses [sysinfo](https://linux.die.net/man/2/sysinfo), [clock_gettime](https://linux.die.net/man/3/clock_gettime) and proc stats.
### FreeBSD
Uses [sysctl](https://www.freebsd.org/cgi/man.cgi?query=sysctl&apropos=0&sektion=3&arch=default&format=html) to get RAM and
[libkvm](https://www.freebsd.org/cgi/man.cgi?query=kvm_open&apropos=0&sektion=3&arch=default&format=html) to get swap memory to calculate virtual memory.
Uses clock_gettime to evaluate CPU usage.