Files
yttrium-virtio-gpu/Balloon/app/memstat.cpp
T
Ladi Prosek 08c0ce54af balloon: Static analysis: blnsvr fixes
Static analysis was failing with:

app\device.cpp(190): warning C6258: Using TerminateThread does not allow proper thread clean up.
app\main.cpp(30): warning C28285: For function 'wmain' '_Param_(2)' syntax error in 'SAL_readableTo(elementCount(Argc))' near 'Argc))'.
app\main.cpp(51): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\memstat.cpp(28): warning C6214: Cast between semantically different integer types:  HRESULT to a Boolean type.
app\memstat.cpp(162): warning C6386: Buffer overrun while writing to 'this->m_Stats':  the writable size is '70' bytes, but '80' bytes might be written.
app\service.cpp(231): warning C6031: Return value ignored: 'QueryServiceStatus'.
app\service.cpp(257): warning C6001: Using uninitialized memory 'CurrentState'.
app\utils.cpp(28): warning C6387: 'pLog' could be '0':  this does not adhere to the specification for the function 'fprintf'.
app\utils.cpp(29): warning C6387: 'pLog' could be '0':  this does not adhere to the specification for the function 'fclose'. See line 28 for an earlier location where this can occur
app\utils.cpp(104): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 81 for an earlier location where this can occur
app\utils.cpp(80): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CreateServiceW'.
app\utils.cpp(140): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'DeleteService'. See line 126 for an earlier location where this can occur
app\utils.cpp(147): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 126 for an earlier location where this can occur
app\utils.cpp(148): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 121 for an earlier location where this can occur
app\utils.cpp(133): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ControlService'. See line 126 for an earlier location where this can occur
app\utils.cpp(121): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(126): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'QueryServiceStatus'.
app\utils.cpp(178): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(197): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(200): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(221): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 167 for an earlier location where this can occur
app\utils.cpp(167): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(255): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 238 for an earlier location where this can occur
app\utils.cpp(254): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'.
app\utils.cpp(238): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(245): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ControlService'.
app\utils.cpp(248): warning C6001: Using uninitialized memory 'res'.
app\utils.cpp(279): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'QueryServiceConfigW'.
app\utils.cpp(273): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'.
app\utils.cpp(292): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 279 for an earlier location where this can occur
app\utils.cpp(293): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 273 for an earlier location where this can occur
app\utils.cpp(284): warning C28182: Dereferencing NULL pointer. 'buffer' contains the same NULL value as 'LocalAlloc()`278' did.
app\utils.cpp(317): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'ChangeServiceConfigW'.
app\utils.cpp(339): warning C6387: 'service' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 318 for an earlier location where this can occur
app\utils.cpp(335): warning C6387: 'lock' could be '0':  this does not adhere to the specification for the function 'UnlockServiceDatabase'.
app\utils.cpp(340): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'CloseServiceHandle'. See line 309 for an earlier location where this can occur
app\utils.cpp(309): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'LockServiceDatabase'.
app\utils.cpp(313): warning C6387: 'scm' could be '0':  this does not adhere to the specification for the function 'OpenServiceW'. See line 309 for an earlier location where this can occur

This commit fixes everything except for:

app\device.cpp(190): warning C6258: Using TerminateThread does not allow proper thread clean up.
app\utils.cpp(178): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(197): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem
app\utils.cpp(200): warning C28159: Consider using 'GetTickCount64' instead of 'GetTickCount'. Reason: GetTickCount overflows roughly every 49 days.  Code that does not take that into account can loop indefinitely.  GetTickCount64 operates on 64 bit values and does not have that problem

Signed-off-by: Ladi Prosek <[email protected]>
2017-12-16 21:35:58 +02:00

228 lines
6.6 KiB
C++
Executable File

