-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcpu_info.c
More file actions
194 lines (171 loc) · 5.73 KB
/
cpu_info.c
File metadata and controls
194 lines (171 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*------------------------------------------------------------------------
* cpu_info.c
* System CPU information
*
* Copyright (c) 2020, EnterpriseDB Corporation. All Rights Reserved.
*
*------------------------------------------------------------------------
*/
#include "postgres.h"
#include "system_stats.h"
#include <windows.h>
#include <wbemidl.h>
void ReadCPUInformation(Tuplestorestate *tupstore, TupleDesc tupdesc)
{
Datum values[Natts_cpu_info];
bool nulls[Natts_cpu_info];
int no_physical_cpus = 0;
memset(nulls, 0, sizeof(nulls));
// result code from COM calls from program
HRESULT hres = 0;
IEnumWbemClassObject *results = NULL;
BSTR query = SysAllocString(L"SELECT Caption, Manufacturer, Name, ProcessorType, Architecture,MaxClockSpeed, L2CacheSize, L3CacheSize, NumberOfCores, NumberOfLogicalProcessors FROM Win32_Processor");
// issue a WMI query
results = execute_query(query);
/* list the query results */
if (results != NULL)
{
IWbemClassObject *result = NULL;
ULONG returnedCount = 0;
// enumerate the retrieved objects
while ((hres = results->lpVtbl->Next(results, WBEM_INFINITE, 1, &result, &returnedCount)) == S_OK)
{
VARIANT query_result;
int wstr_length = 0;
char *dst = NULL;
size_t charsConverted = 0;
no_physical_cpus++;
hres = result->lpVtbl->Get(result, L"Caption", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_cpu_description] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_cpu_description] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
values[Anum_cpu_description] = CStringGetTextDatum(dst);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"Manufacturer", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_cpu_vendor] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_cpu_vendor] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
values[Anum_cpu_vendor] = CStringGetTextDatum(dst);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"Name", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_model_name] = true;
else
{
wstr_length = 0;
charsConverted = 0;
wstr_length = SysStringLen(query_result.bstrVal);
if (wstr_length == 0)
nulls[Anum_model_name] = true;
else
{
dst = (char *)malloc(wstr_length + 10);
memset(dst, 0x00, (wstr_length + 10));
wcstombs_s(&charsConverted, dst, wstr_length + 10, query_result.bstrVal, wstr_length);
values[Anum_model_name] = CStringGetTextDatum(dst);
free(dst);
}
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"ProcessorType", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_processor_type] = true;
else {
values[Anum_processor_type] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"MaxClockSpeed", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_cpu_clock_speed] = true;
else
{
/* convert MHz to Hz */
uint64 max_clock_speed = (uint64)((uint64)(query_result.intVal) * (uint64)1000000);
values[Anum_cpu_clock_speed] = UInt64GetDatum(max_clock_speed);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"Architecture", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_architecture] = true;
else
{
int val = query_result.intVal;
char arch[MAXPGPATH];
memset(arch, 0x00, MAXPGPATH);
snprintf(arch, MAXPGPATH, "%d", val);
values[Anum_architecture] = CStringGetTextDatum(arch);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"L2CacheSize", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_l2cache_size] = true;
else {
values[Anum_l2cache_size] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"L3CacheSize", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_l3cache_size] = true;
else {
values[Anum_l3cache_size] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"NumberOfCores", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_no_of_cores] = true;
else {
values[Anum_no_of_cores] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
hres = result->lpVtbl->Get(result, L"NumberOfLogicalProcessors", 0, &query_result, 0, 0);
if (FAILED(hres))
nulls[Anum_logical_processor] = true;
else {
values[Anum_logical_processor] = Int32GetDatum(query_result.intVal);
VariantClear(&query_result);
}
/* release the current result object */
result->lpVtbl->Release(result);
}
nulls[Anum_l1dcache_size] = true;
nulls[Anum_l1icache_size] = true;
nulls[Anum_cpu_type] = true;
nulls[Anum_cpu_family] = true;
nulls[Anum_cpu_byte_order] = true;
values[Anum_physical_processor] = Int32GetDatum(no_physical_cpus);
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
/* release results set */
results->lpVtbl->Release(results);
}
else
ereport(DEBUG1, (errmsg("[ReadCPUInformation]: Failed to get query result")));
SysFreeString(query);
}