Debian Codegraph @

psutil/arch/windows/wmi.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * Functions related to the Windows Management Instrumentation API.
7 */
8
9 #include <Python.h>
10 #include <windows.h>
11 #include <pdh.h>
12
13 #include "../../arch/all/init.h"
14
15
16 // We use an exponentially weighted moving average, just like Unix systems do
17 // https://en.wikipedia.org/wiki/Load_(computing)#Unix-style_load_calculation
18 //
19 // These constants serve as the damping factor and are calculated with
20 // 1 / exp(sampling interval in seconds / window size in seconds)
21 //
22 // This formula comes from linux's include/linux/sched/loadavg.h
23 // https://github.com/torvalds/linux/blob/345671ea0f9258f410eb057b9ced9cefbbe5dc78/include/linux/sched/loadavg.h#L20-L23
24 #define LOADAVG_FACTOR_1F 0.9200444146293232478931553241
25 #define LOADAVG_FACTOR_5F 0.9834714538216174894737477501
26 #define LOADAVG_FACTOR_15F 0.9944598480048967508795473394
27 // The time interval in seconds between taking load counts, same as Linux
28 #define SAMPLING_INTERVAL 5
29
30 double load_avg_1m = 0;
31 double load_avg_5m = 0;
32 double load_avg_15m = 0;
33
34 #ifdef Py_GIL_DISABLED
35 static PyMutex mutex;
36 #define MUTEX_LOCK(m) PyMutex_Lock(m)
37 #define MUTEX_UNLOCK(m) PyMutex_Unlock(m)
38 #else
39 #define MUTEX_LOCK(m)
40 #define MUTEX_UNLOCK(m)
41 #endif
42
43
44 VOID CALLBACK LoadAvgCallback(PVOID hCounter, BOOLEAN timedOut) {
45 PDH_FMT_COUNTERVALUE displayValue;
46 double currentLoad;
47 PDH_STATUS err;
48
49 err = PdhGetFormattedCounterValue(
50 (PDH_HCOUNTER)hCounter, PDH_FMT_DOUBLE, 0, &displayValue);
51 // Skip updating the load if we can't get the value successfully
52 if (err != ERROR_SUCCESS) {
53 return;
54 }
55 currentLoad = displayValue.doubleValue;
56
57 MUTEX_LOCK(&mutex);
58 load_avg_1m = load_avg_1m * LOADAVG_FACTOR_1F + currentLoad * \
59 (1.0 - LOADAVG_FACTOR_1F);
60 load_avg_5m = load_avg_5m * LOADAVG_FACTOR_5F + currentLoad * \
61 (1.0 - LOADAVG_FACTOR_5F);
62 load_avg_15m = load_avg_15m * LOADAVG_FACTOR_15F + currentLoad * \
63 (1.0 - LOADAVG_FACTOR_15F);
64 MUTEX_UNLOCK(&mutex);
65 }
66
67
68 PyObject *
69 psutil_init_loadavg_counter(PyObject *self, PyObject *args) {
70 WCHAR *szCounterPath = L"\\System\\Processor Queue Length";
71 PDH_STATUS s;
72 BOOL ret;
73 HQUERY hQuery;
74 HCOUNTER hCounter;
75 HANDLE event;
76 HANDLE waitHandle;
77
78 if ((PdhOpenQueryW(NULL, 0, &hQuery)) != ERROR_SUCCESS) {
79 PyErr_Format(PyExc_RuntimeError, "PdhOpenQueryW failed");
80 return NULL;
81 }
82
83 s = PdhAddEnglishCounterW(hQuery, szCounterPath, 0, &hCounter);
84 if (s != ERROR_SUCCESS) {
85 PyErr_Format(
86 PyExc_RuntimeError,
87 "PdhAddEnglishCounterW failed. Performance counters may be disabled."
88 );
89 return NULL;
90 }
91
92 event = CreateEventW(NULL, FALSE, FALSE, L"LoadUpdateEvent");
93 if (event == NULL) {
94 psutil_PyErr_SetFromOSErrnoWithSyscall("CreateEventW");
95 return NULL;
96 }
97
98 s = PdhCollectQueryDataEx(hQuery, SAMPLING_INTERVAL, event);
99 if (s != ERROR_SUCCESS) {
100 PyErr_Format(PyExc_RuntimeError, "PdhCollectQueryDataEx failed");
101 return NULL;
102 }
103
104 ret = RegisterWaitForSingleObject(
105 &waitHandle,
106 event,
107 (WAITORTIMERCALLBACK)LoadAvgCallback,
108 (PVOID)
109 hCounter,
110 INFINITE,
111 WT_EXECUTEDEFAULT);
112
113 if (ret == 0) {
114 psutil_PyErr_SetFromOSErrnoWithSyscall("RegisterWaitForSingleObject");
115 return NULL;
116 }
117
118 Py_RETURN_NONE;
119 }
120
121
122 /*
123 * Gets the emulated 1 minute, 5 minute and 15 minute load averages
124 * (processor queue length) for the system.
125 * `init_loadavg_counter` must be called before this function to engage the
126 * mechanism that records load values.
127 */
128 PyObject *
129 psutil_get_loadavg(PyObject *self, PyObject *args) {
130 MUTEX_LOCK(&mutex);
131 double load_avg_1m_l = load_avg_1m;
132 double load_avg_5m_l = load_avg_5m;
133 double load_avg_15m_l = load_avg_15m;
134 MUTEX_UNLOCK(&mutex);
135 return Py_BuildValue("(ddd)", load_avg_1m_l, load_avg_5m_l, load_avg_15m_l);
136 }