#include "stdafx.h"
CMemStat::CMemStat()
{
initialized = FALSE;
locator = NULL;
service = NULL;
memset(m_Stats, -1, sizeof(m_Stats));
}
CMemStat::~CMemStat()
{
service = NULL;
locator = NULL;
if (initialized == TRUE) {
CoUninitialize();
}
}
BOOL CMemStat::Init()
{
HRESULT status = S_OK;
status = CoInitializeEx(NULL,
COINIT_MULTITHREADED);
if (FAILED(status)) {
PrintMessage("Cannot initialize COM");
return FALSE;
}
initialized = TRUE;
status = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
0
);
if (FAILED(status)) {
PrintMessage("Cannot initialize security");
return FALSE;
}
status = CoCreateInstance(
CLSID_WbemLocator,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
reinterpret_cast< void** >( &locator )
);
if (FAILED(status)) {
PrintMessage("Cannot create instance");
return FALSE;
}
status = locator->ConnectServer(
L"root\\cimv2",
NULL,
NULL,
0L,
0L,
NULL,
NULL,
&service
);
if (FAILED(status)) {
PrintMessage("Cannot connect to wmi server");
return FALSE;
}
status = CoSetProxyBlanket(service,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(status)) {
PrintMessage("Cannot set proxy blanket");
return FALSE;
}
return TRUE;
}
BOOL CMemStat::Update()
{
SYSTEM_INFO sysinfo;
MEMORYSTATUSEX statex = {sizeof(statex)};
CComPtr< IEnumWbemClassObject > enumerator;
CComPtr< IWbemClassObject > memory;
ULONG retcnt;
_variant_t var_val;
HRESULT status = S_OK;
UINT idx = 0;
SIZE_T minCacheSize = 0;
SIZE_T maxCacheSize = 0;
DWORD flags = 0;
GetSystemInfo(&sysinfo);
status = service->ExecQuery(
L"WQL",
L"SELECT * FROM Win32_PerfRawData_PerfOS_Memory",
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&enumerator
);
if (FAILED(status)) {
PrintMessage("Cannot execute query");
return FALSE;
}
if (enumerator == NULL || FAILED(enumerator->Next(
WBEM_INFINITE,
1L,
&memory,
&retcnt))) {
PrintMessage("Cannot enumerate results");
return FALSE;
}
if (retcnt > 0) {
status = memory->Get(
L"PagesInputPerSec",
0,
&var_val,
NULL,
NULL
);
if (FAILED(status) || (var_val.vt == VT_NULL)) {
PrintMessage("Cannot get PagesInputPerSec");
var_val = (__int64)-1;
}
m_Stats[idx].tag = VIRTIO_BALLOON_S_SWAP_IN;
m_Stats[idx].val = (__int64)var_val * sysinfo.dwPageSize;
idx++;
status = memory->Get(
L"PagesOutputPerSec",
0,
&var_val,
NULL,
NULL
);
if (FAILED(status) || (var_val.vt == VT_NULL)) {
PrintMessage("Cannot get PagesOutputPerSec");
var_val = (__int64)-1;
}
m_Stats[idx].tag = VIRTIO_BALLOON_S_SWAP_OUT;
m_Stats[idx].val = (__int64)var_val * sysinfo.dwPageSize;
idx++;
status = memory->Get(
L"PageReadsPerSec",
0,
&var_val,
NULL,
NULL
);
if (FAILED(status) || (var_val.vt == VT_NULL)) {
PrintMessage("Cannot get PageReadsPerSec");
var_val = (__int64)-1;
}
m_Stats[idx].tag = VIRTIO_BALLOON_S_MAJFLT;
m_Stats[idx].val = (long)var_val;
idx++;
status = memory->Get(
L"PageFaultsPerSec",
0,
&var_val,
NULL,
NULL
);
if (FAILED(status) || (var_val.vt == VT_NULL)) {
PrintMessage("Cannot get PageFaultsPerSec");
var_val = (__int64)-1;
}
m_Stats[idx].tag = VIRTIO_BALLOON_S_MINFLT;
m_Stats[idx].val = (long)var_val;
idx++;
GlobalMemoryStatusEx(&statex);
m_Stats[idx].tag = VIRTIO_BALLOON_S_MEMFREE;
m_Stats[idx].val = statex.ullAvailPhys;
idx++;
m_Stats[idx].tag = VIRTIO_BALLOON_S_MEMTOT;
m_Stats[idx].val = statex.ullTotalPhys;
idx++;
status = memory->Get(
L"CacheBytes",
0,
&var_val,
NULL,
NULL
);
if (FAILED(status) || (var_val.vt == VT_NULL)) {
PrintMessage("Cannot get CacheBytes");
var_val.vt = 0;
}
else if (GetSystemFileCacheSize(&minCacheSize, &maxCacheSize, &flags) &&
(flags & FILE_CACHE_MIN_HARD_ENABLE) &&
((ULONGLONG)var_val > minCacheSize)) {
var_val = (ULONGLONG)var_val - minCacheSize;
}
m_Stats[idx].tag = VIRTIO_BALLOON_S_AVAIL;
m_Stats[idx].val = statex.ullAvailPhys + (ULONGLONG)var_val/2;
}
return TRUE;
}