Virtual memory, RAM, CPU usage by system or process
To use this package, put the following dependency into your project's dependencies section:
This package provides sub packages which can be used individually:
resusage:totalusage - A minimal D application.
resusage:processusage - A minimal D application.
resusage:cpuwatcher - A minimal D application.
resusage:cpuselfwatcher - A minimal D application.
Obtaining of virtual memory, RAM and CPU usage by the whole system or by single process.
Currently works on Linux and Windows.
FreeBSD support is partial - only system-wide memory information and per-process CPU usage can be retrieved now.
dub build --build=docs
dub build --build=ddox
// 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();
Prints total amount of virtual and physical memory (in bytes) and their current usage in the system (in percents).
dub run resusage:totalusage
Prints amount of virtual and physical memory currently used by process, in bytes.
dub run resusage:processusage -- `pidof process`
Watch system CPU time:
dub run resusage:cpuwatcher
Watch process CPU time:
dub run resusage:cpuwatcher -- `pidof process`
Spawn prcoess and watch for its CPU time:
dub run resusage:cpuwatcher -- --spawn firefox
Adjust the rate of output:
dub run resusage:cpuwatcher -- --rate=1 --spawn firefox
Consume CPU time and report CPU usage by this process:
dub run :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
In order to provide some functionality resusage dynamically loads the following libraries at startup:
- GetProcessMemoryInfo to get memory (physical and virtual) used by specific process.
- Pdh.dll to calculate CPU time used by system.
If Psapi.dll or Pdh.dll could not be loaded, corresponding functions will always throw WindowsException.
- Registered by Roman Chistokhodov
- Roman Chistokhodov
- Sub packages:
- resusage:totalusage, resusage:processusage, resusage:cpuwatcher, resusage:cpuselfwatcher
0.2.7 2017-Jul-01 0.2.6 2017-Jun-30 0.2.5 2017-Jun-30 0.2.4 2017-Mar-31 0.2.3 2016-Nov-02
- Download Stats:
8 downloads today
79 downloads this week
365 downloads this month
3367 downloads total