Skip to content

Commit

Permalink
Move psutil_percent_swap to mem.c
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Apr 12, 2023
1 parent aa910e2 commit e3ce8a3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 54 deletions.
50 changes: 50 additions & 0 deletions psutil/arch/windows/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <Python.h>
#include <windows.h>
#include <Psapi.h>
#include <pdh.h>

#include "../../_psutil_common.h"

Expand Down Expand Up @@ -41,3 +42,52 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
totalSys,
availSys);
}

/*
* Return a Python float representing the percent usage of all paging files on
* the system.
*/
PyObject *
psutil_swap_percent(PyObject *self, PyObject *args) {
WCHAR *szCounterPath = L"\\Paging File(_Total)\\% Usage";
PDH_STATUS s;
HQUERY hQuery;
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE counterValue;
double percentUsage;

if ((PdhOpenQueryW(NULL, 0, &hQuery)) != ERROR_SUCCESS) {
PyErr_Format(PyExc_RuntimeError, "PdhOpenQueryW failed");
return NULL;
}

s = PdhAddEnglishCounterW(hQuery, szCounterPath, 0, &hCounter);
if (s != ERROR_SUCCESS) {
PdhCloseQuery(hQuery);
PyErr_Format(
PyExc_RuntimeError,
"PdhAddEnglishCounterW failed. Performance counters may be disabled."
);
return NULL;
}

s = PdhCollectQueryData(hQuery);
if (s != ERROR_SUCCESS) {
// If swap disabled this will fail
percentUsage = 0;
} else {
s = PdhGetFormattedCounterValue(
(PDH_HCOUNTER)hCounter, PDH_FMT_DOUBLE, 0, &counterValue);
if (s != ERROR_SUCCESS) {
percentUsage = 0;
} else {
percentUsage = counterValue.doubleValue;
}
}

PdhRemoveCounter(hCounter);

PdhCloseQuery(hQuery);

return Py_BuildValue("d", percentUsage);
}
1 change: 1 addition & 0 deletions psutil/arch/windows/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@

PyObject *psutil_getpagesize(PyObject *self, PyObject *args);
PyObject *psutil_virtual_mem(PyObject *self, PyObject *args);
PyObject *psutil_swap_percent(PyObject *self, PyObject *args);
53 changes: 0 additions & 53 deletions psutil/arch/windows/wmi.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,56 +121,3 @@ PyObject *
psutil_get_loadavg(PyObject *self, PyObject *args) {
return Py_BuildValue("(ddd)", load_avg_1m, load_avg_5m, load_avg_15m);
}

/*
* Percent swap implementation using "\Paging File(_Total)\% Usage" counter
*/

/*
* Return a Python float representing the percent usage of all paging files on
* the system.
*/
PyObject *
psutil_swap_percent(PyObject *self, PyObject *args) {
WCHAR *szCounterPath = L"\\Paging File(_Total)\\% Usage";
PDH_STATUS s;
HQUERY hQuery;
HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE counterValue;
double percentUsage;

if ((PdhOpenQueryW(NULL, 0, &hQuery)) != ERROR_SUCCESS) {
PyErr_Format(PyExc_RuntimeError, "PdhOpenQueryW failed");
return NULL;
}

s = PdhAddEnglishCounterW(hQuery, szCounterPath, 0, &hCounter);
if (s != ERROR_SUCCESS) {
PdhCloseQuery(hQuery);
PyErr_Format(
PyExc_RuntimeError,
"PdhAddEnglishCounterW failed. Performance counters may be disabled."
);
return NULL;
}

s = PdhCollectQueryData(hQuery);
if (s != ERROR_SUCCESS) {
// If swap disabled this will fail
percentUsage = 0;
} else {
s = PdhGetFormattedCounterValue(
(PDH_HCOUNTER)hCounter, PDH_FMT_DOUBLE, 0, &counterValue);
if (s != ERROR_SUCCESS) {
percentUsage = 0;
} else {
percentUsage = counterValue.doubleValue;
}
}

PdhRemoveCounter(hCounter);

PdhCloseQuery(hQuery);

return Py_BuildValue("d", percentUsage);
}
1 change: 0 additions & 1 deletion psutil/arch/windows/wmi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@

PyObject* psutil_init_loadavg_counter();
PyObject* psutil_get_loadavg();
PyObject* psutil_swap_percent();

0 comments on commit e3ce8a3

Please sign in to comment.